litex/examples/dataflow/structuring.py

68 lines
1.7 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
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():
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()
# 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)
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)
2012-06-20 12:25:01 -04:00
def main():
2013-04-10 13:12:42 -04:00
tb = TB()
sim = Simulator(tb).run(1000)
2013-04-10 13:12:42 -04:00
g_layout = nx.spectral_layout(tb.g)
nx.draw(tb.g, g_layout)
nx.draw_networkx_edge_labels(tb.g, g_layout, tb.reporter.get_edge_labels())
2012-06-20 15:59:17 -04:00
plt.show()
2012-06-20 12:25:01 -04:00
main()