|
|
@@ -0,0 +1,463 @@
|
|
|
+`timescale 1ns/1ps
|
|
|
+
|
|
|
+// Round-12: source-endpoint limit lingering must NOT overwrite gated_step_rise ±1.
|
|
|
+// On broken RTL, continuous close_limit during MOVE_OPEN snaps position back each cycle.
|
|
|
+module door_axis_source_limit_linger_tb;
|
|
|
+ reg clk = 1'b0;
|
|
|
+ always #1 clk = ~clk;
|
|
|
+
|
|
|
+ reg rst_n = 1'b0;
|
|
|
+ reg open_limit = 1'b0;
|
|
|
+ reg close_limit = 1'b0;
|
|
|
+ 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;
|
|
|
+
|
|
|
+ localparam [2:0] ST_IDLE = 3'd2;
|
|
|
+ localparam [2:0] ST_MOVE_OPEN = 3'd3;
|
|
|
+ localparam [2:0] ST_MOVE_CLOSE = 3'd4;
|
|
|
+ localparam integer TRAVEL = 100;
|
|
|
+
|
|
|
+ reg auto_planner = 1'b0;
|
|
|
+ reg planner_req_q = 1'b0;
|
|
|
+ integer gated_rise_total;
|
|
|
+ integer i;
|
|
|
+
|
|
|
+ door_axis_controller #(
|
|
|
+ .AXIS_ID(0), .MAX_CAL_STEPS(256), .MIN_TRAVEL(2),
|
|
|
+ .ENDPOINT_GUARD_EXTRA(8), .DIR_SETUP_TICKS(1), .PERCENT_MAX(100)
|
|
|
+ ) dut (
|
|
|
+ .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()
|
|
|
+ );
|
|
|
+
|
|
|
+ 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_q) 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
|
|
|
+ end
|
|
|
+ planner_req_q <= planner_req;
|
|
|
+ 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 calibrate_axis;
|
|
|
+ integer k;
|
|
|
+ begin
|
|
|
+ open_limit = 1'b0;
|
|
|
+ close_limit = 1'b1;
|
|
|
+ limit_sync_valid = 1'b1;
|
|
|
+ repeat (4) pulse_tick();
|
|
|
+ for (k = 0; k < (TRAVEL - 1); k = k + 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, "calibrate_axis failed");
|
|
|
+ open_limit = 1'b0;
|
|
|
+ close_limit = 1'b0;
|
|
|
+ end
|
|
|
+ endtask
|
|
|
+
|
|
|
+ task setup_at_close_endpoint;
|
|
|
+ integer k;
|
|
|
+ begin
|
|
|
+ auto_planner = 1'b1;
|
|
|
+ open_limit = 1'b0;
|
|
|
+ close_limit = 1'b0;
|
|
|
+ requested_percent_in = 8'd100;
|
|
|
+ request_valid = 1'b1;
|
|
|
+ @(negedge clk);
|
|
|
+ request_valid = 1'b0;
|
|
|
+ if (status_requested_percent !== 8'd100)
|
|
|
+ $fatal(1, "setup: requested=%0d want 100", status_requested_percent);
|
|
|
+ for (k = 0; k < 1400; k = k + 1) begin
|
|
|
+ pulse_tick();
|
|
|
+ if (step) 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 >= (TRAVEL - 13'd3))
|
|
|
+ close_limit = 1'b1;
|
|
|
+ if ((status_state == ST_IDLE) && close_limit && (status_current == TRAVEL))
|
|
|
+ k = 1400;
|
|
|
+ if (faulted)
|
|
|
+ $fatal(1, "setup_at_close fault fc=%0d cur=%0d", status_fault_code, status_current);
|
|
|
+ end
|
|
|
+ auto_planner = 1'b0;
|
|
|
+ planner_done = 1'b0;
|
|
|
+ if ((status_current !== TRAVEL) || (status_requested_percent !== 8'd100))
|
|
|
+ $fatal(1, "setup_at_close: cur=%0d req=%0d", status_current, status_requested_percent);
|
|
|
+ close_limit = 1'b1;
|
|
|
+ open_limit = 1'b0;
|
|
|
+ end
|
|
|
+ endtask
|
|
|
+
|
|
|
+ task depart_close_with_linger;
|
|
|
+ input integer linger_rises;
|
|
|
+ input [7:0] pct;
|
|
|
+ reg [12:0] target;
|
|
|
+ reg [12:0] expect_pos;
|
|
|
+ integer rises;
|
|
|
+ begin
|
|
|
+ target = pct_to_steps(pct, TRAVEL);
|
|
|
+ close_limit = 1'b0;
|
|
|
+ auto_planner = 1'b0;
|
|
|
+ planner_done = 1'b0;
|
|
|
+ requested_percent_in = pct;
|
|
|
+ request_valid = 1'b1;
|
|
|
+ @(negedge clk);
|
|
|
+ request_valid = 1'b0;
|
|
|
+ for (i = 0; i < 80; i = i + 1) begin
|
|
|
+ pulse_tick();
|
|
|
+ if (planner_req)
|
|
|
+ i = 80;
|
|
|
+ end
|
|
|
+ if (!planner_req)
|
|
|
+ $fatal(1, "depart: planner_req never asserted");
|
|
|
+ @(negedge clk);
|
|
|
+ planner_done = 1'b1;
|
|
|
+ planner_done_axis = 2'd0;
|
|
|
+ planner_done_percent = pct;
|
|
|
+ planner_result = target;
|
|
|
+ @(negedge clk);
|
|
|
+ planner_done = 1'b0;
|
|
|
+ for (i = 0; i < 80; i = i + 1) begin
|
|
|
+ pulse_tick();
|
|
|
+ if (status_state == ST_MOVE_OPEN && !status_flags[4]) begin
|
|
|
+ close_limit = 1'b1;
|
|
|
+ i = 80;
|
|
|
+ end
|
|
|
+ if (faulted)
|
|
|
+ $fatal(1, "depart: fault fc=%0d st=%0d ep=%0d",
|
|
|
+ status_fault_code, status_state, status_flags[4]);
|
|
|
+ end
|
|
|
+ if (status_state !== ST_MOVE_OPEN || status_flags[4])
|
|
|
+ $fatal(1, "depart: want mid MOVE_OPEN ep=0 got st=%0d ep=%0d",
|
|
|
+ status_state, status_flags[4]);
|
|
|
+
|
|
|
+ rises = 0;
|
|
|
+ expect_pos = TRAVEL;
|
|
|
+ gated_rise_total = 0;
|
|
|
+ while (rises < linger_rises) begin
|
|
|
+ pulse_tick();
|
|
|
+ if (step) begin
|
|
|
+ @(negedge clk);
|
|
|
+ gated_step = 1'b1;
|
|
|
+ gated_step_rise = 1'b1;
|
|
|
+ @(negedge clk);
|
|
|
+ gated_step_rise = 1'b0;
|
|
|
+ gated_step = 1'b0;
|
|
|
+ rises = rises + 1;
|
|
|
+ gated_rise_total = gated_rise_total + 1;
|
|
|
+ expect_pos = expect_pos - 13'd1;
|
|
|
+ if (status_current !== expect_pos)
|
|
|
+ $fatal(1,
|
|
|
+ "close linger %0d rise %0d: pos=%0d want=%0d (snap overwrote step)",
|
|
|
+ linger_rises, rises, status_current, expect_pos);
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ close_limit = 1'b0;
|
|
|
+ for (i = 0; i < 900; 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;
|
|
|
+ gated_rise_total = gated_rise_total + 1;
|
|
|
+ end
|
|
|
+ if ((status_state == ST_IDLE) && (status_current == target))
|
|
|
+ i = 900;
|
|
|
+ end
|
|
|
+ auto_planner = 1'b0;
|
|
|
+ planner_done = 1'b0;
|
|
|
+ if (status_current !== target)
|
|
|
+ $fatal(1, "close linger %0d: final pos=%0d target=%0d rises=%0d",
|
|
|
+ linger_rises, status_current, target, gated_rise_total);
|
|
|
+ if (gated_rise_total !== (TRAVEL - target))
|
|
|
+ $fatal(1, "close linger %0d: total rises=%0d want=%0d",
|
|
|
+ linger_rises, gated_rise_total, TRAVEL - target);
|
|
|
+ end
|
|
|
+ endtask
|
|
|
+
|
|
|
+ task setup_at_open_endpoint;
|
|
|
+ integer k;
|
|
|
+ begin
|
|
|
+ auto_planner = 1'b1;
|
|
|
+ open_limit = 1'b0;
|
|
|
+ close_limit = 1'b0;
|
|
|
+ requested_percent_in = 8'd0;
|
|
|
+ request_valid = 1'b1;
|
|
|
+ @(negedge clk);
|
|
|
+ request_valid = 1'b0;
|
|
|
+ for (k = 0; k < 1200; k = k + 1) begin
|
|
|
+ pulse_tick();
|
|
|
+ if (step) 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'd3)
|
|
|
+ open_limit = 1'b1;
|
|
|
+ if ((status_state == ST_IDLE) && open_limit && (status_current == 13'd0))
|
|
|
+ k = 1200;
|
|
|
+ if (faulted)
|
|
|
+ $fatal(1, "setup_at_open fault fc=%0d cur=%0d", status_fault_code, status_current);
|
|
|
+ end
|
|
|
+ auto_planner = 1'b0;
|
|
|
+ planner_done = 1'b0;
|
|
|
+ if (status_current !== 13'd0)
|
|
|
+ $fatal(1, "setup_at_open: cur=%0d", status_current);
|
|
|
+ open_limit = 1'b1;
|
|
|
+ close_limit = 1'b0;
|
|
|
+ end
|
|
|
+ endtask
|
|
|
+
|
|
|
+ task depart_open_with_linger;
|
|
|
+ input integer linger_rises;
|
|
|
+ input [7:0] pct;
|
|
|
+ reg [12:0] target;
|
|
|
+ reg [12:0] expect_pos;
|
|
|
+ integer rises;
|
|
|
+ begin
|
|
|
+ target = pct_to_steps(pct, TRAVEL);
|
|
|
+ open_limit = 1'b0;
|
|
|
+ auto_planner = 1'b0;
|
|
|
+ planner_done = 1'b0;
|
|
|
+ requested_percent_in = pct;
|
|
|
+ request_valid = 1'b1;
|
|
|
+ @(negedge clk);
|
|
|
+ request_valid = 1'b0;
|
|
|
+ for (i = 0; i < 80; i = i + 1) begin
|
|
|
+ pulse_tick();
|
|
|
+ if (planner_req)
|
|
|
+ i = 80;
|
|
|
+ end
|
|
|
+ if (!planner_req)
|
|
|
+ $fatal(1, "open depart: planner_req never asserted");
|
|
|
+ @(negedge clk);
|
|
|
+ planner_done = 1'b1;
|
|
|
+ planner_done_axis = 2'd0;
|
|
|
+ planner_done_percent = pct;
|
|
|
+ planner_result = target;
|
|
|
+ @(negedge clk);
|
|
|
+ planner_done = 1'b0;
|
|
|
+ for (i = 0; i < 80; i = i + 1) begin
|
|
|
+ pulse_tick();
|
|
|
+ if (status_state == ST_MOVE_CLOSE && !status_flags[4]) begin
|
|
|
+ open_limit = 1'b1;
|
|
|
+ i = 80;
|
|
|
+ end
|
|
|
+ if (faulted)
|
|
|
+ $fatal(1, "open depart fault fc=%0d", status_fault_code);
|
|
|
+ end
|
|
|
+ if (status_state !== ST_MOVE_CLOSE || status_flags[4])
|
|
|
+ $fatal(1, "open depart: want mid MOVE_CLOSE got st=%0d ep=%0d",
|
|
|
+ status_state, status_flags[4]);
|
|
|
+
|
|
|
+ rises = 0;
|
|
|
+ expect_pos = 13'd0;
|
|
|
+ gated_rise_total = 0;
|
|
|
+ while (rises < linger_rises) begin
|
|
|
+ pulse_tick();
|
|
|
+ if (step) begin
|
|
|
+ @(negedge clk);
|
|
|
+ gated_step = 1'b1;
|
|
|
+ gated_step_rise = 1'b1;
|
|
|
+ @(negedge clk);
|
|
|
+ gated_step_rise = 1'b0;
|
|
|
+ gated_step = 1'b0;
|
|
|
+ rises = rises + 1;
|
|
|
+ gated_rise_total = gated_rise_total + 1;
|
|
|
+ expect_pos = expect_pos + 13'd1;
|
|
|
+ if (status_current !== expect_pos)
|
|
|
+ $fatal(1,
|
|
|
+ "open linger %0d rise %0d: pos=%0d want=%0d (snap overwrote step)",
|
|
|
+ linger_rises, rises, status_current, expect_pos);
|
|
|
+ end
|
|
|
+ end
|
|
|
+
|
|
|
+ open_limit = 1'b0;
|
|
|
+ for (i = 0; i < 900; 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;
|
|
|
+ gated_rise_total = gated_rise_total + 1;
|
|
|
+ end
|
|
|
+ if ((status_state == ST_IDLE) && (status_current == target))
|
|
|
+ i = 900;
|
|
|
+ end
|
|
|
+ auto_planner = 1'b0;
|
|
|
+ planner_done = 1'b0;
|
|
|
+ if (status_current !== target)
|
|
|
+ $fatal(1, "open linger %0d: final pos=%0d target=%0d",
|
|
|
+ linger_rises, status_current, target);
|
|
|
+ if (gated_rise_total !== target)
|
|
|
+ $fatal(1, "open linger %0d: total rises=%0d want=%0d",
|
|
|
+ linger_rises, gated_rise_total, target);
|
|
|
+ end
|
|
|
+ endtask
|
|
|
+
|
|
|
+ initial begin
|
|
|
+ repeat (200_000) @(negedge clk);
|
|
|
+ $fatal(1, "door_axis_source_limit_linger_tb timeout");
|
|
|
+ end
|
|
|
+
|
|
|
+ initial begin
|
|
|
+ gated_rise_total = 0;
|
|
|
+ repeat (4) @(negedge clk);
|
|
|
+ rst_n = 1'b1;
|
|
|
+ calibrate_axis();
|
|
|
+ setup_at_close_endpoint();
|
|
|
+
|
|
|
+ depart_close_with_linger(1, 8'd50);
|
|
|
+ gated_rise_total = 0;
|
|
|
+ setup_at_close_endpoint();
|
|
|
+ depart_close_with_linger(2, 8'd50);
|
|
|
+ gated_rise_total = 0;
|
|
|
+ setup_at_close_endpoint();
|
|
|
+ depart_close_with_linger(4, 8'd50);
|
|
|
+ gated_rise_total = 0;
|
|
|
+ setup_at_close_endpoint();
|
|
|
+ depart_close_with_linger(8, 8'd50);
|
|
|
+
|
|
|
+ setup_at_open_endpoint();
|
|
|
+ depart_open_with_linger(1, 8'd50);
|
|
|
+ setup_at_open_endpoint();
|
|
|
+ depart_open_with_linger(4, 8'd50);
|
|
|
+ setup_at_open_endpoint();
|
|
|
+ depart_open_with_linger(8, 8'd50);
|
|
|
+
|
|
|
+ // same-percent heartbeat while lingering must not replan-stop motion
|
|
|
+ setup_at_close_endpoint();
|
|
|
+ close_limit = 1'b1;
|
|
|
+ requested_percent_in = 8'd50;
|
|
|
+ request_valid = 1'b1;
|
|
|
+ auto_planner = 1'b1;
|
|
|
+ @(negedge clk);
|
|
|
+ request_valid = 1'b0;
|
|
|
+ repeat (4) pulse_tick();
|
|
|
+ requested_percent_in = 8'd50;
|
|
|
+ request_valid = 1'b1;
|
|
|
+ @(negedge clk);
|
|
|
+ request_valid = 1'b0;
|
|
|
+ if (status_state !== ST_MOVE_OPEN)
|
|
|
+ $fatal(1, "same-percent heartbeat aborted linger move");
|
|
|
+ repeat (6) begin
|
|
|
+ pulse_tick();
|
|
|
+ if (step) force_rise();
|
|
|
+ end
|
|
|
+
|
|
|
+ $display("door_axis_source_limit_linger_tb PASS");
|
|
|
+ $finish;
|
|
|
+ end
|
|
|
+endmodule
|