litex/migen/flow/actor.py

115 lines
3.9 KiB
Python
Raw Normal View History

2011-12-22 13:37:16 -05:00
from migen.fhdl.structure import *
2011-12-22 18:35:53 -05:00
from migen.corelogic.misc import optree
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
def __str__(self):
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
class Sink:
def __init__(self, actor, token):
self.actor = actor
self.token = token
self.stb_i = Signal()
self.ack_o = Signal()
class Source:
def __init__(self, actor, token):
self.actor = actor
self.token = token
self.stb_o = Signal()
self.ack_i = Signal()
def _control_fragment_comb(l_stb_i, l_ack_o, l_stb_o, l_ack_i):
en = Signal()
comb = [en.eq(optree('&', l_stb_i + l_ack_i))]
comb += [o.eq(en) for o in l_ack_o + l_stb_o]
return Fragment(comb)
def _control_fragment_seq(latency, l_stb_i, l_ack_o, l_stb_o, l_ack_i, trigger):
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)
)
]
nsources = len(l_stb_o)
mask = Signal(BV(nsources))
sync.append(If(trigger, mask.eq(0)).Else(mask.eq(mask | Cat(*l_ack_i))))
2011-12-22 18:35:53 -05:00
comb.append(Cat(*l_stb_o).eq(Replicate(ready, len(l_stb_o)) & ~mask))
comb.append(Cat(*l_ack_o).eq(Replicate(trigger, len(l_ack_o))))
2011-12-22 13:37:16 -05:00
stb_all = Signal()
comb.append(stb_all.eq(optree('&', l_stb_i)))
2011-12-22 18:35:53 -05:00
comb.append(trigger.eq(ready & stb_all & ((mask | Cat(*l_ack_i)) == Replicate(1, nsources))))
2011-12-22 13:37:16 -05:00
return Fragment(comb, sync)
def _control_fragment_pipe(latency, l_stb_i, l_ack_o, l_stb_o, l_ack_i, pipe_ce):
stb_all = Signal()
comb = [stb_all.eq(optree('&', l_stb_i))]
valid = Signal(BV(latency))
if latency > 1:
2011-12-22 18:35:53 -05:00
sync = [If(pipe_ce, valid.eq(Cat(stb_all, valid[:latency-1])))]
2011-12-22 13:37:16 -05:00
else:
sync = [If(pipe_ce, valid.eq(stb_all))]
last_valid = valid[latency-1]
nsources = len(l_stb_o)
mask = Signal(BV(nsources))
sync.append(If(pipe_ce, mask.eq(0)).Else(mask.eq(mask | Cat(*l_ack_i))))
2011-12-22 18:35:53 -05:00
comb.append(Cat(*l_stb_o).eq(Replicate(last_valid, len(l_stb_o)) & ~mask))
comb.append(Cat(*l_ack_o).eq(Replicate(pipe_ce, len(l_ack_o))))
2011-12-22 13:37:16 -05:00
2011-12-22 18:35:53 -05:00
comb.append(pipe_ce.eq(~last_valid | ((mask | Cat(*l_ack_i)) == Replicate(1, nsources))))
2011-12-22 13:37:16 -05:00
return Fragment(comb, sync)
class Actor:
def __init__(self, scheduling_model, sinks, sources):
self.scheduling_model = scheduling_model
self.sinks = sinks
self.sources = sources
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()
2011-12-22 18:35:53 -05:00
def get_control_fragment(self):
2011-12-22 13:37:16 -05:00
l_stb_i = [e.stb_i for e in self.sinks]
l_ack_o = [e.ack_o for e in self.sinks]
l_stb_o = [e.stb_o for e in self.sources]
l_ack_i = [e.ack_i for e in self.sources]
2011-12-22 18:35:53 -05:00
if self.scheduling_model.model == SchedulingModel.COMBINATORIAL:
2011-12-22 13:37:16 -05:00
return _control_fragment_comb(l_stb_i, l_ack_o, l_stb_o, l_ack_i)
2011-12-22 18:35:53 -05:00
elif self.scheduling_model.model == SchedulingModel.SEQUENTIAL:
2011-12-22 13:37:16 -05:00
return _control_fragment_seq(self.scheduling_model.latency, l_stb_i, l_ack_o, l_stb_o, l_ack_i, self.trigger)
2011-12-22 18:35:53 -05:00
elif self.scheduling_model.model == SchedulingModel.PIPELINE:
2011-12-22 13:37:16 -05:00
return _control_fragment_pipe(self.scheduling_model.latency, l_stb_i, l_ack_o, l_stb_o, l_ack_i, self.pipe_ce)
2011-12-22 18:35:53 -05:00
elif self.scheduling_model.model == SchedulingModel.DYNAMIC:
2011-12-22 13:37:16 -05:00
raise NotImplementedError("Actor classes with dynamic scheduling must overload get_control_fragment")
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")