فهرست منبع

fix(motor): repair BRAM-history integration contracts in RTL and TBs

- gate QC staging on queue write drain: the 256K storage streams the live
  wr_data port across four word writes, so re-staging during an in-flight
  write tore the captured descriptor (words 1..3 got next-descriptor bytes)
- defer event dispatch one rotation when the selected history record was
  just written: sync-BRAM read of the axis at head W+1 returns pre-write
  data (seen as total=0/zero records)
- rewrite motor_history_shared_tb rotation-phase and per-clock equivalence
  assertions as the bounded settled-state contract (goal §7)
- pin motor_axis_contract_tb/shared_timing_tb to PARALLEL_TELEMETRY(1):
  their port-level count assertions target the diagnostic view; production
  scan readout stays covered by engine/e2e TBs (core default is now 0 and
  the diagnostic_data bus is genuinely unpopulated there)
- snapshot_contract_tb progress probe: dut.index (removed) -> word 100 of
  mem_waddr (byte 200); engine TB queue-capture assertion follows the new
  streamed-word storage contract
codex 2 ماه پیش
والد
کامیت
bb98eefb17

+ 23 - 5
src/motor/motor_engine.v

@@ -549,10 +549,22 @@ module motor_engine #(
     // Both producers register the previous scan axis. The same rotating
     // storage serves each independent write stream; unused upper bits vanish.
     // u_done_history merged into u_history_brams
-    // Reset clears owner validity, never exposing stale stored events.
+    // Reset clears owner validity, never exposing stale stored records.
     always @(posedge clk)begin
         if(PARALLEL_TELEMETRY && rst_n && (|ax_limit_pulse))limit_record[limit_write_axis]<={ax_limit_time_high,ax_limit_data};
     end
