فهرست منبع

fix(motor): publish validated batches atomically and preserve armed queues

codex 3 ماه پیش
والد
کامیت
76cd6b9745
1فایلهای تغییر یافته به همراه111 افزوده شده و 102 حذف شده
  1. 111 102
      src/motor/motor_engine.v

+ 111 - 102
src/motor/motor_engine.v

@@ -77,6 +77,8 @@ module motor_engine #(
 
     genvar g;
     integer i;
+    localparam integer AXIS_INDEX_BITS=(AXIS_COUNT<=1) ? 1 : $clog2(AXIS_COUNT);
+    localparam integer AXIS_COUNTER_BITS=$clog2(AXIS_COUNT+1);
 
     // ---- time base ----
     reg [31:0] time_us;
@@ -113,7 +115,7 @@ module motor_engine #(
     // ---- 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       queue_paused_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];
@@ -157,28 +159,38 @@ module motor_engine #(
     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;
+    localparam [3:0] QC_IDLE=0, QC_QUIESCE=1, QC_VALIDATE=2, QC_SPACE=3,
+        QC_WRITE=4, QC_DRAIN=5, QC_PUBLISH=6;
     reg [3:0] qc_state;
     reg [3:0] qc_index;
+    reg [3:0] qc_count;
+    reg [7:0] qc_is_urgent;
+    reg [5:0] qc_old_depth [0:AXIS_COUNT-1];
+    reg [5:0] qc_new_count [0:AXIS_COUNT-1];
+    reg [4:0] qc_new_head [0:AXIS_COUNT-1];
+    reg [AXIS_COUNT-1:0] qc_resume, start_credit;
+    reg qc_space_bad;
+    integer space_axis;
+    always @* begin
+        qc_space_bad=0;
+        for(space_axis=0;space_axis<AXIS_COUNT;space_axis=space_axis+1)
+            if(qc_old_depth[space_axis]+qc_new_count[space_axis]>32) qc_space_bad=1;
+    end
     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]);
