2016-03-22 20:42:35 -04:00
|
|
|
#!/usr/bin/env python3
|
2015-11-13 09:11:57 -05:00
|
|
|
from litex.gen import *
|
2015-09-07 07:28:02 -04:00
|
|
|
|
2015-11-13 09:11:57 -05:00
|
|
|
from litex.soc.interconnect import wishbone
|
2015-11-13 18:42:33 -05:00
|
|
|
from litex.soc.interconnect.stream_sim import *
|
2015-11-13 09:11:57 -05:00
|
|
|
|
2015-09-08 03:50:45 -04:00
|
|
|
from liteeth.common import *
|
|
|
|
from liteeth.core.mac import LiteEthMAC
|
2015-09-07 07:28:02 -04:00
|
|
|
|
2016-03-22 05:16:17 -04:00
|
|
|
from model import phy, mac
|
2015-09-07 07:28:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
class WishboneMaster:
|
|
|
|
def __init__(self, obj):
|
|
|
|
self.obj = obj
|
|
|
|
self.dat = 0
|
|
|
|
|
|
|
|
def write(self, adr, dat):
|
2016-03-21 14:30:47 -04:00
|
|
|
yield self.obj.cyc.eq(1)
|
|
|
|
yield self.obj.stb.eq(1)
|
|
|
|
yield self.obj.adr.eq(adr)
|
|
|
|
yield self.obj.we.eq(1)
|
|
|
|
yield self.obj.sel.eq(0xf)
|
|
|
|
yield self.obj.dat_w.eq(dat)
|
2016-03-23 04:47:47 -04:00
|
|
|
while not (yield self.obj.ack):
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
2016-03-21 14:30:47 -04:00
|
|
|
yield self.obj.cyc.eq(0)
|
|
|
|
yield self.obj.stb.eq(0)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
|
|
|
|
|
|
|
def read(self, adr):
|
2016-03-21 14:30:47 -04:00
|
|
|
yield self.obj.cyc.eq(1)
|
|
|
|
yield self.obj.stb.eq(1)
|
|
|
|
yield self.obj.adr.eq(adr)
|
|
|
|
yield self.obj.we.eq(0)
|
|
|
|
yield self.obj.sel.eq(0xf)
|
|
|
|
yield self.obj.dat_w.eq(0)
|
2016-03-23 04:47:47 -04:00
|
|
|
while not (yield self.obj.ack):
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
2016-03-23 04:47:47 -04:00
|
|
|
self.dat = (yield self.obj.dat_r)
|
2016-03-21 14:30:47 -04:00
|
|
|
yield self.obj.cyc.eq(0)
|
|
|
|
yield self.obj.stb.eq(0)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
class SRAMReaderDriver:
|
|
|
|
def __init__(self, obj):
|
|
|
|
self.obj = obj
|
|
|
|
|
|
|
|
def start(self, slot, length):
|
2016-03-21 14:30:47 -04:00
|
|
|
yield self.obj._slot.storage.eq(slot)
|
|
|
|
yield self.obj._length.storage.eq(length)
|
|
|
|
yield self.obj._start.re.eq(1)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
2016-03-21 14:30:47 -04:00
|
|
|
yield self.obj._start.re.eq(0)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
|
|
|
|
|
|
|
def wait_done(self):
|
2016-03-23 04:47:47 -04:00
|
|
|
while not (yield self.obj.ev.done.pending):
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
|
|
|
|
|
|
|
def clear_done(self):
|
2016-03-21 14:30:47 -04:00
|
|
|
yield self.obj.ev.done.clear.eq(1)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
2016-03-21 14:30:47 -04:00
|
|
|
yield self.obj.ev.done.clear.eq(0)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
class SRAMWriterDriver:
|
|
|
|
def __init__(self, obj):
|
|
|
|
self.obj = obj
|
|
|
|
|
|
|
|
def wait_available(self):
|
2016-03-23 04:47:47 -04:00
|
|
|
while not (yield self.obj.ev.available.pending):
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
|
|
|
|
|
|
|
def clear_available(self):
|
2016-03-21 14:30:47 -04:00
|
|
|
yield self.obj.ev.available.clear.eq(1)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
2016-03-21 14:30:47 -04:00
|
|
|
yield self.obj.ev.available.clear.eq(0)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
class TB(Module):
|
|
|
|
def __init__(self):
|
2016-03-23 04:47:47 -04:00
|
|
|
self.submodules.phy_model = phy.PHY(8, debug=False)
|
|
|
|
self.submodules.mac_model = mac.MAC(self.phy_model, debug=False, loopback=True)
|
2015-09-07 07:28:02 -04:00
|
|
|
self.submodules.ethmac = LiteEthMAC(phy=self.phy_model, dw=32, interface="wishbone", with_preamble_crc=True)
|
|
|
|
|
|
|
|
|
2016-03-21 14:30:47 -04:00
|
|
|
def main_generator(dut):
|
|
|
|
wishbone_master = WishboneMaster(dut.ethmac.bus)
|
|
|
|
sram_reader_driver = SRAMReaderDriver(dut.ethmac.interface.sram.reader)
|
|
|
|
sram_writer_driver = SRAMWriterDriver(dut.ethmac.interface.sram.writer)
|
2015-09-07 07:28:02 -04:00
|
|
|
|
2016-03-21 14:30:47 -04:00
|
|
|
sram_writer_slots_offset = [0x000, 0x200]
|
|
|
|
sram_reader_slots_offset = [0x400, 0x600]
|
2015-09-07 07:28:02 -04:00
|
|
|
|
2016-03-21 14:30:47 -04:00
|
|
|
length = 150+2
|
2015-09-07 07:28:02 -04:00
|
|
|
|
2016-03-23 04:47:47 -04:00
|
|
|
tx_payload = [seed_to_data(i, True) % 0xff for i in range(length)] + [0, 0, 0, 0]
|
2015-09-07 07:28:02 -04:00
|
|
|
|
2016-03-21 14:30:47 -04:00
|
|
|
errors = 0
|
2015-11-13 08:47:57 -05:00
|
|
|
|
2016-03-23 04:47:47 -04:00
|
|
|
for i in range(2):
|
2016-03-21 14:30:47 -04:00
|
|
|
for i in range(20):
|
|
|
|
yield
|
|
|
|
for slot in range(2):
|
|
|
|
print("slot {}: ".format(slot), end="")
|
|
|
|
# fill tx memory
|
|
|
|
for i in range(length//4+1):
|
|
|
|
dat = int.from_bytes(tx_payload[4*i:4*(i+1)], "big")
|
|
|
|
yield from wishbone_master.write(sram_reader_slots_offset[slot]+i, dat)
|
|
|
|
|
2016-03-23 04:47:47 -04:00
|
|
|
# send tx payload & wait
|
|
|
|
yield from sram_reader_driver.start(slot, length)
|
|
|
|
yield from sram_reader_driver.wait_done()
|
|
|
|
yield from sram_reader_driver.clear_done()
|
2016-03-21 14:30:47 -04:00
|
|
|
|
2016-03-23 04:47:47 -04:00
|
|
|
# wait rx
|
|
|
|
yield from sram_writer_driver.wait_available()
|
|
|
|
yield from sram_writer_driver.clear_available()
|
|
|
|
|
|
|
|
# get rx payload (loopback on PHY Model)
|
|
|
|
rx_payload = []
|
|
|
|
for i in range(length//4+1):
|
|
|
|
yield from wishbone_master.read(sram_writer_slots_offset[slot]+i)
|
|
|
|
dat = wishbone_master.dat
|
|
|
|
rx_payload += list(dat.to_bytes(4, byteorder='big'))
|
|
|
|
|
|
|
|
# check results
|
|
|
|
s, l, e = check(tx_payload[:length], rx_payload[:min(length, len(rx_payload))])
|
|
|
|
print("shift " + str(s) + " / length " + str(l) + " / errors " + str(e))
|
2015-09-07 07:28:02 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-03-21 14:30:47 -04:00
|
|
|
tb = TB()
|
|
|
|
generators = {
|
|
|
|
"sys" : main_generator(tb),
|
|
|
|
"eth_tx": [tb.phy_model.phy_sink.generator(),
|
|
|
|
tb.phy_model.generator()],
|
|
|
|
"eth_rx": tb.phy_model.phy_source.generator()
|
|
|
|
}
|
|
|
|
clocks = {"sys": 10,
|
|
|
|
"eth_rx": 10,
|
|
|
|
"eth_tx": 10}
|
|
|
|
run_simulation(tb, generators, clocks, vcd_name="sim.vcd")
|