litex/migen/actorlib/structuring.py

93 lines
2.2 KiB
Python
Raw Normal View History

from migen.fhdl.std import *
2012-06-20 10:35:01 -04:00
from migen.flow.actor import *
def _rawbits_layout(l):
if isinstance(l, int):
return [("rawbits", l)]
else:
return l
2012-06-20 10:35:01 -04:00
class Cast(CombinatorialActor):
def __init__(self, layout_from, layout_to, reverse_from=False, reverse_to=False):
2013-04-10 13:12:42 -04:00
self.sink = Sink(_rawbits_layout(layout_from))
self.source = Source(_rawbits_layout(layout_to))
CombinatorialActor.__init__(self)
2012-06-20 10:35:01 -04:00
2013-04-10 13:12:42 -04:00
###
sigs_from = self.sink.payload.flatten()
if reverse_from:
sigs_from = list(reversed(sigs_from))
2013-04-10 13:12:42 -04:00
sigs_to = self.source.payload.flatten()
if reverse_to:
sigs_to = list(reversed(sigs_to))
if sum(flen(s) for s in sigs_from) != sum(flen(s) for s in sigs_to):
2012-06-20 10:35:01 -04:00
raise TypeError
2013-04-10 13:12:42 -04:00
self.comb += Cat(*sigs_to).eq(Cat(*sigs_from))
2012-06-20 10:35:01 -04:00
2012-06-20 12:25:01 -04:00
def pack_layout(l, n):
2013-04-01 16:15:23 -04:00
return [("chunk"+str(i), l) for i in range(n)]
2012-06-20 12:25:01 -04:00
2013-04-10 13:12:42 -04:00
class Unpack(Module):
2012-06-20 10:35:01 -04:00
def __init__(self, n, layout_to):
2013-04-10 13:12:42 -04:00
self.sink = Sink(pack_layout(layout_to, n))
self.source = Source(layout_to)
self.busy = Signal()
2012-06-20 10:35:01 -04:00
2013-04-10 13:12:42 -04:00
###
mux = Signal(max=n)
2012-06-20 10:35:01 -04:00
last = Signal()
2013-04-10 13:12:42 -04:00
self.comb += [
last.eq(mux == (n-1)),
self.source.stb.eq(self.sink.stb),
self.sink.ack.eq(last & self.source.ack)
2012-06-20 10:35:01 -04:00
]
2013-04-10 13:12:42 -04:00
self.sync += [
If(self.source.stb & self.source.ack,
2012-06-20 10:35:01 -04:00
If(last,
mux.eq(0)
).Else(
mux.eq(mux + 1)
)
)
]
2012-11-28 19:11:15 -05:00
cases = {}
2013-04-10 13:12:42 -04:00
for i in range(n):
cases[i] = [self.source.payload.raw_bits().eq(getattr(self.sink.payload, "chunk"+str(i)).raw_bits())]
self.comb += Case(mux, cases).makedefault()
2012-06-20 10:35:01 -04:00
2013-04-10 13:12:42 -04:00
class Pack(Module):
2012-06-20 10:35:01 -04:00
def __init__(self, layout_from, n):
2013-04-10 13:12:42 -04:00
self.sink = Sink(layout_from)
self.source = Source(pack_layout(layout_from, n))
self.busy = Signal()
2012-06-20 10:35:01 -04:00
2013-04-10 13:12:42 -04:00
###
demux = Signal(max=n)
2012-06-20 10:35:01 -04:00
load_part = Signal()
strobe_all = Signal()
2012-11-28 19:11:15 -05:00
cases = {}
2013-04-10 13:12:42 -04:00
for i in range(n):
cases[i] = [getattr(self.source.payload, "chunk"+str(i)).raw_bits().eq(self.sink.payload.raw_bits())]
self.comb += [
self.busy.eq(strobe_all),
2013-04-10 13:12:42 -04:00
self.sink.ack.eq(~strobe_all | self.source.ack),
self.source.stb.eq(strobe_all),
load_part.eq(self.sink.stb & self.sink.ack)
2012-06-20 10:35:01 -04:00
]
2013-04-10 13:12:42 -04:00
self.sync += [
If(self.source.ack, strobe_all.eq(0)),
2012-06-20 10:35:01 -04:00
If(load_part,
2012-11-28 19:11:15 -05:00
Case(demux, cases),
2013-04-10 13:12:42 -04:00
If(demux == (n - 1),
2012-06-20 10:35:01 -04:00
demux.eq(0),
strobe_all.eq(1)
).Else(
demux.eq(demux + 1)
)
)
]