`timescale 1ns/1ps // Shared planner arbiter: each axis request granted once; X done must not // steal a second grant before Y. module door_planner_grant_once_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(1'b0), .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) ); integer starts_x, starts_y, starts_z; always @(posedge clk) begin if (rst_n && dut.grant_start) begin if (dut.grant_axis == 2'd0) starts_x = starts_x + 1; else if (dut.grant_axis == 2'd1) starts_y = starts_y + 1; else if (dut.grant_axis == 2'd2) starts_z = starts_z + 1; end end task pulse_tick; begin @(negedge clk); tick_500us = 1'b1; @(negedge clk); tick_500us = 1'b0; end endtask task force_rise_ax; 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'b000; gated_step = gated_step & ~m; end endtask task wait_step_ax; input integer ax; input want; integer t; begin for (t = 0; t < 20000; t = t + 1) begin if (step[ax] === want) disable wait_step_ax; @(negedge clk); end $fatal(1, "timeout step[%0d]==%0d", ax, want); end endtask task cal_axis; 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(ax, 1); force_rise_ax(ax); wait_step_ax(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]) disable w; @(negedge clk); end end if (!calibrated_mask[ax]) $fatal(1, "cal fail ax=%0d", ax); end endtask integer i; initial begin #5_000_000 $fatal(1, "door_planner_grant_once_tb timeout"); end initial begin starts_x = 0; starts_y = 0; starts_z = 0; open_limit = 3'b000; close_limit = 3'b000; repeat (2) @(posedge clk); rst_n = 1'b1; @(negedge clk); limit_sync_valid = 1'b1; cal_axis(0, 4); cal_axis(1, 4); cal_axis(2, 4); begin : ws integer t; for (t = 0; t < 8000 && !startup_complete; t = t + 1) @(negedge clk); end if (!startup_complete) $fatal(1, "no startup"); open_limit = 3'b000; starts_x = 0; starts_y = 0; starts_z = 0; @(negedge clk); percent_x = 8'd25; percent_y = 8'd50; percent_z = 8'd75; motor_request_valid = 1'b1; @(negedge clk); motor_request_valid = 1'b0; // Planner iterates up to travel*percent — with travel=4, tiny. for (i = 0; i < 80; i = i + 1) pulse_tick(); if (starts_x !== 1) $fatal(1, "X starts=%0d expect 1", starts_x); if (starts_y !== 1) $fatal(1, "Y starts=%0d expect 1", starts_y); if (starts_z !== 1) $fatal(1, "Z starts=%0d expect 1", starts_z); // Endpoint must not use planner starts_x = 0; starts_y = 0; starts_z = 0; @(negedge clk); percent_x = 8'd0; percent_y = 8'd100; percent_z = 8'd0; motor_request_valid = 1'b1; @(negedge clk); motor_request_valid = 1'b0; for (i = 0; i < 20; i = i + 1) pulse_tick(); if ((starts_x + starts_y + starts_z) !== 0) $fatal(1, "endpoint targets must not grant planner"); $display("door_planner_grant_once_tb PASS"); $finish; end endmodule