2015-03-20 17:10:41 -04:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from migen.fhdl.std import *
|
|
|
|
from migen.flow.actor import *
|
|
|
|
from migen.flow.transactions import *
|
|
|
|
from migen.flow.network import *
|
|
|
|
from migen.actorlib.sim import *
|
|
|
|
|
|
|
|
from migen.test.support import SimCase, SimBench
|
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-20 17:10:41 -04:00
|
|
|
def source_gen(sent):
|
2015-04-13 14:07:07 -04:00
|
|
|
for i in range(10):
|
|
|
|
yield Token("source", {"value": i})
|
|
|
|
sent.append(i)
|
2015-03-20 17:10:41 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-20 17:10:41 -04:00
|
|
|
class SimSource(SimActor):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.source = Source([("value", 32)])
|
|
|
|
self.sent = []
|
|
|
|
SimActor.__init__(self, source_gen(self.sent))
|
2015-03-20 17:10:41 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-20 17:10:41 -04:00
|
|
|
def sink_gen(received):
|
2015-04-13 14:07:07 -04:00
|
|
|
while True:
|
|
|
|
t = Token("sink")
|
|
|
|
yield t
|
|
|
|
received.append(t.value["value"])
|
2015-03-20 17:10:41 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-20 17:10:41 -04:00
|
|
|
class SimSink(SimActor):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.sink = Sink([("value", 32)])
|
|
|
|
self.received = []
|
|
|
|
SimActor.__init__(self, sink_gen(self.received))
|
2015-03-20 17:10:41 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-20 17:10:41 -04:00
|
|
|
class SourceSinkCase(SimCase, unittest.TestCase):
|
2015-04-13 14:07:07 -04:00
|
|
|
class TestBench(SimBench):
|
|
|
|
def __init__(self):
|
|
|
|
self.source = SimSource()
|
|
|
|
self.sink = SimSink()
|
|
|
|
g = DataFlowGraph()
|
|
|
|
g.add_connection(self.source, self.sink)
|
|
|
|
self.submodules.comp = CompositeActor(g)
|
2015-03-20 17:10:41 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def do_simulation(self, selfp):
|
|
|
|
if self.source.token_exchanger.done:
|
|
|
|
raise StopSimulation
|
2015-03-20 17:10:41 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def test_equal(self):
|
|
|
|
self.run_with(lambda tb, tbp: None)
|
|
|
|
self.assertEqual(self.tb.source.sent, self.tb.sink.received)
|
2015-03-20 17:10:41 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-20 17:10:41 -04:00
|
|
|
class SourceSinkDirectCase(SimCase, unittest.TestCase):
|
2015-04-13 14:07:07 -04:00
|
|
|
class TestBench(SimBench):
|
|
|
|
def __init__(self):
|
|
|
|
self.source = SimSource()
|
|
|
|
self.sink = SimSink()
|
|
|
|
self.submodules += self.source, self.sink
|
|
|
|
self.comb += self.sink.sink.connect(self.source.source)
|
2015-03-20 17:10:41 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def do_simulation(self, selfp):
|
|
|
|
if self.source.token_exchanger.done:
|
|
|
|
raise StopSimulation
|
2015-03-20 17:10:41 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def test_equal(self):
|
|
|
|
self.run_with(lambda tb, tbp: None)
|
|
|
|
self.assertEqual(self.tb.source.sent, self.tb.sink.received)
|