Fix string format of field
[bitfield] / bitfield
index 1152067ebe1199c703779f8c6c61ff1afbe9da3e..da2b94b7f294fe23c24a864da5f502e955b486ed 100644 (file)
--- a/bitfield
+++ b/bitfield
@@ -47,10 +47,6 @@ class bitfield:
                        return self.values[value]
                return None
 
-       def __str__(self):
-               return "[%2d:%-2d] %s 0x%x" % (int(self.start_bit),
-                       int(self.end_bit), self.name, self.mask())
-
        @staticmethod
        def parse_bitfield(line):
                a = line.split(None, 1)
@@ -83,27 +79,23 @@ class register:
        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" \
                                        % (name_width, field.name, v)
                return str
 
-       def __str__(self):
-               str = self.name + "\n"
-               for f in self.fields:
-                       str += "\t%s\n" % f
-               return str
-
 def list_regs(regs):
        for (id, r) in regs.iteritems():
                print "%18s : %s" % (id, r.name)
@@ -220,8 +212,8 @@ def usage(prog):
 
 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
@@ -237,6 +229,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])
@@ -250,6 +245,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])
@@ -273,7 +270,7 @@ def main():
 
        for value in values:
                i = long(value.strip(), 0)
-               print r.decode(i)
+               print r.decode(i, options['non-zero'])
 
        return 0