-
Notifications
You must be signed in to change notification settings - Fork 1
/
json-dat.py
54 lines (45 loc) · 1.43 KB
/
json-dat.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
"""Convert between fud-style JSON and hex data files.
Use the machinery from "old fud" to convert a JSON data file into a
directory of flat hex-encoded files, suitable for loading into a
hardware simulator, and back again.
"""
from fud.stages.verilator.json_to_dat import convert2dat, convert2json
import simplejson
import sys
import os
import re
def json2dat(in_file, out_dir):
os.makedirs(out_dir, exist_ok=True)
round_float_to_fixed = True
with open(in_file) as json:
convert2dat(
out_dir,
simplejson.load(json, use_decimal=True),
"dat",
round_float_to_fixed,
)
def dat2json(out_file, in_dir, sim_log=None):
mem = convert2json(in_dir, "out")
if sim_log:
cycles = 0
with open(sim_log) as f:
for line in f:
match = re.search(r"Simulated\s+((-)?\d+) cycles", line)
if match:
cycles = int(match.group(1))
break
out = {
"cycles": cycles,
"memories": mem,
}
else:
out = mem
with open(out_file, 'w') as f:
simplejson.dump(out, f, indent=2, sort_keys=True, use_decimal=True)
if __name__ == '__main__':
if sys.argv[1] == '--from-json':
json2dat(*sys.argv[2:])
elif sys.argv[1] == '--to-json':
dat2json(*sys.argv[2:])
else:
print("specify --from-json or --to-json", file=sys.stderr)