11import logging
2- import construct
32
43LOGGER = logging .getLogger (__name__ )
54
65
76class Bitfield (object ):
8- """Wraps contruct's BitStruct class. It describes a chunk of memory that
9- consists of a number of fields.
107 """
11- def __init__ (self , name , width , fields = None ):
8+ Describes a chunk of memory that consists of a number of Fields.
9+ """
10+ def __init__ (self , name , width_bits , fields = None ):
1211 self .name = name
13- self .width = width
12+ self .width_bits = width_bits
1413 self ._fields = {}
15- self .bitstruct = None
1614 if fields is not None :
1715 self .fields_add (fields )
1816 LOGGER .debug ('New Bitfield(%s) with %i fields' % (self .name , len (self ._fields )))
@@ -21,12 +19,14 @@ def __init__(self, name, width, fields=None):
2119 # return self._fields.keys()
2220
2321 def fields_clear (self ):
24- """Reset the fields in this bitstruct.
22+ """
23+ Reset the fields in this bitstruct.
2524 """
2625 self ._fields = {}
2726
2827 def fields_add (self , fields ):
29- """Add a dictionary of Fields to this bitfield.
28+ """
29+ Add a dictionary of Fields to this bitfield.
3030 """
3131 if not isinstance (fields , dict ):
3232 raise TypeError ('fields should be a dictionary of Field objects.' )
@@ -36,55 +36,34 @@ def fields_add(self, fields):
3636 self .field_add (newfield )
3737
3838 def field_add (self , newfield , auto_offset = False ):
39- """Add a Field to this bitfield.
39+ """
40+ Add a Field to this bitfield.
4041 """
4142 if not isinstance (newfield , Field ):
4243 raise TypeError ('Expecting Field object.' )
4344 # add it at the end of the current fields
4445 if auto_offset :
4546 width = 0
4647 for field in self ._fields .itervalues ():
47- width += field .width
48+ width += field .width_bits
4849 newfield .offset = width
4950 self ._fields [newfield .name ] = newfield
50- self ._update_bitstruct ()
51-
52- def _update_bitstruct (self ):
53- """
54- Update this Bitfield's bitstruct after a field has been modified.
55- """
56- # need it msb -> lsb
57- # print 100 * '#'
58- fields = sorted (self ._fields .itervalues (), key = lambda x : x .offset , reverse = True )
59- field_width_sum = sum (f .width for f in self ._fields .itervalues ())
60- # print fields
61- # print field_width_sum
62- # print 100 * '#'
63- bs = construct .BitStruct (self .name , construct .Padding (self .width - field_width_sum ))
64- for f in fields :
65- if f .width == 1 :
66- newfield = construct .Flag (f .name )
67- else :
68- newfield = construct .BitField (f .name , f .width )
69- bs .subcon .subcons = bs .subcon .subcons + (newfield ,)
70- self .bitstruct = bs
7151
7252 def field_names (self ):
7353 return self ._fields .keys ()
7454
75- def names (self ):
76- return self ._fields .keys ()
77-
7855 def field_get_by_name (self , fieldname ):
79- """Get a field from this bitfield by its name.
56+ """
57+ Get a field from this bitfield by its name.
8058 """
8159 try :
8260 return self ._fields [fieldname ]
8361 except KeyError :
8462 return None
8563
8664 def fields_string_get (self ):
87- """Get a string of all the field names.
65+ """
66+ Get a string of all the field names.
8867 """
8968 fieldstring = ''
9069 for field in self ._fields .itervalues ():
@@ -93,38 +72,43 @@ def fields_string_get(self):
9372 return fieldstring
9473
9574 def __str__ (self ):
96- """Return a string representation of this object.
9775 """
98- rv = self .name + '(' + str (self .width ) + ',['
76+ Return a string representation of this object.
77+ """
78+ rv = self .name + '(' + str (self .width_bits ) + ',['
9979 rv = rv + self .fields_string_get () + '])'
10080 return rv
10181
10282
10383class Field (object ):
104- """A Field object is a number of bits somewhere in a Bitfield object.
10584 """
106- def __init__ (self , name , numtype , width , binary_pt , lsb_offset ):
85+ A Field object is a number of bits somewhere in a Bitfield object.
86+ """
87+ def __init__ (self , name , numtype , width_bits , binary_pt , lsb_offset ):
10788 """
108- Initialise a field object.
109- @param name The name of the field
110- @param numtype A numerical description of the type - 0 is unsigned,
111- 1 is signed 2's comp and 2 is boolean
112- @param width The width of the field, in bits
113- @param binary_pt The binary point position, in bits
114- @param lsb_offset The offset in the memory field, in bits - -1 means it hasn't been set yet.
89+ Initialise a Field object.
90+ :param name: The name of the field
91+ :param numtype: A numerical description of the type:
92+ 0 is unsigned, 1 is signed 2's comp and 2 is boolean
93+ :param width: The width of the field, in bits
94+ :param binary_pt: The binary point position, in bits
95+ :param lsb_offset: The offset in the memory field, in bits:
96+ -1 means it hasn't been set yet.
97+ :return:
11598 """
11699 if not isinstance (numtype , int ):
117100 raise TypeError ('Type must be an integer.' )
118101 assert name .strip () != '' , 'Cannot have a Field with empty name?!'
119102 self .name = name
120103 self .numtype = numtype
121- self .width = width
104+ self .width_bits = width_bits
122105 self .binary_pt = binary_pt
123106 self .offset = lsb_offset
124107
125108 def __str__ (self ):
126- return '%s(%i,%i,%i,%i)' % (self .name , self .offset , self .width , self .binary_pt , self .numtype )
109+ return '%s(%i,%i,%i,%i)' % (self .name , self .offset , self .width_bits ,
110+ self .binary_pt , self .numtype )
127111
128112 def __repr__ (self ):
129- return '%s(%i,%i,%i,%i)' % ( self . name , self . offset , self . width , self . binary_pt , self . numtype )
113+ return str ( self )
130114# end
0 commit comments