2013-11-02 11:03:47 -04:00
|
|
|
from migen.util.misc import xdir
|
2013-05-22 11:11:09 -04:00
|
|
|
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
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
def _make_m2s(layout):
|
2015-04-13 14:07:07 -04:00
|
|
|
r = []
|
|
|
|
for f in layout:
|
|
|
|
if isinstance(f[1], (int, tuple)):
|
|
|
|
r.append((f[0], f[1], DIR_M_TO_S))
|
|
|
|
else:
|
|
|
|
r.append((f[0], _make_m2s(f[1])))
|
|
|
|
return r
|
2013-04-10 13:12:42 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2014-11-21 01:31:56 -05:00
|
|
|
class EndpointDescription:
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, payload_layout, param_layout=[], packetized=False):
|
|
|
|
self.payload_layout = payload_layout
|
|
|
|
self.param_layout = param_layout
|
|
|
|
self.packetized = packetized
|
|
|
|
|
|
|
|
def get_full_layout(self):
|
|
|
|
reserved = {"stb", "ack", "payload", "param", "sop", "eop", "description"}
|
|
|
|
attributed = set()
|
|
|
|
for f in self.payload_layout + self.param_layout:
|
|
|
|
if f[0] in attributed:
|
|
|
|
raise ValueError(f[0] + " already attributed in payload or param layout")
|
|
|
|
if f[0] in reserved:
|
|
|
|
raise ValueError(f[0] + " cannot be used in endpoint layout")
|
|
|
|
attributed.add(f[0])
|
|
|
|
|
|
|
|
full_layout = [
|
|
|
|
("payload", _make_m2s(self.payload_layout)),
|
|
|
|
("param", _make_m2s(self.param_layout)),
|
|
|
|
("stb", 1, DIR_M_TO_S),
|
|
|
|
("ack", 1, DIR_S_TO_M)
|
|
|
|
]
|
|
|
|
if self.packetized:
|
|
|
|
full_layout += [
|
|
|
|
("sop", 1, DIR_M_TO_S),
|
|
|
|
("eop", 1, DIR_M_TO_S)
|
|
|
|
]
|
|
|
|
return full_layout
|
2014-11-21 01:31:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
class _Endpoint(Record):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, description_or_layout):
|
|
|
|
if isinstance(description_or_layout, EndpointDescription):
|
|
|
|
self.description = description_or_layout
|
|
|
|
else:
|
|
|
|
self.description = EndpointDescription(description_or_layout)
|
|
|
|
Record.__init__(self, self.description.get_full_layout())
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
try:
|
|
|
|
return getattr(object.__getattribute__(self, "payload"), name)
|
|
|
|
except:
|
|
|
|
return getattr(object.__getattribute__(self, "param"), name)
|
2014-10-20 02:47:48 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
class Source(_Endpoint):
|
2015-04-13 14:07:07 -04:00
|
|
|
def connect(self, sink):
|
|
|
|
return Record.connect(self, sink)
|
2013-04-10 13:12:42 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
class Sink(_Endpoint):
|
2015-04-13 14:07:07 -04:00
|
|
|
def connect(self, source):
|
|
|
|
return source.connect(self)
|
2013-04-10 13:12:42 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
def get_endpoints(obj, filt=_Endpoint):
|
2015-04-13 14:07:07 -04:00
|
|
|
if hasattr(obj, "get_endpoints") and callable(obj.get_endpoints):
|
|
|
|
return obj.get_endpoints(filt)
|
|
|
|
r = dict()
|
|
|
|
for k, v in xdir(obj, True):
|
|
|
|
if isinstance(v, filt):
|
|
|
|
r[k] = v
|
|
|
|
return r
|
2013-04-10 13:12:42 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
def get_single_ep(obj, filt):
|
2015-04-13 14:07:07 -04:00
|
|
|
eps = get_endpoints(obj, filt)
|
|
|
|
if len(eps) != 1:
|
|
|
|
raise ValueError("More than one endpoint")
|
|
|
|
return list(eps.items())[0]
|
2013-04-10 13:12:42 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
class BinaryActor(Module):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.busy = Signal()
|
|
|
|
sink = get_single_ep(self, Sink)[1]
|
|
|
|
source = get_single_ep(self, Source)[1]
|
|
|
|
self.build_binary_control(sink, source, *args, **kwargs)
|
2012-06-07 08:44:43 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def build_binary_control(self, sink, source):
|
|
|
|
raise NotImplementedError("Binary actor classes must overload build_binary_control_fragment")
|
2012-06-07 08:44:43 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-06-07 08:44:43 -04:00
|
|
|
class CombinatorialActor(BinaryActor):
|
2015-04-13 14:07:07 -04:00
|
|
|
def build_binary_control(self, sink, source):
|
|
|
|
self.comb += [
|
|
|
|
source.stb.eq(sink.stb),
|
|
|
|
sink.ack.eq(source.ack),
|
|
|
|
self.busy.eq(0)
|
|
|
|
]
|
|
|
|
if sink.description.packetized:
|
|
|
|
self.comb += [
|
|
|
|
source.sop.eq(sink.sop),
|
|
|
|
source.eop.eq(sink.eop)
|
|
|
|
]
|
2012-06-07 08:44:43 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-06-07 08:44:43 -04:00
|
|
|
class SequentialActor(BinaryActor):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, delay):
|
|
|
|
self.trigger = Signal()
|
|
|
|
BinaryActor.__init__(self, delay)
|
|
|
|
|
|
|
|
def build_binary_control(self, sink, source, delay):
|
|
|
|
ready = Signal()
|
|
|
|
timer = Signal(max=delay+1)
|
|
|
|
self.comb += ready.eq(timer == 0)
|
|
|
|
self.sync += If(self.trigger,
|
|
|
|
timer.eq(delay)
|
|
|
|
).Elif(~ready,
|
|
|
|
timer.eq(timer - 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
mask = Signal()
|
|
|
|
self.comb += [
|
|
|
|
source.stb.eq(ready & mask),
|
|
|
|
self.trigger.eq(sink.stb & (source.ack | ~mask) & ready),
|
|
|
|
sink.ack.eq(self.trigger),
|
|
|
|
self.busy.eq(~ready)
|
|
|
|
]
|
|
|
|
self.sync += [
|
|
|
|
If(self.trigger, mask.eq(1)),
|
|
|
|
If(source.stb & source.ack, mask.eq(0))
|
|
|
|
]
|
|
|
|
if sink.packetized:
|
|
|
|
self.comb += [
|
|
|
|
source.sop.eq(sink.sop),
|
|
|
|
source.eop.eq(sink.eop)
|
|
|
|
]
|
2012-06-07 08:44:43 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-06-07 08:44:43 -04:00
|
|
|
class PipelinedActor(BinaryActor):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, latency):
|
|
|
|
self.pipe_ce = Signal()
|
|
|
|
BinaryActor.__init__(self, latency)
|
|
|
|
|
|
|
|
def build_binary_control(self, sink, source, latency):
|
|
|
|
busy = 0
|
|
|
|
valid = sink.stb
|
|
|
|
for i in range(latency):
|
|
|
|
valid_n = Signal()
|
|
|
|
self.sync += If(self.pipe_ce, valid_n.eq(valid))
|
|
|
|
valid = valid_n
|
|
|
|
busy = busy | valid
|
|
|
|
|
|
|
|
self.comb += [
|
|
|
|
self.pipe_ce.eq(source.ack | ~valid),
|
|
|
|
sink.ack.eq(self.pipe_ce),
|
|
|
|
source.stb.eq(valid),
|
|
|
|
self.busy.eq(busy)
|
|
|
|
]
|
|
|
|
if sink.description.packetized:
|
2015-08-09 13:53:50 -04:00
|
|
|
sop = sink.stb & sink.sop
|
|
|
|
eop = sink.stb & sink.eop
|
2015-04-13 14:07:07 -04:00
|
|
|
for i in range(latency):
|
|
|
|
sop_n = Signal()
|
|
|
|
eop_n = Signal()
|
|
|
|
self.sync += \
|
|
|
|
If(self.pipe_ce,
|
|
|
|
sop_n.eq(sop),
|
|
|
|
eop_n.eq(eop)
|
|
|
|
)
|
|
|
|
sop = sop_n
|
|
|
|
eop = eop_n
|
|
|
|
|
|
|
|
self.comb += [
|
|
|
|
source.eop.eq(eop),
|
|
|
|
source.sop.eq(sop)
|
|
|
|
]
|