Better dictionary...

This commit is contained in:
pca006132 2020-07-29 11:27:11 +08:00
parent fd78ec3960
commit 1cc788c9a1

51
main.py
View File

@ -119,18 +119,41 @@ def parse_registers():
next(it)
return it
def interpret_field(field):
bit_pattern = re.compile(r'(\d+)(:(\d+))?')
m = bit_pattern.fullmatch(field['bits'][0])
if m is None:
raise ValueError(field['bits'])
high = int(m.group(1))
low = high
if m.group(3) is not None:
low = int(m.group(3))
result = {}
result['name'] = field['name'][0]
result['bits'] = [low, high]
result['type'] = field['type'][0]
return result
def interpret(reg):
pattern = re.compile(r'(.+\w)\d+')
width_pattern = re.compile(r'(\d+)\s?bits')
result = {}
name_pattern = None
expected = ['Name', 'Relative Address', 'Absolute Address', 'Width',
'Access Type', 'Reset Value', 'Description']
expected = [
['Name', 'name', lambda v: ''.join(v)],
['Relative Address', 'rel', lambda v: int(v[0], 16)],
['Absolute Address', 'abs', lambda v: int(v[0], 16)],
['Width', 'width', lambda v: int(width_pattern.fullmatch(v[0]).group(1))],
['Access Type', 'access', lambda v: v[0]],
['Reset Value', 'reset', lambda v: v[0]],
['Description', 'description', lambda v: ' '.join(v)]
]
for v in reg['def']:
a = v['key']
b = v['value']
key = a[0].strip()
if len(expected) > 0 and key == expected[0]:
result[key] = b
if len(expected) > 0 and key == expected[0][0]:
result[expected[0][1]] = expected[0][2](b)
expected = expected[1:]
if key == 'Name':
m = pattern.fullmatch(b[0].strip())
@ -139,10 +162,24 @@ def interpret(reg):
elif name_pattern != None and key.startswith(name_pattern):
if 'similar' not in result:
result['similar'] = []
result['similar'].append({'name': key, 'address': b[0].strip()})
result['fields'] = reg['fields']
result['similar'].append({'name': key, 'abs': int(b[0], 16)})
result['fields'] = [interpret_field(f) for f in reg['fields']]
assert len(expected) == 0
return result
def snake_to_camel(name: str):
result = []
start = True
for c in name:
if c == '_':
start = True
elif start:
start = False
result.append(c.upper())
else:
result.append(c.lower())
return ''.join(result)
def emit_rust(base_addr, ending_addr, registers):
current_addr = base_addr
reserved_id = 0
@ -188,4 +225,4 @@ for line in sys.stdin:
v = end_iterator(parser)
for reg in v:
reg = interpret(reg)
print(json.dumps(reg, indent = 4))
print(reg)