2012-06-20 10:35:01 -04:00
|
|
|
from migen.fhdl.structure import *
|
|
|
|
from migen.flow.actor import *
|
|
|
|
|
2012-06-29 10:10:50 -04:00
|
|
|
def _rawbits_layout(l):
|
|
|
|
if isinstance(l, int):
|
2012-11-29 15:22:38 -05:00
|
|
|
return [("rawbits", l)]
|
2012-06-29 10:10:50 -04:00
|
|
|
else:
|
|
|
|
return l
|
|
|
|
|
2012-06-20 10:35:01 -04:00
|
|
|
class Cast(CombinatorialActor):
|
|
|
|
def __init__(self, layout_from, layout_to):
|
|
|
|
super().__init__(
|
2012-06-29 10:10:50 -04:00
|
|
|
("sink", Sink, _rawbits_layout(layout_from)),
|
|
|
|
("source", Source, _rawbits_layout(layout_to)))
|
2012-06-20 10:35:01 -04:00
|
|
|
|
|
|
|
def get_process_fragment(self):
|
|
|
|
sigs_from = self.token("sink").flatten()
|
|
|
|
sigs_to = self.token("source").flatten()
|
|
|
|
if sum(len(s) for s in sigs_from) != sum(len(s) for s in sigs_to):
|
|
|
|
raise TypeError
|
|
|
|
return Fragment([
|
|
|
|
Cat(*sigs_to).eq(Cat(*sigs_from))
|
|
|
|
])
|
|
|
|
|
2012-06-20 12:25:01 -04:00
|
|
|
def pack_layout(l, n):
|
|
|
|
return [("chunk{0}".format(i), l) for i in range(n)]
|
|
|
|
|
|
|
|
class Unpack(Actor):
|
2012-06-20 10:35:01 -04:00
|
|
|
def __init__(self, n, layout_to):
|
|
|
|
self.n = n
|
|
|
|
super().__init__(
|
2012-06-20 12:25:01 -04:00
|
|
|
("sink", Sink, pack_layout(layout_to, n)),
|
2012-06-20 10:35:01 -04:00
|
|
|
("source", Source, layout_to))
|
|
|
|
|
|
|
|
def get_fragment(self):
|
2012-11-29 15:53:36 -05:00
|
|
|
mux = Signal(max=self.n)
|
2012-06-20 10:35:01 -04:00
|
|
|
last = Signal()
|
|
|
|
comb = [
|
|
|
|
last.eq(mux == (self.n-1)),
|
|
|
|
self.endpoints["source"].stb.eq(self.endpoints["sink"].stb),
|
|
|
|
self.endpoints["sink"].ack.eq(last & self.endpoints["source"].ack)
|
|
|
|
]
|
|
|
|
sync = [
|
|
|
|
If(self.endpoints["source"].stb & self.endpoints["source"].ack,
|
|
|
|
If(last,
|
|
|
|
mux.eq(0)
|
|
|
|
).Else(
|
|
|
|
mux.eq(mux + 1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
2012-11-28 19:11:15 -05:00
|
|
|
cases = {}
|
|
|
|
for i in range(self.n):
|
|
|
|
cases[i] = [Cat(*self.token("source").flatten()).eq(Cat(*self.token("sink").subrecord("chunk{0}".format(i)).flatten()))]
|
|
|
|
comb.append(Case(mux, cases).makedefault())
|
2012-06-20 10:35:01 -04:00
|
|
|
return Fragment(comb, sync)
|
|
|
|
|
|
|
|
class Pack(Actor):
|
|
|
|
def __init__(self, layout_from, n):
|
|
|
|
self.n = n
|
|
|
|
super().__init__(
|
|
|
|
("sink", Sink, layout_from),
|
2012-06-20 12:25:01 -04:00
|
|
|
("source", Source, pack_layout(layout_from, n)))
|
2012-06-20 10:35:01 -04:00
|
|
|
|
|
|
|
def get_fragment(self):
|
2012-11-29 15:53:36 -05:00
|
|
|
demux = Signal(max=self.n)
|
2012-06-20 10:35:01 -04:00
|
|
|
|
|
|
|
load_part = Signal()
|
|
|
|
strobe_all = Signal()
|
2012-11-28 19:11:15 -05:00
|
|
|
cases = {}
|
|
|
|
for i in range(self.n):
|
|
|
|
cases[i] = [Cat(*self.token("source").subrecord("chunk{0}".format(i)).flatten()).eq(*self.token("sink").flatten())]
|
2012-06-20 10:35:01 -04:00
|
|
|
comb = [
|
2012-06-20 16:39:03 -04:00
|
|
|
self.busy.eq(strobe_all),
|
2012-06-20 10:35:01 -04:00
|
|
|
self.endpoints["sink"].ack.eq(~strobe_all | self.endpoints["source"].ack),
|
|
|
|
self.endpoints["source"].stb.eq(strobe_all),
|
|
|
|
load_part.eq(self.endpoints["sink"].stb & self.endpoints["sink"].ack)
|
|
|
|
]
|
|
|
|
sync = [
|
|
|
|
If(self.endpoints["source"].ack,
|
|
|
|
strobe_all.eq(0)
|
|
|
|
),
|
|
|
|
If(load_part,
|
2012-11-28 19:11:15 -05:00
|
|
|
Case(demux, cases),
|
2012-06-20 10:35:01 -04:00
|
|
|
If(demux == (self.n - 1),
|
|
|
|
demux.eq(0),
|
|
|
|
strobe_all.eq(1)
|
|
|
|
).Else(
|
|
|
|
demux.eq(demux + 1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
return Fragment(comb, sync)
|