`timescale 1ns/1ps // Scheme A (R12 per-axis kernels): mid-percent overlap with >=2 gated rises/axis. module door_motion_three_axis_overlap_tb; localparam integer CAL_TRAVEL = 32; localparam integer MID_PCT = 50; reg clk = 1'b0; reg rst_n = 1'b0; reg tick_500us = 1'b0; reg limit_sync_valid = 1'b0; reg [2:0] open_limit = 3'b000; reg [2:0] close_limit = 3'b111; reg [2:0] gated_step = 3'b000; reg [2:0] gated_step_rise = 3'b000; reg motor_request_valid = 1'b0; reg [7:0] percent_x = 8'd0, percent_y = 8'd0, percent_z = 8'd0; wire [2:0] step, dir_close; wire startup_complete; wire [2:0] calibrated_mask, fault_mask; wire x_moving = dut.u_axis_x.moving; wire y_moving = dut.u_axis_y.moving; wire z_moving = dut.u_axis_z.moving; integer overlap_cycles, overlap_rise_x, overlap_rise_y, overlap_rise_z; integer sustained_overlap, overlap_done, ax, poll, k; always #1 clk = ~clk; door_motion_controller #( .MAX_CAL_STEPS(64), .MIN_TRAVEL(2), .ENDPOINT_GUARD_EXTRA(8), .DIR_SETUP_TICKS(1), .PERCENT_MAX(100) ) dut ( .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us), .limit_sync_valid(limit_sync_valid), .open_limit(open_limit), .close_limit(close_limit), .gated_step(gated_step), .gated_step_rise(gated_step_rise), .motor_request_valid(motor_request_valid), .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z), .watchdog_stop(1'b0), .watchdog_tripped(1'b0), .step(step), .dir_close(dir_close), .startup_complete(startup_complete), .calibrated_mask(calibrated_mask), .fault_mask(fault_mask), .status_capture(1'b0), .status_rd_addr(8'd0), .status_rd_data() ); task pulse_tick; begin @(negedge clk); tick_500us = 1'b1; @(negedge clk); tick_500us = 1'b0; end endtask reg [2:0] step_d; always @(posedge clk or negedge rst_n) begin if (!rst_n) step_d <= 3'b0; else step_d <= step; end wire [2:0] step_rise = step & ~step_d; initial begin #8000000 $fatal(1, "timeout"); end initial begin overlap_cycles = 0; overlap_rise_x = 0; overlap_rise_y = 0; overlap_rise_z = 0; sustained_overlap = 0; overlap_done = 0; repeat (2) @(posedge clk); rst_n = 1'b1; @(negedge clk); limit_sync_valid = 1'b1; // Concurrent cal: start with close_limit high → measure-open; count travel; hit open. repeat (8) pulse_tick(); for (k = 0; k < CAL_TRAVEL; k = k + 1) begin @(negedge clk); gated_step = 3'b111; gated_step_rise = 3'b111; @(negedge clk); gated_step = 3'b000; gated_step_rise = 3'b000; repeat (2) pulse_tick(); end @(negedge clk); open_limit = 3'b111; close_limit = 3'b000; @(negedge clk); open_limit = 3'b000; close_limit = 3'b000; repeat (40) pulse_tick(); if (!startup_complete) $fatal(1, "no startup_complete cal=%b fault=%b", calibrated_mask, fault_mask); if (calibrated_mask !== 3'b111) $fatal(1, "cal mask %b fault %b", calibrated_mask, fault_mask); percent_x = MID_PCT; percent_y = MID_PCT; percent_z = MID_PCT; @(negedge clk); motor_request_valid = 1'b1; @(negedge clk); motor_request_valid = 1'b0; for (poll = 0; poll < 200000 && !overlap_done; poll = poll + 1) begin @(posedge clk); tick_500us = (poll % 4) == 0; if (x_moving && y_moving && z_moving) begin overlap_cycles = overlap_cycles + 1; if (overlap_cycles >= 8) sustained_overlap = 1; end for (ax = 0; ax < 3; ax = ax + 1) begin if (step_rise[ax]) begin gated_step[ax] = 1'b1; gated_step_rise[ax] = 1'b1; if (x_moving && y_moving && z_moving) begin if (ax == 0) overlap_rise_x = overlap_rise_x + 1; else if (ax == 1) overlap_rise_y = overlap_rise_y + 1; else overlap_rise_z = overlap_rise_z + 1; end end else begin gated_step[ax] = 1'b0; gated_step_rise[ax] = 1'b0; end end if (sustained_overlap && overlap_rise_x >= 2 && overlap_rise_y >= 2 && overlap_rise_z >= 2) overlap_done = 1; end if (!sustained_overlap) $fatal(1, "no overlap mov=%b%b%b", x_moving, y_moving, z_moving); if (overlap_rise_x < 2 || overlap_rise_y < 2 || overlap_rise_z < 2) $fatal(1, "rises %0d/%0d/%0d", overlap_rise_x, overlap_rise_y, overlap_rise_z); $display("door_motion_three_axis_overlap_tb PASS schemeA overlap_cycles=%0d rises=%0d/%0d/%0d", overlap_cycles, overlap_rise_x, overlap_rise_y, overlap_rise_z); $finish; end endmodule