utils/vlog: Replace ALTERNATIVE_TO attr with MODE parameter

Signed-off-by: David Shah <davey1576@gmail.com>
diff --git a/tests/adder/model.xml b/tests/adder/adder.model.xml
similarity index 99%
rename from tests/adder/model.xml
rename to tests/adder/adder.model.xml
index 987196e..7b8f4f3 100644
--- a/tests/adder/model.xml
+++ b/tests/adder/adder.model.xml
@@ -6,8 +6,8 @@
       <port combinational_sink_ports="co y" name="ci"/>
     </input_ports>
     <output_ports>
-      <port name="y"/>
       <port name="co"/>
+      <port name="y"/>
     </output_ports>
   </model>
 </models>
diff --git a/tests/adder/adder.pb_type.xml b/tests/adder/adder.pb_type.xml
new file mode 100644
index 0000000..c01378e
--- /dev/null
+++ b/tests/adder/adder.pb_type.xml
@@ -0,0 +1,13 @@
+<pb_type xmlns:xi="http://www.w3.org/2001/XInclude" blif_model=".subckt adder" name="BEL_BB-adder" num_pb="1">
+  <input equivalent="false" name="a" num_pins="1"/>
+  <input equivalent="false" name="b" num_pins="1"/>
+  <input equivalent="false" name="ci" num_pins="1"/>
+  <output equivalent="false" name="co" num_pins="1"/>
+  <output equivalent="false" name="y" num_pins="1"/>
+  <delay_constant in_port="BEL_BB-adder.a" max="10e-12" out_port="BEL_BB-adder.co"/>
+  <delay_constant in_port="BEL_BB-adder.b" max="10e-12" out_port="BEL_BB-adder.co"/>
+  <delay_constant in_port="BEL_BB-adder.ci" max="7e-12" out_port="BEL_BB-adder.co"/>
+  <delay_constant in_port="BEL_BB-adder.a" max="20e-12" out_port="BEL_BB-adder.y"/>
+  <delay_constant in_port="BEL_BB-adder.b" max="20e-12" out_port="BEL_BB-adder.y"/>
+  <delay_constant in_port="BEL_BB-adder.ci" max="10e-12" out_port="BEL_BB-adder.y"/>
+</pb_type>
diff --git a/tests/adder/pb_type.xml b/tests/adder/pb_type.xml
deleted file mode 100644
index e48eb59..0000000
--- a/tests/adder/pb_type.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-<pb_type xmlns:xi="http://www.w3.org/2001/XInclude" blif_model=".subckt adder" name="BLK_BB-adder" num_pb="1">
-  <input equivalent="false" name="a" num_pins="1"/>
-  <input equivalent="false" name="b" num_pins="1"/>
-  <input equivalent="false" name="ci" num_pins="1"/>
-  <output equivalent="false" name="y" num_pins="1"/>
-  <output equivalent="false" name="co" num_pins="1"/>
-  <delay_constant in_port="BLK_BB-adder.a" max="20e-12" out_port="BLK_BB-adder.y"/>
-  <delay_constant in_port="BLK_BB-adder.b" max="20e-12" out_port="BLK_BB-adder.y"/>
-  <delay_constant in_port="BLK_BB-adder.ci" max="10e-12" out_port="BLK_BB-adder.y"/>
-  <delay_constant in_port="BLK_BB-adder.a" max="10e-12" out_port="BLK_BB-adder.co"/>
-  <delay_constant in_port="BLK_BB-adder.b" max="10e-12" out_port="BLK_BB-adder.co"/>
-  <delay_constant in_port="BLK_BB-adder.ci" max="7e-12" out_port="BLK_BB-adder.co"/>
-</pb_type>
diff --git a/tests/dff/model.xml b/tests/dff/dff.model.xml
similarity index 100%
rename from tests/dff/model.xml
rename to tests/dff/dff.model.xml
diff --git a/tests/dff/dff.pb_type.xml b/tests/dff/dff.pb_type.xml
new file mode 100644
index 0000000..2e019d9
--- /dev/null
+++ b/tests/dff/dff.pb_type.xml
@@ -0,0 +1,8 @@
+<pb_type xmlns:xi="http://www.w3.org/2001/XInclude" blif_model=".latch" class="flipflop" name="BEL_FF-dff" num_pb="1">
+  <clock equivalent="false" name="clk" num_pins="1" port_class="clock"/>
+  <input equivalent="false" name="d" num_pins="1" port_class="D"/>
+  <output equivalent="false" name="q" num_pins="1" port_class="Q"/>
+  <T_setup clock="clk" port="BEL_FF-dff.d" value="200e-12"/>
+  <T_hold clock="clk" port="BEL_FF-dff.d" value="50e-12"/>
+  <T_clock_to_Q clock="clk" max="400e-12" port="BEL_FF-dff.q"/>
+</pb_type>
diff --git a/tests/dff/pb_type.xml b/tests/dff/pb_type.xml
deleted file mode 100644
index 60e40f9..0000000
--- a/tests/dff/pb_type.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-<pb_type xmlns:xi="http://www.w3.org/2001/XInclude" blif_model=".latch" class="flipflop" name="BLK_FF-dff" num_pb="1">
-  <clock equivalent="false" name="clk" num_pins="1" port_class="clock"/>
-  <input equivalent="false" name="d" num_pins="1" port_class="D"/>
-  <output equivalent="false" name="q" num_pins="1" port_class="Q"/>
-  <T_setup clock="clk" port="BLK_FF-dff.d" value="200e-12"/>
-  <T_hold clock="clk" port="BLK_FF-dff.d" value="50e-12"/>
-  <T_clock_to_Q clock="clk" max="400e-12" port="BLK_FF-dff.q"/>
-</pb_type>
diff --git a/tests/lut2/model.xml b/tests/lut2/lut2.model.xml
similarity index 100%
rename from tests/lut2/model.xml
rename to tests/lut2/lut2.model.xml
diff --git a/tests/lut2/pb_type.xml b/tests/lut2/lut2.pb_type.xml
similarity index 67%
rename from tests/lut2/pb_type.xml
rename to tests/lut2/lut2.pb_type.xml
index 1c6d780..0d63ab2 100644
--- a/tests/lut2/pb_type.xml
+++ b/tests/lut2/lut2.pb_type.xml
@@ -1,7 +1,7 @@
-<pb_type xmlns:xi="http://www.w3.org/2001/XInclude" blif_model=".subckt lut2" name="BLK_BB-lut2" num_pb="1">
+<pb_type xmlns:xi="http://www.w3.org/2001/XInclude" blif_model=".subckt lut2" name="BEL_BB-lut2" num_pb="1">
   <input equivalent="false" name="in" num_pins="2"/>
   <output equivalent="false" name="y" num_pins="1"/>
