Use env to find python
[bitfield] / bitfield
index 5ade7af4fac6e03832e4d294cc7bae86aa14e284..ae5b1380b48ced412f579c13566f07164df08edf 100644 (file)
--- a/bitfield
+++ b/bitfield
@@ -1,4 +1,4 @@
-#!/usr/bin/python2.4
+#!/usr/bin/env python
 #
 # Utility to decode register values
 # Copyright (c) 2006 Jeremy Kerr <jk@ozlabs.org>
@@ -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)
@@ -107,7 +125,7 @@ class register:
                                str += "%*s: 0x%x\n" \
                                        % (name_width, field.name, v)
                return str
-       
+
        def bit_number(self, number):
                if self.bit_order == self.bit_0_is_lsb:
                        number = self.width - 1 - number
@@ -178,12 +196,14 @@ def parse_config(bnf, regs, file):
                                                + "fields in %s") % id)
 
                                order_str = t[1].strip().lower()
+                               order_str = order_str.replace(' ', '-')
+
                                if order_str not in order_map.keys():
                                        raise ConfigurationError(file,
                                                "Invalid bit order %s in %s" % \
                                                (order_str, id))
                                reg.bit_order = order_map[order_str]
-                               
+
                        elif t[0] == 'alias':
                                alias_id = t[1].strip()
 
@@ -329,7 +349,7 @@ def main():
                value_iter = args.__iter__()
        else:
                value_iter = iter(sys.stdin.readline, '')
-               
+
        try:
                for value in value_iter:
                        decode_value(reg, value.strip(), options)