`timescale 1ns/1ps // Full E2E bit-level simulation testbench for MRDIMM Final Architecture. module prod2u_mrdimm_final_e2e_tb; localparam integer BIT_CLKS = 27; // 921600 baud @ 25 MHz reg clk = 1'b0; reg rst_n = 1'b0; reg rs232_rx = 1'b1; wire rs232_tx; reg [11:0] limit_lower_raw = 12'd0; reg [11:0] limit_upper_raw = 12'd0; reg hood_switch_in = 1'b0; wire buzzer_out; wire [23:0] ptc_pwm_out; wire [3:0] fan_pwm_out; wire motor_en_n; wire [11:0] motor_step_gate; wire [11:0] motor_dir_gate; wire x_step = motor_step_gate[0]; wire x_dir = motor_dir_gate[0]; wire y_step = motor_step_gate[1]; wire y_dir = motor_dir_gate[1]; wire z_step = motor_step_gate[2]; wire z_dir = motor_dir_gate[2]; always #20 clk = ~clk; // 25 MHz prod2u_mrdimm_final_top #( .CLOCK_HZ(25000000), .HOST_BAUD(921600), .BMC_BAUD(115200), .PWM_PERIOD_CLKS(5000), .PWM_CLKS_PER_PERCENT(50), .HOST_TIMEOUT_CYCLES(2500) ) dut ( .sys_clk(clk), .host_rs232_rx(rs232_rx), .host_rs232_tx(rs232_tx), .host_ttl_rx(1'b1), .host_ttl_tx(), .bmc_rs232_rx(1'b1), .bmc_rs232_tx(), .temp_scl(), .temp_sda(), .limit_lower_raw(limit_lower_raw), .limit_upper_raw(limit_upper_raw), .ptc_pwm_out(ptc_pwm_out), .fan_pwm_out(fan_pwm_out), .motor_en_n(motor_en_n), .motor_step_gate(motor_step_gate), .motor_dir_gate(motor_dir_gate), .hood_switch_in(hood_switch_in), .buzzer_out(buzzer_out) ); // Byte-wise CRC over TYPE, LENGTH and PAYLOAD (excludes header A5 5A) reg [7:0] frame_bytes [0:255]; 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 DUT task uart_send_byte; input [7:0] value; integer b; begin rs232_rx = 1'b0; repeat (BIT_CLKS) @(posedge clk); for (b = 0; b < 8; b = b + 1) begin rs232_rx = value[b]; repeat (BIT_CLKS) @(posedge clk); end rs232_rx = 1'b1; repeat (BIT_CLKS) @(posedge clk); end endtask 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 // UART RX from DUT reg [7:0] rx_bytes [0:511]; integer rx_count = 0; reg [3:0] rx_bit = 0; reg [7:0] rx_shift = 0; integer rx_clk_cnt = 0; reg rx_busy = 0; initial begin forever begin @(posedge clk); if (!rx_busy && rs232_tx === 1'b0) begin rx_busy = 1'b1; 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] = 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_busy = 1'b0; end end else begin rx_clk_cnt = rx_clk_cnt - 1; end end end end // Step edge counters integer x_steps_count = 0; integer y_steps_count = 0; integer z_steps_count = 0; always @(posedge x_step) x_steps_count = x_steps_count + 1; always @(posedge y_step) y_steps_count = y_steps_count + 1; always @(posedge z_step) z_steps_count = z_steps_count + 1; // Timeout guard initial begin #30000000; // 30ms simulation time $display("FATAL: Simulation timeout!"); $fflush; $finish; end integer start_idx; integer i; initial begin $display("=== STARTING MRDIMM FINAL E2E TESTBENCH ==="); $fflush; rst_n = 1'b0; limit_lower_raw = 3'b000; limit_upper_raw = 3'b000; repeat (100) @(posedge clk); rst_n = 1'b1; repeat (2000) @(posedge clk); // -------------------------------------------------------------------- // TEST A: Legal GET_ALL at reset // -------------------------------------------------------------------- $display("[TEST A] Sending GET_ALL request..."); $fflush; frame_bytes[0] = 8'hA5; frame_bytes[1] = 8'h5A; frame_bytes[2] = 8'h02; // GET_ALL frame_bytes[3] = 8'h00; // LEN 0 start_idx = rx_count; uart_send_frame(4); // Expect GET_ACK (6 bytes: A5 5A 82 00 F5 60) + GET_DATA (71 bytes: A5 5A 42 41 ... CRC) // Total bytes = 6 + 71 = 77 bytes wait(rx_count >= start_idx + 77); $display(" -> Received 77 bytes from GET_ALL"); $fflush; // Verify GET_ACK if (rx_bytes[start_idx] !== 8'hA5 || rx_bytes[start_idx+1] !== 8'h5A || rx_bytes[start_idx+2] !== 8'h82 || rx_bytes[start_idx+3] !== 8'h00) begin $fatal(1, "FAIL A: GET_ACK mismatch: %02h %02h %02h %02h", rx_bytes[start_idx], rx_bytes[start_idx+1], rx_bytes[start_idx+2], rx_bytes[start_idx+3]); end $display(" -> Verified GET_ACK (82 00)"); $fflush; // Verify GET_DATA start_idx = start_idx + 6; if (rx_bytes[start_idx] !== 8'hA5 || rx_bytes[start_idx+1] !== 8'h5A || rx_bytes[start_idx+2] !== 8'h42 || rx_bytes[start_idx+3] !== 8'h41) begin $fatal(1, "FAIL A: GET_DATA header mismatch: %02h %02h %02h %02h", rx_bytes[start_idx], rx_bytes[start_idx+1], rx_bytes[start_idx+2], rx_bytes[start_idx+3]); end // Check FAN 0..3 defaults = 20 (0x14) at payload 0..3 (indices start_idx+4 .. +7) if (rx_bytes[start_idx+4] !== 8'd20 || rx_bytes[start_idx+5] !== 8'd20 || rx_bytes[start_idx+6] !== 8'd20 || rx_bytes[start_idx+7] !== 8'd20) begin $fatal(1, "FAIL A: fan defaults wrong"); end // Check PTC 0..3 defaults = 0 at payload 4..7 (indices start_idx+8 .. +11) if (rx_bytes[start_idx+8] !== 8'd0 || rx_bytes[start_idx+9] !== 8'd0 || rx_bytes[start_idx+10] !== 8'd0 || rx_bytes[start_idx+11] !== 8'd0) begin $fatal(1, "FAIL A: ptc defaults wrong"); end // Check hood_switch (byte 63) and buzzer_state (byte 64) default to 0 if (rx_bytes[start_idx+4+63] !== 8'd0 || rx_bytes[start_idx+4+64] !== 8'd0) begin $fatal(1, "FAIL A: hood/buzzer defaults wrong: hood=%h buzzer=%h", rx_bytes[start_idx+4+63], rx_bytes[start_idx+4+64]); end $display(" -> TEST A PASSED! GET_ACK and GET_DATA (65B) verified."); $fflush; // -------------------------------------------------------------------- // TEST B: SET_ALL with PUSH_DOWN on Axis 0 (X) for 5 steps // -------------------------------------------------------------------- $display("[TEST B] Sending SET_ALL: Axis 0 PUSH_DOWN 5 steps (half_period 20us)..."); $fflush; frame_bytes[0] = 8'hA5; frame_bytes[1] = 8'h5A; frame_bytes[2] = 8'h01; // SET_ALL frame_bytes[3] = 8'd68; // LEN 68 // Payload 0..3: FAN 20 frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd20; frame_bytes[6] = 8'd20; frame_bytes[7] = 8'd20; // Payload 4..7: PTC 0 frame_bytes[8] = 8'd0; frame_bytes[9] = 8'd0; frame_bytes[10] = 8'd0; frame_bytes[11] = 8'd0; // Axis 0: payload 8..12 (frame_bytes 12..16) frame_bytes[12] = 8'h01; // action PUSH_DOWN frame_bytes[13] = 8'd5; // steps low frame_bytes[14] = 8'd0; // steps high frame_bytes[15] = 8'd20; // half_period low = 20 us frame_bytes[16] = 8'd0; // half_period high // Axis 1..11: NOTHING (0) for (i = 17; i < 4 + 68; i = i + 1) frame_bytes[i] = 8'd0; start_idx = rx_count; x_steps_count = 0; uart_send_frame(72); // Expect SET_ACK (6 bytes: A5 5A 81 00 A6 35) wait(rx_count >= start_idx + 6); if (rx_bytes[start_idx+2] !== 8'h81 || rx_bytes[start_idx+3] !== 8'h00) begin $fatal(1, "FAIL B: expected SET_ACK (81 00), got %02h %02h", rx_bytes[start_idx+2], rx_bytes[start_idx+3]); end $display(" -> Verified SET_ACK (81 00)"); $fflush; // Expect MOTOR_DONE (10 bytes: A5 5A 50 04 ) wait(rx_count >= start_idx + 6 + 10); start_idx = start_idx + 6; if (rx_bytes[start_idx+2] !== 8'h50 || rx_bytes[start_idx+3] !== 8'h04) begin $fatal(1, "FAIL B: expected MOTOR_DONE (50 04), got %02h %02h", rx_bytes[start_idx+2], rx_bytes[start_idx+3]); end $display(" -> Got MOTOR_DONE: axis=%0d, reason=%0d, steps_done=%0d", rx_bytes[start_idx+4], rx_bytes[start_idx+5], {rx_bytes[start_idx+7], rx_bytes[start_idx+6]}); $fflush; if (rx_bytes[start_idx+4] !== 8'd0) $fatal(1, "FAIL B: wrong axis, expected 0"); if (rx_bytes[start_idx+5] !== 8'd0) $fatal(1, "FAIL B: wrong reason, expected 0"); if ({rx_bytes[start_idx+7], rx_bytes[start_idx+6]} !== 16'd5) $fatal(1, "FAIL B: wrong steps_done, expected 5"); if (x_steps_count !== 5) $fatal(1, "FAIL B: x_step edge count mismatch, expected 5 got %0d", x_steps_count); $display(" -> TEST B PASSED! Exactly 5 pulses emitted on x_step and completed normally."); $fflush; // -------------------------------------------------------------------- // TEST C: Limit switch triggered during motion on Axis 1 (Y) // -------------------------------------------------------------------- $display("[TEST C] Sending SET_ALL: Axis 1 PUSH_DOWN 50 steps, tripping limit..."); $fflush; // Axis 0: NOTHING frame_bytes[12] = 8'd0; frame_bytes[13] = 8'd0; frame_bytes[14] = 8'd0; frame_bytes[15] = 8'd0; frame_bytes[16] = 8'd0; // Axis 1: payload 13..17 (frame_bytes 17..21) frame_bytes[17] = 8'h01; // action PUSH_DOWN frame_bytes[18] = 8'd50; // 50 steps frame_bytes[19] = 8'd0; frame_bytes[20] = 8'd20; // 20 us frame_bytes[21] = 8'd0; start_idx = rx_count; y_steps_count = 0; uart_send_frame(72); // Wait for SET_ACK wait(rx_count >= start_idx + 6); $display(" -> Got SET_ACK, motor Y running..."); $fflush; // Wait for 3 steps, then assert lower limit wait(y_steps_count >= 3); @(posedge clk); $display(" -> Asserting Axis 1 lower limit at y_steps_count=%0d", y_steps_count); $fflush; limit_lower_raw[1] = 1'b1; // Wait for MOTOR_DONE wait(rx_count >= start_idx + 6 + 10); start_idx = start_idx + 6; $display(" -> Got MOTOR_DONE: axis=%0d, reason=%0d, steps_done=%0d", rx_bytes[start_idx+4], rx_bytes[start_idx+5], {rx_bytes[start_idx+7], rx_bytes[start_idx+6]}); $fflush; if (rx_bytes[start_idx+4] !== 8'd1) $fatal(1, "FAIL C: wrong axis, expected 1"); if (rx_bytes[start_idx+5] !== 8'd1) $fatal(1, "FAIL C: wrong reason, expected 1 (lower limit)"); if ({rx_bytes[start_idx+7], rx_bytes[start_idx+6]} !== y_steps_count) begin $fatal(1, "FAIL C: steps_done mismatch: got %0d, counted %0d", {rx_bytes[start_idx+7], rx_bytes[start_idx+6]}, y_steps_count); end $display(" -> TEST C PASSED! Limit aborted motion cleanly."); $fflush; limit_lower_raw[1] = 1'b0; // -------------------------------------------------------------------- // TEST D: Blocked at start on Axis 2 (Z) // -------------------------------------------------------------------- $display("[TEST D] Asserting upper limit on Axis 2, then sending PUSH_UP..."); $fflush; limit_upper_raw[2] = 1'b1; repeat (100) @(posedge clk); // Axis 1: NOTHING frame_bytes[17] = 8'd0; frame_bytes[18] = 8'd0; frame_bytes[19] = 8'd0; frame_bytes[20] = 8'd0; frame_bytes[21] = 8'd0; // Axis 2: payload 18..22 (frame_bytes 22..26) frame_bytes[22] = 8'h02; // PUSH_UP frame_bytes[23] = 8'd50; // 50 steps frame_bytes[24] = 8'd0; frame_bytes[25] = 8'd20; // 20 us frame_bytes[26] = 8'd0; start_idx = rx_count; z_steps_count = 0; uart_send_frame(72); // Wait for SET_ACK + MOTOR_DONE (16 bytes) wait(rx_count >= start_idx + 16); $display(" -> Got SET_ACK and MOTOR_DONE"); start_idx = start_idx + 6; $display(" -> MOTOR_DONE: axis=%0d, reason=%0d, steps_done=%0d", rx_bytes[start_idx+4], rx_bytes[start_idx+5], {rx_bytes[start_idx+7], rx_bytes[start_idx+6]}); $fflush; if (rx_bytes[start_idx+4] !== 8'd2) $fatal(1, "FAIL D: wrong axis, expected 2"); if (rx_bytes[start_idx+5] !== 8'd2) $fatal(1, "FAIL D: wrong reason, expected 2 (upper limit)"); if ({rx_bytes[start_idx+7], rx_bytes[start_idx+6]} !== 16'd0) $fatal(1, "FAIL D: expected 0 steps done"); if (z_steps_count !== 0) $fatal(1, "FAIL D: z_step emitted pulses when blocked!"); $display(" -> TEST D PASSED! Emitted 0 pulses when blocked at start."); $fflush; limit_upper_raw[2] = 1'b0; // -------------------------------------------------------------------- // TEST E: STOP_CLEAR on Axis 0 // -------------------------------------------------------------------- $display("[TEST E] Starting long move on Axis 0, then issuing STOP_CLEAR..."); $fflush; // Axis 2: NOTHING frame_bytes[22] = 8'd0; frame_bytes[23] = 8'd0; frame_bytes[24] = 8'd0; frame_bytes[25] = 8'd0; frame_bytes[26] = 8'd0; // Axis 0: 1000 steps frame_bytes[12] = 8'h01; // PUSH_DOWN frame_bytes[13] = 8'hE8; // 1000 steps low frame_bytes[14] = 8'h03; // 1000 steps high frame_bytes[15] = 8'd20; // 20 us frame_bytes[16] = 8'd0; start_idx = rx_count; x_steps_count = 0; uart_send_frame(72); wait(rx_count >= start_idx + 6); $display(" -> Got SET_ACK, motor running..."); $fflush; wait(x_steps_count >= 5); $display(" -> Sending STOP_CLEAR (action=0xFF) for Axis 0..."); $fflush; frame_bytes[12] = 8'hFF; // STOP_CLEAR frame_bytes[13] = 8'd0; frame_bytes[14] = 8'd0; frame_bytes[15] = 8'd0; frame_bytes[16] = 8'd0; start_idx = rx_count; uart_send_frame(72); wait(rx_count >= start_idx + 6); $display(" -> Got SET_ACK for STOP_CLEAR"); $fflush; repeat (2000) @(posedge clk); if (x_step !== 1'b0) $fatal(1, "FAIL E: x_step not low after STOP_CLEAR"); $display(" -> TEST E PASSED! Motor stopped immediately and queue cleared."); $fflush; // -------------------------------------------------------------------- // TEST F: V3.2.0 69-byte SET_ALL (buzzer=1, 4000Hz 80% duty) + hood_switch on GET_ALL // -------------------------------------------------------------------- $display("[TEST F] Testing 69B SET_ALL buzzer 4000Hz 80%% ON/OFF + hood_switch on GET_ALL..."); $fflush; frame_bytes[0] = 8'hA5; frame_bytes[1] = 8'h5A; frame_bytes[2] = 8'h01; // SET_ALL frame_bytes[3] = 8'd69; // LEN 69 (with buzzer byte 68) for (i = 4; i < 4 + 69; i = i + 1) frame_bytes[i] = 8'd0; frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd20; frame_bytes[6] = 8'd20; frame_bytes[7] = 8'd20; frame_bytes[4 + 68] = 8'd1; // buzzer ON start_idx = rx_count; uart_send_frame(73); wait(rx_count >= start_idx + 6); if (rx_bytes[start_idx+2] !== 8'h81) $fatal(1, "FAIL F: expected SET_ACK for 69B SET_ALL"); repeat (50) @(posedge clk); begin : check_mr_bz_on integer bz_hi, bz_lo, bz_k; bz_hi = 0; bz_lo = 0; for (bz_k = 0; bz_k < 6250; bz_k = bz_k + 1) begin @(posedge clk); if (buzzer_out === 1'b1) bz_hi = bz_hi + 1; else if (buzzer_out === 1'b0) bz_lo = bz_lo + 1; end if (bz_hi !== 5000 || bz_lo !== 1250) $fatal(1, "FAIL F: buzzer_out 4000Hz 80%% mismatch: high=%0d low=%0d", bz_hi, bz_lo); end // Press hood_switch_in and verify GET_ALL reports hood=1, buzzer=1 hood_switch_in = 1'b1; repeat (20) @(posedge clk); frame_bytes[0] = 8'hA5; frame_bytes[1] = 8'h5A; frame_bytes[2] = 8'h02; frame_bytes[3] = 8'h00; start_idx = rx_count; uart_send_frame(4); wait(rx_count >= start_idx + 77); start_idx = start_idx + 6; if (rx_bytes[start_idx+3] !== 8'h41 || rx_bytes[start_idx+4+63] !== 8'd1 || rx_bytes[start_idx+4+64] !== 8'd1) begin $fatal(1, "FAIL F: expected hood=1 buzzer=1, got len=%0d hood=%h buzzer=%h", rx_bytes[start_idx+3], rx_bytes[start_idx+4+63], rx_bytes[start_idx+4+64]); end // Release hood_switch and turn buzzer OFF via 69B SET_ALL hood_switch_in = 1'b0; frame_bytes[0] = 8'hA5; frame_bytes[1] = 8'h5A; frame_bytes[2] = 8'h01; frame_bytes[3] = 8'd69; for (i = 4; i < 4 + 69; i = i + 1) frame_bytes[i] = 8'd0; frame_bytes[4] = 8'd20; frame_bytes[5] = 8'd20; frame_bytes[6] = 8'd20; frame_bytes[7] = 8'd20; frame_bytes[4 + 68] = 8'd0; // buzzer OFF start_idx = rx_count; uart_send_frame(73); wait(rx_count >= start_idx + 6); repeat (50) @(posedge clk); if (buzzer_out !== 1'b0) $fatal(1, "FAIL F: buzzer_out did not clear to LOW"); $display(" -> TEST F PASSED! Buzzer ON/OFF & hood_switch GET_ALL verified."); $fflush; // -------------------------------------------------------------------- // ALL TESTS PASSED! // -------------------------------------------------------------------- $display("\n=================================================="); $display("PROD2U MRDIMM FINAL E2E TESTBENCH: ALL TESTS PASS!"); $display("prod2u_mrdimm_final_e2e_tb PASS"); $display("=================================================="); $fflush; $finish; end endmodule