litex/test/test_gearbox.py

56 lines
1.6 KiB
Python
Raw Normal View History

2017-04-24 15:41:46 -04:00
import unittest
import random
from migen import *
2017-04-24 15:41:46 -04:00
2018-11-17 11:29:45 -05:00
from litex.soc.interconnect.stream import Gearbox
2017-04-24 15:41:46 -04:00
2018-11-17 11:29:45 -05:00
def data_generator(dut, gearbox, datas):
prng = random.Random(42)
for i, data in enumerate(datas):
while prng.randrange(4):
yield
yield gearbox.sink.valid.eq(1)
yield gearbox.sink.data.eq(data)
yield
2018-11-17 11:29:45 -05:00
while (yield gearbox.sink.ready) == 0:
yield
yield gearbox.sink.valid.eq(0)
def data_checker(dut, gearbox, datas):
prng = random.Random(42)
dut.errors = 0
for i, reference in enumerate(datas):
yield gearbox.source.ready.eq(1)
yield
while (yield gearbox.source.valid) == 0:
yield
data = (yield gearbox.source.data)
if data != reference:
dut.errors += 1
yield gearbox.source.ready.eq(0)
while prng.randrange(4):
yield
2017-04-24 15:41:46 -04:00
class GearboxDUT(Module):
def __init__(self):
2018-11-17 11:29:45 -05:00
self.submodules.gearbox0 = Gearbox(20, 32)
self.submodules.gearbox1 = Gearbox(32, 20)
self.comb += self.gearbox0.source.connect(self.gearbox1.sink)
2017-04-24 15:41:46 -04:00
class TestGearbox(unittest.TestCase):
def test_gearbox(self):
2018-11-17 11:29:45 -05:00
prng = random.Random(42)
dut = GearboxDUT()
2018-11-17 11:29:45 -05:00
datas = [prng.randrange(2**20) for i in range(128)]
generators = [
data_generator(dut, dut.gearbox0, datas),
data_checker(dut, dut.gearbox1, datas)
]
run_simulation(dut, generators, vcd_name="sim.vcd")
self.assertEqual(dut.errors, 0)