litex/migen/actorlib/structuring.py

100 lines
2.7 KiB
Python
Raw Normal View History

2012-06-20 10:35:01 -04:00
from migen.fhdl.structure import *
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):
self.reverse_from = reverse_from
self.reverse_to = reverse_to
2012-12-18 08:54:33 -05:00
CombinatorialActor.__init__(self,
("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()
if self.reverse_from:
sigs_from = list(reversed(sigs_from))
2012-06-20 10:35:01 -04:00
sigs_to = self.token("source").flatten()
if self.reverse_to:
sigs_to = list(reversed(sigs_to))
2012-06-20 10:35:01 -04:00
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
2012-12-18 08:54:33 -05:00
Actor.__init__(self,
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):
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
2012-12-18 08:54:33 -05:00
Actor.__init__(self,
2012-06-20 10:35:01 -04:00
("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):
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 = [
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)