pb_type.xml generation basics implemented

Signed-off-by: David Shah <davey1576@gmail.com>
diff --git a/.gitignore b/.gitignore
index 1af8baf..db17b36 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@
 *.pyc
 dump.json
 pb_type.xml
+merged.xml
diff --git a/tests/adder.v b/tests/adder/sim.v
similarity index 100%
rename from tests/adder.v
rename to tests/adder/sim.v
diff --git a/tests/dff.v b/tests/dff/sim.v
similarity index 74%
rename from tests/dff.v
rename to tests/dff/sim.v
index edebdfc..bda1a49 100644
--- a/tests/dff.v
+++ b/tests/dff/sim.v
@@ -1,3 +1,4 @@
+(* TYPE="bel" *) (* CLASS="flipflop" *)
 module dff(input clk, rst, d, output q);
 
 always @(posedge clk)
diff --git a/tests/run.sh b/tests/run.sh
new file mode 100755
index 0000000..23c8067
--- /dev/null
+++ b/tests/run.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+INPUT_DIRS="adder dff ."
+THIS_TOP="test_pb"
+
+for dir in $INPUT_DIRS; do
+	if [ "$dir" = "." ]; then
+		TOP=$THIS_TOP
+	else
+		TOP=$dir
+	fi
+	../vlog_to_model.py --top $TOP -o ${dir}/model.xml ${dir}/sim.v
+	../vlog_to_pbtype.py --top $TOP -o ${dir}/pb_type.xml ${dir}/sim.v
+done
+
+xmllint --xinclude --nsclean --noblanks --format ./pb_type.xml > ./merged.xml
diff --git a/tests/test_pb.v b/tests/sim.v
similarity index 80%
rename from tests/test_pb.v
rename to tests/sim.v
index 7b5a92a..eebd0ab 100644
--- a/tests/test_pb.v
+++ b/tests/sim.v
@@ -1,5 +1,5 @@
-`include "adder.v"
-`include "dff.v"
+`include "adder/sim.v"
+`include "dff/sim.v"
 
 module test_pb(input clk, rst, a, b, ci, output q, co);
 
diff --git a/vlog_to_model.py b/vlog_to_model.py
index 4296013..65d90b2 100755
--- a/vlog_to_model.py
+++ b/vlog_to_model.py
@@ -34,48 +34,6 @@
 
 aig_json = yosys.run.vlog_to_json(args.infiles, True, True)
 
-"""
-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.run.yosys_commands(sel_cmd, infiles)
-    pins = []
-    with open(outfile, 'r') as f:
-        for net in f:
-            snet = net.strip()
-            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 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:
@@ -90,14 +48,14 @@
 inports_xml = ET.SubElement(model_xml, "input_ports")
 outports_xml = ET.SubElement(model_xml, "output_ports")
 
-clocks = list_clocks(args.infiles, top)
+clocks = yosys.run.list_clocks(args.infiles, top)
 clk_sigs = dict()
 for clk in clocks:
-    clk_sigs[clk] = 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)
-    sinks = get_combinational_sinks(args.infiles, top, name)
+    sinks = yosys.run.get_combinational_sinks(args.infiles, top, name)
     if len(sinks) > 0:
         attrs["combinational_sink_ports"] = " ".join(sinks)
     if name in clocks:
diff --git a/vlog_to_pbtype.py b/vlog_to_pbtype.py
index d74c08c..1af272d 100755
--- a/vlog_to_pbtype.py
+++ b/vlog_to_pbtype.py
@@ -117,6 +117,18 @@
     # List of entries in format ((from_cell, from_pin), (to) 
     interconn = []
     
+    # Process IOs
+    clocks = yosys.run.list_clocks(args.infiles, mod.get_name())
+    for name, width, iodir in mod.get_ports():
+        ioattrs = {"name": name, "num_pins": str(width)}
+        if name in clocks:
+            ET.SubElement(pb_type_xml, "clock", ioattrs)
+        elif iodir == "input":
+            ET.SubElement(pb_type_xml, "input", ioattrs)
+        elif iodir == "output":
+            ET.SubElement(pb_type_xml, "output", ioattrs)
+        else:
+            assert False
     # Process cells
     for cname, i_of in mod.get_cells(False):
         pb_name = mod.get_cell_attr(cname, "PB_NAME", i_of)
diff --git a/yosys/run.py b/yosys/run.py
index 0730b3b..fa1ebff 100755
--- a/yosys/run.py
+++ b/yosys/run.py
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 import os, subprocess, sys, json
-import yosys.utils
+import yosys.utils, tempfile, re
 
 def get_yosys():
     return os.getenv('YOSYS', "yosys")
@@ -25,3 +25,47 @@
     """with open('dump.json', 'w') as dbg:
         print(j,file=dbg)"""
     return json.loads(j)
+
+"""
+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_commands(sel_cmd, infiles)
+    pins = []
+    with open(outfile, 'r') as f:
+        for net in f:
+            snet = net.strip()
+            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 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))