pty_bridge_tb.v 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 = 27; // 921600 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 [2:0] limit_lower_raw = 3'b000;
  15. reg [2:0] limit_upper_raw = 3'b000;
  16. wire [23:0] ptc_pwm_out;
  17. wire [3:0] fan_pwm_out;
  18. wire motor_en_n;
  19. wire x_step, x_dir;
  20. wire y_step, y_dir;
  21. wire z_step, z_dir;
  22. always #20 sys_clk = ~sys_clk; // 25 MHz clock (40ns period)
  23. // Pullups for I2C
  24. pullup(temp_scl);
  25. pullup(temp_sda);
  26. // Production physical top
  27. prod2u_mrdimm_rs232_only_top dut (
  28. .sys_clk(sys_clk),
  29. .host_rs232_rx(host_rs232_rx),
  30. .host_rs232_tx(host_rs232_tx),
  31. .host_ttl_rx(1'b1),
  32. .host_ttl_tx(host_ttl_tx),
  33. .temp_scl(temp_scl),
  34. .temp_sda(temp_sda),
  35. .limit_lower_raw(limit_lower_raw),
  36. .limit_upper_raw(limit_upper_raw),
  37. .ptc_pwm_out(ptc_pwm_out),
  38. .fan_pwm_out(fan_pwm_out),
  39. .motor_en_n(motor_en_n),
  40. .x_step(x_step), .x_dir(x_dir),
  41. .y_step(y_step), .y_dir(y_dir),
  42. .z_step(z_step), .z_dir(z_dir)
  43. );
  44. // Step edge counters & automated limit triggering
  45. integer x_steps_count = 0;
  46. integer y_steps_count = 0;
  47. integer z_steps_count = 0;
  48. always @(posedge x_step) x_steps_count = x_steps_count + 1;
  49. always @(posedge y_step) begin
  50. y_steps_count = y_steps_count + 1;
  51. if (y_steps_count >= 3) begin
  52. // Trip Axis 1 lower limit at 3 steps
  53. limit_lower_raw[1] <= 1'b1;
  54. end
  55. end
  56. always @(posedge z_step) z_steps_count = z_steps_count + 1;
  57. reg sim_ready = 1'b0;
  58. // Initialization & VPI PTY
  59. initial begin
  60. limit_lower_raw = 3'b000;
  61. limit_upper_raw = 3'b000;
  62. host_rs232_rx = 1'b1;
  63. $pty_init("/workspace/tmp/mrdimm_pty_slave");
  64. repeat (2000) @(posedge sys_clk); // Wait for POR (1024 cycles)
  65. sim_ready = 1'b1;
  66. $display("[TB] sim_ready=1 at time %0t", $time);
  67. end
  68. // ------------------------------------------------------------------------
  69. // UART TX BFM (PTY -> host_rs232_rx @ 921600 baud 8N1)
  70. // ------------------------------------------------------------------------
  71. integer pty_byte;
  72. integer pty_valid;
  73. integer b_tx;
  74. initial begin
  75. host_rs232_rx = 1'b1;
  76. wait (sim_ready);
  77. forever begin
  78. @(posedge sys_clk);
  79. $pty_poll_read(pty_byte, pty_valid);
  80. if (pty_valid != 0) begin
  81. $display("[TB] TX to DUT: byte 0x%02x at time %0t", pty_byte, $time);
  82. // Start bit
  83. host_rs232_rx = 1'b0;
  84. repeat (BIT_CLKS) @(posedge sys_clk);
  85. // 8 Data bits LSB first
  86. for (b_tx = 0; b_tx < 8; b_tx = b_tx + 1) begin
  87. host_rs232_rx = pty_byte[b_tx];
  88. repeat (BIT_CLKS) @(posedge sys_clk);
  89. end
  90. // Stop bit
  91. host_rs232_rx = 1'b1;
  92. repeat (BIT_CLKS) @(posedge sys_clk);
  93. end
  94. end
  95. end
  96. // ------------------------------------------------------------------------
  97. // UART RX BFM (host_rs232_tx -> PTY @ 921600 baud 8N1)
  98. // ------------------------------------------------------------------------
  99. integer rx_clk_cnt = 0;
  100. reg [3:0] rx_bit = 0;
  101. reg [7:0] rx_shift = 0;
  102. reg rx_busy = 0;
  103. always @(posedge sys_clk) begin
  104. if (!rx_busy && host_rs232_tx === 1'b0) begin
  105. rx_busy <= 1'b1;
  106. rx_clk_cnt <= BIT_CLKS + (BIT_CLKS / 2) - 1;
  107. rx_bit <= 0;
  108. end else if (rx_busy) begin
  109. if (rx_clk_cnt == 0) begin
  110. if (rx_bit < 8) begin
  111. rx_shift[rx_bit] <= host_rs232_tx;
  112. rx_bit <= rx_bit + 1'b1;
  113. rx_clk_cnt <= BIT_CLKS - 1;
  114. end else begin
  115. // Full byte received, send out to PTY
  116. $display("[TB] RX from DUT: byte 0x%02x at time %0t", rx_shift, $time);
  117. $pty_send_byte(rx_shift);
  118. rx_busy <= 1'b0;
  119. end
  120. end else begin
  121. rx_clk_cnt <= rx_clk_cnt - 1;
  122. end
  123. end
  124. end
  125. endmodule