utils/vlog: Generate XML includes in model.xml where there are Verilog includes

Signed-off-by: David Shah <davey1576@gmail.com>
diff --git a/tests/adder/adder.model.xml b/tests/adder/adder.model.xml
index 7b8f4f3..45f7e09 100644
--- a/tests/adder/adder.model.xml
+++ b/tests/adder/adder.model.xml
@@ -1,4 +1,4 @@
-<models>
+<models xmlns:xi="http://www.w3.org/2001/XInclude">
   <model name="adder">
     <input_ports>
       <port combinational_sink_ports="co y" name="a"/>
diff --git a/tests/dff/dff.model.xml b/tests/dff/dff.model.xml
index 07cce64..fba1fe9 100644
--- a/tests/dff/dff.model.xml
+++ b/tests/dff/dff.model.xml
@@ -1,4 +1,4 @@
-<models>
+<models xmlns:xi="http://www.w3.org/2001/XInclude">
   <model name="dff">
     <input_ports>
       <port is_clock="1" name="clk"/>
diff --git a/tests/lut2/lut2.model.xml b/tests/lut2/lut2.model.xml
index 6c721c2..c1f0ac0 100644
--- a/tests/lut2/lut2.model.xml
+++ b/tests/lut2/lut2.model.xml
@@ -1,4 +1,4 @@
-<models>
+<models xmlns:xi="http://www.w3.org/2001/XInclude">
   <model name="lut2">
     <input_ports>
       <port combinational_sink_ports="y" name="in"/>
diff --git a/tests/tests.model.xml b/tests/tests.model.xml
index c51de8e..bc8b783 100644
--- a/tests/tests.model.xml
+++ b/tests/tests.model.xml
@@ -1,14 +1,5 @@
-<models>
-  <model name="tests">
-    <input_ports>
-      <port name="a"/>
-      <port name="b"/>
-      <port name="ci"/>
-      <port name="clk"/>
-    </input_ports>
-    <output_ports>
-      <port name="co"/>
-      <port name="q"/>
-    </output_ports>
-  </model>
+<models xmlns:xi="http://www.w3.org/2001/XInclude">
+  <xi:include href="adder/adder.model.xml" xpointer="xpointer(models/child::node())"/>
+  <xi:include href="dff/dff.model.xml" xpointer="xpointer(models/child::node())"/>
+  <xi:include href="lut2/lut2.model.xml" xpointer="xpointer(models/child::node())"/>
 </models>
diff --git a/vlog_to_model.py b/vlog_to_model.py
index 6f8fdd7..862c1e5 100755
--- a/vlog_to_model.py
+++ b/vlog_to_model.py
@@ -64,38 +64,64 @@
     print("ERROR: more than one module in design, cannot detect top level. Manually specify the top level module using --top")
     sys.exit(1)
 
+xi_url = "http://www.w3.org/2001/XInclude"
+ET.register_namespace('xi', xi_url)
+xi_include = "{%s}include" % xi_url
+
+def include_xml(parent, href, xptr):
+    return ET.SubElement(parent, xi_include, {'href': href, 'xpointer': xptr})
+
 tmod = yj.top_module
-models_xml = ET.Element("models")
+models_xml = ET.Element("models", nsmap = {'xi': xi_url})
 
-topname = tmod.attr("MODEL_NAME", top)
+deps_files = set()
+for mod in yj.all_modules():
+    if mod != top:
+        modfile = yj.get_module_file(mod)
+        deps_files.add(modfile)
 
-model_xml = ET.SubElement(models_xml, "model", {'name': topname})
-ports = tmod.ports
+if len(deps_files) > 0:
+    # Has dependencies, not a leaf model
+    for df in sorted(deps_files):
+        module_path = os.path.dirname(df)
+        module_basename = os.path.basename(df)
+        wm = re.match(r"([A-Za-z0-9_]+)\.sim\.v", module_basename)
+        if wm:
+            model_path = "%s/%s.model.xml" % (module_path, wm.group(1).lower())
+        else:
+            assert False
+        include_xml(models_xml, model_path, "xpointer(models/child::node())")
+else:
+    # Is a leaf model
+    topname = tmod.attr("MODEL_NAME", top)
 
-inports_xml = ET.SubElement(model_xml, "input_ports")
-outports_xml = ET.SubElement(model_xml, "output_ports")
+    model_xml = ET.SubElement(models_xml, "model", {'name': topname})
+    ports = tmod.ports
 
-clocks = yosys.run.list_clocks(args.infiles, top)
-clk_sigs = dict()
-for clk in clocks:
-    clk_sigs[clk] = yosys.run.get_clock_assoc_signals(args.infiles, top, clk)
+    inports_xml = ET.SubElement(model_xml, "input_ports")
+    outports_xml = ET.SubElement(model_xml, "output_ports")
 
-for name, width, iodir in ports:
-    attrs = dict(name=name)
-    sinks = yosys.run.get_combinational_sinks(args.infiles, top, name)
-    if len(sinks) > 0 and iodir == "input":
-        attrs["combinational_sink_ports"] = " ".join(sinks)
-    if name in clocks:
-        attrs["is_clock"] = "1"
+    clocks = yosys.run.list_clocks(args.infiles, top)
+    clk_sigs = dict()
     for clk in clocks:
-        if name in clk_sigs[clk]:
-            attrs["clock"] = clk
-    if iodir == "input":
-        ET.SubElement(inports_xml, "port", attrs)
-    elif iodir == "output":
-        ET.SubElement(outports_xml, "port", attrs)
-    else:
-        assert(False) #how does VPR specify inout (only applicable for PACKAGEPIN of an IO primitive)
+        clk_sigs[clk] = yosys.run.get_clock_assoc_signals(args.infiles, top, clk)
+
+    for name, width, iodir in ports:
+        attrs = dict(name=name)
+        sinks = yosys.run.get_combinational_sinks(args.infiles, top, name)
+        if len(sinks) > 0 and iodir == "input":
+            attrs["combinational_sink_ports"] = " ".join(sinks)
+        if name in clocks:
+            attrs["is_clock"] = "1"
+        for clk in clocks:
+            if name in clk_sigs[clk]:
+                attrs["clock"] = clk
+        if iodir == "input":
+            ET.SubElement(inports_xml, "port", attrs)
+        elif iodir == "output":
+            ET.SubElement(outports_xml, "port", attrs)
+        else:
+            assert(False) #how does VPR specify inout (only applicable for PACKAGEPIN of an IO primitive)
 
 outfile = "model.xml"
 if "o" in args and args.o is not None:
diff --git a/yosys/json.py b/yosys/json.py
index 37c2121..1acf097 100755
--- a/yosys/json.py
+++ b/yosys/json.py
@@ -226,6 +226,10 @@
                 mods.append(ymod)
         return mods
 
+    def all_modules(self):
+        """Return a list of the names of all modules in the design, sorted alphabetically"""
+        return sorted(self.data["modules"].keys())
+
     @property
     def top_module(self):
         """Get a given module (by name) as a `YosysModule`"""