-  <delay_matrix in_port="BLK_BB-lut2.in" out_port="BLK_BB-lut2.y" type="max">
+  <delay_matrix in_port="BEL_BB-lut2.in" out_port="BEL_BB-lut2.y" type="max">
 10e-12
  10e-12
 </delay_matrix>
diff --git a/tests/merged.xml b/tests/merged.xml
new file mode 100644
index 0000000..ae5baac
--- /dev/null
+++ b/tests/merged.xml
@@ -0,0 +1,74 @@
+<?xml version="1.0"?>
+<pb_type xmlns:xi="http://www.w3.org/2001/XInclude" name="BLK_IG-tests" num_pb="1">
+  <input equivalent="false" name="a" num_pins="1"/>
+  <input equivalent="false" name="b" num_pins="1"/>
+  <input equivalent="false" name="ci" num_pins="1"/>
+  <input equivalent="false" name="clk" num_pins="1"/>
+  <output equivalent="false" name="co" num_pins="1"/>
+  <output equivalent="false" name="q" num_pins="1"/>
+  <mode name="ADD">
+
+    <pb_type xmlns:xi="http://www.w3.org/2001/XInclude" blif_model=".subckt adder" name="BEL_BB-adder" num_pb="1" xml:base="adder/adder.pb_type.xml">
+      <input equivalent="false" name="a" num_pins="1"/>
+      <input equivalent="false" name="b" num_pins="1"/>
+      <input equivalent="false" name="ci" num_pins="1"/>
+      <output equivalent="false" name="co" num_pins="1"/>
+      <output equivalent="false" name="y" num_pins="1"/>
+      <delay_constant in_port="BEL_BB-adder.a" max="10e-12" out_port="BEL_BB-adder.co"/>
+      <delay_constant in_port="BEL_BB-adder.b" max="10e-12" out_port="BEL_BB-adder.co"/>
+      <delay_constant in_port="BEL_BB-adder.ci" max="7e-12" out_port="BEL_BB-adder.co"/>
+      <delay_constant in_port="BEL_BB-adder.a" max="20e-12" out_port="BEL_BB-adder.y"/>
+      <delay_constant in_port="BEL_BB-adder.b" max="20e-12" out_port="BEL_BB-adder.y"/>
+      <delay_constant in_port="BEL_BB-adder.ci" max="10e-12" out_port="BEL_BB-adder.y"/>
+    </pb_type>
+
+
+    <pb_type xmlns:xi="http://www.w3.org/2001/XInclude" blif_model=".latch" class="flipflop" name="BEL_FF-dff" num_pb="1" xml:base="dff/dff.pb_type.xml">
+      <clock equivalent="false" name="clk" num_pins="1" port_class="clock"/>
+      <input equivalent="false" name="d" num_pins="1" port_class="D"/>
+      <output equivalent="false" name="q" num_pins="1" port_class="Q"/>
+      <T_setup clock="clk" port="BEL_FF-dff.d" value="200e-12"/>
+      <T_hold clock="clk" port="BEL_FF-dff.d" value="50e-12"/>
+      <T_clock_to_Q clock="clk" max="400e-12" port="BEL_FF-dff.q"/>
+    </pb_type>
+
+    <interconnect>
+      <direct input="BLK_IG-tests.a" name="BEL_BB-adder_a" output="BEL_BB-adder.a"/>
+      <direct input="BLK_IG-tests.b" name="BEL_BB-adder_b" output="BEL_BB-adder.b"/>
+      <direct input="BLK_IG-tests.ci" name="BEL_BB-adder_ci" output="BEL_BB-adder.ci"/>
+      <direct input="BEL_BB-adder.co" name="BLK_IG-tests_co" output="BLK_IG-tests.co"/>
+      <direct input="BLK_IG-tests.clk" name="BEL_FF-dff_clk" output="BEL_FF-dff.clk"/>
+      <direct input="BEL_BB-adder.y" name="BEL_FF-dff_d" output="BEL_FF-dff.d"/>
+      <direct input="BEL_FF-dff.q" name="BLK_IG-tests_q" output="BLK_IG-tests.q"/>
+    </interconnect>
+  </mode>
+  <mode name="LUT">
+
+    <pb_type xmlns:xi="http://www.w3.org/2001/XInclude" blif_model=".latch" class="flipflop" name="BEL_FF-dff" num_pb="1" xml:base="dff/dff.pb_type.xml">
+      <clock equivalent="false" name="clk" num_pins="1" port_class="clock"/>
+      <input equivalent="false" name="d" num_pins="1" port_class="D"/>
+      <output equivalent="false" name="q" num_pins="1" port_class="Q"/>
+      <T_setup clock="clk" port="BEL_FF-dff.d" value="200e-12"/>
+      <T_hold clock="clk" port="BEL_FF-dff.d" value="50e-12"/>
+      <T_clock_to_Q clock="clk" max="400e-12" port="BEL_FF-dff.q"/>
+    </pb_type>
+
+
+    <pb_type xmlns:xi="http://www.w3.org/2001/XInclude" blif_model=".subckt lut2" name="BEL_BB-lut2" num_pb="1" xml:base="lut2/lut2.pb_type.xml">
+      <input equivalent="false" name="in" num_pins="2"/>
+      <output equivalent="false" name="y" num_pins="1"/>
+      <delay_matrix in_port="BEL_BB-lut2.in" out_port="BEL_BB-lut2.y" type="max">
+10e-12
+ 10e-12
+</delay_matrix>
+    </pb_type>
+
+    <interconnect>
+      <direct input="BLK_IG-tests.clk" name="BEL_FF-dff_clk" output="BEL_FF-dff.clk"/>
+      <direct input="BEL_BB-lut2.y" name="BEL_FF-dff_d" output="BEL_FF-dff.d"/>
+      <direct input="BEL_FF-dff.q" name="BLK_IG-tests_q" output="BLK_IG-tests.q"/>
+      <direct input="BLK_IG-tests.a" name="BEL_BB-lut2_in_0" output="BEL_BB-lut2.in[0]"/>
+      <direct input="BLK_IG-tests.b" name="BEL_BB-lut2_in_1" output="BEL_BB-lut2.in[1]"/>
+    </interconnect>
+  </mode>
+</pb_type>
diff --git a/tests/pb_type.xml b/tests/pb_type.xml
deleted file mode 100644
index a4b7408..0000000
--- a/tests/pb_type.xml
+++ /dev/null
@@ -1,32 +0,0 @@
-<pb_type xmlns:xi="http://www.w3.org/2001/XInclude" name="BLK_IG-test_pb" num_pb="1">
-  <input equivalent="false" name="clk" num_pins="1"/>
-  <input equivalent="false" name="a" num_pins="1"/>
-  <input equivalent="false" name="b" num_pins="1"/>
-  <input equivalent="false" name="ci" num_pins="1"/>
-  <output equivalent="false" name="q" num_pins="1"/>
-  <output equivalent="false" name="co" num_pins="1"/>
-  <mode name="test_add">
-    <xi:include href="adder/pb_type.xml"/>
-    <xi:include href="dff/pb_type.xml"/>
-    <interconnect>
-      <direct input="test_pb.a" name="BLK_BB-adder_a" output="BLK_BB-adder.a"/>
-      <direct input="test_pb.b" name="BLK_BB-adder_b" output="BLK_BB-adder.b"/>
-      <direct input="test_pb.ci" name="BLK_BB-adder_ci" output="BLK_BB-adder.ci"/>
-      <direct input="BLK_BB-adder.co" name="test_pb_co" output="test_pb.co"/>
-      <direct input="test_pb.clk" name="BLK_FF-dff_clk" output="BLK_FF-dff.clk"/>
-      <direct input="BLK_BB-adder.y" name="BLK_FF-dff_d" output="BLK_FF-dff.d"/>
-      <direct input="BLK_FF-dff.q" name="test_pb_q" output="test_pb.q"/>
-    </interconnect>
-  </mode>
-  <mode name="test_lut">
-    <xi:include href="dff/pb_type.xml"/>
-    <xi:include href="lut2/pb_type.xml"/>
-    <interconnect>
-      <direct input="test_pb.clk" name="BLK_FF-dff_clk" output="BLK_FF-dff.clk"/>
-      <direct input="BLK_BB-lut2.y" name="BLK_FF-dff_d" output="BLK_FF-dff.d"/>
-      <direct input="BLK_FF-dff.q" name="test_pb_q" output="test_pb.q"/>
-      <direct input="test_pb.a" name="BLK_BB-lut2_in_0" output="BLK_BB-lut2.in[0]"/>
-      <direct input="test_pb.b" name="BLK_BB-lut2_in_1" output="BLK_BB-lut2.in[1]"/>
-    </interconnect>
-  </mode>
-</pb_type>
diff --git a/tests/run.sh b/tests/run.sh
index 7c49ce6..a13c8a5 100755
--- a/tests/run.sh
+++ b/tests/run.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 INPUT_DIRS="adder dff lut2 ."
-THIS_TOP="test_pb"
+THIS_TOP="tests"
 
 for dir in $INPUT_DIRS; do
 	if [ "$dir" = "." ]; then
