Skip to content

Commit 56eeead

Browse files
authored
Merge pull request #2312 from Pinata-Consulting/make-memory-list-instances
makefile: make memory now lists names of instances of memories
2 parents f3f1324 + e15a33e commit 56eeead

File tree

1 file changed

+79
-5
lines changed

1 file changed

+79
-5
lines changed

flow/scripts/mem_dump.py

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,97 @@
33
import sys
44

55

6+
def find_top_modules(data):
7+
# There can be some cruft in the modules list so that
8+
# we have multiple top level candidates.
9+
top_module = []
10+
instantiations = set(
11+
[
12+
cell["type"]
13+
for minfo2 in data["modules"].values()
14+
for cell in minfo2["cells"].values()
15+
]
16+
)
17+
for mname, minfo in data["modules"].items():
18+
if mname not in instantiations:
19+
top_module.append(mname)
20+
return top_module
21+
22+
23+
def find_cells_by_type_in_module(
24+
module_name, data, target_type, current_path, matching_cells
25+
):
26+
"""
27+
Searches through hierarchy starting at module_name to find all instances of
28+
the given module/type in the hierarchy.
29+
30+
Returns list of cell paths, which are constructed as:
31+
32+
<top_module_name>.(<child_inst_name>.<child_module_name>+).<memory_inst_name>
33+
34+
where the child_inst_name/child_module_name pairs are repeated for each level of the hierarchy.
35+
"""
36+
for cell_name, cell in data["modules"][module_name]["cells"].items():
37+
cell_path = (
38+
f"{current_path}.{module_name}.{cell_name}"
39+
if current_path
40+
else f"{module_name}.{cell_name}"
41+
)
42+
if cell["type"] == target_type:
43+
matching_cells.append(cell_path)
44+
elif cell["type"] in data["modules"]:
45+
# Recursively search within the module
46+
matching_cells.extend(
47+
find_cells_by_type_in_module(
48+
cell["type"], data, target_type, cell_path, []
49+
)
50+
)
51+
52+
return matching_cells
53+
54+
55+
def find_cells_by_type(top_modules, data, module_name, current_path=""):
56+
# first find top module, the module without any submodules
57+
names = []
58+
for top_module in top_modules:
59+
names.extend(
60+
find_cells_by_type_in_module(
61+
top_module, data, module_name, current_path, []
62+
)
63+
)
64+
return names
65+
66+
667
def format_ram_table_from_json(data, max_bits=None):
7-
formatting = "{:<15} | {:<15} | {:<15} | {:<50}\n"
8-
table = formatting.format("Rows", "Width", "Total bits", "Name")
68+
top_modules = find_top_modules(data)
69+
formatting = "{:>5} | {:>5} | {:>6} | {:<20} | {:<80}\n"
70+
table = formatting.format("Rows", "Width", "Bits", "Module", "Instances")
971
table += "-" * len(table) + "\n"
1072
max_ok = True
73+
entries = []
74+
75+
# Collect the entries in a list
1176
for module_name, module_info in data["modules"].items():
1277
cells = module_info["cells"]
13-
for memory, cell in cells.items():
78+
for cell in cells.values():
1479
if not cell["type"].startswith("$mem"):
1580
continue
1681
parameters = cell["parameters"]
1782
size = int(parameters["SIZE"], 2)
1883
width = int(parameters["WIDTH"], 2)
19-
bits = size * width
20-
table += formatting.format(size, width, bits, module_name + "." + memory)
84+
instances = find_cells_by_type(top_modules, data, module_name)
85+
bits = size * width * len(instances)
86+
entries.append((size, width, bits, module_name, ", ".join(instances)))
2187
if max_bits is not None and bits > max_bits:
2288
max_ok = False
89+
90+
# Sort the entries by descending bits
91+
entries.sort(key=lambda x: x[2], reverse=True)
92+
93+
# Format the sorted entries into the table
94+
for entry in entries:
95+
table += formatting.format(*entry)
96+
2397
return table, max_ok
2498

2599

0 commit comments

Comments
 (0)