litex/examples/pytholite/basic.py

47 lines
1.0 KiB
Python
Raw Normal View History

2012-11-16 13:34:34 -05:00
from migen.flow.network import *
2012-12-14 09:55:38 -05:00
from migen.flow.transactions import *
2012-11-16 13:34:34 -05:00
from migen.actorlib.sim import *
2013-04-10 17:42:46 -04:00
from migen.pytholite.compiler import Pytholite
2014-01-26 16:19:43 -05:00
from migen.sim.generic import run_simulation
from migen.fhdl import verilog
2012-11-16 13:34:34 -05:00
layout = [("r", 32)]
2012-11-16 13:34:34 -05:00
def number_gen(n):
for i in range(n):
2012-11-16 13:34:34 -05:00
yield Token("result", {"r": i})
2013-04-10 13:12:42 -04:00
class SimNumberGen(SimActor):
def __init__(self):
self.result = Source(layout)
SimActor.__init__(self, number_gen(5))
2013-04-10 13:12:42 -04:00
2014-01-26 16:19:43 -05:00
def run_ng_sim(ng):
2012-11-16 13:34:34 -05:00
g = DataFlowGraph()
2012-11-23 18:00:07 -05:00
d = Dumper(layout)
2012-12-12 16:20:48 -05:00
g.add_connection(ng, d)
2014-10-17 05:08:37 -04:00
2012-11-16 13:34:34 -05:00
c = CompositeActor(g)
2014-01-26 16:19:43 -05:00
run_simulation(c, ncycles=20)
2012-11-16 13:34:34 -05:00
2013-04-10 17:42:46 -04:00
def make_ng_pytholite():
ng_pytholite = Pytholite(number_gen, 5)
2013-04-10 17:42:46 -04:00
ng_pytholite.result = Source(layout)
ng_pytholite.finalize()
return ng_pytholite
2012-11-16 13:34:34 -05:00
def main():
print("Simulating native Python:")
2013-04-10 13:12:42 -04:00
ng_native = SimNumberGen()
2014-01-26 16:19:43 -05:00
run_ng_sim(ng_native)
2014-10-17 05:08:37 -04:00
2012-11-16 13:34:34 -05:00
print("Simulating Pytholite:")
2013-04-10 17:42:46 -04:00
ng_pytholite = make_ng_pytholite()
2014-01-26 16:19:43 -05:00
run_ng_sim(ng_pytholite)
2014-10-17 05:08:37 -04:00
print("Converting Pytholite to Verilog:")
2013-04-10 17:42:46 -04:00
ng_pytholite = make_ng_pytholite()
2013-03-12 11:59:24 -04:00
print(verilog.convert(ng_pytholite))
2012-11-16 13:34:34 -05:00
main()