lib.io: lower to platform-independent tristate buffer.
authorwhitequark <whitequark@whitequark.org>
Mon, 14 Jan 2019 16:50:04 +0000 (16:50 +0000)
committerwhitequark <whitequark@whitequark.org>
Mon, 14 Jan 2019 16:50:04 +0000 (16:50 +0000)
examples/tbuf.py [new file with mode: 0644]
nmigen/__init__.py
nmigen/lib/io.py

diff --git a/examples/tbuf.py b/examples/tbuf.py
new file mode 100644 (file)
index 0000000..4e19af0
--- /dev/null
@@ -0,0 +1,12 @@
+from nmigen import *
+from nmigen.cli import main
+
+
+pin = Signal()
+pin_t = TSTriple()
+
+m = Module()
+m.submodules += pin_t.get_tristate(pin)
+
+if __name__ == "__main__":
+    main(m.lower(platform=None), ports=[pin, pin_t.oe, pin_t.i, pin_t.o])
index 613cb50d53418f248a4ab3d6da87a439f5db0367..9c3c7771749a409ce6ea83dab5be6289e6174570 100644 (file)
@@ -7,3 +7,4 @@ from .hdl.rec import Record
 from .hdl.xfrm import ResetInserter, CEInserter
 
 from .lib.cdc import MultiReg
+from .lib.io import TSTriple
index 684e58f60383bf5add46bab3b261f1dad63d5d27..1801f5f5157df93c8e9ff9ef2f2d45445e4dfd45 100644 (file)
@@ -1,7 +1,7 @@
 from .. import *
 
 
-__all__ = ["TSTriple"]
+__all__ = ["TSTriple", "Tristate"]
 
 
 class TSTriple:
@@ -19,3 +19,26 @@ class TSTriple:
 
     def get_fragment(self, platform):
         return Fragment()
+
+    def get_tristate(self, io):
+        return Tristate(self, io)
+
+
+class Tristate:
+    def __init__(self, triple, io):
+        self.triple = triple
+        self.io     = io
+
+    def get_fragment(self, platform):
+        if hasattr(platform, "get_tristate"):
+            return platform.get_tristate(self.triple)
+
+        m = Module()
+        m.d.comb += self.triple.i.eq(self.io)
+        m.submodules += Instance("$tribuf",
+            p_WIDTH=len(self.io),
+            i_EN=self.triple.oe,
+            i_A=self.triple.o,
+            o_Y=self.io,
+        )
+        return m.lower(platform)