"""cocotb test for lfsr.sv (built as a 32-bit LFSR): compare every state with lfsr_model.py.""" from __future__ import annotations import random import cocotb from cocotb.clock import Clock from cocotb.triggers import ClockCycles, FallingEdge, ReadOnly, RisingEdge from lfsr_model import galois_step @cocotb.test() async def matches_model(dut) -> None: width = len(dut.state) mask = int(dut.MASK.value) seed = int(dut.SEED.value) rng = random.Random(32) cocotb.start_soon(Clock(dut.clk, 10, unit="ns").start()) dut.rst.value = 1 dut.en.value = 0 await ClockCycles(dut.clk, 2) await FallingEdge(dut.clk) dut.rst.value = 0 expected = seed for _ in range(5000): en = int(rng.random() < 0.8) dut.en.value = en await RisingEdge(dut.clk) await ReadOnly() if en: expected = galois_step(expected, mask) assert dut.state.value.to_unsigned() == expected assert int(dut.bit_out.value) == expected & 1 await FallingEdge(dut.clk) assert width == 32