Rename nMigen to Amaranth HDL.

This commit is contained in:
whitequark 2021-12-10 05:39:50 +00:00
parent 0b28a97ca0
commit 909a3b8be7
200 changed files with 14493 additions and 14451 deletions

View file

@ -0,0 +1,6 @@
from amaranth.lib import *
import warnings
warnings.warn("instead of nmigen.lib, use amaranth.lib",
DeprecationWarning, stacklevel=2)

View file

@ -1,267 +1,7 @@
from .. import *
from amaranth.lib.cdc import *
from amaranth.lib.cdc import __all__
__all__ = ["FFSynchronizer", "AsyncFFSynchronizer", "ResetSynchronizer", "PulseSynchronizer"]
def _check_stages(stages):
if not isinstance(stages, int) or stages < 1:
raise TypeError("Synchronization stage count must be a positive integer, not {!r}"
.format(stages))
if stages < 2:
raise ValueError("Synchronization stage count may not safely be less than 2")
class FFSynchronizer(Elaboratable):
"""Resynchronise a signal to a different clock domain.
Consists of a chain of flip-flops. Eliminates metastabilities at the output, but provides
no other guarantee as to the safe domain-crossing of a signal.
Parameters
----------
i : Signal(n), in
Signal to be resynchronised.
o : Signal(n), out
Signal connected to synchroniser output.
o_domain : str
Name of output clock domain.
reset : int
Reset value of the flip-flops. On FPGAs, even if ``reset_less`` is True,
the :class:`FFSynchronizer` is still set to this value during initialization.
reset_less : bool
If ``True`` (the default), this :class:`FFSynchronizer` is unaffected by ``o_domain``
reset. See "Note on Reset" below.
stages : int
Number of synchronization stages between input and output. The lowest safe number is 2,
with higher numbers reducing MTBF further, at the cost of increased latency.
max_input_delay : None or float
Maximum delay from the input signal's clock to the first synchronization stage, in seconds.
If specified and the platform does not support it, elaboration will fail.
Platform override
-----------------
Define the ``get_ff_sync`` platform method to override the implementation of
:class:`FFSynchronizer`, e.g. to instantiate library cells directly.
Note on Reset
-------------
:class:`FFSynchronizer` is non-resettable by default. Usually this is the safest option;
on FPGAs the :class:`FFSynchronizer` will still be initialized to its ``reset`` value when
the FPGA loads its configuration.
However, in designs where the value of the :class:`FFSynchronizer` must be valid immediately
after reset, consider setting ``reset_less`` to False if any of the following is true:
- You are targeting an ASIC, or an FPGA that does not allow arbitrary initial flip-flop states;
- Your design features warm (non-power-on) resets of ``o_domain``, so the one-time
initialization at power on is insufficient;
- Your design features a sequenced reset, and the :class:`FFSynchronizer` must maintain
its reset value until ``o_domain`` reset specifically is deasserted.
:class:`FFSynchronizer` is reset by the ``o_domain`` reset only.
"""
def __init__(self, i, o, *, o_domain="sync", reset=0, reset_less=True, stages=2,
max_input_delay=None):
_check_stages(stages)
self.i = i
self.o = o
self._reset = reset
self._reset_less = reset_less
self._o_domain = o_domain
self._stages = stages
self._max_input_delay = max_input_delay
def elaborate(self, platform):
if hasattr(platform, "get_ff_sync"):
return platform.get_ff_sync(self)
if self._max_input_delay is not None:
raise NotImplementedError("Platform '{}' does not support constraining input delay "
"for FFSynchronizer"
.format(type(platform).__name__))
m = Module()
flops = [Signal(self.i.shape(), name="stage{}".format(index),
reset=self._reset, reset_less=self._reset_less)
for index in range(self._stages)]
for i, o in zip((self.i, *flops), flops):
m.d[self._o_domain] += o.eq(i)
m.d.comb += self.o.eq(flops[-1])
return m
class AsyncFFSynchronizer(Elaboratable):
"""Synchronize deassertion of an asynchronous signal.
The signal driven by the :class:`AsyncFFSynchronizer` is asserted asynchronously and deasserted
synchronously, eliminating metastability during deassertion.
This synchronizer is primarily useful for resets and reset-like signals.
Parameters
----------
i : Signal(1), in
Asynchronous input signal, to be synchronized.
o : Signal(1), out
Synchronously released output signal.
o_domain : str
Name of clock domain to synchronize to.
stages : int, >=2
Number of synchronization stages between input and output. The lowest safe number is 2,
with higher numbers reducing MTBF further, at the cost of increased deassertion latency.
async_edge : str
The edge of the input signal which causes the output to be set. Must be one of "pos" or "neg".
max_input_delay : None or float
Maximum delay from the input signal's clock to the first synchronization stage, in seconds.
If specified and the platform does not support it, elaboration will fail.
Platform override
-----------------
Define the ``get_async_ff_sync`` platform method to override the implementation of
:class:`AsyncFFSynchronizer`, e.g. to instantiate library cells directly.
"""
def __init__(self, i, o, *, o_domain="sync", stages=2, async_edge="pos", max_input_delay=None):
_check_stages(stages)
if len(i) != 1:
raise ValueError("AsyncFFSynchronizer input width must be 1, not {}"
.format(len(i)))
if len(o) != 1:
raise ValueError("AsyncFFSynchronizer output width must be 1, not {}"
.format(len(o)))
if async_edge not in ("pos", "neg"):
raise ValueError("AsyncFFSynchronizer async edge must be one of 'pos' or 'neg', "
"not {!r}"
.format(async_edge))
self.i = i
self.o = o
self._o_domain = o_domain
self._stages = stages
self._edge = async_edge
self._max_input_delay = max_input_delay
def elaborate(self, platform):
if hasattr(platform, "get_async_ff_sync"):
return platform.get_async_ff_sync(self)
if self._max_input_delay is not None:
raise NotImplementedError("Platform '{}' does not support constraining input delay "
"for AsyncFFSynchronizer"
.format(type(platform).__name__))
m = Module()
m.domains += ClockDomain("async_ff", async_reset=True, local=True)
flops = [Signal(1, name="stage{}".format(index), reset=1)
for index in range(self._stages)]
for i, o in zip((0, *flops), flops):
m.d.async_ff += o.eq(i)
if self._edge == "pos":
m.d.comb += ResetSignal("async_ff").eq(self.i)
else:
m.d.comb += ResetSignal("async_ff").eq(~self.i)
m.d.comb += [
ClockSignal("async_ff").eq(ClockSignal(self._o_domain)),
self.o.eq(flops[-1])
]
return m
class ResetSynchronizer(Elaboratable):
"""Synchronize deassertion of a clock domain reset.
The reset of the clock domain driven by the :class:`ResetSynchronizer` is asserted
asynchronously and deasserted synchronously, eliminating metastability during deassertion.
The driven clock domain could use a reset that is asserted either synchronously or
asynchronously; a reset is always deasserted synchronously. A domain with an asynchronously
asserted reset is useful if the clock of the domain may be gated, yet the domain still
needs to be reset promptly; otherwise, synchronously asserted reset (the default) should
be used.
Parameters
----------
arst : Signal(1), in
Asynchronous reset signal, to be synchronized.
domain : str
Name of clock domain to reset.
stages : int, >=2
Number of synchronization stages between input and output. The lowest safe number is 2,
with higher numbers reducing MTBF further, at the cost of increased deassertion latency.
max_input_delay : None or float
Maximum delay from the input signal's clock to the first synchronization stage, in seconds.
If specified and the platform does not support it, elaboration will fail.
Platform override
-----------------
Define the ``get_reset_sync`` platform method to override the implementation of
:class:`ResetSynchronizer`, e.g. to instantiate library cells directly.
"""
def __init__(self, arst, *, domain="sync", stages=2, max_input_delay=None):
_check_stages(stages)
self.arst = arst
self._domain = domain
self._stages = stages
self._max_input_delay = max_input_delay
def elaborate(self, platform):
return AsyncFFSynchronizer(self.arst, ResetSignal(self._domain), o_domain=self._domain,
stages=self._stages, max_input_delay=self._max_input_delay)
class PulseSynchronizer(Elaboratable):
"""A one-clock pulse on the input produces a one-clock pulse on the output.
If the output clock is faster than the input clock, then the input may be safely asserted at
100% duty cycle. Otherwise, if the clock ratio is `n`:1, the input may be asserted at most once
in every `n` input clocks, else pulses may be dropped. Other than this there is no constraint
on the ratio of input and output clock frequency.
Parameters
----------
i_domain : str
Name of input clock domain.
o_domain : str
Name of output clock domain.
stages : int, >=2
Number of synchronization stages between input and output. The lowest safe number is 2,
with higher numbers reducing MTBF further, at the cost of increased deassertion latency.
"""
def __init__(self, i_domain, o_domain, *, stages=2):
_check_stages(stages)
self.i = Signal()
self.o = Signal()
self._i_domain = i_domain
self._o_domain = o_domain
self._stages = stages
def elaborate(self, platform):
m = Module()
i_toggle = Signal()
o_toggle = Signal()
r_toggle = Signal()
ff_sync = m.submodules.ff_sync = \
FFSynchronizer(i_toggle, o_toggle, o_domain=self._o_domain, stages=self._stages)
m.d[self._i_domain] += i_toggle.eq(i_toggle ^ self.i)
m.d[self._o_domain] += r_toggle.eq(o_toggle)
m.d.comb += self.o.eq(o_toggle ^ r_toggle)
return m
import warnings
warnings.warn("instead of nmigen.lib.cdc, use amaranth.lib.cdc",
DeprecationWarning, stacklevel=2)

