-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_module.py
65 lines (46 loc) · 2.09 KB
/
new_module.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
#!/bin/python3
import os
import sys
import argparse
module_template = """`ifndef {MODULE_HIERARCHY}_{MODULE_NAME}
`define {MODULE_HIERARCHY}_{MODULE_NAME}
module {MODULE_NAME} #() ();
endmodule
`endif
"""
sim_template = """`include "rtl/{MODULE_HIERARCHY}/{MODULE_NAME}.sv"
module {MODULE_NAME}_tb;
endmodule
"""
makefile_template = """
.PHONY: test_{MODULE_NAME}
test_{MODULE_NAME}:
\t@mkdir -p build
\t${{COMPILER}} -o ${{TARGET_DIR}}/{MODULE_NAME}_tb.test sim/{MODULE_HIERARCHY}/{MODULE_NAME}/{MODULE_NAME}_tb.sv
\t${{INTERPRETER}} ${{TARGET_DIR}}/{MODULE_NAME}_tb.test"
"""
script_dir = os.path.dirname(os.path.realpath(__file__))
if __name__ == '__main__':
parser = argparse.ArgumentParser("Generate the template for a new module")
parser.add_argument("MODULE")
parser.add_argument("--template")
parser.add_argument("--template_sim")
args = parser.parse_args()
if args.template:
module_template = args.template
if args.template_sim:
sim_template = args.template_sim
module_name = args.MODULE.split(".")[-1]
module_hierarchy = args.MODULE.split(".")[:-1]
os.makedirs( os.path.join(script_dir, "rtl", *module_hierarchy ), exist_ok=True )
with open( os.path.join(script_dir, "rtl", *module_hierarchy, module_name + '.sv' ), 'w') as f:
f.write( module_template.format(MODULE_NAME=module_name.upper(), MODULE_HIERARCHY="_".join(module_hierarchy).upper() ) )
os.makedirs( os.path.join(script_dir, "sim", *module_hierarchy, module_name ), exist_ok=True )
with open( os.path.join(script_dir, "sim", *module_hierarchy, module_name, module_name + '.sv' ), 'w') as f:
f.write( sim_template.format(MODULE_NAME=module_name, MODULE_HIERARCHY="/".join(module_hierarchy) ) )
makefile = ""
with open( os.path.join( script_dir, "Makefile"), 'r' ) as f:
makefile = [l if not l.startswith("test_all") else l.rstrip() + " test_" + module_name for l in f.readlines() ]
makefile.append( makefile_template.format( MODULE_HIERARCHY="/".join(module_hierarchy), MODULE_NAME=module_name) )
with open(os.path.join( script_dir, "Makefile"), 'w' ) as f:
f.write( "".join(makefile) )