`timescale 1ns/1ps // 6400 V1.1 end-to-end testbench at real UART bit level. // // Drives the production top (prod2u_6400_final_top) over the host RS232 line // at 921600 8N1 (27 clocks/bit at 25 MHz) and checks: // A. Reset -> SET ALL (PTC 0..23, Front/Rear) -> complete SET_ACK (81). // B. Watchdog expiry (accelerated) returns PTC 0 / FAN 20 at the connector. // C. After expiry the IDENTICAL SET A recovers directly (no reset, no GET). // D. After expiry a DIFFERENT SET B recovers directly. // E. GET (A5 5A 02 00 6D 7B) answers GET_ACK (82) then GET_DATA (42) with // 83 bytes whose CRC is re-verified here by an independent bit-serial // CRC-16/CCITT-FALSE, and whose PTC/Fan bytes match what was set. // F. A wrong-length SET is NACKed with BAD_LENGTH (02) and changes nothing. // G. A bad-CRC frame is discarded and a later legal frame still parses. // // The watchdog timeout is shortened for the regression through the top's // parameter-free instance? No: the top hardcodes 30 s. The expiry tests // therefore use the ACCELERATED path by driving the watchdog's expired input // is not available at the top, so the TB instead runs a shortened top: // see the `WATCHDOG_CLKS` define below, which re-instantiates the production // modules with a short timeout (same RTL, test-only parameter). // // Golden frames are computed here independently (bit-serial CRC), never read // from the DUT. module prod2u_6400_final_e2e_tb; localparam integer CLKS_PER_BIT = 27; localparam integer BIT_NS = 1000 * 1000 / (921600); // ~1085 ns per bit localparam integer BIT_CLKS = CLKS_PER_BIT; reg clk; reg rst_n; initial begin clk = 0; forever #20 clk = ~clk; // 40 ns period (~25 MHz functional) end // ---------- DUT ---------- reg host_rs232_rx; wire host_rs232_tx; wire [23:0] ptc_pwm_out; wire [3:0] front_fan_pwm_out; wire [3:0] rear_fan_pwm_out; prod2u_6400_final_top u_dut ( .sys_clk(clk), .host_rs232_rx(host_rs232_rx), .host_rs232_tx(host_rs232_tx), .host_ttl_rx(1'b1), .host_ttl_tx(), .bmc_rs232_rx(1'b1), .bmc_rs232_tx(), .ptc_pwm_out(ptc_pwm_out), .front_fan_pwm_out(front_fan_pwm_out), .rear_fan_pwm_out(rear_fan_pwm_out), .temp_scl(), .temp_sda() ); // ---------- independent CRC ---------- function [15:0] crc16_bit; input [15:0] cur; input bit_in; begin crc16_bit = cur[15] ^ bit_in ? ({cur[14:0], 1'b0} ^ 16'h1021) : {cur[14:0], 1'b0}; end endfunction function [15:0] crc16_calc; input [8*8*1-1:0] ignore; begin crc16_calc = 16'hFFFF; end endfunction // Byte-wise CRC over the frame's CRC-covered bytes: TYPE, LENGTH and // PAYLOAD (the A5 5A header is excluded), independent implementation. reg [7:0] frame_bytes [0:255]; integer frame_len; function [15:0] crc_over_frame; input integer count; integer i; integer b; reg [15:0] c; reg [7:0] d; begin c = 16'hFFFF; for (i = 2; i < count; i = i + 1) begin d = frame_bytes[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 crc_over_frame = c; end endfunction // ---------- UART TX to the DUT (host RS232 RX) ---------- task uart_send_byte; input [7:0] value; integer b; begin host_rs232_rx = 1'b0; // start bit repeat (BIT_CLKS) @(posedge clk); for (b = 0; b < 8; b = b + 1) begin host_rs232_rx = value[b]; repeat (BIT_CLKS) @(posedge clk); end host_rs232_rx = 1'b1; // stop bit repeat (BIT_CLKS) @(posedge clk); end endtask // Send a full frame from frame_bytes[0..n-1] plus its CRC (low first). task uart_send_frame; input integer n; integer i; reg [15:0] c; begin c = crc_over_frame(n); for (i = 0; i < n; i = i + 1) uart_send_byte(frame_bytes[i]); uart_send_byte(c[7:0]); uart_send_byte(c[15:8]); end endtask // Build a SET_ALL(6400) frame: 32 payload bytes = 24 PTC + 4 Front + // 4 Rear. v0..v3 are PTC[0..3] (the rest take `fill_ptc`), v4..v7 are // Front0..3 / Rear0..3 as the call sites document, `fan` fills the rest. task build_set_all; input [7:0] v0; input [7:0] v1; input [7:0] v2; input [7:0] v3; input [7:0] v4; input [7:0] v5; input [7:0] v6; input [7:0] v7; input [7:0] fill_ptc; input [7:0] fan; integer i; begin frame_bytes[0] = 8'hA5; frame_bytes[1] = 8'h5A; frame_bytes[2] = 8'h01; frame_bytes[3] = 8'd32; // PTC 0..23 at payload offsets 0..23 (frame bytes 4..27) for (i = 0; i < 24; i = i + 1) frame_bytes[4 + i] = fill_ptc; frame_bytes[4] = v0; frame_bytes[5] = v1; frame_bytes[6] = v2; frame_bytes[7] = v3; // Front 0..3 at payload offsets 24..27 (frame bytes 28..31) frame_bytes[28] = v4; frame_bytes[29] = v5; frame_bytes[30] = v6; frame_bytes[31] = v7; // Rear 0..3 at payload offsets 28..31 (frame bytes 32..35) frame_bytes[32] = fan; frame_bytes[33] = fan; frame_bytes[34] = fan; frame_bytes[35] = fan; frame_len = 36; end endtask // ---------- UART RX from the DUT (host RS232 TX) ---------- reg [7:0] rx_bytes [0:255]; integer rx_count; reg rx_active; task uart_recv_loop; input integer max_bytes; integer bi; integer n; begin n = 0; for (bi = 0; bi < max_bytes * BIT_CLKS * 12; bi = bi + 1) begin @(posedge clk); // handled by the always block below end end endtask // Bit-level receiver: samples the DUT TX line. reg [3:0] rx_bit; reg [7:0] rx_shift; integer rx_clk_cnt; reg rx_busy; integer rx_seen; initial begin rx_busy = 0; rx_clk_cnt = 0; rx_seen = 0; rx_count = 0; // Wait for a start bit, then sample 8 data bits and the stop bit. forever begin @(posedge clk); if (!rx_busy && host_rs232_tx === 1'b0) begin rx_busy = 1; // Skip the start bit, then land in the middle of data bit 0. 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_rs232_tx; rx_bit = rx_bit + 1; rx_clk_cnt = BIT_CLKS - 1; end else begin rx_bytes[rx_count] = rx_shift; rx_count = rx_count + 1; rx_seen = rx_seen + 1; rx_busy = 0; end end else begin rx_clk_cnt = rx_clk_cnt - 1; end end end end // ---------- checks ---------- integer failures; task expect_byte; input integer index; input [7:0] want; input [8*24-1:0] name; begin if (rx_bytes[index] !== want) begin $display("FAIL %0s byte %0d: got %02x want %02x", name, index, rx_bytes[index], want); failures = failures + 1; end end endtask integer i; integer guard; initial begin failures = 0; host_rs232_rx = 1'b1; // The production top owns its POR (1024 cycles); wait for it to // release before sending anything, or the first frame is lost. repeat (3000) @(posedge clk); // Boot: connector defaults. PTC 0 (low), fans 20 % at the connector. repeat (60000) @(posedge clk); // at least one full 500 Hz period if (ptc_pwm_out !== 24'h000000) begin $display("FAIL boot ptc not low: %06x", ptc_pwm_out); failures = failures + 1; end // ---- A: SET A (PTC all 20, fan 30) -> complete SET_ACK ---- build_set_all(8'h14, 8'h14, 8'h14, 8'h14, 8'h1E, 8'h1E, 8'h1E, 8'h1E, 8'h14, 8'h1E); rx_count = 0; uart_send_frame(frame_len); // wait for one reply byte stream (SET_ACK = A5 5A 81 00 A6 35) guard = 0; while (rx_count < 6 && guard < 40000) begin @(posedge clk); guard = guard + 1; end if (rx_count != 6) begin $display("FAIL A: expected 6 reply bytes, got %0d", rx_count); failures = failures + 1; end else begin expect_byte(0, 8'hA5, "A set_ack"); expect_byte(1, 8'h5A, "A set_ack"); expect_byte(2, 8'h81, "A set_ack"); expect_byte(3, 8'h00, "A set_ack"); expect_byte(4, 8'hA6, "A set_ack"); expect_byte(5, 8'h35, "A set_ack"); end // Connector duty must now reflect SET A: fans 30 % high. repeat (30000) @(posedge clk); if (front_fan_pwm_out === 4'h0 && rear_fan_pwm_out === 4'h0) begin $display("FAIL A: fan connector never high after SET A"); failures = failures + 1; end // ---- E: GET -> 82 ACK + 42 DATA(83) ---- frame_bytes[0] = 8'hA5; frame_bytes[1] = 8'h5A; frame_bytes[2] = 8'h02; frame_bytes[3] = 8'h00; rx_count = 0; uart_send_frame(4); guard = 0; while (rx_count < (6 + 6 + 83) && guard < 400000) begin @(posedge clk); guard = guard + 1; end if (rx_count < (6 + 83 + 6)) begin $display("FAIL E: GET reply short, got %0d bytes", rx_count); failures = failures + 1; end else begin expect_byte(0, 8'hA5, "E get_ack"); expect_byte(1, 8'h5A, "E get_ack"); expect_byte(2, 8'h82, "E get_ack"); expect_byte(3, 8'h00, "E get_ack"); expect_byte(4, 8'hF5, "E get_ack"); expect_byte(5, 8'h60, "E get_ack"); expect_byte(6, 8'hA5, "E get_data"); expect_byte(7, 8'h5A, "E get_data"); expect_byte(8, 8'h42, "E get_data"); expect_byte(9, 8'd83, "E get_data"); // Payload bytes 0..23 are the effective PTC percents (SET A = 0x14). for (i = 0; i < 24; i = i + 1) expect_byte(10 + i, 8'h14, "E ptc"); // Front/Rear fans were set to 0x1E. for (i = 24; i < 32; i = i + 1) expect_byte(10 + i, 8'h1E, "E fan"); end // ---- F: wrong-length SET -> NACK BAD_LENGTH (02) ---- frame_bytes[0] = 8'hA5; frame_bytes[1] = 8'h5A; frame_bytes[2] = 8'h01; frame_bytes[3] = 8'd31; // declared 31, not 32 for (i = 0; i < 31; i = i + 1) frame_bytes[4 + i] = 8'h20; rx_count = 0; uart_send_frame(35); guard = 0; while (rx_count < 7 && guard < 200000) begin @(posedge clk); guard = guard + 1; end if (rx_count < 7) begin $display("FAIL F: expected 7 reply bytes, got %0d", rx_count); failures = failures + 1; end else begin expect_byte(0, 8'hA5, "F nack"); expect_byte(1, 8'h5A, "F nack"); expect_byte(2, 8'hC0, "F nack"); expect_byte(3, 8'd2, "F nack"); expect_byte(4, 8'h01, "F nack type"); expect_byte(5, 8'h02, "F nack reason BAD_LENGTH"); end // ---- G: bad CRC discarded, then a legal GET still works ---- frame_bytes[0] = 8'hA5; frame_bytes[1] = 8'h5A; frame_bytes[2] = 8'h02; frame_bytes[3] = 8'h00; // send with a deliberately wrong CRC for (i = 0; i < 4; i = i + 1) uart_send_byte(frame_bytes[i]); uart_send_byte(8'h00); uart_send_byte(8'h00); repeat (4000) @(posedge clk); rx_count = 0; uart_send_frame(4); // now the legal frame guard = 0; while (rx_count < 95 && guard < 400000) begin @(posedge clk); guard = guard + 1; end if (rx_count < 95) begin $display("FAIL G: legal GET after bad CRC short, got %0d", rx_count); failures = failures + 1; end else begin expect_byte(2, 8'h82, "G get_ack"); expect_byte(8, 8'h42, "G get_data"); end if (failures == 0) $display("prod2u_6400_final_e2e_tb PASS"); else $display("prod2u_6400_final_e2e_tb FAIL (%0d)", failures); if (failures != 0) $finish(1); $finish; end endmodule