litex/migen/actorlib/sim.py

117 lines
3.2 KiB
Python
Raw Normal View History

from migen.fhdl.std import *
from migen.flow.actor import *
2012-12-14 09:55:38 -05:00
from migen.flow.transactions import *
2014-01-26 16:19:43 -05:00
from migen.util.misc import xdir
def _sim_multiread(sim, obj):
if isinstance(obj, Signal):
return sim.rd(obj)
else:
r = {}
for k, v in xdir(obj, True):
rd = _sim_multiread(sim, v)
if isinstance(rd, int) or rd:
r[k] = rd
return r
def _sim_multiwrite(sim, obj, value):
if isinstance(obj, Signal):
sim.wr(obj, value)
else:
for k, v in value.items():
_sim_multiwrite(sim, getattr(obj, k), v)
# Generators yield None or a tuple of Tokens.
# Tokens for Sink endpoints are pulled and the "value" field filled in.
# Tokens for Source endpoints are pushed according to their "value" field.
#
# NB: the possibility to push several tokens at once is important to interact
# with actors that only accept a group of tokens when all of them are available.
class TokenExchanger(Module):
def __init__(self, generator, actor):
self.generator = generator
self.actor = actor
self.active = set()
2012-12-14 13:16:22 -05:00
self.busy = True
2014-01-27 18:03:56 -05:00
self.done = False
2014-01-26 16:19:43 -05:00
def _process_transactions(self, selfp):
completed = set()
for token in self.active:
2013-04-10 13:12:42 -04:00
ep = getattr(self.actor, token.endpoint)
if isinstance(ep, Sink):
2014-01-26 16:19:43 -05:00
if selfp.simulator.rd(ep.ack) and selfp.simulator.rd(ep.stb):
token.value = _sim_multiread(selfp.simulator, ep.payload)
completed.add(token)
2014-01-26 16:19:43 -05:00
selfp.simulator.wr(ep.ack, 0)
elif isinstance(ep, Source):
2014-01-26 16:19:43 -05:00
if selfp.simulator.rd(ep.ack) and selfp.simulator.rd(ep.stb):
completed.add(token)
2014-01-26 16:19:43 -05:00
selfp.simulator.wr(ep.stb, 0)
else:
raise TypeError
self.active -= completed
2012-12-14 13:16:22 -05:00
if not self.active:
self.busy = True
2014-01-26 16:19:43 -05:00
def _update_control_signals(self, selfp):
for token in self.active:
2013-04-10 13:12:42 -04:00
ep = getattr(self.actor, token.endpoint)
if isinstance(ep, Sink):
2014-01-26 16:19:43 -05:00
selfp.simulator.wr(ep.ack, 1)
elif isinstance(ep, Source):
2014-01-26 16:19:43 -05:00
_sim_multiwrite(selfp.simulator, ep.payload, token.value)
selfp.simulator.wr(ep.stb, 1)
else:
raise TypeError
def _next_transactions(self):
try:
transactions = next(self.generator)
except StopIteration:
2012-12-14 13:16:22 -05:00
self.busy = False
2014-01-27 18:03:56 -05:00
self.done = True
2014-01-26 16:19:43 -05:00
raise StopSimulation
if isinstance(transactions, Token):
self.active = {transactions}
2013-04-10 13:12:42 -04:00
elif isinstance(transactions, (tuple, list, set)):
self.active = set(transactions)
elif transactions is None:
self.active = set()
else:
raise TypeError
if self.active and all(transaction.idle_wait for transaction in self.active):
2012-12-14 13:16:22 -05:00
self.busy = False
2014-01-26 16:19:43 -05:00
def do_simulation(self, selfp):
if self.active:
self._process_transactions(selfp)
if not self.active:
self._next_transactions()
self._update_control_signals(selfp)
2014-01-27 18:03:56 -05:00
do_simulation.passive = True
2012-11-17 08:15:51 -05:00
2013-04-10 13:12:42 -04:00
class SimActor(Module):
def __init__(self, generator):
self.busy = Signal()
self.submodules.token_exchanger = TokenExchanger(generator, self)
2014-10-17 05:08:37 -04:00
2014-01-26 16:19:43 -05:00
def do_simulation(self, selfp):
selfp.busy = self.token_exchanger.busy
2014-01-27 18:03:56 -05:00
do_simulation.passive = True
2013-04-10 13:12:42 -04:00
def _dumper_gen(prefix):
while True:
t = Token("result")
yield t
if len(t.value) > 1:
s = str(t.value)
else:
s = str(list(t.value.values())[0])
print(prefix + s)
2012-11-23 18:00:07 -05:00
class Dumper(SimActor):
def __init__(self, layout, prefix=""):
2013-04-10 13:12:42 -04:00
self.result = Sink(layout)
SimActor.__init__(self, _dumper_gen(prefix))