| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- `timescale 1ns/1ps
- // PTY Bridge Testbench for Full-Chain MRDIMM Closed Loop Simulation.
- // Uses VPI non-blocking PTY interface.
- // Instantiates the production physical top: prod2u_mrdimm_rs232_only_top.
- module pty_bridge_tb;
- localparam integer CLOCK_HZ = 25000000;
- localparam integer BIT_CLKS = 217; // 115200 baud @ 25 MHz
- reg sys_clk = 1'b0;
- reg host_rs232_rx = 1'b1;
- wire host_rs232_tx;
- wire host_ttl_tx;
- wire temp_scl;
- wire temp_sda;
- reg [11:0] limit_lower_raw = 12'b0;
- reg [11:0] limit_upper_raw = 12'b0;
- reg hood_switch_in = 1'b0;
- wire buzzer_out;
- wire [23:0] ptc_pwm_out;
- wire [3:0] fan_pwm_out;
- wire motor_en_n;
- wire [11:0] motor_step_gate;
- wire [11:0] motor_dir_gate;
- wire x_step = motor_step_gate[0];
- wire x_dir = motor_dir_gate[0];
- wire y_step = motor_step_gate[1];
- wire y_dir = motor_dir_gate[1];
- wire z_step = motor_step_gate[2];
- wire z_dir = motor_dir_gate[2];
- always #20 sys_clk = ~sys_clk; // 25 MHz clock (40ns period)
- // Pullups for I2C
- pullup(temp_scl);
- pullup(temp_sda);
- // Production physical top
- // This PTY regression intentionally stays at 115200; production defaults
- // may change independently. Both BFMs below use the matching 217 clocks.
- prod2u_mrdimm_rs232_only_top #(.HOST_BAUD(115200)) dut (
- .sys_clk(sys_clk),
- .host_rs232_rx(host_rs232_rx),
- .host_rs232_tx(host_rs232_tx),
- .host_ttl_rx(1'b1),
- .host_ttl_tx(host_ttl_tx),
- .bmc_rs232_rx(1'b1),
- .bmc_rs232_tx(),
- .temp_scl(temp_scl),
- .temp_sda(temp_sda),
- .limit_lower_raw(limit_lower_raw),
- .limit_upper_raw(limit_upper_raw),
- .ptc_pwm_out(ptc_pwm_out),
- .fan_pwm_out(fan_pwm_out),
- .motor_en_n(motor_en_n),
- .motor_step_gate(motor_step_gate),
- .motor_dir_gate(motor_dir_gate),
- .hood_switch_in(hood_switch_in),
- .buzzer_out(buzzer_out)
- );
- // Tie hood switch stimulus to buzzer_out latch in PTY test (presses hood once buzzer turns ON)
- // so full_chain_pty_e2e.py can verify both SET_ALL buzzer=1 and GET_ALL hood_switch=1.
- always @(posedge sys_clk) begin
- if (buzzer_out) hood_switch_in <= 1'b1;
- else if (y_steps_count >= 3) hood_switch_in <= 1'b0;
- end
- // Step edge counters & automated limit triggering
- integer x_steps_count = 0;
- integer y_steps_count = 0;
- integer z_steps_count = 0;
- always @(posedge x_step) x_steps_count = x_steps_count + 1;
- always @(posedge y_step) begin
- y_steps_count = y_steps_count + 1;
- if (y_steps_count >= 3) begin
- // Trip Axis 1 lower limit at 3 steps
- limit_lower_raw[1] <= 1'b1;
- end
- end
- always @(posedge z_step) z_steps_count = z_steps_count + 1;
- reg sim_ready = 1'b0;
- reg [1023:0] pty_path_str = "/workspace/tmp/mrdimm_pty_slave";
- // Initialization & VPI PTY
- initial begin
- limit_lower_raw = 3'b000;
- limit_upper_raw = 3'b000;
- host_rs232_rx = 1'b1;
- if (!$value$plusargs("PTY_PATH=%s", pty_path_str)) begin
- pty_path_str = "/workspace/tmp/mrdimm_pty_slave";
- end
- $pty_init(pty_path_str);
- repeat (2000) @(posedge sys_clk); // Wait for POR (1024 cycles)
- sim_ready = 1'b1;
- $display("[TB] sim_ready=1 at time %0t (pty_path=%0s)", $time, pty_path_str);
- end
- // ------------------------------------------------------------------------
- // UART TX BFM (PTY -> host_rs232_rx @ 115200 baud 8N1)
- // ------------------------------------------------------------------------
- integer pty_byte;
- integer pty_valid;
- integer b_tx;
- initial begin
- host_rs232_rx = 1'b1;
- wait (sim_ready);
- forever begin
- repeat (20) @(posedge sys_clk);
- $pty_poll_read(pty_byte, pty_valid);
- if (pty_valid != 0) begin
- $display("[TB] TX to DUT: byte 0x%02x at time %0t", pty_byte, $time);
- // Start bit
- host_rs232_rx = 1'b0;
- repeat (BIT_CLKS) @(posedge sys_clk);
- // 8 Data bits LSB first
- for (b_tx = 0; b_tx < 8; b_tx = b_tx + 1) begin
- host_rs232_rx = pty_byte[b_tx];
- repeat (BIT_CLKS) @(posedge sys_clk);
- end
- // Stop bit
- host_rs232_rx = 1'b1;
- repeat (BIT_CLKS) @(posedge sys_clk);
- end
- end
- end
- // ------------------------------------------------------------------------
- // UART RX BFM (host_rs232_tx -> PTY @ 115200 baud 8N1)
- // ------------------------------------------------------------------------
- integer rx_clk_cnt = 0;
- reg [3:0] rx_bit = 0;
- reg [7:0] rx_shift = 0;
- reg rx_busy = 0;
- always @(posedge sys_clk) begin
- if (!rx_busy && host_rs232_tx === 1'b0) begin
- rx_busy <= 1'b1;
- rx_clk_cnt <= BIT_CLKS + (BIT_CLKS / 2) - 1;
- rx_bit <= 0;
- end else if (rx_busy) begin
- if (rx_clk_cnt == 0) begin
- if (rx_bit < 8) begin
- rx_shift[rx_bit] <= host_rs232_tx;
- rx_bit <= rx_bit + 1'b1;
- rx_clk_cnt <= BIT_CLKS - 1;
- end else begin
- // Full byte received, send out to PTY
- $display("[TB] RX from DUT: byte 0x%02x at time %0t", rx_shift, $time);
- $pty_send_byte(rx_shift);
- rx_busy <= 1'b0;
- end
- end else begin
- rx_clk_cnt <= rx_clk_cnt - 1;
- end
- end
- end
- endmodule
|