View file

@ -1,186 +1,7 @@
"""Encoders and decoders between binary and one-hot representation."""
from .. import *
from amaranth.lib.coding import *
from amaranth.lib.coding import __all__
__all__ = [
"Encoder", "Decoder",
"PriorityEncoder", "PriorityDecoder",
"GrayEncoder", "GrayDecoder",
]
class Encoder(Elaboratable):
"""Encode one-hot to binary.
If one bit in ``i`` is asserted, ``n`` is low and ``o`` indicates the asserted bit.
Otherwise, ``n`` is high and ``o`` is ``0``.
Parameters
----------
width : int
Bit width of the input
Attributes
----------
i : Signal(width), in
One-hot input.
o : Signal(range(width)), out
Encoded binary.
n : Signal, out
Invalid: either none or multiple input bits are asserted.
"""
def __init__(self, width):
self.width = width
self.i = Signal(width)
self.o = Signal(range(width))
self.n = Signal()
def elaborate(self, platform):
m = Module()
with m.Switch(self.i):
for j in range(self.width):
with m.Case(1 << j):
m.d.comb += self.o.eq(j)
with m.Case():
m.d.comb += self.n.eq(1)
return m
class PriorityEncoder(Elaboratable):
"""Priority encode requests to binary.
If any bit in ``i`` is asserted, ``n`` is low and ``o`` indicates the least significant
asserted bit.
Otherwise, ``n`` is high and ``o`` is ``0``.
Parameters
----------
width : int
Bit width of the input.
Attributes
----------
i : Signal(width), in
Input requests.
o : Signal(range(width)), out
Encoded binary.
n : Signal, out
Invalid: no input bits are asserted.
"""
def __init__(self, width):
self.width = width
self.i = Signal(width)
self.o = Signal(range(width))
self.n = Signal()
def elaborate(self, platform):
m = Module()
for j in reversed(range(self.width)):
with m.If(self.i[j]):
m.d.comb += self.o.eq(j)
m.d.comb += self.n.eq(self.i == 0)
return m
class Decoder(Elaboratable):
"""Decode binary to one-hot.
If ``n`` is low, only the ``i``th bit in ``o`` is asserted.
If ``n`` is high, ``o`` is ``0``.
Parameters
----------
width : int
Bit width of the output.
Attributes
----------
i : Signal(range(width)), in
Input binary.
o : Signal(width), out
Decoded one-hot.
n : Signal, in
Invalid, no output bits are to be asserted.
"""
def __init__(self, width):
self.width = width
self.i = Signal(range(width))
self.n = Signal()
self.o = Signal(width)
def elaborate(self, platform):
m = Module()
with m.Switch(self.i):
for j in range(len(self.o)):
with m.Case(j):
m.d.comb += self.o.eq(1 << j)
with m.If(self.n):
m.d.comb += self.o.eq(0)
return m
class PriorityDecoder(Decoder):
"""Decode binary to priority request.
Identical to :class:`Decoder`.
"""
class GrayEncoder(Elaboratable):
"""Encode binary to Gray code.
Parameters
----------
width : int
Bit width.
Attributes
----------
i : Signal(width), in
Input natural binary.
o : Signal(width), out
Encoded Gray code.
"""
def __init__(self, width):
self.width = width
self.i = Signal(width)
self.o = Signal(width)
def elaborate(self, platform):
m = Module()
m.d.comb += self.o.eq(self.i ^ self.i[1:])
return m
class GrayDecoder(Elaboratable):
"""Decode Gray code to binary.
Parameters
----------
width : int
Bit width.
Attributes
----------
i : Signal(width), in
Input Gray code.
o : Signal(width), out
Decoded natural binary.
"""
def __init__(self, width):
self.width = width
self.i = Signal(width)
self.o = Signal(width)
def elaborate(self, platform):
m = Module()
m.d.comb += self.o[-1].eq(self.i[-1])
for i in reversed(range(self.width - 1)):
m.d.comb += self.o[i].eq(self.o[i + 1] ^ self.i[i])
return m
import warnings
warnings.warn("instead of nmigen.lib.coding, use amaranth.lib.coding",
DeprecationWarning, stacklevel=2)

