`timescale 1ns/1ps module startup_close_seek_tb; reg clk = 1'b0; reg rst_n = 1'b0; reg tick_500us = 1'b0; reg [1:0] limit = 2'b00; reg limit_sync_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; reg motor_done; integer burst_count; integer tick_count; integer motor_tick; reg previous_motor_start; always #1 clk = ~clk; 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) ) dut ( .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us), .limit_sync_valid(limit_sync_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) ); task pulse_tick; begin @(negedge clk); tick_500us = 1'b1; @(negedge clk); tick_500us = 1'b0; end endtask task wait_sync_valid; begin // startup_close_seek transitions out of WAIT_SYNC only when limit_sync_valid is 1. repeat (3) @(posedge clk); limit_sync_valid = 1'b1; end endtask // Fake motor engine: assert done for one cycle shortly after start. reg motor_busy; always @(posedge clk or negedge rst_n) begin if (!rst_n) begin motor_done <= 1'b0; motor_tick <= 0; motor_busy <= 1'b0; previous_motor_start <= 1'b0; end else begin previous_motor_start <= motor_start; if (motor_start && !previous_motor_start) begin motor_busy <= 1'b1; motor_tick <= 0; end else if (motor_busy) begin if (motor_tick < 3) begin motor_tick <= motor_tick + 1; motor_done <= 1'b0; end else begin motor_done <= 1'b1; motor_busy <= 1'b0; end end else begin motor_done <= 1'b0; end end end initial begin burst_count = 0; #10000000 $fatal(1, "startup close-seek timeout"); end always @(posedge clk) begin if (motor_start && !previous_motor_start) burst_count <= burst_count + 1; end always @(posedge clk) begin if (!limit_sync_valid && motor_start) begin $fatal(1, "motor_start asserted before limit_sync_valid"); end end initial begin // Scenario 1: both limits already high. Startup must finish without any motor burst. limit = 2'b11; limit_sync_valid = 1'b0; burst_count = 0; repeat (3) @(posedge clk); rst_n = 1'b1; wait_sync_valid(); while (!startup_done) begin pulse_tick(); end if (burst_count !== 0) $fatal(1, "expected 0 bursts when both limits are high, got %0d", burst_count); if (motor_active_mask !== 2'b00) $fatal(1, "motor_active_mask not zero after done"); if (motor_dir_mask !== 2'b00) $fatal(1, "motor_dir_mask not zero after done"); if (motor_start !== 1'b0) $fatal(1, "motor_start not zero after done"); // Scenario 2: no limit. Should run MAX_BURSTS (10) bursts and then assert done. rst_n = 1'b0; limit = 2'b00; limit_sync_valid = 1'b0; burst_count = 0; @(negedge clk); repeat (3) @(posedge clk); rst_n = 1'b1; wait_sync_valid(); while (!startup_done) begin pulse_tick(); end if (burst_count !== 10) $fatal(1, "expected 10 bursts without limit, got %0d", burst_count); if (motor_active_mask !== 2'b00) $fatal(1, "motor_active_mask not zero after done"); if (motor_dir_mask !== 2'b00) $fatal(1, "motor_dir_mask not zero after done"); if (motor_start !== 1'b0) $fatal(1, "motor_start not zero after done"); if (motor_stop_mask !== 2'b00) $fatal(1, "motor_stop_mask not zero after done"); // Post-calibration inertness: toggling limits must not restart or assert stop. burst_count = 0; limit = 2'b11; repeat (20) pulse_tick(); limit = 2'b00; repeat (20) pulse_tick(); if (!startup_done) $fatal(1, "startup_done cleared after post-done limit toggle"); if (burst_count !== 0) $fatal(1, "post-done limit toggle restarted bursts"); if (motor_start !== 1'b0) $fatal(1, "post-done motor_start reasserted"); if (motor_stop_mask !== 2'b00) $fatal(1, "post-done motor_stop_mask not inert"); if (motor_active_mask !== 2'b00) $fatal(1, "post-done motor_active_mask not zero"); $display("startup_close_seek_tb PASS"); $finish; end endmodule