Improve timing-related attribute support

Signed-off-by: David Shah <davey1576@gmail.com>
diff --git a/tests/dff/pb_type.xml b/tests/dff/pb_type.xml
index 4b92f05..83b56e9 100644
--- a/tests/dff/pb_type.xml
+++ b/tests/dff/pb_type.xml
@@ -4,4 +4,7 @@
   <input equivalent="false" name="d" num_pins="1"/>
   <output equivalent="false" name="q" num_pins="1"/>
   <interconnect/>
+  <T_setup clock="clk" port="dff.d" value="200e-12"/>
+  <T_hold clock="clk" port="dff.d" value="50e-12"/>
+  <T_clock_to_Q clock="clk" max="400e-12" port="dff.q"/>
 </pb_type>
diff --git a/tests/dff/sim.v b/tests/dff/sim.v
index bda1a49..4e7731d 100644
--- a/tests/dff/sim.v
+++ b/tests/dff/sim.v
@@ -1,5 +1,14 @@
 (* TYPE="bel" *) (* CLASS="flipflop" *)
-module dff(input clk, rst, d, output q);
+module dff(clk, rst, d, q);
+
+input wire clk;
+input wire rst;
+
+(* SETUP = "clk 200e-12" *) (* HOLD = "clk 50e-12" *)
+input wire d;
+
+(* CLK_TO_Q = "clk 400e-12" *)
+output wire q;
 
 always @(posedge clk)
 	if (rst == 1'b1)
diff --git a/vlog_to_model.py b/vlog_to_model.py
index 53b6f83..7192b02 100755
--- a/vlog_to_model.py
+++ b/vlog_to_model.py
@@ -1,11 +1,16 @@
 #!/usr/bin/env python3
 """
 Convert a Verilog simulation model to a VPR `model.xml`
+
+The following Verilog attributes are considered on ports:
+    - `(* CLOCK *)` : force a given port to be a clock
+
+    - `(* ASSOC_CLOCK="RDCLK" *)` : force a port's associated clock to a given value
 """
 import yosys.run
 import lxml.etree as ET
 import argparse, re
-import os, tempfile
+import os, tempfile, sys
 from yosys.json import YosysJson
 
 
@@ -40,6 +45,10 @@
     yj = YosysJson(aig_json)
 
 top = yj.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")
+    sys.exit(1)
+
 tmod = yj.top_module
 models_xml = ET.Element("models")
 model_xml = ET.SubElement(models_xml, "model", {'name': top})
diff --git a/vlog_to_pbtype.py b/vlog_to_pbtype.py
index ca5ffa2..93fc5ab 100755
--- a/vlog_to_pbtype.py
+++ b/vlog_to_pbtype.py
@@ -5,32 +5,37 @@
 
 The following are allowed on a top level module:
 
-    - (* TYPE="bel|blackbox" *) : specify the type of the module
+    - `(* TYPE="bel|blackbox" *)` : specify the type of the module
     (either a Basic ELement or a blackbox named after the pb_type
 
-    - (* CLASS="lut|routing|flipflop|mem" *) : specify the class of an given
+    - `(* CLASS="lut|routing|flipflop|mem" *)` : specify the class of an given
     instance. Must be specified for BELs
 
-    - (* ALTERNATIVE_TO="module" *) : specify the module is one of several
+    - `(* ALTERNATIVE_TO="module" *)` : specify the module is one of several
     modes of another module (i.e. a <mode> in the pb_type). Note that all modes
     must be visible at the time of pb_type generation.
 
 The following are allowed on nets within modules (TODO: use proper Verilog timing):
     All are NYI at the moment!
-    - (* SETUP="clk 10e-12" *) : specify setup time for a given clock
+    - `(* SETUP="clk 10e-12" *)` : specify setup time for a given clock
 
-    - (* HOLD="clk 10e-12" *) : specify hold time for a given clock
+    - `(* HOLD="clk 10e-12" *)` : specify hold time for a given clock
 
-    - (* CLK_TO_Q="clk 10e-12" *) : specify clock-to-output time for a given clock
+    - `(* CLK_TO_Q="clk 10e-12" *)` : specify clock-to-output time for a given clock
 
-    - (* PB_MUX=1 *) : if the signal is driven by a $mux cell, generate a
+    - `(* PB_MUX=1 *)` : if the signal is driven by a $mux cell, generate a
     pb_type <mux> element for it
+
+The following are allowed on ports:
+    - `(* CLOCK *)` : force a given port to be a clock
+
+    - `(* ASSOC_CLOCK="RDCLK" *)` : force a port's associated clock to a given value
 """
 
 import yosys.run
 import lxml.etree as ET
 import argparse, re
-import os, tempfile
+import os, tempfile, sys
 from yosys.json import YosysJson
 
 
@@ -71,6 +76,11 @@
     top = args.top
 else:
     top = yj.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")
+    sys.exit(1)
+
 tmod = yj.module(top)
 
 def make_pb_content(mod, xml_parent):
@@ -129,6 +139,27 @@
     for source, dest in interconn:
         make_direct_conn(ic_xml, source, dest)
 
+    def process_clocked_tmg(tmgspec, port, xmltype, xml_parent):
+        """Add a suitable timing spec if necessary to the pb_type"""
+        if tmgspec is not None:
+            splitspec = tmgspec.split(" ")
+            assert len(splitspec) == 2
+            attrs = {"port": port, "clock": splitspec[0]}
+            if xmltype == "T_clock_to_Q":
+                attrs["max"] = splitspec[1]
+            else:
+                attrs["value"] = splitspec[1]
+            ET.SubElement(xml_parent, xmltype, attrs)
+
+    # Process timing
+    for name, width, iodir in mod.ports:
+        port = "%s.%s" % (mod.name, name)
+        Tsetup = mod.net_attr(name, "SETUP")
+        Thold = mod.net_attr(name, "HOLD")
+        Tctoq = mod.net_attr(name, "CLK_TO_Q")
+        process_clocked_tmg(Tsetup, port, "T_setup", xml_parent)
+        process_clocked_tmg(Thold, port, "T_hold", xml_parent)
+        process_clocked_tmg(Tctoq, port, "T_clock_to_Q", xml_parent)
 
 def make_pb_type(mod):
     """Build the pb_type for a given module. mod is the YosysModule object to
diff --git a/yosys/json.py b/yosys/json.py
index 517b465..802e9d5 100755
--- a/yosys/json.py
+++ b/yosys/json.py
@@ -82,6 +82,14 @@
         else:
             return defval
 
+    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.data["netnames"][netname]["attributes"]:
+            return self.data["netnames"][netname]["attributes"][attr]
+        else:
+            return defval
+
     # TODO: the below code is kind of ugly, but because module and cell IO
     # specifications are inconsistent in how they are represented in the JSON,
     # it's hard to make any nicer...
diff --git a/yosys/run.py b/yosys/run.py
index de471d9..7a2d6e3 100755
--- a/yosys/run.py
+++ b/yosys/run.py
@@ -80,8 +80,8 @@
 
 def list_clocks(infiles, module):
     """Return a list of clocks in the module"""
-    return do_select(infiles, module, "c:* %x:+[CLK] c:* %d")
+    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."""
-    return do_select(infiles, module, "select -list %s %%x* i:* o:* %%u %%i %s %%d" % (clk, clk))
+    return do_select(infiles, module, "select -list %s %%x* i:* o:* %%u %%i a:ASSOC_CLOCK=%s %%u %s %%d" % (clk, clk, clk))