| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- `timescale 1ns/1ps
- // Top-level startup: all LS already high -> startup_done without motion; then host 450-step jog.
- module prod2u_p4_normal_v3_top_startup_tb;
- reg sys_clk = 1'b0;
- reg host_uart_rx = 1'b1;
- wire host_uart_tx;
- wire [7:0] ptc_pwm_out;
- wire [3:0] fan_pwm_out;
- wire motor_en_n;
- wire x_step, x_dir, y_step, y_dir, z_step, z_dir;
- reg x_limit_ls = 1'b1;
- reg x_limit_hs = 1'b0;
- reg y_limit_ls = 1'b1;
- reg y_limit_hs = 1'b0;
- reg z_limit_ls = 1'b1;
- reg z_limit_hs = 1'b0;
- tri temp_scl;
- tri temp_sda;
- localparam integer FAST_TICK_DIV = 10;
- localparam integer HOST_STEPS = 450;
- reg [31:0] fast_tick_div;
- reg fast_tick;
- integer startup_x_steps, startup_y_steps, startup_z_steps;
- integer host_x_steps, host_y_steps;
- reg prev_x_step, prev_y_step, prev_z_step;
- reg startup_phase;
- always #1 sys_clk = ~sys_clk;
- prod2u_p4_normal_v3_top dut (
- .sys_clk(sys_clk), .host_uart_rx(host_uart_rx), .host_uart_tx(host_uart_tx),
- .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),
- .x_limit_ls(x_limit_ls), .x_limit_hs(x_limit_hs),
- .y_limit_ls(y_limit_ls), .y_limit_hs(y_limit_hs),
- .z_limit_ls(z_limit_ls), .z_limit_hs(z_limit_hs),
- .temp_scl(temp_scl), .temp_sda(temp_sda)
- );
- initial begin
- fast_tick = 1'b0;
- fast_tick_div = 0;
- force dut.tick_500us = fast_tick;
- end
- always @(posedge sys_clk) begin
- if (fast_tick_div + 1 >= FAST_TICK_DIV) begin
- fast_tick_div <= 0;
- fast_tick <= 1'b1;
- end else begin
- fast_tick_div <= fast_tick_div + 1'b1;
- fast_tick <= 1'b0;
- end
- end
- always @(posedge sys_clk) begin
- prev_x_step <= x_step;
- prev_y_step <= y_step;
- if (startup_phase) begin
- if (!prev_x_step && x_step) startup_x_steps <= startup_x_steps + 1;
- if (!prev_y_step && y_step) startup_y_steps <= startup_y_steps + 1;
- if (!prev_z_step && z_step) startup_z_steps <= startup_z_steps + 1;
- end else begin
- if (!prev_x_step && x_step) host_x_steps <= host_x_steps + 1;
- if (!prev_y_step && y_step) host_y_steps <= host_y_steps + 1;
- end
- end
- task wait_for_startup_done;
- begin
- while (!dut.startup_done) @(posedge sys_clk);
- end
- endtask
- task drive_host_motor;
- begin
- @(negedge sys_clk);
- force dut.host_motor_start = 1'b1;
- force dut.host_motor_active_mask = 3'b011;
- force dut.host_motor_dir_mask = 3'b000;
- @(posedge sys_clk);
- @(negedge sys_clk);
- release dut.host_motor_start;
- release dut.host_motor_active_mask;
- release dut.host_motor_dir_mask;
- end
- endtask
- initial begin
- startup_x_steps = 0;
- startup_y_steps = 0;
- startup_z_steps = 0;
- host_x_steps = 0;
- host_y_steps = 0;
- startup_phase = 1'b1;
- prev_x_step = 1'b0;
- prev_y_step = 1'b0;
- prev_z_step = 1'b0;
- repeat (1100) @(posedge sys_clk);
- wait_for_startup_done();
- if (startup_x_steps !== 0)
- $fatal(1, "x_step during startup with all LS high: %0d", startup_x_steps);
- if (startup_y_steps !== 0)
- $fatal(1, "y_step during startup with all LS high: %0d", startup_y_steps);
- if (startup_z_steps !== 0)
- $fatal(1, "z_step during startup with all LS high: %0d", startup_z_steps);
- startup_phase = 1'b0;
- drive_host_motor();
- while (!dut.u_motor_engine.done) @(posedge sys_clk);
- if (host_x_steps !== HOST_STEPS)
- $fatal(1, "expected host x_step=%0d, got %0d", HOST_STEPS, host_x_steps);
- if (host_y_steps !== HOST_STEPS)
- $fatal(1, "expected host y_step=%0d, got %0d", HOST_STEPS, host_y_steps);
- if (!dut.startup_done)
- $fatal(1, "startup_done not sticky after host motor");
- $display("prod2u_p4_normal_v3_top_startup_tb PASS");
- $finish;
- end
- initial begin
- #100000000 $fatal(1, "top startup test timeout");
- end
- endmodule
|