2012-06-08 11:54:03 -04:00
|
|
|
from migen.fhdl.structure import *
|
|
|
|
from migen.flow.actor import *
|
2012-06-12 11:08:56 -04:00
|
|
|
from migen.sim.generic import PureSimulable
|
2012-06-08 11:54:03 -04:00
|
|
|
|
|
|
|
class Token:
|
|
|
|
def __init__(self, endpoint, value=None):
|
|
|
|
self.endpoint = endpoint
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
# 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.
|
2012-06-12 11:52:08 -04:00
|
|
|
class SimActor(PureSimulable, Actor):
|
2012-06-08 11:54:03 -04:00
|
|
|
def __init__(self, generator, *endpoint_descriptions, **misc):
|
|
|
|
self.generator = generator
|
2012-06-08 11:56:52 -04:00
|
|
|
self.active = set()
|
2012-06-08 11:54:03 -04:00
|
|
|
self.done = False
|
|
|
|
super().__init__(*endpoint_descriptions, **misc)
|
|
|
|
|
|
|
|
def _process_transactions(self, s):
|
2012-06-08 11:56:52 -04:00
|
|
|
completed = set()
|
2012-06-08 11:54:03 -04:00
|
|
|
for token in self.active:
|
|
|
|
ep = self.endpoints[token.endpoint]
|
|
|
|
if isinstance(ep, Sink):
|
|
|
|
if s.rd(ep.ack):
|
|
|
|
if s.rd(ep.stb):
|
|
|
|
token.value = s.multiread(ep.token)
|
2012-06-08 11:56:52 -04:00
|
|
|
completed.add(token)
|
2012-06-08 11:54:03 -04:00
|
|
|
s.wr(ep.ack, 0)
|
|
|
|
else:
|
|
|
|
s.wr(ep.ack, 1)
|
|
|
|
elif isinstance(ep, Source):
|
|
|
|
if s.rd(ep.stb):
|
|
|
|
if s.rd(ep.ack):
|
2012-06-08 11:56:52 -04:00
|
|
|
completed.add(token)
|
2012-06-08 11:54:03 -04:00
|
|
|
s.wr(ep.stb, 0)
|
|
|
|
else:
|
|
|
|
s.wr(ep.stb, 1)
|
|
|
|
s.multiwrite(ep.token, token.value)
|
|
|
|
else:
|
|
|
|
raise TypeError
|
2012-06-08 11:56:52 -04:00
|
|
|
self.active -= completed
|
2012-06-08 11:54:03 -04:00
|
|
|
|
|
|
|
def _next_transactions(self):
|
|
|
|
try:
|
|
|
|
transactions = next(self.generator)
|
|
|
|
except StopIteration:
|
|
|
|
self.done = True
|
|
|
|
transactions = None
|
|
|
|
if isinstance(transactions, Token):
|
2012-06-08 11:56:52 -04:00
|
|
|
self.active = {transactions}
|
|
|
|
elif isinstance(transactions, tuple) \
|
|
|
|
or isinstance(transactions, list) \
|
|
|
|
or isinstance(transactions, set):
|
|
|
|
self.active = set(transactions)
|
2012-06-08 11:54:03 -04:00
|
|
|
elif transactions is None:
|
2012-06-20 16:39:52 -04:00
|
|
|
self.active = set()
|
2012-06-08 11:54:03 -04:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
|
|
|
|
def do_simulation(self, s):
|
|
|
|
if not self.done:
|
2012-06-20 16:39:52 -04:00
|
|
|
if not self.active:
|
|
|
|
self._next_transactions()
|
2012-06-08 11:54:03 -04:00
|
|
|
if self.active:
|
|
|
|
self._process_transactions(s)
|