litex/misoclib/dvisampler/chansync.py

89 lines
2.2 KiB
Python
Raw Normal View History

2013-05-22 11:10:13 -04:00
from migen.fhdl.std import *
2013-03-22 13:37:10 -04:00
from migen.genlib.cdc import MultiReg
2013-05-05 09:07:57 -04:00
from migen.genlib.fifo import _inc
2013-05-05 06:58:24 -04:00
from migen.genlib.record import Record, layout_len
2013-03-22 13:37:10 -04:00
from migen.genlib.misc import optree
from migen.bank.description import *
2013-11-09 09:27:32 -05:00
from misoclib.dvisampler.common import channel_layout
2013-03-22 13:37:10 -04:00
2013-05-05 09:07:57 -04:00
class _SyncBuffer(Module):
def __init__(self, width, depth):
self.din = Signal(width)
self.dout = Signal(width)
self.re = Signal()
###
produce = Signal(max=depth)
consume = Signal(max=depth)
storage = Memory(width, depth)
self.specials += storage
wrport = storage.get_port(write_capable=True)
2013-05-28 09:56:14 -04:00
self.specials += wrport
2013-05-05 09:07:57 -04:00
self.comb += [
wrport.adr.eq(produce),
wrport.dat_w.eq(self.din),
wrport.we.eq(1)
]
self.sync += _inc(produce, depth)
rdport = storage.get_port(async_read=True)
2013-05-28 09:56:14 -04:00
self.specials += rdport
2013-05-05 09:07:57 -04:00
self.comb += [
rdport.adr.eq(consume),
self.dout.eq(rdport.dat_r)
]
self.sync += If(self.re, _inc(consume, depth))
2013-03-30 12:28:15 -04:00
class ChanSync(Module, AutoCSR):
2013-03-22 13:37:10 -04:00
def __init__(self, nchan=3, depth=8):
2013-03-22 18:49:25 -04:00
self.valid_i = Signal()
2013-03-22 13:37:10 -04:00
self.chan_synced = Signal()
2013-03-30 12:28:15 -04:00
self._r_channels_synced = CSRStatus()
2013-03-22 13:37:10 -04:00
2013-05-05 09:07:57 -04:00
lst_control = []
all_control = Signal()
2013-03-22 13:37:10 -04:00
for i in range(nchan):
name = "data_in" + str(i)
2013-03-22 18:49:25 -04:00
data_in = Record(channel_layout, name=name)
2013-03-22 13:37:10 -04:00
setattr(self, name, data_in)
name = "data_out" + str(i)
2013-03-22 18:49:25 -04:00
data_out = Record(channel_layout, name=name)
2013-03-22 13:37:10 -04:00
setattr(self, name, data_out)
###
syncbuffer = RenameClockDomains(_SyncBuffer(layout_len(channel_layout), depth), "pix")
self.submodules += syncbuffer
2013-03-22 13:37:10 -04:00
self.comb += [
2013-05-05 09:07:57 -04:00
syncbuffer.din.eq(data_in.raw_bits()),
data_out.raw_bits().eq(syncbuffer.dout)
2013-03-22 13:37:10 -04:00
]
is_control = Signal()
self.comb += [
2013-03-22 18:49:25 -04:00
is_control.eq(~data_out.de),
2013-05-05 09:07:57 -04:00
syncbuffer.re.eq(~is_control | all_control)
2013-03-22 13:37:10 -04:00
]
2013-05-05 09:07:57 -04:00
lst_control.append(is_control)
2013-03-22 13:37:10 -04:00
2013-05-05 09:07:57 -04:00
some_control = Signal()
self.comb += [
2013-05-05 09:07:57 -04:00
all_control.eq(optree("&", lst_control)),
some_control.eq(optree("|", lst_control))
]
2013-03-22 18:49:25 -04:00
self.sync.pix += If(~self.valid_i,
2013-03-22 13:37:10 -04:00
self.chan_synced.eq(0)
).Else(
2013-05-05 09:07:57 -04:00
If(some_control,
If(all_control,
self.chan_synced.eq(1)
).Else(
self.chan_synced.eq(0)
)
)
)
2013-03-30 12:28:15 -04:00
self.specials += MultiReg(self.chan_synced, self._r_channels_synced.status)