|
@@ -0,0 +1,707 @@
|
|
|
|
|
+`timescale 1ns/1ps
|
|
|
|
|
+
|
|
|
|
|
+// Round-11 P0/P1: same-percent heartbeat must not swallow planner_done / tick_500us;
|
|
|
|
|
+// materially different percent still latest-wins; planner+single-limit uses
|
|
|
|
|
+// effective endpoint coordinate for direction.
|
|
|
|
|
+module door_axis_request_planner_collision_tb;
|
|
|
|
|
+ reg clk = 1'b0;
|
|
|
|
|
+ always #1 clk = ~clk;
|
|
|
|
|
+
|
|
|
|
|
+ reg rst_n = 1'b0;
|
|
|
|
|
+ reg open_limit = 1'b0;
|
|
|
|
|
+ reg close_limit = 1'b1;
|
|
|
|
|
+ reg limit_sync_valid = 1'b0;
|
|
|
|
|
+ reg gated_step = 1'b0;
|
|
|
|
|
+ reg gated_step_rise = 1'b0;
|
|
|
|
|
+ reg tick_500us = 1'b0;
|
|
|
|
|
+ reg request_valid = 1'b0;
|
|
|
|
|
+ reg [7:0] requested_percent_in = 8'd0;
|
|
|
|
|
+ reg watchdog_stop = 1'b0;
|
|
|
|
|
+
|
|
|
|
|
+ wire planner_req;
|
|
|
|
|
+ wire [7:0] planner_percent;
|
|
|
|
|
+ wire [12:0] planner_travel;
|
|
|
|
|
+ reg planner_done = 1'b0;
|
|
|
|
|
+ reg [1:0] planner_done_axis = 2'd0;
|
|
|
|
|
+ reg [7:0] planner_done_percent = 8'd0;
|
|
|
|
|
+ reg [12:0] planner_result = 13'd0;
|
|
|
|
|
+
|
|
|
|
|
+ wire step;
|
|
|
|
|
+ wire dir_close;
|
|
|
|
|
+ wire [2:0] status_state;
|
|
|
|
|
+ wire [7:0] status_flags;
|
|
|
|
|
+ wire [7:0] status_requested_percent;
|
|
|
|
|
+ wire [12:0] status_current;
|
|
|
|
|
+ wire [12:0] status_target;
|
|
|
|
|
+ wire [12:0] status_travel;
|
|
|
|
|
+ wire [7:0] status_fault_code;
|
|
|
|
|
+ wire calibrated;
|
|
|
|
|
+ wire faulted;
|
|
|
|
|
+ wire terminal;
|
|
|
|
|
+
|
|
|
|
|
+ // Peer axis (Y) — ensure grant_hold / orphan on X does not block Y forever.
|
|
|
|
|
+ reg open_limit_y = 1'b0;
|
|
|
|
|
+ reg close_limit_y = 1'b1;
|
|
|
|
|
+ reg gated_step_y = 1'b0;
|
|
|
|
|
+ reg gated_step_rise_y = 1'b0;
|
|
|
|
|
+ reg request_valid_y = 1'b0;
|
|
|
|
|
+ reg [7:0] requested_percent_in_y = 8'd0;
|
|
|
|
|
+ wire planner_req_y;
|
|
|
|
|
+ wire [7:0] planner_percent_y;
|
|
|
|
|
+ wire [12:0] planner_travel_y;
|
|
|
|
|
+ reg planner_done_y = 1'b0;
|
|
|
|
|
+ reg [1:0] planner_done_axis_y = 2'd1;
|
|
|
|
|
+ reg [7:0] planner_done_percent_y = 8'd0;
|
|
|
|
|
+ reg [12:0] planner_result_y = 13'd0;
|
|
|
|
|
+ wire step_y;
|
|
|
|
|
+ wire dir_close_y;
|
|
|
|
|
+ wire [2:0] status_state_y;
|
|
|
|
|
+ wire [7:0] status_flags_y;
|
|
|
|
|
+ wire [7:0] status_requested_percent_y;
|
|
|
|
|
+ wire [12:0] status_current_y;
|
|
|
|
|
+ wire [12:0] status_target_y;
|
|
|
|
|
+ wire [12:0] status_travel_y;
|
|
|
|
|
+ wire [7:0] status_fault_code_y;
|
|
|
|
|
+ wire calibrated_y;
|
|
|
|
|
+ wire faulted_y;
|
|
|
|
|
+ wire terminal_y;
|
|
|
|
|
+
|
|
|
|
|
+ localparam [2:0] ST_IDLE = 3'd2;
|
|
|
|
|
+ localparam [2:0] ST_MOVE_OPEN = 3'd3;
|
|
|
|
|
+ localparam [2:0] ST_MOVE_CLOSE = 3'd4;
|
|
|
|
|
+ localparam [2:0] ST_WAIT_REPLAN = 3'd5;
|
|
|
|
|
+ localparam integer TRAVEL = 100;
|
|
|
|
|
+
|
|
|
|
|
+ // Manual planner: TB drives planner_done explicitly (no auto-ack).
|
|
|
|
|
+ reg auto_planner = 1'b0;
|
|
|
|
|
+
|
|
|
|
|
+ door_axis_controller #(
|
|
|
|
|
+ .AXIS_ID(0), .MAX_CAL_STEPS(256), .MIN_TRAVEL(2),
|
|
|
|
|
+ .ENDPOINT_GUARD_EXTRA(8), .DIR_SETUP_TICKS(1), .PERCENT_MAX(100)
|
|
|
|
|
+ ) dut0 (
|
|
|
|
|
+ .clk(clk), .rst_n(rst_n),
|
|
|
|
|
+ .open_limit(open_limit), .close_limit(close_limit),
|
|
|
|
|
+ .limit_sync_valid(limit_sync_valid),
|
|
|
|
|
+ .gated_step(gated_step), .gated_step_rise(gated_step_rise),
|
|
|
|
|
+ .tick_500us(tick_500us),
|
|
|
|
|
+ .request_valid(request_valid),
|
|
|
|
|
+ .requested_percent_in(requested_percent_in),
|
|
|
|
|
+ .watchdog_stop(watchdog_stop),
|
|
|
|
|
+ .planner_req(planner_req),
|
|
|
|
|
+ .planner_percent(planner_percent),
|
|
|
|
|
+ .planner_travel(planner_travel),
|
|
|
|
|
+ .planner_done(planner_done),
|
|
|
|
|
+ .planner_done_axis(planner_done_axis),
|
|
|
|
|
+ .planner_done_percent(planner_done_percent),
|
|
|
|
|
+ .planner_result(planner_result),
|
|
|
|
|
+ .step(step), .dir_close(dir_close),
|
|
|
|
|
+ .status_state(status_state), .status_flags(status_flags),
|
|
|
|
|
+ .status_requested_percent(status_requested_percent),
|
|
|
|
|
+ .status_current(status_current), .status_target(status_target),
|
|
|
|
|
+ .status_travel(status_travel), .status_fault_code(status_fault_code),
|
|
|
|
|
+ .calibrated(calibrated), .faulted(faulted), .terminal(terminal)
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ door_axis_controller #(
|
|
|
|
|
+ .AXIS_ID(1), .MAX_CAL_STEPS(256), .MIN_TRAVEL(2),
|
|
|
|
|
+ .ENDPOINT_GUARD_EXTRA(8), .DIR_SETUP_TICKS(1), .PERCENT_MAX(100)
|
|
|
|
|
+ ) dut1 (
|
|
|
|
|
+ .clk(clk), .rst_n(rst_n),
|
|
|
|
|
+ .open_limit(open_limit_y), .close_limit(close_limit_y),
|
|
|
|
|
+ .limit_sync_valid(limit_sync_valid),
|
|
|
|
|
+ .gated_step(gated_step_y), .gated_step_rise(gated_step_rise_y),
|
|
|
|
|
+ .tick_500us(tick_500us),
|
|
|
|
|
+ .request_valid(request_valid_y),
|
|
|
|
|
+ .requested_percent_in(requested_percent_in_y),
|
|
|
|
|
+ .watchdog_stop(1'b0),
|
|
|
|
|
+ .planner_req(planner_req_y),
|
|
|
|
|
+ .planner_percent(planner_percent_y),
|
|
|
|
|
+ .planner_travel(planner_travel_y),
|
|
|
|
|
+ .planner_done(planner_done_y),
|
|
|
|
|
+ .planner_done_axis(planner_done_axis_y),
|
|
|
|
|
+ .planner_done_percent(planner_done_percent_y),
|
|
|
|
|
+ .planner_result(planner_result_y),
|
|
|
|
|
+ .step(step_y), .dir_close(dir_close_y),
|
|
|
|
|
+ .status_state(status_state_y), .status_flags(status_flags_y),
|
|
|
|
|
+ .status_requested_percent(status_requested_percent_y),
|
|
|
|
|
+ .status_current(status_current_y), .status_target(status_target_y),
|
|
|
|
|
+ .status_travel(status_travel_y), .status_fault_code(status_fault_code_y),
|
|
|
|
|
+ .calibrated(calibrated_y), .faulted(faulted_y), .terminal(terminal_y)
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ function automatic [12:0] pct_to_steps;
|
|
|
|
|
+ input [7:0] pct;
|
|
|
|
|
+ input [12:0] travel;
|
|
|
|
|
+ reg [31:0] num;
|
|
|
|
|
+ begin
|
|
|
|
|
+ if (pct == 8'd0)
|
|
|
|
|
+ pct_to_steps = 13'd0;
|
|
|
|
|
+ else if (pct >= 8'd100)
|
|
|
|
|
+ pct_to_steps = travel;
|
|
|
|
|
+ else begin
|
|
|
|
|
+ num = (travel * pct) + 32'd50;
|
|
|
|
|
+ pct_to_steps = num / 32'd100;
|
|
|
|
|
+ if (pct_to_steps < 13'd1)
|
|
|
|
|
+ pct_to_steps = 13'd1;
|
|
|
|
|
+ if (pct_to_steps >= travel)
|
|
|
|
|
+ pct_to_steps = travel - 13'd1;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ endfunction
|
|
|
|
|
+
|
|
|
|
|
+ always @(posedge clk) begin
|
|
|
|
|
+ if (auto_planner) begin
|
|
|
|
|
+ planner_done <= 1'b0;
|
|
|
|
|
+ if (planner_req) begin
|
|
|
|
|
+ planner_done <= 1'b1;
|
|
|
|
|
+ planner_done_axis <= 2'd0;
|
|
|
|
|
+ planner_done_percent <= planner_percent;
|
|
|
|
|
+ planner_result <= pct_to_steps(planner_percent, planner_travel);
|
|
|
|
|
+ end
|
|
|
|
|
+ planner_done_y <= 1'b0;
|
|
|
|
|
+ if (planner_req_y) begin
|
|
|
|
|
+ planner_done_y <= 1'b1;
|
|
|
|
|
+ planner_done_axis_y <= 2'd1;
|
|
|
|
|
+ planner_done_percent_y <= planner_percent_y;
|
|
|
|
|
+ planner_result_y <= pct_to_steps(planner_percent_y, planner_travel_y);
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ task pulse_tick;
|
|
|
|
|
+ begin
|
|
|
|
|
+ @(negedge clk); tick_500us = 1'b1;
|
|
|
|
|
+ @(negedge clk); tick_500us = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task force_rise;
|
|
|
|
|
+ begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step = 1'b1;
|
|
|
|
|
+ gated_step_rise = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step_rise = 1'b0;
|
|
|
|
|
+ gated_step = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task force_rise_y;
|
|
|
|
|
+ begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step_y = 1'b1;
|
|
|
|
|
+ gated_step_rise_y = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step_rise_y = 1'b0;
|
|
|
|
|
+ gated_step_y = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task calibrate_axis0;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ open_limit = 1'b0;
|
|
|
|
|
+ close_limit = 1'b1;
|
|
|
|
|
+ limit_sync_valid = 1'b1;
|
|
|
|
|
+ repeat (4) pulse_tick();
|
|
|
|
|
+ for (i = 0; i < (TRAVEL - 1); i = i + 1) begin
|
|
|
|
|
+ force_rise();
|
|
|
|
|
+ pulse_tick();
|
|
|
|
|
+ end
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ open_limit = 1'b1;
|
|
|
|
|
+ close_limit = 1'b0;
|
|
|
|
|
+ gated_step = 1'b1;
|
|
|
|
|
+ gated_step_rise = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step = 1'b0;
|
|
|
|
|
+ gated_step_rise = 1'b0;
|
|
|
|
|
+ repeat (8) @(negedge clk);
|
|
|
|
|
+ if (!calibrated)
|
|
|
|
|
+ $fatal(1, "axis0 cal failed");
|
|
|
|
|
+ open_limit = 1'b0;
|
|
|
|
|
+ close_limit = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task calibrate_axis1;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ open_limit_y = 1'b0;
|
|
|
|
|
+ close_limit_y = 1'b1;
|
|
|
|
|
+ repeat (4) pulse_tick();
|
|
|
|
|
+ for (i = 0; i < (TRAVEL - 1); i = i + 1) begin
|
|
|
|
|
+ force_rise_y();
|
|
|
|
|
+ pulse_tick();
|
|
|
|
|
+ end
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ open_limit_y = 1'b1;
|
|
|
|
|
+ close_limit_y = 1'b0;
|
|
|
|
|
+ gated_step_y = 1'b1;
|
|
|
|
|
+ gated_step_rise_y = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step_y = 1'b0;
|
|
|
|
|
+ gated_step_rise_y = 1'b0;
|
|
|
|
|
+ repeat (8) @(negedge clk);
|
|
|
|
|
+ if (!calibrated_y)
|
|
|
|
|
+ $fatal(1, "axis1 cal failed");
|
|
|
|
|
+ open_limit_y = 1'b0;
|
|
|
|
|
+ close_limit_y = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ // Drive axis to a settled mid percent with auto planner.
|
|
|
|
|
+ task drive_to_percent;
|
|
|
|
|
+ input [7:0] pct;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ begin
|
|
|
|
|
+ auto_planner = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in = pct;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ for (i = 0; i < 600; i = i + 1) begin
|
|
|
|
|
+ pulse_tick();
|
|
|
|
|
+ if (step && !open_limit && !close_limit) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step = 1'b1;
|
|
|
|
|
+ gated_step_rise = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step_rise = 1'b0;
|
|
|
|
|
+ gated_step = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ if ((status_state == ST_IDLE) && !step && !planner_req &&
|
|
|
|
|
+ (status_flags[5] == 1'b1)) // target_valid bit
|
|
|
|
|
+ i = 600;
|
|
|
|
|
+ end
|
|
|
|
|
+ auto_planner = 1'b0;
|
|
|
|
|
+ planner_done = 1'b0;
|
|
|
|
|
+ if (status_current !== pct_to_steps(pct, status_travel))
|
|
|
|
|
+ $fatal(1, "drive_to %0d: current=%0d want=%0d",
|
|
|
|
|
+ pct, status_current, pct_to_steps(pct, status_travel));
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ // Enter WAIT_REPLAN with planner_req held (manual planner off).
|
|
|
|
|
+ task enter_wait_replan_mid;
|
|
|
|
|
+ input [7:0] pct;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ begin
|
|
|
|
|
+ auto_planner = 1'b0;
|
|
|
|
|
+ planner_done = 1'b0;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in = pct;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ // Drain / decide → planner_req
|
|
|
|
|
+ for (i = 0; i < 40; i = i + 1) begin
|
|
|
|
|
+ pulse_tick();
|
|
|
|
|
+ if (planner_req && (status_state == ST_WAIT_REPLAN))
|
|
|
|
|
+ i = 40;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (!planner_req || (status_state != ST_WAIT_REPLAN))
|
|
|
|
|
+ $fatal(1, "enter_wait_replan failed: state=%0d req=%0d",
|
|
|
|
|
+ status_state, planner_req);
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task collide_same_pct_planner_done;
|
|
|
|
|
+ input [7:0] pct;
|
|
|
|
|
+ input [12:0] result;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ begin
|
|
|
|
|
+ enter_wait_replan_mid(pct);
|
|
|
|
|
+ // Same cycle: matching planner_done + same-percent heartbeat
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ planner_done = 1'b1;
|
|
|
|
|
+ planner_done_axis = 2'd0;
|
|
|
|
|
+ planner_done_percent = pct;
|
|
|
|
|
+ planner_result = result;
|
|
|
|
|
+ requested_percent_in = pct;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ planner_done = 1'b0;
|
|
|
|
|
+
|
|
|
|
|
+ // Must consume result: clear planner_req within a few cycles
|
|
|
|
|
+ for (i = 0; i < 8; i = i + 1) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ if (!planner_req)
|
|
|
|
|
+ i = 8;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (planner_req)
|
|
|
|
|
+ $fatal(1, "same-pct+planner_done: planner_req stuck (WAIT_REPLAN deadlock)");
|
|
|
|
|
+ if (status_flags[5] !== 1'b1)
|
|
|
|
|
+ $fatal(1, "same-pct+planner_done: target_valid not set");
|
|
|
|
|
+ if (status_target !== result)
|
|
|
|
|
+ $fatal(1, "same-pct+planner_done: target=%0d want=%0d",
|
|
|
|
|
+ status_target, result);
|
|
|
|
|
+ if (status_state == ST_WAIT_REPLAN)
|
|
|
|
|
+ $fatal(1, "same-pct+planner_done: still WAIT_REPLAN");
|
|
|
|
|
+ if (!((status_state == ST_IDLE) ||
|
|
|
|
|
+ (status_state == ST_MOVE_OPEN) ||
|
|
|
|
|
+ (status_state == ST_MOVE_CLOSE)))
|
|
|
|
|
+ $fatal(1, "same-pct+planner_done: bad state=%0d", status_state);
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task collide_diff_pct_planner_done;
|
|
|
|
|
+ input [7:0] old_pct;
|
|
|
|
|
+ input [7:0] new_pct;
|
|
|
|
|
+ input [12:0] old_result;
|
|
|
|
|
+ input [12:0] new_result;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ begin
|
|
|
|
|
+ enter_wait_replan_mid(old_pct);
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ // Old matching done + new percent same cycle → discard old, replan
|
|
|
|
|
+ planner_done = 1'b1;
|
|
|
|
|
+ planner_done_axis = 2'd0;
|
|
|
|
|
+ planner_done_percent = old_pct;
|
|
|
|
|
+ planner_result = old_result;
|
|
|
|
|
+ requested_percent_in = new_pct;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ planner_done = 1'b0;
|
|
|
|
|
+
|
|
|
|
|
+ if (status_target === old_result && status_flags[5] === 1'b1 &&
|
|
|
|
|
+ status_requested_percent === old_pct)
|
|
|
|
|
+ $fatal(1, "diff-pct: accepted old planner result");
|
|
|
|
|
+
|
|
|
|
|
+ // Must re-request and accept new
|
|
|
|
|
+ for (i = 0; i < 40; i = i + 1) begin
|
|
|
|
|
+ pulse_tick();
|
|
|
|
|
+ if (planner_req)
|
|
|
|
|
+ i = 40;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (!planner_req)
|
|
|
|
|
+ $fatal(1, "diff-pct: no re-request for new percent");
|
|
|
|
|
+
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ planner_done = 1'b1;
|
|
|
|
|
+ planner_done_axis = 2'd0;
|
|
|
|
|
+ planner_done_percent = new_pct;
|
|
|
|
|
+ planner_result = new_result;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ planner_done = 1'b0;
|
|
|
|
|
+
|
|
|
|
|
+ for (i = 0; i < 8; i = i + 1) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ if (!planner_req)
|
|
|
|
|
+ i = 8;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (planner_req)
|
|
|
|
|
+ $fatal(1, "diff-pct: planner_req stuck after new accept");
|
|
|
|
|
+ if (status_target !== new_result)
|
|
|
|
|
+ $fatal(1, "diff-pct: target=%0d want=%0d", status_target, new_result);
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task collide_heartbeat_tick_low_to_high;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ reg saw_rise;
|
|
|
|
|
+ begin
|
|
|
|
|
+ // Settled at 50, then request 80 so we move with STEP cycling
|
|
|
|
|
+ drive_to_percent(8'd50);
|
|
|
|
|
+ auto_planner = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in = 8'd80;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ // Wait until MOVE PH_RUN with STEP low and allow_rise=1
|
|
|
|
|
+ for (i = 0; i < 120; i = i + 1) begin
|
|
|
|
|
+ pulse_tick();
|
|
|
|
|
+ if (step && !open_limit && !close_limit) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step = 1'b1;
|
|
|
|
|
+ gated_step_rise = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step_rise = 1'b0;
|
|
|
|
|
+ gated_step = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (((status_state == ST_MOVE_CLOSE) || (status_state == ST_MOVE_OPEN))
|
|
|
|
|
+ && !step && dut0.allow_rise && (dut0.phase == 3'd2))
|
|
|
|
|
+ i = 120;
|
|
|
|
|
+ end
|
|
|
|
|
+ auto_planner = 1'b0;
|
|
|
|
|
+ planner_done = 1'b0;
|
|
|
|
|
+ if (step)
|
|
|
|
|
+ $fatal(1, "tick L->H: precondition STEP low failed");
|
|
|
|
|
+ if (!dut0.allow_rise || (dut0.phase != 3'd2))
|
|
|
|
|
+ $fatal(1, "tick L->H: not in PH_RUN allow_rise (phase=%0d ar=%0d)",
|
|
|
|
|
+ dut0.phase, dut0.allow_rise);
|
|
|
|
|
+ if (!((status_state == ST_MOVE_CLOSE) || (status_state == ST_MOVE_OPEN)))
|
|
|
|
|
+ $fatal(1, "tick L->H: not in MOVE, state=%0d", status_state);
|
|
|
|
|
+
|
|
|
|
|
+ // Same cycle heartbeat + tick must produce STEP rise.
|
|
|
|
|
+ // Sample after NBA settles (next negedge).
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in = 8'd80;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ tick_500us = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ saw_rise = step;
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ tick_500us = 1'b0;
|
|
|
|
|
+ if (!saw_rise)
|
|
|
|
|
+ $fatal(1, "tick L->H: same-pct heartbeat swallowed STEP rise");
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task collide_heartbeat_tick_high_to_low;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ reg saw_fall;
|
|
|
|
|
+ begin
|
|
|
|
|
+ drive_to_percent(8'd50);
|
|
|
|
|
+ auto_planner = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in = 8'd80;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ for (i = 0; i < 120; i = i + 1) begin
|
|
|
|
|
+ pulse_tick();
|
|
|
|
|
+ if (step && !open_limit && !close_limit && dut0.allow_rise &&
|
|
|
|
|
+ (dut0.phase == 3'd2)) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step = step;
|
|
|
|
|
+ gated_step_rise = 1'b0;
|
|
|
|
|
+ i = 120;
|
|
|
|
|
+ end
|
|
|
|
|
+ end
|
|
|
|
|
+ auto_planner = 1'b0;
|
|
|
|
|
+ planner_done = 1'b0;
|
|
|
|
|
+ if (!step)
|
|
|
|
|
+ $fatal(1, "tick H->L: precondition STEP high failed");
|
|
|
|
|
+
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in = 8'd80;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ tick_500us = 1'b1;
|
|
|
|
|
+ gated_step = 1'b1;
|
|
|
|
|
+ gated_step_rise = 1'b0;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ saw_fall = ~step;
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ tick_500us = 1'b0;
|
|
|
|
|
+ gated_step = 1'b0;
|
|
|
|
|
+ if (!saw_fall)
|
|
|
|
|
+ $fatal(1, "tick H->L: same-pct heartbeat extended STEP high pulse");
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task collide_planner_done_close_limit;
|
|
|
|
|
+ // Old current mid; close_limit + matching planner mid result same cycle
|
|
|
|
|
+ // → direction must be OPEN (travel → mid), not CLOSE.
|
|
|
|
|
+ input [7:0] pct;
|
|
|
|
|
+ input [12:0] mid_result;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ begin
|
|
|
|
|
+ drive_to_percent(8'd70);
|
|
|
|
|
+ enter_wait_replan_mid(pct);
|
|
|
|
|
+ // Corrupt estimate already mid; assert close_limit + planner_done
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ close_limit = 1'b1;
|
|
|
|
|
+ open_limit = 1'b0;
|
|
|
|
|
+ planner_done = 1'b1;
|
|
|
|
|
+ planner_done_axis = 2'd0;
|
|
|
|
|
+ planner_done_percent = pct;
|
|
|
|
|
+ planner_result = mid_result;
|
|
|
|
|
+ // also same-pct heartbeat (triple collision)
|
|
|
|
|
+ requested_percent_in = pct;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ planner_done = 1'b0;
|
|
|
|
|
+
|
|
|
|
|
+ if (status_current !== status_travel)
|
|
|
|
|
+ $fatal(1, "limit+planner: snap failed current=%0d", status_current);
|
|
|
|
|
+ if (planner_req)
|
|
|
|
|
+ $fatal(1, "limit+planner: planner_req still high");
|
|
|
|
|
+ if (status_flags[5] !== 1'b1)
|
|
|
|
|
+ $fatal(1, "limit+planner: target_valid missing");
|
|
|
|
|
+ // Mid below travel → must choose OPEN
|
|
|
|
|
+ if (status_state == ST_MOVE_CLOSE)
|
|
|
|
|
+ $fatal(1, "limit+planner: wrong DIR CLOSE from close endpoint");
|
|
|
|
|
+ if (status_state == ST_MOVE_OPEN && dir_close)
|
|
|
|
|
+ $fatal(1, "limit+planner: dir_close set while MOVE_OPEN");
|
|
|
|
|
+ if (!((status_state == ST_IDLE) || (status_state == ST_MOVE_OPEN)))
|
|
|
|
|
+ $fatal(1, "limit+planner: unexpected state=%0d", status_state);
|
|
|
|
|
+
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ close_limit = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ task peer_survives_x_collision;
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ begin
|
|
|
|
|
+ auto_planner = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in_y = 8'd40;
|
|
|
|
|
+ request_valid_y = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid_y = 1'b0;
|
|
|
|
|
+ for (i = 0; i < 400; i = i + 1) begin
|
|
|
|
|
+ pulse_tick();
|
|
|
|
|
+ if (step_y && !open_limit_y && !close_limit_y) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step_y = 1'b1;
|
|
|
|
|
+ gated_step_rise_y = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step_rise_y = 1'b0;
|
|
|
|
|
+ gated_step_y = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ if ((status_state_y == ST_IDLE) && !step_y && !planner_req_y)
|
|
|
|
|
+ i = 400;
|
|
|
|
|
+ end
|
|
|
|
|
+ auto_planner = 1'b0;
|
|
|
|
|
+ if (status_current_y !== pct_to_steps(8'd40, status_travel_y))
|
|
|
|
|
+ $fatal(1, "peer Y failed after X collision: current=%0d",
|
|
|
|
|
+ status_current_y);
|
|
|
|
|
+ end
|
|
|
|
|
+ endtask
|
|
|
|
|
+
|
|
|
|
|
+ integer i;
|
|
|
|
|
+ initial begin
|
|
|
|
|
+ #3000000 $fatal(1, "door_axis_request_planner_collision_tb timeout");
|
|
|
|
|
+ end
|
|
|
|
|
+
|
|
|
|
|
+ initial begin
|
|
|
|
|
+ repeat (2) @(posedge clk);
|
|
|
|
|
+ rst_n = 1'b1;
|
|
|
|
|
+ calibrate_axis0();
|
|
|
|
|
+ calibrate_axis1();
|
|
|
|
|
+
|
|
|
|
|
+ // 1) mid planner_done + same-pct request (from open/0 after cal)
|
|
|
|
|
+ collide_same_pct_planner_done(8'd50, pct_to_steps(8'd50, TRAVEL));
|
|
|
|
|
+ $display("PASS: same-pct + planner_done");
|
|
|
|
|
+
|
|
|
|
|
+ // Settle then 2) different percent latest-wins:
|
|
|
|
|
+ // material replan to 30, then collide old-done(30) with new request 70.
|
|
|
|
|
+ drive_to_percent(8'd50);
|
|
|
|
|
+ collide_diff_pct_planner_done(
|
|
|
|
|
+ 8'd30, 8'd70,
|
|
|
|
|
+ pct_to_steps(8'd30, TRAVEL),
|
|
|
|
|
+ pct_to_steps(8'd70, TRAVEL)
|
|
|
|
|
+ );
|
|
|
|
|
+ $display("PASS: diff-pct + old planner_done latest-wins");
|
|
|
|
|
+
|
|
|
|
|
+ // 3) heartbeat + tick L→H
|
|
|
|
|
+ collide_heartbeat_tick_low_to_high();
|
|
|
|
|
+ $display("PASS: same-pct heartbeat + tick L→H");
|
|
|
|
|
+
|
|
|
|
|
+ // 4) heartbeat + tick H→L
|
|
|
|
|
+ collide_heartbeat_tick_high_to_low();
|
|
|
|
|
+ $display("PASS: same-pct heartbeat + tick H→L");
|
|
|
|
|
+
|
|
|
|
|
+ // 5) mid target complete + heartbeat: settle at target then heartbeat
|
|
|
|
|
+ drive_to_percent(8'd60);
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in = 8'd60;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ tick_500us = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ tick_500us = 1'b0;
|
|
|
|
|
+ if (status_state !== ST_IDLE)
|
|
|
|
|
+ $fatal(1, "mid complete+heartbeat left state=%0d", status_state);
|
|
|
|
|
+ $display("PASS: mid complete + heartbeat stays IDLE");
|
|
|
|
|
+
|
|
|
|
|
+ // 6) endpoint heartbeat must not reset guard (open endpoint motion)
|
|
|
|
|
+ auto_planner = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in = 8'd0;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ // Get into MOVE_OPEN endpoint, then heartbeat while STEP low
|
|
|
|
|
+ for (i = 0; i < 40; i = i + 1) begin
|
|
|
|
|
+ pulse_tick();
|
|
|
|
|
+ if ((status_state == ST_MOVE_OPEN) && !step)
|
|
|
|
|
+ i = 40;
|
|
|
|
|
+ end
|
|
|
|
|
+ auto_planner = 1'b0;
|
|
|
|
|
+ if (status_state !== ST_MOVE_OPEN)
|
|
|
|
|
+ $fatal(1, "endpoint setup failed state=%0d", status_state);
|
|
|
|
|
+ begin : guard_chk
|
|
|
|
|
+ reg [12:0] mc_before;
|
|
|
|
|
+ mc_before = dut0.motion_count;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in = 8'd0;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ tick_500us = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ tick_500us = 1'b0;
|
|
|
|
|
+ if (dut0.motion_count !== mc_before)
|
|
|
|
|
+ $fatal(1, "endpoint heartbeat reset guard/motion_count");
|
|
|
|
|
+ if (status_state !== ST_MOVE_OPEN)
|
|
|
|
|
+ $fatal(1, "endpoint heartbeat disrupted motion");
|
|
|
|
|
+ end
|
|
|
|
|
+ // Finish to open limit
|
|
|
|
|
+ auto_planner = 1'b1;
|
|
|
|
|
+ for (i = 0; i < 200; i = i + 1) begin
|
|
|
|
|
+ pulse_tick();
|
|
|
|
|
+ if (step && !open_limit && !close_limit) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step = 1'b1;
|
|
|
|
|
+ gated_step_rise = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ gated_step_rise = 1'b0;
|
|
|
|
|
+ gated_step = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (status_current < 13'd5) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ open_limit = 1'b1;
|
|
|
|
|
+ end
|
|
|
|
|
+ if ((status_state == ST_IDLE) && open_limit)
|
|
|
|
|
+ i = 200;
|
|
|
|
|
+ end
|
|
|
|
|
+ open_limit = 1'b0;
|
|
|
|
|
+ auto_planner = 1'b0;
|
|
|
|
|
+ $display("PASS: endpoint heartbeat keeps guard");
|
|
|
|
|
+
|
|
|
|
|
+ // 7) planner_done + close_limit + same-pct (effective coordinate)
|
|
|
|
|
+ collide_planner_done_close_limit(8'd40, pct_to_steps(8'd40, TRAVEL));
|
|
|
|
|
+ $display("PASS: planner_done + close_limit effective OPEN");
|
|
|
|
|
+
|
|
|
|
|
+ // 8) peer Y still serviced
|
|
|
|
|
+ peer_survives_x_collision();
|
|
|
|
|
+ $display("PASS: peer Y after X collision");
|
|
|
|
|
+
|
|
|
|
|
+ // Extra: multi-heartbeat around planner windows
|
|
|
|
|
+ enter_wait_replan_mid(8'd55);
|
|
|
|
|
+ for (i = 0; i < 5; i = i + 1) begin
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ requested_percent_in = 8'd55;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ end
|
|
|
|
|
+ if (!planner_req)
|
|
|
|
|
+ $fatal(1, "multi-heartbeat cleared planner_req before done");
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ planner_done = 1'b1;
|
|
|
|
|
+ planner_done_axis = 2'd0;
|
|
|
|
|
+ planner_done_percent = 8'd55;
|
|
|
|
|
+ planner_result = pct_to_steps(8'd55, TRAVEL);
|
|
|
|
|
+ requested_percent_in = 8'd55;
|
|
|
|
|
+ request_valid = 1'b1;
|
|
|
|
|
+ @(negedge clk);
|
|
|
|
|
+ request_valid = 1'b0;
|
|
|
|
|
+ planner_done = 1'b0;
|
|
|
|
|
+ if (planner_req)
|
|
|
|
|
+ $fatal(1, "multi-heartbeat window: stuck planner_req");
|
|
|
|
|
+ $display("PASS: multi-heartbeat around planner_done");
|
|
|
|
|
+
|
|
|
|
|
+ $display("door_axis_request_planner_collision_tb PASS");
|
|
|
|
|
+ $finish;
|
|
|
|
|
+ end
|
|
|
|
|
+endmodule
|