`timescale 1ns/1ps module thermal_host_command_v2_tb; reg clk = 0, rst_n = 0, frame_valid = 0, tx_done = 0; reg [7:0] frame_type = 0, frame_length = 0; wire [7:0] payload_addr; reg [7:0] payload [0:32]; reg safety_locked = 0, manual_done = 0, manual_ok = 1, snapshot_ready = 0; wire manual_wr, manual_commit, pid_commit, snapshot_capture; wire [4:0] manual_wr_addr; wire [7:0] manual_wr_data, pid_target; wire tx_start, transaction_done, busy; wire [7:0] tx_type, tx_length; reg [7:0] tx_payload_addr = 0; wire [7:0] ack_data; integer writes = 0, i; always #5 clk = ~clk; always @(posedge clk) if (manual_wr) writes <= writes + 1; thermal_host_command #(.SNAPSHOT_LENGTH(201)) dut ( .clk(clk), .rst_n(rst_n), .frame_valid(frame_valid), .frame_ready(), .frame_accept_enable(), .frame_type(frame_type), .frame_length(frame_length), .payload_read_addr(payload_addr), .payload_read_data(payload[payload_addr]), .manual_wr_en(manual_wr), .manual_wr_addr(manual_wr_addr), .manual_wr_data(manual_wr_data), .manual_commit(manual_commit), .manual_commit_done(manual_done), .manual_commit_ok(manual_ok), .safety_locked(safety_locked), .pid_target_commit(pid_commit), .pid_target_celsius(pid_target), .snapshot_capture(snapshot_capture), .snapshot_ready(snapshot_ready), .tx_start(tx_start), .tx_frame_type(tx_type), .tx_payload_length(tx_length), .tx_payload_addr(tx_payload_addr), .ack_payload_data(ack_data), .tx_done(tx_done), .transaction_done(transaction_done), .busy(busy) ); task request; input [7:0] kind; input [7:0] length; begin while (busy) @(negedge clk); @(negedge clk); frame_type = kind; frame_length = length; frame_valid = 1; @(negedge clk); frame_valid = 0; end endtask task expect_ack; input [7:0] ok; input [7:0] request_kind; input [7:0] status; input [7:0] detail; integer timeout; begin timeout = 0; while (!tx_start && timeout < 300) begin @(negedge clk); timeout = timeout + 1; end if (timeout == 300 || tx_type != 8'h06 || tx_length != 4) $fatal(1, "ACK timeout/type state=%0d req=%h len=%0d tx_start=%b tx_type=%h tx_len=%0d", dut.state, frame_type, frame_length, tx_start, tx_type, tx_length); tx_payload_addr = 0; #1; if (ack_data != ok) $fatal; tx_payload_addr = 1; #1; if (ack_data != request_kind) $fatal; tx_payload_addr = 2; #1; if (ack_data != status) $fatal; tx_payload_addr = 3; #1; if (ack_data != detail) $fatal; @(negedge clk); tx_done = 1; @(negedge clk); tx_done = 0; end endtask initial begin for (i = 0; i < 33; i = i + 1) payload[i] = 0; repeat (3) @(negedge clk); rst_n = 1; request(8'h01, 31); expect_ack(0, 8'h01, 2, 32); request(8'h03, 33); expect_ack(0, 8'h03, 1, 0); request(8'h55, 0); expect_ack(0, 8'h55, 1, 0); payload[7] = 101; writes = 0; request(8'h01, 32); expect_ack(0, 8'h01, 3, 7); if (writes != 0 || manual_commit) $fatal(1, "bad SET_ALL partially wrote"); // A new lock after validation but before Manual Bank commit is // fail-closed and never emits manual_commit or ACK OK. for (i = 0; i < 32; i = i + 1) payload[i] = 20; writes = 0; request(8'h01, 32); wait(dut.state == 3); safety_locked = 1; expect_ack(0, 8'h01, 4, 0); if (manual_commit || writes != 32) $fatal(1, "validate race did not fail closed"); safety_locked = 0; for (i = 0; i < 32; i = i + 1) payload[i] = (i < 24) ? i : 20; writes = 0; request(8'h01, 32); wait(manual_commit); if (writes != 32) $fatal(1, "valid writes=%0d", writes); repeat (3) @(negedge clk); if (tx_start) $fatal(1, "ACK before atomic commit boundary"); manual_done = 1; @(negedge clk); manual_done = 0; expect_ack(1, 8'h01, 0, 0); // A hard lock discovered after the manual bank commit cancels ACK OK. writes = 0; request(8'h01, 32); wait(manual_commit); manual_ok = 0; @(negedge clk); manual_done = 1; @(negedge clk); manual_done = 0; expect_ack(0, 8'h01, 4, 0); manual_ok = 1; safety_locked = 1; request(8'h01, 32); expect_ack(0, 8'h01, 4, 0); safety_locked = 0; payload[0] = 19; request(8'h04, 1); expect_ack(0, 8'h04, 3, 0); payload[0] = 70; request(8'h04, 1); wait(pid_commit); if (pid_target != 70) $fatal; expect_ack(1, 8'h04, 0, 0); request(8'h02, 0); wait(snapshot_capture); snapshot_ready = 1; @(negedge clk); snapshot_ready = 0; expect_ack(1, 8'h02, 0, 0); wait(tx_start); if (tx_type != 8'h82 || tx_length != 201) $fatal(1, "GET DATA order"); @(negedge clk); tx_done = 1; @(negedge clk); tx_done = 0; wait(transaction_done); $display("PASS: unified ACK/NACK, atomic SET_ALL, PID and GET 06->82"); $finish; end endmodule