Python to generate model.xml from Verilog

Signed-off-by: David Shah <davey1576@gmail.com>
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1697bbe
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+model.xml
+__pycache__
+*.pyc
+dump.json
diff --git a/tests/adder.v b/tests/adder.v
new file mode 100644
index 0000000..a454237
--- /dev/null
+++ b/tests/adder.v
@@ -0,0 +1,3 @@
+module adder(input a, b, ci, output y, co);
+assign {co, y} = a + b + ci;
+endmodule
diff --git a/tests/dff.v b/tests/dff.v
new file mode 100644
index 0000000..edebdfc
--- /dev/null
+++ b/tests/dff.v
@@ -0,0 +1,9 @@
+module dff(input clk, rst, d, output q);
+
+always @(posedge clk)
+	if (rst == 1'b1)
+		q <= 1'b0;
+	else
+		q <= d;
+
+endmodule
diff --git a/vlog_to_model.py b/vlog_to_model.py
new file mode 100755
index 0000000..cfbbc59
--- /dev/null
+++ b/vlog_to_model.py
@@ -0,0 +1,92 @@
+#!/usr/bin/python3
+import yosys_exec
+import lxml.etree as ET
+import argparse, re
+import os, tempfile
+from yosys_json import YosysJson
+
+"""
+Convert a Verilog simulation model to a VPR `model.xml`
+"""
+
+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')
+
+args = parser.parse_args()
+
+aig_json = yosys_exec.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?
+
+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)
+    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))
+    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")
+
+def get_clock_assoc_signals(infiles, module, clk):
+    return do_select(infiles, module, "select -list %s %%x* i:* o:* %%u %%i %s %%d" % (clk, clk))
+
+if "top" in args:
+    yj = YosysJson(aig_json, args.top)
+else:
+    yj = YosysJson(aig_json)    
+
+top = yj.get_top()
+models = ET.Element("models")
+model = ET.SubElement(models, "model", dict(name=top))
+ports = yj.get_ports()
+
+inports = ET.SubElement(model, "input_ports")
+outports = ET.SubElement(model, "output_ports")
+
+clocks = list_clocks(args.infiles, top)
+clk_sigs = dict()
+for clk in clocks:
+    clk_sigs[clk] = get_clock_assoc_signals(args.infiles, top, clk)
+
+for name, width, iodir in ports:
+    attrs = dict(name=name)
+    sinks = get_combinational_sinks(args.infiles, top, name)
+    if len(sinks) > 0:
+        attrs["combinational_sink_ports"] = " ".join(sinks)
+    if name in clocks:
+        attrs["is_clock"] = "1"
+    for clk in clocks:
+        if name in clk_sigs[clk]:
+            attrs["clock"] = clk
+    if iodir == "input":
+        ET.SubElement(inports, "port", attrs)
+    elif iodir == "output":
+        ET.SubElement(outports, "port", attrs)
+    else:
+        assert(False) #how does VPR specify inout (only applicable for PACKAGEPIN of an IO primitive)
+
+outfile = "model.xml"
+if "o" in args and args.o is not None:
+    outfile = args.o
+    
+f = open(outfile, 'w')
+f.write(ET.tostring(models, pretty_print=True).decode('utf-8'))
+f.close()
diff --git a/yosys_exec.py b/yosys_exec.py
new file mode 100755
index 0000000..29d8e7e
--- /dev/null
+++ b/yosys_exec.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+import os, subprocess, sys, json
+import yosys_utils
+
+def get_yosys():
+    return os.getenv('YOSYS', "yosys")
+
+def run_yosys_get_output(params):
+    cmd = [get_yosys()] + params
+    return subprocess.check_output(cmd).decode("utf-8")
+
+def run_yosys_commands(commands, infiles = []):
+    params = ["-q", "-p", commands] + infiles
+    return run_yosys_get_output(params)
+
+def run_yosys_script(script, infiles = []):
+    params = ["-q", "-s", script] + infiles
+    return run_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))
+    """with open('dump.json', 'w') as dbg:
+        print(j,file=dbg)"""
+    return json.loads(j)
diff --git a/yosys_json.py b/yosys_json.py
new file mode 100755
index 0000000..5b6ed39
--- /dev/null
+++ b/yosys_json.py
@@ -0,0 +1,58 @@
+#!/usr/bin/python3
+
+import json
+import os, sys
+
+"""
+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
+and/or in AIG format. In any case, at minimum `proc` and usually `prep` should
+be used before outputting the JSON.
+"""
+
+class YosysJson:
+    # Takes either a filename, or already parsed JSON as a dictionary
+    # Also optionally pass the top level module
+    def __init__(self, j, top = None):
+        if(isinstance(j, str)):
+            with open(j, 'r') as f:
+                self.data = json.load(f)
+        else:
+            self.data = j
+        if top is None:
+            if len(self.data["modules"]) == 1:
+                self.top = list(self.data["modules"].keys())[0]
+            else:
+                self.top = None
+        else:
+            self.top = top
+    
+    def get_top(self):
+        if self.top is None:
+            print("no top module selected")
+            assert(False)
+        return self.top
+    
+    # Return a list of ports of a module (default: top), as a 3-tuple (name, width, dir)
+    def get_ports(self, module = None):
+        if module is None:
+            if self.top is None:
+                print("no top module selected, can't get_ports")
+                assert(False)
+            module = self.top
+        plist = []
+        for port, pdata in self.data["modules"][module]["ports"].items():
+            plist.append((port, len(pdata["bits"]), pdata["direction"]))
+        return plist
+    
+    # Return a list of cells of a module, as a 2-tuple (name, type)
+    def get_cells(self, module = None, include_hidden = True):
+        if module is None:
+            if self.top is None:
+                print("no top module selected, can't get_cells")
+                assert(False)
+            module = self.top
+        clist = []
+        for cell, cdata in self.data["modules"][module]["cells"].items():
+            if include_hidden or not cdata["hide_name"]:
+                clist.append((cell, cdata["type"]))
diff --git a/yosys_utils.py b/yosys_utils.py
new file mode 100644
index 0000000..9db082a
--- /dev/null
+++ b/yosys_utils.py
@@ -0,0 +1,11 @@
+#!/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