2020-08-23 09:40:21 -04:00
|
|
|
#
|
|
|
|
# This file is part of LiteX.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2017-2019 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
2019-06-23 17:31:11 -04:00
|
|
|
|
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):
|
2021-03-18 08:47:10 -04:00
|
|
|
def __init__(self, dw0=20, dw1=32):
|
|
|
|
self.submodules.gearbox0 = Gearbox(dw0, dw1)
|
|
|
|
self.submodules.gearbox1 = Gearbox(dw1, dw0)
|
2018-11-17 11:29:45 -05:00
|
|
|
self.comb += self.gearbox0.source.connect(self.gearbox1.sink)
|
2017-04-24 15:41:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
class TestGearbox(unittest.TestCase):
|
2021-03-18 08:47:10 -04:00
|
|
|
def gearbox_test(self, dw0, dw1):
|
2018-11-17 11:29:45 -05:00
|
|
|
prng = random.Random(42)
|
2021-03-18 08:47:10 -04:00
|
|
|
dut = GearboxDUT(dw0, dw1)
|
|
|
|
datas = [prng.randrange(2**dw0) for i in range(128)]
|
2018-11-17 11:29:45 -05:00
|
|
|
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)
|
2021-03-18 08:47:10 -04:00
|
|
|
|
|
|
|
def test_gearbox_20_32(self):
|
|
|
|
self.gearbox_test(20, 32)
|
|
|
|
|
|
|
|
def test_gearbox_10_2(self):
|
|
|
|
self.gearbox_test(10, 2)
|
|
|
|
|
|
|
|
def test_gearbox_10_4(self):
|
|
|
|
self.gearbox_test(10, 4)
|