"""cocotb test for tmds_encoder.sv: every output character is compared with the Python model of the DVI 1.0 encode algorithm (tmds_model.py).""" from __future__ import annotations import random import cocotb from cocotb.clock import Clock from cocotb.triggers import ClockCycles, FallingEdge, RisingEdge from tmds_model import TmdsEncoder @cocotb.test() async def video_like_stream(dut) -> None: """Bursts of random pixels (DE high) between blanking runs with random C0/C1.""" cocotb.start_soon(Clock(dut.clk, 10, unit="ns").start()) rng = random.Random(1919) dut.rst.value = 1 dut.de.value = 0 dut.d.value = 0 dut.c0.value = 0 dut.c1.value = 0 await ClockCycles(dut.clk, 3) await FallingEdge(dut.clk) dut.rst.value = 0 model = TmdsEncoder() checked = 0 for _burst in range(400): active = rng.randint(1, 120) blank = rng.randint(1, 40) c0, c1 = rng.randrange(2), rng.randrange(2) # Half the bursts repeat one value, which stresses the DC balance. const = rng.randrange(256) if rng.random() < 0.5 else None for i in range(active + blank): de = int(i < active) d = (const if const is not None else rng.randrange(256)) if de else rng.randrange(256) dut.d.value, dut.de.value, dut.c0.value, dut.c1.value = d, de, c0, c1 expected = model.encode(d, c0, c1, de) await RisingEdge(dut.clk) await FallingEdge(dut.clk) got = int(dut.q_out.value) assert got == expected, f"character {checked}: got {got:010b}, expected {expected:010b}" checked += 1 dut._log.info(f"{checked} characters match the model")