liteeth/test/test_mac_wishbone.py

161 lines
4.8 KiB
Python
Raw Permalink Normal View History

#
# This file is part of LiteEth.
#
# Copyright (c) 2015-2019 Florent Kermarrec <florent@enjoy-digital.fr>
# SPDX-License-Identifier: BSD-2-Clause
import unittest
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
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 *
2019-06-24 05:23:03 -04:00
from liteeth.mac import LiteEthMAC
2015-09-07 07:28:02 -04:00
from test.model import phy, mac
2015-09-07 07:28:02 -04:00
from litex.gen.sim import *
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):
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)
while not (yield self.obj.ack):
2015-09-07 07:28:02 -04:00
yield
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):
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)
while not (yield self.obj.ack):
2015-09-07 07:28:02 -04:00
yield
self.dat = (yield self.obj.dat_r)
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):
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
yield self.obj._start.re.eq(0)
2015-09-07 07:28:02 -04:00
yield
def wait_done(self):
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):
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
class DUT(Module):
2015-09-07 07:28:02 -04:00
def __init__(self):
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)
def main_generator(dut):
wishbone_tx_master = WishboneMaster(dut.ethmac.bus_tx)
wishbone_rx_master = WishboneMaster(dut.ethmac.bus_rx)
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
sram_writer_slots_offset = [0x000, 0x200]
sram_reader_slots_offset = [0x000, 0x200]
2015-09-07 07:28:02 -04:00
length = 150+2
2015-09-07 07:28:02 -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
errors = 0
for i in range(2):
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_tx_master.write(sram_reader_slots_offset[slot]+i, dat)
# 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()
# 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_tx_master.read(sram_writer_slots_offset[slot]+i)
dat = wishbone_tx_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
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")