Refactoring Verilog->XML code Signed-off-by: David Shah <davey1576@gmail.com>
diff --git a/vlog_to_model.py b/vlog_to_model.py index cfbbc59..43cd801 100755 --- a/vlog_to_model.py +++ b/vlog_to_model.py
@@ -1,47 +1,75 @@ -#!/usr/bin/python3 -import yosys_exec -import lxml.etree as ET -import argparse, re -import os, tempfile -from yosys_json import YosysJson - +#!/usr/bin/env python3 """ Convert a Verilog simulation model to a VPR `model.xml` """ +import yosys.run +import lxml.etree as ET +import argparse, re +import os, tempfile +from yosys.json import YosysJson -parser = argparse.ArgumentParser(description='Convert a Verilog simulation model into a VPR model.xml file') -parser.add_argument('infiles', metavar='input.v', type=str, nargs='+', - help='one or more Verilog input files') -parser.add_argument('--top', help='top level module, not needed if only one module across all files') -parser.add_argument('-o', help='output filename, default model.xml') + +parser = argparse.ArgumentParser(description=__doc__.strip()) +parser.add_argument( + 'infiles', + metavar='input.v', type=str, nargs='+', + help="""\ +One or more Verilog input files, that will be passed to Yosys internally. +They should be enough to generate a flattened representation of the model, +so that paths through the model can be determined. +""") +parser.add_argument( + '--top', + help="""\ +Top level module, not needed if only one module exists across all input Verilog +files, in which case that module will be used as the top level. +""") +parser.add_argument( + '-o', + help="""\ +Output filename, default 'model.xml' +""") args = parser.parse_args() -aig_json = yosys_exec.vlog_to_json(args.infiles, True, True) +aig_json = yosys.run.vlog_to_json(args.infiles, True, True) -# TODO: All of these functions involve a fairly large number of calls to Yosys -# Although performance here is unlikely to be a major priority any time soon, -# it might be worth investigating better options? +""" +Extract the pin from a line of the result of a Yosys select command +""" +def extract_pin(module, pstr, _regex=re.compile(r"([^/]+)/([^/]+)")): + m = re.match(r"([^/]+)/([^/]+)", pstr) + if m and m.group(1) == module: + return m.group(2) + else: + return None +""" +Run a Yosys select command (given the expression and input files) on a module +and return the result as a list of pins +TODO: All of these functions involve a fairly large number of calls to Yosys +Although performance here is unlikely to be a major priority any time soon, +it might be worth investigating better options? + +""" def do_select(infiles, module, expr): outfile = tempfile.mktemp() sel_cmd = "prep -top %s -flatten; cd %s; select -write %s %s" % (module, module, outfile, expr) - yosys_exec.run_yosys_commands(sel_cmd, infiles) + yosys.run.yosys_commands(sel_cmd, infiles) pins = [] with open(outfile, 'r') as f: for net in f: snet = net.strip() if(len(snet) > 0): - m = re.match(r"([^/]+)/([^/]+)", snet) - if m and m.group(1) == module: - pins.append(m.group(2)) + pin = extract_pin(module, snet) + if pin is not None: + pins.append(pin) os.remove(outfile) return pins def get_combinational_sinks(infiles, module, innet): return do_select(infiles, module, "%s %%coe* o:* %%i %s %%d" % (innet, innet)) - def list_clocks(infiles, module): return do_select(infiles, module, "c:* %x:+[CLK] c:* %d") @@ -54,12 +82,12 @@ yj = YosysJson(aig_json) top = yj.get_top() -models = ET.Element("models") -model = ET.SubElement(models, "model", dict(name=top)) +models_xml = ET.Element("models") +model_xml = ET.SubElement(models_xml, "model", {'name': top}) ports = yj.get_ports() -inports = ET.SubElement(model, "input_ports") -outports = ET.SubElement(model, "output_ports") +inports_xml = ET.SubElement(model_xml, "input_ports") +outports_xml = ET.SubElement(model_xml, "output_ports") clocks = list_clocks(args.infiles, top) clk_sigs = dict() @@ -77,9 +105,9 @@ if name in clk_sigs[clk]: attrs["clock"] = clk if iodir == "input": - ET.SubElement(inports, "port", attrs) + ET.SubElement(inports_xml, "port", attrs) elif iodir == "output": - ET.SubElement(outports, "port", attrs) + ET.SubElement(outports_xml, "port", attrs) else: assert(False) #how does VPR specify inout (only applicable for PACKAGEPIN of an IO primitive) @@ -88,5 +116,5 @@ outfile = args.o f = open(outfile, 'w') -f.write(ET.tostring(models, pretty_print=True).decode('utf-8')) +f.write(ET.tostring(models_xml, pretty_print=True).decode('utf-8')) f.close()
diff --git a/vlog_to_pbtype.py b/vlog_to_pbtype.py old mode 100644 new mode 100755 index 225ff62..715affe --- a/vlog_to_pbtype.py +++ b/vlog_to_pbtype.py
@@ -1,29 +1,20 @@ -#!/usr/bin/python3 -import yosys_exec -import lxml.etree as ET -import argparse, re -import os, tempfile -from yosys_json import YosysJson +#!/usr/bin/env python3 -""" +"""\ Convert a Verilog simulation model to a VPR `pb_type.xml` -The following Verilog attributes are allowed on instances: - - (* PB_PATH="../path/to/pb_type.xml" *) : import the pb_type for a given instance - from an external XML file instead of including it inline. +The following are allowed on a top level module: - - (* GENERATE_PB *) : generate (and overwrite if applicable) the above file, as well as including it - -The following are allowed on both instances (except where PB_PATH is set but GENERATE_PB isn't), and -the top level module. + - (* TYPE="bel|blackbox" *) : specify the type of the module + (either a Basic ELement or a blackbox named after the pb_type - - (* BLIF_MODEL=".latch" *) : specify the corresponding blif_model for a given instance - - - (* CLASS="flipflop" *) : specify the corresponding class for a given instance + - (* CLASS="lut|routing|flipflop|mem" *) : specify the class of an given + instance. Must be specified for BELs - - (* PB_NAME="name" *) : override the name of the pb_type (default: cell type) + - (* PB_NAME="name" *) : override the name of the pb_type + (default: name of module) -The following are allowed on nets within modules: +The following are allowed on nets within modules (TODO: use proper Verilog timing): - (* SETUP="clk 10e-12" *) : specify setup time for a given clock @@ -34,13 +25,35 @@ - (* PB_MUX=1 *) : if the signal is driven by a $mux cell, generate a pb_type <mux> element for it """ -parser = argparse.ArgumentParser(description='Convert a Verilog simulation model into a VPR pb_type.xml file') -parser.add_argument('infiles', metavar='input.v', type=str, nargs='+', - help='one or more Verilog input files') -parser.add_argument('--top', help='top level module, not needed if only one module across all files') -parser.add_argument('-o', help='output filename, default model.xml') +import yosys.run +import lxml.etree as ET +import argparse, re +import os, tempfile +from yosys.json import YosysJson + + +parser = argparse.ArgumentParser(description=__doc__.strip(), formatter_class=argparse.RawTextHelpFormatter) +parser.add_argument( + 'infiles', + metavar='input.v', type=str, nargs='+', + help="""\ +One or more Verilog input files, that will be passed to Yosys internally. +They should be enough to generate a flattened representation of the model, +so that paths through the model can be determined. +""") +parser.add_argument( + '--top', + help="""\ +Top level module, not needed if only one module exists across all input Verilog +files, in which case that module will be used as the top level. +""") +parser.add_argument( + '-o', + help="""\ +Output filename, default 'model.xml' +""") args = parser.parse_args() -vjson = yosys_exec.vlog_to_json(args.infiles, False, False) +vjson = yosys.run.vlog_to_json(args.infiles, False, False) yj = YosysJson(vjson)
diff --git a/yosys/__init__.py b/yosys/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/yosys/__init__.py
diff --git a/yosys_json.py b/yosys/json.py similarity index 98% rename from yosys_json.py rename to yosys/json.py index 5b6ed39..6454b08 100755 --- a/yosys_json.py +++ b/yosys/json.py
@@ -1,8 +1,4 @@ -#!/usr/bin/python3 - -import json -import os, sys - +#!/usr/bin/env python3 """ This is intended to provide a range of helper functions around the output of Yosys' `write_json`. Depending on the tasks, this may need to be flattened @@ -10,6 +6,9 @@ be used before outputting the JSON. """ +import os, sys +import json + class YosysJson: # Takes either a filename, or already parsed JSON as a dictionary # Also optionally pass the top level module
diff --git a/yosys_exec.py b/yosys/run.py similarity index 66% rename from yosys_exec.py rename to yosys/run.py index 29d8e7e..0730b3b 100755 --- a/yosys_exec.py +++ b/yosys/run.py
@@ -1,27 +1,27 @@ #!/usr/bin/env python3 import os, subprocess, sys, json -import yosys_utils +import yosys.utils def get_yosys(): return os.getenv('YOSYS', "yosys") -def run_yosys_get_output(params): +def yosys_get_output(params): cmd = [get_yosys()] + params return subprocess.check_output(cmd).decode("utf-8") -def run_yosys_commands(commands, infiles = []): +def yosys_commands(commands, infiles = []): params = ["-q", "-p", commands] + infiles - return run_yosys_get_output(params) + return yosys_get_output(params) -def run_yosys_script(script, infiles = []): +def yosys_script(script, infiles = []): params = ["-q", "-s", script] + infiles - return run_yosys_get_output(params) + return yosys_get_output(params) def vlog_to_json(infiles, flatten = False, aig = False): prep_opts = "-flatten" if flatten else "" json_opts = "-aig" if aig else "" cmds = "prep %s; write_json %s" % (prep_opts, json_opts) - j = yosys_utils.strip_yosys_json(run_yosys_commands(cmds, infiles)) + j = yosys.utils.strip_yosys_json(yosys_commands(cmds, infiles)) """with open('dump.json', 'w') as dbg: print(j,file=dbg)""" return json.loads(j)
diff --git a/yosys/utils.py b/yosys/utils.py new file mode 100644 index 0000000..bea8acf --- /dev/null +++ b/yosys/utils.py
@@ -0,0 +1,11 @@ +#!/usr/bin/env python3 +import re + +"""The JSON Yosys outputs isn't acutally compliant JSON, as it contains C-style +comments. These must be stripped.""" + +def strip_yosys_json(text): + stripped = re.sub(r'\\\n', '', text) + stripped = re.sub(r'//.*\n', '\n', stripped) + stripped = re.sub(r'/\*.*\*/', '', stripped) + return stripped
diff --git a/yosys_utils.py b/yosys_utils.py deleted file mode 100644 index 9db082a..0000000 --- a/yosys_utils.py +++ /dev/null
@@ -1,11 +0,0 @@ -#!/usr/bin/python3 -import re - -# The JSON Yosys outputs isn't acutally compliant JSON, as it contains C-style -# comments. These must be stripped. - -def strip_yosys_json(text): - stripped = re.sub(r'\\\n', '', text) - stripped = re.sub(r'//.*\n', '\n', stripped) - stripped = re.sub(r'/\*.*\*/', '', stripped) - return stripped