`timescale 1ns/1ps // axis_independence_tb: X both-limits fault must not stall Y/Z motion. module door_axis_independence_tb; 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; always #1 clk = ~clk; door_motion_controller #( .MAX_CAL_STEPS(16), .MIN_TRAVEL(2), .ENDPOINT_GUARD_EXTRA(4), .DIR_SETUP_TICKS(1), .PERCENT_MAX(100), .MOTION_SERVICE_CLKS(4), .PHASE_FACTOR(64), .CAL_RAMP_UNIT(2) ) 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 automatic pulse_tick; begin @(negedge clk); tick_500us = 1'b1; @(negedge clk); tick_500us = 1'b0; end endtask task automatic force_rise; input integer ax; reg [2:0] m; begin m = 3'b001 << ax; @(negedge clk); gated_step = gated_step | m; gated_step_rise = m; @(negedge clk); gated_step_rise = 3'b0; gated_step = gated_step & ~m; end endtask task automatic wait_step; input integer ax; input want; integer t; begin for (t = 0; t < 8000; t = t + 1) begin if (step[ax] === want) return; @(negedge clk); end $fatal(1, "timeout step[%0d]", ax); end endtask task automatic cal_ok; input integer ax; input integer travel; integer r; integer t; begin close_limit[ax] = 1'b1; open_limit[ax] = 1'b0; repeat (4) pulse_tick(); for (r = 0; r < travel; r = r + 1) begin wait_step(ax, 1); force_rise(ax); wait_step(ax, 0); end @(negedge clk); open_limit[ax] = 1'b1; close_limit[ax] = 1'b0; for (t = 0; t < 5000; t = t + 1) begin if (calibrated_mask[ax]) return; @(negedge clk); end $fatal(1, "cal %0d", ax); end endtask initial begin #12_000_000 $fatal(1, "timeout"); end initial begin integer t, rises_y, rises_z, started; repeat (2) @(posedge clk); rst_n = 1'b1; @(negedge clk); limit_sync_valid = 1'b1; cal_ok(0, 3); cal_ok(1, 4); cal_ok(2, 5); for (t = 0; t < 5000 && !startup_complete; t = t + 1) @(negedge clk); if (!startup_complete) $fatal(1, "no startup"); open_limit = 3'b0; close_limit = 3'b0; @(negedge clk); percent_x = 8'd50; percent_y = 8'd80; percent_z = 8'd80; motor_request_valid = 1'b1; @(negedge clk); motor_request_valid = 1'b0; started = 0; for (t = 0; t < 8000; t = t + 1) begin pulse_tick(); if (step[1]) force_rise(1); if (step[2]) force_rise(2); if (dut.mov_y && dut.mov_z) begin started = 1; @(negedge clk); open_limit[0] = 1'b1; close_limit[0] = 1'b1; t = 8000; end @(negedge clk); end if (!started) $fatal(1, "Y/Z never both moving"); for (t = 0; t < 2000 && !fault_mask[0]; t = t + 1) @(negedge clk); if (!fault_mask[0]) $fatal(1, "X not faulted"); rises_y = 0; rises_z = 0; for (t = 0; t < 30000; t = t + 1) begin pulse_tick(); if (step[1]) begin force_rise(1); rises_y = rises_y + 1; end if (step[2]) begin force_rise(2); rises_z = rises_z + 1; end if (rises_y >= 2 && rises_z >= 2) begin $display("door_axis_independence_tb PASS y=%0d z=%0d", rises_y, rises_z); $finish; end @(negedge clk); end $fatal(1, "Y/Z stalled y=%0d z=%0d", rises_y, rises_z); end endmodule