Add support for non-contiguous bitfields.
[bitfield] / bitfield
index e9be554734bb9ba37613cf9a4b3ca5ffafe41387..c70c6c6af86f2682906936f7d5338245031eb87d 100644 (file)
--- a/bitfield
+++ b/bitfield
@@ -19,28 +19,28 @@ configs = [os.path.join(os.getenv("HOME"), ".bitfields.conf"),
                os.path.join(os.getenv("HOME"), ".bitfields.d")]
 
 class bitfield:
-       def __init__(self, start_bit, end_bit, name):
-               self.start_bit = start_bit
-               self.end_bit = end_bit
+       def __init__(self, bits, name):
+               self.bits = bits
                self.name = name
                self.values = {}
 
-       def start_bit(self):
-               return self.start_bit
-
-       def end_bit(self):
-               return self.end_bit
-
        def width(self):
-               return 1 + self.end_bit - self.start_bit
+               return len(self.bits)
 
        def add_value(self, value, description):
                self.values[int(value)] = description
 
        def mask(self, reg_width, value):
-               shift = (reg_width - 1) - self.end_bit
-               return (((2 ** self.width() - 1) << (shift))
-                       & value) >> shift
+               ret = 0
+               out_len = len(self.bits)
+               for out_bit in range(0, out_len):
+                       in_bit = self.bits[out_bit]
+                       # shift this bit down to the LSB (and mask the rest)
+                       i = (value >> (reg_width - in_bit - 1)) & 1
+                       # shift back to the output position in the field
+                       i <<= out_len - out_bit - 1
+                       ret |= i
+               return ret
 
        def value(self, value):
                if value in self.values:
@@ -54,14 +54,15 @@ class bitfield:
                        return None
                (range_str, name) = a
 
-               range = (None,None)
-               if range_str.find(':') != -1:
-                       r = range_str.split(":")
-                       range = (int(r[0]),int(r[1]))
-               else:
-                       range = (int(range_str),int(range_str))
-
-               return bitfield(range[0], range[1], name)
+               bits = []
+               for s in range_str.split(','):
+                       if ':' in s:
+                               (start, end) = s.split(':')
+                               bits.extend(range(int(start), int(end) + 1, 1))
+                       else:
+                               bits.append(int(s))
+               return bitfield(bits, name)
 
        @staticmethod
        def parse_value(line):
@@ -71,23 +72,28 @@ class bitfield:
                return a
 
 class register:
-       def __init__(self, name, width):
+       def __init__(self, id, name, width):
+               self.id = id
                self.name = name
                self.width = width
                self.fields = []
 
-       def add_field(self, field,):
+       def add_field(self, field):
                self.fields.append(field)
 
-       def decode(self, value):
+       def decode(self, value, ignore_zero):
                field_width = (self.width + 3) / 4
                name_width = max(map(lambda f: len(f.name), self.fields))
+
                str = "0x%0*lx [%d]\n" % (field_width, value, value)
+
                for field in self.fields:
                        v = field.mask(self.width, value);
+                       if ignore_zero and v == 0:
+                               continue
                        desc = field.value(v)
                        if desc is not None:
-                               str += "%*s: 0x%s [%s]\n" \
+                               str += "%*s: 0x%x [%s]\n" \
                                        % (name_width, field.name, v, desc)
                        else:
                                str += "%*s: 0x%x\n" \
@@ -110,7 +116,7 @@ class ConfigurationError(Exception):
 def parse_config(bnf, regs, file):
        f = open(file)
 
-       tokens = bnf.parseString("".join(f.readlines()))
+       tokens = bnf.parseString(f.read())
 
        for tok in tokens:
                ts = tok.asList()
@@ -156,7 +162,7 @@ def parse_config(bnf, regs, file):
                        raise ConfigurationError(file,
                                "Register %s has no fields" % id)
 
-               r = register(name, width)
+               r = register(id, name, width)
                for f in fields:
                        r.add_field(f)
 
@@ -206,12 +212,27 @@ def parse_all_configs(configs):
        return regs
 
 def usage(prog):
-       print "Usage: %s <-l> | <-s pattern> | register [value...]" % prog
+       print "Usage: %s <-l> | <-s pattern> | [-n] register [value...]" % prog
+
+def decode_value(reg, value, options):
+       try:
+               i = long(value, 0)
+       except ValueError, e:
+               print "error: invalid value '%s'" % value
+               return
+
+       if i > ((1 << reg.width) - 1):
+               print ("error: value '%s' is too large " + \
+                       "for %d-bit register '%s'") % (value, reg.width, reg.id)
+               return
+
+       print reg.decode(i, options.has_key('non_zero'))
+
 
 def main():
        try:
-               (opts, args) = getopt(sys.argv[1:], "hls:", \
-                       ["help", "list", "search="])
+               (opts, args) = getopt(sys.argv[1:], "hlns:", \
+                       ["help", "list", "non-zero", "search="])
        except GetoptError:
                usage(sys.argv[0])
                return 1
@@ -227,6 +248,9 @@ def main():
                print "No configuration available"
                return 1
 
+       options = {}
+       options['non-zero'] = False
+
        for o, a in opts:
                if o in ("-h", "--help"):
                        usage(sys.argv[0])
@@ -240,6 +264,8 @@ def main():
                        list_regs(search_regs(regs, a))
                        return
 
+               if o in ("-n", "--non-zero"):
+                       options['non-zero'] = True
 
        if not args:
                usage(sys.argv[0])
@@ -250,20 +276,19 @@ def main():
                print "No such register '%s'" % reg_id
                return 1
 
-       r = regs[reg_id]
-       print "decoding as %s" % r.name
+       reg = regs[reg_id]
+       print "decoding as %s" % reg.name
 
        if args:
-               values = args
+               value_iter = args.__iter__()
        else:
-               try:
-                       values = sys.stdin.readlines()
-               except KeyboardInterrupt, e:
-                       return
-
-       for value in values:
-               i = long(value.strip(), 0)
-               print r.decode(i)
+               value_iter = iter(sys.stdin.readline, '')
+               
+       try:
+               for value in value_iter:
+                       decode_value(reg, value.strip(), options)
+       except KeyboardInterrupt, e:
+               pass
 
        return 0