litex/examples/dataflow/dma.py

106 lines
2.7 KiB
Python
Raw Normal View History

2012-06-08 15:31:05 -04:00
from random import Random
from migen.fhdl.std import *
from migen.flow.network import *
2012-12-14 09:55:38 -05:00
from migen.flow.transactions import *
2014-01-26 16:19:43 -05:00
from migen.actorlib import dma_wishbone
2012-06-08 15:31:05 -04:00
from migen.actorlib.sim import *
2014-01-26 16:19:43 -05:00
from migen.bus import wishbone
from migen.sim.generic import run_simulation
2012-06-08 15:31:05 -04:00
2015-04-13 14:45:35 -04:00
2012-06-12 13:55:57 -04:00
class MyModel:
def read(self, address):
return address + 4
2012-06-12 13:55:57 -04:00
2015-04-13 14:45:35 -04:00
2012-06-12 13:55:57 -04:00
class MyModelWB(MyModel, wishbone.TargetModel):
def __init__(self):
self.prng = Random(763627)
2012-06-12 13:55:57 -04:00
def can_ack(self, bus):
return self.prng.randrange(0, 2)
2012-06-08 15:31:05 -04:00
2015-04-13 14:45:35 -04:00
2012-06-08 15:31:05 -04:00
def adrgen_gen():
for i in range(10):
print("Address: " + hex(i))
yield Token("address", {"a": i})
2012-06-08 15:31:05 -04:00
2015-04-13 14:45:35 -04:00
2013-04-10 13:12:42 -04:00
class SimAdrGen(SimActor):
def __init__(self, nbits):
self.address = Source([("a", nbits)])
SimActor.__init__(self, adrgen_gen())
2013-04-10 13:12:42 -04:00
2015-04-13 14:45:35 -04:00
2012-06-08 15:31:05 -04:00
def dumper_gen():
while True:
t = Token("data", idle_wait=True)
yield t
print("Received: " + hex(t.value["d"]))
2012-06-08 15:31:05 -04:00
2015-04-13 14:45:35 -04:00
2013-04-10 13:12:42 -04:00
class SimDumper(SimActor):
def __init__(self):
self.data = Sink([("d", 32)])
SimActor.__init__(self, dumper_gen())
2013-04-10 13:12:42 -04:00
2015-04-13 14:45:35 -04:00
2012-06-12 13:55:57 -04:00
def trgen_gen():
for i in range(10):
a = i
d = i+10
print("Address: " + hex(a) + " Data: " + hex(d))
yield Token("address_data", {"a": a, "d": d})
2012-06-12 13:55:57 -04:00
2015-04-13 14:45:35 -04:00
2013-04-10 13:12:42 -04:00
class SimTrGen(SimActor):
def __init__(self, a_nbits):
self.address_data = Source([("a", a_nbits), ("d", 32)])
SimActor.__init__(self, trgen_gen())
2013-04-10 13:12:42 -04:00
2015-04-13 14:45:35 -04:00
2013-04-10 13:12:42 -04:00
class TBWishbone(Module):
def __init__(self, master):
self.submodules.peripheral = wishbone.Target(MyModelWB())
self.submodules.tap = wishbone.Tap(self.peripheral.bus)
self.submodules.interconnect = wishbone.InterconnectPointToPoint(master.bus,
self.peripheral.bus)
2013-04-10 13:12:42 -04:00
2015-04-13 14:45:35 -04:00
2013-04-10 13:12:42 -04:00
class TBWishboneReader(TBWishbone):
def __init__(self):
self.adrgen = SimAdrGen(30)
self.reader = dma_wishbone.Reader()
self.dumper = SimDumper()
g = DataFlowGraph()
g.add_connection(self.adrgen, self.reader)
g.add_connection(self.reader, self.dumper)
self.submodules.comp = CompositeActor(g)
TBWishbone.__init__(self, self.reader)
2013-04-10 13:12:42 -04:00
2015-04-13 14:45:35 -04:00
2013-04-10 13:12:42 -04:00
class TBWishboneWriter(TBWishbone):
def __init__(self):
self.trgen = SimTrGen(30)
self.writer = dma_wishbone.Writer()
g = DataFlowGraph()
g.add_connection(self.trgen, self.writer)
self.submodules.comp = CompositeActor(g)
TBWishbone.__init__(self, self.writer)
2013-04-10 13:12:42 -04:00
2015-04-13 14:45:35 -04:00
2012-06-12 13:55:57 -04:00
def test_wb_reader():
print("*** Testing Wishbone reader")
run_simulation(TBWishboneReader(), 100)
2012-06-08 15:31:05 -04:00
2015-04-13 14:45:35 -04:00
2012-06-12 13:55:57 -04:00
def test_wb_writer():
print("*** Testing Wishbone writer")
run_simulation(TBWishboneWriter(), 100)
2014-01-26 16:19:43 -05:00
if __name__ == "__main__":
test_wb_reader()
test_wb_writer()