prod2u_mrdimm_uart_diag_tb.v 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. `timescale 1ns/1ps
  2. module prod2u_mrdimm_uart_diag_tb;
  3. reg clk = 1'b0;
  4. reg rs232_rx = 1'b1;
  5. wire rs232_tx;
  6. wire ttl_tx;
  7. wire [23:0] ptc_pwm_out;
  8. wire [3:0] fan_pwm_out;
  9. wire motor_en_n;
  10. wire x_step, x_dir;
  11. wire y_step, y_dir;
  12. wire z_step, z_dir;
  13. always #20 clk = ~clk; // 25 MHz
  14. prod2u_mrdimm_uart_diag_top #(
  15. .CLOCK_HZ(25000000),
  16. .BAUD_RATE(921600),
  17. .CLKS_PER_BIT(27)
  18. ) dut (
  19. .sys_clk(clk),
  20. .host_rs232_rx(rs232_rx),
  21. .host_rs232_tx(rs232_tx),
  22. .host_ttl_rx(1'b1),
  23. .host_ttl_tx(ttl_tx),
  24. .temp_scl(),
  25. .temp_sda(),
  26. .limit_lower_raw(3'd0),
  27. .limit_upper_raw(3'd0),
  28. .ptc_pwm_out(ptc_pwm_out),
  29. .fan_pwm_out(fan_pwm_out),
  30. .motor_en_n(motor_en_n),
  31. .x_step(x_step), .x_dir(x_dir),
  32. .y_step(y_step), .y_dir(y_dir),
  33. .z_step(z_step), .z_dir(z_dir)
  34. );
  35. // Byte receiver monitor on rs232_tx
  36. reg [7:0] captured_bytes [0:255];
  37. integer captured_cnt = 0;
  38. reg [7:0] cur_byte = 0;
  39. integer bit_idx = 0;
  40. always @(negedge rs232_tx) begin
  41. // Start bit detected
  42. #(27 * 40 * 1.5); // Wait 1.5 bit times to sample bit 0
  43. for (bit_idx = 0; bit_idx < 8; bit_idx = bit_idx + 1) begin
  44. cur_byte[bit_idx] = rs232_tx;
  45. #(27 * 40);
  46. end
  47. // Stop bit
  48. captured_bytes[captured_cnt] = cur_byte;
  49. captured_cnt = captured_cnt + 1;
  50. $display("[%0t ns] DIAG TX captured byte 0x%02X (%c)", $time, cur_byte, (cur_byte >= 32 && cur_byte <= 126) ? cur_byte : ".");
  51. end
  52. initial begin
  53. $display("=== STARTING DIAGNOSTIC FIRMWARE SIMULATION ===");
  54. // Wait for POR (1ms = 1,000,000 ns)
  55. #1100000;
  56. // Verify safety outputs
  57. if (ptc_pwm_out !== 24'b0) begin
  58. $display("FAIL: PTC outputs not 0! Got %b", ptc_pwm_out);
  59. $finish(1);
  60. end
  61. if (motor_en_n !== 1'b1) begin
  62. $display("FAIL: motor_en_n not 1!");
  63. $finish(1);
  64. end
  65. if (fan_pwm_out !== 4'b0) begin
  66. $display("FAIL: fan_pwm_out not 0!");
  67. $finish(1);
  68. end
  69. $display("SAFETY VERIFIED: PTC=0, Motor disabled, Fan=0");
  70. // Wait to capture at least 32 bytes of the diagnostic burst
  71. wait (captured_cnt >= 32);
  72. $display("Captured %0d bytes from diagnostic burst:", captured_cnt);
  73. if (captured_bytes[0] === 8'h55 && captured_bytes[4] === 8'hAA &&
  74. captured_bytes[8] === 8'h00 && captured_bytes[10] === 8'hFF &&
  75. captured_bytes[25] === 8'hA5 && captured_bytes[26] === 8'h5A) begin
  76. $display("DIAGNOSTIC PATTERN MATCHED: 55, AA, 00, FF, FRAME verified!");
  77. end else begin
  78. $display("FAIL: Pattern mismatch! Byte0=0x%02X, Byte4=0x%02X", captured_bytes[0], captured_bytes[4]);
  79. $finish(1);
  80. end
  81. $display("ALL DIAGNOSTIC CHECKS PASSED!");
  82. $finish(0);
  83. end
  84. endmodule