-
Notifications
You must be signed in to change notification settings - Fork 653
/
split-mems-conf.py
executable file
·99 lines (81 loc) · 2.95 KB
/
split-mems-conf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
import os
import json
import argparse
import sys
from typing import List, Optional
# Schema of json emitted by circt
"""
{
"module_name": "mem_ext",
"depth": 512,
"width": 64,
"masked": true,
"read": false,
"write": false,
"readwrite": true,
"mask_granularity": 8,
"extra_ports": [],
"hierarchy": [
"TestHarness.ram.srams.mem.mem_ext"
]
}
"""
sys.setrecursionlimit(100)
def bfs_find_root(tree, module_name):
q = [tree]
while len(q) != 0:
front = q[0]
q.pop(0)
if front['module_name'] == module_name:
return front
for c in front['instances']:
q.append(c)
return None
def bfs_collect_submodules(tree):
output = set()
q = [(tree['instance_name'], tree['module_name'], tree['instances'])]
while len(q) != 0:
front = q[0]
q.pop(0)
(inst, mod, child) = front
output.add(mod)
for c in child:
q.append((c['instance_name'], c['module_name'], c['instances']))
return output
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Use MFC(FIRTOOL) generated model-hrchy JSONs to create smems confs for the DUT and TestHarness')
parser.add_argument('--in-smems-conf', type=str, required=True, help='Overall smems conf file that contains all memory definitions')
parser.add_argument('--in-model-hrchy-json', type=str, required=True, help='JSON indicating which mem modules are in the DUT')
parser.add_argument('--dut-module-name', type=str, required=True, help='Module name of the DUT')
parser.add_argument('--model-module-name', type=str, required=True, help='Module name of the model')
parser.add_argument('--out-dut-smems-conf', type=str, required=True, help='Smems conf with only DUT mem module definitions')
parser.add_argument('--out-model-smems-conf', type=str, required=True, help='Smems conf with only top-most level mem module definitions (not including DUT modules)')
args = parser.parse_args()
with open(args.in_smems_conf) as isc, \
open(args.in_model_hrchy_json) as imhj:
imhj_data = json.load(imhj)
dut_root = bfs_find_root(imhj_data, args.dut_module_name)
if dut_root:
dut_submodules = bfs_collect_submodules(dut_root)
else:
dut_submodules = set()
model_root = bfs_find_root(imhj_data, args.model_module_name)
model_submodules = bfs_collect_submodules(model_root)
with open(args.out_dut_smems_conf, "w") as odsc, \
open(args.out_model_smems_conf, "w") as otsc:
os.utime(args.out_dut_smems_conf)
os.utime(args.out_model_smems_conf)
for l in isc:
sl = l.split()
# the line can't be split then stop immediately (normally an empty file)
if len(sl) > 2:
name = sl[1]
if name in dut_submodules:
odsc.write(l)
elif name in model_submodules:
otsc.write(l)
else:
assert False, "Unable to find smem CONF module in MFC(FIRTOOL) emitted JSON files."
else:
exit(0)