@@ -266,7 +266,6 @@ def field_name(name):
266
266
return MatchedFields (match , fields )
267
267
268
268
269
- # TODO: not currently tested or exported at package level
270
269
def bitpattern_to_val (bitpattern , * ordered_fields , ** named_fields ):
271
270
""" Return an unsigned integer representation of field format filled with the provided values.
272
271
@@ -275,6 +274,9 @@ def bitpattern_to_val(bitpattern, *ordered_fields, **named_fields):
275
274
the order provided. If ordered_fields are provided then no named_fields can be used.
276
275
:param named_fields: A list of parameters to be matched to the provided bit pattern in
277
276
by the names provided. If named_fields are provided then no ordered_fields can be used.
277
+ A special keyword argument, 'field_map', can be provided, which will allow you specify
278
+ a correspondence between the 1-letter field names in the bitpattern string and longer,
279
+ human readable field names (see example below).
278
280
:return: An unsigned integer carrying the result of the field substitution.
279
281
280
282
This function will compare take a specified pattern of bits, where some
@@ -285,14 +287,24 @@ def bitpattern_to_val(bitpattern, *ordered_fields, **named_fields):
285
287
creating values for testing when the resulting values will be "chopped" up by the hardware
286
288
later (e.g. instruction decode or other bitfield heavy functions).
287
289
290
+ If a special keyword argument, 'field_map', is provided, then the named fields provided
291
+ can be longer, human-readable field names, which will correspond to the field in the
292
+ bitpattern according to the field_map. See the third example below.
293
+
288
294
Examples::
289
295
290
- bitpattern_to_val('0000000rrrrrsssss000ddddd0110011 ', r=1, s=2 , d=3) # RISCV ADD instr
296
+ bitpattern_to_val('0000000sssssrrrrr000ddddd0110011 ', s=2, r=1 , d=3) # RISCV ADD instr
291
297
# evaluates to 0b00000000000100010000000110110011
292
298
293
- bitpattern_to_val('iiiiiiirrrrrsssss010iiiii0100011 ', i=1, r=3, s=4 ) # RISCV SW instr
299
+ bitpattern_to_val('iiiiiiisssssrrrrr010iiiii0100011 ', i=1, s=4, r=3 ) # RISCV SW instr
294
300
# evaluates to 0b00000000001100100010000010100011
295
301
302
+ bitpattern_to_val(
303
+ 'iiiiiiisssssrrrrr010iiiii0100011',
304
+ imm=1, rs2=4, rs1=3,
305
+ field_map={'i': 'imm', 's': 'rs2', 'r': 'rs1}
306
+ ) # RISCV SW instr
307
+ # evaluates to 0b00000000001100100010000010100011
296
308
"""
297
309
298
310
if len (ordered_fields ) > 0 and len (named_fields ) > 0 :
@@ -305,6 +317,11 @@ def letters_in_field_order():
305
317
seen .append (c )
306
318
return seen
307
319
320
+ field_map = None
321
+ if 'field_map' in named_fields :
322
+ field_map = named_fields ['field_map' ]
323
+ named_fields .pop ('field_map' )
324
+
308
325
bitlist = []
309
326
lifo = letters_in_field_order ()
310
327
if ordered_fields :
@@ -315,7 +332,9 @@ def letters_in_field_order():
315
332
if len (lifo ) != len (named_fields ):
316
333
raise PyrtlError ('number of fields and number of unique patterns do not match' )
317
334
try :
318
- intfields = [int (named_fields [n ]) for n in lifo ]
335
+ def fn (n ):
336
+ return field_map [n ] if field_map else n
337
+ intfields = [int (named_fields [fn (n )]) for n in lifo ]
319
338
except KeyError as e :
320
339
raise PyrtlError ('bitpattern field %s was not provided in named_field list' % e .args [0 ])
321
340
0 commit comments