@@ -8,8 +8,8 @@
 	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
+	../vlog_to_model.py --top $TOP -o ${dir}/${TOP}.model.xml ${dir}/${TOP}.sim.v
+	../vlog_to_pbtype.py --top $TOP -o ${dir}/${TOP}.pb_type.xml ${dir}/${TOP}.sim.v
 done
 
-xmllint --xinclude --nsclean --noblanks --format ./pb_type.xml > ./merged.xml
+xmllint --xinclude --nsclean --noblanks --format ./tests.pb_type.xml > ./merged.xml
diff --git a/tests/model.xml b/tests/tests.model.xml
similarity index 90%
rename from tests/model.xml
rename to tests/tests.model.xml
index 5bbe083..c51de8e 100644
--- a/tests/model.xml
+++ b/tests/tests.model.xml
@@ -1,14 +1,14 @@
 <models>
-  <model name="test_pb">
+  <model name="tests">
     <input_ports>
-      <port name="clk"/>
       <port name="a"/>
       <port name="b"/>
       <port name="ci"/>
+      <port name="clk"/>
     </input_ports>
     <output_ports>
-      <port name="q"/>
       <port name="co"/>
+      <port name="q"/>
     </output_ports>
   </model>
 </models>
diff --git a/tests/tests.pb_type.xml b/tests/tests.pb_type.xml
new file mode 100644
index 0000000..8ae380e
--- /dev/null
+++ b/tests/tests.pb_type.xml
@@ -0,0 +1,32 @@
+<pb_type xmlns:xi="http://www.w3.org/2001/XInclude" name="BLK_IG-tests" num_pb="1">
+  <input equivalent="false" name="a" num_pins="1"/>
+  <input equivalent="false" name="b" num_pins="1"/>
+  <input equivalent="false" name="ci" num_pins="1"/>
+  <input equivalent="false" name="clk" num_pins="1"/>
+  <output equivalent="false" name="co" num_pins="1"/>
+  <output equivalent="false" name="q" num_pins="1"/>
+  <mode name="ADD">
+    <xi:include href="adder/adder.pb_type.xml"/>
+    <xi:include href="dff/dff.pb_type.xml"/>
+    <interconnect>
+      <direct input="BLK_IG-tests.a" name="BEL_BB-adder_a" output="BEL_BB-adder.a"/>
+      <direct input="BLK_IG-tests.b" name="BEL_BB-adder_b" output="BEL_BB-adder.b"/>
+      <direct input="BLK_IG-tests.ci" name="BEL_BB-adder_ci" output="BEL_BB-adder.ci"/>
+      <direct input="BEL_BB-adder.co" name="BLK_IG-tests_co" output="BLK_IG-tests.co"/>
+      <direct input="BLK_IG-tests.clk" name="BEL_FF-dff_clk" output="BEL_FF-dff.clk"/>
+      <direct input="BEL_BB-adder.y" name="BEL_FF-dff_d" output="BEL_FF-dff.d"/>
+      <direct input="BEL_FF-dff.q" name="BLK_IG-tests_q" output="BLK_IG-tests.q"/>
+    </interconnect>
+  </mode>
+  <mode name="LUT">
+    <xi:include href="dff/dff.pb_type.xml"/>
+    <xi:include href="lut2/lut2.pb_type.xml"/>
+    <interconnect>
+      <direct input="BLK_IG-tests.clk" name="BEL_FF-dff_clk" output="BEL_FF-dff.clk"/>
+      <direct input="BEL_BB-lut2.y" name="BEL_FF-dff_d" output="BEL_FF-dff.d"/>
+      <direct input="BEL_FF-dff.q" name="BLK_IG-tests_q" output="BLK_IG-tests.q"/>
+      <direct input="BLK_IG-tests.a" name="BEL_BB-lut2_in_0" output="BEL_BB-lut2.in[0]"/>
+      <direct input="BLK_IG-tests.b" name="BEL_BB-lut2_in_1" output="BEL_BB-lut2.in[1]"/>
+    </interconnect>
+  </mode>
+</pb_type>
diff --git a/tests/tests.sim.v b/tests/tests.sim.v
index 1151952..478cb36 100644
--- a/tests/tests.sim.v
+++ b/tests/tests.sim.v
@@ -1,26 +1,20 @@
-`include "adder/sim.v"
-`include "dff/sim.v"
-`include "lut2/sim.v"
+`include "adder/adder.sim.v"
+`include "dff/dff.sim.v"
+`include "lut2/lut2.sim.v"
 
 
-(* blackbox *)
-module test_pb(input clk, a, b, ci, output q, co);
-endmodule
-
-(* ALTERNATIVE_TO="test_pb" *)
-module test_add(input clk, a, b, ci, output q, co);
-
-wire d;
-adder adder_i(.a(a), .b(b), .ci(ci), .y(d), .co(co));
-dff dff_i(.clk(clk), .d(d), .q(q));
-
-endmodule
-
-(* ALTERNATIVE_TO="test_pb" *)
-module test_lut(input clk, a, b, ci, output q, co);
-
-wire d;
-lut2 lut2_i(.in({b, a}), .y(d));
-dff dff_i(.clk(clk), .d(d), .q(q));
+(* MODES="ADD,LUT" *)
+module tests(input clk, a, b, ci, output q, co);
+  parameter MODE = "ADD";
+  wire d;
+  generate
+    if(MODE == "ADD") begin
+    adder adder_i(.a(a), .b(b), .ci(ci), .y(d), .co(co));
+    end else if(MODE == "LUT") begin
+    assign co = 0;
+    lut2 lut2_i(.in({b, a}), .y(d));
+    end
+  endgenerate
+  dff dff_i(.clk(clk), .d(d), .q(q));
 
 endmodule
