2017-01-19 08:33:24 -05:00
|
|
|
import unittest
|
2018-02-23 07:40:09 -05:00
|
|
|
|
|
|
|
from migen 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
|
|
|
|
2017-01-19 08:33:24 -05:00
|
|
|
from test.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-25 07:26:02 -04:00
|
|
|
yield self.obj.ev.pending.re.eq(1)
|
|
|
|
yield self.obj.ev.pending.r.eq(1)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
2016-03-25 07:26:02 -04:00
|
|
|
yield self.obj.ev.pending.re.eq(0)
|
|
|
|
yield self.obj.ev.pending.r.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-25 07:26:02 -04:00
|
|
|
yield self.obj.ev.pending.re.eq(1)
|
|
|
|
yield self.obj.ev.pending.r.eq(1)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
2016-03-25 07:26:02 -04:00
|
|
|
yield self.obj.ev.pending.re.eq(0)
|
|
|
|
yield self.obj.ev.pending.r.eq(0)
|
2015-09-07 07:28:02 -04:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2017-01-19 08:33:24 -05:00
|
|
|
class DUT(Module):
|
2015-09-07 07:28:02 -04:00
|
|
|
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 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
|
|
|
|
2017-01-19 08:33:24 -05:00
|
|
|
|
|
|
|
class TestMACWishbone(unittest.TestCase):
|
|
|
|
def test(self):
|
|
|
|
dut = DUT()
|
|
|
|
generators = {
|
|
|
|
"sys" : main_generator(dut),
|
|
|
|
"eth_tx": [dut.phy_model.phy_sink.generator(),
|
|
|
|
dut.phy_model.generator()],
|
|
|
|
"eth_rx": dut.phy_model.phy_source.generator()
|
|
|
|
}
|
|
|
|
clocks = {"sys": 20,
|
|
|
|
"eth_rx": 8,
|
|
|
|
"eth_tx": 8}
|
|
|
|
run_simulation(dut, generators, clocks, vcd_name="sim.vcd")
|