2011-12-22 13:37:16 -05:00
|
|
|
from migen.fhdl.structure import *
|
2012-01-15 09:09:44 -05:00
|
|
|
from migen.fhdl.structure import _make_signal_name
|
2012-01-09 08:21:45 -05:00
|
|
|
from migen.corelogic.misc import optree
|
2012-01-15 09:09:44 -05:00
|
|
|
from migen.corelogic.record import *
|
2011-12-22 13:37:16 -05:00
|
|
|
|
|
|
|
class SchedulingModel:
|
2011-12-22 18:35:53 -05:00
|
|
|
COMBINATORIAL, SEQUENTIAL, PIPELINE, DYNAMIC = range(4)
|
2011-12-22 13:37:16 -05:00
|
|
|
|
|
|
|
def __init__(self, model, latency=1):
|
|
|
|
self.model = model
|
|
|
|
self.latency = latency
|
|
|
|
|
2012-01-06 09:35:17 -05:00
|
|
|
def __repr__(self):
|
2011-12-22 13:37:16 -05:00
|
|
|
if self.model == SchedulingModel.COMBINATORIAL:
|
|
|
|
return "<SchedulingModel: COMBINATORIAL>"
|
|
|
|
elif self.model == SchedulingModel.SEQUENTIAL:
|
|
|
|
return "<SchedulingModel: SEQUENTIAL({0})>".format(self.latency)
|
|
|
|
elif self.model == SchedulingModel.PIPELINE:
|
|
|
|
return "<SchedulingModel: PIPELINE({0})>".format(self.latency)
|
|
|
|
elif self.model == SchedulingModel.DYNAMIC:
|
|
|
|
return "<SchedulingModel: DYNAMIC>"
|
|
|
|
else:
|
|
|
|
raise AttributeError
|
|
|
|
|
2012-01-06 09:35:17 -05:00
|
|
|
class Endpoint:
|
2012-01-06 18:33:28 -05:00
|
|
|
def __init__(self, token):
|
2011-12-22 13:37:16 -05:00
|
|
|
self.token = token
|
2012-01-07 15:21:46 -05:00
|
|
|
if isinstance(self, Sink):
|
|
|
|
self.stb = Signal(namer="stb_i")
|
|
|
|
self.ack = Signal(namer="ack_o")
|
|
|
|
else:
|
|
|
|
self.stb = Signal(namer="stb_o")
|
|
|
|
self.ack = Signal(namer="ack_i")
|
2012-01-08 07:56:11 -05:00
|
|
|
|
|
|
|
def token_signal(self):
|
|
|
|
sigs = self.token.flatten()
|
|
|
|
assert(len(sigs) == 1)
|
|
|
|
return sigs[0]
|
|
|
|
|
2012-01-06 18:33:28 -05:00
|
|
|
def __hash__(self):
|
|
|
|
return id(self)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Endpoint " + str(self.token) + ">"
|
|
|
|
|
|
|
|
|
|
|
|
class Sink(Endpoint):
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Sink " + str(self.token) + ">"
|
|
|
|
|
|
|
|
class Source(Endpoint):
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Source " + str(self.token) + ">"
|
2011-12-22 13:37:16 -05:00
|
|
|
|
2012-01-09 08:21:45 -05:00
|
|
|
def _control_fragment_comb(stb_i, ack_o, stb_o, ack_i, busy):
|
|
|
|
return Fragment([stb_o.eq(stb_i), ack_o.eq(ack_i), busy.eq(0)])
|
2011-12-22 13:37:16 -05:00
|
|
|
|
2012-01-09 08:21:45 -05:00
|
|
|
def _control_fragment_seq(latency, stb_i, ack_o, stb_o, ack_i, busy, trigger):
|
2011-12-22 13:37:16 -05:00
|
|
|
ready = Signal()
|
|
|
|
timer = Signal(BV(bits_for(latency)))
|
2011-12-22 18:35:53 -05:00
|
|
|
comb = [ready.eq(timer == 0)]
|
2011-12-22 13:37:16 -05:00
|
|
|
sync = [
|
|
|
|
If(trigger,
|
|
|
|
timer.eq(latency)
|
|
|
|
).Elif(~ready,
|
|
|
|
timer.eq(timer - 1)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2012-01-06 09:35:17 -05:00
|
|
|
mask = Signal()
|
|
|
|
comb += [
|
|
|
|
stb_o.eq(ready & mask),
|
|
|
|
trigger.eq(stb_i & (ack_i | ~mask) & ready),
|
|
|
|
ack_o.eq(trigger),
|
2012-01-09 08:21:45 -05:00
|
|
|
busy.eq(~ready)
|
2012-01-06 09:35:17 -05:00
|
|
|
]
|
|
|
|
sync += [
|
|
|
|
If(trigger, mask.eq(1)),
|
|
|
|
If(stb_o & ack_i, mask.eq(0))
|
|
|
|
]
|
2011-12-22 13:37:16 -05:00
|
|
|
|
|
|
|
return Fragment(comb, sync)
|
|
|
|
|
2012-01-09 08:21:45 -05:00
|
|
|
def _control_fragment_pipe(latency, stb_i, ack_o, stb_o, ack_i, busy, pipe_ce):
|
2011-12-22 13:37:16 -05:00
|
|
|
valid = Signal(BV(latency))
|
|
|
|
if latency > 1:
|
2012-01-06 09:35:17 -05:00
|
|
|
sync = [If(pipe_ce, valid.eq(Cat(stb_i, valid[:latency-1])))]
|
2011-12-22 13:37:16 -05:00
|
|
|
else:
|
2012-01-06 09:35:17 -05:00
|
|
|
sync = [If(pipe_ce, valid.eq(stb_i))]
|
2011-12-22 13:37:16 -05:00
|
|
|
last_valid = valid[latency-1]
|
|
|
|
|
2012-01-06 09:35:17 -05:00
|
|
|
comb = [
|
|
|
|
pipe_ce.eq(ack_i | ~last_valid),
|
|
|
|
ack_o.eq(pipe_ce),
|
2012-01-09 08:21:45 -05:00
|
|
|
stb_o.eq(last_valid),
|
|
|
|
busy.eq(optree('|', [valid[i] for i in range(latency)]))
|
2012-01-06 09:35:17 -05:00
|
|
|
]
|
2011-12-22 13:37:16 -05:00
|
|
|
|
|
|
|
return Fragment(comb, sync)
|
|
|
|
|
|
|
|
class Actor:
|
2012-01-15 09:09:44 -05:00
|
|
|
def __init__(self, scheduling_model, *endpoint_descriptions, endpoints=None):
|
2011-12-22 13:37:16 -05:00
|
|
|
self.scheduling_model = scheduling_model
|
2012-01-06 18:33:28 -05:00
|
|
|
if endpoints is None:
|
2012-01-15 09:09:44 -05:00
|
|
|
self.endpoints = {}
|
|
|
|
for desc in endpoint_descriptions:
|
|
|
|
# desc: (name, Sink/Source, token layout or existing record)
|
|
|
|
if isinstance(desc[2], Record):
|
|
|
|
token = desc[2]
|
|
|
|
else:
|
|
|
|
token = Record(desc[2], name=_make_signal_name(desc[0], 1))
|
|
|
|
ep = desc[1](token)
|
|
|
|
self.endpoints[desc[0]] = ep
|
2012-01-06 09:35:17 -05:00
|
|
|
else:
|
2012-01-06 18:33:28 -05:00
|
|
|
self.endpoints = endpoints
|
2012-01-09 08:21:45 -05:00
|
|
|
self.busy = Signal()
|
2011-12-22 18:35:53 -05:00
|
|
|
if self.scheduling_model.model == SchedulingModel.SEQUENTIAL:
|
2011-12-22 13:37:16 -05:00
|
|
|
self.trigger = Signal()
|
2011-12-22 18:35:53 -05:00
|
|
|
elif self.scheduling_model.model == SchedulingModel.PIPELINE:
|
2011-12-22 13:37:16 -05:00
|
|
|
self.pipe_ce = Signal()
|
|
|
|
|
2012-01-15 09:09:44 -05:00
|
|
|
def token(self, ep):
|
|
|
|
return self.endpoints[ep].token
|
|
|
|
|
|
|
|
def filter_endpoints(self, cl):
|
|
|
|
return [k for k, v in self.endpoints.items() if isinstance(v, cl)]
|
|
|
|
|
2012-01-06 18:33:28 -05:00
|
|
|
def sinks(self):
|
2012-01-15 09:09:44 -05:00
|
|
|
return self.filter_endpoints(Sink)
|
2012-01-06 18:33:28 -05:00
|
|
|
|
|
|
|
def sources(self):
|
2012-01-15 09:09:44 -05:00
|
|
|
return self.filter_endpoints(Source)
|
|
|
|
|
2011-12-22 18:35:53 -05:00
|
|
|
def get_control_fragment(self):
|
2012-01-15 09:09:44 -05:00
|
|
|
def get_single_ep(l):
|
|
|
|
if len(l) != 1:
|
|
|
|
raise ValueError("Actors with automatic control fragment must have exactly one sink and one source. Consider using plumbing actors.")
|
|
|
|
return self.endpoints[l[0]]
|
|
|
|
sink = get_single_ep(self.sinks())
|
|
|
|
source = get_single_ep(self.sources())
|
2012-01-06 18:33:28 -05:00
|
|
|
stb_i = sink.stb
|
|
|
|
ack_o = sink.ack
|
|
|
|
stb_o = source.stb
|
|
|
|
ack_i = source.ack
|
2011-12-22 18:35:53 -05:00
|
|
|
if self.scheduling_model.model == SchedulingModel.COMBINATORIAL:
|
2012-01-09 08:21:45 -05:00
|
|
|
return _control_fragment_comb(stb_i, ack_o, stb_o, ack_i, self.busy)
|
2011-12-22 18:35:53 -05:00
|
|
|
elif self.scheduling_model.model == SchedulingModel.SEQUENTIAL:
|
2012-01-09 08:21:45 -05:00
|
|
|
return _control_fragment_seq(self.scheduling_model.latency, stb_i, ack_o, stb_o, ack_i, self.busy, self.trigger)
|
2011-12-22 18:35:53 -05:00
|
|
|
elif self.scheduling_model.model == SchedulingModel.PIPELINE:
|
2012-01-09 08:21:45 -05:00
|
|
|
return _control_fragment_pipe(self.scheduling_model.latency, stb_i, ack_o, stb_o, ack_i, self.busy, self.pipe_ce)
|
2011-12-22 18:35:53 -05:00
|
|
|
elif self.scheduling_model.model == SchedulingModel.DYNAMIC:
|
2012-01-10 09:54:51 -05:00
|
|
|
raise NotImplementedError("Actor classes with dynamic scheduling must overload get_control_fragment or get_fragment")
|
2011-12-22 13:37:16 -05:00
|
|
|
|
2011-12-22 18:35:53 -05:00
|
|
|
def get_process_fragment(self):
|
2011-12-22 13:37:16 -05:00
|
|
|
raise NotImplementedError("Actor classes must overload get_process_fragment")
|
2012-01-06 18:33:28 -05:00
|
|
|
|
2012-01-10 09:54:51 -05:00
|
|
|
def get_fragment(self):
|
|
|
|
return self.get_control_fragment() + self.get_process_fragment()
|
|
|
|
|
2012-01-06 18:33:28 -05:00
|
|
|
def __repr__(self):
|
2012-01-09 08:21:54 -05:00
|
|
|
return "<" + self.__class__.__name__ + " " + repr(self.scheduling_model) + " " + repr(self.sinks()) + " " + repr(self.sources()) + ">"
|
2012-01-06 11:24:05 -05:00
|
|
|
|
2012-01-10 09:54:51 -05:00
|
|
|
def get_conn_fragment(source, sink):
|
2012-01-08 07:56:11 -05:00
|
|
|
assert isinstance(source, Source)
|
|
|
|
assert isinstance(sink, Sink)
|
|
|
|
assert sink.token.compatible(source.token)
|
2012-01-06 18:33:28 -05:00
|
|
|
sigs_source = source.token.flatten()
|
|
|
|
sigs_sink = sink.token.flatten()
|
2012-01-10 09:54:51 -05:00
|
|
|
comb = [
|
|
|
|
source.ack.eq(sink.ack),
|
|
|
|
sink.stb.eq(source.stb),
|
|
|
|
Cat(*sigs_sink).eq(Cat(*sigs_source))
|
|
|
|
]
|
2012-01-06 18:33:28 -05:00
|
|
|
return Fragment(comb)
|