pty_bridge_tb.v 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. `timescale 1ns/1ps
  2. // PTY Bridge Testbench for Full-Chain MRDIMM Closed Loop Simulation.
  3. // Uses VPI non-blocking PTY interface.
  4. // Instantiates the production physical top: prod2u_mrdimm_rs232_only_top.
  5. module pty_bridge_tb;
  6. localparam integer CLOCK_HZ = 25000000;
  7. localparam integer BIT_CLKS = 217; // 115200 baud @ 25 MHz
  8. reg sys_clk = 1'b0;
  9. reg host_rs232_rx = 1'b1;
  10. wire host_rs232_tx;
  11. wire host_ttl_tx;
  12. wire temp_scl;
  13. wire temp_sda;
  14. reg [11:0] limit_lower_raw = 12'b0;
  15. reg [11:0] limit_upper_raw = 12'b0;
  16. reg hood_switch_in = 1'b0;
  17. wire buzzer_out;
  18. wire [23:0] ptc_pwm_out;
  19. wire [3:0] fan_pwm_out;
  20. wire motor_en_n;
  21. wire [11:0] motor_step_gate;
  22. wire [11:0] motor_dir_gate;
  23. wire x_step = motor_step_gate[0];
  24. wire x_dir = motor_dir_gate[0];
  25. wire y_step = motor_step_gate[1];
  26. wire y_dir = motor_dir_gate[1];
  27. wire z_step = motor_step_gate[2];
  28. wire z_dir = motor_dir_gate[2];
  29. always #20 sys_clk = ~sys_clk; // 25 MHz clock (40ns period)
  30. // Pullups for I2C
  31. pullup(temp_scl);
  32. pullup(temp_sda);
  33. // Production physical top
  34. // This PTY regression intentionally stays at 115200; production defaults
  35. // may change independently. Both BFMs below use the matching 217 clocks.
  36. prod2u_mrdimm_rs232_only_top #(.HOST_BAUD(115200)) dut (
  37. .sys_clk(sys_clk),
  38. .host_rs232_rx(host_rs232_rx),
  39. .host_rs232_tx(host_rs232_tx),
  40. .host_ttl_rx(1'b1),
  41. .host_ttl_tx(host_ttl_tx),
  42. .bmc_rs232_rx(1'b1),
  43. .bmc_rs232_tx(),
  44. .temp_scl(temp_scl),
  45. .temp_sda(temp_sda),
  46. .limit_lower_raw(limit_lower_raw),
  47. .limit_upper_raw(limit_upper_raw),
  48. .ptc_pwm_out(ptc_pwm_out),
  49. .fan_pwm_out(fan_pwm_out),
  50. .motor_en_n(motor_en_n),
  51. .motor_step_gate(motor_step_gate),
  52. .motor_dir_gate(motor_dir_gate),
  53. .hood_switch_in(hood_switch_in),
  54. .buzzer_out(buzzer_out)
  55. );
  56. // Tie hood switch stimulus to buzzer_out latch in PTY test (presses hood once buzzer turns ON)
  57. // so full_chain_pty_e2e.py can verify both SET_ALL buzzer=1 and GET_ALL hood_switch=1.
  58. always @(posedge sys_clk) begin
  59. if (buzzer_out) hood_switch_in <= 1'b1;
  60. else if (y_steps_count >= 3) hood_switch_in <= 1'b0;
  61. end
  62. // Step edge counters & automated limit triggering
  63. integer x_steps_count = 0;
  64. integer y_steps_count = 0;
  65. integer z_steps_count = 0;
  66. always @(posedge x_step) x_steps_count = x_steps_count + 1;
  67. always @(posedge y_step) begin
  68. y_steps_count = y_steps_count + 1;
  69. if (y_steps_count >= 3) begin
  70. // Trip Axis 1 lower limit at 3 steps
  71. limit_lower_raw[1] <= 1'b1;
  72. end
  73. end
  74. always @(posedge z_step) z_steps_count = z_steps_count + 1;
  75. reg sim_ready = 1'b0;
  76. reg [1023:0] pty_path_str = "/workspace/tmp/mrdimm_pty_slave";
  77. // Initialization & VPI PTY
  78. initial begin
  79. limit_lower_raw = 3'b000;
  80. limit_upper_raw = 3'b000;
  81. host_rs232_rx = 1'b1;
  82. if (!$value$plusargs("PTY_PATH=%s", pty_path_str)) begin
  83. pty_path_str = "/workspace/tmp/mrdimm_pty_slave";
  84. end
  85. $pty_init(pty_path_str);
  86. repeat (2000) @(posedge sys_clk); // Wait for POR (1024 cycles)
  87. sim_ready = 1'b1;
  88. $display("[TB] sim_ready=1 at time %0t (pty_path=%0s)", $time, pty_path_str);
  89. end
  90. // ------------------------------------------------------------------------
  91. // UART TX BFM (PTY -> host_rs232_rx @ 115200 baud 8N1)
  92. // ------------------------------------------------------------------------
  93. integer pty_byte;
  94. integer pty_valid;
  95. integer b_tx;
  96. initial begin
  97. host_rs232_rx = 1'b1;
  98. wait (sim_ready);
  99. forever begin
  100. repeat (20) @(posedge sys_clk);
  101. $pty_poll_read(pty_byte, pty_valid);
  102. if (pty_valid != 0) begin
  103. $display("[TB] TX to DUT: byte 0x%02x at time %0t", pty_byte, $time);
  104. // Start bit
  105. host_rs232_rx = 1'b0;
  106. repeat (BIT_CLKS) @(posedge sys_clk);
  107. // 8 Data bits LSB first
  108. for (b_tx = 0; b_tx < 8; b_tx = b_tx + 1) begin
  109. host_rs232_rx = pty_byte[b_tx];
  110. repeat (BIT_CLKS) @(posedge sys_clk);
  111. end
  112. // Stop bit
  113. host_rs232_rx = 1'b1;
  114. repeat (BIT_CLKS) @(posedge sys_clk);
  115. end
  116. end
  117. end
  118. // ------------------------------------------------------------------------
  119. // UART RX BFM (host_rs232_tx -> PTY @ 115200 baud 8N1)
  120. // ------------------------------------------------------------------------
  121. integer rx_clk_cnt = 0;
  122. reg [3:0] rx_bit = 0;
  123. reg [7:0] rx_shift = 0;
  124. reg rx_busy = 0;
  125. always @(posedge sys_clk) begin
  126. if (!rx_busy && host_rs232_tx === 1'b0) begin
  127. rx_busy <= 1'b1;
  128. rx_clk_cnt <= BIT_CLKS + (BIT_CLKS / 2) - 1;
  129. rx_bit <= 0;
  130. end else if (rx_busy) begin
  131. if (rx_clk_cnt == 0) begin
  132. if (rx_bit < 8) begin
  133. rx_shift[rx_bit] <= host_rs232_tx;
  134. rx_bit <= rx_bit + 1'b1;
  135. rx_clk_cnt <= BIT_CLKS - 1;
  136. end else begin
  137. // Full byte received, send out to PTY
  138. $display("[TB] RX from DUT: byte 0x%02x at time %0t", rx_shift, $time);
  139. $pty_send_byte(rx_shift);
  140. rx_busy <= 1'b0;
  141. end
  142. end else begin
  143. rx_clk_cnt <= rx_clk_cnt - 1;
  144. end
  145. end
  146. end
  147. endmodule