"""Production-profile full-top integration over real Host and BMC UART bits.""" from __future__ import annotations import cocotb from cocotb.triggers import Timer, with_timeout from thermal_6400_uart import ( BMC_CLKS_PER_BIT, CLOCK_NS, HOST_CLKS_PER_BIT, SET_STIMULUS, SET_TARGET, SET_TARGET_ACK, bring_auto, check_pwm_timing, encode_frame, get_snapshot, host_transaction, pulse_tick_1s, receive_frame, send_bmc_snapshot, send_uart, wait_for, ) def duties(snapshot: dict[str, object], name: str) -> list[int]: value = snapshot[name] assert isinstance(value, list) return [int(item) for item in value] @cocotb.test() async def test_control_dual_host_sensor_safety_and_snapshot(dut): pwm_timing = cocotb.start_soon(check_pwm_timing(dut)) await bring_auto(dut) # Production must silently reject the identification-only command. rejected_payload = bytes([2, *([20] * 32)]) await send_uart( dut.host_rs232_rx, encode_frame(SET_STIMULUS, rejected_payload), HOST_CLKS_PER_BIT, ) await Timer(HOST_CLKS_PER_BIT * CLOCK_NS * 20, units="ns") assert int(dut.host_rs232_tx.value) == 1 assert int(dut.host_ttl_tx.value) == 1 assert int(dut.stimulus_active_mon.value) == 0 # Exercise simultaneous Host and BMC receive frontends. ACK is routed # only to RS-232 while the BMC commits a complete fifth snapshot. old_control_epoch = int(dut.control_epoch_mon.value) bmc_task = cocotb.start_soon(send_bmc_snapshot(dut, [61] * 24)) ack_task = cocotb.start_soon(receive_frame(dut.host_rs232_tx)) await send_uart(dut.host_rs232_rx, encode_frame(SET_TARGET, b"\x41"), HOST_CLKS_PER_BIT) ack_kind, ack_payload = await with_timeout(ack_task, 20, "ms") assert (ack_kind, ack_payload) == (SET_TARGET_ACK, b"") assert int(dut.host_ttl_tx.value) == 1 await bmc_task # A target change deliberately restarts CT/preheat qualification. The # concurrent BMC frame enters WAIT_BMC_STABLE; three further new epochs # are required before AUTO resumes. for _ in range(3): await send_bmc_snapshot(dut, [61] * 24) await wait_for( dut, lambda: int(dut.control_state_mon.value) == 4 and int(dut.control_epoch_mon.value) > old_control_epoch, cycles=1000, label="target/BMC control commit", ) await Timer(HOST_CLKS_PER_BIT * CLOCK_NS * 2, units="ns") snapshot = await get_snapshot(dut, "ttl") assert snapshot["control_state"] == 4 assert snapshot["current_target"] == 65 assert snapshot["host_target_seen"] == 1 assert int(snapshot["bmc_epoch"]) >= 5 assert int(snapshot["ct_epoch"]) >= 1 assert snapshot["bmc_valid_mask"] == (1 << 24) - 1 assert snapshot["ct_valid_mask"] == (1 << 27) - 1 assert duties(snapshot, "bmc_raw") == [0x3D00] * 24 assert duties(snapshot, "ct_raw") == [0x3C00] * 27 ptc = duties(snapshot, "ptc_applied") assert len(ptc) == 24 and all(0 <= value <= 100 for value in ptc) and any(ptc) assert duties(snapshot, "front_applied") == [30] * 4 assert duties(snapshot, "rear_applied") == [30] * 4 # A 74 C sample reduces only the matching PTC and raises both fans for # that lane to the contract's 80% protection duty. The public safety bit # remains telemetry and must not turn the soft action into a full cutoff. old_control_epoch = int(dut.control_epoch_mon.value) await send_bmc_snapshot(dut, [74, *([60] * 23)]) await wait_for( dut, lambda: int(dut.control_epoch_mon.value) > old_control_epoch, cycles=1000, label="74 C soft-limit commit", ) soft = await get_snapshot(dut, "rs232") soft_ptc = duties(soft, "ptc_applied") assert int(soft["safety_override_mask"]) & 1 assert ptc[0] > 1 and soft_ptc[0] == ptc[0] - 1 assert duties(soft, "front_applied") == [80, 30, 30, 30] assert duties(soft, "rear_applied") == [80, 30, 30, 30] # At 76 C the matching PTC is fully off, but the lane is not yet hard # faulted and therefore remains at 80% rather than 100% airflow. old_control_epoch = int(dut.control_epoch_mon.value) await send_bmc_snapshot(dut, [76, *([60] * 23)]) await wait_for( dut, lambda: int(dut.control_epoch_mon.value) > old_control_epoch, cycles=1000, label="76 C pretrip commit", ) pretrip = await get_snapshot(dut, "ttl") assert int(pretrip["safety_override_mask"]) & 1 assert not (int(pretrip["lane_fault_mask"]) & 1) assert duties(pretrip, "ptc_applied")[0] == 0 assert duties(pretrip, "front_applied") == [80, 30, 30, 30] assert duties(pretrip, "rear_applied") == [80, 30, 30, 30] # One invalid DIMM is quarantined without corrupting the other 23 values. await send_bmc_snapshot(dut, [None, *([60] * 23)]) await wait_for( dut, lambda: int(dut.control_state_mon.value) == 5 and bool(int(dut.safety_mask_mon.value) & 1), cycles=1000, label="single-channel invalid override", ) invalid = await get_snapshot(dut, "rs232") assert int(invalid["bmc_valid_mask"]) == ((1 << 24) - 2) assert int(invalid["safety_override_mask"]) & 1 assert duties(invalid, "ptc_applied")[0] == 0 # A 77 C sample latches the full L0 hard override and both lane fans. await send_bmc_snapshot(dut, [77, *([60] * 23)]) await wait_for( dut, lambda: bool(int(dut.lane_fault_mask_mon.value) & 1), cycles=1000, label="lane hard limit", ) hard = await get_snapshot(dut, "ttl") assert int(hard["lane_fault_mask"]) & 1 assert duties(hard, "ptc_applied")[:6] == [0] * 6 assert duties(hard, "front_applied")[0] == 100 assert duties(hard, "rear_applied")[0] == 100 # Three explicit age ticks without a new BMC epoch create global stale. await pulse_tick_1s(dut, 3) await wait_for( dut, lambda: int(dut.global_fault_mon.value) == 1, cycles=200, label="global BMC stale", ) stale = await get_snapshot(dut, "rs232") assert stale["global_fault"] == 1 assert duties(stale, "ptc_applied") == [0] * 24 assert duties(stale, "front_applied") == [100] * 4 assert duties(stale, "rear_applied") == [100] * 4 # Three complete safe epochs retain the clamp through the release commit; # the following epoch resumes only the normal slew-limited ramp. for _ in range(3): await send_bmc_snapshot(dut, [60] * 24) await wait_for( dut, lambda: int(dut.control_epoch_mon.value) >= 1, cycles=1000, label="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) == 4, cycles=1000, label="three-epoch recovery", ) released = await get_snapshot(dut, "ttl") assert duties(released, "ptc_applied") == [0] * 24 await send_bmc_snapshot(dut, [60] * 24) await wait_for( dut, lambda: int(dut.control_epoch_mon.value) > int(released["control_epoch"]), cycles=1000, label="post-recovery limited ramp", ) ramped = await get_snapshot(dut, "rs232") assert all(0 <= value <= 5 for value in duties(ramped, "ptc_applied")) assert any(duties(ramped, "ptc_applied")) kind, payload = await host_transaction(dut, "ttl", SET_TARGET, b"\x00") assert (kind, payload) == (SET_TARGET_ACK, b"") stopped = await get_snapshot(dut, "rs232") assert stopped["control_state"] == 6 assert duties(stopped, "ptc_applied") == [0] * 24 await pwm_timing