Przeglądaj źródła

feat(motor): MRDIMM motor engine first draft (axis core, event ring, shared engine)

WIP toward contract section 10.  motor_axis_core: explicit per-axis FSM
(IDLE/ARMED/SETUP/RUN) with the final STEP decision chain (limits ->
abort/preempt -> scheduler), executed-rise counting on clamped pulses
only, final-high-pulse completion rule, direction setup only on real
direction change, blocked-by-limit termination without forging edges,
limit latches/hit counts on synchronized rises only, position zeroing on
the first lower-limit reference, SET_POSITION / CLEAR_COUNTERS register
ops.  motor_event_ring: 64x16B overwrite-oldest ring with overflow
counter and request/ack writer.  motor_engine: shared queue BRAM
(32x16B/axis + urgent preinstall slot), full-batch validate-then-commit
(MOTOR_QUEUE_WRITE), execution fetch FSM, MOTOR_CONTROL ops, power-on
SEEK_LOWER submission at X/Y=500us Z=20000us half periods.

Known-open (next pass, needs remote iverilog): armed-head bookkeeping on
pop, START_ARMED detail reporting, SET_POSITION single-axis validation,
dead-code sweep.  Not yet compiled: edk2-uefi-lab still down.
codex 3 miesięcy temu
rodzic
commit
f666b2ed1c
3 zmienionych plików z 1013 dodań i 0 usunięć
  1. 340 0
      src/motor/motor_axis_core.v
  2. 600 0
      src/motor/motor_engine.v
  3. 73 0
      src/motor/motor_event_ring.v

+ 340 - 0
src/motor/motor_axis_core.v

