From 5c139511e888218731c59a4a51248a13683197db Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 16 Jun 2012 19:23:59 +0200 Subject: [PATCH] examples/flow/arithmetic: simulate --- examples/dataflow/arithmetic.py | 77 ++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/examples/dataflow/arithmetic.py b/examples/dataflow/arithmetic.py index aa859212e..28f42159f 100644 --- a/examples/dataflow/arithmetic.py +++ b/examples/dataflow/arithmetic.py @@ -3,37 +3,62 @@ import sys import matplotlib.pyplot as plt import networkx as nx -from migen.fhdl import verilog from migen.flow.ala import * from migen.flow.network import * from migen.flow.composer import * +from migen.actorlib.sim import * +from migen.sim.generic import Simulator +from migen.sim.icarus import Runner -draw = len(sys.argv) > 1 and sys.argv[1] == "draw" +class NumberGen(SimActor): + def __init__(self): + self.bv_r = BV(16) + def number_gen(): + for i in range(10): + yield Token("result", {"r": i}) + super().__init__(number_gen(), + ("result", Source, [("r", self.bv_r)])) -# Create graph -g = DataFlowGraph() -a1 = ComposableSource(g, Add(BV(16))) -a2 = ComposableSource(g, Add(BV(16))) -a3 = ComposableSource(g, Add(BV(16))) -c3 = (a1 + a2)*a3 +class Dumper(SimActor): + def __init__(self): + def dumper_gen(): + while True: + t = Token("result") + yield t + print("Received: " + str(t.value["r"])) + super().__init__(dumper_gen(), + ("result", Sink, [("r", BV(32))])) -a1.actor_node.name = "in1" -a2.actor_node.name = "in2" -a3.actor_node.name = "in3" -c3.actor_node.name = "result" +def main(): + # Create graph + g = DataFlowGraph() + a1 = ComposableSource(g, NumberGen()) + a2 = ComposableSource(g, NumberGen()) + a3 = ComposableSource(g, NumberGen()) + c3 = (a1 + a2)*a3 + g.add_connection(c3.actor_node, Dumper()) -# Elaborate -print("is_abstract before elaboration: " + str(g.is_abstract())) -if draw: - nx.draw(g) - plt.show() -g.elaborate() -print("is_abstract after elaboration : " + str(g.is_abstract())) -if draw: - nx.draw(g) - plt.show() + a1.actor_node.actor.name = "gen1" + a2.actor_node.actor.name = "gen2" + a3.actor_node.actor.name = "gen3" + c3.actor_node.name = "result" + + # Elaborate + draw = len(sys.argv) > 1 and sys.argv[1] == "draw" + print("is_abstract before elaboration: " + str(g.is_abstract())) + if draw: + nx.draw(g) + plt.show() + g.elaborate() + print("is_abstract after elaboration : " + str(g.is_abstract())) + if draw: + nx.draw(g) + plt.show() -# Convert -c = CompositeActor(g) -frag = c.get_fragment() -print(verilog.convert(frag)) + # Simulate + c = CompositeActor(g) + fragment = c.get_fragment() + sim = Simulator(fragment, Runner()) + sim.run(100) + +main()