| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- `timescale 1ns/1ps
- 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;
- wire x_dir;
- wire y_step;
- wire y_dir;
- reg x_limit_raw = 1'b0;
- reg y_limit_raw = 1'b0;
- tri temp_scl;
- tri temp_sda;
- localparam integer FAST_TICK_DIV = 10;
- reg [31:0] fast_tick_div;
- reg fast_tick;
- integer startup_x_steps;
- integer startup_y_steps;
- integer host_x_steps;
- integer host_y_steps;
- reg prev_x_step;
- reg prev_y_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),
- .x_limit_raw(x_limit_raw), .y_limit_raw(y_limit_raw),
- .temp_scl(temp_scl), .temp_sda(temp_sda)
- );
- // Accelerate the 500us tick so the motor engine completes in simulation time.
- 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;
- 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 = 2'b11;
- force dut.host_motor_dir_mask = 2'b11;
- @(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;
- host_x_steps = 0;
- host_y_steps = 0;
- startup_phase = 1'b1;
- prev_x_step = 1'b0;
- prev_y_step = 1'b0;
- // Scenario: both raw limits are already high at power-on.
- x_limit_raw = 1'b1;
- y_limit_raw = 1'b1;
- // Wait for power-on reset and synchronizer valid.
- repeat (1100) @(posedge sys_clk);
- wait_for_startup_done();
- if (startup_x_steps !== 0)
- $fatal(1, "x_step rising edges during startup with both limits high: %0d", startup_x_steps);
- if (startup_y_steps !== 0)
- $fatal(1, "y_step rising edges during startup with both limits high: %0d", startup_y_steps);
- // Hand over to host-motor phase and count steps independently.
- startup_phase = 1'b0;
- drive_host_motor();
- // Wait for the host motor to finish 450 steps on each axis.
- while (!dut.u_motor_engine.done) @(posedge sys_clk);
- if (host_x_steps !== 450)
- $fatal(1, "expected host x_step=450, got %0d", host_x_steps);
- if (host_y_steps !== 450)
- $fatal(1, "expected host y_step=450, got %0d", host_y_steps);
- // Startup pulses must not satisfy the 450-step host motor assertion.
- if (startup_x_steps == 450)
- $fatal(1, "startup x_step count must not equal host 450-step assertion");
- if (startup_y_steps == 450)
- $fatal(1, "startup y_step count must not equal host 450-step assertion");
- $display("prod2u_p4_normal_v3_top_startup_tb PASS");
- $finish;
- end
- initial begin
- #100000000 $fatal(1, "top startup test timeout");
- end
- endmodule
|