artiq/artiq/gateware/rtio/core.py

406 lines
15 KiB
Python
Raw Normal View History

from fractions import Fraction
2014-07-26 06:23:35 +08:00
from migen.fhdl.std import *
from migen.bank.description import *
2014-11-30 00:59:39 +08:00
from migen.genlib.record import Record
2014-11-30 00:13:54 +08:00
from migen.genlib.cdc import *
from migen.genlib.fifo import AsyncFIFO
from migen.genlib.resetsync import AsyncResetSynchronizer
2014-07-26 06:23:35 +08:00
2015-03-08 18:00:24 +08:00
from artiq.gateware.rtio.rbus import get_fine_ts_width
2014-07-26 06:23:35 +08:00
2014-09-05 12:03:22 +08:00
2014-11-30 00:13:54 +08:00
class _GrayCodeTransfer(Module):
def __init__(self, width):
self.i = Signal(width) # in rio domain
self.o = Signal(width) # in rsys domain
# # #
# convert to Gray code
value_gray_rio = Signal(width)
self.sync.rio += value_gray_rio.eq(self.i ^ self.i[1:])
# transfer to system clock domain
value_gray_sys = Signal(width)
self.specials += [
NoRetiming(value_gray_rio),
MultiReg(value_gray_rio, value_gray_sys, "rsys")
]
# convert back to binary
value_sys = Signal(width)
self.comb += value_sys[-1].eq(value_gray_sys[-1])
for i in reversed(range(width-1)):
self.comb += value_sys[i].eq(value_sys[i+1] ^ value_gray_sys[i])
self.sync.rsys += self.o.eq(value_sys)
class _RTIOCounter(Module):
def __init__(self, width, loopback_latency):
self.width = width
# Timestamp counter in RTIO domain for outputs
self.o_value_rio = Signal(width)
# Timestamp counter resynchronized to sys domain
# Lags behind o_value_rio, monotonic and glitch-free
self.o_value_sys = Signal(width)
# Timestamp counter in RTIO domain for inputs,
# compensated for PHY loopback latency
self.i_value_rio = Signal(width, reset=2**width-loopback_latency)
# # #
self.sync.rio += [
self.o_value_rio.eq(self.o_value_rio + 1),
self.i_value_rio.eq(self.i_value_rio + 1)
]
gt = _GrayCodeTransfer(width)
self.submodules += gt
self.comb += gt.i.eq(self.o_value_rio), self.o_value_sys.eq(gt.o)
# CHOOSING A GUARD TIME
#
# The buffer must be transferred to the FIFO soon enough to account for:
# * transfer of counter to sys domain: Tio + 2*Tsys + Tsys
# * FIFO latency: Tsys + 2*Tio
# * FIFO buffer latency: Tio
2014-11-30 00:13:54 +08:00
# Therefore we must choose:
# guard_io_cycles > (4*Tio + 4*Tsys)/Tio
2014-11-30 00:13:54 +08:00
#
2015-04-02 18:22:18 +08:00
# We are writing to the FIFO from the buffer when the guard time has been
# reached. This can fill the FIFO and deassert the writable flag. A race
# condition occurs that causes problems if the deassertion happens between
# the CPU checking the writable flag (and reading 1) and writing a new event.
2014-11-30 00:13:54 +08:00
#
# When the FIFO is about to be full, it contains fifo_depth-1 events of
# strictly increasing timestamps.
2014-11-30 00:13:54 +08:00
#
# Thus the FIFO-filling event's timestamp must satisfy:
# timestamp*Tio > (fifo_depth-1)*Tio + time
2014-11-30 00:13:54 +08:00
# We also have (guard time reached):
# timestamp*Tio < time + guard_io_cycles*Tio
# [NB: time > counter.o_value_sys*Tio]
# Thus we must have:
# guard_io_cycles > fifo_depth-1
2014-11-30 00:13:54 +08:00
#
# We can prevent overflows by choosing instead:
# guard_io_cycles < fifo_depth-1
2014-11-30 00:13:54 +08:00
2014-07-26 06:23:35 +08:00
class _RTIOBankO(Module):
2014-11-30 00:13:54 +08:00
def __init__(self, rbus, counter, fine_ts_width, fifo_depth, guard_io_cycles):
2014-09-05 12:03:22 +08:00
self.sel = Signal(max=len(rbus))
2014-12-01 17:32:36 +08:00
# timestamp and value must be valid 1 cycle before we
2014-11-30 00:13:54 +08:00
self.timestamp = Signal(counter.width + fine_ts_width)
2014-09-05 12:03:22 +08:00
self.value = Signal(2)
self.writable = Signal()
2014-11-30 00:13:54 +08:00
self.we = Signal() # maximum throughput 1/2
2014-12-01 17:32:36 +08:00
self.underflow = Signal() # valid 2 cycles after we
2014-11-30 00:13:54 +08:00
self.underflow_reset = Signal()
2014-12-01 17:32:36 +08:00
self.sequence_error = Signal()
self.sequence_error_reset = Signal()
2014-09-05 12:03:22 +08:00
# # #
2014-11-30 00:13:54 +08:00
signal_underflow = Signal()
2014-12-01 17:32:36 +08:00
signal_sequence_error = Signal()
2014-09-05 12:03:22 +08:00
fifos = []
2014-11-30 00:59:39 +08:00
ev_layout = [("timestamp", counter.width + fine_ts_width),
("value", 2)]
2014-09-05 12:03:22 +08:00
for n, chif in enumerate(rbus):
2014-11-30 00:13:54 +08:00
# FIFO
2014-11-30 00:59:39 +08:00
fifo = RenameClockDomains(AsyncFIFO(ev_layout, fifo_depth),
2014-11-30 00:13:54 +08:00
{"write": "rsys", "read": "rio"})
2014-09-05 12:03:22 +08:00
self.submodules += fifo
fifos.append(fifo)
2014-11-30 00:13:54 +08:00
# Buffer
2014-12-01 17:32:36 +08:00
buf_pending = Signal()
2014-11-30 00:59:39 +08:00
buf = Record(ev_layout)
2014-11-30 00:13:54 +08:00
buf_just_written = Signal()
2014-12-01 17:32:36 +08:00
# Special cases
replace = Signal()
sequence_error = Signal()
nop = Signal()
self.sync.rsys += [
replace.eq(self.timestamp == buf.timestamp[fine_ts_width:]),
sequence_error.eq(self.timestamp < buf.timestamp[fine_ts_width:]),
nop.eq(self.value == buf.value)
]
self.comb += If(self.we & (self.sel == n) & sequence_error,
signal_sequence_error.eq(1))
2014-11-30 00:13:54 +08:00
# Buffer read and FIFO write
2014-11-30 00:59:39 +08:00
self.comb += fifo.din.eq(buf)
2014-11-30 00:13:54 +08:00
in_guard_time = Signal()
self.comb += in_guard_time.eq(
2014-11-30 00:59:39 +08:00
buf.timestamp[fine_ts_width:] < counter.o_value_sys + guard_io_cycles)
2014-12-01 17:32:36 +08:00
self.sync.rsys += If(in_guard_time, buf_pending.eq(0))
2014-11-30 00:13:54 +08:00
self.comb += \
2014-12-01 17:32:36 +08:00
If(buf_pending,
2014-11-30 00:13:54 +08:00
If(in_guard_time,
If(buf_just_written,
signal_underflow.eq(1)
).Else(
fifo.we.eq(1)
)
),
2014-12-01 17:32:36 +08:00
If((self.we & (self.sel == n)
& ~replace & ~nop & ~sequence_error),
fifo.we.eq(1)
)
2014-11-30 00:13:54 +08:00
)
# Buffer write
# Must come after read to handle concurrent read+write properly
self.sync.rsys += [
buf_just_written.eq(0),
2014-12-01 17:32:36 +08:00
If(self.we & (self.sel == n) & ~nop & ~sequence_error,
2014-11-30 00:13:54 +08:00
buf_just_written.eq(1),
2014-12-01 17:32:36 +08:00
buf_pending.eq(1),
2014-11-30 00:59:39 +08:00
buf.timestamp.eq(self.timestamp),
buf.value.eq(self.value)
2014-11-30 00:13:54 +08:00
)
2014-09-05 12:03:22 +08:00
]
# Buffer output of FIFO to improve timing
dout_stb = Signal()
dout_ack = Signal()
dout = Record(ev_layout)
self.sync.rio += \
If(fifo.re,
dout_stb.eq(1),
dout.eq(fifo.dout)
).Elif(dout_ack,
dout_stb.eq(0)
)
self.comb += fifo.re.eq(fifo.readable & (~dout_stb | dout_ack))
# FIFO read through buffer
2014-09-05 12:03:22 +08:00
self.comb += [
dout_ack.eq(
dout.timestamp[fine_ts_width:] == counter.o_value_rio),
chif.o_stb.eq(dout_stb & dout_ack),
chif.o_value.eq(dout.value)
2014-09-05 12:03:22 +08:00
]
if fine_ts_width:
2014-09-05 17:06:41 +08:00
self.comb += chif.o_fine_ts.eq(
dout.timestamp[:fine_ts_width])
2014-09-05 12:03:22 +08:00
2014-11-30 00:13:54 +08:00
self.comb += \
self.writable.eq(Array(fifo.writable for fifo in fifos)[self.sel])
self.sync.rsys += [
If(self.underflow_reset, self.underflow.eq(0)),
2014-12-01 17:32:36 +08:00
If(self.sequence_error_reset, self.sequence_error.eq(0)),
If(signal_underflow, self.underflow.eq(1)),
If(signal_sequence_error, self.sequence_error.eq(1))
2014-09-05 17:06:41 +08:00
]
2014-09-05 12:03:22 +08:00
2014-07-26 06:23:35 +08:00
class _RTIOBankI(Module):
2014-10-10 20:12:22 +08:00
def __init__(self, rbus, counter, fine_ts_width, fifo_depth):
2014-09-05 12:03:22 +08:00
self.sel = Signal(max=len(rbus))
2014-11-30 00:13:54 +08:00
self.timestamp = Signal(counter.width + fine_ts_width)
2014-09-05 12:03:22 +08:00
self.value = Signal()
self.readable = Signal()
self.re = Signal()
self.overflow = Signal()
2014-11-30 00:13:54 +08:00
self.overflow_reset = Signal()
2014-10-21 23:14:01 +08:00
self.pileup_count = Signal(16)
self.pileup_reset = Signal()
2014-09-05 12:03:22 +08:00
2014-10-10 20:12:22 +08:00
# # #
2014-09-05 12:03:22 +08:00
timestamps = []
values = []
readables = []
overflows = []
2014-10-21 23:14:01 +08:00
pileup_counts = []
2014-11-30 00:59:39 +08:00
ev_layout = [("timestamp", counter.width+fine_ts_width),
("value", 1)]
2014-09-05 12:03:22 +08:00
for n, chif in enumerate(rbus):
if hasattr(chif, "oe"):
sensitivity = Signal(2)
2014-11-30 00:13:54 +08:00
self.sync.rio += If(~chif.oe & chif.o_stb,
sensitivity.eq(chif.o_value))
2014-09-05 12:03:22 +08:00
2014-11-30 00:59:39 +08:00
fifo = RenameClockDomains(AsyncFIFO(ev_layout, fifo_depth),
2014-11-30 00:13:54 +08:00
{"read": "rsys", "write": "rio"})
2014-09-05 12:03:22 +08:00
self.submodules += fifo
2014-09-05 17:06:41 +08:00
2014-09-05 12:03:22 +08:00
# FIFO write
if fine_ts_width:
2014-11-30 00:13:54 +08:00
full_ts = Cat(chif.i_fine_ts, counter.i_value_rio)
2014-09-05 12:03:22 +08:00
else:
2014-11-30 00:13:54 +08:00
full_ts = counter.i_value_rio
2014-09-05 12:03:22 +08:00
self.comb += [
fifo.din.timestamp.eq(full_ts),
fifo.din.value.eq(chif.i_value),
2014-09-05 17:06:41 +08:00
fifo.we.eq(
~chif.oe & chif.i_stb &
((chif.i_value & sensitivity[0])
| (~chif.i_value & sensitivity[1])))
2014-09-05 12:03:22 +08:00
]
# FIFO read
timestamps.append(fifo.dout.timestamp)
values.append(fifo.dout.value)
readables.append(fifo.readable)
self.comb += fifo.re.eq(self.re & (self.sel == n))
2014-09-05 17:06:41 +08:00
2014-09-05 12:03:22 +08:00
overflow = Signal()
2014-11-30 00:13:54 +08:00
overflow_reset_sync = PulseSynchronizer("rsys", "rio")
self.submodules += overflow_reset_sync
self.comb += overflow_reset_sync.i.eq(
self.overflow_reset & (self.sel == n))
self.sync.rio += [
If(overflow_reset_sync.o, overflow.eq(0)),
If(fifo.we & ~fifo.writable, overflow.eq(1))
]
overflow_sys = Signal()
self.specials += MultiReg(overflow, overflow_sys, "rsys")
overflows.append(overflow_sys)
2014-10-21 23:14:01 +08:00
pileup_count = Signal(16)
2014-11-30 00:13:54 +08:00
pileup_count_reset_sync = PulseSynchronizer("rsys", "rio")
self.submodules += pileup_count_reset_sync
self.comb += pileup_count_reset_sync.i.eq(
self.pileup_reset & (self.sel == n))
self.sync.rio += \
If(pileup_count_reset_sync.o,
2014-10-21 23:14:01 +08:00
pileup_count.eq(0)
).Elif(chif.i_pileup,
If(pileup_count != 2**16 - 1, # saturate
pileup_count.eq(pileup_count + 1)
)
)
2014-11-30 00:13:54 +08:00
pileup_count_sync = _GrayCodeTransfer(16)
self.submodules += pileup_count_sync
self.comb += pileup_count_sync.i.eq(pileup_count)
pileup_counts.append(pileup_count_sync.o)
2014-09-05 12:03:22 +08:00
else:
timestamps.append(0)
values.append(0)
readables.append(0)
overflows.append(0)
2014-10-21 23:14:01 +08:00
pileup_counts.append(0)
2014-09-05 12:03:22 +08:00
self.comb += [
self.timestamp.eq(Array(timestamps)[self.sel]),
self.value.eq(Array(values)[self.sel]),
self.readable.eq(Array(readables)[self.sel]),
self.overflow.eq(Array(overflows)[self.sel]),
2014-10-21 23:14:01 +08:00
self.pileup_count.eq(Array(pileup_counts)[self.sel])
2014-09-05 12:03:22 +08:00
]
2014-07-26 06:23:35 +08:00
class RTIO(Module, AutoCSR):
def __init__(self, phy, clk_freq, counter_width=63,
2014-11-30 00:13:54 +08:00
ofifo_depth=64, ififo_depth=64,
guard_io_cycles=20):
2014-09-05 12:03:22 +08:00
fine_ts_width = get_fine_ts_width(phy.rbus)
# Submodules
2014-11-30 00:13:54 +08:00
self.submodules.counter = _RTIOCounter(
counter_width, phy.loopback_latency)
self.submodules.bank_o = _RTIOBankO(
phy.rbus, self.counter, fine_ts_width, ofifo_depth, guard_io_cycles)
self.submodules.bank_i = _RTIOBankI(
2014-11-30 12:12:35 +08:00
phy.rbus, self.counter, fine_ts_width, ififo_depth)
2014-09-05 12:03:22 +08:00
# CSRs
2015-04-02 18:22:18 +08:00
self._reset = CSRStorage(reset=1)
self._chan_sel = CSRStorage(flen(self.bank_o.sel))
2014-09-05 17:06:41 +08:00
2015-04-02 18:22:18 +08:00
self._oe = CSR()
2014-09-05 12:03:22 +08:00
2015-04-02 18:22:18 +08:00
self._o_timestamp = CSRStorage(counter_width + fine_ts_width)
self._o_value = CSRStorage(2)
self._o_we = CSR()
self._o_status = CSRStatus(3)
self._o_underflow_reset = CSR()
self._o_sequence_error_reset = CSR()
2014-09-05 12:03:22 +08:00
2015-04-02 18:22:18 +08:00
self._i_timestamp = CSRStatus(counter_width + fine_ts_width)
self._i_value = CSRStatus()
self._i_re = CSR()
self._i_status = CSRStatus(2)
self._i_overflow_reset = CSR()
self._i_pileup_count = CSRStatus(16)
self._i_pileup_reset = CSR()
2014-09-05 12:03:22 +08:00
2015-04-02 18:22:18 +08:00
self._counter = CSRStatus(counter_width + fine_ts_width)
self._counter_update = CSR()
2015-04-02 18:22:18 +08:00
self._frequency_i = CSRStatus(32)
self._frequency_fn = CSRStatus(8)
self._frequency_fd = CSRStatus(8)
2014-11-30 00:13:54 +08:00
# Clocking/Reset
# Create rsys and rio domains based on sys and rio
# with reset controlled by CSR.
self.clock_domains.cd_rsys = ClockDomain()
self.clock_domains.cd_rio = ClockDomain()
self.comb += [
self.cd_rsys.clk.eq(ClockSignal()),
2015-04-02 18:22:18 +08:00
self.cd_rsys.rst.eq(self._reset.storage)
2014-11-30 00:13:54 +08:00
]
self.comb += self.cd_rio.clk.eq(ClockSignal("rtio"))
self.specials += AsyncResetSynchronizer(
2015-04-02 18:22:18 +08:00
self.cd_rio, self._reset.storage)
2014-11-30 00:13:54 +08:00
2014-09-05 12:03:22 +08:00
# OE
oes = []
for n, chif in enumerate(phy.rbus):
if hasattr(chif, "oe"):
self.sync += \
2015-04-02 18:22:18 +08:00
If(self._oe.re & (self._chan_sel.storage == n),
chif.oe.eq(self._oe.r)
2014-09-05 12:03:22 +08:00
)
oes.append(chif.oe)
else:
oes.append(1)
2015-04-02 18:22:18 +08:00
self.comb += self._oe.w.eq(Array(oes)[self._chan_sel.storage])
2014-09-05 12:03:22 +08:00
# Output/Gate
self.comb += [
2015-04-02 18:22:18 +08:00
self.bank_o.sel.eq(self._chan_sel.storage),
self.bank_o.timestamp.eq(self._o_timestamp.storage),
self.bank_o.value.eq(self._o_value.storage),
self.bank_o.we.eq(self._o_we.re),
self._o_status.status.eq(Cat(~self.bank_o.writable,
2014-12-01 17:32:36 +08:00
self.bank_o.underflow,
self.bank_o.sequence_error)),
2015-04-02 18:22:18 +08:00
self.bank_o.underflow_reset.eq(self._o_underflow_reset.re),
self.bank_o.sequence_error_reset.eq(self._o_sequence_error_reset.re)
2014-09-05 12:03:22 +08:00
]
# Input
self.comb += [
2015-04-02 18:22:18 +08:00
self.bank_i.sel.eq(self._chan_sel.storage),
self._i_timestamp.status.eq(self.bank_i.timestamp),
self._i_value.status.eq(self.bank_i.value),
self.bank_i.re.eq(self._i_re.re),
self._i_status.status.eq(Cat(~self.bank_i.readable, self.bank_i.overflow)),
self.bank_i.overflow_reset.eq(self._i_overflow_reset.re),
self._i_pileup_count.status.eq(self.bank_i.pileup_count),
self.bank_i.pileup_reset.eq(self._i_pileup_reset.re)
2014-09-05 12:03:22 +08:00
]
2014-10-10 20:12:22 +08:00
# Counter access
self.sync += \
2015-04-02 18:22:18 +08:00
If(self._counter_update.re,
self._counter.status.eq(Cat(Replicate(0, fine_ts_width),
2014-11-30 00:13:54 +08:00
self.counter.o_value_sys))
)
# Frequency
clk_freq = Fraction(clk_freq).limit_denominator(255)
clk_freq_i = int(clk_freq)
clk_freq_f = clk_freq - clk_freq_i
self.comb += [
2015-04-02 18:22:18 +08:00
self._frequency_i.status.eq(clk_freq_i),
self._frequency_fn.status.eq(clk_freq_f.numerator),
self._frequency_fd.status.eq(clk_freq_f.denominator)
]