`timescale 1ns/1ps // serial_calibration_tb: X→Y→Z; mutual exclusion; X fail → Y/Z continue; startup after profile. module door_serial_calibration_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'b000; // X starts seek-close (not already on close) reg [2:0] gated_step = 3'b000; reg [2:0] gated_step_rise = 3'b000; wire [2:0] step, dir_close; wire startup_complete; wire [2:0] calibrated_mask, fault_mask; wire [2:0] cal_en = dut.cal_enable; integer max_concurrent; always #1 clk = ~clk; door_motion_controller #( .MAX_CAL_STEPS(8), .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(1'b0), .percent_x(8'd0), .percent_y(8'd0), .percent_z(8'd0), .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() ); always @(posedge clk) if (rst_n) begin if (((dut.in_cal_x && dut.mov_x) + (dut.in_cal_y && dut.mov_y) + (dut.in_cal_z && dut.mov_z)) > 1) $fatal(1, "concurrent cal motion"); if ((cal_en[0]+cal_en[1]+cal_en[2]) > 1) $fatal(1, "multi enable %b", cal_en); end 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 < 20000; t = t + 1) begin if (step[ax] === want) return; @(negedge clk); end $fatal(1, "timeout step[%0d]==%0d state=%0d en=%b", ax, want, (ax==0)?dut.u_axis_x.status_state:(ax==1)?dut.u_axis_y.status_state:dut.u_axis_z.status_state, cal_en); end endtask task automatic cal_measure; input integer ax; input integer travel; integer r; begin close_limit[ax] = 1'b1; open_limit[ax] = 1'b0; repeat (6) 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; begin : w integer t; for (t = 0; t < 8000; t = t + 1) begin if (calibrated_mask[ax] || fault_mask[ax]) disable w; @(negedge clk); end end if (!calibrated_mask[ax]) $fatal(1, "cal fail ax=%0d", ax); end endtask initial begin #12_000_000 $fatal(1, "timeout"); end initial begin integer r, t; max_concurrent = 0; repeat (2) @(posedge clk); rst_n = 1'b1; @(negedge clk); limit_sync_valid = 1'b1; // X seek-close timeout (close never asserts) repeat (6) pulse_tick(); begin : xfail integer r, t; for (r = 0; r < 40 && !fault_mask[0]; r = r + 1) begin for (t = 0; t < 20000 && !step[0] && !fault_mask[0]; t = t + 1) @(negedge clk); if (fault_mask[0]) disable xfail; if (step[0]) begin force_rise(0); for (t = 0; t < 20000 && step[0] && !fault_mask[0]; t = t + 1) @(negedge clk); end else pulse_tick(); end end if (!fault_mask[0]) $fatal(1, "X not faulted"); begin : wait_y_en integer t; for (t = 0; t < 200; t = t + 1) begin if (cal_en[1]) disable wait_y_en; @(negedge clk); end $fatal(1, "Y not enabled after X fault en=%b term=%b/%b/%b", cal_en, dut.term_x, dut.term_y, dut.term_z); end cal_measure(1, 3); cal_measure(2, 5); for (t = 0; t < 8000 && !startup_complete; t = t + 1) @(negedge clk); if (!startup_complete) $fatal(1, "no startup_complete"); if (calibrated_mask !== 3'b110) $fatal(1, "mask=%b", calibrated_mask); if (dut.u_axis_y.status_travel !== 13'd3) $fatal(1, "Y travel"); if (dut.u_axis_z.status_travel !== 13'd5) $fatal(1, "Z travel"); $display("door_serial_calibration_tb PASS"); $finish; end endmodule