| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- `timescale 1ns/1ps
- module prod2u_mrdimm_uart_diag_tb;
- reg clk = 1'b0;
- reg rs232_rx = 1'b1;
- wire rs232_tx;
- wire ttl_tx;
- wire [23:0] ptc_pwm_out;
- wire [3:0] fan_pwm_out;
- wire motor_en_n;
- wire x_step, x_dir;
- wire y_step, y_dir;
- wire z_step, z_dir;
- always #20 clk = ~clk; // 25 MHz
- prod2u_mrdimm_uart_diag_top #(
- .CLOCK_HZ(25000000),
- .BAUD_RATE(921600),
- .CLKS_PER_BIT(27)
- ) dut (
- .sys_clk(clk),
- .host_rs232_rx(rs232_rx),
- .host_rs232_tx(rs232_tx),
- .host_ttl_rx(1'b1),
- .host_ttl_tx(ttl_tx),
- .temp_scl(),
- .temp_sda(),
- .limit_lower_raw(3'd0),
- .limit_upper_raw(3'd0),
- .ptc_pwm_out(ptc_pwm_out),
- .fan_pwm_out(fan_pwm_out),
- .motor_en_n(motor_en_n),
- .x_step(x_step), .x_dir(x_dir),
- .y_step(y_step), .y_dir(y_dir),
- .z_step(z_step), .z_dir(z_dir)
- );
- // Byte receiver monitor on rs232_tx
- reg [7:0] captured_bytes [0:255];
- integer captured_cnt = 0;
- reg [7:0] cur_byte = 0;
- integer bit_idx = 0;
- always @(negedge rs232_tx) begin
- // Start bit detected
- #(27 * 40 * 1.5); // Wait 1.5 bit times to sample bit 0
- for (bit_idx = 0; bit_idx < 8; bit_idx = bit_idx + 1) begin
- cur_byte[bit_idx] = rs232_tx;
- #(27 * 40);
- end
- // Stop bit
- captured_bytes[captured_cnt] = cur_byte;
- captured_cnt = captured_cnt + 1;
- $display("[%0t ns] DIAG TX captured byte 0x%02X (%c)", $time, cur_byte, (cur_byte >= 32 && cur_byte <= 126) ? cur_byte : ".");
- end
- initial begin
- $display("=== STARTING DIAGNOSTIC FIRMWARE SIMULATION ===");
- // Wait for POR (1ms = 1,000,000 ns)
- #1100000;
- // Verify safety outputs
- if (ptc_pwm_out !== 24'b0) begin
- $display("FAIL: PTC outputs not 0! Got %b", ptc_pwm_out);
- $finish(1);
- end
- if (motor_en_n !== 1'b1) begin
- $display("FAIL: motor_en_n not 1!");
- $finish(1);
- end
- if (fan_pwm_out !== 4'b0) begin
- $display("FAIL: fan_pwm_out not 0!");
- $finish(1);
- end
- $display("SAFETY VERIFIED: PTC=0, Motor disabled, Fan=0");
- // Wait to capture at least 32 bytes of the diagnostic burst
- wait (captured_cnt >= 32);
- $display("Captured %0d bytes from diagnostic burst:", captured_cnt);
- if (captured_bytes[0] === 8'h55 && captured_bytes[4] === 8'hAA &&
- captured_bytes[8] === 8'h00 && captured_bytes[10] === 8'hFF &&
- captured_bytes[25] === 8'hA5 && captured_bytes[26] === 8'h5A) begin
- $display("DIAGNOSTIC PATTERN MATCHED: 55, AA, 00, FF, FRAME verified!");
- end else begin
- $display("FAIL: Pattern mismatch! Byte0=0x%02X, Byte4=0x%02X", captured_bytes[0], captured_bytes[4]);
- $finish(1);
- end
- $display("ALL DIAGNOSTIC CHECKS PASSED!");
- $finish(0);
- end
- endmodule
|