| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- `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 = 27; // 921600 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 [2:0] limit_lower_raw = 3'b000;
- reg [2:0] limit_upper_raw = 3'b000;
- wire [23:0] ptc_pwm_out;
- wire [3:0] fan_pwm_out;
- wire motor_en_n;
- wire x_step, x_dir;
- wire y_step, y_dir;
- wire z_step, z_dir;
- always #20 sys_clk = ~sys_clk; // 25 MHz clock (40ns period)
- // Pullups for I2C
- pullup(temp_scl);
- pullup(temp_sda);
- // Production physical top
- prod2u_mrdimm_rs232_only_top 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),
- .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),
- .x_step(x_step), .x_dir(x_dir),
- .y_step(y_step), .y_dir(y_dir),
- .z_step(z_step), .z_dir(z_dir)
- );
- // 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;
- // Initialization & VPI PTY
- initial begin
- limit_lower_raw = 3'b000;
- limit_upper_raw = 3'b000;
- host_rs232_rx = 1'b1;
- $pty_init("/workspace/tmp/mrdimm_pty_slave");
- repeat (2000) @(posedge sys_clk); // Wait for POR (1024 cycles)
- sim_ready = 1'b1;
- $display("[TB] sim_ready=1 at time %0t", $time);
- end
- // ------------------------------------------------------------------------
- // UART TX BFM (PTY -> host_rs232_rx @ 921600 baud 8N1)
- // ------------------------------------------------------------------------
- integer pty_byte;
- integer pty_valid;
- integer b_tx;
- initial begin
- host_rs232_rx = 1'b1;
- wait (sim_ready);
- forever begin
- @(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 @ 921600 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
|