Use env to find python
[bitfield] / bitfield
index c70c6c6af86f2682906936f7d5338245031eb87d..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>
@@ -15,8 +15,9 @@ from getopt import getopt, GetoptError
 
 # List of paths to look for configuration files. If a directory is specified,
 # it will be (recursively) scanned for .conf files.
-configs = [os.path.join(os.getenv("HOME"), ".bitfields.conf"),
-               os.path.join(os.getenv("HOME"), ".bitfields.d")]
+configs = ["/etc/bitfield.d", "/etc/bitfield",
+               os.path.join(os.getenv("HOME"), ".bitfield.d"),
+               os.path.join(os.getenv("HOME"), ".bitfield.conf")]
 
 class bitfield:
        def __init__(self, bits, name):
@@ -48,7 +49,16 @@ class bitfield:
                return None
 
        @staticmethod
-       def parse_bitfield(line):
+       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)
                if len(a) != 2:
                        return None
@@ -57,13 +67,24 @@ class bitfield:
                bits = []
                for s in range_str.split(','):
                        if ':' in s:
-                               (start, end) = s.split(':')
-                               bits.extend(range(int(start), int(end) + 1, 1))
+                               (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(int(s))
+                               bits.append(reg.bit_number(int(s)))
+
                return bitfield(bits, name)
 
+
+
        @staticmethod
        def parse_value(line):
                a = line.split(None, 1)
@@ -72,11 +93,16 @@ class bitfield:
                return a
 
 class register:
-       def __init__(self, id, name, width):
+       bit_0_is_msb = 0
+       bit_0_is_lsb = 1
+
+       def __init__(self, id):
                self.id = id
-               self.name = name
-               self.width = width
                self.fields = []
+               # set defaults
+               self.name = None
+               self.bit_order = self.bit_0_is_msb
+               self.width = 64
 
        def add_field(self, field):
                self.fields.append(field)
@@ -100,6 +126,11 @@ class register:
                                        % (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
+               return number
+
 def list_regs(regs):
        for (id, r) in regs.iteritems():
                print "%18s : %s" % (id, r.name)
@@ -118,6 +149,11 @@ def parse_config(bnf, regs, file):
 
        tokens = bnf.parseString(f.read())
 
+       order_map = {'bit-0-is-lsb':    register.bit_0_is_lsb,
+                       'bit-0-is-msb': register.bit_0_is_msb,
+                       'ibm':          register.bit_0_is_msb,
+                       'default':      register.bit_0_is_msb}
+
        for tok in tokens:
                ts = tok.asList()
                id = ts.pop(0)
@@ -126,19 +162,19 @@ def parse_config(bnf, regs, file):
                        raise ConfigurationError(file,
                                "Register %s is already defined" % id)
 
-               # default to 64 bit registers
-               width = 64
-               name = None
+               reg = register(id)
+
+               alias_id = None
                fields = []
 
                for t in ts:
                        if t[0] == 'name':
                                name = t[1]
-                               name = name.strip()
+                               reg.name = name.strip()
                        elif t[0] == 'width':
-                               width = int(t[1])
+                               reg.width = int(t[1])
                        elif t[0] == 'field':
-                               f = bitfield.parse_bitfield(t[1])
+                               f = bitfield.parse_bitfield(t[1], reg)
                                if f is None:
                                        raise ConfigurationError(file,
                                                "Invalid field in %s" % id)
@@ -153,8 +189,39 @@ def parse_config(bnf, regs, file):
                                                "Invalid value in %s" % id)
 
                                fields[-1].add_value(v[0], v[1])
+                       elif t[0] == 'order':
+                               if len(fields) != 0:
+                                       raise ConfigurationError(file,
+                                               ("bit order defined after " \
+                                               + "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()
 
-               if name is None or name == '':
+               if alias_id is not None:
+                       if reg.name is not None or fields != []:
+                               raise ConfigurationError(file, ("Definiton " \
+                                       + "for %s is an alias, but has other " \
+                                       + "attributes") % id)
+
+                       if not regs.has_key(alias_id):
+                               raise ConfigurationError(file, "Aliasing "
+                                       "non-existent register %s (from %s)" \
+                                       % (alias_id, id))
+
+                       reg = regs[alias_id]
+                       continue
+
+               if reg.name is None or reg.name == '':
                        raise ConfigurationError(file,
                                "No name for entry %s" % id)
 
@@ -162,11 +229,10 @@ def parse_config(bnf, regs, file):
                        raise ConfigurationError(file,
                                "Register %s has no fields" % id)
 
-               r = register(id, name, width)
                for f in fields:
-                       r.add_field(f)
+                       reg.add_field(f)
 
-               regs[id] = r
+               regs[id] = reg
 
 def parse_config_dir(data, dir, fnames):
        (bnf, regs) = data
@@ -226,7 +292,7 @@ def decode_value(reg, value, options):
                        "for %d-bit register '%s'") % (value, reg.width, reg.id)
                return
 
-       print reg.decode(i, options.has_key('non_zero'))
+       print reg.decode(i, options['non-zero'])
 
 
 def main():
@@ -283,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)