2017-04-24 15:41:46 -04:00
|
|
|
import unittest
|
|
|
|
import random
|
|
|
|
|
2018-02-23 07:38:19 -05:00
|
|
|
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)
|
2017-04-25 04:57:34 -04:00
|
|
|
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-25 04:57:34 -04:00
|
|
|
|
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)
|
2017-04-25 04:57:34 -04:00
|
|
|
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)
|
|
|
|
]
|
2019-04-22 02:41:28 -04:00
|
|
|
run_simulation(dut, generators)
|
2018-11-17 11:29:45 -05:00
|
|
|
self.assertEqual(dut.errors, 0)
|