litex/migen/flow/actor.py

113 lines
2.9 KiB
Python
Raw Normal View History

from migen.util.misc import xdir
from migen.fhdl.std import *
2013-02-22 17:19:37 -05:00
from migen.genlib.misc import optree
from migen.genlib.record import *
2011-12-22 13:37:16 -05:00
2013-04-10 13:12:42 -04:00
def _make_m2s(layout):
r = []
for f in layout:
if isinstance(f[1], (int, tuple)):
r.append((f[0], f[1], DIR_M_TO_S))
else:
2013-04-10 13:12:42 -04:00
r.append((f[0], _make_m2s(f[1])))
return r
class _Endpoint(Record):
def __init__(self, layout, packetized=False):
endpoint_layout = [
2013-04-10 13:12:42 -04:00
("payload", _make_m2s(layout)),
("stb", 1, DIR_M_TO_S),
("ack", 1, DIR_S_TO_M)
]
if packetized:
endpoint_layout += [
("sop", 1, DIR_M_TO_S),
2014-09-22 12:09:30 -04:00
("eop", 1, DIR_M_TO_S)
]
Record.__init__(self, endpoint_layout)
2013-04-10 13:12:42 -04:00
class Source(_Endpoint):
def connect(self, sink):
return Record.connect(self, sink)
2013-04-10 13:12:42 -04:00
class Sink(_Endpoint):
def connect(self, source):
return source.connect(self)
2013-04-10 13:12:42 -04:00
def get_endpoints(obj, filt=_Endpoint):
if hasattr(obj, "get_endpoints") and callable(obj.get_endpoints):
return obj.get_endpoints(filt)
r = dict()
for k, v in xdir(obj, True):
2013-04-10 13:12:42 -04:00
if isinstance(v, filt):
r[k] = v
return r
def get_single_ep(obj, filt):
eps = get_endpoints(obj, filt)
if len(eps) != 1:
raise ValueError("More than one endpoint")
return list(eps.items())[0]
class BinaryActor(Module):
def __init__(self, *args, **kwargs):
2012-01-09 08:21:45 -05:00
self.busy = Signal()
2013-04-10 13:12:42 -04:00
sink = get_single_ep(self, Sink)[1]
source = get_single_ep(self, Source)[1]
self.build_binary_control(sink.stb, sink.ack, source.stb, source.ack, *args, **kwargs)
2012-06-07 08:44:43 -04:00
2013-04-10 13:12:42 -04:00
def build_binary_control(self, stb_i, ack_o, stb_o, ack_i):
raise NotImplementedError("Binary actor classes must overload build_binary_control_fragment")
2012-06-07 08:44:43 -04:00
class CombinatorialActor(BinaryActor):
2013-04-10 13:12:42 -04:00
def build_binary_control(self, stb_i, ack_o, stb_o, ack_i):
self.comb += [stb_o.eq(stb_i), ack_o.eq(ack_i), self.busy.eq(0)]
2012-06-07 08:44:43 -04:00
class SequentialActor(BinaryActor):
2013-04-10 13:12:42 -04:00
def __init__(self, delay):
2012-06-07 08:44:43 -04:00
self.trigger = Signal()
2013-04-10 13:12:42 -04:00
BinaryActor.__init__(self, delay)
2012-06-07 08:44:43 -04:00
2013-04-10 13:12:42 -04:00
def build_binary_control(self, stb_i, ack_o, stb_o, ack_i, delay):
2012-06-07 08:44:43 -04:00
ready = Signal()
2013-04-10 13:12:42 -04:00
timer = Signal(max=delay+1)
self.comb += ready.eq(timer == 0)
self.sync += If(self.trigger,
timer.eq(delay)
2012-06-07 08:44:43 -04:00
).Elif(~ready,
timer.eq(timer - 1)
)
2012-06-07 08:44:43 -04:00
mask = Signal()
2013-04-10 13:12:42 -04:00
self.comb += [
2012-06-07 08:44:43 -04:00
stb_o.eq(ready & mask),
self.trigger.eq(stb_i & (ack_i | ~mask) & ready),
ack_o.eq(self.trigger),
2013-04-10 13:12:42 -04:00
self.busy.eq(~ready)
2012-06-07 08:44:43 -04:00
]
2013-04-10 13:12:42 -04:00
self.sync += [
2012-06-07 08:44:43 -04:00
If(self.trigger, mask.eq(1)),
If(stb_o & ack_i, mask.eq(0))
]
class PipelinedActor(BinaryActor):
2013-04-10 13:12:42 -04:00
def __init__(self, latency):
2012-06-07 08:44:43 -04:00
self.pipe_ce = Signal()
2013-04-10 13:12:42 -04:00
BinaryActor.__init__(self, latency)
2012-06-07 08:44:43 -04:00
2013-04-10 13:12:42 -04:00
def build_binary_control(self, stb_i, ack_o, stb_o, ack_i, latency):
2013-07-12 12:52:34 -04:00
busy = 0
valid = stb_i
for i in range(latency):
valid_n = Signal()
self.sync += If(self.pipe_ce, valid_n.eq(valid))
valid = valid_n
busy = busy | valid
2013-04-10 13:12:42 -04:00
self.comb += [
2013-07-12 12:52:34 -04:00
self.pipe_ce.eq(ack_i | ~valid),
2012-06-07 08:44:43 -04:00
ack_o.eq(self.pipe_ce),
2013-07-12 12:52:34 -04:00
stb_o.eq(valid),
self.busy.eq(busy)
2012-06-07 08:44:43 -04:00
]