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 *
|
|
|
|
from migen.sim.generic import Simulator
|
|
|
|
from migen.sim.icarus import Runner
|
2012-06-20 15:59:17 -04:00
|
|
|
from migen.flow import perftools
|
2012-06-20 12:25:01 -04:00
|
|
|
|
|
|
|
pack_factor = 5
|
|
|
|
|
|
|
|
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})
|
|
|
|
|
|
|
|
def sink_gen():
|
|
|
|
while True:
|
|
|
|
t = Token("sink")
|
|
|
|
yield t
|
|
|
|
print(t.value["value"])
|
|
|
|
|
|
|
|
def main():
|
2012-11-29 15:22:38 -05:00
|
|
|
base_layout = [("value", 32)]
|
2012-06-20 12:29:03 -04:00
|
|
|
packed_layout = structuring.pack_layout(base_layout, pack_factor)
|
2012-11-29 15:22:38 -05:00
|
|
|
rawbits_layout = [("value", 32*pack_factor)]
|
2012-06-20 12:25:01 -04:00
|
|
|
|
2012-12-12 16:20:48 -05:00
|
|
|
source = SimActor(source_gen(), ("source", Source, base_layout))
|
|
|
|
sink = SimActor(sink_gen(), ("sink", Sink, base_layout))
|
2012-06-20 12:29:03 -04:00
|
|
|
|
|
|
|
# A tortuous way of passing integer tokens.
|
2012-12-12 16:20:48 -05:00
|
|
|
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)
|
2012-06-20 12:25:01 -04:00
|
|
|
|
|
|
|
g = DataFlowGraph()
|
|
|
|
g.add_connection(source, packer)
|
2012-06-20 12:29:03 -04:00
|
|
|
g.add_connection(packer, to_raw)
|
|
|
|
g.add_connection(to_raw, from_raw)
|
|
|
|
g.add_connection(from_raw, unpacker)
|
2012-06-20 12:25:01 -04:00
|
|
|
g.add_connection(unpacker, sink)
|
|
|
|
comp = CompositeActor(g)
|
2012-06-20 15:59:17 -04:00
|
|
|
reporter = perftools.DFGReporter(g)
|
2012-06-20 12:25:01 -04:00
|
|
|
|
2012-06-20 15:59:17 -04:00
|
|
|
fragment = comp.get_fragment() + reporter.get_fragment()
|
2012-06-20 12:25:01 -04:00
|
|
|
sim = Simulator(fragment, Runner())
|
2012-06-20 15:59:17 -04:00
|
|
|
sim.run(1000)
|
|
|
|
|
|
|
|
g_layout = nx.spectral_layout(g)
|
|
|
|
nx.draw(g, g_layout)
|
|
|
|
nx.draw_networkx_edge_labels(g, g_layout, reporter.get_edge_labels())
|
|
|
|
plt.show()
|
|
|
|
|
2012-06-20 12:25:01 -04:00
|
|
|
|
|
|
|
main()
|