`timescale 1ns/1ps // Production-top duty readback and accelerated watchdog recovery regression. // // Drives UART SET/GET, checks response headers and selected duty bytes, checks // duty-bank validity over ten accelerated expiry/recovery rounds, and samples // SSR1/2/4 at the beginning and end. Bad-CRC input is followed by eventual // expiry and a successful GET. // // Scope: this bench does not monitor the first PWM output transition, prove // period-boundary channel atomicity, calculate response CRCs, measure connector // duty, or drive SET/GET exactly on watchdog expiry. The bad-CRC case waits a // whole timeout after injection, so it does not prove the exact reload edge. // The independent connector and nominal-UART fixtures supply their stated // physical-pin, full-period-duty, CRC and complete-stop checks separately. module pwm_commit_handshake_tb; localparam integer CLKS_PER_BIT = 27; // 921600 baud at 25 MHz localparam integer BIT_CLKS = CLKS_PER_BIT; // 98-byte GET reply takes ~26,460 clocks at 921600 baud. // Set WD_CLKS = 35,000 (~1.4 ms) so replies finish cleanly before accelerated expiry. localparam integer WD_CLKS = 35000; reg clk; initial begin clk = 0; forever #20 clk = ~clk; // 25 MHz (40 ns) end reg host_ttl_rx; wire host_ttl_tx; wire [23:0] ptc_pwm_out; wire [3:0] front_fan_pwm_out; wire [3:0] rear_fan_pwm_out; wire temp_scl; wire temp_sda; wire ssr1_out, ssr2_out, ssr4_out; wire buzzer_out; pullup(temp_scl); pullup(temp_sda); prod2u_6400_v2_top #( .FAN_BOARD_INVERT_MASK(8'hFF), .WD_TIMEOUT_CLKS(WD_CLKS) ) u_dut ( .sys_clk(clk), .host_ttl_rx(host_ttl_rx), .host_ttl_tx(host_ttl_tx), .host_rs232_rx(1'b1), .host_rs232_tx(), .bmc_rs232_rx(1'b1), .bmc_rs232_tx(), .ptc_pwm_out(ptc_pwm_out), .ptc_spare_zero(), .front_fan_pwm_out(front_fan_pwm_out), .rear_fan_pwm_out(rear_fan_pwm_out), .temp_scl(temp_scl), .temp_sda(temp_sda), .temp_scl_alt(), .temp_sda_alt(), .ssr1_out(ssr1_out), .ssr2_out(ssr2_out), .ssr4_out(ssr4_out), .ssr_unused_zero(), .estop_btn_in(1'b0), .hood_open_btn_in(1'b0), .hood_close_btn_in(1'b0), .buzzer_out(buzzer_out) ); // CRC-16/CCITT-FALSE function [15:0] calc_crc; input integer count; integer i, b; reg [15:0] c; reg [7:0] d; begin c = 16'hFFFF; for (i = 2; i < count; i = i + 1) begin d = tx_buf[i]; for (b = 7; b >= 0; b = b - 1) c = (c[15] ^ d[b]) ? ({c[14:0], 1'b0} ^ 16'h1021) : {c[14:0], 1'b0}; end calc_crc = c; end endfunction reg [7:0] tx_buf [0:255]; task uart_send_byte; input [7:0] val; integer b; begin host_ttl_rx = 1'b0; repeat (BIT_CLKS) @(posedge clk); for (b = 0; b < 8; b = b + 1) begin host_ttl_rx = val[b]; repeat (BIT_CLKS) @(posedge clk); end host_ttl_rx = 1'b1; repeat (BIT_CLKS) @(posedge clk); end endtask task uart_send_frame; input integer n; integer i; reg [15:0] crc; begin crc = calc_crc(n); for (i = 0; i < n; i = i + 1) uart_send_byte(tx_buf[i]); uart_send_byte(crc[7:0]); uart_send_byte(crc[15:8]); end endtask task uart_send_bad_crc_frame; input integer n; integer i; reg [15:0] crc; begin crc = calc_crc(n) ^ 16'hA5A5; // corrupt CRC for (i = 0; i < n; i = i + 1) uart_send_byte(tx_buf[i]); uart_send_byte(crc[7:0]); uart_send_byte(crc[15:8]); end endtask // Receiver reg [7:0] rx_buf [0:255]; integer rx_count; reg [3:0] rx_bit; reg [7:0] rx_shift; integer rx_clk_cnt; reg rx_busy; initial begin rx_busy = 0; rx_clk_cnt = 0; rx_count = 0; forever begin @(posedge clk); if (!rx_busy && host_ttl_tx === 1'b0) begin rx_busy = 1; rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1; rx_bit = 0; end else if (rx_busy) begin if (rx_clk_cnt == 0) begin if (rx_bit < 8) begin rx_shift[rx_bit] = host_ttl_tx; rx_bit = rx_bit + 1; rx_clk_cnt = BIT_CLKS - 1; end else begin rx_buf[rx_count] = rx_shift; rx_count = rx_count + 1; rx_busy = 0; end end else begin rx_clk_cnt = rx_clk_cnt - 1; end end end end task send_set_all_uniform; input [7:0] ptc_val; input [7:0] front_val; input [7:0] rear_val; integer i; begin tx_buf[0] = 8'hA5; tx_buf[1] = 8'h5A; tx_buf[2] = 8'h01; tx_buf[3] = 8'd36; for (i = 0; i < 24; i = i + 1) tx_buf[4 + i] = ptc_val; for (i = 0; i < 4; i = i + 1) tx_buf[28 + i] = front_val; for (i = 0; i < 4; i = i + 1) tx_buf[32 + i] = rear_val; tx_buf[36] = 100; tx_buf[37] = 0; tx_buf[38] = 100; tx_buf[39] = 1; // buzzer ON rx_count = 0; uart_send_frame(40); end endtask task send_get_all; begin tx_buf[0] = 8'hA5; tx_buf[1] = 8'h5A; tx_buf[2] = 8'h02; tx_buf[3] = 8'd0; rx_count = 0; uart_send_frame(4); end endtask task wait_for_set_ack; output ok; begin while (rx_count < 6) @(posedge clk); if (rx_buf[0] == 8'hA5 && rx_buf[1] == 8'h5A && rx_buf[2] == 8'h81 && rx_buf[3] == 8'h00) begin ok = 1; end else begin ok = 0; $display("ERROR: Bad SET_ACK: %02h %02h %02h %02h", rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3]); end end endtask task wait_for_get_reply; output ok; begin while (rx_count < 102) @(posedge clk); if (rx_buf[0] == 8'hA5 && rx_buf[1] == 8'h5A && rx_buf[2] == 8'h82 && rx_buf[6] == 8'hA5 && rx_buf[7] == 8'h5A && rx_buf[8] == 8'h42 && rx_buf[9] == 8'd90) begin ok = 1; end else begin ok = 0; $display("ERROR: Bad GET reply (len=%0d)", rx_buf[9]); end end endtask integer failures = 0; integer round; reg ack_ok; initial begin host_ttl_rx = 1'b1; repeat (2000) @(posedge clk); // Allow POR (1024 cycles) to release sys_rst_n $display("=== START PWM Commit Handshake & Watchdog Test ==="); // Test 1: SSR boot defaults zero if (ssr1_out !== 1'b0 || ssr2_out !== 1'b0 || ssr4_out !== 1'b0) begin $display("FAIL: SSRs not zero initially"); failures = failures + 1; end else begin $display("PASS: SSR1/2/4 boot zero"); end // Test 2: Initial defaults (PTC 0%, Fan 20%) via GET_ALL before any SET $display("--- Testing Initial Default Outputs via GET_ALL ---"); send_get_all(); wait_for_get_reply(ack_ok); if (!ack_ok) failures = failures + 1; else begin for (round = 0; round < 24; round = round + 1) begin if (rx_buf[10 + round] !== 8'd0) begin $display("FAIL: Default PTC[%0d] = %0d != 0", round, rx_buf[10 + round]); failures = failures + 1; end end for (round = 0; round < 8; round = round + 1) begin if (rx_buf[34 + round] !== 8'd20) begin $display("FAIL: Default FAN[%0d] = %0d != 20", round, rx_buf[34 + round]); failures = failures + 1; end end $display("PASS: Initial defaults confirmed (PTC 0%%, FAN 20%%)"); end // Test 3: SET all channels to100 and check subsequent GET readback $display("--- Testing All-100 Duty Commit Readback ---"); send_set_all_uniform(8'd100, 8'd100, 8'd100); wait_for_set_ack(ack_ok); if (!ack_ok) begin $display("FAIL: SET_ACK for All 100 failed"); failures = failures + 1; end else begin $display("PASS: SET_ACK received for All 100"); end send_get_all(); wait_for_get_reply(ack_ok); if (!ack_ok) failures = failures + 1; else begin for (round = 0; round < 24; round = round + 1) begin if (rx_buf[10 + round] !== 8'd100) begin $display("FAIL: PTC[%0d] = %0d != 100", round, rx_buf[10 + round]); failures = failures + 1; end end for (round = 0; round < 8; round = round + 1) begin if (rx_buf[34 + round] !== 8'd100) begin $display("FAIL: FAN[%0d] = %0d != 100", round, rx_buf[34 + round]); failures = failures + 1; end end $display("PASS: Output confirmed 100%% on all channels"); end // Test 4: Watchdog expiry to defaults & consecutive GET checks $display("--- Testing Expiry to Safe Defaults & Consecutive GETs ---"); repeat (WD_CLKS + 50) @(posedge clk); if (u_dut.u_duty_bank.valid_r !== 1'b0) begin $display("FAIL: Duty bank not invalidated on expiry"); failures = failures + 1; end else begin $display("PASS: Duty bank invalidated to safe defaults on expiry"); end // GET 1: should return defaulted outputs send_get_all(); wait_for_get_reply(ack_ok); if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin $display("FAIL: Post-expiry GET did not return default PTC 0 / FAN 20"); failures = failures + 1; end else begin $display("PASS: Post-expiry GET returns PTC 0 / FAN 20 defaults"); end // GET 2: consecutive GET must NOT restore pre-expiry 100% duty send_get_all(); wait_for_get_reply(ack_ok); if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin $display("FAIL: Consecutive GET improperly restored old bank"); failures = failures + 1; end else begin $display("PASS: Consecutive GET preserved default outputs"); end // Test 5: Direct recovery with identical bytes SET A $display("--- Testing Direct Recovery with Identical SET A ---"); send_set_all_uniform(8'd100, 8'd100, 8'd100); wait_for_set_ack(ack_ok); if (!ack_ok) begin $display("FAIL: SET_ACK on direct recovery failed"); failures = failures + 1; end else begin $display("PASS: SET_ACK received on direct recovery"); end send_get_all(); wait_for_get_reply(ack_ok); if (!ack_ok || rx_buf[10] !== 8'd100 || rx_buf[34] !== 8'd100) begin $display("FAIL: Output not restored after direct SET A"); failures = failures + 1; end else begin $display("PASS: Output 100%% restored after direct SET A"); end // Test 6: Direct recovery with SET B (different values) $display("--- Testing SET B Recovery ---"); repeat (WD_CLKS + 50) @(posedge clk); // let expire send_set_all_uniform(8'd50, 8'd30, 8'd70); wait_for_set_ack(ack_ok); if (!ack_ok) begin $display("FAIL: SET_ACK for SET B failed"); failures = failures + 1; end else begin $display("PASS: SET_ACK received for SET B"); end send_get_all(); wait_for_get_reply(ack_ok); if (!ack_ok || rx_buf[10] !== 8'd50 || rx_buf[34] !== 8'd30 || rx_buf[38] !== 8'd70) begin $display("FAIL: SET B values not applied: PTC=%0d F=%0d R=%0d", rx_buf[10], rx_buf[34], rx_buf[38]); failures = failures + 1; end else begin $display("PASS: SET B output applied cleanly"); end // Test 7: Bad CRC followed by eventual watchdog expiry and legal GET $display("--- Testing Bad CRC Input, Eventual Expiry and Recovery GET ---"); // Frame with bad CRC tx_buf[0] = 8'hA5; tx_buf[1] = 8'h5A; tx_buf[2] = 8'h01; tx_buf[3] = 8'd35; for (round = 0; round < 35; round = round + 1) tx_buf[4 + round] = 8'd99; rx_count = 0; uart_send_bad_crc_frame(39); repeat (WD_CLKS + 50) @(posedge clk); send_get_all(); wait_for_get_reply(ack_ok); if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin $display("FAIL: Output did not reach defaults after bad CRC and full timeout!"); failures = failures + 1; end else begin $display("PASS: Defaults observed after bad CRC input and full timeout"); end // Test 8: Accelerated Expiry & Direct Recovery Rounds $display("--- Testing Repeated Rounds of Accelerated Expiry & Direct Recovery ---"); for (round = 1; round <= 10; round = round + 1) begin // Direct SET recovery (same values: 60%, 40%, 80%) send_set_all_uniform(8'd60, 8'd40, 8'd80); wait_for_set_ack(ack_ok); if (!ack_ok) begin $display("FAIL: Round %0d SET_ACK failed", round); failures = failures + 1; end if (u_dut.u_duty_bank.valid_r !== 1'b1) begin $display("FAIL: Round %0d duty bank not valid after commit", round); failures = failures + 1; end // Wait for watchdog expiry repeat (WD_CLKS + 50) @(posedge clk); if (u_dut.u_duty_bank.valid_r !== 1'b0) begin $display("FAIL: Round %0d duty bank not invalidated on expiry", round); failures = failures + 1; end if ({ssr4_out,ssr2_out,ssr1_out} !== 3'b0 || buzzer_out !== 1'b0) begin $display("FAIL: SSR or buzzer not zero at watchdog expiry"); failures = failures + 1; end $display(" Progress: %0d/10 recovery rounds passed", round); end $display("PASS: All 10 accelerated recovery rounds passed"); // Verify final state via full GET_ALL send_set_all_uniform(8'd75, 8'd45, 8'd65); wait_for_set_ack(ack_ok); send_get_all(); wait_for_get_reply(ack_ok); if (!ack_ok || rx_buf[10] !== 8'd75 || rx_buf[34] !== 8'd45 || rx_buf[38] !== 8'd65) begin $display("FAIL: Final GET_ALL verification failed"); failures = failures + 1; end else begin $display("PASS: Final GET_ALL matches applied SET"); end // Final SSR check if (ssr1_out !== 1'b1 || ssr2_out !== 1'b0 || ssr4_out !== 1'b1) begin $display("FAIL: SSRs differ from100/0/100"); failures = failures + 1; end else begin $display("PASS: SSRs match100/0/100 at final check"); end #2000; if (failures == 0) begin $display("pwm_commit_handshake_tb PASS"); end else begin $fatal(1, "pwm_commit_handshake_tb FAIL (%0d errors)", failures); end $finish; end initial begin #600000000; // 600 ms timeout $fatal(1, "pwm_commit_handshake_tb TIMEOUT"); end endmodule