| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- """Identification-profile stimulus lifetime and safety integration."""
- from __future__ import annotations
- import cocotb
- from cocotb.triggers import ReadOnly, RisingEdge
- from thermal_6400_uart import (
- SET_STIMULUS,
- SET_STIMULUS_ACK,
- HOST_CLKS_PER_BIT,
- bring_auto,
- encode_frame,
- get_snapshot,
- host_transaction,
- pulse_tick_1s,
- send_uart,
- send_bmc_snapshot,
- wait_for,
- )
- def array(snapshot: dict[str, object], name: str) -> list[int]:
- value = snapshot[name]
- assert isinstance(value, list)
- return [int(item) for item in value]
- async def send_silent_invalid_stimulus(dut, port: str, payload: bytes) -> None:
- responses_before = int(dut.host_response_count_mon.value)
- active_bank_before = int(dut.u_dut.u_control.stimulus_bank_active.value)
- await send_uart(
- getattr(dut, f"host_{port}_rx"),
- encode_frame(SET_STIMULUS, payload),
- HOST_CLKS_PER_BIT,
- )
- await wait_for(
- dut,
- lambda: int(dut.host_locked_mon.value) == 0
- and int(dut.u_dut.u_command.busy.value) == 0,
- cycles=200,
- label=f"invalid stimulus silent retirement on {port}",
- )
- assert int(dut.host_response_count_mon.value) == responses_before
- assert int(dut.u_dut.u_control.stimulus_bank_active.value) == active_bank_before
- assert int(dut.stimulus_active_mon.value) == 1
- assert int(getattr(dut, f"host_{port}_tx").value) == 1
- @cocotb.test()
- async def test_identification_stimulus_expires_and_safety_preempts(dut):
- await bring_auto(dut)
- ptc = [20 + (index % 3) for index in range(24)]
- front = [20, 30, 40, 50]
- rear = [50, 40, 30, 20]
- stimulus = bytes([2, *ptc, *front, *rear])
- responses_before_repeated_stimulus = int(dut.host_response_count_mon.value)
- for transaction_index in (1, 2):
- kind, payload = await host_transaction(dut, "rs232", SET_STIMULUS, stimulus)
- assert (kind, payload) == (SET_STIMULUS_ACK, b"")
- assert int(dut.host_response_count_mon.value) == (
- responses_before_repeated_stimulus + transaction_index
- )
- assert int(dut.stimulus_active_mon.value) == 1
- old_control_epoch = int(dut.control_epoch_mon.value)
- await send_bmc_snapshot(dut, [60] * 24)
- await wait_for(
- dut,
- lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
- cycles=1000,
- label="stimulus output commit",
- )
- active = await get_snapshot(dut, "ttl")
- assert array(active, "ptc_requested") == ptc
- assert array(active, "ptc_applied") == ptc
- assert array(active, "front_requested") == front
- assert array(active, "front_applied") == front
- assert array(active, "rear_requested") == rear
- assert array(active, "rear_applied") == rear
- # Duration zero and one out-of-range duty both dirty only the inactive
- # staging bank. They are silently rejected: no ACK, no active pointer
- # flip, and the already-running A vector remains intact.
- await send_silent_invalid_stimulus(
- dut, "rs232", bytes([0, *([83] * 32)])
- )
- invalid_duty = [67] * 32
- invalid_duty[15] = 101
- await send_silent_invalid_stimulus(
- dut, "ttl", bytes([2, *invalid_duty])
- )
- still_a = await get_snapshot(dut, "rs232")
- assert array(still_a, "ptc_applied") == ptc
- assert array(still_a, "front_applied") == front
- assert array(still_a, "rear_applied") == rear
- # Launch a different legal B vector, then admit a new BMC epoch while the
- # command is still staging its bytes. The running control pass must lock
- # A. The 0x83 ACK is fresh and the pointer flips mid-pass, but every read
- # in that pass remains A; the following pass is wholly B.
- ptc_b = [41 + (index % 5) for index in range(24)]
- front_b = [61, 62, 63, 64]
- rear_b = [71, 72, 73, 74]
- stimulus_b = bytes([2, *ptc_b, *front_b, *rear_b])
- responses_before_b = int(dut.host_response_count_mon.value)
- old_active_bank = int(dut.u_dut.u_control.stimulus_bank_active.value)
- old_control_epoch = int(dut.control_epoch_mon.value)
- b_transaction = cocotb.start_soon(
- host_transaction(dut, "rs232", SET_STIMULUS, stimulus_b)
- )
- await wait_for(
- dut,
- lambda: int(dut.u_dut.u_command.state.value) == 4
- and 4 <= int(dut.u_dut.u_command.index.value) <= 12,
- cycles=15000,
- label="B stimulus staging window",
- )
- forced_epoch = (int(dut.bmc_epoch_mon.value) + 1) & 0xFFFF
- dut.bmc_epoch_override_value.value = forced_epoch
- dut.bmc_epoch_override_enable.value = 1
- await wait_for(
- dut,
- lambda: int(dut.u_dut.u_control.work_state.value) == 2
- and int(dut.u_dut.u_control.work_index.value) == 0,
- cycles=200,
- label="A-locked control pass",
- )
- assert int(dut.u_dut.u_control.stimulus_pass_bank.value) == old_active_bank
- assert int(dut.u_dut.u_control.stimulus_pass_running.value) == 1
- vector_a = [*ptc, *front, *rear]
- saw_b_commit_mid_pass = False
- for _ in range(500):
- await RisingEdge(dut.sys_clk)
- await ReadOnly()
- if int(dut.control_epoch_mon.value) != old_control_epoch:
- break
- work_state = int(dut.u_dut.u_control.work_state.value)
- work_index = int(dut.u_dut.u_control.work_index.value)
- assert int(dut.u_dut.u_control.stimulus_pass_bank.value) == old_active_bank
- assert int(dut.u_dut.u_control.stimulus_pass_running.value) == 1
- if work_state in (1, 2, 3, 4, 5, 6, 8, 10):
- assert int(dut.u_dut.u_control.stimulus_pass_data.value) == vector_a[work_index]
- if int(dut.u_dut.u_control.stimulus_bank_active.value) != old_active_bank:
- saw_b_commit_mid_pass = True
- assert work_state != 0
- else:
- raise AssertionError("A-locked pass did not commit")
- assert saw_b_commit_mid_pass
- kind, payload = await b_transaction
- assert (kind, payload) == (SET_STIMULUS_ACK, b"")
- assert int(dut.host_response_count_mon.value) == responses_before_b + 1
- first_after_b_commit = await get_snapshot(dut, "ttl")
- assert array(first_after_b_commit, "ptc_applied") == ptc
- assert array(first_after_b_commit, "front_applied") == front
- assert array(first_after_b_commit, "rear_applied") == rear
- old_control_epoch = int(dut.control_epoch_mon.value)
- forced_epoch = (forced_epoch + 1) & 0xFFFF
- dut.bmc_epoch_override_value.value = forced_epoch
- await wait_for(
- dut,
- lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
- cycles=1000,
- label="first wholly-B control pass",
- )
- active_b = await get_snapshot(dut, "rs232")
- assert array(active_b, "ptc_requested") == ptc_b
- assert array(active_b, "ptc_applied") == ptc_b
- assert array(active_b, "front_applied") == front_b
- assert array(active_b, "rear_applied") == rear_b
- ptc, front, rear = ptc_b, front_b, rear_b
- release_control_epoch = int(dut.control_epoch_mon.value)
- dut.bmc_epoch_override_enable.value = 0
- await wait_for(
- dut,
- lambda: int(dut.control_epoch_mon.value) > release_control_epoch,
- cycles=1000,
- label="epoch-hook release pass",
- )
- await pulse_tick_1s(dut)
- assert int(dut.stimulus_active_mon.value) == 1
- await pulse_tick_1s(dut)
- assert int(dut.stimulus_active_mon.value) == 0
- old_control_epoch = int(dut.control_epoch_mon.value)
- await send_bmc_snapshot(dut, [60] * 24)
- await wait_for(
- dut,
- lambda: int(dut.control_epoch_mon.value) > old_control_epoch,
- cycles=1000,
- label="autonomous resume after stimulus expiry",
- )
- resumed = await get_snapshot(dut, "rs232")
- assert resumed["control_state"] == 4
- assert array(resumed, "ptc_applied") != ptc
- # A fresh stimulus is terminated immediately by the 77 C lane override.
- stimulus = bytes([3, *([35] * 24), *([25] * 4), *([45] * 4)])
- kind, payload = await host_transaction(dut, "ttl", SET_STIMULUS, stimulus)
- assert (kind, payload) == (SET_STIMULUS_ACK, b"")
- assert int(dut.stimulus_active_mon.value) == 1
- # Per-channel thermal protection has priority over an active stimulus.
- 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="soft-limit stimulus preemption",
- )
- soft = await get_snapshot(dut, "rs232")
- assert int(dut.stimulus_active_mon.value) == 1
- assert array(soft, "ptc_applied")[0] < 35
- assert array(soft, "ptc_applied")[1:6] == [35] * 5
- assert array(soft, "front_applied")[0] == 80
- assert array(soft, "rear_applied")[0] == 80
- 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="pretrip stimulus preemption",
- )
- pretrip = await get_snapshot(dut, "ttl")
- assert int(dut.stimulus_active_mon.value) == 1
- assert array(pretrip, "ptc_applied")[0] == 0
- assert array(pretrip, "front_applied")[0] == 80
- assert array(pretrip, "rear_applied")[0] == 80
- await send_bmc_snapshot(dut, [77, *([60] * 23)])
- await wait_for(
- dut,
- lambda: int(dut.stimulus_active_mon.value) == 0
- and bool(int(dut.lane_fault_mask_mon.value) & 1),
- cycles=1000,
- label="hard-limit stimulus preemption",
- )
- hard = await get_snapshot(dut, "rs232")
- assert array(hard, "ptc_applied")[:6] == [0] * 6
- assert array(hard, "front_applied")[0] == 100
- assert array(hard, "rear_applied")[0] == 100
|