View file

@ -1,529 +1,7 @@
"""First-in first-out queues."""
from amaranth.lib.fifo import *
from amaranth.lib.fifo import __all__
from .. import *
from ..asserts import *
from .._utils import log2_int
from .coding import GrayEncoder, GrayDecoder
from .cdc import FFSynchronizer, AsyncFFSynchronizer
__all__ = ["FIFOInterface", "SyncFIFO", "SyncFIFOBuffered", "AsyncFIFO", "AsyncFIFOBuffered"]
class FIFOInterface:
_doc_template = """
{description}
Parameters
----------
width : int
Bit width of data entries.
depth : int
Depth of the queue. If zero, the FIFO cannot be read from or written to.
{parameters}
Attributes
----------
{attributes}
w_data : in, width
Input data.
w_rdy : out
Asserted if there is space in the queue, i.e. ``w_en`` can be asserted to write
a new entry.
w_en : in
Write strobe. Latches ``w_data`` into the queue. Does nothing if ``w_rdy`` is not asserted.
w_level : out
Number of unread entries.
{w_attributes}
r_data : out, width
Output data. {r_data_valid}
r_rdy : out
Asserted if there is an entry in the queue, i.e. ``r_en`` can be asserted to read
an existing entry.
r_en : in
Read strobe. Makes the next entry (if any) available on ``r_data`` at the next cycle.
Does nothing if ``r_rdy`` is not asserted.
r_level : out
Number of unread entries.
{r_attributes}
"""
__doc__ = _doc_template.format(description="""
Data written to the input interface (``w_data``, ``w_rdy``, ``w_en``) is buffered and can be
read at the output interface (``r_data``, ``r_rdy``, ``r_en`). The data entry written first
to the input also appears first on the output.
""",
parameters="",
r_data_valid="The conditions in which ``r_data`` is valid depends on the type of the queue.",
attributes="""
fwft : bool
First-word fallthrough. If set, when ``r_rdy`` rises, the first entry is already
available, i.e. ``r_data`` is valid. Otherwise, after ``r_rdy`` rises, it is necessary
to strobe ``r_en`` for ``r_data`` to become valid.
""".strip(),
w_attributes="",
r_attributes="")
def __init__(self, *, width, depth, fwft):
if not isinstance(width, int) or width < 0:
raise TypeError("FIFO width must be a non-negative integer, not {!r}"
.format(width))
if not isinstance(depth, int) or depth < 0:
raise TypeError("FIFO depth must be a non-negative integer, not {!r}"
.format(depth))
self.width = width
self.depth = depth
self.fwft = fwft
self.w_data = Signal(width, reset_less=True)
self.w_rdy = Signal() # writable; not full
self.w_en = Signal()
self.w_level = Signal(range(depth + 1))
self.r_data = Signal(width, reset_less=True)
self.r_rdy = Signal() # readable; not empty
self.r_en = Signal()
self.r_level = Signal(range(depth + 1))
def _incr(signal, modulo):
if modulo == 2 ** len(signal):
return signal + 1
else:
return Mux(signal == modulo - 1, 0, signal + 1)
class SyncFIFO(Elaboratable, FIFOInterface):
__doc__ = FIFOInterface._doc_template.format(
description="""
Synchronous first in, first out queue.
Read and write interfaces are accessed from the same clock domain. If different clock domains
are needed, use :class:`AsyncFIFO`.
""".strip(),
parameters="""
fwft : bool
First-word fallthrough. If set, when the queue is empty and an entry is written into it,
that entry becomes available on the output on the same clock cycle. Otherwise, it is
necessary to assert ``r_en`` for ``r_data`` to become valid.
""".strip(),
r_data_valid="""
For FWFT queues, valid if ``r_rdy`` is asserted. For non-FWFT queues, valid on the next
cycle after ``r_rdy`` and ``r_en`` have been asserted.
""".strip(),
attributes="",
r_attributes="",
w_attributes="")
def __init__(self, *, width, depth, fwft=True):
super().__init__(width=width, depth=depth, fwft=fwft)
self.level = Signal(range(depth + 1))
def elaborate(self, platform):
m = Module()
if self.depth == 0:
m.d.comb += [
self.w_rdy.eq(0),
self.r_rdy.eq(0),
]
return m
m.d.comb += [
self.w_rdy.eq(self.level != self.depth),
self.r_rdy.eq(self.level != 0),
self.w_level.eq(self.level),
self.r_level.eq(self.level),
]
do_read = self.r_rdy & self.r_en
do_write = self.w_rdy & self.w_en
storage = Memory(width=self.width, depth=self.depth)
w_port = m.submodules.w_port = storage.write_port()
r_port = m.submodules.r_port = storage.read_port(
domain="comb" if self.fwft else "sync", transparent=self.fwft)
produce = Signal(range(self.depth))
consume = Signal(range(self.depth))
m.d.comb += [
w_port.addr.eq(produce),
w_port.data.eq(self.w_data),
w_port.en.eq(self.w_en & self.w_rdy),
]
with m.If(do_write):
m.d.sync += produce.eq(_incr(produce, self.depth))
m.d.comb += [
r_port.addr.eq(consume),
self.r_data.eq(r_port.data),
]
if not self.fwft:
m.d.comb += r_port.en.eq(self.r_en)
with m.If(do_read):
m.d.sync += consume.eq(_incr(consume, self.depth))
with m.If(do_write & ~do_read):
m.d.sync += self.level.eq(self.level + 1)
with m.If(do_read & ~do_write):
m.d.sync += self.level.eq(self.level - 1)
if platform == "formal":
# TODO: move this logic to SymbiYosys
with m.If(Initial()):
m.d.comb += [
Assume(produce < self.depth),
Assume(consume < self.depth),
]
with m.If(produce == consume):
m.d.comb += Assume((self.level == 0) | (self.level == self.depth))
with m.If(produce > consume):
m.d.comb += Assume(self.level == (produce - consume))
with m.If(produce < consume):
m.d.comb += Assume(self.level == (self.depth + produce - consume))
with m.Else():
m.d.comb += [
Assert(produce < self.depth),
Assert(consume < self.depth),
]
with m.If(produce == consume):
m.d.comb += Assert((self.level == 0) | (self.level == self.depth))
with m.If(produce > consume):
m.d.comb += Assert(self.level == (produce - consume))
with m.If(produce < consume):
m.d.comb += Assert(self.level == (self.depth + produce - consume))
return m
class SyncFIFOBuffered(Elaboratable, FIFOInterface):
__doc__ = FIFOInterface._doc_template.format(
description="""
Buffered synchronous first in, first out queue.
This queue's interface is identical to :class:`SyncFIFO` configured as ``fwft=True``, but it
does not use asynchronous memory reads, which are incompatible with FPGA block RAMs.
In exchange, the latency between an entry being written to an empty queue and that entry
becoming available on the output is increased by one cycle compared to :class:`SyncFIFO`.
""".strip(),
parameters="""
fwft : bool
Always set.
""".strip(),
attributes="",
r_data_valid="Valid if ``r_rdy`` is asserted.",
r_attributes="""
level : out
Number of unread entries.
""".strip(),
w_attributes="")
def __init__(self, *, width, depth):
super().__init__(width=width, depth=depth, fwft=True)
self.level = Signal(range(depth + 1))
def elaborate(self, platform):
m = Module()
if self.depth == 0:
m.d.comb += [
self.w_rdy.eq(0),
self.r_rdy.eq(0),
]
return m
# Effectively, this queue treats the output register of the non-FWFT inner queue as
# an additional storage element.
m.submodules.unbuffered = fifo = SyncFIFO(width=self.width, depth=self.depth - 1,
fwft=False)
m.d.comb += [
fifo.w_data.eq(self.w_data),
fifo.w_en.eq(self.w_en),
self.w_rdy.eq(fifo.w_rdy),
]
m.d.comb += [
self.r_data.eq(fifo.r_data),
fifo.r_en.eq(fifo.r_rdy & (~self.r_rdy | self.r_en)),
]
with m.If(fifo.r_en):
m.d.sync += self.r_rdy.eq(1)
with m.Elif(self.r_en):
m.d.sync += self.r_rdy.eq(0)
m.d.comb += [
self.level.eq(fifo.level + self.r_rdy),
self.w_level.eq(self.level),
self.r_level.eq(self.level),
]
return m
class AsyncFIFO(Elaboratable, FIFOInterface):
__doc__ = FIFOInterface._doc_template.format(
description="""
Asynchronous first in, first out queue.
Read and write interfaces are accessed from different clock domains, which can be set when
constructing the FIFO.
:class:`AsyncFIFO` can be reset from the write clock domain. When the write domain reset is
asserted, the FIFO becomes empty. When the read domain is reset, data remains in the FIFO - the
read domain logic should correctly handle this case.
:class:`AsyncFIFO` only supports power of 2 depths. Unless ``exact_depth`` is specified,
the ``depth`` parameter is rounded up to the next power of 2.
""".strip(),
parameters="""
r_domain : str
Read clock domain.
w_domain : str
Write clock domain.
""".strip(),
attributes="""
fwft : bool
Always set.
""".strip(),
r_data_valid="Valid if ``r_rdy`` is asserted.",
r_attributes="""
r_rst : Signal, out
Asserted while the FIFO is being reset by the write-domain reset (for at least one
read-domain clock cycle).
""".strip(),
w_attributes="")
def __init__(self, *, width, depth, r_domain="read", w_domain="write", exact_depth=False):
if depth != 0:
try:
depth_bits = log2_int(depth, need_pow2=exact_depth)
depth = 1 << depth_bits
except ValueError:
raise ValueError("AsyncFIFO only supports depths that are powers of 2; requested "
"exact depth {} is not"
.format(depth)) from None
else:
depth_bits = 0
super().__init__(width=width, depth=depth, fwft=True)
self.r_rst = Signal()
self._r_domain = r_domain
self._w_domain = w_domain
self._ctr_bits = depth_bits + 1
def elaborate(self, platform):
m = Module()
if self.depth == 0:
m.d.comb += [
self.w_rdy.eq(0),
self.r_rdy.eq(0),
]
return m
# The design of this queue is the "style #2" from Clifford E. Cummings' paper "Simulation
# and Synthesis Techniques for Asynchronous FIFO Design":
# http://www.sunburst-design.com/papers/CummingsSNUG2002SJ_FIFO1.pdf
do_write = self.w_rdy & self.w_en
do_read = self.r_rdy & self.r_en
# TODO: extract this pattern into lib.cdc.GrayCounter
produce_w_bin = Signal(self._ctr_bits)
produce_w_nxt = Signal(self._ctr_bits)
m.d.comb += produce_w_nxt.eq(produce_w_bin + do_write)
m.d[self._w_domain] += produce_w_bin.eq(produce_w_nxt)
# Note: Both read-domain counters must be reset_less (see comments below)
consume_r_bin = Signal(self._ctr_bits, reset_less=True)
consume_r_nxt = Signal(self._ctr_bits)
m.d.comb += consume_r_nxt.eq(consume_r_bin + do_read)
m.d[self._r_domain] += consume_r_bin.eq(consume_r_nxt)
produce_w_gry = Signal(self._ctr_bits)
produce_r_gry = Signal(self._ctr_bits)
produce_enc = m.submodules.produce_enc = \
GrayEncoder(self._ctr_bits)
produce_cdc = m.submodules.produce_cdc = \
FFSynchronizer(produce_w_gry, produce_r_gry, o_domain=self._r_domain)
m.d.comb += produce_enc.i.eq(produce_w_nxt),
m.d[self._w_domain] += produce_w_gry.eq(produce_enc.o)
consume_r_gry = Signal(self._ctr_bits, reset_less=True)
consume_w_gry = Signal(self._ctr_bits)
consume_enc = m.submodules.consume_enc = \
GrayEncoder(self._ctr_bits)
consume_cdc = m.submodules.consume_cdc = \
FFSynchronizer(consume_r_gry, consume_w_gry, o_domain=self._w_domain)
m.d.comb += consume_enc.i.eq(consume_r_nxt)
m.d[self._r_domain] += consume_r_gry.eq(consume_enc.o)
consume_w_bin = Signal(self._ctr_bits)
consume_dec = m.submodules.consume_dec = \
GrayDecoder(self._ctr_bits)
m.d.comb += consume_dec.i.eq(consume_w_gry),
m.d[self._w_domain] += consume_w_bin.eq(consume_dec.o)
produce_r_bin = Signal(self._ctr_bits)
produce_dec = m.submodules.produce_dec = \
GrayDecoder(self._ctr_bits)
m.d.comb += produce_dec.i.eq(produce_r_gry),
m.d.comb += produce_r_bin.eq(produce_dec.o)
w_full = Signal()
r_empty = Signal()
m.d.comb += [
w_full.eq((produce_w_gry[-1] != consume_w_gry[-1]) &
(produce_w_gry[-2] != consume_w_gry[-2]) &
(produce_w_gry[:-2] == consume_w_gry[:-2])),
r_empty.eq(consume_r_gry == produce_r_gry),
]
m.d[self._w_domain] += self.w_level.eq((produce_w_bin - consume_w_bin))
m.d.comb += self.r_level.eq((produce_r_bin - consume_r_bin))
storage = Memory(width=self.width, depth=self.depth)
w_port = m.submodules.w_port = storage.write_port(domain=self._w_domain)
r_port = m.submodules.r_port = storage.read_port (domain=self._r_domain,
transparent=False)
m.d.comb += [
w_port.addr.eq(produce_w_bin[:-1]),
w_port.data.eq(self.w_data),
w_port.en.eq(do_write),
self.w_rdy.eq(~w_full),
]
m.d.comb += [
r_port.addr.eq(consume_r_nxt[:-1]),
self.r_data.eq(r_port.data),
r_port.en.eq(1),
self.r_rdy.eq(~r_empty),
]
# Reset handling to maintain FIFO and CDC invariants in the presence of a write-domain
# reset.
# There is a CDC hazard associated with resetting an async FIFO - Gray code counters which
# are reset to 0 violate their Gray code invariant. One way to handle this is to ensure
# that both sides of the FIFO are asynchronously reset by the same signal. We adopt a
# slight variation on this approach - reset control rests entirely with the write domain.
# The write domain's reset signal is used to asynchronously reset the read domain's
# counters and force the FIFO to be empty when the write domain's reset is asserted.
# This requires the two read domain counters to be marked as "reset_less", as they are
# reset through another mechanism. See https://github.com/nmigen/nmigen/issues/181 for the
# full discussion.
w_rst = ResetSignal(domain=self._w_domain, allow_reset_less=True)
r_rst = Signal()
# Async-set-sync-release synchronizer avoids CDC hazards
rst_cdc = m.submodules.rst_cdc = \
AsyncFFSynchronizer(w_rst, r_rst, o_domain=self._r_domain)
# Decode Gray code counter synchronized from write domain to overwrite binary
# counter in read domain.
rst_dec = m.submodules.rst_dec = \
GrayDecoder(self._ctr_bits)
m.d.comb += rst_dec.i.eq(produce_r_gry)
with m.If(r_rst):
m.d.comb += r_empty.eq(1)
m.d[self._r_domain] += consume_r_gry.eq(produce_r_gry)
m.d[self._r_domain] += consume_r_bin.eq(rst_dec.o)
m.d[self._r_domain] += self.r_rst.eq(1)
with m.Else():
m.d[self._r_domain] += self.r_rst.eq(0)
if platform == "formal":
with m.If(Initial()):
m.d.comb += Assume(produce_w_gry == (produce_w_bin ^ produce_w_bin[1:]))
m.d.comb += Assume(consume_r_gry == (consume_r_bin ^ consume_r_bin[1:]))
return m
class AsyncFIFOBuffered(Elaboratable, FIFOInterface):
__doc__ = FIFOInterface._doc_template.format(
description="""
Buffered asynchronous first in, first out queue.
Read and write interfaces are accessed from different clock domains, which can be set when
constructing the FIFO.
:class:`AsyncFIFOBuffered` only supports power of 2 plus one depths. Unless ``exact_depth``
is specified, the ``depth`` parameter is rounded up to the next power of 2 plus one.
(The output buffer acts as an additional queue element.)
This queue's interface is identical to :class:`AsyncFIFO`, but it has an additional register
on the output, improving timing in case of block RAM that has large clock-to-output delay.
In exchange, the latency between an entry being written to an empty queue and that entry
becoming available on the output is increased by one cycle compared to :class:`AsyncFIFO`.
""".strip(),
parameters="""
r_domain : str
Read clock domain.
w_domain : str
Write clock domain.
""".strip(),
attributes="""
fwft : bool
Always set.
""".strip(),
r_data_valid="Valid if ``r_rdy`` is asserted.",
r_attributes="""
r_rst : Signal, out
Asserted while the FIFO is being reset by the write-domain reset (for at least one
read-domain clock cycle).
""".strip(),
w_attributes="")
def __init__(self, *, width, depth, r_domain="read", w_domain="write", exact_depth=False):
if depth != 0:
try:
depth_bits = log2_int(max(0, depth - 1), need_pow2=exact_depth)
depth = (1 << depth_bits) + 1
except ValueError:
raise ValueError("AsyncFIFOBuffered only supports depths that are one higher "
"than powers of 2; requested exact depth {} is not"
.format(depth)) from None
super().__init__(width=width, depth=depth, fwft=True)
self.r_rst = Signal()
self._r_domain = r_domain
self._w_domain = w_domain
def elaborate(self, platform):
m = Module()
if self.depth == 0:
m.d.comb += [
self.w_rdy.eq(0),
self.r_rdy.eq(0),
]
return m
m.submodules.unbuffered = fifo = AsyncFIFO(width=self.width, depth=self.depth - 1,
r_domain=self._r_domain, w_domain=self._w_domain)
m.d.comb += [
fifo.w_data.eq(self.w_data),
self.w_rdy.eq(fifo.w_rdy),
fifo.w_en.eq(self.w_en),
]
r_consume_buffered = Signal()
m.d.comb += r_consume_buffered.eq((self.r_rdy - self.r_en) & self.r_rdy)
m.d[self._r_domain] += self.r_level.eq(fifo.r_level + r_consume_buffered)
w_consume_buffered = Signal()
m.submodules.consume_buffered_cdc = FFSynchronizer(r_consume_buffered, w_consume_buffered, o_domain=self._w_domain, stages=4)
m.d.comb += self.w_level.eq(fifo.w_level + w_consume_buffered)
with m.If(self.r_en | ~self.r_rdy):
m.d[self._r_domain] += [
self.r_data.eq(fifo.r_data),
self.r_rdy.eq(fifo.r_rdy),
self.r_rst.eq(fifo.r_rst),
]
m.d.comb += [
fifo.r_en.eq(1)
]
return m
import warnings
warnings.warn("instead of nmigen.lib.fifo, use amaranth.lib.fifo",
DeprecationWarning, stacklevel=2)

