"""L11 accelerated randomized full-top soak with deterministic coverage.""" from __future__ import annotations import json import os from pathlib import Path import random import cocotb from cocotb.triggers import RisingEdge, Timer, with_timeout from thermal_6400_uart import ( BMC_CLKS_PER_BIT, CLOCK_NS, GET_SNAPSHOT, HOST_CLKS_PER_BIT, ACK_NACK, SET_ALL, SET_PID_TARGET, clock_cycles, encode_frame, get_snapshot, host_transaction, pulse_tick_1s, send_bmc_snapshot, send_uart, wait_for, ) SEED = int(os.environ.get("PROD2U_SOAK_SEED", "1677779166")) ITERATIONS = int(os.environ.get("PROD2U_SOAK_ITERATIONS", "96")) def values(snapshot: dict[str, object], name: str) -> list[int]: value = snapshot[name] assert isinstance(value, list) return [int(item) for item in value] async def wait_control_epoch(dut, previous: int, label: str) -> None: await wait_for( dut, lambda: int(dut.control_epoch_mon.value) != previous, cycles=1200, label=label, ) async def wait_host_idle(dut) -> None: await wait_for( dut, lambda: int(dut.host_locked_mon.value) == 0, cycles=6000, label="Host selector idle unlock", ) async def initialize_current(dut) -> None: dut.host_rs232_rx.value = 1 dut.host_ttl_rx.value = 1 dut.bmc_rs232_rx.value = 1 dut.tick_1s_test.value = 0 dut.bmc_epoch_override_enable.value = 0 dut.bmc_epoch_override_value.value = 0 await with_timeout(RisingEdge(dut.rst_n_mon), 1, "ms") await clock_cycles(dut, 50) assert int(dut.control_state_mon.value) == 0 async def set_pid_target(dut, port: str, target: int) -> None: try: kind, payload = await host_transaction(dut, port, SET_PID_TARGET, bytes([target])) except BaseException: cocotb.log.error( "SET_PID_TARGET_TIMEOUT port=%s target=%d locked=%d selected=%d " "rx_valid=%d rx_error=%d frame_valid=%d frame_ready=%d command_busy=%d " "target_commits=%d responses=%d", port, target, int(dut.u_dut.host_locked.value), int(dut.u_dut.host_port_sel.value), int(dut.u_dut.host_rx_valid.value), int(dut.u_dut.host_rx_error.value), int(dut.u_dut.frame_valid.value), int(dut.u_dut.frame_ready.value), int(dut.u_dut.host_command_busy.value), int(dut.target_commit_count_mon.value), int(dut.host_response_count_mon.value), ) raise assert (kind, payload) == (ACK_NACK, bytes((1, SET_PID_TARGET, 0, 0))) async def set_all(dut, port: str, vector: bytes) -> None: kind, payload = await host_transaction(dut, port, SET_ALL, vector) assert (kind, payload) == (ACK_NACK, bytes((1, SET_ALL, 0, 0))) async def send_noise(dut, rng: random.Random) -> None: await wait_host_idle(dut) before_commit = int(dut.target_commit_count_mon.value) before_response = int(dut.host_response_count_mon.value) # A bad-CRC target-shaped frame plus arbitrary bytes must not commit or # produce a response. Let the bounded idle unlock recover the selected port. corrupt = bytearray(encode_frame(SET_PID_TARGET, bytes([rng.randint(55, 70)]))) corrupt[-1] ^= 0x80 port = "rs232" if rng.randrange(2) == 0 else "ttl" await send_uart(getattr(dut, f"host_{port}_rx"), corrupt, HOST_CLKS_PER_BIT) await send_uart(getattr(dut, f"host_{port}_rx"), bytes(rng.randrange(256) for _ in range(5)), HOST_CLKS_PER_BIT) await clock_cycles(dut, 1400) await wait_host_idle(dut) assert int(dut.target_commit_count_mon.value) == before_commit assert int(dut.host_response_count_mon.value) == before_response async def recover_safe(dut) -> None: for _ in range(4): previous = int(dut.control_epoch_mon.value) await send_bmc_snapshot(dut, [60] * 24) if int(dut.control_state_mon.value) == 2: await wait_control_epoch(dut, previous, "soak recovery control pass") await wait_for( dut, lambda: int(dut.global_fault_mon.value) == 0 and int(dut.lane_fault_mask_mon.value) == 0 and int(dut.control_state_mon.value) == 2, cycles=1600, label="soak recovery PID", ) async def validate_snapshot(dut, port: str, counters: dict[str, int]) -> dict[str, object]: await wait_host_idle(dut) snapshot = await get_snapshot(dut, port) assert snapshot["mode"] in range(3) for name in ("ptc_requested", "ptc_applied", "front_requested", "front_applied", "rear_requested", "rear_applied"): assert all(0 <= value <= 100 for value in values(snapshot, name)) lane_faults = int(snapshot["lane_fault_mask"]) if lane_faults: for lane in range(4): if lane_faults & (1 << lane): assert values(snapshot, "ptc_applied")[lane * 6 : lane * 6 + 6] == [0] * 6 assert values(snapshot, "front_applied")[lane] == 100 assert values(snapshot, "rear_applied")[lane] == 100 elif snapshot["global_fault"]: assert values(snapshot, "ptc_applied") == [0] * 24 assert values(snapshot, "front_applied") == [20] * 4 assert values(snapshot, "rear_applied") == [20] * 4 assert int(dut.pwm_deadline_miss_mon.value) == 0 counters["get_snapshot"] += 1 return snapshot @cocotb.test() async def test_accelerated_randomized_system_soak(dut): rng = random.Random(SEED) counters = { "bmc_random": 0, "invalid": 0, "stale": 0, "get_snapshot": 0, "set_pid_target": 0, "repeated_set": 0, "set_all": 0, "recovery": 0, "epoch_wrap": 0, "uart_noise": 0, "reset": 0, "hard_limit": 0, "state_transitions": 0, "virtual_seconds": 0, } dut.reset_test.value = 0 previous_state = 0 await initialize_current(dut) await pulse_tick_1s(dut, 30) await wait_for(dut, lambda: int(dut.control_state_mon.value) == 2, cycles=2400, label="SAFE watchdog atomic handoff to PID") for _ in range(4): await send_bmc_snapshot(dut, [60] * 24) # The soak-only wrapper starts at 0xfffa, so the first four real BMC # frames leave the active epoch at 0xfffe. Two more frames cross the wrap. assert int(dut.bmc_epoch_mon.value) == 0xFFFE await send_bmc_snapshot(dut, [60] * 24) assert int(dut.bmc_epoch_mon.value) == 0xFFFF await send_bmc_snapshot(dut, [60] * 24) assert int(dut.bmc_epoch_mon.value) == 0 counters["epoch_wrap"] += 1 for iteration in range(ITERATIONS): action = iteration % 8 cocotb.log.info( "SOAK_ITERATION iteration=%d action=%d state=%d host_locked=%d bmc_epoch=%d", iteration, action, int(dut.control_state_mon.value), int(dut.host_locked_mon.value), int(dut.bmc_epoch_mon.value), ) if action == 0: temperatures: list[int | None] = [rng.randint(54, 75) for _ in range(24)] if iteration % 16 == 0: temperatures[rng.randrange(24)] = None counters["invalid"] += 1 previous = int(dut.control_epoch_mon.value) await send_bmc_snapshot(dut, temperatures) await wait_control_epoch(dut, previous, "random BMC control pass") counters["bmc_random"] += 1 elif action == 1: port = "rs232" if iteration & 1 else "ttl" target = rng.choice((58, 63, 67, 70)) await wait_host_idle(dut) await set_pid_target(dut, port, target) await wait_host_idle(dut) await set_pid_target(dut, port, target) counters["set_pid_target"] += 2 counters["repeated_set"] += 1 await recover_safe(dut) counters["recovery"] += 1 elif action == 2: await pulse_tick_1s(dut, 3) counters["virtual_seconds"] += 3 await wait_for(dut, lambda: int(dut.global_fault_mon.value) == 1, cycles=300, label="random stale clamp") stale = await validate_snapshot(dut, "rs232", counters) assert values(stale, "ptc_applied") == [0] * 24 counters["stale"] += 1 await recover_safe(dut) counters["recovery"] += 1 elif action == 3: await validate_snapshot(dut, "ttl" if iteration & 1 else "rs232", counters) elif action == 4: ptc = [rng.randint(0, 45) for _ in range(24)] front = [rng.randint(0, 100) for _ in range(4)] rear = [rng.randint(0, 100) for _ in range(4)] vector = bytes([*ptc, *front, *rear]) await wait_host_idle(dut) await set_all(dut, "rs232", vector) assert int(dut.control_state_mon.value) == 1 snap = await validate_snapshot(dut, "ttl", counters) assert values(snap, "ptc_requested") == ptc assert values(snap, "front_requested") == front assert values(snap, "rear_requested") == rear counters["set_all"] += 1 elif action == 5: await send_noise(dut, rng) counters["uart_noise"] += 1 elif action == 6: lane = rng.randrange(4) temperatures = [60] * 24 temperatures[lane * 6 + rng.randrange(6)] = 77 previous = int(dut.control_epoch_mon.value) await send_bmc_snapshot(dut, temperatures) await wait_control_epoch(dut, previous, "hard-limit control pass") assert int(dut.lane_fault_mask_mon.value) & (1 << lane) snap = await validate_snapshot(dut, "ttl", counters) assert values(snap, "ptc_applied")[lane * 6 : lane * 6 + 6] == [0] * 6 assert values(snap, "front_applied")[lane] == 100 assert values(snap, "rear_applied")[lane] == 100 counters["hard_limit"] += 1 await recover_safe(dut) counters["recovery"] += 1 else: # Two deterministic mid-run global resets/reconfigurations. if iteration in (31, 63): dut.reset_test.value = 1 await clock_cycles(dut, 4) dut.reset_test.value = 0 await initialize_current(dut) await pulse_tick_1s(dut, 30) for _ in range(4): await send_bmc_snapshot(dut, [60] * 24) await wait_for(dut, lambda: int(dut.control_state_mon.value) == 2, cycles=2400, label="PID after reset") counters["reset"] += 1 else: await validate_snapshot(dut, "rs232", counters) state = int(dut.control_state_mon.value) if state != previous_state: counters["state_transitions"] += 1 previous_state = state assert state in range(3) assert int(dut.pwm_deadline_miss_mon.value) == 0 final_snapshot = await validate_snapshot(dut, "ttl", counters) assert final_snapshot["control_epoch"] >= 1 required_nonzero = ( "bmc_random", "invalid", "stale", "get_snapshot", "set_pid_target", "repeated_set", "set_all", "recovery", "epoch_wrap", "uart_noise", "reset", "hard_limit", "state_transitions", "virtual_seconds", ) assert all(counters[name] > 0 for name in required_nonzero), counters result = { "schema": "prod2u-6400-soak-result-v1", "seed": SEED, "iterations": ITERATIONS, "counters": counters, "final_control_epoch": int(final_snapshot["control_epoch"]), "final_bmc_epoch": int(final_snapshot["bmc_epoch"]), "status": "PASS", } result_path = Path(os.environ["PROD2U_SOAK_RESULT"]) result_path.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8") cocotb.log.info("L11_SOAK_PASS %s", json.dumps(result, sort_keys=True))