Skip to content

Commit

Permalink
examples/flow/arithmetic: simulate
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Bourdeauducq committed Jun 16, 2012
1 parent bde8361 commit 5c13951
Showing 1 changed file with 55 additions and 30 deletions.
85 changes: 55 additions & 30 deletions examples/dataflow/arithmetic.py
Expand Up @@ -3,37 +3,62 @@
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import networkx as nx import networkx as nx


from migen.fhdl import verilog
from migen.flow.ala import * from migen.flow.ala import *
from migen.flow.network import * from migen.flow.network import *
from migen.flow.composer 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):
# Create graph self.bv_r = BV(16)
g = DataFlowGraph() def number_gen():
a1 = ComposableSource(g, Add(BV(16))) for i in range(10):
a2 = ComposableSource(g, Add(BV(16))) yield Token("result", {"r": i})
a3 = ComposableSource(g, Add(BV(16))) super().__init__(number_gen(),
c3 = (a1 + a2)*a3 ("result", Source, [("r", self.bv_r)]))


a1.actor_node.name = "in1" class Dumper(SimActor):
a2.actor_node.name = "in2" def __init__(self):
a3.actor_node.name = "in3" def dumper_gen():
c3.actor_node.name = "result" while True:

t = Token("result")
# Elaborate yield t
print("is_abstract before elaboration: " + str(g.is_abstract())) print("Received: " + str(t.value["r"]))
if draw: super().__init__(dumper_gen(),
nx.draw(g) ("result", Sink, [("r", BV(32))]))
plt.show()
g.elaborate() def main():
print("is_abstract after elaboration : " + str(g.is_abstract())) # Create graph
if draw: g = DataFlowGraph()
nx.draw(g) a1 = ComposableSource(g, NumberGen())
plt.show() a2 = ComposableSource(g, NumberGen())

a3 = ComposableSource(g, NumberGen())
# Convert c3 = (a1 + a2)*a3
c = CompositeActor(g) g.add_connection(c3.actor_node, Dumper())
frag = c.get_fragment()
print(verilog.convert(frag)) 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()

# Simulate
c = CompositeActor(g)
fragment = c.get_fragment()
sim = Simulator(fragment, Runner())
sim.run(100)

main()

0 comments on commit 5c13951

Please sign in to comment.