`timescale 1ns/1ps `include "src/generated/prod2u_door_temp_params.vh" // Top startup: axes begin on close limit, measure-open until open limit after // >=2 gated STEP rises, then sticky startup_complete. 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; wire temp_scl, temp_sda; integer x_rises; integer y_rises; integer z_rises; always #20 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) ); always @(posedge x_step) x_rises = x_rises + 1; always @(posedge y_step) y_rises = y_rises + 1; always @(posedge z_step) z_rises = z_rises + 1; initial begin #1_000_000_000 $fatal(1, "top_startup_tb timeout sc=%b rises=%0d/%0d/%0d", dut.startup_complete, x_rises, y_rises, z_rises); end // Serial X→Y→Z calibration: hit open only after that axis's measure-open rises. task automatic cal_hit_open; input integer ax; integer base_x, base_y, base_z; integer k; begin base_x = x_rises; base_y = y_rises; base_z = z_rises; begin : wait_ax for (k = 0; k < 5_000_000; k = k + 1) begin @(posedge sys_clk); if (ax == 0 && (x_rises - base_x) >= 2) disable wait_ax; if (ax == 1 && (y_rises - base_y) >= 2) disable wait_ax; if (ax == 2 && (z_rises - base_z) >= 2) disable wait_ax; end $fatal(1, "insufficient measure rises axis=%0d x=%0d y=%0d z=%0d", ax, x_rises, y_rises, z_rises); end if (ax == 0) begin x_limit_hs = 1'b1; x_limit_ls = 1'b0; end if (ax == 1) begin y_limit_hs = 1'b1; y_limit_ls = 1'b0; end if (ax == 2) begin z_limit_hs = 1'b1; z_limit_ls = 1'b0; end repeat (2000) @(posedge sys_clk); end endtask initial begin x_rises = 0; y_rises = 0; z_rises = 0; cal_hit_open(0); cal_hit_open(1); cal_hit_open(2); begin : wait_sc integer k; for (k = 0; k < 2_000_000; k = k + 1) begin @(posedge sys_clk); if (dut.startup_complete) disable wait_sc; end $fatal(1, "startup_complete not reached"); end if (motor_en_n !== 1'b1) $fatal(1, "EN must remain permanent_high"); repeat (100) @(posedge sys_clk); if (!dut.startup_complete) $fatal(1, "startup_complete not sticky"); $display("prod2u_p4_normal_v3_top_startup_tb PASS"); $finish; end endmodule