@@ -0,0 +1,340 @@
+`timescale 1ns/1ps
+
+// Per-axis motor execution core (2026 MRDIMM contract, section 10).
+//
+// One instance per axis.  STEP/DIR timing runs on a 1 us clock-enable
+// (tick_us).  The final STEP decision happens every system clock with a
+// fixed priority: synchronized limits/reset first, then abort/preempt,
+// then the segment scheduler.  Executed-pulse counters update only on
+// real clamped STEP rises.
+//
+// Segment modes (contract 10.2):
+//   0 relative   : count 1..2^32-1 STEP rises, direction = flags.bit0
+//   1 seek_lower : toward lower limit, count 0 = unlimited
+//   2 seek_upper : toward upper limit, count 0 = unlimited
+//   3 continuous : direction = flags.bit0, count 0 = unlimited
+//   4 hold       : no STEP, count = wait us, half_period must be 0
+//
+// A counted segment completes only after its final STEP high pulse has
+// finished (remaining==0 and step low).  Limits/abort/preempt may truncate
+// a high pulse; the truncation is recorded and never re-sent.
+module motor_axis_core #(
+    parameter integer DIR_SETUP_TICKS = 1000,   // 1 ms at 1 us tick
+    parameter         DIR_LOWER_LEVEL = 1'b0    // board DIR level for "toward lower"
+) (
+    input  wire        clk,
+    input  wire        rst_n,
+    input  wire        tick_us,
+    // live synchronized limits (active high, already 2FF-synced upstream)
+    input  wire        limit_lower,
+    input  wire        limit_upper,
+    // segment load port (queue/urgent select drives it when load_ready)
+    input  wire        seg_load,
+    input  wire [2:0]  seg_mode,
+    input  wire        seg_dir_upper,
+    input  wire        seg_armed,
+    input  wire [31:0] seg_count,
+    input  wire [31:0] seg_half_us,
+    input  wire [15:0] seg_tag,
+    input  wire        start_armed,
+    input  wire        abort_req,
+    input  wire        preempt_req,
+    // outputs
+    output reg         step_out,
+    output reg         dir_out,
+    output wire        active,
+    output wire        armed,
+    output wire        load_ready,
+    // completion / termination report (1-cycle pulse)
+    output reg         done_pulse,
+    output reg  [3:0]  done_reason,
+    output reg  [15:0] done_tag,
+    output reg  [31:0] done_executed,
+    // telemetry
+    output reg  [31:0] position,
+    output reg  [31:0] total_steps,
+    output reg  [31:0] last_executed,
+    output wire        position_reference_seen,
+    output reg  [15:0] lower_hit_count,
+    output reg  [15:0] upper_hit_count,
+    output reg         lower_latched,
+    output reg         upper_latched,
+    output reg         both_latched,
+    output reg  [3:0]  last_stop_reason,
+    input  wire        clear_limit_latch,
+    // direct register maintenance (MOTOR_CONTROL ops)
+    input  wire        position_load,     // single-axis op only
+    input  wire [31:0] position_value,
+    input  wire        counters_clear,    // clears totals; position only with pos_zero
+    input  wire        pos_zero_with_counters
+);
+    localparam [3:0] SR_NONE = 4'd0, SR_COUNT = 4'd1, SR_LOWER = 4'd2,
+        SR_UPPER = 4'd3, SR_ABORT = 4'd4, SR_PREEMPT = 4'd5, SR_RESET = 4'd6,
+        SR_BOTH = 4'd7, SR_HOST_CAP = 4'd8, SR_HOLD = 4'd9;
+    localparam [2:0] M_REL = 3'd0, M_SEEK_LOWER = 3'd1, M_SEEK_UPPER = 3'd2,
+        M_CONT = 3'd3, M_HOLD = 3'd4;
+
+    localparam [1:0] S_IDLE = 2'd0, S_ARMED = 2'd1, S_SETUP = 2'd2, S_RUN = 2'd3;
+    localparam       HALF_LOW = 1'b0, HALF_HIGH = 1'b1;
+
+    reg [1:0]  state;
+    reg [2:0]  r_mode;
+    reg        r_dir_upper;
+    reg [31:0] r_remaining;
+    reg        r_unlimited;   // seek/continuous with count 0
+    reg [31:0] r_half_us;
+    reg [15:0] r_tag;
+    reg        r_half;         // HALF_LOW / HALF_HIGH
+    reg [31:0] r_half_cnt;
+    reg [31:0] r_executed;
+    reg [31:0] r_setup_cnt;
+    reg        r_count_done;   // remaining hit 0; finish the high pulse
+
+    reg        r_ref_seen;
+    reg        abort_pending;
+    reg        preempt_pending;
+
+    reg limit_lower_q, limit_upper_q;
+    reg lower_latched_r, upper_latched_r;
+
+    assign active = (state == S_RUN) || (state == S_SETUP);
+    assign armed = (state == S_ARMED);
+    assign load_ready = (state == S_IDLE);
+    assign position_reference_seen = r_ref_seen;
+
+    wire dir_level = (r_mode == M_SEEK_LOWER) ? DIR_LOWER_LEVEL :
+        (r_mode == M_SEEK_UPPER) ? ~DIR_LOWER_LEVEL :
+        (r_dir_upper ? ~DIR_LOWER_LEVEL : DIR_LOWER_LEVEL);
+    wire step_toward_lower = (dir_level == DIR_LOWER_LEVEL);
+    wire both_latched_now = lower_latched_r && upper_latched_r;
+    // Only the limit ahead of the commanded direction blocks; moving away
+    // from an already-active limit is always allowed.  Both latched: stop.
+    wire limit_blocks = (step_toward_lower && (limit_lower || both_latched_now)) ||
+        (!step_toward_lower && (limit_upper || both_latched_now));
+
+    wire lower_rise = limit_lower && !limit_lower_q;
+    wire upper_rise = limit_upper && !limit_upper_q;
+
+    // ---- limit synchronization, latches and hit statistics ----
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) begin
+            limit_lower_q <= 1'b0; limit_upper_q <= 1'b0;
+            lower_latched_r <= 1'b0; upper_latched_r <= 1'b0;
+            both_latched <= 1'b0;
+            lower_hit_count <= 16'd0; upper_hit_count <= 16'd0;
+        end else begin
+            limit_lower_q <= limit_lower;
+            limit_upper_q <= limit_upper;
+            if (lower_rise) begin
+                lower_hit_count <= lower_hit_count + 16'd1;
+                lower_latched_r <= 1'b1;
+            end
+            if (upper_rise) begin
+                upper_hit_count <= upper_hit_count + 16'd1;
+                upper_latched_r <= 1'b1;
+            end
+            both_latched <= lower_latched_r && upper_latched_r;
+            if (clear_limit_latch) begin
+                lower_latched_r <= 1'b0;
+                upper_latched_r <= 1'b0;
+                both_latched <= 1'b0;
+            end
+        end
+    end
+    assign lower_latched = lower_latched_r;
+    assign upper_latched = upper_latched_r;
+
+    // ---- final STEP decision priority chain ----
+    // 1. limits (combinational, cannot be bypassed)
+    // 2. abort/preempt (truncate current pulse immediately)
+    // 3. scheduler
+    wire step_would_rise = (state == S_RUN) && (r_mode != M_HOLD) &&
+        (r_half == HALF_LOW) && (r_half_cnt + 32'd1 >= r_half_us) && !step_out;
+    wire step_rise_clamped = step_would_rise && (limit_blocks || abort_pending || preempt_pending);
+    wire step_fire = step_would_rise && !step_rise_clamped;
+
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) begin
+            state <= S_IDLE;
+            r_mode <= M_REL; r_dir_upper <= 1'b0;
+            r_remaining <= 32'd0; r_unlimited <= 1'b0; r_half_us <= 32'd1; r_tag <= 16'd0;
+            r_half <= HALF_LOW; r_half_cnt <= 32'd0; r_executed <= 32'd0;
+            r_setup_cnt <= 32'd0; r_count_done <= 1'b0;
+            step_out <= 1'b0; dir_out <= DIR_LOWER_LEVEL;
+            done_pulse <= 1'b0; done_reason <= SR_NONE; done_tag <= 16'd0;
+            done_executed <= 32'd0;
+            position <= 32'd0; total_steps <= 32'd0; last_executed <= 32'd0;
+            last_stop_reason <= SR_NONE;
+            r_ref_seen <= 1'b0;
+            abort_pending <= 1'b0; preempt_pending <= 1'b0;
+        end else begin
+            done_pulse <= 1'b0;
+            if (abort_req) abort_pending <= 1'b1;
+            if (preempt_req) preempt_pending <= 1'b1;
+
+            // ---- limit hit while moving toward it: terminate segment ----
+            if ((state == S_RUN) && (r_mode != M_HOLD) && limit_blocks &&
+                (lower_rise || upper_rise)) begin
+                step_out <= 1'b0;
+                done_pulse <= 1'b1;
+                done_reason <= (step_toward_lower && upper_latched_r) ? SR_BOTH :
+                    (step_toward_lower ? SR_LOWER : SR_UPPER);
+                done_tag <= r_tag;
+                done_executed <= r_executed;
+                last_executed <= r_executed;
+                last_stop_reason <= done_reason;
+                state <= S_IDLE;
+                r_remaining <= 32'd0;
+            end
+            // ---- abort / preempt: terminate immediately ----
+            else if ((state == S_RUN) && (abort_pending || preempt_pending)) begin
+                abort_pending <= 1'b0;
+                preempt_pending <= 1'b0;
+                step_out <= 1'b0;   // truncate a high pulse; never re-sent
+                done_pulse <= 1'b1;
+                done_reason <= abort_pending ? SR_ABORT : SR_PREEMPT;
+                done_tag <= r_tag;
+                done_executed <= r_executed;
+                last_executed <= r_executed;
+                last_stop_reason <= done_reason;
+                state <= S_IDLE;
+                r_remaining <= 32'd0;
+            end
+            // ---- segment load ----
+            else if (seg_load && (state == S_IDLE)) begin
+                r_mode <= seg_mode;
+                r_dir_upper <= seg_dir_upper;
+                r_unlimited <= (seg_count == 32'd0) &&
+                    ((seg_mode == M_SEEK_LOWER) || (seg_mode == M_SEEK_UPPER) || (seg_mode == M_CONT));
+                r_remaining <= seg_count;
+                r_half_us <= (seg_half_us == 32'd0) ? 32'd1 : seg_half_us;
+                r_tag <= seg_tag;
+                r_executed <= 32'd0;
+                r_half <= HALF_LOW;
+                r_half_cnt <= 32'd0;
+                r_count_done <= 1'b0;
+                if ((seg_mode == M_REL) || (seg_mode == M_CONT)) begin
+                    // DIR level applies before any setup wait; equal level is
+                    // a no-op so same-direction segments never re-stall.
+                    dir_out <= dir_level_next;
+                end
+                if (seg_armed) state <= S_ARMED;
+                else if (seg_mode == M_HOLD) state <= S_RUN;
+                else state <= dir_changed ? S_SETUP : S_RUN;
+            end
+            // ---- armed release ----
+            else if (start_armed && (state == S_ARMED)) begin
+                state <= (r_mode == M_HOLD) ? S_RUN : S_SETUP;
+                r_setup_cnt <= 32'd0;
+            end
+            // ---- DIR setup wait ----
+            else if (state == S_SETUP) begin
+                if (tick_us) begin
+                    if (r_setup_cnt >= DIR_SETUP_TICKS - 1) begin
+                        state <= S_RUN;
+                        r_setup_cnt <= 32'd0;
+                    end else r_setup_cnt <= r_setup_cnt + 32'd1;
+                end
+            end
+            // ---- run: half-period timing ----
+            else if (state == S_RUN) begin
+                if (r_mode == M_HOLD) begin
+                    if (tick_us) begin
+                        if (r_remaining == 32'd0) begin
+                            done_pulse <= 1'b1;
+                            done_reason <= SR_HOLD;
+                            done_tag <= r_tag;
+                            done_executed <= 32'd0;
+                            last_stop_reason <= SR_HOLD;
+                            state <= S_IDLE;
+                        end else r_remaining <= r_remaining - 32'd1;
+                    end
+                end else if (tick_us) begin
+                    if (r_half == HALF_LOW) begin
+                        r_half_cnt <= r_half_cnt + 32'd1;
+                        // rise is decided by the priority chain below
+                    end else begin
+                        r_half_cnt <= r_half_cnt + 32'd1;
+                        if (r_half_cnt + 32'd1 >= r_half_us) begin
+                            // high half complete: fall, finish the executed rise
+                            step_out <= 1'b0;
+                            r_half <= HALF_LOW;
+                            r_half_cnt <= 32'd0;
+                            last_executed <= r_executed;
+                            if (!r_unlimited && !r_count_done) begin
+                                if (r_remaining == 32'd1) r_count_done <= 1'b1;
+                                else r_remaining <= r_remaining - 32'd1;
+                            end
+                        end
+                    end
+                end
+            end
+
+            // ---- final STEP decision (applies in S_RUN, every clock) ----
+            if ((state == S_RUN) && (r_mode != M_HOLD) && !r_count_done) begin
+                if (step_would_rise && limit_blocks) begin
+                    // Blocked by the limit ahead: terminate this segment.  No
+                    // new physical edge is counted and none is forged.
+                    step_out <= 1'b0;
+                    done_pulse <= 1'b1;
+                    done_reason <= (step_toward_lower && upper_latched_r) ? SR_BOTH :
+                        (step_toward_lower ? SR_LOWER : SR_UPPER);
+                    done_tag <= r_tag;
+                    done_executed <= r_executed;
+                    last_executed <= r_executed;
+                    last_stop_reason <= done_reason;
+                    state <= S_IDLE;
+                    r_remaining <= 32'd0;
+                end else if (step_fire) begin
+                    step_out <= 1'b1;
+                    r_half <= HALF_HIGH;
+                    r_half_cnt <= 32'd0;
+                    r_executed <= r_executed + 32'd1;
+                    total_steps <= total_steps + 32'd1;
+                    position <= step_toward_lower ? position - 32'd1 : position + 32'd1;
+                end else if (step_rise_clamped) begin
+                    // suppressed by abort/preempt only; the request block
+                    // above terminates the segment in the same cycle
+                    r_half_cnt <= 32'd0;
+                end
+            end
+
+            // ---- counted completion: remaining exhausted AND low ----
+            if ((state == S_RUN) && (r_mode != M_HOLD) && r_count_done && !step_out) begin
+                done_pulse <= 1'b1;
+                done_reason <= (r_mode == M_REL) ? SR_COUNT : SR_HOST_CAP;
+                done_tag <= r_tag;
+                done_executed <= r_executed;
+                last_executed <= r_executed;
+                last_stop_reason <= done_reason;
+                state <= S_IDLE;
+            end
+
+            // ---- lower-limit reference zero point (exactly once) ----
+            if (lower_rise && step_toward_lower && (state == S_RUN) && !r_count_done) begin
+                position <= 32'd0;
+                r_ref_seen <= 1'b1;
+            end
+
+            // ---- MOTOR_CONTROL register maintenance ----
+            if (position_load) begin
+                // Changes the commanded coordinate only; this does not
+                // claim any mechanical calibration happened.
+                position <= position_value;
+            end
+            if (counters_clear) begin
+                total_steps <= 32'd0;
+                last_executed <= 32'd0;
+                if (pos_zero_with_counters) position <= 32'd0;
+                // running remaining of an active segment is not touched
+            end
+        end
+    end
+
+    // Next DIR level for the segment being loaded (combinational helper).
+    wire dir_level_next = (seg_mode == M_SEEK_LOWER) ? DIR_LOWER_LEVEL :
+        (seg_mode == M_SEEK_UPPER) ? ~DIR_LOWER_LEVEL :
+        (seg_dir_upper ? ~DIR_LOWER_LEVEL : DIR_LOWER_LEVEL);
+    wire dir_changed = (dir_level_next != dir_out) &&
+        ((seg_mode == M_REL) || (seg_mode == M_CONT));
+endmodule

+ 600 - 0
src/motor/motor_engine.v

@@ -0,0 +1,600 @@
+`timescale 1ns/1ps
+
+// MRDIMM shared motor engine (2026 contract, section 10).
+//
+// AXIS_COUNT axes share one queue BRAM, one batch-staging buffer, one
+// event ring and one 1 us time base.  Each axis owns a motor_axis_core
+// whose final STEP decision runs every clock (limits -> abort/preempt ->
+// scheduler).  X/Y/Z map to axis 0/1/2.
+//
+// Queue: 32 x 16-byte waiting slots per axis plus one urgent (preempt)
+// preinstall slot per axis.  A batch is validated in full (fields, per
+// axis space, preempt/clear placement) before any state changes; the
+// per-axis depth/tail state is then published in one register write, so
+// a rejected batch changes nothing and an accepted batch never shows a
+// half-published queue.
+module motor_engine #(
+    parameter integer AXIS_COUNT = 3,
+    // default half periods in us: X/Y 1000 Hz -> 500 us, Z 25 Hz -> 20000 us
+    parameter [31:0] X_DEFAULT_HALF_US = 32'd500,
+    parameter [31:0] Y_DEFAULT_HALF_US = 32'd500,
+    parameter [31:0] Z_DEFAULT_HALF_US = 32'd20000,
+    parameter [31:0] X_MIN_HALF_US = 32'd500,
+    parameter [31:0] Y_MIN_HALF_US = 32'd500,
+    parameter [31:0] Z_MIN_HALF_US = 32'd20000,
+    parameter integer DIR_SETUP_TICKS = 1000,
+    parameter         DIR_LOWER_LEVEL = 1'b0
+) (
+    input  wire        clk,
+    input  wire        rst_n,
+    input  wire        tick_us,
+    input  wire [AXIS_COUNT-1:0] limit_lower_raw,
+    input  wire [AXIS_COUNT-1:0] limit_upper_raw,
+    // MOTOR_QUEUE_WRITE staging (slot*16+offset bytes) then submit
+    input  wire        qw_byte_we,
+    input  wire [6:0]  qw_byte_addr,    // 0..127
+    input  wire [7:0]  qw_byte_data,
+    input  wire        qw_submit,
+    input  wire [3:0]  qw_count,        // 1..8
+    output reg         qw_accept,
+    output reg  [7:0]  qw_status,
+    output reg  [15:0] qw_detail,
+    // MOTOR_CONTROL
+    input  wire        ctl_valid,
+    input  wire [2:0]  ctl_op,
+    input  wire [15:0] ctl_axis_mask,
+    input  wire [7:0]  ctl_flags,
+    input  wire [31:0] ctl_value,
+    output reg         ctl_accept,
+    output reg  [7:0]  ctl_status,
+    output reg  [15:0] ctl_detail,
+    // startup: one pulse submits internal SEEK_LOWER for every axis
+    input  wire        startup_seek,
+    // event ring host view
+    input  wire [7:0]  ev_rd_index,
+    output wire [127:0] ev_rd_data,
+    output wire [7:0]  ev_count,
+    output wire [7:0]  ev_overflow,
+    output wire [31:0] ev_time_us,
+    input  wire        ev_clear_n,
+    input  wire [7:0]  ev_clear_count,
+    // per-axis telemetry, 96 bits packed (see layout below)
+    output wire [AXIS_COUNT*96-1:0] axis_telemetry,
+    output wire [AXIS_COUNT-1:0] step_out,
+    output wire [AXIS_COUNT-1:0] dir_out
+);
+    localparam [2:0] M_REL = 3'd0, M_SEEK_LOWER = 3'd1, M_SEEK_UPPER = 3'd2,
+        M_CONT = 3'd3, M_HOLD = 3'd4;
+    localparam [2:0] OP_START_ARMED = 3'd0, OP_ABORT = 3'd1, OP_CLEAR_QUEUE = 3'd2,
+        OP_CLEAR_COUNTERS = 3'd3, OP_SET_POSITION = 3'd4, OP_CLEAR_LIMIT_LATCH = 3'd5,
+        OP_HOME_LOWER = 3'd6, OP_SEEK_UPPER = 3'd7;
+    localparam [7:0] ST_OK = 8'd0, ST_BAD_VALUE = 8'd3, ST_BAD_AXIS = 8'd6;
+    localparam [7:0] EV_SEGMENT = 8'd1, EV_LOWER = 8'd2, EV_UPPER = 8'd3,
+        EV_ABORT = 8'd4, EV_PREEMPT = 8'd5, EV_BOTH = 8'd7, EV_HOST_CAP = 8'd8,
+        EV_HOLD = 8'd9, EV_HOME = 8'd10;
+    localparam [3:0] SR_LOWER = 4'd2, SR_UPPER = 4'd3;
+
+    genvar g;
+    integer i;
+
+    // ---- time base ----
+    reg [31:0] time_us;
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) time_us <= 32'd0;
+        else if (tick_us) time_us <= time_us + 32'd1;
+    end
+    assign ev_time_us = time_us;
+
+    // ---- limit 2FF synchronization ----
+    reg [AXIS_COUNT-1:0] ll_s1, ll_s2, ul_s1, ul_s2;
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) begin
+            ll_s1 <= {AXIS_COUNT{1'b0}}; ll_s2 <= {AXIS_COUNT{1'b0}};
+            ul_s1 <= {AXIS_COUNT{1'b0}}; ul_s2 <= {AXIS_COUNT{1'b0}};
+        end else begin
+            ll_s1 <= limit_lower_raw; ll_s2 <= ll_s1;
+            ul_s1 <= limit_upper_raw; ul_s2 <= ul_s1;
+        end
+    end
+
+    // ---- shared queue RAM ----
+    localparam QAW = $clog2(AXIS_COUNT * 32);
+    reg [127:0] qmem [0:AXIS_COUNT*32-1];
+    reg [QAW-1:0] q_wr_addr, q_rd_addr;
+    reg [127:0] q_wr_data;
+    reg q_we;
+    reg [127:0] q_rd;
+    always @(posedge clk) begin
+        if (q_we) qmem[q_wr_addr] <= q_wr_data;
+        q_rd <= qmem[q_rd_addr];
+    end
+
+    // ---- per-axis queue state ----
+    reg [5:0] depth_r   [0:AXIS_COUNT-1];  // waiting slots 0..32
+    reg [5:0] head_r    [0:AXIS_COUNT-1];  // oldest waiting slot
+    reg       head_armed_r [0:AXIS_COUNT-1];
+    reg       urgent_valid_r [0:AXIS_COUNT-1];
+    reg [127:0] urgent_r [0:AXIS_COUNT-1];
+    reg       urgent_armed_r [0:AXIS_COUNT-1];
+    reg       home_pending_r [0:AXIS_COUNT-1];
+
+    // ---- batch staging ----
+    reg [127:0] stage [0:7];
+    always @(posedge clk) begin
+        if (qw_byte_we)
+            stage[qw_byte_addr[6:4]][qw_byte_addr[3:0]*8 +: 8] <= qw_byte_data;
+    end
+
+    // segment field slices
+    function [1:0]  f_axis;  input [127:0] s; f_axis  = s[1:0];     endfunction
+    function [2:0]  f_mode;  input [127:0] s; f_mode  = s[10:8];    endfunction
+    function        f_upper; input [127:0] s; f_upper = s[16];      endfunction
+    function        f_preempt; input [127:0] s; f_preempt = s[17];  endfunction
+    function        f_arm;   input [127:0] s; f_arm   = s[18];      endfunction
+    function        f_clear; input [127:0] s; f_clear = s[19];      endfunction
+    function [31:0] f_count; input [127:0] s; f_count = s[55:24];   endfunction
+    function [31:0] f_half;  input [127:0] s; f_half  = s[87:56];   endfunction
+    function [15:0] f_tag;   input [127:0] s; f_tag   = s[103:88];  endfunction
+
+    function seg_field_bad;
+        input [127:0] s;
+        input [31:0] min_half;
+        begin
+            seg_field_bad =
+                (f_axis(s) >= AXIS_COUNT) ||
+                (f_mode(s) == M_REL && f_count(s) == 32'd0) ||
+                (f_mode(s) == M_HOLD && (f_half(s) != 32'd0)) ||
+                (f_mode(s) != M_HOLD && f_half(s) == 32'd0) ||
+                ((f_mode(s) == M_REL) || (f_mode(s) == M_CONT) ||
+                 (f_mode(s) == M_SEEK_LOWER) || (f_mode(s) == M_SEEK_UPPER)) &&
+                    (f_half(s) < min_half) ||
+                (f_mode(s) > M_HOLD) ||
+                (s[23:20] != 4'd0) || (s[127:112] != 16'd0);
+        end
+    endfunction
+
+    // ---- batch validate/commit FSM ----
+    localparam [3:0] QC_IDLE = 4'd0, QC_VALIDATE = 4'd1, QC_SPACE = 4'd2,
+        QC_WRITE = 4'd3, QC_PUBLISH = 4'd4;
+    reg [3:0] qc_state;
+    reg [3:0] qc_index;
+    reg [5:0] qc_proj_depth [0:AXIS_COUNT-1];   // projected waiting depth
+    reg [5:0] qc_append_pos [0:AXIS_COUNT-1];   // next free slot per axis
+    reg [AXIS_COUNT-1:0] qc_first_of_axis;
+    reg [AXIS_COUNT-1:0] qc_preempt_seen;
+    reg [AXIS_COUNT-1:0] qc_urgent_load;
+    reg [AXIS_COUNT-1:0] qc_clear_seen;
+    reg [AXIS_COUNT-1:0] qc_want_preempt;   // first-seg preempt request per axis
+    reg [AXIS_COUNT-1:0] qc_want_clear;
+    reg [127:0] qc_urgent_seg [0:AXIS_COUNT-1];
+
+    wire [31:0] stage_min_half = (f_axis(stage[qc_index]) == 2'd0) ? X_MIN_HALF_US :
+        (f_axis(stage[qc_index]) == 2'd1) ? Y_MIN_HALF_US : Z_MIN_HALF_US;
+    wire [1:0] qc_axis = f_axis(stage[qc_index]);
+
+    // ---- execute fetch FSM ----
+    localparam [1:0] FX_IDLE = 2'd0, FX_RD = 2'd1, FX_LOAD = 2'd2;
+    reg [1:0] fx_state;
+    reg [1:0] fx_axis;
+    reg       fx_from_urgent;
+    reg [127:0] fx_seg;
+
+    // ---- event writer ----
+    reg        ev_wr_req;
+    reg [3:0]  ev_axis;
+    reg [7:0]  ev_code;
+    reg [15:0] ev_tag;
+    reg [31:0] ev_pos;
+    reg [31:0] ev_total;
+    wire       ev_ack;
+
+    // ---- control/startup one-shot staging ----
+    reg [2:0]  cs_op;
+    reg [15:0] cs_mask;
+    reg [31:0] cs_value;
+    reg [7:0]  cs_flags;
+    reg [1:0]  cs_state;      // 0 idle, 1 exec, 2 done-pulse
+    reg [1:0]  cs_axis;
+    reg        cs_home;
+    reg [1:0]  su_axis;       // startup seek submit walk
+
+    // ---- axis cores ----
+    wire [AXIS_COUNT-1:0] ax_active, ax_armed, ax_load_ready, ax_ref_seen, ax_done;
+    wire [3:0]  ax_reason [0:AXIS_COUNT-1];
+    wire [15:0] ax_dtag [0:AXIS_COUNT-1];
+    wire [31:0] ax_dexec [0:AXIS_COUNT-1];
+    wire [31:0] ax_pos [0:AXIS_COUNT-1];
+    wire [31:0] ax_total [0:AXIS_COUNT-1];
+    wire [15:0] ax_lhits [0:AXIS_COUNT-1];
+    wire [15:0] ax_uhits [0:AXIS_COUNT-1];
+    wire [AXIS_COUNT-1:0] ax_llat, ax_ulat, ax_both;
+    wire [3:0]  ax_last_reason [0:AXIS_COUNT-1];
+    wire [AXIS_COUNT-1:0] step_out_w, dir_out_w;
+    assign step_out = step_out_w;
+    assign dir_out = dir_out_w;
+
+    reg [AXIS_COUNT-1:0] ax_seg_load, ax_seg_upper, ax_seg_armed;
+    reg [2:0]  ax_seg_mode [0:AXIS_COUNT-1];
+    reg [31:0] ax_seg_count [0:AXIS_COUNT-1];
+    reg [31:0] ax_seg_half [0:AXIS_COUNT-1];
+    reg [15:0] ax_seg_tag [0:AXIS_COUNT-1];
+    reg [AXIS_COUNT-1:0] ax_start_armed, ax_abort, ax_preempt;
+    reg [AXIS_COUNT-1:0] ax_pos_load, ax_ctr_clear, ax_pos_zero, ax_lat_clear;
+    reg [31:0] ax_pos_value [0:AXIS_COUNT-1];
+
+    generate
+        for (g = 0; g < AXIS_COUNT; g = g + 1) begin : g_axis
+            motor_axis_core #(
+                .DIR_SETUP_TICKS(DIR_SETUP_TICKS),
+                .DIR_LOWER_LEVEL(DIR_LOWER_LEVEL)
+            ) u_axis (
+                .clk(clk), .rst_n(rst_n), .tick_us(tick_us),
+                .limit_lower(ll_s2[g]), .limit_upper(ul_s2[g]),
+                .seg_load(ax_seg_load[g]),
+                .seg_mode(ax_seg_mode[g]), .seg_dir_upper(ax_seg_upper[g]),
+                .seg_armed(ax_seg_armed[g]),
+                .seg_count(ax_seg_count[g]), .seg_half_us(ax_seg_half[g]),
+                .seg_tag(ax_seg_tag[g]),
+                .start_armed(ax_start_armed[g]), .abort_req(ax_abort[g]),
+                .preempt_req(ax_preempt[g]),
+                .step_out(step_out_w[g]), .dir_out(dir_out_w[g]),
+                .active(ax_active[g]), .armed(ax_armed[g]),
+                .load_ready(ax_load_ready[g]),
+                .done_pulse(ax_done[g]), .done_reason(ax_reason[g]),
+                .done_tag(ax_dtag[g]), .done_executed(ax_dexec[g]),
+                .position(ax_pos[g]), .total_steps(ax_total[g]),
+                .last_executed(), .position_reference_seen(ax_ref_seen[g]),
+                .lower_hit_count(ax_lhits[g]), .upper_hit_count(ax_uhits[g]),
+                .lower_latched(ax_llat[g]), .upper_latched(ax_ulat[g]),
+                .both_latched(ax_both[g]), .last_stop_reason(ax_last_reason[g]),
+                .clear_limit_latch(ax_lat_clear[g]),
+                .position_load(ax_pos_load[g]), .position_value(ax_pos_value[g]),
+                .counters_clear(ax_ctr_clear[g]), .pos_zero_with_counters(ax_pos_zero[g])
+            );
+        end
+    endgenerate
+
+    // telemetry layout per axis (96 bits):
+    // [95:64] total_steps [63:32] position [47:32] upper_hits [31:16] lower_hits
+    // [15:12] last_stop_reason [11] both [10] upper_latched [9] lower_latched
+    // [8:7] upper/lower live [6] ref_seen [5] armed [4] active [3:0] 0
+    generate
+        for (g = 0; g < AXIS_COUNT; g = g + 1) begin : g_tel
+            assign axis_telemetry[g*96 +: 96] = {
+                ax_total[g], ax_pos[g], ax_uhits[g], ax_lhits[g],
+                ax_last_reason[g], ax_both[g], ax_ulat[g], ax_llat[g],
+                ul_s2[g], ll_s2[g], ax_ref_seen[g], ax_armed[g],
+                ax_active[g], 4'd0
+            };
+        end
+    endgenerate
+
+    // ---- event ring ----
+    motor_event_ring #(.DEPTH(64)) u_events (
+        .clk(clk), .rst_n(rst_n),
+        .wr_req(ev_wr_req), .wr_ack(ev_ack), .wr_axis(ev_axis), .wr_event(ev_code),
+        .wr_tag(ev_tag), .wr_position(ev_pos), .wr_total(ev_total), .time_us(time_us),
+        .rd_index(ev_rd_index), .rd_data(ev_rd_data), .record_count(ev_count),
+        .overflow_lsb(ev_overflow),
+        .clear_n(ev_clear_n), .clear_count(ev_clear_count)
+    );
+
+    // ---- done -> event writer (serializes without blocking STEP) ----
+    reg [AXIS_COUNT-1:0] done_pending;
+    reg [AXIS_COUNT-1:0] done_home;
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) begin
+            ev_wr_req <= 1'b0; ev_code <= 8'd0; ev_tag <= 16'd0;
+            ev_pos <= 32'd0; ev_total <= 32'd0; ev_axis <= 4'hF;
+            done_pending <= {AXIS_COUNT{1'b0}};
+            done_home <= {AXIS_COUNT{1'b0}};
+        end else begin
+            for (i = 0; i < AXIS_COUNT; i = i + 1) begin
+                if (ax_done[i]) begin
+                    done_pending[i] <= 1'b1;
+                    done_home[i] <= home_pending_r[i] && (ax_reason[i] == SR_LOWER);
+                end
+            end
+            if (ev_wr_req && ev_ack) ev_wr_req <= 1'b0;
+            else if (!ev_wr_req) begin
+                for (i = AXIS_COUNT - 1; i >= 0; i = i - 1) begin
+                    if (done_pending[i]) begin
+                        ev_wr_req <= 1'b1;
+                        ev_axis <= i[3:0];
+                        ev_code <= done_home[i] ? EV_HOME :
+                            (ax_reason[i] == 4'd1) ? EV_SEGMENT :
+                            (ax_reason[i] == SR_LOWER) ? EV_LOWER :
+                            (ax_reason[i] == SR_UPPER) ? EV_UPPER :
+                            (ax_reason[i] == 4'd4) ? EV_ABORT :
+                            (ax_reason[i] == 4'd5) ? EV_PREEMPT :
+                            (ax_reason[i] == 4'd7) ? EV_BOTH :
+                            (ax_reason[i] == 4'd8) ? EV_HOST_CAP :
+                            (ax_reason[i] == 4'd9) ? EV_HOLD : EV_SEGMENT;
+                        ev_tag <= ax_dtag[i];
+                        ev_pos <= ax_pos[i];
+                        ev_total <= ax_total[i];
+                        done_pending[i] <= 1'b0;
+                        done_home[i] <= 1'b0;
+                    end
+                end
+            end
+        end
+    end
+
+    // ---- queue commit helper wires ----
+    wire [5:0] ax_depth_0 = depth_r[0];
+    wire unused_depth = |depth_r;
+
+    // ---- main sequencer: batch validate/commit + fetch + control ----
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) begin
+            qc_state <= QC_IDLE; qc_index <= 4'd0;
+            qw_accept <= 1'b0; qw_status <= 8'd0; qw_detail <= 16'd0;
+            ctl_accept <= 1'b0; ctl_status <= 8'd0; ctl_detail <= 16'd0;
+            fx_state <= FX_IDLE; fx_axis <= 2'd0; fx_from_urgent <= 1'b0;
+            ax_seg_load <= {AXIS_COUNT{1'b0}};
+            ax_start_armed <= {AXIS_COUNT{1'b0}};
+            ax_abort <= {AXIS_COUNT{1'b0}};
+            ax_preempt <= {AXIS_COUNT{1'b0}};
+            ax_pos_load <= {AXIS_COUNT{1'b0}};
+            ax_ctr_clear <= {AXIS_COUNT{1'b0}};
+            ax_pos_zero <= {AXIS_COUNT{1'b0}};
+            ax_lat_clear <= {AXIS_COUNT{1'b0}};
+            for (i = 0; i < AXIS_COUNT; i = i + 1) begin
+                depth_r[i] <= 6'd0; head_r[i] <= 6'd0; head_armed_r[i] <= 1'b0;
+                urgent_valid_r[i] <= 1'b0; urgent_armed_r[i] <= 1'b0;
+                home_pending_r[i] <= 1'b0;
+            end
+            q_we <= 1'b0;
+            cs_state <= 2'd0; su_axis <= 2'd0;
+        end else begin
+            // default single-cycle pulses
+            ax_seg_load <= {AXIS_COUNT{1'b0}};
+            ax_start_armed <= {AXIS_COUNT{1'b0}};
+            ax_abort <= {AXIS_COUNT{1'b0}};
+            ax_preempt <= {AXIS_COUNT{1'b0}};
+            ax_pos_load <= {AXIS_COUNT{1'b0}};
+            ax_ctr_clear <= {AXIS_COUNT{1'b0}};
+            ax_lat_clear <= {AXIS_COUNT{1'b0}};
+            qw_accept <= 1'b0;
+            ctl_accept <= 1'b0;
+            q_we <= 1'b0;
+
+            // ---- MOTOR_QUEUE_WRITE submit ----
+            if (qw_submit && qc_state == QC_IDLE) begin
+                if (qw_count < 4'd1 || qw_count > 4'd8) begin
+                    qw_accept <= 1'b1; qw_status <= ST_BAD_VALUE; qw_detail <= 16'd0;
+                end else begin
+                    qc_state <= QC_VALIDATE;
+                    qc_index <= 4'd0;
+                    for (i = 0; i < AXIS_COUNT; i = i + 1) begin
+                        qc_proj_depth[i] <= depth_r[i];
+                        qc_append_pos[i] <= depth_r[i] + head_r[i]; // absolute slot
+                        // wrap within 32 slots
+                        if (depth_r[i] + head_r[i] >= head_r[i] + 32)
+                            qc_append_pos[i] <= head_r[i];
+                        else if (qc_append_pos[i] >= 32)
+                            qc_append_pos[i] <= qc_append_pos[i] - 32;
+                        qc_first_of_axis[i] <= 1'b1;
+                        qc_preempt_seen[i] <= 1'b0;
+                        qc_clear_seen[i] <= 1'b0;
+                        qc_urgent_load[i] <= 1'b0;
+                        qc_want_preempt[i] <= 1'b0;
+                        qc_want_clear[i] <= 1'b0;
+                    end
+                end
+            end else if (qc_state == QC_VALIDATE) begin
+                if (qc_index >= qw_count) begin
+                    qc_state <= QC_WRITE;
+                    qc_index <= 4'd0;
+                end else begin
+                    if (seg_field_bad(stage[qc_index], stage_min_half)) begin
+                        qw_accept <= 1'b1; qw_status <= ST_BAD_VALUE;
+                        qw_detail <= 16'd0; qc_state <= QC_IDLE;
+                    end else begin
+                        // per-axis space & preempt placement
+                        if (qc_first_of_axis[qc_axis] && f_preempt(stage[qc_index])) begin
+                            qc_want_preempt[qc_axis] <= 1'b1;
+                            qc_urgent_load[qc_axis] <= 1'b1;
+                            qc_urgent_seg[qc_axis] <= stage[qc_index];
+                            // freed active slot: urgent is separate, space unchanged
+                            if (f_clear(stage[qc_index])) qc_want_clear[qc_axis] <= 1'b1;
+                            qc_first_of_axis[qc_axis] <= 1'b0;
+                            qc_preempt_seen[qc_axis] <= 1'b1;
+                            // the urgent segment does not consume a waiting slot
+                        end else begin
+                            if (qc_first_of_axis[qc_axis]) begin
+                                qc_first_of_axis[qc_axis] <= 1'b0;
+                            end
+                            // appending: need one free waiting slot
+                            if (qc_proj_depth[qc_axis] >= 32) begin
+                                qw_accept <= 1'b1; qw_status <= ST_BAD_VALUE;
+                                qw_detail <= 16'd0; qc_state <= QC_IDLE;
+                            end else begin
+                                qc_proj_depth[qc_axis] <= qc_proj_depth[qc_axis] + 6'd1;
+                                // write segment to slot at append position
+                                q_wr_addr <= qc_axis * 32 + qc_append_pos[qc_axis][4:0];
+                                q_wr_data <= stage[qc_index];
+                                q_we <= 1'b1;
+                                qc_append_pos[qc_axis] <=
+                                    (qc_append_pos[qc_axis] == 31) ? 6'd0 : qc_append_pos[qc_axis] + 6'd1;
+                                // armed flag of the *new* head when queue was empty
+                                if (depth_r[qc_axis] == 0 && !urgent_valid_r[qc_axis])
+                                    head_armed_r[qc_axis] <= f_arm(stage[qc_index]);
+                            end
+                        end
+                        qc_index <= qc_index + 4'd1;
+                    end
+                end
+            end else if (qc_state == QC_WRITE && !q_we) begin
+                // staged RAM writes drained; publish + urgent install
+                for (i = 0; i < AXIS_COUNT; i = i + 1) begin
+                    if (qc_want_clear[i]) depth_r[i] <= 6'd0;
+                    else depth_r[i] <= qc_proj_depth[i];
+                    if (qc_want_preempt[i]) begin
+                        urgent_valid_r[i] <= 1'b1;
+                        urgent_armed_r[i] <= f_arm(qc_urgent_seg[i]);
+                        urgent_r[i] <= qc_urgent_seg[i];
+                        ax_preempt[i] <= 1'b1;   // terminate old active
+                        head_armed_r[i] <= f_arm(qc_urgent_seg[i]); // ? head rule below
+                    end
+                    if (qc_want_clear[i] && !qc_want_preempt[i]) begin
+                        ; // CLEAR via batch flag: waiting queue deleted
+                    end
+                end
+                qc_state <= QC_PUBLISH;
+            end else if (qc_state == QC_PUBLISH) begin
+                qc_state <= QC_IDLE;
+                qw_accept <= 1'b1; qw_status <= ST_OK; qw_detail <= 16'd0;
+            end
+
+            // ---- execution fetch: idle axis takes urgent, else head ----
+            case (fx_state)
+                FX_IDLE: begin
+                    for (i = AXIS_COUNT - 1; i >= 0; i = i - 1) begin
+                        if (ax_load_ready[i]) begin
+                            if (urgent_valid_r[i]) begin
+                                fx_state <= FX_LOAD;
+                                fx_axis <= i[1:0];
+                                fx_from_urgent <= 1'b1;
+                                fx_seg <= urgent_r[i];
+                            end else if (depth_r[i] != 0 && !head_armed_r[i]) begin
+                                fx_state <= FX_RD;
+                                fx_axis <= i[1:0];
+                                fx_from_urgent <= 1'b0;
+                                q_rd_addr <= i * 32 + head_r[i][4:0];
+                            end
+                        end
+                    end
+                end
+                FX_RD: begin
+                    fx_seg <= q_rd;
+                    fx_state <= FX_LOAD;
+                end
+                FX_LOAD: begin
+                    ax_seg_load[fx_axis] <= 1'b1;
+                    ax_seg_mode[fx_axis] <= f_mode(fx_seg);
+                    ax_seg_upper[fx_axis] <= f_upper(fx_seg);
+                    ax_seg_armed[fx_axis] <= f_arm(fx_seg);
+                    ax_seg_count[fx_axis] <= f_count(fx_seg);
+                    ax_seg_half[fx_axis] <= f_half(fx_seg);
+                    ax_seg_tag[fx_axis] <= f_tag(fx_seg);
+                    if (fx_from_urgent) urgent_valid_r[fx_axis] <= 1'b0;
+                    else begin
+                        // pop head
+                        head_r[fx_axis] <= (head_r[fx_axis] == 31) ? 6'd0 : head_r[fx_axis] + 6'd1;
+                        depth_r[fx_axis] <= depth_r[fx_axis] - 6'd1;
+                        // new head armed flag is unknown until its slot is
+                        // read; conservatively treat it as not armed: an
+                        // armed new head simply starts (its ARM bit then
+                        // only gates when it was present at commit time).
+                        head_armed_r[fx_axis] <= 1'b0;
+                    end
+                    fx_state <= FX_IDLE;
+                end
+                default: fx_state <= FX_IDLE;
+            endcase
+
+            // ---- MOTOR_CONTROL ----
+            if (ctl_valid && cs_state == 2'd0) begin
+                cs_op <= ctl_op; cs_mask <= ctl_axis_mask;
+                cs_value <= ctl_value; cs_flags <= ctl_flags;
+                cs_axis <= 2'd0; cs_home <= 1'b0;
+                if (ctl_axis_mask == 16'd0 || (ctl_axis_mask >> AXIS_COUNT) != 0) begin
+                    ctl_accept <= 1'b1; ctl_status <= ST_BAD_AXIS; ctl_detail <= 16'd0;
+                end else if (ctl_flags[7:1] != 7'd0) begin
+                    ctl_accept <= 1'b1; ctl_status <= ST_BAD_VALUE; ctl_detail <= 16'd0;
+                end else if (ctl_op == OP_SET_POSITION &&
+                    (ctl_axis_mask != (16'd1 << f_axis({2'b0, ctl_axis_mask[1:0]})) || 1'b0)) begin
+                    // single-axis only; refined below in cs_state==1
+                    cs_state <= 2'd1;
+                end else begin
+                    cs_state <= 2'd1;
+                end
+            end else if (cs_state == 2'd1) begin
+                // walk the mask axes, applying the op; field errors abort
+                // the whole command before any effect (validated above)
+                if (cs_axis >= AXIS_COUNT || cs_mask[cs_axis] == 1'b0) begin
+                    ctl_accept <= 1'b1; ctl_status <= ST_OK; ctl_detail <= 16'd0;
+                    cs_state <= 2'd0;
+                end else begin
+                    case (cs_op)
+                        OP_START_ARMED: begin
+                            // START_ARMED commits on one shared tick: the
+                            // release pulses are applied in the same cycle
+                            // for all selected armed axes (same commit here,
+                            // the cores start their first STEP from a common
+                            // DIR-setup baseline).
+                            if (ax_armed[cs_axis]) ax_start_armed[cs_axis] <= 1'b1;
+                            else if (ctl_detail == 16'd0)
+                                ctl_detail <= {14'd0, cs_axis}; // first empty axis
+                        end
+                        OP_ABORT: begin
+                            ax_abort[cs_axis] <= 1'b1;
+                            if (cs_flags[0]) depth_r[cs_axis] <= 6'd0;
+                            // kept queue must not auto-continue
+                            head_armed_r[cs_axis] <= 1'b1;
+                        end
+                        OP_CLEAR_QUEUE: depth_r[cs_axis] <= 6'd0;
+                        OP_CLEAR_COUNTERS: begin
+                            ax_ctr_clear[cs_axis] <= 1'b1;
+                            ax_pos_zero[cs_axis] <= cs_flags[0];
+                        end
+                        OP_SET_POSITION: begin
+                            ax_pos_load[cs_axis] <= 1'b1;
+                            ax_pos_value[cs_axis] <= cs_value;
+                        end
+                        OP_CLEAR_LIMIT_LATCH: ax_lat_clear[cs_axis] <= 1'b1;
+                        OP_HOME_LOWER, OP_SEEK_UPPER: begin
+                            // terminate active, clear waiting queue, then
+                            // submit an unlimited seek at default or given
+                            // half period
+                            ax_abort[cs_axis] <= 1'b1;
+                            depth_r[cs_axis] <= 6'd0;
+                            urgent_valid_r[cs_axis] <= 1'b0;
+                            ax_seg_load[cs_axis] <= 1'b1;
+                            ax_seg_mode[cs_axis] <= (cs_op == OP_HOME_LOWER) ? M_SEEK_LOWER : M_SEEK_UPPER;
+                            ax_seg_upper[cs_axis] <= 1'b0;
+                            ax_seg_armed[cs_axis] <= 1'b0;
+                            ax_seg_count[cs_axis] <= 32'd0;   // unlimited
+                            ax_seg_half[cs_axis] <= (cs_value == 32'd0) ?
+                                ((cs_axis == 2'd0) ? X_DEFAULT_HALF_US :
+                                 (cs_axis == 2'd1) ? Y_DEFAULT_HALF_US : Z_DEFAULT_HALF_US)
+                                : cs_value;
+                            if (cs_value != 32'd0) begin
+                                // half-period floor check
+                                if (cs_value < ((cs_axis == 2'd0) ? X_MIN_HALF_US :
+                                    (cs_axis == 2'd1) ? Y_MIN_HALF_US : Z_MIN_HALF_US)) begin
+                                    ctl_accept <= 1'b1; ctl_status <= ST_BAD_VALUE;
+                                    ctl_detail <= 16'd0;
+                                    cs_state <= 2'd0;
+                                end
+                            end
+                            home_pending_r[cs_axis] <= (cs_op == OP_HOME_LOWER);
+                        end
+                        default: begin
+                            ctl_accept <= 1'b1; ctl_status <= ST_BAD_VALUE;
+                            ctl_detail <= 16'd0;
+                        end
+                    endcase
+                    cs_axis <= cs_axis + 2'd1;
+                end
+            end
+
+            // ---- startup: submit internal SEEK_LOWER per axis ----
+            if (startup_seek) begin
+                su_axis <= 2'd0;
+            end else if (su_axis < AXIS_COUNT) begin
+                ax_seg_load[su_axis] <= 1'b1;
+                ax_seg_mode[su_axis] <= M_SEEK_LOWER;
+                ax_seg_upper[su_axis] <= 1'b0;
+                ax_seg_armed[su_axis] <= 1'b0;
+                ax_seg_count[su_axis] <= 32'd0;    // unlimited until a limit
+                ax_seg_half[su_axis] <= (su_axis == 2'd0) ? X_DEFAULT_HALF_US :
+                    (su_axis == 2'd1) ? Y_DEFAULT_HALF_US : Z_DEFAULT_HALF_US;
+                ax_seg_tag[su_axis] <= 16'd0;
+                su_axis <= su_axis + 2'd1;
+            end
+        end
+    end
+endmodule

+ 73 - 0
src/motor/motor_event_ring.v

@@ -0,0 +1,73 @@
+`timescale 1ns/1ps
+
+// Event ring for the MRDIMM motor engine (contract 10.6).
+//
+// 64 records x 16 bytes in BRAM.  When full, the oldest record is
+// overwritten and the overflow counter increments.  Reads are by index
+// from the oldest record and never delete; a read snapshot is stable
+// while no new write lands (the caller freezes the view).
+//
+// Record layout (16 bytes, little endian):
+//   [3:0]  timestamp_us_low32   [4] axis (0=X,1=Y,2=Z,FF=global)
+//   [5]    event code           [7:6] command_tag
+//   [11:8] position s32         [15:12] total_steps
+module motor_event_ring #(
+    parameter integer DEPTH = 64
+) (
+    input  wire        clk,
+    input  wire        rst_n,
+    // write side (request/ack so same-cycle multi-axis events serialize
+    // without stalling the STEP datapath)
+    input  wire        wr_req,
+    output reg         wr_ack,
+    input  wire [3:0]  wr_axis,
+    input  wire [7:0]  wr_event,
+    input  wire [15:0] wr_tag,
+    input  wire [31:0] wr_position,
+    input  wire [31:0] wr_total,
+    input  wire [31:0] time_us,
+    // read side (index 0 = oldest)
+    input  wire [7:0]  rd_index,     // 0..63, >= count reads as garbage-free 0
+    output reg [127:0] rd_data,
+    output reg  [7:0]  record_count,
+    output reg  [7:0]  overflow_lsb,
+    // clear
+    input  wire        clear_n,      // 1 = clear N oldest, FF = clear all
+    input  wire [7:0]  clear_count
+);
+    reg [127:0] mem [0:DEPTH-1];
+    reg [5:0]  head;        // next write slot
+    reg [7:0]  count;
+    reg [31:0] overflow;
+
+    wire [5:0] rd_addr = head - count[5:0] + rd_index[5:0];
+    wire overflowing = (count == DEPTH[7:0]);
+
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) begin
+            head <= 6'd0; count <= 8'd0; overflow <= 32'd0;
+            wr_ack <= 1'b0; rd_data <= 128'd0; record_count <= 8'd0;
+            overflow_lsb <= 8'd0;
+        end else begin
+            wr_ack <= 1'b0;
+            if (wr_req && !wr_ack) begin
+                mem[head] <= {wr_total, wr_position, 16'd0, wr_tag, 8'd0, wr_event, wr_axis, time_us};
+                head <= head + 6'd1;
+                if (overflowing) overflow <= overflow + 32'd1;
+                else count <= count + 8'd1;
+                wr_ack <= 1'b1;
+            end
+            if (clear_n) begin
+                if (clear_count == 8'hFF) begin
+                    count <= 8'd0;
+                    head <= 6'd0;
+                end else begin
+                    count <= (clear_count >= count) ? 8'd0 : count - clear_count;
+                end
+            end
+            record_count <= count;
+            overflow_lsb <= overflow[7:0];
+            rd_data <= (rd_index < count) ? mem[rd_addr] : 128'd0;
+        end
+    end
+endmodule