Skip to content

Commit

Permalink
lib.io: lower to platform-independent tristate buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Jan 14, 2019
1 parent 011bf22 commit 6f66885
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
12 changes: 12 additions & 0 deletions 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])
1 change: 1 addition & 0 deletions nmigen/__init__.py
Expand Up @@ -7,3 +7,4 @@
from .hdl.xfrm import ResetInserter, CEInserter

from .lib.cdc import MultiReg
from .lib.io import TSTriple
25 changes: 24 additions & 1 deletion nmigen/lib/io.py
@@ -1,7 +1,7 @@
from .. import *


__all__ = ["TSTriple"]
__all__ = ["TSTriple", "Tristate"]


class TSTriple:
Expand All @@ -19,3 +19,26 @@ def __len__(self):

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)

0 comments on commit 6f66885

Please sign in to comment.