ソースを参照

test: add stop-mask testbenches and update unit contracts

- Add tests/sim/door_motor_engine_stop_tb.v covering per-axis stop
  semantics: stop while low, stop while high, staggered X/Y stops,
  setup-time stop, 449th-pulse boundary, and host-mode unchanged.
- Add tests/sim/startup_motor_stop_tb.v covering startup/controller
  + motor engine integration stop scenarios with reset.
- Update tests/unit/test_startup_close_seek.py and
  test_startup_mutation.py for the new motor_stop_mask output and
  project contract fields.
- Include new simulation targets in verification manifest.

Generated with Devin

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Codex Resource Selftest 4 ヶ月 前
親
コミット
e9b16d476d

+ 3 - 1
config/prod2u_p4_verification_targets_v1.json

@@ -27,6 +27,7 @@
       "project_adapter_tb",
       "pwm_scheduler_tb",
       "door_motor_engine_tb",
+      "door_motor_engine_stop_tb",
       "temperature_reader_tb",
       "frame_tx_uart_tx_tb",
       "temperature_reader_config_tb",
@@ -35,7 +36,8 @@
       "output_target_ram_tb",
       "host_watchdog_tb",
       "shared_500us_tick_tb",
-      "startup_close_seek_tb"
+      "startup_close_seek_tb",
+      "startup_motor_stop_tb"
     ],
     "verilator": ["prod2u_door_temp_top_lint"],
     "cocotb": ["prod2u_door_temp_uart_e2e"],

+ 7 - 1
src/modules/door_motor_engine.v

@@ -75,7 +75,13 @@ module door_motor_engine #(
                         end
                     end else begin
                         step <= 2'b00;
