`timescale 1ns/1ps // Power-on / reconfig close-seek one-shot for motor-configured dampers only. // AXIS_ENABLE_MASK selects which axes participate (default all). Solenoid // intake is never moved here (production AXIS_COUNT=2). Motor-compat uses // AXIS_COUNT=3 with bit2=intake. DIR_MASK defaults to close (DIR=1). // After sticky startup_done: no further starts, stop_mask stays 0 at the top. module startup_close_seek #( /* verilator lint_off UNUSEDPARAM */ parameter integer FIXED_STEPS = 450, parameter integer DIR_SETUP_TICKS = 2, parameter integer STEP_HALF_CYCLES = 12500, /* verilator lint_on UNUSEDPARAM */ parameter integer MAX_BURSTS = 10, parameter integer INTER_BURST_TICKS = 100, parameter integer AXIS_COUNT = 2, parameter [AXIS_COUNT-1:0] DIR_MASK = {AXIS_COUNT{1'b1}}, parameter [AXIS_COUNT-1:0] AXIS_ENABLE_MASK = {AXIS_COUNT{1'b1}} ) ( input wire clk, input wire rst_n, input wire tick_500us, input wire limit_sync_valid, input wire [AXIS_COUNT-1:0] limit_sync, output reg startup_done, output reg motor_start, output reg [AXIS_COUNT-1:0] motor_active_mask, output reg [AXIS_COUNT-1:0] motor_dir_mask, output wire [AXIS_COUNT-1:0] motor_stop_mask, input wire motor_done ); localparam integer BURST_WIDTH = (MAX_BURSTS <= 1) ? 1 : $clog2(MAX_BURSTS + 1); localparam integer TICK_WIDTH = (INTER_BURST_TICKS <= 1) ? 1 : $clog2(INTER_BURST_TICKS + 1); localparam [AXIS_COUNT-1:0] AXIS_ZERO = {AXIS_COUNT{1'b0}}; localparam [2:0] ST_WAIT_SYNC = 3'd0; localparam [2:0] ST_CHECK = 3'd1; localparam [2:0] ST_START = 3'd2; localparam [2:0] ST_WAIT_DONE = 3'd3; localparam [2:0] ST_WAIT_IDLE = 3'd4; localparam [2:0] ST_DONE = 3'd5; reg [2:0] state; reg [AXIS_COUNT-1:0] reached_mask; reg [BURST_WIDTH-1:0] burst_count [0:AXIS_COUNT-1]; reg [TICK_WIDTH-1:0] idle_tick; integer ai; wire [AXIS_COUNT-1:0] reached_mask_next = reached_mask | (limit_sync_valid ? limit_sync : AXIS_ZERO); reg [AXIS_COUNT-1:0] exhausted_mask; always @(*) begin exhausted_mask = AXIS_ZERO; for (ai = 0; ai < AXIS_COUNT; ai = ai + 1) begin if (burst_count[ai] >= MAX_BURSTS) exhausted_mask[ai] = 1'b1; end end wire [AXIS_COUNT-1:0] move_mask = ~reached_mask_next & ~exhausted_mask & AXIS_ENABLE_MASK; assign motor_stop_mask = (state == ST_WAIT_DONE) ? (reached_mask_next & motor_active_mask) : AXIS_ZERO; always @(posedge clk or negedge rst_n) begin if (!rst_n) begin state <= ST_WAIT_SYNC; startup_done <= 1'b0; motor_start <= 1'b0; motor_active_mask <= AXIS_ZERO; motor_dir_mask <= AXIS_ZERO; reached_mask <= AXIS_ZERO; idle_tick <= {TICK_WIDTH{1'b0}}; for (ai = 0; ai < AXIS_COUNT; ai = ai + 1) burst_count[ai] <= {BURST_WIDTH{1'b0}}; end else begin reached_mask <= reached_mask_next; case (state) ST_WAIT_SYNC: begin motor_start <= 1'b0; if (limit_sync_valid) begin state <= ST_CHECK; end end ST_CHECK: begin motor_start <= 1'b0; if (move_mask == AXIS_ZERO) begin state <= ST_DONE; startup_done <= 1'b1; end else begin state <= ST_START; end end ST_START: begin motor_active_mask <= move_mask; motor_dir_mask <= DIR_MASK; motor_start <= 1'b1; state <= ST_WAIT_DONE; end ST_WAIT_DONE: begin motor_start <= 1'b0; if (motor_done) begin for (ai = 0; ai < AXIS_COUNT; ai = ai + 1) begin if (motor_active_mask[ai]) burst_count[ai] <= burst_count[ai] + 1'b1; end idle_tick <= {TICK_WIDTH{1'b0}}; state <= ST_WAIT_IDLE; end end ST_WAIT_IDLE: begin if (tick_500us) begin if (idle_tick + 1'b1 >= INTER_BURST_TICKS) begin state <= ST_CHECK; end else begin idle_tick <= idle_tick + 1'b1; end end end ST_DONE: begin startup_done <= 1'b1; motor_start <= 1'b0; motor_active_mask <= AXIS_ZERO; motor_dir_mask <= AXIS_ZERO; end default: state <= ST_DONE; endcase end end endmodule