utils: Lots of mux_gen improvements.

Signed-off-by: Tim 'mithro' Ansell <mithro@mithis.com>
diff --git a/v2x/mux_gen.py b/v2x/mux_gen.py
index f2323d5..a4323b5 100755
--- a/v2x/mux_gen.py
+++ b/v2x/mux_gen.py
@@ -70,6 +70,18 @@
     '--outdir', default=None,
     help="""Directory to output generated content too.""")
 
+parser.add_argument(
+    '--comment', default=None,
+    help="""Add some type of comment to the mux.""")
+
+parser.add_argument(
+    '--num_pb', default=1,
+    help="""Set the num_pb for the mux.""")
+
+parser.add_argument(
+    '--subckt', default=None,
+    help="""Override the subcircuit name.""")
+
 
 def clog2(x):
     """Ceiling log 2 of x.
@@ -144,6 +156,8 @@
 
 args = parser.parse_args()
 args.width_bits = clog2(args.width)
+if not args.subckt:
+    args.subckt = args.name_mux
 
 mypath = __file__
 
@@ -174,7 +188,7 @@
             skip_next = True
             continue
 
-    repo_args.append(arg)
+    repo_args.append(repr(arg))
     assert "--" not in arg[2:], "Can't have -- in argument value or name %r" % arg
 
 os.makedirs(outdir, exist_ok=True)
@@ -184,12 +198,17 @@
 Generated with mux_gen.py, run the following to regenerate in this directory;
 %s
 """ % " ".join(repo_args)
+if args.comment:
+    generated_with += args.comment
 
 # XML Files can't have "--" in them, so instead we use ~~
-generated_with_xml = """
-Generated with mux_gen.py, run the following to regenerate in this directory;
-%s
-""" % " ".join(a.replace("--", "~~") for a in repo_args)
+xml_comment = """
+Generated with %s
+""" % mypath
+if args.comment:
+    xml_comment += "\n"
+    xml_comment += args.comment.replace("--", "~~")
+    xml_comment += "\n"
 
 # Create a makefile to regenerate files.
 makefile_file = os.path.join(outdir, "Makefile")
@@ -257,9 +276,9 @@
 
 # Generate the Model XML form.
 models_xml = ET.Element('models')
-models_xml.append(ET.Comment(generated_with_xml))
+models_xml.append(ET.Comment(xml_comment))
 
-model_xml = ET.SubElement(models_xml, 'model', {'name': args.name_mux})
+model_xml = ET.SubElement(models_xml, 'model', {'name': args.subckt})
 
 input_ports = ET.SubElement(model_xml, 'input_ports')
 output_ports = ET.SubElement(model_xml, 'output_ports')
@@ -284,10 +303,10 @@
 pb_type_xml = ET.Element(
     'pb_type', {
         'name': args.name_mux,
-        'num_pb': '1',
-        'blif_model': '.subckt %s' % args.name_mux,
+        'num_pb': str(args.num_pb),
+        'blif_model': '.subckt %s' % args.subckt,
     })
-pb_type_xml.append(ET.Comment(generated_with_xml))
+pb_type_xml.append(ET.Comment(xml_comment))
 
 for type, name, width, index in port_names:
     ET.SubElement(
@@ -299,43 +318,54 @@
     )
 
 if args.type == 'logic':
-    #pb_type = ET.SubElement(
-    #    pb_type_xml,
-    #    'pb_type',
-    #    {},
-    #)
-    pass
+    for itype, iname, iwidth, iindex in port_names:
+        if itype not in ('i', 's'):
+            continue
 
-interconnect = ET.SubElement(pb_type_xml, 'interconnect')
-if args.type == 'routing':
-    ET.SubElement(
-        interconnect,
-        'mux', {
-            'name': 'OUT',
-            'input': ' '.join(n for t, n, w, i in port_names if t in ('i',)),
-            'output': args.name_output,
-        },
-    )
-else:
-    for type, name, width, index in port_names:
-        if type in ('i', 's'):
+        for otype, oname, owidth, oindex in port_names:
+            if otype not in ('o',):
+                continue
+
             ET.SubElement(
-                interconnect,
-                'direct', {
-                    'name': name,
-                    'input': name,
-                    'output': "MUX.%s%s" % (type.upper(), index),
+                pb_type_xml,
+                'delay_constant', {
+                    'max': "10e-12",
+                    'in_port': iname,
+                    'out_port': oname,
                 },
             )
-        elif type in ('o',):
-            ET.SubElement(
-                interconnect,
-                'direct', {
-                    'name': name,
-                    'output': name,
-                    'input': "MUX.%s%s" % (type.upper(), index),
-                },
-            )
+
+if False:
+    interconnect = ET.SubElement(pb_type_xml, 'interconnect')
+    if args.type == 'routing':
+        ET.SubElement(
+            interconnect,
+            'mux', {
+                'name': 'OUT',
+                'input': ' '.join(n for t, n, w, i in port_names if t in ('i',)),
+                'output': args.name_output,
+            },
+        )
+    else:
+        for type, name, width, index in port_names:
+            if type in ('i', 's'):
+                ET.SubElement(
+                    interconnect,
+                    'direct', {
+                        'name': name,
+                        'input': name,
+                        'output': "MUX.%s%s" % (type.upper(), index),
+                    },
+                )
+            elif type in ('o',):
+                ET.SubElement(
+                    interconnect,
+                    'direct', {
+                        'name': name,
+                        'output': name,
+                        'input': "MUX.%s%s" % (type.upper(), index),
+                    },
+                )
 
 pb_type_str = ET.tostring(pb_type_xml, pretty_print=True).decode('utf-8')
 print("pb_type.xml", "-"*75)