litex/examples/dataflow/arithmetic.py

55 lines
1.2 KiB
Python
Raw Normal View History

2012-01-09 08:21:54 -05:00
import sys
2012-01-06 18:33:28 -05:00
2012-06-15 11:52:19 -04:00
import matplotlib.pyplot as plt
import networkx as nx
2012-01-06 18:33:28 -05:00
from migen.flow.network import *
from migen.actorlib.ala import *
2012-06-16 13:23:59 -04:00
from migen.actorlib.sim import *
from migen.sim.generic import Simulator
from migen.sim.icarus import Runner
2012-01-06 11:24:05 -05:00
2012-06-16 13:23:59 -04:00
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)]))
2012-06-16 15:23:42 -04:00
def draw(g):
if len(sys.argv) > 1 and sys.argv[1] == "draw":
nx.draw_spectral(g)
2012-06-16 15:23:42 -04:00
plt.show()
2012-06-16 13:23:59 -04:00
def main():
# Create graph
g = DataFlowGraph()
gen1 = ComposableNode(g, NumberGen())
gen2 = ComposableNode(g, NumberGen())
2012-06-16 15:23:42 -04:00
ps = gen1 + gen2
result = ps*gen1 + ps*gen2
2012-11-23 18:00:07 -05:00
g.add_connection(result, ActorNode(Dumper([("r", BV(32))])))
2012-06-16 13:23:59 -04:00
gen1.actor.name = "gen1"
gen2.actor.name = "gen2"
result.name = "result"
2012-06-16 13:23:59 -04:00
# Elaborate
print("is_abstract before elaboration: " + str(g.is_abstract()))
2012-06-16 15:23:42 -04:00
draw(g)
2012-06-16 13:23:59 -04:00
g.elaborate()
print("is_abstract after elaboration : " + str(g.is_abstract()))
2012-06-16 15:23:42 -04:00
draw(g)
2012-06-16 13:23:59 -04:00
# Simulate
c = CompositeActor(g)
fragment = c.get_fragment()
sim = Simulator(fragment, Runner())
sim.run(100)
main()