Add support for combinational delay specification in Verilog

Signed-off-by: David Shah <davey1576@gmail.com>
diff --git a/tests/adder/pb_type.xml b/tests/adder/pb_type.xml
index 05124b1..e675790 100644
--- a/tests/adder/pb_type.xml
+++ b/tests/adder/pb_type.xml
@@ -5,4 +5,10 @@
   <output equivalent="false" name="y" num_pins="1"/>
   <output equivalent="false" name="co" num_pins="1"/>
   <interconnect/>
+  <delay_constant in_port="adder.a" max="20e-12" out_port="adder.y"/>
+  <delay_constant in_port="adder.b" max="20e-12" out_port="adder.y"/>
+  <delay_constant in_port="adder.ci" max="10e-12" out_port="adder.y"/>
+  <delay_constant in_port="adder.a" max="10e-12" out_port="adder.co"/>
+  <delay_constant in_port="adder.b" max="10e-12" out_port="adder.co"/>
+  <delay_constant in_port="adder.ci" max="7e-12" out_port="adder.co"/>
 </pb_type>
diff --git a/tests/adder/sim.v b/tests/adder/sim.v
index a454237..2d9be56 100644
--- a/tests/adder/sim.v
+++ b/tests/adder/sim.v
@@ -1,3 +1,14 @@
-module adder(input a, b, ci, output y, co);
+module adder(
+	input a,
+	input b,
+	input ci,
+	(* DELAY_CONST_a = "20e-12" *)
+	(* DELAY_CONST_b = "20e-12" *)
+	(* DELAY_CONST_ci = "10e-12" *)
+	output y,
+	(* DELAY_CONST_a = "10e-12" *)
+	(* DELAY_CONST_b = "10e-12" *)
+	(* DELAY_CONST_ci = "7e-12" *)
+	output co);
 assign {co, y} = a + b + ci;
 endmodule
diff --git a/tests/lut2/model.xml b/tests/lut2/model.xml
index cb6470d..f062f9f 100644
--- a/tests/lut2/model.xml
+++ b/tests/lut2/model.xml
@@ -1,8 +1,7 @@
 <models>
   <model name="lut2">
     <input_ports>
-      <port combinational_sink_ports="y" name="a"/>
-      <port combinational_sink_ports="y" name="b"/>
+      <port combinational_sink_ports="y" name="in"/>
     </input_ports>
     <output_ports>
       <port name="y"/>
diff --git a/tests/lut2/pb_type.xml b/tests/lut2/pb_type.xml
index 96896f1..953d8f3 100644
--- a/tests/lut2/pb_type.xml
+++ b/tests/lut2/pb_type.xml
@@ -1,7 +1,10 @@
 <pb_type xmlns:xi="http://www.w3.org/2001/XInclude" name="lut2" num_pb="1">
-  <input equivalent="false" name="a" num_pins="1"/>
-  <input equivalent="false" name="b" num_pins="1"/>
+  <input equivalent="false" name="in" num_pins="2"/>
   <output equivalent="false" name="y" num_pins="1"/>
   <output equivalent="false" name="co" num_pins="1"/>
   <interconnect/>
+  <delay_matrix in_port="lut2.in" out_port="lut2.y" type="max">
+10e-12
+ 10e-12
+</delay_matrix>
 </pb_type>
diff --git a/tests/lut2/sim.v b/tests/lut2/sim.v
index a1143b3..34c235d 100644
--- a/tests/lut2/sim.v
+++ b/tests/lut2/sim.v
@@ -1,5 +1,9 @@
-module lut2 (input a, b, output y, co);
+module lut2 (
+		input [1:0] in,
+		(* DELAY_MATRIX_in = "10e-12; 10e-12" *)
+		output y,
+		output co);
 parameter [3:0] content = 4'b1000;
-assign y = content[{b, a}];
+assign y = content[in];
 assign co = 1'b0;
 endmodule
diff --git a/tests/pb_type.xml b/tests/pb_type.xml
index 095c270..83bcaf3 100644
--- a/tests/pb_type.xml
+++ b/tests/pb_type.xml
@@ -28,8 +28,8 @@
       <direct input="lut2.y" name="dff_d" output="dff.d"/>
       <direct input="test_lut.rst" name="dff_rst" output="dff.rst"/>
       <direct input="dff.q" name="test_lut_q" output="test_lut.q"/>
