From 6f66885c09928906823190d1cc9a593885c8913b Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 14 Jan 2019 16:50:04 +0000 Subject: [PATCH] lib.io: lower to platform-independent tristate buffer. --- examples/tbuf.py | 12 ++++++++++++ nmigen/__init__.py | 1 + nmigen/lib/io.py | 25 ++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 examples/tbuf.py diff --git a/examples/tbuf.py b/examples/tbuf.py new file mode 100644 index 0000000..4e19af0 --- /dev/null +++ b/examples/tbuf.py @@ -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]) diff --git a/nmigen/__init__.py b/nmigen/__init__.py index 613cb50..9c3c777 100644 --- a/nmigen/__init__.py +++ b/nmigen/__init__.py @@ -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 diff --git a/nmigen/lib/io.py b/nmigen/lib/io.py index 684e58f..1801f5f 100644 --- a/nmigen/lib/io.py +++ b/nmigen/lib/io.py @@ -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) -- 2.30.2