View file

@ -1,116 +1,7 @@
from .. import *
from ..hdl.rec import *
from amaranth.lib.io import *
from amaranth.lib.io import __all__
__all__ = ["pin_layout", "Pin"]
def pin_layout(width, dir, xdr=0):
"""
Layout of the platform interface of a pin or several pins, which may be used inside
user-defined records.
See :class:`Pin` for details.
"""
if not isinstance(width, int) or width < 1:
raise TypeError("Width must be a positive integer, not {!r}"
.format(width))
if dir not in ("i", "o", "oe", "io"):
raise TypeError("Direction must be one of \"i\", \"o\", \"io\", or \"oe\", not {!r}"""
.format(dir))
if not isinstance(xdr, int) or xdr < 0:
raise TypeError("Gearing ratio must be a non-negative integer, not {!r}"
.format(xdr))
fields = []
if dir in ("i", "io"):
if xdr > 0:
fields.append(("i_clk", 1))
if xdr > 2:
fields.append(("i_fclk", 1))
if xdr in (0, 1):
fields.append(("i", width))
else:
for n in range(xdr):
fields.append(("i{}".format(n), width))
if dir in ("o", "oe", "io"):
if xdr > 0:
fields.append(("o_clk", 1))
if xdr > 2:
fields.append(("o_fclk", 1))
if xdr in (0, 1):
fields.append(("o", width))
else:
for n in range(xdr):
fields.append(("o{}".format(n), width))
if dir in ("oe", "io"):
fields.append(("oe", 1))
return Layout(fields)
class Pin(Record):
"""
An interface to an I/O buffer or a group of them that provides uniform access to input, output,
or tristate buffers that may include a 1:n gearbox. (A 1:2 gearbox is typically called "DDR".)
A :class:`Pin` is identical to a :class:`Record` that uses the corresponding :meth:`pin_layout`
except that it allos accessing the parameters like ``width`` as attributes. It is legal to use
a plain :class:`Record` anywhere a :class:`Pin` is used, provided that these attributes are
not necessary.
Parameters
----------
width : int
Width of the ``i``/``iN`` and ``o``/``oN`` signals.
dir : ``"i"``, ``"o"``, ``"io"``, ``"oe"``
Direction of the buffers. If ``"i"`` is specified, only the ``i``/``iN`` signals are
present. If ``"o"`` is specified, only the ``o``/``oN`` signals are present. If ``"oe"`` is
specified, the ``o``/``oN`` signals are present, and an ``oe`` signal is present.
If ``"io"`` is specified, both the ``i``/``iN`` and ``o``/``oN`` signals are present, and
an ``oe`` signal is present.
xdr : int
Gearbox ratio. If equal to 0, the I/O buffer is combinatorial, and only ``i``/``o``
signals are present. If equal to 1, the I/O buffer is SDR, and only ``i``/``o`` signals are
present. If greater than 1, the I/O buffer includes a gearbox, and ``iN``/``oN`` signals
are present instead, where ``N in range(0, N)``. For example, if ``xdr=2``, the I/O buffer
is DDR; the signal ``i0`` reflects the value at the rising edge, and the signal ``i1``
reflects the value at the falling edge.
name : str
Name of the underlying record.
Attributes
----------
i_clk:
I/O buffer input clock. Synchronizes `i*`. Present if ``xdr`` is nonzero.
i_fclk:
I/O buffer input fast clock. Synchronizes `i*` on higer gearbox ratios. Present if ``xdr``
is greater than 2.
i : Signal, out
I/O buffer input, without gearing. Present if ``dir="i"`` or ``dir="io"``, and ``xdr`` is
equal to 0 or 1.
i0, i1, ... : Signal, out
I/O buffer inputs, with gearing. Present if ``dir="i"`` or ``dir="io"``, and ``xdr`` is
greater than 1.
o_clk:
I/O buffer output clock. Synchronizes `o*`, including `oe`. Present if ``xdr`` is nonzero.
o_fclk:
I/O buffer output fast clock. Synchronizes `o*` on higher gearbox ratios. Present if
``xdr`` is greater than 2.
o : Signal, in
I/O buffer output, without gearing. Present if ``dir="o"`` or ``dir="io"``, and ``xdr`` is
equal to 0 or 1.
o0, o1, ... : Signal, in
I/O buffer outputs, with gearing. Present if ``dir="o"`` or ``dir="io"``, and ``xdr`` is
greater than 1.
oe : Signal, in
I/O buffer output enable. Present if ``dir="io"`` or ``dir="oe"``. Buffers generally
cannot change direction more than once per cycle, so at most one output enable signal
is present.
"""
def __init__(self, width, dir, *, xdr=0, name=None, src_loc_at=0):
self.width = width
self.dir = dir
self.xdr = xdr
super().__init__(pin_layout(self.width, self.dir, self.xdr),
name=name, src_loc_at=src_loc_at + 1)
import warnings
warnings.warn("instead of nmigen.lib.io, use amaranth.lib.io",
DeprecationWarning, stacklevel=2)