-                        if ((rising_edges + 1'b1 >= FIXED_STEPS) || (active_after_stop == 2'b00)) begin
+                        if (rising_edges + 1 >= FIXED_STEPS) begin
+                            busy <= 1'b0;
+                            done <= 1'b1;
+                            setup_count <= {SETUP_BITS{1'b0}};
+                            rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
+                            active_reg <= 2'b00;
+                        end else if (active_after_stop == 2'b00) begin
                             busy <= 1'b0;
                             done <= 1'b1;
                             setup_count <= {SETUP_BITS{1'b0}};

+ 183 - 0
tests/sim/door_motor_engine_stop_tb.v

@@ -0,0 +1,183 @@
+`timescale 1ns/1ps
+
+module door_motor_engine_stop_tb;
+    reg clk = 1'b0;
+    reg rst_n = 1'b0;
+    reg start = 1'b0;
+    reg [1:0] active_mask = 2'b11;
+    reg [1:0] dir_mask = 2'b11;
+    reg tick_500us = 1'b0;
+    reg abort = 1'b0;
+    reg [1:0] stop_mask = 2'b00;
+    wire busy;
+    wire done;
+    wire [1:0] step;
+    wire [1:0] dir;
+
+    integer x_rises;
+    integer y_rises;
+    integer edge_count;
+    reg prev_x;
+    reg prev_y;
+
+    always #1 clk = ~clk;
+
+    door_motor_engine #(
+        .FIXED_STEPS(450), .DIR_SETUP_TICKS(2)
+    ) dut (
+        .clk(clk), .rst_n(rst_n), .start(start),
+        .active_mask(active_mask), .dir_mask(dir_mask),
+        .tick_500us(tick_500us), .abort(abort),
+        .stop_mask(stop_mask),
+        .busy(busy), .done(done), .step(step), .dir(dir)
+    );
+
+    always @(posedge clk) begin
+        prev_x <= step[0];
+        prev_y <= step[1];
+        if (step[0] && !prev_x) x_rises <= x_rises + 1;
+        if (step[1] && !prev_y) y_rises <= y_rises + 1;
+    end
+
+    task pulse_tick;
+        begin
+            @(negedge clk);
+            tick_500us = 1'b1;
+            @(negedge clk);
+            tick_500us = 1'b0;
+        end
+    endtask
+
+    task reset;
+        begin
+            rst_n = 1'b0;
+            start = 1'b0;
+            stop_mask = 2'b00;
+            abort = 1'b0;
+            active_mask = 2'b11;
+            x_rises = 0;
+            y_rises = 0;
+            prev_x = 1'b0;
+            prev_y = 1'b0;
+            repeat (3) @(posedge clk);
+            rst_n = 1'b1;
+            @(posedge clk);
+        end
+    endtask
+
+    task start_motor;
+        begin
+            @(negedge clk);
+            start = 1'b1;
+            @(negedge clk);
+            start = 1'b0;
+        end
+    endtask
+
+    task wait_for_done;
+        begin
+            while (!done) pulse_tick();
+            @(posedge clk);
+        end
+    endtask
+
+    initial begin
+        #10000000 $fatal(1, "door_motor_engine_stop_tb timeout");
+    end
+
+    initial begin
+        // Scenario 1: X stopped while STEP is low. No new X rising edge; final STEP=0.
+        reset();
+        start_motor();
+        // Wait for the first X high pulse to complete and step to become low.
+        while (step[0] !== 1'b1) pulse_tick();
+        while (step[0] !== 1'b0) pulse_tick();
+        stop_mask = 2'b01;
+        edge_count = x_rises;
+        // Allow many ticks; X should not rise again.
+        repeat (30) pulse_tick();
+        if (x_rises !== edge_count)
+            $fatal(1, "scenario1: new X rising edge after stop low, x_rises=%0d expected %0d", x_rises, edge_count);
+        if (y_rises <= edge_count)
+            $fatal(1, "scenario1: Y did not continue");
+        wait_for_done();
+        if (step !== 2'b00)
+            $fatal(1, "scenario1: final step not zero");
+
+        // Scenario 2: X stopped while STEP is high. Current pulse must complete, no narrow pulse.
+        reset();
+        start_motor();
+        while (step[0] !== 1'b1) pulse_tick();
+        stop_mask = 2'b01;
+        edge_count = x_rises;
+        // Wait for the current high phase to finish and observe no new X rising edge.
+        while (step[0] !== 1'b0) pulse_tick();
+        if (x_rises !== edge_count + 1)
+            $fatal(1, "scenario2: X pulse not counted as completed");
+        edge_count = x_rises;
+        repeat (20) pulse_tick();
+        if (x_rises !== edge_count)
+            $fatal(1, "scenario2: new X rising edge after high-phase stop");
+
+        // Scenario 3: X/Y run; X stopped first, Y reaches 450.
+        reset();
+        start_motor();
+        stop_mask = 2'b01; // stop X immediately (during setup)
+        wait_for_done();
+        if (y_rises !== 450)
+            $fatal(1, "scenario3: Y did not reach 450, got %0d", y_rises);
+        if (x_rises !== 0)
+            $fatal(1, "scenario3: X should not step, got %0d", x_rises);
+
+        // Scenario 4: X and Y stopped at different times.
+        reset();
+        start_motor();
+        while (x_rises < 3) pulse_tick();
+        stop_mask = 2'b01; // stop X
+        edge_count = x_rises;
+        repeat (30) pulse_tick(); // Y continues
+        if (x_rises !== edge_count)
+            $fatal(1, "scenario4: X moved after stop");
+        if (y_rises <= edge_count)
+            $fatal(1, "scenario4: Y did not continue");
+        stop_mask = 2'b11; // stop Y too
+        wait_for_done();
+        if (x_rises !== edge_count)
+            $fatal(1, "scenario4: X moved after Y stop");
+
+        // Scenario 5: stop asserted during DIR setup. Zero steps on that axis.
+        reset();
+        start_motor();
+        stop_mask = 2'b01;
+        wait_for_done();
+        if (x_rises !== 0)
+            $fatal(1, "scenario5: X stepped during setup stop");
+        if (y_rises !== 450)
+            $fatal(1, "scenario5: Y did not reach 450");
+
+        // Scenario 6: stop at the 450th pulse boundary. No 451st rising edge.
+        reset();
+        start_motor();
+        // Run until 449 complete pulses on both axes (step low after 449th pulse).
+        while (x_rises < 449 || y_rises < 449) pulse_tick();
+        while (step[0] !== 1'b0 || step[1] !== 1'b0) pulse_tick();
+        // Stop X at the low boundary before the 450th high phase.
+        stop_mask = 2'b01;
+        wait_for_done();
+        if (x_rises > 449)
+            $fatal(1, "scenario6: X produced more than 449 rising edges");
+        if (y_rises !== 450)
+            $fatal(1, "scenario6: Y did not finish 450, got %0d", y_rises);
+
+        // Scenario 7: host mode (stop_mask=0). Original 450-step behavior unchanged.
+        reset();
+        start_motor();
+        stop_mask = 2'b00;
+        wait_for_done();
+        if (x_rises !== 450 || y_rises !== 450)
+            $fatal(1, "scenario7: host mode count mismatch x=%0d y=%0d", x_rises, y_rises);
+
+        $display("door_motor_engine_stop_tb PASS");
+        $finish;
+    end
+endmodule

+ 200 - 0
tests/sim/startup_motor_stop_tb.v

@@ -0,0 +1,200 @@
+`timescale 1ns/1ps
+
+module startup_motor_stop_tb;
+    reg clk = 1'b0;
+    reg rst_n = 1'b0;
+    reg tick_500us = 1'b0;
+    reg [1:0] limit = 2'b00;
+    reg limit_valid = 1'b0;
+
+    wire startup_done;
+    wire motor_start;
+    wire [1:0] motor_active_mask;
+    wire [1:0] motor_dir_mask;
+    wire [1:0] motor_stop_mask;
+    wire motor_busy;
+    wire motor_done;
+    wire [1:0] step;
+    wire [1:0] dir;
+
+    integer x_rises;
+    integer y_rises;
+    reg prev_x;
+    reg prev_y;
+    integer edge_count;
+    integer x_edge;
+    integer y_edge;
+
+    localparam integer FAST_TICK_DIV = 10;
+    reg [31:0] fast_tick_div;
+    reg fast_tick;
+
+    always #1 clk = ~clk;
+
+    initial begin
+        fast_tick = 1'b0;
+        fast_tick_div = 0;
+    end
+
+    always @(posedge clk) begin
+        if (fast_tick_div + 1 >= FAST_TICK_DIV) begin
+            fast_tick_div <= 0;
+            fast_tick <= 1'b1;
+        end else begin
+            fast_tick_div <= fast_tick_div + 1'b1;
+            fast_tick <= 1'b0;
+        end
+    end
+
+    always @(posedge clk) begin
+        tick_500us <= fast_tick;
+        prev_x <= step[0];
+        prev_y <= step[1];
+        if (!prev_x && step[0]) x_rises <= x_rises + 1;
+        if (!prev_y && step[1]) y_rises <= y_rises + 1;
+    end
+
+    startup_close_seek #(
+        .FIXED_STEPS(450), .DIR_SETUP_TICKS(2), .STEP_HALF_CYCLES(12500),
+        .MAX_BURSTS(10), .INTER_BURST_TICKS(4), .DIR_MASK(2'b11)
+    ) u_startup (
+        .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us),
+        .limit_sync_valid(limit_valid), .limit_sync(limit),
+        .startup_done(startup_done), .motor_start(motor_start),
+        .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
+        .motor_stop_mask(motor_stop_mask),
+        .motor_done(motor_done)
+    );
+
+    door_motor_engine #(
+        .FIXED_STEPS(450), .DIR_SETUP_TICKS(2)
+    ) u_motor (
+        .clk(clk), .rst_n(rst_n), .start(motor_start),
+        .active_mask(motor_active_mask), .dir_mask(motor_dir_mask),
+        .tick_500us(tick_500us), .abort(1'b0),
+        .stop_mask(motor_stop_mask),
+        .busy(motor_busy), .done(motor_done), .step(step), .dir(dir)
+    );
+
+    task pulse_tick;
+        begin
+            @(posedge clk);
+            while (!tick_500us) @(posedge clk);
+        end
+    endtask
+
+    task reset_dut;
+        begin
+            rst_n = 1'b0;
+            limit_valid = 1'b0;
+            x_rises = 0;
+            y_rises = 0;
+            prev_x = 1'b0;
+            prev_y = 1'b0;
+            edge_count = 0;
+            x_edge = 0;
+            y_edge = 0;
+            repeat (5) @(posedge clk);
+            rst_n = 1'b1;
+            // wait for limit synchronizer (simulated by testbench raising limit_valid)
+            repeat (5) @(posedge clk);
+            limit_valid = 1'b1;
+            @(posedge clk);
+        end
+    endtask
+
+    task wait_startup_done;
+        begin
+            while (!startup_done) pulse_tick();
+            @(posedge clk);
+        end
+    endtask
+
+    task wait_reached;
+        input [1:0] mask;
+        begin
+            while (!((u_startup.reached_mask & mask) == mask)) pulse_tick();
+            @(posedge clk);
+        end
+    endtask
+
+    initial begin
+        #40000000 $fatal(1, "startup_motor_stop_tb timeout");
+    end
+
+    initial begin
+        // Scenario 1: both limits high at reset.
+        limit = 2'b11;
+        reset_dut();
+        wait_startup_done();
+        if (x_rises !== 0 || y_rises !== 0)
+            $fatal(1, "scenario1: steps with both limits high x=%0d y=%0d", x_rises, y_rises);
+
+        // Scenario 2: X limit goes high after X has started stepping.
+        limit = 2'b00;
+        reset_dut();
+        while (step[0] !== 1'b1) pulse_tick();
+        limit = 2'b01;
+        wait_reached(2'b01);
+        edge_count = x_rises;
+        while (!startup_done) begin
+            pulse_tick();
+            if (x_rises > edge_count)
+                $fatal(1, "scenario2: X rose after reached, before=%0d after=%0d", edge_count, x_rises);
+        end
+        if (y_rises < 1)
+            $fatal(1, "scenario2: Y did not continue");
+
+        // Scenario 3: Y limit mid-burst, X runs to max bursts.
+        limit = 2'b00;
+        reset_dut();
+        while (step[1] !== 1'b1) pulse_tick();
+        limit = 2'b10;
+        wait_reached(2'b10);
+        edge_count = y_rises;
+        wait_startup_done();
+        if (y_rises > edge_count)
+            $fatal(1, "scenario3: Y rose after reached");
+        if (x_rises > 4500 + 10 || x_rises < 4500 - 10)
+            $fatal(1, "scenario3: X did not complete ~10 bursts: %0d", x_rises);
+
+        // Scenario 4: X and Y limits at different times.
+        limit = 2'b00;
+        reset_dut();
+        while (step[0] !== 1'b1) pulse_tick();
+        limit = 2'b01;
+        wait_reached(2'b01);
+        x_edge = x_rises;
+        repeat (10) pulse_tick();
+        limit = 2'b11;
+        wait_reached(2'b11);
+        y_edge = y_rises;
+        wait_startup_done();
+        if (x_rises > x_edge)
+            $fatal(1, "scenario4: X rose after both reached");
+        if (y_rises > y_edge)
+            $fatal(1, "scenario4: Y rose after both reached");
+
+        // Scenario 5: no limits, both reach max bursts.
+        limit = 2'b00;
+        reset_dut();
+        wait_startup_done();
+        if (x_rises !== 4500 || y_rises !== 4500)
+            $fatal(1, "scenario5: max bursts mismatch x=%0d y=%0d", x_rises, y_rises);
+
+        // Scenario 6: stop at 449th pulse boundary for X.
+        limit = 2'b00;
+        reset_dut();
+        while (x_rises < 448) pulse_tick();
+        while (step[0] !== 1'b0 || step[1] !== 1'b0) pulse_tick();
+        limit = 2'b01;
+        wait_startup_done();
+        if (x_rises > 449)
+            $fatal(1, "scenario6: X produced a 450th rising edge");
+        if (y_rises !== 4500)
+            $fatal(1, "scenario6: Y did not finish max bursts: %0d", y_rises);
+
+        $display("startup_motor_stop_tb PASS");
+        $finish;
+    end
+endmodule

+ 10 - 0
tests/unit/test_startup_close_seek.py

@@ -25,6 +25,7 @@ class StartupCloseSeekContractTests(unittest.TestCase):
         project = json.loads(PROJECT_PATH.read_text(encoding="utf-8"))
         limits = project["limits"]
         self.assertTrue(limits["functional_use"])
+        self.assertFalse(limits["runtime_use"])
         startup = limits["startup"]
         self.assertTrue(startup["enabled"])
         self.assertEqual(startup["direction_meaning"], "close")
@@ -33,6 +34,10 @@ class StartupCloseSeekContractTests(unittest.TestCase):
         self.assertEqual(startup["inter_burst_ms"], 50)
         self.assertEqual(startup["max_bursts"], 10)
         self.assertEqual(startup["axis_order"], ["x", "y"])
+        self.assertTrue(startup["limit_stop_during_burst"])
+        self.assertEqual(startup["stop_semantics"], "finish_current_high_pulse_then_no_next_rising_edge")
+        self.assertEqual(startup["additional_rising_edges_after_synchronized_limit"], 0)
+        self.assertFalse(startup["runtime_limit_use"])
 
     def test_generated_params_match_startup_config(self):
         project = json.loads(PROJECT_PATH.read_text(encoding="utf-8"))
@@ -60,6 +65,9 @@ class StartupCloseSeekContractTests(unittest.TestCase):
         self.assertIn("assign motor_start = startup_done ? host_motor_start : startup_motor_start", top)
         self.assertIn("assign motor_active_mask = startup_done ? host_motor_active_mask : startup_motor_active_mask", top)
         self.assertIn("assign motor_dir_mask = startup_done ? host_motor_dir_mask : startup_motor_dir_mask", top)
+        self.assertIn("assign motor_stop_mask = startup_done ? 2'b00 : startup_motor_stop_mask", top)
+        self.assertIn(".motor_stop_mask(startup_motor_stop_mask)", top)
+        self.assertIn(".stop_mask(motor_stop_mask)", top)
 
     def test_top_level_synchronizes_limit_inputs(self):
         top = TOP_PATH.read_text(encoding="utf-8")
@@ -80,6 +88,8 @@ class StartupCloseSeekContractTests(unittest.TestCase):
         self.assertIn("limit_sync_valid", source)
         self.assertIn("if (limit_sync_valid)", source)
         self.assertIn("reached_mask_next = reached_mask | (limit_sync_valid ? limit_sync : 2'b00)", source)
+        self.assertIn("motor_stop_mask", source)
+        self.assertIn("assign motor_stop_mask", source)
 
     def test_startup_module_has_expected_parameters(self):
         source = (ROOT / "src" / "modules" / "startup_close_seek.v").read_text(encoding="utf-8")

+ 2 - 0
tests/unit/test_startup_mutation.py

@@ -69,6 +69,7 @@ module tb;
     wire motor_start;
     wire [1:0] motor_active_mask;
     wire [1:0] motor_dir_mask;
+    wire [1:0] motor_stop_mask;
     reg motor_done = 1'b0;
 
     always #1 clk = ~clk;
@@ -85,6 +86,7 @@ module tb;
         .limit_sync_valid(limit_sync_valid), .limit_sync(limit),
         .startup_done(startup_done), .motor_start(motor_start),
         .motor_active_mask(motor_active_mask), .motor_dir_mask(motor_dir_mask),
+        .motor_stop_mask(motor_stop_mask),
         .motor_done(motor_done)
     );