| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- `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),
- .cal_enable(1'b1),
- .gated_step(gated_step), .gated_step_rise(gated_step_rise),
- .tick_500us(tick_500us),
- .step_toggle_pulse(tick_500us),
- .rate_quantum_in(21'd0),
- .rate_quantum_valid(1'b0),
- .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
|