View file

@ -1,60 +1,7 @@
from .. import *
from amaranth.lib.scheduler import *
from amaranth.lib.scheduler import __all__
__all__ = ["RoundRobin"]
class RoundRobin(Elaboratable):
"""Round-robin scheduler.
For a given set of requests, the round-robin scheduler will
grant one request. Once it grants a request, if any other
requests are active, it grants the next active request with
a greater number, restarting from zero once it reaches the
highest one.
Use :class:`EnableInserter` to control when the scheduler
is updated.
Parameters
----------
count : int
Number of requests.
Attributes
----------
requests : Signal(count), in
Set of requests.
grant : Signal(range(count)), out
Number of the granted request. Does not change if there are no
active requests.
valid : Signal(), out
Asserted if grant corresponds to an active request. Deasserted
otherwise, i.e. if no requests are active.
"""
def __init__(self, *, count):
if not isinstance(count, int) or count < 0:
raise ValueError("Count must be a non-negative integer, not {!r}"
.format(count))
self.count = count
self.requests = Signal(count)
self.grant = Signal(range(count))
self.valid = Signal()
def elaborate(self, platform):
m = Module()
with m.Switch(self.grant):
for i in range(self.count):
with m.Case(i):
for pred in reversed(range(i)):
with m.If(self.requests[pred]):
m.d.sync += self.grant.eq(pred)
for succ in reversed(range(i + 1, self.count)):
with m.If(self.requests[succ]):
m.d.sync += self.grant.eq(succ)
m.d.sync += self.valid.eq(self.requests.any())
return m
import warnings
warnings.warn("instead of nmigen.lib.scheduler, use amaranth.lib.scheduler",
DeprecationWarning, stacklevel=2)