`timescale 1ns/1ps // Smoke: serial X→Y→Z calibration + profile ready → startup_complete; // then mid-percent request on X uses planner. Uses fast motion_service for sim. module door_motion_controller_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; reg [7:0] percent_y = 8'd0; reg [7:0] percent_z = 8'd0; reg watchdog_stop = 1'b0; wire [2:0] step; wire [2:0] dir_close; wire startup_complete; wire [2:0] calibrated_mask; wire [2:0] fault_mask; reg status_capture = 1'b0; reg [7:0] status_rd_addr = 8'd0; wire [7:0] status_rd_data; always #1 clk = ~clk; door_motion_controller #( .MAX_CAL_STEPS(32), .MIN_TRAVEL(2), .ENDPOINT_GUARD_EXTRA(8), .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(watchdog_stop), .watchdog_tripped(watchdog_stop), .step(step), .dir_close(dir_close), .startup_complete(startup_complete), .calibrated_mask(calibrated_mask), .fault_mask(fault_mask), .status_capture(status_capture), .status_rd_addr(status_rd_addr), .status_rd_data(status_rd_data) ); task pulse_tick; begin @(negedge clk); tick_500us = 1'b1; @(negedge clk); tick_500us = 1'b0; end endtask task pump_service; input integer n; integer k; begin for (k = 0; k < n; k = k + 1) @(negedge clk); end endtask task force_rise_axis; input integer ax; reg [2:0] mask; begin mask = 3'b001 << ax; @(negedge clk); gated_step = gated_step | mask; gated_step_rise = mask; @(negedge clk); gated_step_rise = 3'b000; gated_step = gated_step & ~mask; end endtask task calibrate_axis; input integer ax; integer r; begin // DIR setup via 500us ticks repeat (4) pulse_tick(); // Produce >=2 measure-open rises (already on close → measure-open) for (r = 0; r < 3; r = r + 1) begin // Wait until STEP high for this axis (DDA toggles) begin : wait_hi integer t; for (t = 0; t < 4000; t = t + 1) begin if (step[ax]) disable wait_hi; @(negedge clk); end $fatal(1, "timeout waiting step high axis=%0d", ax); end force_rise_axis(ax); // Wait STEP low then continue begin : wait_lo integer t; for (t = 0; t < 4000; t = t + 1) begin if (!step[ax]) disable wait_lo; @(negedge clk); end end end // Hit open on a rise begin : wait_hi2 integer t; for (t = 0; t < 4000; t = t + 1) begin if (step[ax]) disable wait_hi2; @(negedge clk); end $fatal(1, "timeout waiting final step high axis=%0d", ax); end @(negedge clk); open_limit[ax] = 1'b1; close_limit[ax] = 1'b0; gated_step[ax] = 1'b1; gated_step_rise[ax] = 1'b1; @(negedge clk); gated_step[ax] = 1'b0; gated_step_rise[ax] = 1'b0; repeat (8) @(negedge clk); end endtask integer i; initial begin #5_000_000 $fatal(1, "door_motion_controller_tb timeout"); end initial begin repeat (2) @(posedge clk); rst_n = 1'b1; @(negedge clk); limit_sync_valid = 1'b1; // Serial X → Y → Z calibrate_axis(0); calibrate_axis(1); calibrate_axis(2); // Allow profile builder to finish pump_service(64); if (!startup_complete) $fatal(1, "startup_complete not set after serial cal+profile"); if (calibrated_mask !== 3'b111) $fatal(1, "calibrated_mask=%b", calibrated_mask); if (fault_mask !== 3'b000) $fatal(1, "unexpected fault_mask=%b", fault_mask); @(negedge clk); status_capture = 1'b1; @(negedge clk); status_capture = 1'b0; status_rd_addr = 8'd0; @(negedge clk); if (status_rd_data !== 8'h47) $fatal(1, "status byte0=%h expect 47", status_rd_data); // Mid request 50% on X open_limit = 3'b000; @(negedge clk); percent_x = 8'd50; percent_y = 8'd0; percent_z = 8'd0; motor_request_valid = 1'b1; @(negedge clk); motor_request_valid = 1'b0; for (i = 0; i < 2000; i = i + 1) begin pulse_tick(); if (step[0]) begin @(negedge clk); gated_step[0] = 1'b1; gated_step_rise[0] = 1'b1; @(negedge clk); gated_step[0] = 1'b0; gated_step_rise[0] = 1'b0; end @(negedge clk); end if (dir_close[0] !== 1'b1) $fatal(1, "X mid 50%% from open should move close"); $display("door_motion_controller_tb PASS"); $finish; end endmodule