@@ -32,9 +32,6 @@ def output_power(v):
3232# an optional parser which interprets the field's content. Parser are
3333# expected to yield a single text string when they exist. Other types
3434# of output are passed to Python's .format() routine as is.
35- # - TODO Add support for the creation of formatted values, as well as
36- # optional warnings from register field parsers? The current approach
37- # lets invalid register content go unnoticed.
3835# - Bit fields' width in registers determines the range of indices in
3936# table/tuple lookups. Keep the implementation as robust as possible
4037# during future maintenance. Avoid Python runtime errors when adjusting
@@ -44,8 +41,9 @@ def output_power(v):
4441 # offset, width, name, parser.
4542 0 : (
4643 ( 3 , 12 , 'FRAC' ),
47- # Lower limit is 23 not 32?
48- (15 , 16 , 'INT' , lambda v : 'Not Allowed' if v < 32 else v ),
44+ (15 , 16 , 'INT' ,
45+ None , lambda v : 'Not Allowed' if v < 23 else None ,
46+ ),
4947 ),
5048 1 : (
5149 ( 3 , 12 , 'MOD' ),
@@ -115,8 +113,7 @@ def output_power(v):
115113 ),
116114}
117115
118- ANN_REG = 0
119- ANN_WARN = 1
116+ ( ANN_REG , ANN_WARN , ) = range (2 )
120117
121118class Decoder (srd .Decoder ):
122119 api_version = 3
@@ -157,7 +154,7 @@ def decode_bits(self, offset, width):
157154 value = bitpack_lsb (bits , 0 )
158155 return ( value , ( ss , es , ))
159156
160- def decode_field (self , name , offset , width , parser = None ):
157+ def decode_field (self , name , offset , width , parser = None , checker = None ):
161158 '''Interpret a bit field. Emits an annotation.'''
162159 # Get the register field's content and position.
163160 val , ( ss , es , ) = self .decode_bits (offset , width )
@@ -169,6 +166,11 @@ def decode_field(self, name, offset, width, parser = None):
169166 text = ['{name}' .format (name = name )]
170167 if text :
171168 self .putg (ss , es , ANN_REG , text )
169+ # Have the field's content checked, emit an optional warning.
170+ warn = checker (val ) if checker else None
171+ if warn :
172+ text = ['{}' .format (warn )]
173+ self .putg (ss , es , ANN_WARN , text )
172174
173175 def decode_word (self , ss , es , bits ):
174176 '''Interpret a 32bit word after accumulation completes.'''
@@ -200,14 +202,17 @@ def decode_word(self, ss, es, bits):
200202 return
201203 for field_desc in field_descs :
202204 parser = None
205+ checker = None
203206 if len (field_desc ) == 3 :
204207 start , count , name , = field_desc
205208 elif len (field_desc ) == 4 :
206209 start , count , name , parser = field_desc
210+ elif len (field_desc ) == 5 :
211+ start , count , name , parser , checker = field_desc
207212 else :
208213 # Unsupported regs{} syntax, programmer's error.
209214 return
210- self .decode_field (name , start , count , parser )
215+ self .decode_field (name , start , count , parser , checker )
211216
212217 def decode (self , ss , es , data ):
213218 ptype , _ , _ = data
0 commit comments