"""cocotb tests for health_tests.sv against the Python model in sp80090b_health.py. For every seeded source the test records the sample index at which rct_fail and apt_fail first rise, and compares it with the first failure index of the model. The parameters come from environment variables set by test_cocotb_health.py. These coroutines run inside the simulator; start them with pytest. """ from __future__ import annotations import os import numpy as np import cocotb from cocotb.clock import Clock from cocotb.triggers import FallingEdge, ReadOnly, RisingEdge import sp80090b_health as H SW = int(os.environ.get("HT_SW", "1")) RCT_C = int(os.environ.get("HT_RCT_C", "21")) APT_W = int(os.environ.get("HT_APT_W", "1024")) APT_C = int(os.environ.get("HT_APT_C", "589")) STARTUP = int(os.environ.get("HT_STARTUP", "1024")) async def reset(dut) -> None: dut.rst.value = 1 dut.sample_valid.value = 0 dut.sample.value = 0 for _ in range(2): await RisingEdge(dut.clk) await FallingEdge(dut.clk) dut.rst.value = 0 async def feed(dut, samples: np.ndarray, rng: np.random.Generator | None = None) -> dict: """Send the samples (with optional idle cycles). Return first failure indices and ready info.""" first = {"rct": None, "apt": None, "ready_at": None} for idx, s in enumerate(samples): if rng is not None and rng.random() < 0.1: await FallingEdge(dut.clk) dut.sample_valid.value = 0 await RisingEdge(dut.clk) await FallingEdge(dut.clk) dut.sample_valid.value = 1 dut.sample.value = int(s) await RisingEdge(dut.clk) await ReadOnly() if first["rct"] is None and int(dut.rct_fail.value): first["rct"] = idx if first["apt"] is None and int(dut.apt_fail.value): first["apt"] = idx if first["ready_at"] is None and int(dut.ready.value): first["ready_at"] = idx await FallingEdge(dut.clk) dut.sample_valid.value = 0 return first def model(samples: np.ndarray) -> dict: r = H.rct_failures(samples, RCT_C) a = H.apt_failures(samples, APT_C, APT_W) return {"rct": r[0] if r else None, "apt": a[0] if a else None} def sources(n: int) -> dict[str, np.ndarray]: """Seeded test streams. Non-binary sources draw SW-bit symbols.""" if SW == 1: return { "fair": H.biased_bits(0.5, n, 101), "p=0.60": H.biased_bits(0.60, n, 102), "p=0.75": H.biased_bits(0.75, n, 103), "stuck at 1": H.stuck_bits(1, n), "alternating": H.periodic_bits([0, 1], n), "fair, then p=0.80": np.concatenate([H.biased_bits(0.5, 2 * APT_W + 300, 106), H.biased_bits(0.8, n - 2 * APT_W - 300, 107)]), } rng = np.random.default_rng(104) k = 1 << SW skew = np.full(k, 0.5 / (k - 1)) skew[3] = 0.5 # one symbol takes half the probability return { "uniform": rng.integers(0, k, size=n), "one symbol at 0.5": rng.choice(k, size=n, p=skew), "stuck at 5": H.stuck_bits(5, n), "cycle 0..k-1": H.periodic_bits(list(range(k)), n), "uniform, then skewed": np.concatenate([rng.integers(0, k, size=2 * APT_W + 300), rng.choice(k, size=n - 2 * APT_W - 300, p=skew)]), } @cocotb.test() async def first_failures_match_model(dut) -> None: Clock(dut.clk, 10, unit="ns").start() n = 4 * APT_W + 37 for name, s in sources(n).items(): await reset(dut) got = await feed(dut, s) expect = model(s) assert got["rct"] == expect["rct"], f"{name}: RCT first failure {got['rct']}, model {expect['rct']}" assert got["apt"] == expect["apt"], f"{name}: APT first failure {got['apt']}, model {expect['apt']}" healthy = expect["rct"] is None and expect["apt"] is None if healthy: assert got["ready_at"] == STARTUP - 1, f"{name}: ready at {got['ready_at']}" dut._log.info(f"SW={SW} {name:18s} RCT {expect['rct']} APT {expect['apt']} ready {got['ready_at']}") @cocotb.test() async def idle_cycles_do_not_count(dut) -> None: """Random gaps in sample_valid must not change the result.""" Clock(dut.clk, 10, unit="ns").start() rng = np.random.default_rng(105) for name, s in list(sources(4 * APT_W + 37).items())[1:3]: await reset(dut) got = await feed(dut, s, rng) expect = model(s) assert (got["rct"], got["apt"]) == (expect["rct"], expect["apt"]), name @cocotb.test() async def reset_clears_sticky_failures(dut) -> None: Clock(dut.clk, 10, unit="ns").start() await reset(dut) stuck = H.stuck_bits(1 if SW == 1 else 5, RCT_C + 2) got = await feed(dut, stuck) assert got["rct"] == RCT_C - 1 await reset(dut) await ReadOnly() assert int(dut.rct_fail.value) == 0 and int(dut.apt_fail.value) == 0 and int(dut.ready.value) == 0