Pārlūkot izejas kodu

fix(rtl): one-shot limit snap and gate hold until engine low

Convert runtime endpoint limit correction to consumed assertion events so
source-limit hysteresis cannot overwrite gated_step_rise position deltas.
Add per-axis gate_hold_until_engine_low so limit truncate cannot reopen
gated STEP before the engine high phase ends.

Co-authored-by: Cursor <cursoragent@cursor.com>
root 3 mēneši atpakaļ
vecāks
revīzija
db9b7202e5

+ 39 - 17
src/modules/door_axis_controller.v

@@ -83,6 +83,8 @@ module door_axis_controller #(
     reg        cal_started;
     reg        cal_started;
     reg        need_planner;
     reg        need_planner;
     reg        wd_stopped;
     reg        wd_stopped;
+    reg        open_limit_armed;
+    reg        close_limit_armed;
 
 
     assign calibrated = calibrated_r;
     assign calibrated = calibrated_r;
     assign faulted = (status_state == ST_FAULT);
     assign faulted = (status_state == ST_FAULT);
@@ -100,19 +102,29 @@ module door_axis_controller #(
     wire [CNT_W-1:0] guard_limit = travel_steps + ENDPOINT_GUARD_EXTRA[CNT_W-1:0];
     wire [CNT_W-1:0] guard_limit = travel_steps + ENDPOINT_GUARD_EXTRA[CNT_W-1:0];
     wire in_calibration = (status_state == ST_CAL_SEEK_CLOSE) ||
     wire in_calibration = (status_state == ST_CAL_SEEK_CLOSE) ||
                           (status_state == ST_CAL_MEASURE_OPEN);
                           (status_state == ST_CAL_MEASURE_OPEN);
-    // Pre-NBA view: single-limit coordinate disagrees with endpoint snap target.
-    // Used so same-cycle heartbeat/request still replans after §1b correction.
+    wire open_assert_event = open_limit && open_limit_armed;
+    wire close_assert_event = close_limit && close_limit_armed;
+    wire departing_close_source =
+        calibrated_r && !in_calibration &&
+        ((status_state == ST_MOVE_OPEN) || (moving && !dir_close));
+    wire departing_open_source =
+        calibrated_r && !in_calibration &&
+        ((status_state == ST_MOVE_CLOSE) || (moving && dir_close));
+    wire close_snap_accept =
+        calibrated_r && !in_calibration &&
+        close_assert_event && !departing_close_source;
+    wire open_snap_accept =
+        calibrated_r && !in_calibration &&
+        open_assert_event && !departing_open_source;
+    // Pre-NBA view: single-limit coordinate disagrees with accepted snap event.
     wire limit_pos_mismatch =
     wire limit_pos_mismatch =
