2021-10-23 11:40:41 -04:00
|
|
|
#
|
|
|
|
# This file is part of LiteX.
|
|
|
|
#
|
2020-08-23 09:40:21 -04:00
|
|
|
# Copyright (c) 2020 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
2020-01-29 12:27:29 -05:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
import random
|
|
|
|
|
|
|
|
from migen import *
|
|
|
|
|
|
|
|
from litex.soc.interconnect.stream import *
|
|
|
|
|
|
|
|
|
2021-10-23 11:40:41 -04:00
|
|
|
class TestStream(unittest.TestCase):
|
|
|
|
def pipe_test(self, dut):
|
|
|
|
prng = random.Random(42)
|
|
|
|
def generator(dut, valid_rand=90):
|
|
|
|
for data in range(128):
|
|
|
|
yield dut.sink.valid.eq(1)
|
|
|
|
yield dut.sink.data.eq(data)
|
2020-01-29 12:27:29 -05:00
|
|
|
yield
|
2021-10-23 11:40:41 -04:00
|
|
|
while (yield dut.sink.ready) == 0:
|
|
|
|
yield
|
|
|
|
yield dut.sink.valid.eq(0)
|
|
|
|
while prng.randrange(100) < valid_rand:
|
|
|
|
yield
|
|
|
|
|
|
|
|
def checker(dut, ready_rand=90):
|
|
|
|
dut.errors = 0
|
|
|
|
for data in range(128):
|
|
|
|
yield dut.source.ready.eq(0)
|
2021-09-08 12:25:42 -04:00
|
|
|
yield
|
2021-10-23 11:40:41 -04:00
|
|
|
while (yield dut.source.valid) == 0:
|
|
|
|
yield
|
|
|
|
while prng.randrange(100) < ready_rand:
|
|
|
|
yield
|
|
|
|
yield dut.source.ready.eq(1)
|
2021-09-08 12:25:42 -04:00
|
|
|
yield
|
2021-10-23 11:40:41 -04:00
|
|
|
if ((yield dut.source.data) != data):
|
|
|
|
dut.errors += 1
|
|
|
|
yield
|
|
|
|
run_simulation(dut, [generator(dut), checker(dut)])
|
|
|
|
self.assertEqual(dut.errors, 0)
|
2020-01-29 12:27:29 -05:00
|
|
|
|
|
|
|
def test_pipe_valid(self):
|
2021-10-23 11:40:41 -04:00
|
|
|
dut = PipeValid([("data", 8)])
|
2020-01-29 12:27:29 -05:00
|
|
|
self.pipe_test(dut)
|
|
|
|
|
|
|
|
def test_pipe_ready(self):
|
2021-10-23 11:40:41 -04:00
|
|
|
dut = PipeReady([("data", 8)])
|
2020-01-29 12:27:29 -05:00
|
|
|
self.pipe_test(dut)
|
2022-09-07 02:58:51 -04:00
|
|
|
|
|
|
|
def test_buffer_valid(self):
|
|
|
|
dut = Buffer([("data", 8)], pipe_valid=True, pipe_ready=False)
|
|
|
|
self.pipe_test(dut)
|
|
|
|
|
|
|
|
def test_buffer_ready(self):
|
|
|
|
dut = Buffer([("data", 8)], pipe_valid=False, pipe_ready=True)
|
|
|
|
self.pipe_test(dut)
|
|
|
|
|
|
|
|
def test_buffer_valid_ready(self):
|
|
|
|
dut = Buffer([("data", 8)], pipe_valid=True, pipe_ready=True)
|
|
|
|
self.pipe_test(dut)
|