2012-06-20 18:44:41 -04:00
|
|
|
from itertools import count
|
|
|
|
|
2012-06-20 15:59:17 -04:00
|
|
|
import networkx as nx
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
2012-06-20 12:25:01 -04:00
|
|
|
from migen.flow.network import *
|
2012-12-14 09:55:38 -05:00
|
|
|
from migen.flow.transactions import *
|
2012-06-20 12:25:01 -04:00
|
|
|
from migen.actorlib import structuring
|
|
|
|
from migen.actorlib.sim import *
|
2012-06-20 15:59:17 -04:00
|
|
|
from migen.flow import perftools
|
2014-01-26 16:19:43 -05:00
|
|
|
from migen.sim.generic import run_simulation
|
2012-06-20 12:25:01 -04:00
|
|
|
|
|
|
|
pack_factor = 5
|
2013-04-10 13:12:42 -04:00
|
|
|
base_layout = [("value", 32)]
|
|
|
|
packed_layout = structuring.pack_layout(base_layout, pack_factor)
|
|
|
|
rawbits_layout = [("value", 32*pack_factor)]
|
2012-06-20 12:25:01 -04:00
|
|
|
|
|
|
|
def source_gen():
|
2012-06-20 18:44:41 -04:00
|
|
|
for i in count(0):
|
2012-06-20 12:25:01 -04:00
|
|
|
yield Token("source", {"value": i})
|
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
class SimSource(SimActor):
|
|
|
|
def __init__(self):
|
|
|
|
self.source = Source(base_layout)
|
|
|
|
SimActor.__init__(self, source_gen())
|
|
|
|
|
2012-06-20 12:25:01 -04:00
|
|
|
def sink_gen():
|
|
|
|
while True:
|
|
|
|
t = Token("sink")
|
|
|
|
yield t
|
|
|
|
print(t.value["value"])
|
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
class SimSink(SimActor):
|
|
|
|
def __init__(self):
|
|
|
|
self.sink = Sink(base_layout)
|
|
|
|
SimActor.__init__(self, sink_gen())
|
|
|
|
|
|
|
|
class TB(Module):
|
|
|
|
def __init__(self):
|
|
|
|
source = SimSource()
|
|
|
|
sink = SimSink()
|
2014-10-17 05:08:37 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
# A tortuous way of passing integer tokens.
|
|
|
|
packer = structuring.Pack(base_layout, pack_factor)
|
|
|
|
to_raw = structuring.Cast(packed_layout, rawbits_layout)
|
|
|
|
from_raw = structuring.Cast(rawbits_layout, packed_layout)
|
|
|
|
unpacker = structuring.Unpack(pack_factor, base_layout)
|
2014-10-17 05:08:37 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
self.g = DataFlowGraph()
|
|
|
|
self.g.add_connection(source, packer)
|
|
|
|
self.g.add_connection(packer, to_raw)
|
|
|
|
self.g.add_connection(to_raw, from_raw)
|
|
|
|
self.g.add_connection(from_raw, unpacker)
|
|
|
|
self.g.add_connection(unpacker, sink)
|
|
|
|
self.submodules.comp = CompositeActor(self.g)
|
|
|
|
self.submodules.reporter = perftools.DFGReporter(self.g)
|
|
|
|
|
2014-01-26 16:19:43 -05:00
|
|
|
if __name__ == "__main__":
|
2013-04-10 13:12:42 -04:00
|
|
|
tb = TB()
|
2014-01-26 16:19:43 -05:00
|
|
|
run_simulation(tb, ncycles=1000)
|
2014-10-17 05:08:37 -04:00
|
|
|
|
2014-09-07 02:09:53 -04:00
|
|
|
g = nx.MultiDiGraph()
|
|
|
|
for u, v, edge in tb.g.edges_iter():
|
|
|
|
g.add_edge(u, v, **edge)
|
|
|
|
g_layout = nx.spectral_layout(g)
|
|
|
|
nx.draw(g, g_layout)
|
|
|
|
nx.draw_networkx_edge_labels(g, g_layout, tb.reporter.get_edge_labels())
|
2012-06-20 15:59:17 -04:00
|
|
|
plt.show()
|