Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
#
# This file is part of LiteX.
#
# Copyright (c) 2021 Florent Kermarrec <florent@enjoy-digital.fr>
# SPDX-License-Identifier: BSD-2-Clause
from migen.fhdl.module import Module
from migen.genlib.resetsync import AsyncResetSynchronizer
from litex.gen import *
from litex.build.io import *
# Gowin AsyncResetSynchronizer ---------------------------------------------------------------------
class GowinAsyncResetSynchronizerImpl(Module):
def __init__(self, cd, async_reset):
rst1 = Signal()
self.specials += [
Instance("DFFP",
i_D = 0,
i_PRESET = async_reset,
i_CLK = cd.clk,
o_Q = rst1
),
Instance("DFFP",
i_D = rst1,
i_PRESET = async_reset,
i_CLK = cd.clk,
o_Q = cd.rst)
]
class GowinAsyncResetSynchronizer:
@staticmethod
def lower(dr):
return GowinAsyncResetSynchronizerImpl(dr.cd, dr.async_reset)
# Gowin DDR Input ----------------------------------------------------------------------------------
class GowinDDRInputImpl(Module):
def __init__(self, i, o1, o2, clk):
for j in range(len(i)):
self.specials += Instance("IDDR",
i_CLK = clk,
i_D = i[j],
o_Q0 = o1[j],
o_Q1 = o2[j],
)
class GowinDDRInput:
@staticmethod
def lower(dr):
return GowinDDRInputImpl(dr.i, dr.o1, dr.o2, dr.clk)
# Gowin DDR Output ---------------------------------------------------------------------------------
class GowinDDROutputImpl(Module):
def __init__(self, i1, i2, o, clk):
for j in range(len(o)):
self.specials += Instance("ODDR",
i_CLK = clk,
i_D0 = i1[j],
i_D1 = i2[j],
i_TX = 0,
o_Q0 = o[j],
o_Q1 = Open(),
)
class GowinDDROutput:
@staticmethod
def lower(dr):
return GowinDDROutputImpl(dr.i1, dr.i2, dr.o, dr.clk)
# Gowin Differential Input -------------------------------------------------------------------------
class GowinDifferentialInputImpl(Module):
def __init__(self, i_p, i_n, o):
self.specials += Instance("TLVDS_IBUF",
i_I = i_p,
i_IB = i_n,
o_O = o,
)
class GowinDifferentialInput:
@staticmethod
def lower(dr):
return GowinDifferentialInputImpl(dr.i_p, dr.i_n, dr.o)
# Gowin Differential Output ------------------------------------------------------------------------
class GowinDifferentialOutputImpl(Module):
def __init__(self, i, o_p, o_n):
self.specials += Instance("TLVDS_OBUF",
i_I = i,
o_O = o_p,
o_OB = o_n,
)
class GowinDifferentialOutput:
@staticmethod
def lower(dr):
return GowinDifferentialOutputImpl(dr.i, dr.o_p, dr.o_n)
# Gowin Tristate -----------------------------------------------------------------------------------
class GowinTristateImpl(Module):
def __init__(self, io, o, oe, i):
nbits, _ = value_bits_sign(io)
if i is None:
i = Signal.like(o)
for bit in range(nbits):
self.specials += Instance("IOBUF",
io_IO = io[bit] if nbits > 1 else io,
o_O = i[bit] if nbits > 1 else i,
i_I = o[bit] if nbits > 1 else o,
i_OEN = ~oe[bit] if len(oe) == nbits > 1 else ~oe,
)
class GowinTristate:
@staticmethod
def lower(dr):
return GowinTristateImpl(dr.target, dr.o, dr.oe, dr.i)
# Gowin Special Overrides --------------------------------------------------------------------------
gowin_special_overrides = {
AsyncResetSynchronizer: GowinAsyncResetSynchronizer,
DDRInput: GowinDDRInput,
DDROutput: GowinDDROutput,
DifferentialInput: GowinDifferentialInput,
DifferentialOutput: GowinDifferentialOutput,
Tristate: GowinTristate,
}
# Gw5A Tristate ------------------------------------------------------------------------------------
class Gw5ATristateImpl(Module):
def __init__(self, io, o, oe, i):
nbits, _ = value_bits_sign(io)
if i is None:
i = Signal.like(o)
for bit in range(nbits):
self.specials += Instance("IOBUF",
io_IO = io[bit] if nbits > 1 else io,
o_O = i[bit] if nbits > 1 else i,
i_I = o[bit] if nbits > 1 else o,
i_OEN = ~oe[bit] if len(oe) == nbits > 1 else ~oe,
)
class Gw5ATristate:
@staticmethod
def lower(dr):
return Gw5ATristateImpl(dr.target, dr.o, dr.oe, dr.i)
# Gw5A SDROutput -----------------------------------------------------------------------------------
class Gw5ASDROutputImpl(Module):
def __init__(self, i, o, clk):
for j in range(len(o)):
self.specials += Instance("DFFSE",
i_D = i[j],
o_Q = o[j],
i_CLK = clk,
i_SET = Constant(0,1),
i_CE = Constant(1,1),
)
class Gw5ASDROutput:
@staticmethod
def lower(dr):
return Gw5ASDROutputImpl(dr.i, dr.o, dr.clk)
# Gw5A SDRInput ------------------------------------------------------------------------------------
class Gw5ASDRInputImpl(Module):
def __init__(self, i, o, clk):
for j in range(len(i)):
self.specials += Instance("DFFSE",
i_D = i[j],
o_Q = o[j],
i_CLK = clk,
i_SET = Constant(0,1),
i_CE = Constant(1,1),
)
class Gw5ASDRInput:
@staticmethod
def lower(dr):
return Gw5ASDRInputImpl(dr.i, dr.o, dr.clk)
# Gw5A SDRTristate ---------------------------------------------------------------------------------
class Gw5ASDRTristateImpl(Module):
def __init__(self, io, o, oe, i, clk):
_o = Signal.like(o)
_oe_n = Signal.like(oe)
_i = Signal.like(i if i is not None else o)
self.specials += [
SDROutput(o, _o, clk),
SDROutput(~oe, _oe_n, clk),
]
if i is not None:
self.specials += SDRInput(_i, i, clk)
for j in range(len(io)):
self.specials += Instance("IOBUF",
io_IO = io[j],
o_O = _i[j],
i_I = _o[j],
i_OEN = _oe_n[j],
)
class Gw5ASDRTristate:
@staticmethod
def lower(dr):
return Gw5ASDRTristateImpl(dr.io, dr.o, dr.oe, dr.i, dr.clk)
# Gw5A SerDes --------------------------------------------------------------------------------------
# Note: Instances are named explicitly: GowinSynthesis can silently mis-synthesize designs with an
# instance named as its primitive (ex: "OSER4 OSER4(...)").
class Gw5AOSER(LiteXModule):
"""GW5A output serializer (OSER4/OSER8/OSER10/OSER16).
Serializes ``d`` (``len(d)`` in 4/8/10/16, ``d[0]`` sent first) on ``q``: ``pclk`` is the
parallel clock, ``fclk`` the fast clock (``len(d)//2`` x ``pclk``, DDR). OSER4/OSER8 also have an
optional tristate path: ``t`` (1: high-impedance, sampled on ``pclk``) is serialized to ``q_t``
(to drive the ``OEN`` of an ``IOBUF``). OSER10/OSER16 use both IOs of the pad pair (the other IO
of the pair can't be used).
"""
def __init__(self, d, q, pclk, fclk, reset=0, t=None, q_t=None):
ratio = len(d)
if ratio not in [4, 8, 10, 16]:
raise ValueError(f"Unsupported OSER ratio {ratio} (4, 8, 10, 16).")
if (t is not None or q_t is not None) and ratio not in [4, 8]:
raise ValueError(f"OSER{ratio} has no tristate path (OSER4/OSER8 only).")
# # #
params = dict(
i_PCLK = pclk,
i_FCLK = fclk,
i_RESET = reset,
**{f"i_D{n}" : d[n] for n in range(ratio)},
)
if ratio in [4, 8]:
t = 0 if t is None else t
params.update(
p_HWL = "false",
p_TXCLK_POL = 0,
o_Q0 = q,
o_Q1 = Signal() if q_t is None else q_t,
**{f"i_TX{n}" : t for n in range(ratio//2)},
)
else:
params.update(o_Q=q)
self.specials += Instance(f"OSER{ratio}", name=f"gw5a_oser{ratio}", **params)
class Gw5AIDES(LiteXModule):
"""GW5A input deserializer (IDES4/IDES8/IDES10/IDES16).
Deserializes ``d`` on ``q`` (``len(q)`` in 4/8/10/16, ``q[0]`` received first): ``pclk`` is the
parallel clock, ``fclk`` the fast clock (``len(q)//2`` x ``pclk``, DDR). A ``calib`` pulse shifts
the word alignment by one bit.
"""
def __init__(self, d, q, pclk, fclk, reset=0, calib=0):
ratio = len(q)
if ratio not in [4, 8, 10, 16]:
raise ValueError(f"Unsupported IDES ratio {ratio} (4, 8, 10, 16).")
# # #
self.specials += Instance(f"IDES{ratio}", name=f"gw5a_ides{ratio}",
i_D = d,
i_PCLK = pclk,
i_FCLK = fclk,
i_CALIB = calib,
i_RESET = reset,
**{f"o_Q{n}" : q[n] for n in range(ratio)},
)
# Gw5A IODelay -------------------------------------------------------------------------------------
class Gw5AIODELAY(LiteXModule):
"""GW5A input/output delay (IODELAY), static ``delay`` taps (0-255)."""
def __init__(self, i, o, delay=0):
if not (0 <= delay <= 255):
raise ValueError(f"Unsupported IODELAY delay {delay} (0-255 taps).")
# # #
self.specials += Instance("IODELAY", name="gw5a_iodelay",
p_C_STATIC_DLY = delay,
i_DI = i,
i_SDTAP = 0,
i_DLYSTEP = Constant(0, 8),
i_VALUE = 0,
o_DO = o,
o_DF = Signal(),
)
# Gw5A SerDes Input/Output (Special Overrides) -----------------------------------------------------
class Gw5ASerDesInput:
@staticmethod
def lower(dr):
return Gw5AIDES(dr.i, dr.o, pclk=dr.clk, fclk=dr.clk_fast, reset=dr.rst)
class Gw5ASerDesOutput:
@staticmethod
def lower(dr):
return Gw5AOSER(dr.i, dr.o, pclk=dr.clk, fclk=dr.clk_fast, reset=dr.rst)
class Gw5ASerDesTristateImpl(LiteXModule):
def __init__(self, io, o, oe, i, clk, clk_fast, rst):
_o = Signal()
_oe_n = Signal()
_i = Signal()
self.oser = Gw5AOSER(o, _o, pclk=clk, fclk=clk_fast, reset=rst, t=~oe, q_t=_oe_n)
self.specials += Instance("IOBUF", name="gw5a_serdes_iobuf",
io_IO = io,
o_O = _i,
i_I = _o,
i_OEN = _oe_n,
)
if i is not None:
self.ides = Gw5AIDES(_i, i, pclk=clk, fclk=clk_fast, reset=rst)
class Gw5ASerDesTristate:
"""Output serializer (with its tristate path) and input deserializer on an IOBUF (OSER4/OSER8,
IDES4/IDES8)."""
@staticmethod
def lower(dr):
return Gw5ASerDesTristateImpl(dr.io, dr.o, dr.oe, dr.i, dr.clk, dr.clk_fast, dr.rst)
# Gw5A Differential Tristate -----------------------------------------------------------------------
class Gw5ADifferentialTristateImpl(Module):
def __init__(self, io_p, io_n, o, oe, i):
self.specials += Instance("ELVDS_IOBUF", name="gw5a_elvds_iobuf",
i_I = o,
i_OEN = ~oe,
o_O = Signal() if i is None else i,
io_IO = io_p,
io_IOB = io_n,
)
class Gw5ADifferentialTristate:
"""Emulated LVDS bidirectional buffer (ELVDS_IOBUF)."""
@staticmethod
def lower(dr):
return Gw5ADifferentialTristateImpl(dr.io_p, dr.io_n, dr.o, dr.oe, dr.i)
# Gw5A Special Overrides ---------------------------------------------------------------------------
gw5a_special_overrides = {
DifferentialTristate: Gw5ADifferentialTristate,
SerDesInput: Gw5ASerDesInput,
SerDesOutput: Gw5ASerDesOutput,
SerDesTristate: Gw5ASerDesTristate,
SDRTristate: Gw5ASDRTristate,
SDROutput: Gw5ASDROutput,
SDRInput: Gw5ASDRInput,
Tristate: Gw5ATristate,
}
6ccd314b2This file is pinned to the selected release. Download the original to open it in your design tool.