Skip to content

Commit

Permalink
Rename script and pass in FIRRTL file
Browse files Browse the repository at this point in the history
  • Loading branch information
ayakayorihiro committed Jan 23, 2024
1 parent 4562a83 commit a87c7f7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
39 changes: 0 additions & 39 deletions tools/firrtl/generate-firrtl-primitives.py

This file was deleted.

53 changes: 53 additions & 0 deletions tools/firrtl/generate-firrtl-with-primitives.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import json
import os
import subprocess
import sys

# Generates the arguments for m4 based on the JSON data
def generate_m4_arguments(inst):
primitive_name = inst["name"]
args = []
# hack to replace the module name with the corresponding parameterized version
# FIXME: figure out a way to do substring replacement in m4
module_name_value = primitive_name
# get the parameters for the primitive
for param in inst["params"]:
key = param["param_name"]
value = param["param_value"]
args.append(f"-D{key}={value}")
module_name_value += "_" + str(value)

args.append(f"-DMODULE_NAME={module_name_value}")

# retrieve the appropriate template file for the primitive
firrtl_file_path = os.path.join(sys.path[0], "templates", inst['name'] + ".fir")
args.append(firrtl_file_path)

return args

def main():
if len(sys.argv) != 3:
args_desc = [
"FIRRTL_FILE",
"PRIMITIVE_USES_JSON",
]
print(f"Usage: {sys.argv[0]} {' '.join(args_desc)}")
return 1
firrtl_file = open(sys.argv[1])
primitive_uses_file = open(sys.argv[2])
# The first line contains the circuit name, which needs to come before the primitives.
print(firrtl_file.readline().rstrip())
# Display the primitive definitions.
primitive_insts = json.load(primitive_uses_file)
for inst in primitive_insts:
m4_args = ["m4"]
m4_args += generate_m4_arguments(inst)
# execute the subprocess containing m4
subprocess.run(m4_args)
print()
# Display the rest of the FIRRTL program.
for line in firrtl_file.readlines():
print(line.rstrip())

if __name__ == '__main__':
main()

0 comments on commit a87c7f7

Please # to comment.