Skip to content

Commit 0ed706b

Browse files
Flatten nested RDLs in peripepherals that share stuff for hubris
1 parent ec01ae6 commit 0ed706b

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

tools/site_cobble/rdl_pkg/json_dump.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,33 @@ def convert_addr_map_only(
151151

152152
return json_obj
153153

154+
def _flatten_children(
155+
rdlc: RDLCompiler, obj: Union[AddrmapNode, RegfileNode],
156+
parent_offset: int = 0, prefix: str = ""
157+
) -> list:
158+
"""Recursively flatten nested addrmap/regfile children into a flat list
159+
of registers and memories with adjusted offsets and prefixed names."""
160+
result = []
161+
for child in obj.children():
162+
if isinstance(child, (AddrmapNode, RegfileNode)):
163+
nested_prefix = f"{prefix}{child.inst_name}_"
164+
nested_offset = parent_offset + child.address_offset
165+
result.extend(_flatten_children(rdlc, child, nested_offset, nested_prefix))
166+
elif isinstance(child, RegNode):
167+
json_child = convert_reg(rdlc, child)
168+
json_child["inst_name"] = f"{prefix}{json_child['inst_name']}"
169+
json_child["addr_offset"] = parent_offset + child.address_offset
170+
result.append(json_child)
171+
elif isinstance(child, MemNode):
172+
json_child = convert_mem(child)
173+
json_child["inst_name"] = f"{prefix}{json_child['inst_name']}"
174+
json_child["addr_offset"] = parent_offset + child.address_offset
175+
result.append(json_child)
176+
else:
177+
raise RuntimeError("Unknown child type seen during JSON generation.")
178+
return result
179+
180+
154181
def convert_addrmap_or_regfile(
155182
rdlc: RDLCompiler, obj: Union[AddrmapNode, RegfileNode]
156183
) -> dict:
@@ -169,18 +196,6 @@ def convert_addrmap_or_regfile(
169196

170197
json_obj["inst_name"] = obj.inst_name
171198
json_obj["addr_offset"] = obj.address_offset
172-
173-
json_obj["children"] = []
174-
for child in obj.children():
175-
if isinstance(child, (AddrmapNode, RegfileNode)):
176-
json_child = convert_addrmap_or_regfile(rdlc, child)
177-
elif isinstance(child, RegNode):
178-
json_child = convert_reg(rdlc, child)
179-
elif isinstance(child, MemNode):
180-
json_child = convert_mem(child)
181-
else:
182-
raise RuntimeError("Unknown child type seen during JSON generation.")
183-
184-
json_obj["children"].append(json_child)
199+
json_obj["children"] = _flatten_children(rdlc, obj)
185200

186201
return json_obj

0 commit comments

Comments
 (0)