2013-06-09 08:17:30 -04:00
|
|
|
from random import Random
|
|
|
|
|
|
|
|
from migen.fhdl.std import *
|
|
|
|
from migen.bus.transactions import *
|
|
|
|
from migen.bus import wishbone
|
2014-01-26 16:19:43 -05:00
|
|
|
from migen.sim.generic import run_simulation
|
2013-06-09 08:17:30 -04:00
|
|
|
|
|
|
|
# Our bus master.
|
|
|
|
# Python generators let us program bus transactions in an elegant sequential style.
|
|
|
|
def my_generator():
|
2015-04-13 14:07:07 -04:00
|
|
|
prng = Random(92837)
|
2013-06-09 08:17:30 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
# Write to the first addresses.
|
|
|
|
for x in range(10):
|
|
|
|
t = TWrite(x, 2*x)
|
|
|
|
yield t
|
|
|
|
print("Wrote in " + str(t.latency) + " cycle(s)")
|
|
|
|
# Insert some dead cycles to simulate bus inactivity.
|
|
|
|
for delay in range(prng.randrange(0, 3)):
|
|
|
|
yield None
|
2013-06-09 08:17:30 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
# Read from the first addresses.
|
|
|
|
for x in range(10):
|
|
|
|
t = TRead(x)
|
|
|
|
yield t
|
|
|
|
print("Read " + str(t.data) + " in " + str(t.latency) + " cycle(s)")
|
|
|
|
for delay in range(prng.randrange(0, 3)):
|
|
|
|
yield None
|
2013-06-09 08:17:30 -04:00
|
|
|
|
|
|
|
# Our bus slave.
|
|
|
|
class MyModelWB(wishbone.TargetModel):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.prng = Random(763627)
|
2013-06-09 08:17:30 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def read(self, address):
|
|
|
|
return address + 4
|
2013-06-09 08:17:30 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def can_ack(self, bus):
|
|
|
|
# Simulate variable latency.
|
|
|
|
return self.prng.randrange(0, 2)
|
2013-06-09 08:17:30 -04:00
|
|
|
|
|
|
|
class TB(Module):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self):
|
|
|
|
# The "wishbone.Initiator" library component runs our generator
|
|
|
|
# and manipulates the bus signals accordingly.
|
|
|
|
self.submodules.master = wishbone.Initiator(my_generator())
|
|
|
|
# The "wishbone.Target" library component examines the bus signals
|
|
|
|
# and calls into our model object.
|
|
|
|
self.submodules.slave = wishbone.Target(MyModelWB())
|
|
|
|
# The "wishbone.Tap" library component examines the bus at the slave port
|
|
|
|
# and displays the transactions on the console (<TRead...>/<TWrite...>).
|
|
|
|
self.submodules.tap = wishbone.Tap(self.slave.bus)
|
|
|
|
# Connect the master to the slave.
|
|
|
|
self.submodules.intercon = wishbone.InterconnectPointToPoint(self.master.bus, self.slave.bus)
|
2013-06-09 08:17:30 -04:00
|
|
|
|
2014-01-26 16:19:43 -05:00
|
|
|
if __name__ == "__main__":
|
2015-04-13 14:07:07 -04:00
|
|
|
run_simulation(TB())
|
2013-06-09 08:17:30 -04:00
|
|
|
|
|
|
|
# Output:
|
|
|
|
# <TWrite adr:0x0 dat:0x0>
|
|
|
|
# Wrote in 0 cycle(s)
|
|
|
|
# <TWrite adr:0x1 dat:0x2>
|
|
|
|
# Wrote in 0 cycle(s)
|
|
|
|
# <TWrite adr:0x2 dat:0x4>
|
|
|
|
# Wrote in 0 cycle(s)
|
|
|
|
# <TWrite adr:0x3 dat:0x6>
|
|
|
|
# Wrote in 1 cycle(s)
|
|
|
|
# <TWrite adr:0x4 dat:0x8>
|
|
|
|
# Wrote in 1 cycle(s)
|
|
|
|
# <TWrite adr:0x5 dat:0xa>
|
|
|
|
# Wrote in 2 cycle(s)
|
|
|
|
# ...
|
|
|
|
# <TRead adr:0x0 dat:0x4>
|
|
|
|
# Read 4 in 2 cycle(s)
|
|
|
|
# <TRead adr:0x1 dat:0x5>
|
|
|
|
# Read 5 in 2 cycle(s)
|
|
|
|
# <TRead adr:0x2 dat:0x6>
|
|
|
|
# Read 6 in 1 cycle(s)
|
|
|
|
# <TRead adr:0x3 dat:0x7>
|
|
|
|
# Read 7 in 1 cycle(s)
|
|
|
|
# ...
|