`timescale 1ns/1ps module startup_motor_stop_tb; reg clk = 1'b0; reg rst_n = 1'b0; reg tick_500us = 1'b0; reg [1:0] limit = 2'b00; reg limit_valid = 1'b0; wire startup_done; wire motor_start; wire [1:0] motor_active_mask; wire [1:0] motor_dir_mask; wire [1:0] motor_stop_mask; wire motor_busy; wire motor_done; wire [1:0] step; wire [1:0] dir; integer x_rises; integer y_rises; reg prev_x; reg prev_y; integer edge_count; integer x_edge; integer y_edge; localparam integer FAST_TICK_DIV = 10; reg [31:0] fast_tick_div; reg fast_tick; always #1 clk = ~clk; initial begin fast_tick = 1'b0; fast_tick_div = 0; end always @(posedge 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 clk) begin tick_500us <= fast_tick; prev_x <= step[0]; prev_y <= step[1]; if (!prev_x && step[0]) x_rises <= x_rises + 1; if (!prev_y && step[1]) y_rises <= y_rises + 1; end startup_close_seek #( .FIXED_STEPS(450), .DIR_SETUP_TICKS(2), .STEP_HALF_CYCLES(12500), .MAX_BURSTS(10), .INTER_BURST_TICKS(4), .DIR_MASK(2'b11) ) u_startup ( .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us), .limit_sync_valid(limit_valid), .limit_sync(limit), .startup_done(startup_done), .motor_start(motor_start), .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask), .motor_stop_mask(motor_stop_mask), .motor_done(motor_done) ); door_motor_engine #( .FIXED_STEPS(450), .DIR_SETUP_TICKS(2) ) u_motor ( .clk(clk), .rst_n(rst_n), .start(motor_start), .active_mask(motor_active_mask), .dir_mask(motor_dir_mask), .tick_500us(tick_500us), .abort(1'b0), .stop_mask(motor_stop_mask), .busy(motor_busy), .done(motor_done), .step(step), .dir(dir) ); task pulse_tick; begin @(posedge clk); while (!tick_500us) @(posedge clk); end endtask task reset_dut; begin rst_n = 1'b0; limit_valid = 1'b0; x_rises = 0; y_rises = 0; prev_x = 1'b0; prev_y = 1'b0; edge_count = 0; x_edge = 0; y_edge = 0; repeat (5) @(posedge clk); rst_n = 1'b1; // wait for limit synchronizer (simulated by testbench raising limit_valid) repeat (5) @(posedge clk); limit_valid = 1'b1; @(posedge clk); end endtask task wait_startup_done; begin while (!startup_done) pulse_tick(); @(posedge clk); end endtask task wait_reached; input [1:0] mask; begin while (!((u_startup.reached_mask & mask) == mask)) pulse_tick(); @(posedge clk); end endtask initial begin #40000000 $fatal(1, "startup_motor_stop_tb timeout"); end initial begin // Scenario 1: both limits high at reset. limit = 2'b11; reset_dut(); wait_startup_done(); if (x_rises !== 0 || y_rises !== 0) $fatal(1, "scenario1: steps with both limits high x=%0d y=%0d", x_rises, y_rises); // Scenario 2: X limit goes high after X has started stepping. limit = 2'b00; reset_dut(); while (step[0] !== 1'b1) pulse_tick(); limit = 2'b01; wait_reached(2'b01); edge_count = x_rises; while (!startup_done) begin pulse_tick(); if (x_rises > edge_count) $fatal(1, "scenario2: X rose after reached, before=%0d after=%0d", edge_count, x_rises); end if (y_rises < 1) $fatal(1, "scenario2: Y did not continue"); // Scenario 3: Y limit mid-burst, X runs to max bursts. limit = 2'b00; reset_dut(); while (step[1] !== 1'b1) pulse_tick(); limit = 2'b10; wait_reached(2'b10); edge_count = y_rises; wait_startup_done(); if (y_rises > edge_count) $fatal(1, "scenario3: Y rose after reached"); if (x_rises > 4500 + 10 || x_rises < 4500 - 10) $fatal(1, "scenario3: X did not complete ~10 bursts: %0d", x_rises); // Scenario 4: X and Y limits at different times. limit = 2'b00; reset_dut(); while (step[0] !== 1'b1) pulse_tick(); limit = 2'b01; wait_reached(2'b01); x_edge = x_rises; repeat (10) pulse_tick(); limit = 2'b11; wait_reached(2'b11); y_edge = y_rises; wait_startup_done(); if (x_rises > x_edge) $fatal(1, "scenario4: X rose after both reached"); if (y_rises > y_edge) $fatal(1, "scenario4: Y rose after both reached"); // Scenario 5: no limits, both reach max bursts. limit = 2'b00; reset_dut(); wait_startup_done(); if (x_rises !== 4500 || y_rises !== 4500) $fatal(1, "scenario5: max bursts mismatch x=%0d y=%0d", x_rises, y_rises); // Scenario 6: stop at 449th pulse boundary for X. limit = 2'b00; reset_dut(); while (x_rises < 448) pulse_tick(); while (step[0] !== 1'b0 || step[1] !== 1'b0) pulse_tick(); limit = 2'b01; wait_startup_done(); if (x_rises > 449) $fatal(1, "scenario6: X produced a 450th rising edge"); if (y_rises !== 4500) $fatal(1, "scenario6: Y did not finish max bursts: %0d", y_rises); $display("startup_motor_stop_tb PASS"); $finish; end endmodule