-      <direct input="test_lut.a" name="lut2_a" output="lut2.a"/>
-      <direct input="test_lut.b" name="lut2_b" output="lut2.b"/>
+      <direct input="test_lut.a" name="lut2_in[2]" output="lut2.in[2]"/>
+      <direct input="test_lut.b" name="lut2_in[2]" output="lut2.in[2]"/>
       <direct input="lut2.co" name="test_lut_co" output="test_lut.co"/>
     </interconnect>
   </mode>
diff --git a/tests/sim.v b/tests/sim.v
index 0500920..ed6ceff 100644
--- a/tests/sim.v
+++ b/tests/sim.v
@@ -20,7 +20,7 @@
 module test_lut(input clk, rst, a, b, ci, output q, co);
 
 wire d;
-lut2 lut2_i(.a(a), .b(b), .y(d), .co(co));
+lut2 lut2_i(.in({b, a}), .y(d), .co(co));
 dff dff_i(.clk(clk), .rst(rst), .d(d), .q(q));
 
 endmodule
diff --git a/vlog_to_pbtype.py b/vlog_to_pbtype.py
index 93fc5ab..5f51a63 100755
--- a/vlog_to_pbtype.py
+++ b/vlog_to_pbtype.py
@@ -16,15 +16,17 @@
     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
 
     - `(* 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
 
-    - `(* PB_MUX=1 *)` : if the signal is driven by a $mux cell, generate a
-    pb_type <mux> element for it
+    - `(* DELAY_CONST_{input}="30e-12" *)` : specify a constant max delay from an input (applied to the output)
+
+    - `(* DELAY_MATRIX_{input}="30e-12 35e-12; 20e-12 25e-12; ..." *)` : specify a VPR
+        delay matrix (semicolons indicate rows). In this format columns specify
+        inputs bits and rows specify output bits. This should be applied to the output.
 
 The following are allowed on ports:
     - `(* CLOCK *)` : force a given port to be a clock
@@ -154,6 +156,7 @@
     # Process timing
     for name, width, iodir in mod.ports:
         port = "%s.%s" % (mod.name, name)
+        # Clocked timing
         Tsetup = mod.net_attr(name, "SETUP")
         Thold = mod.net_attr(name, "HOLD")
         Tctoq = mod.net_attr(name, "CLK_TO_Q")
@@ -161,6 +164,31 @@
         process_clocked_tmg(Thold, port, "T_hold", xml_parent)
         process_clocked_tmg(Tctoq, port, "T_clock_to_Q", xml_parent)
 
+        # Combinational delays
+        dly_prefix = "DELAY_CONST_"
+        dly_mat_prefix = "DELAY_MATRIX_"
+        for attr, atvalue in mod.net_attrs(name).items():
+            if attr.startswith(dly_prefix):
+                # Single, constant delays
+                inp = attr[len(dly_prefix):]
+                inport = "%s.%s" % (mod.name, inp)
+                ET.SubElement(xml_parent, "delay_constant", {
+                    "in_port": inport,
+                    "out_port": port,
+                    "max": str(atvalue)
+                })
+            elif attr.startswith(dly_mat_prefix):
+                # Constant delay matrices
+                inp = attr[len(dly_mat_prefix):]
+                inport = "%s.%s" % (mod.name, inp)
+                mat = "\n" + atvalue.replace(";", "\n") + "\n"
+                xml_mat = ET.SubElement(xml_parent, "delay_matrix", {
+                    "in_port": inport,
+                    "out_port": port,
+                    "type": "max"
+                })
+                xml_mat.text = mat
+
 def make_pb_type(mod):
     """Build the pb_type for a given module. mod is the YosysModule object to
     generate."""
@@ -224,7 +252,6 @@
     else:
         make_pb_content(mod, pb_type_xml)
 
-    # TODO: timing
     return pb_type_xml
 
 pb_type_xml = make_pb_type(tmod)
diff --git a/yosys/json.py b/yosys/json.py
index 802e9d5..5bdc050 100755
--- a/yosys/json.py
+++ b/yosys/json.py
@@ -82,11 +82,15 @@
         else:
             return defval
 
+    def net_attrs(self, netname):
+        """Get all attributes of a given net as a dictionary"""
+        return self.data["netnames"][netname]["attributes"]
+
     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]
+        if attr in self.net_attrs(netname):
+            return self.net_attrs(netname)[attr]
         else:
             return defval