+    wire [AXIS_INDEX_BITS-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, FX_WAIT = 2'd3;
     reg [1:0] fx_state;
-    reg [1:0] fx_axis;
+    reg [AXIS_INDEX_BITS-1:0] fx_axis;
     reg       fx_from_urgent;
     reg [127:0] fx_seg;
 
@@ -199,7 +211,7 @@ module motor_engine #(
     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
+    reg [AXIS_COUNTER_BITS-1:0] su_axis; // includes terminal AXIS_COUNT
 
     // ---- axis cores ----
     wire [AXIS_COUNT-1:0] ax_active, ax_armed, ax_load_ready, ax_ref_seen, ax_done;
@@ -338,7 +350,7 @@ module motor_engine #(
     // ---- 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;
+            qc_state <= QC_IDLE; qc_index <= 4'd0;start_credit<=0;
             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;
@@ -351,11 +363,15 @@ module motor_engine #(
             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;
+                depth_r[i] <= 6'd0; head_r[i] <= 6'd0; queue_paused_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;
+
+            for(i=0;i<AXIS_COUNT;i=i+1)
+                if(ax_done[i] && (ax_reason[i]==2 || ax_reason[i]==3 || ax_reason[i]==7))
+                    queue_paused_r[i]<=1;
             cs_state <= 2'd0; su_axis <= AXIS_COUNT;
         end else begin
             // default single-cycle pulses
@@ -370,107 +386,101 @@ module motor_engine #(
             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;
+            // ---- MOTOR_QUEUE_WRITE: quiesce loader, validate, write, publish ----
+            // Active STEP execution never waits for these RAM transactions.
+            if(qw_submit && qc_state==QC_IDLE) begin
+                if(qw_count<1 || qw_count>8) begin
+                    qw_accept<=1;qw_status<=ST_BAD_VALUE;qw_detail<=0;
+                end else begin qc_count<=qw_count;qc_state<=QC_QUIESCE;end
+            end else if(qc_state==QC_QUIESCE && fx_state==FX_IDLE && ax_seg_load==0) begin
+                qc_index<=0;qc_is_urgent<=0;
+                for(i=0;i<AXIS_COUNT;i=i+1) begin
+                    qc_old_depth[i]<=depth_r[i];qc_new_count[i]<=0;
+                    qc_first_of_axis[i]<=1;qc_want_preempt[i]<=0;qc_want_clear[i]<=0;qc_resume[i]<=0;
+                end
+                qc_state<=QC_VALIDATE;
+            end else if(qc_state==QC_VALIDATE) begin
+                if(qc_index==qc_count) qc_state<=QC_SPACE;
+                else if(f_axis(stage[qc_index])>=AXIS_COUNT || f_mode(stage[qc_index])>M_HOLD ||
+                    seg_field_bad(stage[qc_index],stage_min_half) ||
+                    (!qc_first_of_axis[qc_axis] && (f_preempt(stage[qc_index]) || f_clear(stage[qc_index])))) begin
+                    qw_accept<=1;qw_detail<=qc_index;qc_state<=QC_IDLE;
+                    qw_status<=(f_axis(stage[qc_index])>=AXIS_COUNT) ? 8'd6 :
+                        ((f_mode(stage[qc_index])>M_HOLD) ? 8'd7 : ST_BAD_VALUE);
                 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
+                    if(qc_first_of_axis[qc_axis]) begin
+                        qc_resume[qc_axis]<=!f_arm(stage[qc_index]);
+                        qc_want_clear[qc_axis]<=f_clear(stage[qc_index]);
+                        if(f_clear(stage[qc_index])) qc_old_depth[qc_axis]<=0;
+                        qc_want_preempt[qc_axis]<=f_preempt(stage[qc_index]);
+                        if(f_preempt(stage[qc_index])) begin
+                            qc_urgent_seg[qc_axis]<=stage[qc_index];qc_is_urgent[qc_index]<=1;
+                        end else qc_new_count[qc_axis]<=qc_new_count[qc_axis]+1'b1;
+                        qc_first_of_axis[qc_axis]<=0;
+                    end else qc_new_count[qc_axis]<=qc_new_count[qc_axis]+1'b1;
+                    qc_index<=qc_index+1'b1;
                 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 if(qc_state==QC_SPACE) begin
+                if(qc_space_bad) begin
+                    qw_accept<=1;qw_status<=8'd5;qw_detail<=0;qc_state<=QC_IDLE;
                 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;
+                    for(i=0;i<AXIS_COUNT;i=i+1) begin
+                        // Preempt's new waiting segments precede the old queue.
+                        // Clear permits reusing old slots only AFTER validation.
+                        qc_proj_depth[i]<=qc_old_depth[i]+qc_new_count[i];
+                        qc_new_head[i]<=qc_want_preempt[i] && !qc_want_clear[i] ?
+                            (head_r[i][4:0]-qc_new_count[i][4:0]) : head_r[i][4:0];
+                        qc_append_pos[i]<=qc_want_preempt[i] ?
+                            (qc_want_clear[i] ? head_r[i][4:0] :
+                                ((head_r[i][4:0]-qc_new_count[i][4:0]) & 5'h1F)) :
+                            ((head_r[i][4:0]+qc_old_depth[i][4:0]) & 5'h1F);
                     end
+                    qc_index<=0;qc_state<=QC_WRITE;
                 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 else if(qc_state==QC_WRITE) begin
+                if(qc_index==qc_count) qc_state<=QC_DRAIN;
+                else begin
+                    if(!qc_is_urgent[qc_index]) begin
+                        q_wr_addr<=qc_axis*32+qc_append_pos[qc_axis][4:0];
+                        q_wr_data<=stage[qc_index];q_we<=1;
+                        qc_append_pos[qc_axis]<=(qc_append_pos[qc_axis]+1'b1)&6'd31;
                     end
-                    if (qc_want_clear[i] && !qc_want_preempt[i]) begin
-                        ; // CLEAR via batch flag: waiting queue deleted
+                    qc_index<=qc_index+1'b1;
+                end
+            end else if(qc_state==QC_DRAIN) begin
+                // Last synchronous RAM write has completed before any reader
+                // can see the new head/depth. All axes publish on this edge.
+                for(i=0;i<AXIS_COUNT;i=i+1) begin
+                    if(!qc_first_of_axis[i]) begin
+                        depth_r[i]<=qc_proj_depth[i];head_r[i]<=qc_new_head[i];
+                        if(qc_want_clear[i]) urgent_valid_r[i]<=0;
+                        if(qc_want_preempt[i]) begin
+                            urgent_valid_r[i]<=1;urgent_r[i]<=qc_urgent_seg[i];
+                            urgent_armed_r[i]<=f_arm(qc_urgent_seg[i]);ax_preempt[i]<=1;
+                        end
+                        // A new nonarmed command or preempt explicitly resumes
+                        // an axis paused by Abort/limit. ARM alone cannot do so.
+                        if(qc_resume[i] || qc_want_preempt[i]) queue_paused_r[i]<=0;
                     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;
+                qc_state<=QC_PUBLISH;
+            end else if(qc_state==QC_PUBLISH) begin
+                qw_accept<=1;qw_status<=ST_OK;qw_detail<=0;qc_state<=QC_IDLE;
             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] && !ctl_valid && cs_state==0 && qc_state==QC_IDLE && !qw_submit) begin
+                        if (ax_load_ready[i] && !queue_paused_r[i] && !ctl_valid && cs_state==0 && qc_state==QC_IDLE && !qw_submit) begin
                             if (urgent_valid_r[i]) begin
                                 fx_state <= FX_LOAD;
-                                fx_axis <= i[1:0];
+                                fx_axis <= i[AXIS_INDEX_BITS-1:0];
                                 fx_from_urgent <= 1'b1;
                                 fx_seg <= urgent_r[i];
-                            end else if (depth_r[i] != 0 && !head_armed_r[i]) begin
+                            end else if (depth_r[i] != 0) begin
                                 fx_state <= FX_RD;
-                                fx_axis <= i[1:0];
+                                fx_axis <= i[AXIS_INDEX_BITS-1:0];
                                 fx_from_urgent <= 1'b0;
                                 q_rd_addr <= i * 32 + head_r[i][4:0];
                             end
@@ -488,7 +498,8 @@ module motor_engine #(
                     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_armed[fx_axis] <= f_arm(fx_seg) && !start_credit[fx_axis];
+                    start_credit[fx_axis] <= 0;
                     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);
@@ -497,11 +508,8 @@ module motor_engine #(
                         // 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;
+                        // Each head's ARM bit is loaded into the axis;
+                        // the pause latch belongs to Abort/limit, not ARM.
                     end
                     fx_state <= FX_IDLE;
                 end
@@ -531,12 +539,13 @@ module motor_engine #(
                     if(cs_mask[i]) begin
                         case(cs_op)
                             OP_START_ARMED: begin
-                                head_armed_r[i]<=0;
+                                queue_paused_r[i]<=0;
                                 if(ax_armed[i]) ax_start_armed[i]<=1;
+                                else if(depth_r[i]!=0 || urgent_valid_r[i]) start_credit[i]<=1;
                                 else ctl_detail<=i;
                             end
                             OP_ABORT: begin
-                                ax_abort[i]<=1;head_armed_r[i]<=1;
+                                ax_abort[i]<=1;queue_paused_r[i]<=1;
                                 if(cs_flags[0]) begin depth_r[i]<=0;urgent_valid_r[i]<=0;end
                             end
                             OP_CLEAR_QUEUE: begin depth_r[i]<=0;urgent_valid_r[i]<=0;end
@@ -544,7 +553,7 @@ module motor_engine #(
                             OP_SET_POSITION: begin ax_pos_load[i]<=1;ax_pos_value[i]<=cs_value;end
                             OP_CLEAR_LIMIT_LATCH: ax_lat_clear[i]<=1;
                             OP_HOME_LOWER,OP_SEEK_UPPER: begin
-                                ax_preempt[i]<=1;depth_r[i]<=0;head_armed_r[i]<=0;
+                                ax_preempt[i]<=1;depth_r[i]<=0;queue_paused_r[i]<=0;
                                 urgent_valid_r[i]<=1;urgent_armed_r[i]<=0;
                                 urgent_r[i]<={16'd0,16'd0,
                                     (cs_value!=0 ? cs_value :