Add support for mask<<shift field definitions
authorJeremy Kerr <jk@ozlabs.org>
Wed, 22 Nov 2006 03:34:24 +0000 (14:34 +1100)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 12 Nov 2009 23:58:46 +0000 (10:58 +1100)
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
ChangeLog
bitfield

index 79dbd74..f072e44 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,6 @@
 version ??
        * Allow alternate (bit 0 is lsb) bit numbering scheme
+       * Add support for mask<<shift field definitions
 
 version 0.2:
        * Add support for aliased definitions.
index 3b34aeb..8bb8d35 100644 (file)
--- a/bitfield
+++ b/bitfield
@@ -48,6 +48,15 @@ class bitfield:
                        return self.values[value]
                return None
 
+       @staticmethod
+       def mask_and_shift_to_bits(width, mask, shift):
+               bits = []
+               val = mask << shift
+               for i in range(0, width):
+                       if mask & (1 << i):
+                               bits.append(width - i - 1 - shift)
+               return bits
+
        @staticmethod
        def parse_bitfield(line, reg):
                a = line.split(None, 1)
@@ -58,15 +67,24 @@ class bitfield:
                bits = []
                for s in range_str.split(','):
                        if ':' in s:
-                               (start, end) = s.split(':')
+                               (start, end) = map( \
+                                       lambda s: reg.bit_number(int(s)),
+                                       s.split(':'))
                                start = reg.bit_number(int(start))
                                end = reg.bit_number(int(end))
                                bits.extend(range(start, end + 1, 1))
+                       elif '<<' in s:
+                               (mask, shift) = map(lambda s: int(s.strip()),
+                                       s.split('<<'))
+                               bits.extend(bitfield.mask_and_shift_to_bits( \
+                                       reg.width, mask, shift))
                        else:
                                bits.append(reg.bit_number(int(s)))
  
                return bitfield(bits, name)
 
+
+
        @staticmethod
        def parse_value(line):
                a = line.split(None, 1)