2013-03-22 13:37:10 -04:00
|
|
|
from migen.fhdl.structure import *
|
|
|
|
from migen.fhdl.module import Module
|
|
|
|
from migen.genlib.cdc import MultiReg
|
|
|
|
from migen.genlib.fifo import SyncFIFO
|
2013-03-22 18:49:25 -04:00
|
|
|
from migen.genlib.record import Record
|
2013-03-22 13:37:10 -04:00
|
|
|
from migen.genlib.misc import optree
|
|
|
|
from migen.bank.description import *
|
|
|
|
|
2013-03-22 18:49:25 -04:00
|
|
|
from milkymist.dvisampler.common import channel_layout
|
2013-03-22 13:37:10 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
lst_control_starts = []
|
|
|
|
all_control_starts = Signal()
|
|
|
|
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)
|
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
fifo = SyncFIFO(10, depth)
|
|
|
|
self.add_submodule(fifo, "pix")
|
|
|
|
self.comb += [
|
2013-03-22 18:49:25 -04:00
|
|
|
fifo.we.eq(self.valid_i),
|
2013-04-14 11:06:29 -04:00
|
|
|
fifo.din.eq(data_in.raw_bits()),
|
|
|
|
data_out.raw_bits().eq(fifo.dout)
|
2013-03-22 13:37:10 -04:00
|
|
|
]
|
|
|
|
is_control = Signal()
|
|
|
|
is_control_r = Signal()
|
2013-03-22 18:49:25 -04:00
|
|
|
self.sync.pix += If(fifo.readable & fifo.re, is_control_r.eq(is_control))
|
2013-03-22 13:37:10 -04:00
|
|
|
control_starts = Signal()
|
|
|
|
self.comb += [
|
2013-03-22 18:49:25 -04:00
|
|
|
is_control.eq(~data_out.de),
|
2013-03-22 13:37:10 -04:00
|
|
|
control_starts.eq(is_control & ~is_control_r),
|
|
|
|
fifo.re.eq(~is_control | all_control_starts)
|
|
|
|
]
|
|
|
|
lst_control_starts.append(control_starts)
|
|
|
|
|
2013-04-16 16:21:03 -04:00
|
|
|
some_control_starts = Signal()
|
|
|
|
self.comb += [
|
|
|
|
all_control_starts.eq(optree("&", lst_control_starts)),
|
|
|
|
some_control_starts.eq(optree("|", lst_control_starts))
|
|
|
|
]
|
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)
|
2013-04-16 16:21:03 -04:00
|
|
|
).Else(
|
|
|
|
If(some_control_starts,
|
|
|
|
If(all_control_starts,
|
|
|
|
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)
|