diff --git a/vlog_to_pbtype.py b/vlog_to_pbtype.py
index 1e0e27f..51e471b 100755
--- a/vlog_to_pbtype.py
+++ b/vlog_to_pbtype.py
@@ -13,9 +13,10 @@
     - `(* CLASS="lut|routing|mux|flipflop|mem" *)` : specify the class of an given
     instance.
 
-    - `(* 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.
+    - `(* MODES="mode1;mode2;..." *)` : specify that the module has more than one functional
+    mode, each with a given name. The module will be evaluated n times, each time setting
+    the MODE parameter to the nth value in the list of mode names. Each evaluation will be
+    put in a pb_type `<mode>` section named accordingly.
 
     - `(* MODEL_NAME="model" *)` : override the name used for <model> and for
     ".subckt name" in the BLIF model. Mostly intended for use with w.py, when several
@@ -104,8 +105,8 @@
     """Convert a Verilog module to a pb_type name in the format documented here:
     https://github.com/SymbiFlow/symbiflow-arch-defs/#names"""
     is_blackbox = (mod.attr("blackbox", 0) == 1)
-    modes = yj.modules_with_attr("ALTERNATIVE_TO", mod.name)
-    has_modes = len(modes) > 0
+    modes = mod.attr("MODES", None)
+    has_modes = modes is not None
     # Process type and class of module
     mod_cls = mod.CLASS
     if mod_cls == "routing":
@@ -249,14 +250,16 @@
     generate."""
 
     attrs = mod.module_attrs
-    modes = yj.modules_with_attr("ALTERNATIVE_TO", mod.name)
+    modes = mod.attr("MODES", None)
+    if modes is not None:
+        modes = modes.split(",")
     mod_pname = mod_pb_name(mod)
 
     pb_xml_attrs = dict()
     pb_xml_attrs["name"] = mod_pname
     # If we are a blackbox with no modes, then generate a blif_model
     is_blackbox = (mod.attr("blackbox", 0) == 1)
-    has_modes = len(modes) > 0
+    has_modes = modes is not None
     # Process type and class of module
     mod_cls = mod.CLASS
     if mod_cls is not None:
@@ -297,9 +300,12 @@
         else:
             assert False
 
-    if len(modes) > 0:
-        for mode_mod in modes:
-            mode_xml = ET.SubElement(pb_type_xml, "mode", {"name" : mode_mod.name})
+    if has_modes:
+        for mode in modes:
+            mode_xml = ET.SubElement(pb_type_xml, "mode", {"name" : mode})
+            # Rerun Yosys with mode parameter
+            mode_yj = YosysJson(yosys.run.vlog_to_json(args.infiles, False, False, mode, mod.name))
+            mode_mod = mode_yj.module(mod.name)
             make_pb_content(mode_mod, mode_xml, mod_pname, True)
     else:
         make_pb_content(mod, pb_type_xml, mod_pname)
diff --git a/yosys/run.py b/yosys/run.py
index 4eb0755..f34ad57 100755
--- a/yosys/run.py
+++ b/yosys/run.py
@@ -27,7 +27,7 @@
     params = ["-q", "-s", script] + infiles
     return get_output(params)
 
-def vlog_to_json(infiles, flatten = False, aig = False):
+def vlog_to_json(infiles, flatten = False, aig = False, mode = None, mode_mod = None):
     """
     Convert Verilog to a JSON representation using Yosys
 
@@ -36,10 +36,16 @@
     infiles : list of input files
     flatten : set to flatten output hierarchy
     aig : generate And-Inverter-Graph modules for gates
+    mode : set to a value other than None to use `chparam` to set the value of the MODE parameter
+    mode_mod : the name of the module to apply `mode` to
     """
     prep_opts = "-flatten" if flatten else ""
     json_opts = "-aig" if aig else ""
-    cmds = "prep %s; write_json %s" % (prep_opts, json_opts)
+    if mode is not None:
+        mode_str = "chparam -set MODE \"%s\" %s; " % (mode, mode_mod)
+    else:
+        mode_str = ""
+    cmds = "%sprep %s; write_json %s" % (mode_str, prep_opts, json_opts)
     j = yosys.utils.strip_yosys_json(commands(cmds, infiles))
     """with open('dump.json', 'w') as dbg:
         print(j,file=dbg)"""