"""cocotb test for the 1011 sequence detector (seq_detect_moore.sv).""" from __future__ import annotations import random import cocotb from cocotb.clock import Clock from cocotb.triggers import ClockCycles, FallingEdge, ReadOnly, RisingEdge from models import SeqDetectModel async def send(dut, model: SeqDetectModel, bits: list[int]) -> int: """Send bits one per clock and check detect after every edge. Returns the number of matches seen.""" matches = 0 for bit in bits: await FallingEdge(dut.clk) dut.din.value = bit await RisingEdge(dut.clk) model.step(bit) await ReadOnly() assert int(dut.detect.value) == model.detect, ( f"history {list(model.history)}: detect = {dut.detect.value}") matches += model.detect return matches @cocotb.test() async def detect_1011(dut) -> None: cocotb.start_soon(Clock(dut.clk, 10, unit="ns").start()) dut.rst.value = 1 dut.din.value = 0 await ClockCycles(dut.clk, 3) await FallingEdge(dut.clk) dut.rst.value = 0 model = SeqDetectModel() directed = [1, 0, 1, 1] + [1, 0, 1, 1, 0, 1, 1] + [1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1] n_directed = await send(dut, model, directed) assert n_directed == 4, f"expected 4 matches in the directed bits, saw {n_directed}" n_random = await send(dut, model, [random.getrandbits(1) for _ in range(2000)]) assert n_random > 50, "random stimulus produced too few matches to be useful" dut._log.info(f"{n_directed} directed and {n_random} random matches checked")