utils: Running autoformatter over code. Not doing rr_graph lib because of #94. Signed-off-by: Tim 'mithro' Ansell <me@mith.ro>
diff --git a/vlog_to_model.py b/vlog_to_model.py index 012a7e8..070e301 100755 --- a/vlog_to_model.py +++ b/vlog_to_model.py
@@ -28,7 +28,9 @@ parser = argparse.ArgumentParser(description=__doc__.strip()) parser.add_argument( 'infiles', - metavar='input.v', type=str, nargs='+', + 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, @@ -40,9 +42,7 @@ Top level module, will usually be automatically determined from the file name %.sim.v """) -parser.add_argument( - '-o', - help="""\ +parser.add_argument('-o', help="""\ Output filename, default 'model.xml' """) @@ -63,16 +63,20 @@ if wm: top = wm.group(1).upper() else: - print("ERROR file name not of format %.sim.v ({}), cannot detect top level. Manually specify the top level module using --top").format(iname) + print( + "ERROR file name not of format %.sim.v ({}), cannot detect top level. Manually specify the top level module using --top" + ).format(iname) sys.exit(1) yj = YosysJSON(aig_json, top) if top is None: - print("ERROR: more than one module in design, cannot detect top level. Manually specify the top level module using --top") + print( + "ERROR: more than one module in design, cannot detect top level. Manually specify the top level module using --top" + ) sys.exit(1) tmod = yj.top_module -models_xml = ET.Element("models", nsmap = {'xi': xmlinc.xi_url}) +models_xml = ET.Element("models", nsmap={'xi': xmlinc.xi_url}) inc_re = re.compile(r'^\s*`include\s+"([^"]+)"') @@ -95,10 +99,16 @@ module_basename = os.path.basename(abs_dep) wm = re.match(r"([A-Za-z0-9_]+)\.sim\.v", module_basename) if wm: - model_path = "{}/{}.model.xml" .format(module_path, wm.group(1).lower()) + model_path = "{}/{}.model.xml".format(module_path, + wm.group(1).lower()) else: - assert False, "included Verilog file name {} does not follow pattern %%.sim.v".format(module_basename) - xmlinc.include_xml(parent=models_xml, href=model_path, outfile=outfile, xptr="xpointer(models/child::node())") + assert False, "included Verilog file name {} does not follow pattern %%.sim.v".format( + module_basename) + xmlinc.include_xml( + parent=models_xml, + href=model_path, + outfile=outfile, + xptr="xpointer(models/child::node())") else: # Is a leaf model topname = tmod.attr("MODEL_NAME", top) @@ -113,7 +123,8 @@ clocks = yosys.run.list_clocks(args.infiles, top) clk_sigs = dict() for clk in clocks: - clk_sigs[clk] = yosys.run.get_clock_assoc_signals(args.infiles, top, clk) + clk_sigs[clk] = yosys.run.get_clock_assoc_signals( + args.infiles, top, clk) for name, width, iodir in ports: attrs = dict(name=name) @@ -132,7 +143,6 @@ else: assert False, "bidirectional ports not permitted in VPR models" - if len(models_xml) == 0: models_xml.insert(0, ET.Comment("this file is intentionally left blank"))
diff --git a/vlog_to_pbtype.py b/vlog_to_pbtype.py index 62adaab..f536e7d 100755 --- a/vlog_to_pbtype.py +++ b/vlog_to_pbtype.py
@@ -1,5 +1,4 @@ #!/usr/bin/env python3 - """\ Convert a Verilog simulation model to a VPR `pb_type.xml` @@ -54,10 +53,13 @@ from yosys.json import YosysJSON import xmlinc -parser = argparse.ArgumentParser(description=__doc__.strip(), formatter_class=argparse.RawTextHelpFormatter) +parser = argparse.ArgumentParser( + description=__doc__.strip(), formatter_class=argparse.RawTextHelpFormatter) parser.add_argument( 'infiles', - metavar='input.v', type=str, nargs='+', + 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, @@ -69,9 +71,7 @@ Top level module, will usually be automatically determined from the file name %.sim.v """) -parser.add_argument( - '-o', - help="""\ +parser.add_argument('-o', help="""\ Output filename, default 'model.xml' """) @@ -86,7 +86,6 @@ vjson = yosys.run.vlog_to_json(args.infiles, flatten=False, aig=False) yj = YosysJSON(vjson) - if args.top is not None: top = args.top else: @@ -94,12 +93,14 @@ if wm: top = wm.group(1).upper() else: - print("ERROR file name not of format %.sim.v ({}), cannot detect top level. Manually specify the top level module using --top".format(iname)) + print( + "ERROR file name not of format %.sim.v ({}), cannot detect top level. Manually specify the top level module using --top". + format(iname)) sys.exit(1) - tmod = yj.module(top) + def mod_pb_name(mod): """Convert a Verilog module to a pb_type name in the format documented here: https://github.com/SymbiFlow/symbiflow-arch-defs/#names""" @@ -122,7 +123,8 @@ #TODO: other types return "BLK_IG-" + mod.name -def make_pb_content(mod, xml_parent, mod_pname, is_submode = False): + +def make_pb_content(mod, xml_parent, mod_pname, is_submode=False): """Build the pb_type content - child pb_types, timing and direct interconnect, but not IO. This may be put directly inside <pb_type>, or inside <mode>.""" @@ -138,14 +140,13 @@ def make_direct_conn(ic_xml, source, dest): source_pin = get_full_pin_name(source) dest_pin = get_full_pin_name(dest) - ic_name = dest_pin.replace(".","_").replace("[","_").replace("]","") + ic_name = dest_pin.replace(".", "_").replace("[", "_").replace("]", "") dir_xml = ET.SubElement(ic_xml, 'direct', { 'name': ic_name, 'input': source_pin, 'output': dest_pin }) - # Find out whether or not the module we are generating content for is a blackbox is_blackbox = (mod.attr("blackbox", 0) == 1) @@ -163,10 +164,13 @@ # Heuristic for autogenerated files from w.py wm = re.match(r"([A-Za-z0-9_]+)\.sim\.v", module_basename) if wm: - pb_type_path = "{}/{}.pb_type.xml".format(module_path, wm.group(1).lower()) + pb_type_path = "{}/{}.pb_type.xml".format( + module_path, + wm.group(1).lower()) else: pb_type_path = "{}/pb_type.xml".format(module_path) - xmlinc.include_xml(parent=xml_parent, href=pb_type_path, outfile=outfile) + xmlinc.include_xml( + parent=xml_parent, href=pb_type_path, outfile=outfile) # In order to avoid overspecifying interconnect, there are two directions we currently # consider. All interconnect going INTO a cell, and interconnect going out of a cell # into a top level output - or all outputs if "mode" is used. @@ -174,10 +178,14 @@ for pin, net in inp_cons: drvs = mod.net_drivers(net) if len(drvs) == 0: - print("ERROR: pin {}.{} has no driver, interconnect will be missing".format(pb_name, pin)) + print( + "ERROR: pin {}.{} has no driver, interconnect will be missing". + format(pb_name, pin)) assert False elif len(drvs) > 1: - print("ERROR: pin {}.{} has multiple drivers, interconnect will be overspecified".format(pb_name, pin)) + print( + "ERROR: pin {}.{} has multiple drivers, interconnect will be overspecified". + format(pb_name, pin)) assert False for drv_cell, drv_pin in drvs: interconn.append(((drv_cell, drv_pin), (cname, pin))) @@ -187,8 +195,8 @@ sinks = mod.net_sinks(net) for sink_cell, sink_pin in sinks: if sink_cell == mod.name: - #Only consider outputs from cell to top level IO. Inputs to other cells will be dealt with - #in those cells. + #Only consider outputs from cell to top level IO. Inputs to other cells will be dealt with + #in those cells. interconn.append(((cname, pin), (sink_cell, sink_pin))) ic_xml = ET.SubElement(xml_parent, "interconnect") @@ -200,7 +208,10 @@ """Add a suitable timing spec if necessary to the pb_type""" if tmgspec is not None: splitspec = tmgspec.split(" ") - assert len(splitspec) == 2, 'bad timing specification "{}", must be of format "clock value"'.format(tmgspec) + assert len( + splitspec + ) == 2, 'bad timing specification "{}", must be of format "clock value"'.format( + tmgspec) attrs = {"port": port, "clock": splitspec[0]} if xmltype == "T_clock_to_Q": attrs["max"] = splitspec[1] @@ -244,6 +255,7 @@ }) xml_mat.text = mat + def make_pb_type(mod): """Build the pb_type for a given module. mod is the YosysModule object to generate.""" @@ -277,11 +289,13 @@ else: assert False, "unknown class {}".format(mod_cls) elif is_blackbox and not has_modes: - pb_xml_attrs["blif_model"] = ".subckt " + mod.attr("MODEL_NAME", mod.name) + pb_xml_attrs["blif_model"] = ".subckt " + mod.attr( + "MODEL_NAME", mod.name) #TODO: might not always be the case? should be use Verilog `generate`s to detect this? pb_xml_attrs["num_pb"] = "1" - pb_type_xml = ET.Element("pb_type", pb_xml_attrs, nsmap = {'xi': xmlinc.xi_url}) + pb_type_xml = ET.Element( + "pb_type", pb_xml_attrs, nsmap={'xi': xmlinc.xi_url}) # Process IOs clocks = yosys.run.list_clocks(args.infiles, mod.name) @@ -302,9 +316,15 @@ if has_modes: for mode in modes: smode = mode.strip() - mode_xml = ET.SubElement(pb_type_xml, "mode", {"name" : smode}) + mode_xml = ET.SubElement(pb_type_xml, "mode", {"name": smode}) # Rerun Yosys with mode parameter - mode_yj = YosysJSON(yosys.run.vlog_to_json(args.infiles, flatten=False, aig=False, mode=smode, module_with_mode=mod.name)) + mode_yj = YosysJSON( + yosys.run.vlog_to_json( + args.infiles, + flatten=False, + aig=False, + mode=smode, + module_with_mode=mod.name)) mode_mod = mode_yj.module(mod.name) make_pb_content(mode_mod, mode_xml, mod_pname, True) else: @@ -312,8 +332,8 @@ return pb_type_xml -pb_type_xml = make_pb_type(tmod) +pb_type_xml = make_pb_type(tmod) f = open(outfile, 'w') f.write(ET.tostring(pb_type_xml, pretty_print=True).decode('utf-8'))
diff --git a/xmlinc.py b/xmlinc.py index 120c834..ae99039 100644 --- a/xmlinc.py +++ b/xmlinc.py
@@ -7,12 +7,14 @@ ET.register_namespace('xi', xi_url) + def make_relhref(outfile, href): outpath = os.path.dirname(os.path.abspath(outfile)) relpath = os.path.relpath(os.path.dirname(os.path.abspath(href)), outpath) return os.path.join(relpath, os.path.basename(href)) -def include_xml(parent, href, outfile, xptr = None): + +def include_xml(parent, href, outfile, xptr=None): """ Generate an XML include, using a relative path.
diff --git a/yosys/json.py b/yosys/json.py index 5d34074..ef2bc92 100755 --- a/yosys/json.py +++ b/yosys/json.py
@@ -9,6 +9,7 @@ import os, sys import json + class YosysModule: def __init__(self, name, module_data): self.name = name @@ -60,7 +61,7 @@ """All attributes of a module as a dictionary""" return self.data["attributes"] - def attr(self, attr, defval = None): + def attr(self, attr, defval=None): """Get an attribute, or defval is not set""" if attr in self.module_attrs: return self.module_attrs[attr] @@ -78,7 +79,7 @@ """All attributes of a given cell as a dictionary""" return self.data["cells"][cell]["attributes"] - def cell_attr(self, cell, attr, defval = None): + def cell_attr(self, cell, attr, defval=None): """Get an attribute of a given cell, or defval is not set""" if attr in self.cell_attrs(cell): return self.cell_attrs(cell)[attr] @@ -89,7 +90,7 @@ """Get all attributes of a given net as a dictionary""" return self.data["netnames"][netname]["attributes"] - def net_attr(self, netname, attr, defval = None): + def net_attr(self, netname, attr, defval=None): """Get an attribute of a given net (specified by name), or defval is not set""" pnet = None if attr in self.net_attrs(netname): @@ -101,7 +102,7 @@ # specifications are inconsistent in how they are represented in the JSON, # it's hard to make any nicer... - def cell_conns(self, cell, direction = "input"): + def cell_conns(self, cell, direction="input"): """The connections of a cell in a given direction as a 2-tuple Returns a list of tuples: @@ -135,7 +136,8 @@ if len(pdata["bits"]) == 1: conn_io.append(port) else: - conn_io.append("{}[{}]".format(port, pdata["bits"].index(net))) + conn_io.append("{}[{}]".format( + port, pdata["bits"].index(net))) return conn_io def conn_ports(self, net, pdir): @@ -157,10 +159,10 @@ if len(condata) == 1: conn_ports.append((cell, port)) else: - conn_ports.append((cell, "{}[{}]".format(port, condata.index(net)))) + conn_ports.append((cell, "{}[{}]".format( + port, condata.index(net)))) return conn_ports - def net_drivers(self, net): """Returns a list of drivers of a given net, both top level inputs. and cell outputs. "cell" is set to the name of the module for top level @@ -191,13 +193,13 @@ cell_drivers = self.conn_ports(net, "input") return io_drivers + cell_drivers -class YosysJSON: - def __init__(self, j, top = None): +class YosysJSON: + def __init__(self, j, top=None): """Takes either the filename to a JSON file, or already parsed JSON data as a dictionary. Optionally the top level module can be specified too.""" - if(isinstance(j, str)): + if (isinstance(j, str)): with open(j, 'r') as f: self.data = json.load(f) else:
diff --git a/yosys/run.py b/yosys/run.py index 18e6458..e43629f 100755 --- a/yosys/run.py +++ b/yosys/run.py
@@ -3,27 +3,33 @@ import tempfile, json import yosys.utils + def get_yosys(): """Return how to execute Yosys: the value of $YOSYS if set, otherwise just `yosys`.""" return os.getenv("YOSYS", "yosys") + def get_output(params): """Run Yosys with given command line parameters, and return stdout as a string""" cmd = [get_yosys()] + params return subprocess.check_output(cmd).decode("utf-8") + defines = [] + def add_define(defname): """Add a Verilog define to the list of defines to set in Yosys""" defines.append(defname) + def get_defines(): """Return a list of set Verilog defines, as a list of arguments to pass to Yosys `read_verilog`""" return " ".join(["-D" + _ for _ in defines]) -def commands(commands, infiles = []): + +def commands(commands, infiles=[]): """Run a given string containing Yosys commands Inputs @@ -31,11 +37,13 @@ commands : string of Yosys commands to run infiles : list of input files """ - commands = "read_verilog {} {}; ".format(get_defines(), " ".join(infiles)) + commands + commands = "read_verilog {} {}; ".format(get_defines(), + " ".join(infiles)) + commands params = ["-q", "-p", commands] return get_output(params) -def script(script, infiles = []): + +def script(script, infiles=[]): """Run a Yosys script given a path to the script Inputs @@ -46,7 +54,12 @@ params = ["-q", "-s", script] + infiles return get_output(params) -def vlog_to_json(infiles, flatten = False, aig = False, mode = None, module_with_mode = None): + +def vlog_to_json(infiles, + flatten=False, + aig=False, + mode=None, + module_with_mode=None): """ Convert Verilog to a JSON representation using Yosys @@ -89,7 +102,6 @@ return None - def do_select(infiles, module, expr): """ Run a Yosys select command (given the expression and input files) on a module @@ -101,26 +113,26 @@ module: Name of module to run command on expr: Yosys selector expression for select command """ - """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?""" - outfile = tempfile.mktemp() - sel_cmd = "prep -top {} -flatten; cd {}; select -write {} {}" .format(module, module, outfile, expr) + sel_cmd = "prep -top {} -flatten; cd {}; select -write {} {}".format( + module, module, outfile, expr) commands(sel_cmd, infiles) pins = [] with open(outfile, 'r') as f: for net in f: snet = net.strip() - if(len(snet) > 0): + if (len(snet) > 0): 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 a list of output ports which are combinational sinks of a given input. @@ -131,7 +143,9 @@ module: Name of module to run command on innet: Name of input net to find sinks of """ - return do_select(infiles, module, "{} %coe* o:* %i {} %d".format(innet, innet)) + return do_select(infiles, module, "{} %coe* o:* %i {} %d".format( + innet, innet)) + def list_clocks(infiles, module): """Return a list of clocks in the module @@ -143,6 +157,7 @@ """ return do_select(infiles, module, "c:* %x:+[CLK] a:CLOCK=1 %u c:* %d") + def get_clock_assoc_signals(infiles, module, clk): """Return the list of signals associated with a given clock. @@ -152,4 +167,7 @@ module: Name of module to run command on clk: Name of clock to find associated signals """ - return do_select(infiles, module, "select -list {} %x* i:* o:* %u %i a:ASSOC_CLOCK={} %u {} %d".format(clk, clk, clk)) + return do_select( + infiles, module, + "select -list {} %x* i:* o:* %u %i a:ASSOC_CLOCK={} %u {} %d".format( + clk, clk, clk))
diff --git a/yosys/utils.py b/yosys/utils.py index bea8acf..722ac9b 100644 --- a/yosys/utils.py +++ b/yosys/utils.py
@@ -1,9 +1,9 @@ #!/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)