| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- """Focused current-contract control integration over the production UART top."""
- from __future__ import annotations
- import cocotb
- from cocotb.triggers import RisingEdge, with_timeout
- from thermal_6400_uart import (
- ACK_NACK,
- SET_ALL,
- clock_cycles,
- get_snapshot,
- host_transaction,
- pulse_tick_1s,
- send_bmc_snapshot,
- wait_for,
- )
- def values(snapshot: dict[str, object], field: str) -> list[int]:
- data = snapshot[field]
- assert isinstance(data, list)
- return [int(item) for item in data]
- def check_ack(kind: int, payload: bytes, request: int, ok: int, status: int) -> None:
- assert kind == ACK_NACK
- assert payload == bytes((ok, request, status, 0))
- @cocotb.test()
- async def test_safe_manual_pid_and_lane_safety(dut):
- 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.reset_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
- boot = await get_snapshot(dut, "ttl")
- assert values(boot, "ptc_applied") == [0] * 24
- assert values(boot, "front_applied") == [20] * 4
- assert values(boot, "rear_applied") == [20] * 4
- requested = bytes([40] * 24 + [0, 20, 50, 100, 100, 50, 20, 0])
- kind, payload = await host_transaction(dut, "rs232", SET_ALL, requested)
- check_ack(kind, payload, SET_ALL, 1, 0)
- assert int(dut.control_state_mon.value) == 1
- manual = await get_snapshot(dut, "ttl")
- assert values(manual, "ptc_requested") == [40] * 24
- assert values(manual, "ptc_applied") == [40] * 24
- assert values(manual, "front_applied") == [0, 20, 50, 100]
- assert values(manual, "rear_applied") == [100, 50, 20, 0]
- await send_bmc_snapshot(dut, [76, *([60] * 23)])
- await wait_for(dut, lambda: bool(int(dut.safety_mask_mon.value) & 1),
- cycles=1200, label="76 C clamp")
- clamped = await get_snapshot(dut, "rs232")
- assert int(dut.control_state_mon.value) == 1
- assert values(clamped, "ptc_applied")[0] == 0
- assert values(clamped, "ptc_applied")[1:] == [40] * 23
- await send_bmc_snapshot(dut, [77, *([60] * 23)])
- await wait_for(dut, lambda: bool(int(dut.lane_fault_mask_mon.value) & 1),
- cycles=1200, label="77 C lane latch")
- hard = await get_snapshot(dut, "ttl")
- assert int(hard["lane_fault_mask"]) & 1
- assert values(hard, "ptc_applied")[:6] == [0] * 6
- assert values(hard, "front_applied")[0] == 100
- assert values(hard, "rear_applied")[0] == 100
- kind, payload = await host_transaction(dut, "rs232", SET_ALL, requested)
- check_ack(kind, payload, SET_ALL, 0, 4)
- for _ in range(3):
- await send_bmc_snapshot(dut, [72] * 24)
- await wait_for(dut, lambda: int(dut.lane_fault_mask_mon.value) == 0,
- cycles=2400, label="three-epoch hard release")
- assert int(dut.control_state_mon.value) == 2
- await pulse_tick_1s(dut, 3)
- await wait_for(dut, lambda: int(dut.global_fault_mon.value) == 1,
- cycles=1200, label="PID stale hold")
- stale = await get_snapshot(dut, "ttl")
- assert values(stale, "ptc_applied") == [0] * 24
- assert values(stale, "front_applied") == [20] * 4
- assert values(stale, "rear_applied") == [20] * 4
- epoch = int(dut.control_epoch_mon.value)
- await pulse_tick_1s(dut, 4)
- await clock_cycles(dut, 100)
- assert int(dut.control_epoch_mon.value) == epoch
|