hdl.ir: for instance ports, prioritize defs over uses.
authorwhitequark <cz@m-labs.hk>
Tue, 26 Nov 2019 21:17:12 +0000 (21:17 +0000)
committerwhitequark <cz@m-labs.hk>
Tue, 26 Nov 2019 21:19:03 +0000 (21:19 +0000)
Fixes #274.

nmigen/hdl/ir.py
nmigen/test/test_hdl_ir.py

index 1df1b87b9a63c2b33cb53f181560e41b730e6109..a43e222999e138b04a6850891e0532da8ba5dd30 100644 (file)
@@ -433,7 +433,9 @@ class Fragment:
             if isinstance(subfrag, Instance):
                 for port_name, (value, dir) in subfrag.named_ports.items():
                     if dir == "i":
-                        subfrag.add_ports(value._rhs_signals(), dir=dir)
+                        # Prioritize defs over uses.
+                        rhs_without_outputs = value._rhs_signals() - subfrag.iter_ports(dir="o")
+                        subfrag.add_ports(rhs_without_outputs, dir=dir)
                         add_uses(value._rhs_signals())
                     if dir == "o":
                         subfrag.add_ports(value._lhs_signals(), dir=dir)
index 74a909624600da3f69442b7542456de0cec17742..fc4d0fe0eeb3ecb05621c6ed42f934dd93ae419a 100644 (file)
@@ -266,6 +266,27 @@ class FragmentPortsTestCase(FHDLTestCase):
             (s, "io")
         ]))
 
+    def test_in_out_same_signal(self):
+        s = Signal()
+
+        f1 = Instance("foo", i_x=s, o_y=s)
+        f2 = Fragment()
+        f2.add_subfragment(f1)
+
+        f2._propagate_ports(ports=(), all_undef_as_ports=True)
+        self.assertEqual(f1.ports, SignalDict([
+            (s, "o")
+        ]))
+
+        f3 = Instance("foo", o_y=s, i_x=s)
+        f4 = Fragment()
+        f4.add_subfragment(f3)
+
+        f4._propagate_ports(ports=(), all_undef_as_ports=True)
+        self.assertEqual(f3.ports, SignalDict([
+            (s, "o")
+        ]))
+
     def test_clk_rst(self):
         sync = ClockDomain()
         f = Fragment()