litex/examples/dataflow/structuring.py

59 lines
1.5 KiB
Python
Raw Normal View History

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
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():
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():
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
2012-12-12 16:20:48 -05:00
source = SimActor(source_gen(), ("source", Source, base_layout))
sink = SimActor(sink_gen(), ("sink", Sink, base_layout))
# 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)
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()
2013-02-09 11:04:53 -05:00
sim = Simulator(fragment)
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()