浏览代码

refactor(motor): keep host preempt descriptors in reserved queue RAM slots

codex 3 月之前
父节点
当前提交
93c4d79a42
共有 1 个文件被更改,包括 19 次插入 和 14 次删除
  1. 19 14
      src/motor/motor_engine.v

+ 19 - 14
src/motor/motor_engine.v

@@ -110,13 +110,13 @@ module motor_engine #(
     end
 
     // ---- shared queue RAM ----
-    localparam QAW = $clog2(AXIS_COUNT * 32);
+    localparam QAW = $clog2(AXIS_COUNT * 33);
     reg [QAW-1:0] q_wr_addr, q_rd_addr;
     reg [127:0] q_wr_data;
     reg q_we,q_re;
     wire [127:0] q_rd;
     wire q_write_busy,q_read_valid;
-    motor_queue_storage #(.SLOTS(AXIS_COUNT*32)) u_queue (
+    motor_queue_storage #(.SLOTS(AXIS_COUNT*33)) u_queue (
         .clk(clk),.rst_n(rst_n),.wr_req(q_we),.wr_slot(q_wr_addr),.wr_data(q_wr_data),
         .wr_busy(q_write_busy),.rd_req(q_re),.rd_slot(q_rd_addr),.rd_data(q_rd),.rd_valid(q_read_valid)
     );
@@ -127,7 +127,7 @@ module motor_engine #(
     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];
+    reg       urgent_from_queue [0:AXIS_COUNT-1];
     reg       home_pending_r [0:AXIS_COUNT-1];
 
     // ---- byte-wide batch BRAM and one shared segment assembly register ----
@@ -196,7 +196,6 @@ module motor_engine #(
     reg [AXIS_COUNT-1:0] qc_first_of_axis;
     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_data) == 2'd0) ? X_MIN_HALF_US :
         (f_axis(stage_data) == 2'd1) ? Y_MIN_HALF_US : Z_MIN_HALF_US;
@@ -467,7 +466,7 @@ module motor_engine #(
             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; queue_paused_r[i] <= 1'b0;
-                urgent_valid_r[i] <= 1'b0; urgent_armed_r[i] <= 1'b0;
+                urgent_valid_r[i] <= 1'b0; urgent_from_queue[i] <= 1'b0;
                 home_pending_r[i] <= 1'b0;
             end
             q_we <= 1'b0;q_re<=0;
@@ -523,7 +522,7 @@ module motor_engine #(
                         if(f_clear(stage_data)) qc_old_depth[qc_axis]<=0;
                         qc_want_preempt[qc_axis]<=f_preempt(stage_data);
                         if(f_preempt(stage_data)) begin
-                            qc_urgent_seg[qc_axis]<=stage_data;qc_is_urgent[qc_index]<=1;
+                            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;
@@ -550,11 +549,15 @@ module motor_engine #(
             end else if(qc_state==QC_WRITE) begin
                 if(qc_index==qc_count) qc_state<=QC_DRAIN;
                 else if(!q_write_busy)begin
-                    if(!qc_is_urgent[qc_index]) begin
+                    if(qc_is_urgent[qc_index])begin
+                        // Reserved urgent slots follow all waiting slots.
+                        // Write after full validation, publish valid atomically.
+                        q_wr_addr<=AXIS_COUNT*32+qc_axis;
+                    end else begin
                         q_wr_addr<=qc_axis*32+qc_append_pos[qc_axis][4:0];
-                        q_wr_data<=stage_data;q_we<=1;
                         qc_append_pos[qc_axis]<=(qc_append_pos[qc_axis]+1'b1)&6'd31;
                     end
+                    q_wr_data<=stage_data;q_we<=1;
                     qc_index<=qc_index+1'b1;stage_byte_index<=0;
                     qc_state<=qc_index+1'b1==qc_count ? QC_DRAIN : QC_STAGE_WAIT;
                 end
@@ -567,8 +570,7 @@ module motor_engine #(
                         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;
+                            urgent_valid_r[i]<=1;urgent_from_queue[i]<=1;ax_preempt[i]<=1;
                         end
                         // A new nonarmed command or preempt explicitly resumes
                         // an axis paused by Abort/limit. ARM alone cannot do so.
@@ -589,10 +591,13 @@ module motor_engine #(
                         if (ax_load_ready[fx_candidate] && !ax_seg_load[fx_candidate] && !queue_paused_r[fx_candidate] && !ctl_valid &&
                             (cs_state==0 || (cs_state==3 && cs_start_mask[fx_candidate])) && qc_state==QC_IDLE && !qw_submit) begin
                             if (urgent_valid_r[fx_candidate]) begin
-                                fx_state <= FX_LOAD;
                                 fx_axis <= fx_candidate[AXIS_INDEX_BITS-1:0];
                                 fx_from_urgent <= 1'b1;
-                                fx_seg <= urgent_r[fx_candidate];
+                                if(urgent_from_queue[fx_candidate])begin
+                                    fx_state<=FX_RD;q_rd_addr<=AXIS_COUNT*32+fx_candidate;q_re<=1;
+                                end else begin
+                                    fx_state<=FX_LOAD;fx_seg<=urgent_r[fx_candidate];
+                                end
                             end else if (depth_r[fx_candidate] != 0) begin
                                 fx_state <= FX_RD;
                                 fx_axis <= fx_candidate[AXIS_INDEX_BITS-1:0];
@@ -671,7 +676,7 @@ module motor_engine #(
                             OP_CLEAR_LIMIT_LATCH: ax_lat_clear[i]<=1;
                             OP_HOME_LOWER,OP_SEEK_UPPER: begin
                                 ax_preempt[i]<=1;depth_r[i]<=0;queue_paused_r[i]<=0;
-                                urgent_valid_r[i]<=1;urgent_armed_r[i]<=0;
+                                urgent_valid_r[i]<=1;urgent_from_queue[i]<=0;
                                 urgent_r[i]<={16'd0,16'd0,
                                     (cs_value!=0 ? cs_value :
                                         (i==0 ? X_DEFAULT_HALF_US : (i==1 ? Y_DEFAULT_HALF_US : Z_DEFAULT_HALF_US))),
@@ -712,7 +717,7 @@ endmodule
 // count32, half-period32 and tag16. Axis is implicit in the slot address;
 // preempt/clear/reserved fields have already been consumed by batch commit.
 // Five18-bit words preserve the external16-byte descriptor contract while
-// packing96 waiting slots into480 words of one512x18 BRAM on the3-axis part.
+// packing96 waiting plus3 urgent slots into495 words of one512x18 BRAM.
 module motor_queue_storage #(parameter integer SLOTS=96,
     parameter integer SLOT_AW=$clog2(SLOTS)) (
     input wire clk,rst_n,wr_req,rd_req,