-        (calibrated_r && close_limit && !open_limit &&
-         (current_position != travel_steps)) ||
-        (calibrated_r && open_limit && !close_limit &&
-         (current_position != {CNT_W{1'b0}}));
+        (close_snap_accept && (current_position != travel_steps)) ||
+        (open_snap_accept && (current_position != {CNT_W{1'b0}}));
 
 
     // Combinational endpoint-effective coordinate for same-cycle planner decisions.
     // Combinational endpoint-effective coordinate for same-cycle planner decisions.
-    // §1b NBA snap does not update current_position until the next delta cycle.
     wire [CNT_W-1:0] effective_current_position =
     wire [CNT_W-1:0] effective_current_position =
-        (calibrated_r && close_limit && !open_limit) ? travel_steps :
-        (calibrated_r && open_limit  && !close_limit) ? {CNT_W{1'b0}} :
+        close_snap_accept ? travel_steps :
+        open_snap_accept ? {CNT_W{1'b0}} :
         current_position;
         current_position;
 
 
     // Distinguish request bookkeeping from material replan preempt.
     // Distinguish request bookkeeping from material replan preempt.
@@ -191,6 +203,8 @@ module door_axis_controller #(
             step <= 1'b0;
             step <= 1'b0;
             dir_close <= 1'b1;
             dir_close <= 1'b1;
             wd_stopped <= 1'b0;
             wd_stopped <= 1'b0;
+            open_limit_armed <= 1'b1;
+            close_limit_armed <= 1'b1;
         end else if (both_limits) begin
         end else if (both_limits) begin
             status_state <= ST_FAULT;
             status_state <= ST_FAULT;
             fault_code <= FC_BOTH_LIMITS;
             fault_code <= FC_BOTH_LIMITS;
@@ -232,14 +246,22 @@ module door_axis_controller #(
             end
             end
 
 
             // ----------------------------------------------------------
             // ----------------------------------------------------------
-            // 1b) Single-limit position snap (before host replan / planner).
-            // Same-cycle request+limit must use corrected coordinates; also
-            // overrides any gated_step_rise delta from §1 this cycle.
+            // 1b) One-shot single-limit snap (consumes assertion interval).
+            // Departure from a consumed source limit must not overwrite §1.
             // ----------------------------------------------------------
             // ----------------------------------------------------------
-            if (calibrated_r && open_limit && !close_limit)
-                current_position <= {CNT_W{1'b0}};
-            else if (calibrated_r && close_limit && !open_limit)
+            if (!open_limit)
+                open_limit_armed <= 1'b1;
+            if (!close_limit)
+                close_limit_armed <= 1'b1;
+            if (open_assert_event)
+                open_limit_armed <= 1'b0;
+            if (close_assert_event)
+                close_limit_armed <= 1'b0;
+
+            if (close_snap_accept)
                 current_position <= travel_steps;
                 current_position <= travel_steps;
+            else if (open_snap_accept)
+                current_position <= {CNT_W{1'b0}};
 
 
             // ----------------------------------------------------------
             // ----------------------------------------------------------
             // 2a) Non-destructive request bookkeeping (idempotent OK).
             // 2a) Non-destructive request bookkeeping (idempotent OK).
@@ -669,9 +691,9 @@ module door_axis_controller #(
             end else if (status_state == ST_IDLE) begin
             end else if (status_state == ST_IDLE) begin
                 step <= 1'b0;
                 step <= 1'b0;
                 moving <= 1'b0;
                 moving <= 1'b0;
-                if (calibrated_r && is_open_endpoint && open_limit)
+                if (open_snap_accept)
                     current_position <= {CNT_W{1'b0}};
                     current_position <= {CNT_W{1'b0}};
-                if (calibrated_r && is_close_endpoint && close_limit)
+                if (close_snap_accept)
                     current_position <= travel_steps;
                     current_position <= travel_steps;
                 if (replan_pending)
                 if (replan_pending)
                     status_state <= ST_WAIT_REPLAN;
                     status_state <= ST_WAIT_REPLAN;

+ 21 - 2
src/modules/prod2u_runtime_step_limit_gate.v

@@ -1,10 +1,12 @@
 `timescale 1ns/1ps
 `timescale 1ns/1ps
 
 
-// Production selected-limit hard gate: same assign semantics used by top.
+// Production selected-limit hard gate: immediate truncate + hold until engine low.
 // Formal and sim must instantiate this module (not a hand-copied shell).
 // Formal and sim must instantiate this module (not a hand-copied shell).
 module prod2u_runtime_step_limit_gate #(
 module prod2u_runtime_step_limit_gate #(
     parameter integer AXIS_COUNT = 3
     parameter integer AXIS_COUNT = 3
 ) (
 ) (
+    input  wire                   clk,
+    input  wire                   rst_n,
     input  wire [AXIS_COUNT-1:0] engine_step,
     input  wire [AXIS_COUNT-1:0] engine_step,
     input  wire [AXIS_COUNT-1:0] engine_dir,
     input  wire [AXIS_COUNT-1:0] engine_dir,
     input  wire [AXIS_COUNT-1:0] close_limit,
     input  wire [AXIS_COUNT-1:0] close_limit,
@@ -12,12 +14,29 @@ module prod2u_runtime_step_limit_gate #(
     output wire [AXIS_COUNT-1:0] selected_limit,
     output wire [AXIS_COUNT-1:0] selected_limit,
     output wire [AXIS_COUNT-1:0] gated_step
     output wire [AXIS_COUNT-1:0] gated_step
 );
 );
+    reg [AXIS_COUNT-1:0] gate_hold;
+
     genvar gi;
     genvar gi;
     generate
     generate
         for (gi = 0; gi < AXIS_COUNT; gi = gi + 1) begin : gen_gate
         for (gi = 0; gi < AXIS_COUNT; gi = gi + 1) begin : gen_gate
             assign selected_limit[gi] =
             assign selected_limit[gi] =
                 engine_dir[gi] ? close_limit[gi] : open_limit[gi];
                 engine_dir[gi] ? close_limit[gi] : open_limit[gi];
-            assign gated_step[gi] = engine_step[gi] & ~selected_limit[gi];
+            assign gated_step[gi] =
+                engine_step[gi] & ~selected_limit[gi] & ~gate_hold[gi];
         end
         end
     endgenerate
     endgenerate
+
+    integer ai;
+    always @(posedge clk or negedge rst_n) begin
+        if (!rst_n) begin
+            gate_hold <= {AXIS_COUNT{1'b0}};
+        end else begin
+            for (ai = 0; ai < AXIS_COUNT; ai = ai + 1) begin
+                if (!engine_step[ai])
+                    gate_hold[ai] <= 1'b0;
+                else if (selected_limit[ai])
+                    gate_hold[ai] <= 1'b1;
+            end
+        end
+    end
 endmodule
 endmodule

+ 2 - 0
src/top/prod2u_p4_normal_v3_top.v

@@ -154,6 +154,8 @@ module prod2u_p4_normal_v3_top (
     endgenerate
     endgenerate
 
 
     prod2u_runtime_step_limit_gate #(.AXIS_COUNT(AXIS_COUNT)) u_step_limit_gate (
     prod2u_runtime_step_limit_gate #(.AXIS_COUNT(AXIS_COUNT)) u_step_limit_gate (
+        .clk(sys_clk),
+        .rst_n(sys_rst_n),
         .engine_step(engine_step),
         .engine_step(engine_step),
         .engine_dir(engine_dir),
         .engine_dir(engine_dir),
         .close_limit(close_limit),
         .close_limit(close_limit),

+ 2 - 0
tests/formal/runtime_limit_hard_stop_formal.v

@@ -17,6 +17,8 @@ module runtime_limit_hard_stop_formal (
     reg past_valid;
     reg past_valid;
 
 
     prod2u_runtime_step_limit_gate #(.AXIS_COUNT(3)) dut (
     prod2u_runtime_step_limit_gate #(.AXIS_COUNT(3)) dut (
+        .clk(clk),
+        .rst_n(rst_n),
         .engine_step(engine_step),
         .engine_step(engine_step),
         .engine_dir(engine_dir),
         .engine_dir(engine_dir),
         .close_limit(close_limit),
         .close_limit(close_limit),

+ 1 - 1
tests/unit/test_top_step_gate_matches_formal.py

@@ -20,7 +20,7 @@ class TopStepGateMatchesFormalTests(unittest.TestCase):
         gate = GATE.read_text(encoding="utf-8")
         gate = GATE.read_text(encoding="utf-8")
         sby = SBY.read_text(encoding="utf-8")
         sby = SBY.read_text(encoding="utf-8")
         self.assertIn("module prod2u_runtime_step_limit_gate", gate)
         self.assertIn("module prod2u_runtime_step_limit_gate", gate)
-        self.assertIn("engine_step[gi] & ~selected_limit[gi]", gate)
+        self.assertIn("gate_hold", gate)
         self.assertIn("prod2u_runtime_step_limit_gate", top)
         self.assertIn("prod2u_runtime_step_limit_gate", top)
         self.assertIn("u_step_limit_gate", top)
         self.assertIn("u_step_limit_gate", top)
         self.assertIn("assign x_step = gated_step[0]", top)
         self.assertIn("assign x_step = gated_step[0]", top)