pty_6400_v2_bridge_tb.v 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. `timescale 1ns/1ps
  2. // 1:1 Physical-Circuit PTY Bridge Testbench for 6400 V2.0 Closed-Loop Co-Simulation.
  3. // Connects production prod2u_6400_v2_top (with real CT7428 U6 I2C BFM, real BMC
  4. // 115200 UART BFM, 2N7002 NMOS fan output inverters, SSR1/2/4, V3.0 buttons
  5. // (EPB1=F8, SB1=C11, SB2=A11), and 4000Hz 80% buzzer H6) to Linux host software
  6. // over a non-blocking VPI PTY.
  7. module pty_6400_v2_bridge_tb;
  8. localparam integer CLOCK_HZ = 25000000;
  9. localparam integer BIT_CLKS = 27; // Production 921600 baud @ 25 MHz (8x faster & 1:1 production rate)
  10. reg sys_clk = 1'b0;
  11. reg host_ttl_rx = 1'b1;
  12. wire host_ttl_tx;
  13. wire host_rs232_tx;
  14. wire bmc_rx;
  15. wire bmc_tx;
  16. tri1 temp_scl;
  17. tri1 temp_sda;
  18. wire temp_scl_alt;
  19. wire temp_sda_alt;
  20. wire [23:0] ptc_pwm_out;
  21. wire [10:0] ptc_spare_zero;
  22. wire [3:0] front_fan_pwm_out;
  23. wire [3:0] rear_fan_pwm_out;
  24. wire ssr1_out, ssr2_out, ssr4_out;
  25. wire [2:0] ssr_unused_zero;
  26. reg estop_btn_in = 1'b0;
  27. reg hood_open_btn_in = 1'b0;
  28. reg hood_close_btn_in = 1'b0;
  29. wire buzzer_out;
  30. // 1:1 physical PCB 2N7002 NMOS fan-PWM inverting stage (+5V pullup on connector)
  31. wire [3:0] front_fan_connector_pwm;
  32. wire [3:0] rear_fan_connector_pwm;
  33. genvar gi;
  34. generate
  35. for (gi = 0; gi < 4; gi = gi + 1) begin : g_fan_nmos
  36. pullup p_front(front_fan_connector_pwm[gi]);
  37. nmos q_front(front_fan_connector_pwm[gi], 1'b0, front_fan_pwm_out[gi]);
  38. pullup p_rear(rear_fan_connector_pwm[gi]);
  39. nmos q_rear(rear_fan_connector_pwm[gi], 1'b0, rear_fan_pwm_out[gi]);
  40. end
  41. endgenerate
  42. always #20 sys_clk = ~sys_clk; // 25 MHz (40 ns)
  43. prod2u_6400_v2_top #(
  44. .HOST_BAUD(921600),
  45. .FAN_BOARD_INVERT_MASK(8'hFF)
  46. ) dut (
  47. .sys_clk(sys_clk),
  48. .host_ttl_rx(host_ttl_rx),
  49. .host_ttl_tx(host_ttl_tx),
  50. .host_rs232_rx(1'b1),
  51. .host_rs232_tx(host_rs232_tx),
  52. .bmc_rs232_rx(bmc_rx),
  53. .bmc_rs232_tx(bmc_tx),
  54. .ptc_pwm_out(ptc_pwm_out),
  55. .ptc_spare_zero(ptc_spare_zero),
  56. .front_fan_pwm_out(front_fan_pwm_out),
  57. .rear_fan_pwm_out(rear_fan_pwm_out),
  58. .temp_scl(temp_scl),
  59. .temp_sda(temp_sda),
  60. .temp_scl_alt(temp_scl_alt),
  61. .temp_sda_alt(temp_sda_alt),
  62. .ssr1_out(ssr1_out),
  63. .ssr2_out(ssr2_out),
  64. .ssr4_out(ssr4_out),
  65. .ssr_unused_zero(ssr_unused_zero),
  66. .estop_btn_in(estop_btn_in),
  67. .hood_open_btn_in(hood_open_btn_in),
  68. .hood_close_btn_in(hood_close_btn_in),
  69. .buzzer_out(buzzer_out)
  70. );
  71. // Timer-only elaboration acceleration for BMC poll so live BMC temperatures
  72. // appear quickly in PTY co-simulation without changing UART/I2C timing.
  73. defparam dut.u_bmc_poll.POLL_SECONDS = 0;
  74. defparam dut.u_bmc_poll.ROUND_TIMEOUT_CLKS = 750000;
  75. wire [31:0] ct_reads, bmc_commands, bmc_replies;
  76. wire [8:0] ct_registers;
  77. ct7428_normal_slave_bfm #(
  78. .I2C_ADDRESS(7'h49),
  79. .DIAGNOSTIC_REGISTERS(1)
  80. ) ct_u6 (
  81. .scl(temp_scl),
  82. .sda(temp_sda),
  83. .completed_reads(ct_reads),
  84. .registers_seen(ct_registers)
  85. );
  86. bmc_uart_normal_bfm #(
  87. .RESPOND_ROUNDS(100)
  88. ) bmc (
  89. .rx_from_fpga(bmc_tx),
  90. .tx_to_fpga(bmc_rx),
  91. .commands_seen(bmc_commands),
  92. .replies_sent(bmc_replies)
  93. );
  94. // Physical board stimulus: detect 4000 Hz 80% buzzer_out pulses (LOW phase is 1250 clks)
  95. // to stably assert EPB1=1 & SB1=1 (SB2=0) when buzzer is active so host Python co-sim
  96. // verifies boot=0/0/0, active=1/1/0, and zero CPLD safety interlock on EPB1.
  97. reg [11:0] buzzer_low_cnt = 12'd0;
  98. always @(posedge sys_clk) begin
  99. if (buzzer_out) begin
  100. buzzer_low_cnt <= 12'd0;
  101. estop_btn_in <= 1'b1;
  102. hood_open_btn_in <= 1'b1;
  103. hood_close_btn_in <= 1'b0;
  104. end else if (buzzer_low_cnt < 12'd2500) begin
  105. buzzer_low_cnt <= buzzer_low_cnt + 12'd1;
  106. end else begin
  107. estop_btn_in <= 1'b0;
  108. hood_open_btn_in <= 1'b0;
  109. hood_close_btn_in <= 1'b0;
  110. end
  111. end
  112. reg sim_ready = 1'b0;
  113. reg [1023:0] pty_path_str = "/workspace/tmp/m6400_v2_pty_slave";
  114. initial begin
  115. host_ttl_rx = 1'b1;
  116. if (!$value$plusargs("PTY_PATH=%s", pty_path_str)) begin
  117. pty_path_str = "/workspace/tmp/m6400_v2_pty_slave";
  118. end
  119. $pty_init(pty_path_str);
  120. // Wait for POR + initial CT7428 U6 scan (9 regs) so real sensor temps exist
  121. repeat (2000) @(posedge sys_clk);
  122. sim_ready = 1'b1;
  123. $display("[TB-6400V2] sim_ready=1 at time %0t (pty_path=%0s)", $time, pty_path_str);
  124. end
  125. // UART TX BFM (PTY -> host_ttl_rx @ 115200 8N1)
  126. integer pty_byte;
  127. integer pty_valid;
  128. integer b_tx;
  129. initial begin
  130. host_ttl_rx = 1'b1;
  131. wait (sim_ready);
  132. forever begin
  133. repeat (20) @(posedge sys_clk);
  134. $pty_poll_read(pty_byte, pty_valid);
  135. if (pty_valid != 0) begin
  136. host_ttl_rx = 1'b0;
  137. repeat (BIT_CLKS) @(posedge sys_clk);
  138. for (b_tx = 0; b_tx < 8; b_tx = b_tx + 1) begin
  139. host_ttl_rx = pty_byte[b_tx];
  140. repeat (BIT_CLKS) @(posedge sys_clk);
  141. end
  142. host_ttl_rx = 1'b1;
  143. repeat (BIT_CLKS) @(posedge sys_clk);
  144. end
  145. end
  146. end
  147. // UART RX BFM (host_ttl_tx -> PTY @ 115200 8N1)
  148. integer rx_clk_cnt = 0;
  149. reg [3:0] rx_bit = 0;
  150. reg [7:0] rx_shift = 0;
  151. reg rx_busy = 0;
  152. always @(posedge sys_clk) begin
  153. if (!rx_busy && host_ttl_tx === 1'b0) begin
  154. rx_busy <= 1'b1;
  155. rx_clk_cnt <= BIT_CLKS + (BIT_CLKS / 2) - 1;
  156. rx_bit <= 0;
  157. end else if (rx_busy) begin
  158. if (rx_clk_cnt == 0) begin
  159. if (rx_bit < 8) begin
  160. rx_shift[rx_bit] <= host_ttl_tx;
  161. rx_bit <= rx_bit + 1'b1;
  162. rx_clk_cnt <= BIT_CLKS - 1;
  163. end else begin
  164. $pty_send_byte(rx_shift);
  165. rx_busy <= 1'b0;
  166. end
  167. end else begin
  168. rx_clk_cnt <= rx_clk_cnt - 1;
  169. end
  170. end
  171. end
  172. endmodule