"""Focused full-UART test for the destructive 6400 Host protocol migration.""" from __future__ import annotations import cocotb from cocotb.triggers import RisingEdge, Timer, with_timeout from thermal_6400_uart import ( ACK_NACK, CLOCK_NS, GET_SNAPSHOT, HOST_CLKS_PER_BIT, SET_ALL, SET_PID_TARGET, SET_STIMULUS, SNAPSHOT_DATA, clock_cycles, encode_frame, receive_frame, send_uart, wait_for, ) async def transact(dut, kind: int, payload: bytes) -> tuple[int, bytes]: response = cocotb.start_soon(receive_frame(dut.host_rs232_tx)) await send_uart(dut.host_rs232_rx, encode_frame(kind, payload), HOST_CLKS_PER_BIT) result = await with_timeout(response, 25, "ms") await Timer(HOST_CLKS_PER_BIT * CLOCK_NS * 2, units="ns") await wait_for(dut, lambda: int(dut.host_locked_mon.value) == 0, cycles=1500, label="unlock") return result def ack(payload: bytes, ok: int, request: int, status: int, detail: int = 0) -> None: assert payload == bytes((ok, request, status, detail)) @cocotb.test() async def test_host_ack_nack_crc_silence_atomic_set_all_and_get_order(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 await with_timeout(RisingEdge(dut.rst_n_mon), 1, "ms") await clock_cycles(dut, 40) # Complete CRC-correct requests always get one unified response. kind, payload = await transact(dut, SET_ALL, bytes(31)) assert kind == ACK_NACK; ack(payload, 0, SET_ALL, 2, 32) kind, payload = await transact(dut, SET_STIMULUS, bytes((1,)) + bytes(32)) assert kind == ACK_NACK; ack(payload, 0, SET_STIMULUS, 1) kind, payload = await transact(dut, 0x55, b"") assert kind == ACK_NACK; ack(payload, 0, 0x55, 1) # Oversized requests are bounded streaming discards, not resync points. # The whole payload (including embedded A5 5A) and CRC must retire before # one length/unsupported response is emitted; no ghost command is formed. embedded = bytearray(34) embedded[7:9] = b"\xA5\x5A" before = int(dut.host_response_count_mon.value) kind, payload = await transact(dut, SET_ALL, bytes(embedded)) assert kind == ACK_NACK; ack(payload, 0, SET_ALL, 2, 32) assert int(dut.host_response_count_mon.value) == before + 1 oversized = bytearray((index * 17) & 0xFF for index in range(255)) oversized[120:122] = b"\xA5\x5A" before = int(dut.host_response_count_mon.value) kind, payload = await transact(dut, 0x55, bytes(oversized)) assert kind == ACK_NACK; ack(payload, 0, 0x55, 1) assert int(dut.host_response_count_mon.value) == before + 1 # First invalid index is reported and nothing enters Manual. bad = bytearray([0] * 24 + [20] * 8) bad[7] = 101 commits = int(dut.target_commit_count_mon.value) kind, payload = await transact(dut, SET_ALL, bytes(bad)) assert kind == ACK_NACK; ack(payload, 0, SET_ALL, 3, 7) assert int(dut.target_commit_count_mon.value) == commits assert int(dut.control_state_mon.value) == 0 # Legal and identical SET_ALL both atomically commit/feed and ACK. vector = bytes([50] * 24 + [0, 20, 50, 100, 0, 20, 50, 100]) for repeat in range(2): before = int(dut.target_commit_count_mon.value) kind, payload = await transact(dut, SET_ALL, vector) assert kind == ACK_NACK; ack(payload, 1, SET_ALL, 0) assert int(dut.target_commit_count_mon.value) == before + 1 assert int(dut.control_state_mon.value) == 1 # PID target ACKs but does not exit Manual or feed via a SET_ALL commit. before = int(dut.target_commit_count_mon.value) kind, payload = await transact(dut, SET_PID_TARGET, b"\x46") assert kind == ACK_NACK; ack(payload, 1, SET_PID_TARGET, 0) assert int(dut.current_target_mon.value) == 70 assert int(dut.control_state_mon.value) == 1 assert int(dut.target_commit_count_mon.value) == before # GET locks one snapshot and produces exactly 06 then 82. ack_task = cocotb.start_soon(receive_frame(dut.host_rs232_tx)) await send_uart(dut.host_rs232_rx, encode_frame(GET_SNAPSHOT), HOST_CLKS_PER_BIT) kind, payload = await with_timeout(ack_task, 25, "ms") assert kind == ACK_NACK; ack(payload, 1, GET_SNAPSHOT, 0) assert int(dut.host_locked_mon.value) == 1 data_kind, data = await with_timeout(receive_frame(dut.host_rs232_tx), 25, "ms") assert data_kind == SNAPSHOT_DATA and len(data) == 201 await wait_for(dut, lambda: int(dut.host_locked_mon.value) == 0, cycles=1500, label="GET unlock") # CRC damage and partial timeout are silent; the next legal request resyncs. responses = int(dut.host_response_count_mon.value) damaged = bytearray(encode_frame(SET_PID_TARGET, b"\x41")); damaged[-1] ^= 1 await send_uart(dut.host_rs232_rx, damaged, HOST_CLKS_PER_BIT) await clock_cycles(dut, 1200) assert int(dut.host_response_count_mon.value) == responses await send_uart(dut.host_rs232_rx, b"\xA5\x5A\x04\x01", HOST_CLKS_PER_BIT) await clock_cycles(dut, 1200) assert int(dut.host_response_count_mon.value) == responses kind, payload = await transact(dut, SET_PID_TARGET, b"\x41") assert kind == ACK_NACK; ack(payload, 1, SET_PID_TARGET, 0) assert int(dut.current_target_mon.value) == 65