+    // Sync-BRAM history returns data one cycle after the address is presented.
+    // A record written at edge W would read back stale (pre-write) data if the
+    // rotating head lands on that axis at W+1; defer that dispatch one rotation.
+    reg [AXIS_COUNT-1:0] history_fresh;
+    always @(posedge clk or negedge rst_n) begin
+        if(!rst_n) history_fresh <= {AXIS_COUNT{1'b0}};
+        else begin
+            history_fresh <= {AXIS_COUNT{1'b0}};
+            if(|ax_limit_pulse) history_fresh[limit_write_axis] <= 1'b1;
+            if(done_record_write) history_fresh[done_write_axis] <= 1'b1;
+        end
+    end
     function [7:0] event_for_stop;
         input [3:0] r;
         begin
@@ -606,7 +618,7 @@ module motor_engine #(
             if(ev_wr_req && ev_ack) ev_wr_req<=0;
             else if(!ev_wr_req && reset_pending) begin
                 ev_wr_req<=1;ev_axis<=4'hF;ev_code<=6;ev_tag<=0;ev_pos<=0;ev_total<=0;ev_timestamp<=0;reset_pending<=0;
-            end else if(!ev_wr_req && ev_select>=0 && event_dispatch_ready) begin
+            end else if(!ev_wr_req && ev_select>=0 && event_dispatch_ready && !history_fresh[ev_select]) begin
                 ev_wr_req<=1;ev_axis<=selected_record[35:32];ev_timestamp<=selected_record[31:0];ev_code<=selected_record[47:40];
                 ev_tag<=selected_record[63:48];ev_pos<=selected_record[95:64];ev_total<=selected_record[127:96];
                 if(select_limit)limit_pending[ev_select]<=0;else done_pending[ev_select]<=0;
@@ -687,9 +699,15 @@ module motor_engine #(
             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
+                // The queue storage streams the live wr_data port across its
+                // four write words. Hold the validated descriptor untouched
+                // until the in-flight write has drained, or words 1..3 would
+                // capture the next descriptor's first staging bytes.
+                if(!q_write_busy) 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
             end else if(qc_state==QC_VALIDATE) begin
                 if(qc_index==qc_count) qc_state<=QC_SPACE;
                 else if(f_axis(stage_data)>=AXIS_COUNT || f_mode(stage_data)>M_HOLD ||

+ 1 - 1
tests/sim/motor_axis_contract_tb.v

@@ -23,7 +23,7 @@ module motor_axis_contract_tb;
     wire [31:0] lh,uh;
     wire [15:0] tag;
     wire llatch,ulatch,both;
-    motor_axis_core #(.DIR_SETUP_TICKS(3)) dut (
+    motor_axis_core #(.DIR_SETUP_TICKS(3),.PARALLEL_TELEMETRY(1)) dut (
         .clk(clk),.rst_n(rst_n),.tick_us(tick_us),.limit_lower(ll),.limit_upper(ul),
         .seg_load(load),.seg_mode(mode),.seg_dir_upper(upper),.seg_armed(armed_in),
         .seg_count(count),.seg_half_us(half_us),.seg_tag(16'h1234),.start_armed(start),

+ 6 - 2
tests/sim/motor_engine_12_tb.v

@@ -129,6 +129,10 @@ module motor_engine_12_tb;
     queue_capture_check<=dut.q_we && !dut.u_queue.write_active;
    end
   end
-  always @(negedge clk)if(queue_capture_check && dut.u_queue.write_data_r!==queue_capture_expected)
-   $fatal(1,"queue captured a changed descriptor instead of the validated producer word");
+  // The storage streams wr_data across four word writes; every written word
+  // must come from the validated descriptor captured at q_we.
+  always @(negedge clk)
+   if(dut.u_queue.write_active &&
+      dut.u_queue.write_wdata!==queue_capture_expected[dut.u_queue.write_word*32 +: 32])
+    $fatal(1,"queue captured a changed descriptor instead of the validated producer word");
 endmodule

+ 6 - 2
tests/sim/motor_engine_tb.v

@@ -398,6 +398,10 @@ module motor_engine_tb;
     queue_capture_check<=dut.q_we && !dut.u_queue.write_active;
    end
   end
-  always @(negedge clk)if(queue_capture_check && dut.u_queue.write_data_r!==queue_capture_expected)
-   $fatal(1,"queue captured a changed descriptor instead of the validated producer word");
+  // The storage streams wr_data across four word writes; every written word
+  // must come from the validated descriptor captured at q_we.
+  always @(negedge clk)
+   if(dut.u_queue.write_active &&
+      dut.u_queue.write_wdata!==queue_capture_expected[dut.u_queue.write_word*32 +: 32])
+    $fatal(1,"queue captured a changed descriptor instead of the validated producer word");
 endmodule

+ 15 - 7
tests/sim/motor_history_shared_tb.v

@@ -37,15 +37,23 @@ module motor_history_shared_tb #(parameter integer AXIS_COUNT=3);
   repeat(20)@(negedge clk);
  end endtask
  reg [63:0]expected_time;reg captured=0;
+ // Bounded-visibility contract (sync-BRAM history): limit/done records land by
+ // axis address at event time and production telemetry follows through the
+ // rotating synchronous read within one rotation. Production must equal the
+ // parallel diagnostic view once the engine is quiescent long enough for every
+ // axis to be re-read; per-clock zero-cycle equivalence is retired.
+ integer quiet=0;
  always @(posedge clk)begin
-  if(rst_n && g[0].dut.done_read_axis!=g[0].dut.history_scan_axis)$fatal(1,"event banks lost common rotation phase");
-  if(rst_n && g[0].dut.done_record_write && g[0].dut.done_write_axis!=g[0].dut.done_read_axis)$fatal(1,"done producer differs from rotating bank head");
-  if(rst_n && (|g[0].dut.ax_limit_pulse) && g[0].dut.limit_write_axis!=g[0].dut.history_scan_axis)
-   $fatal(1,"rotating history head differs from emitted limit axis");
+  if(!rst_n) quiet=0;
+  else if(|g[0].dut.ax_limit_pulse || |g[0].dut.ax_done || g[0].dut.done_record_write || g[0].dut.status_changing) quiet=0;
+  else if(quiet<4*AXIS_COUNT+8) quiet=quiet+1;
   if(rst_n && g[0].dut.u_axes.lower_rise[0])begin expected_time=g[0].dut.time_us;captured=1;end
-  #1;
-  if(rst_n && (axis[0]!==axis[1] || word_data[0]!==word_data[1]))$fatal(1,"production/diagnostic status word differs");
-  if(rst_n && word_data[0]!==record[1][axis[0]*512+word_address*16+:16])$fatal(1,"production word differs from full diagnostic record");
+ end
+ always @(negedge clk)begin
+  if(rst_n && quiet>=4*AXIS_COUNT+8)begin
+   if(axis[0]!==axis[1] || word_data[0]!==word_data[1])$fatal(1,"settled production/diagnostic status word differs");
+   if(word_data[0]!==record[1][axis[0]*512+word_address*16+:16])$fatal(1,"settled production word differs from full diagnostic record");
+  end
  end
  // Follow the real snapshot consumer: every axis must be observed for
  // each word before advancing, including while event reads share the port.

+ 1 - 1
tests/sim/motor_shared_timing_tb.v

@@ -13,7 +13,7 @@ module motor_shared_timing_tb;
  reg[N*3-1:0]mode=0;reg[N*32-1:0]count=0,half_period=0;reg[N*16-1:0]tag=0;
  wire[N-1:0]steps,active,armed,ready,done;wire[N*32-1:0]total,last;
  wire stable,miss;
- motor_axis_core #(.AXIS_COUNT(N),.DIR_SETUP_TICKS(10)) dut(.clk(clk),.rst_n(rst_n),.tick_us(tick),
+ motor_axis_core #(.AXIS_COUNT(N),.DIR_SETUP_TICKS(10),.PARALLEL_TELEMETRY(1)) dut(.clk(clk),.rst_n(rst_n),.tick_us(tick),
  .limit_lower(ll),.limit_upper(ul),.seg_load(load),.seg_mode(mode),.seg_dir_upper(up),.seg_armed(arm),
  .seg_count(count),.seg_half_us(half_period),.seg_tag(tag),.start_armed(start),.abort_req(abort_req),.preempt_req(12'd0),
  .step_out(steps),.dir_out(),.active(active),.armed(armed),.load_ready(ready),.done_pulse(done),.done_reason(),.done_tag(),

+ 2 - 1
tests/sim/mrdimm_snapshot_contract_tb.v

@@ -57,7 +57,8 @@ module mrdimm_snapshot_contract_tb;
         // Change every axis byte after a prefix has already been copied.
         // A version wrapFFFF->0 must force a complete retry, not mixed data.
         generation=0;capture=1;@(negedge clk);capture=0;
-        wait(dut.index==200);@(negedge clk);
+        // mem_waddr counts 16-bit words; word 100 == byte 200 of the copy.
+        wait(dut.mem_waddr==100);@(negedge clk);
         for(i=0;i<192;i=i+1)begin
             expected[160+i]=((i*19+73)^(i>>3))&255;axes[i*8 +:8]=expected[160+i];
         end