-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate-mocserver-json.py
executable file
·113 lines (85 loc) · 2.64 KB
/
generate-mocserver-json.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python3
import argparse
import collections
import json
def mk_obj(csr):
"""
Makes an object of dicts and lists from the registers var
"""
def make_dict():
return collections.defaultdict(make_dict);
def set_path(d, path, value):
if path[-1] in ['reset', 'issue', 'en']: return
for key in path[:-1]:
d = d[key]
d[path[-1]] = value
# add a placeholder for where the register's value
# (other keys are metadata about this value)
d[path[-1]]['value'] = 0
the_dict = make_dict()
for name, val in csr['csr_registers'].items():
reg_dict = { 'name': name, 'address': val['addr'] }
reg_dict.update(val)
reg_dict['r'] = val['type']
del(reg_dict['type'])
del(reg_dict['addr'])
reg_dict['shadowed_address'] = None
set_path(
the_dict,
name.split('_'),
reg_dict,
)
return the_dict
def crawl(d):
# normalize
# look for in1:v in2:v and change to in:[v,v]
# are we on a leaf?
if not hasattr(d,'keys'): return
# look for fooN keys
keys=[]
for k in d:
crawl(d[k])
if k in ['fx2']: continue # special cases: fx2 is not a 0,1,2
if k[-1].isdigit():
keys.append(k)
# consolodate them into foo[0,1,2]
keys.sort()
for k in keys:
# grab the value and remove the fooN item
v=d.pop(k)
# split into foo and N
k,n=k[:-1],k[-1]
# make a foo list
if n=='0':
d[k]=[]
# append all the values to the foo list
d[k].append(v)
def mk_json(csr, filepath):
""" writes json file for the moc server to serve.
Args:
csr (dict): LiteX configuration
filepath (string): path to the file lines should be written to
or '-' to write to a standard output
the module's registers var data is turned into an object
and serialized to a .json file.
"""
o = mk_obj(csr)
crawl(o)
with open(filepath, 'w') as f:
json.dump(o, f, indent=2)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('conf_file',
help='JSON configuration generated by LiteX')
parser.add_argument('--json-file', action='store',
help='Output json file for moc server')
args = parser.parse_args()
return args
def main():
args = parse_args()
with open(args.conf_file) as f:
csr = json.load(f)
if args.json_file:
mk_json(csr, args.json_file)
if __name__ == '__main__':
main()