litex/examples/dataflow/dma.py

150 lines
4.1 KiB
Python
Raw Normal View History

2012-06-08 15:31:05 -04:00
from random import Random
2013-04-10 13:12:42 -04:00
from migen.fhdl.module import Module
from migen.flow.network import *
2012-12-14 09:55:38 -05:00
from migen.flow.transactions import *
2012-06-12 15:04:47 -04:00
from migen.actorlib import dma_wishbone, dma_asmi
2012-06-08 15:31:05 -04:00
from migen.actorlib.sim import *
2012-06-12 13:55:57 -04:00
from migen.bus import wishbone, asmibus
2012-06-08 15:31:05 -04:00
from migen.sim.generic import Simulator
2012-06-12 13:55:57 -04:00
class MyModel:
2012-06-10 10:40:33 -04:00
def read(self, address):
return address + 4
2012-06-12 13:55:57 -04:00
class MyModelWB(MyModel, wishbone.TargetModel):
def __init__(self):
self.prng = Random(763627)
2012-06-10 10:40:33 -04:00
def can_ack(self, bus):
return self.prng.randrange(0, 2)
2012-06-08 15:31:05 -04:00
2012-06-12 13:55:57 -04:00
class MyModelASMI(MyModel, asmibus.TargetModel):
pass
2012-06-08 15:31:05 -04:00
def adrgen_gen():
for i in range(10):
2013-04-28 12:06:36 -04:00
print("Address: " + hex(i))
2012-06-08 15:31:05 -04:00
yield Token("address", {"a": i})
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())
2012-06-08 15:31:05 -04:00
def dumper_gen():
while True:
2012-12-14 13:16:22 -05:00
t = Token("data", idle_wait=True)
2012-06-08 15:31:05 -04:00
yield t
2013-04-28 12:06:36 -04:00
print("Received: " + hex(t.value["d"]))
2012-06-08 15:31:05 -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())
2012-06-12 13:55:57 -04:00
def trgen_gen():
for i in range(10):
a = i
d = i+10
2013-04-28 12:06:36 -04:00
print("Address: " + hex(a) + " Data: " + hex(d))
2012-06-12 13:55:57 -04:00
yield Token("address_data", {"a": a, "d": d})
2013-04-10 13:12:42 -04:00
class SimTrGen(SimActor):
2013-04-28 12:06:36 -04:00
def __init__(self, a_nbits):
self.address_data = Source([("a", a_nbits), ("d", 32)])
2013-04-10 13:12:42 -04:00
SimActor.__init__(self, trgen_gen())
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)
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)
def do_simulation(self, s):
s.interrupt = self.adrgen.token_exchanger.done and not s.rd(self.comp.busy)
class TBWishboneWriter(TBWishbone):
def __init__(self):
2013-04-28 12:06:36 -04:00
self.trgen = SimTrGen(30)
2013-04-10 13:12:42 -04:00
self.writer = dma_wishbone.Writer()
g = DataFlowGraph()
g.add_connection(self.trgen, self.writer)
self.submodules.comp = CompositeActor(g)
TBWishbone.__init__(self, self.writer)
def do_simulation(self, s):
s.interrupt = self.trgen.token_exchanger.done and not s.rd(self.comp.busy)
class TBAsmi(Module):
def __init__(self, nslots):
self.submodules.hub = asmibus.Hub(32, 32)
2013-04-28 12:06:36 -04:00
self.port = self.hub.get_port(nslots)
2013-04-10 13:12:42 -04:00
self.hub.finalize()
2013-04-28 12:06:36 -04:00
self.submodules.peripheral = asmibus.Target(MyModelASMI(), self.hub)
self.submodules.tap = asmibus.Tap(self.hub)
class TBAsmiReader(TBAsmi):
def __init__(self, nslots):
TBAsmi.__init__(self, nslots)
2013-04-10 13:12:42 -04:00
self.adrgen = SimAdrGen(32)
2013-04-28 12:06:36 -04:00
self.reader = dma_asmi.Reader(self.port)
2013-04-10 13:12:42 -04:00
self.dumper = SimDumper()
g = DataFlowGraph()
g.add_connection(self.adrgen, self.reader)
g.add_connection(self.reader, self.dumper)
self.submodules.comp = CompositeActor(g)
def do_simulation(self, s):
s.interrupt = self.adrgen.token_exchanger.done and not s.rd(self.comp.busy)
2012-06-12 13:55:57 -04:00
2013-04-28 12:06:36 -04:00
class TBAsmiWriter(TBAsmi):
def __init__(self, nslots):
TBAsmi.__init__(self, nslots)
self.trgen = SimTrGen(32)
self.writer = dma_asmi.Writer(self.port)
g = DataFlowGraph()
g.add_connection(self.trgen, self.writer)
self.submodules.comp = CompositeActor(g)
def do_simulation(self, s):
s.interrupt = self.trgen.token_exchanger.done and not s.rd(self.comp.busy)
2012-06-12 13:55:57 -04:00
def test_wb_reader():
print("*** Testing Wishbone reader")
2013-04-10 13:12:42 -04:00
Simulator(TBWishboneReader()).run()
2012-06-08 15:31:05 -04:00
2012-06-12 13:55:57 -04:00
def test_wb_writer():
print("*** Testing Wishbone writer")
2013-04-10 13:12:42 -04:00
Simulator(TBWishboneWriter()).run()
2012-06-12 15:04:47 -04:00
def test_asmi_reader(nslots):
print("*** Testing ASMI reader (nslots={})".format(nslots))
2013-04-10 13:12:42 -04:00
Simulator(TBAsmiReader(nslots)).run()
2012-06-08 15:31:05 -04:00
2013-04-28 12:06:36 -04:00
def test_asmi_writer(nslots):
print("*** Testing ASMI writer (nslots={})".format(nslots))
Simulator(TBAsmiWriter(nslots)).run()
2012-06-12 13:55:57 -04:00
test_wb_reader()
test_wb_writer()
test_asmi_reader(1)
test_asmi_reader(2)
2013-04-28 12:06:36 -04:00
test_asmi_writer(1)
test_asmi_writer(2)