Просмотр исходного кода

refactor(motor): serialize batch staging through byte-wide BRAM

codex 3 месяцев назад
Родитель
Сommit
44cfbf44cf
1 измененных файлов с 42 добавлено и 35 удалено
  1. 42 35
      src/motor/motor_engine.v

+ 42 - 35
src/motor/motor_engine.v

@@ -129,20 +129,19 @@ module motor_engine #(
     reg       urgent_armed_r [0:AXIS_COUNT-1];
     reg       home_pending_r [0:AXIS_COUNT-1];
 
-    // ---- batch staging ----
-    reg [127:0] stage [0:7];
-    // Constant part-select drivers avoid TD treating a variable byte write
-    // into a variable array element as an undriven memory. Each byte still
-    // has exactly one write enable and the same asynchronous validation view.
-    genvar stage_slot,stage_byte;
-    generate for(stage_slot=0;stage_slot<8;stage_slot=stage_slot+1) begin:g_stage_slot
-        for(stage_byte=0;stage_byte<16;stage_byte=stage_byte+1) begin:g_stage_byte
-            always @(posedge clk) begin
-                if(qw_byte_we && qw_byte_addr[6:4]==stage_slot && qw_byte_addr[3:0]==stage_byte)
-                    stage[stage_slot][stage_byte*8 +:8]<=qw_byte_data;
-            end
-        end
-    end endgenerate
+    // ---- byte-wide batch BRAM and one shared segment assembly register ----
+    // Validate every segment before publishing queue state. Two clocks per
+    // byte hide the synchronous RAM latency; STEP scanning never waits here.
+    reg [3:0] qc_index;
+    reg [3:0] stage_byte_index;
+    reg stage_for_write;
+    reg [127:0] stage_data;
+    wire [7:0] stage_read_data;
+    ef2_bram_dp #(.DATA_WIDTH(8),.ADDR_WIDTH(7),.DATA_DEPTH(128)) u_stage (
+        .clk(clk),.a_en(qw_byte_we),.a_we(qw_byte_we),.a_addr(qw_byte_addr),
+        .a_wdata(qw_byte_data),.a_rdata(),.b_en(1'b1),.b_we(1'b0),
+        .b_addr({qc_index[2:0],stage_byte_index}),.b_wdata(8'd0),.b_rdata(stage_read_data)
+    );
 
     // segment field slices — wire segment byte order (contract 10.2):
     // byte0 axis, byte1 mode, byte2 flags, byte3 reserved,
@@ -176,9 +175,8 @@ module motor_engine #(
 
     // ---- batch validate/commit FSM ----
     localparam [3:0] QC_IDLE=0, QC_QUIESCE=1, QC_VALIDATE=2, QC_SPACE=3,
-        QC_WRITE=4, QC_DRAIN=5, QC_PUBLISH=6;
+        QC_WRITE=4, QC_DRAIN=5, QC_PUBLISH=6, QC_STAGE_WAIT=7, QC_STAGE_READ=8;
     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];
@@ -199,9 +197,9 @@ module motor_engine #(
     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 [AXIS_INDEX_BITS-1:0] qc_axis = f_axis(stage[qc_index]);
+    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;
+    wire [AXIS_INDEX_BITS-1:0] qc_axis = f_axis(stage_data);
 
     // ---- execute fetch FSM ----
     localparam [1:0] FX_IDLE = 2'd0, FX_RD = 2'd1, FX_LOAD = 2'd2, FX_WAIT = 2'd3;
@@ -445,6 +443,7 @@ module motor_engine #(
     always @(posedge clk or negedge rst_n) begin
         if (!rst_n) begin
             qc_state <= QC_IDLE; qc_index <= 4'd0;start_credit<=0;
+            stage_byte_index<=0;stage_for_write<=0;stage_data<=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;fx_round<=0;cs_start_mask<=0;
@@ -492,27 +491,34 @@ module motor_engine #(
                     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;
+                stage_byte_index<=0;stage_for_write<=0;qc_state<=QC_STAGE_WAIT;
+            end else if(qc_state==QC_STAGE_WAIT) begin
+                qc_state<=QC_STAGE_READ;
+            end else if(qc_state==QC_STAGE_READ) begin
+                stage_data<={stage_read_data,stage_data[127:8]};
+                if(stage_byte_index==15) qc_state<=stage_for_write ? QC_WRITE : QC_VALIDATE;
+                else begin stage_byte_index<=stage_byte_index+1'b1;qc_state<=QC_STAGE_WAIT;end
             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
+                else if(f_axis(stage_data)>=AXIS_COUNT || f_mode(stage_data)>M_HOLD ||
+                    seg_field_bad(stage_data,stage_min_half) ||
+                    (!qc_first_of_axis[qc_axis] && (f_preempt(stage_data) || f_clear(stage_data)))) 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);
+                    qw_status<=(f_axis(stage_data)>=AXIS_COUNT) ? 8'd6 :
+                        ((f_mode(stage_data)>M_HOLD) ? 8'd7 : ST_BAD_VALUE);
                 end else begin
                     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;
+                        qc_resume[qc_axis]<=!f_arm(stage_data);
+                        qc_want_clear[qc_axis]<=f_clear(stage_data);
+                        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;
                         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;
+                    qc_index<=qc_index+1'b1;stage_byte_index<=0;
+                    qc_state<=qc_index+1'b1==qc_count ? QC_SPACE : QC_STAGE_WAIT;
                 end
             end else if(qc_state==QC_SPACE) begin
                 if(qc_space_bad) begin
@@ -529,17 +535,18 @@ module motor_engine #(
                                 ((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;
+                    qc_index<=0;stage_byte_index<=0;stage_for_write<=1;qc_state<=QC_STAGE_WAIT;
                 end
             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;
+                        q_wr_data<=stage_data;q_we<=1;
                         qc_append_pos[qc_axis]<=(qc_append_pos[qc_axis]+1'b1)&6'd31;
                     end
-                    qc_index<=qc_index+1'b1;
+                    qc_index<=qc_index+1'b1;stage_byte_index<=0;
+                    qc_state<=qc_index+1'b1==qc_count ? QC_DRAIN : QC_STAGE_WAIT;
                 end
             end else if(qc_state==QC_DRAIN) begin
                 // Last synchronous RAM write has completed before any reader