`timescale 1ns/1ps // Round-12: one engine_step high phase must produce at most one gated_step rise even if // selected_limit pulses high briefly (truncate then must not re-open until engine low). module prod2u_runtime_step_limit_gate_reopen_tb; reg clk = 1'b0; reg rst_n = 1'b0; reg engine_step = 1'b0; reg engine_dir = 1'b0; reg close_limit = 1'b0; reg open_limit = 1'b0; wire selected_limit; wire gated_step; reg gated_prev = 1'b0; integer rise_count; integer cycle; integer i; always #5 clk = ~clk; prod2u_runtime_step_limit_gate #(.AXIS_COUNT(1)) dut ( .clk(clk), .rst_n(rst_n), .engine_step(engine_step), .engine_dir(engine_dir), .close_limit(close_limit), .open_limit(open_limit), .selected_limit(selected_limit), .gated_step(gated_step) ); task sample_rise; begin if (gated_step && !gated_prev) rise_count = rise_count + 1; gated_prev = gated_step; end endtask task count_rises_during_engine_high; input integer limit_pulse_cycles; begin rise_count = 0; gated_prev = gated_step; engine_dir = 1'b1; close_limit = 1'b0; open_limit = 1'b0; engine_step = 1'b0; @(negedge clk); sample_rise(); engine_step = 1'b1; for (cycle = 0; cycle < 24; cycle = cycle + 1) begin if (cycle == 2) close_limit = 1'b1; else if (cycle == (2 + limit_pulse_cycles)) close_limit = 1'b0; @(negedge clk); sample_rise(); end if (rise_count !== 1) $fatal(1, "engine high + %0d-cycle limit pulse: %0d gated rises (want 1)", limit_pulse_cycles, rise_count); engine_step = 1'b0; for (i = 0; i < 4; i = i + 1) begin @(negedge clk); sample_rise(); end end endtask initial begin repeat (50_000) @(negedge clk); $fatal(1, "prod2u_runtime_step_limit_gate_reopen_tb timeout"); end initial begin rst_n = 1'b0; repeat (4) @(negedge clk); rst_n = 1'b1; repeat (2) @(negedge clk); count_rises_during_engine_high(1); count_rises_during_engine_high(2); rise_count = 0; gated_prev = gated_step; engine_dir = 1'b0; close_limit = 1'b0; open_limit = 1'b0; engine_step = 1'b1; for (cycle = 0; cycle < 20; cycle = cycle + 1) begin if (cycle == 1) open_limit = 1'b1; else if (cycle == 2) open_limit = 1'b0; @(negedge clk); sample_rise(); end if (rise_count !== 1) $fatal(1, "open-dir limit pulse: %0d rises (want 1)", rise_count); engine_step = 1'b0; repeat (4) @(negedge clk); rise_count = 0; gated_prev = gated_step; engine_step = 1'b1; @(negedge clk); sample_rise(); if (rise_count !== 1) $fatal(1, "expected fresh rise after engine went low, got %0d", rise_count); engine_step = 1'b0; $display("prod2u_runtime_step_limit_gate_reopen_tb PASS"); $finish; end endmodule