|
3 | 3 | import sys |
4 | 4 |
|
5 | 5 |
|
| 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 | + |
6 | 67 | 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") |
9 | 71 | table += "-" * len(table) + "\n" |
10 | 72 | max_ok = True |
| 73 | + entries = [] |
| 74 | + |
| 75 | + # Collect the entries in a list |
11 | 76 | for module_name, module_info in data["modules"].items(): |
12 | 77 | cells = module_info["cells"] |
13 | | - for memory, cell in cells.items(): |
| 78 | + for cell in cells.values(): |
14 | 79 | if not cell["type"].startswith("$mem"): |
15 | 80 | continue |
16 | 81 | parameters = cell["parameters"] |
17 | 82 | size = int(parameters["SIZE"], 2) |
18 | 83 | 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))) |
21 | 87 | if max_bits is not None and bits > max_bits: |
22 | 88 | 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 | + |
23 | 97 | return table, max_ok |
24 | 98 |
|
25 | 99 |
|
|
0 commit comments