Skip to content

Commit

Permalink
adds --json-file and code to create a json file for the moc server.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlFK committed Aug 18, 2019
1 parent b581fd6 commit e2f6a00
Showing 1 changed file with 87 additions and 2 deletions.
89 changes: 87 additions & 2 deletions generate-renode-scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import csv
import zlib
import argparse
import collections
import json

# those memory regions are handled in a special way
# and should not be generated automatically
non_generated_mem_regions = ['ethmac', 'spiflash', 'csr']

mem_regions = {}
peripherals = {}
registers = {}
constants = {}


Expand Down Expand Up @@ -403,8 +406,12 @@ def parse_csv(data):
# CSRs have already been parsed
pass
elif _type == 'csr_register':
# we are currently not interested in this
pass
# csr_register,info_dna_id,0xe0006800,8,ro
registers[_name] = {'name': _name,
'address': _val,
'size': _val2,
'r': _
}
elif _type == 'constant':
found = False
for _csr_name in peripherals:
Expand Down Expand Up @@ -538,6 +545,79 @@ def remove_comments(data):
yield line


def mk_obj():
"""
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

the_dict = make_dict()
for register in registers:
set_path(
the_dict,
register.split('_'),
registers[register],
)

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(filepath):
""" writes json file for the moc server to serve.
Args:
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()
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',
Expand All @@ -552,6 +632,8 @@ def parse_args():
help='Path to the BIOS binary')
parser.add_argument('--firmware-binary', action='store',
help='Path to the binary to load into boot flash')
parser.add_argument('--json-file', action='store',
help='Output json file for moc server')
args = parser.parse_args()

return args
Expand All @@ -575,6 +657,9 @@ def main():
args.bios_binary,
args.firmware_binary))

if args.json_file:
mk_json(args.json_file)


if __name__ == '__main__':
main()

0 comments on commit e2f6a00

Please sign in to comment.