2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-05 09:46:36 +08:00
artiq/soc/artiqlib/rtio/core.py

376 lines
14 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
from artiqlib.rtio.rbus import get_fine_ts_width
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
# * guard time detection latency: Tsys
# * FIFO latency: Tsys + 2*Tio
# Therefore we must choose:
# guard_io_cycles > (3*Tio + 5*Tsys)/Tio
#
# We are writing to the FIFO from the buffer when the guard time has been
# reached without checking the FIFO's writable status. If the FIFO is full,
# this will produce an overflow and the event will be incorrectly discarded.
#
# When the FIFO is full, it contains fifo_depth events of strictly increasing
# timestamps.
#
# Thus the overflow-causing event's timestamp must satisfy:
# timestamp*Tio > fifo_depth*Tio + time
# 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
#
# We can prevent overflows by choosing instead:
# guard_io_cycles < fifo_depth
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-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-09-11 23:09:20 +08:00
self.replace = Signal()
2014-11-30 00:13:54 +08:00
self.underflow = Signal() # valid 2 cycles after we/replace
self.underflow_reset = Signal()
2014-09-05 12:03:22 +08:00
# # #
2014-11-30 00:13:54 +08:00
signal_underflow = 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
buf_valid = 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()
# 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-11-30 00:13:54 +08:00
self.sync.rsys += If(in_guard_time, buf_valid.eq(0))
self.comb += \
If(buf_valid,
If(in_guard_time,
If(buf_just_written,
signal_underflow.eq(1)
).Else(
fifo.we.eq(1)
)
),
If(self.we & (self.sel == n), fifo.we.eq(1))
)
# Buffer write
# Must come after read to handle concurrent read+write properly
self.sync.rsys += [
buf_just_written.eq(0),
If((self.we | self.replace) & (self.sel == n),
# Replace operations on empty buffers may happen
# on underflows, which will be correctly reported.
buf_just_written.eq(1),
buf_valid.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
]
# FIFO read
self.comb += [
chif.o_stb.eq(fifo.readable &
2014-11-30 00:13:54 +08:00
(fifo.dout.timestamp[fine_ts_width:] == counter.o_value_rio)),
2014-09-05 12:03:22 +08:00
chif.o_value.eq(fifo.dout.value),
fifo.re.eq(chif.o_stb)
]
if fine_ts_width:
2014-09-05 17:06:41 +08:00
self.comb += chif.o_fine_ts.eq(
fifo.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)),
If(signal_underflow, self.underflow.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):
2014-11-30 00:13:54 +08:00
def __init__(self, phy, clk_freq, counter_width=32,
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(
phy.rbus, self.counter, fine_ts_width, ofifo_depth)
2014-09-05 12:03:22 +08:00
# CSRs
2014-11-30 00:13:54 +08:00
self._r_reset = CSRStorage(reset=1)
2014-09-05 12:03:22 +08:00
self._r_chan_sel = CSRStorage(flen(self.bank_o.sel))
2014-09-05 17:06:41 +08:00
2014-09-05 12:03:22 +08:00
self._r_oe = CSR()
2014-11-30 00:13:54 +08:00
self._r_o_timestamp = CSRStorage(counter_width + fine_ts_width)
2014-09-05 12:03:22 +08:00
self._r_o_value = CSRStorage(2)
self._r_o_writable = CSRStatus()
self._r_o_we = CSR()
2014-09-11 23:09:20 +08:00
self._r_o_replace = CSR()
2014-10-21 23:14:01 +08:00
self._r_o_underflow = CSRStatus()
2014-11-30 00:13:54 +08:00
self._r_o_underflow_reset = CSR()
2014-09-05 12:03:22 +08:00
2014-11-30 00:13:54 +08:00
self._r_i_timestamp = CSRStatus(counter_width + fine_ts_width)
2014-09-05 12:03:22 +08:00
self._r_i_value = CSRStatus()
self._r_i_readable = CSRStatus()
self._r_i_re = CSR()
2014-10-21 23:14:01 +08:00
self._r_i_overflow = CSRStatus()
2014-11-30 00:13:54 +08:00
self._r_i_overflow_reset = CSR()
2014-10-21 23:14:01 +08:00
self._r_i_pileup_count = CSRStatus(16)
self._r_i_pileup_reset = CSR()
2014-09-05 12:03:22 +08:00
2014-11-30 00:13:54 +08:00
self._r_counter = CSRStatus(counter_width + fine_ts_width)
self._r_counter_update = CSR()
self._r_frequency_i = CSRStatus(32)
self._r_frequency_fn = CSRStatus(8)
self._r_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()),
self.cd_rsys.rst.eq(self._r_reset.storage)
]
self.comb += self.cd_rio.clk.eq(ClockSignal("rtio"))
self.specials += AsyncResetSynchronizer(
self.cd_rio, self._r_reset.storage)
2014-09-05 12:03:22 +08:00
# OE
oes = []
for n, chif in enumerate(phy.rbus):
if hasattr(chif, "oe"):
self.sync += \
If(self._r_oe.re & (self._r_chan_sel.storage == n),
chif.oe.eq(self._r_oe.r)
)
oes.append(chif.oe)
else:
oes.append(1)
self.comb += self._r_oe.w.eq(Array(oes)[self._r_chan_sel.storage])
# Output/Gate
self.comb += [
self.bank_o.sel.eq(self._r_chan_sel.storage),
self.bank_o.timestamp.eq(self._r_o_timestamp.storage),
self.bank_o.value.eq(self._r_o_value.storage),
self._r_o_writable.status.eq(self.bank_o.writable),
self.bank_o.we.eq(self._r_o_we.re),
2014-09-11 23:09:20 +08:00
self.bank_o.replace.eq(self._r_o_replace.re),
2014-10-21 23:14:01 +08:00
self._r_o_underflow.status.eq(self.bank_o.underflow),
2014-11-30 00:13:54 +08:00
self.bank_o.underflow_reset.eq(self._r_o_underflow_reset.re)
2014-09-05 12:03:22 +08:00
]
# Input
self.comb += [
self.bank_i.sel.eq(self._r_chan_sel.storage),
self._r_i_timestamp.status.eq(self.bank_i.timestamp),
self._r_i_value.status.eq(self.bank_i.value),
self._r_i_readable.status.eq(self.bank_i.readable),
self.bank_i.re.eq(self._r_i_re.re),
2014-10-21 23:14:01 +08:00
self._r_i_overflow.status.eq(self.bank_i.overflow),
2014-11-30 00:13:54 +08:00
self.bank_i.overflow_reset.eq(self._r_i_overflow_reset.re),
2014-10-21 23:14:01 +08:00
self._r_i_pileup_count.status.eq(self.bank_i.pileup_count),
self.bank_i.pileup_reset.eq(self._r_i_pileup_reset.re)
2014-09-05 12:03:22 +08:00
]
2014-10-10 20:12:22 +08:00
# Counter access
self.sync += \
If(self._r_counter_update.re,
self._r_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 += [
self._r_frequency_i.status.eq(clk_freq_i),
self._r_frequency_fn.status.eq(clk_freq_f.numerator),
self._r_frequency_fd.status.eq(clk_freq_f.denominator)
]