| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- `timescale 1ns/1ps
- // DUT: same-cycle single-limit + host request must snap position before replan.
- // Covers close/open mirrors, STEP high/low, heartbeat vs material target,
- // limit hold then release, and peer-axis independence.
- module door_axis_request_limit_race_tb;
- reg clk = 1'b0;
- always #1 clk = ~clk;
- // Axis 0 under test
- 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 1 (must stay undisturbed when axis0 hits limit)
- reg open_limit_b = 1'b0;
- reg close_limit_b = 1'b1;
- reg gated_step_b = 1'b0;
- reg gated_step_rise_b = 1'b0;
- reg request_valid_b = 1'b0;
- reg [7:0] requested_percent_in_b = 8'd0;
- wire planner_req_b;
- wire [7:0] planner_percent_b;
- wire [12:0] planner_travel_b;
- reg planner_done_b = 1'b0;
- reg [1:0] planner_done_axis_b = 2'd1;
- reg [7:0] planner_done_percent_b = 8'd0;
- reg [12:0] planner_result_b = 13'd0;
- wire step_b;
- wire dir_close_b;
- wire [2:0] status_state_b;
- wire [7:0] status_flags_b;
- wire [7:0] status_requested_percent_b;
- wire [12:0] status_current_b;
- wire [12:0] status_target_b;
- wire [12:0] status_travel_b;
- wire [7:0] status_fault_code_b;
- wire calibrated_b;
- wire faulted_b;
- wire terminal_b;
- 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;
- 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),
- .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(terminal),
- .axis_moving(), .axis_allow_rise(), .axis_in_calibration(), .axis_endpoint_crawl(),
- .axis_dda_run(),
- .profile_level(), .profile_distance(), .profile_done_steps(), .profile_ramp_unit(),
- .rate_quantum()
- );
- 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_b), .close_limit(close_limit_b),
- .limit_sync_valid(limit_sync_valid),
- .cal_enable(1'b1),
- .gated_step(gated_step_b), .gated_step_rise(gated_step_rise_b),
- .tick_500us(tick_500us),
- .step_toggle_pulse(tick_500us),
- .rate_quantum_in(21'd0),
- .rate_quantum_valid(1'b0),
- .request_valid(request_valid_b),
- .requested_percent_in(requested_percent_in_b),
- .watchdog_stop(1'b0),
- .planner_req(planner_req_b),
- .planner_percent(planner_percent_b),
- .planner_travel(planner_travel_b),
- .planner_done(planner_done_b),
- .planner_done_axis(planner_done_axis_b),
- .planner_done_percent(planner_done_percent_b),
- .planner_result(planner_result_b),
- .step(step_b), .dir_close(dir_close_b),
- .status_state(status_state_b), .status_flags(status_flags_b),
- .status_requested_percent(status_requested_percent_b),
- .status_current(status_current_b), .status_target(status_target_b),
- .status_travel(status_travel_b), .status_fault_code(status_fault_code_b),
- .calibrated(calibrated_b), .faulted(faulted_b), .terminal(terminal_b),
- .axis_moving(), .axis_allow_rise(), .axis_in_calibration(), .axis_endpoint_crawl(),
- .axis_dda_run(),
- .profile_level(), .profile_distance(), .profile_done_steps(), .profile_ramp_unit(),
- .rate_quantum()
- );
- // Half-up mid percent → steps (matches door_percent_planner contract).
- 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
- 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_b <= 1'b0;
- if (planner_req_b) begin
- planner_done_b <= 1'b1;
- planner_done_axis_b <= 2'd1;
- planner_done_percent_b <= planner_percent_b;
- planner_result_b <= pct_to_steps(planner_percent_b, planner_travel_b);
- 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_b;
- begin
- @(negedge clk);
- gated_step_b = 1'b1;
- gated_step_rise_b = 1'b1;
- @(negedge clk);
- gated_step_rise_b = 1'b0;
- gated_step_b = 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;
- // Already on close → measure-open.
- // TRAVEL-1 rises then open_limit+rise ⇒ travel_steps = TRAVEL.
- 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");
- if (status_travel !== TRAVEL)
- $fatal(1, "axis0 travel=%0d want %0d", status_travel, TRAVEL);
- open_limit = 1'b0;
- close_limit = 1'b0;
- end
- endtask
- task calibrate_axis1;
- integer i;
- begin
- @(negedge clk);
- open_limit_b = 1'b0;
- close_limit_b = 1'b1;
- repeat (4) pulse_tick();
- for (i = 0; i < (TRAVEL - 1); i = i + 1) begin
- force_rise_b();
- pulse_tick();
- end
- @(negedge clk);
- open_limit_b = 1'b1;
- close_limit_b = 1'b0;
- gated_step_b = 1'b1;
- gated_step_rise_b = 1'b1;
- @(negedge clk);
- gated_step_b = 1'b0;
- gated_step_rise_b = 1'b0;
- repeat (8) @(negedge clk);
- if (!calibrated_b)
- $fatal(1, "axis1 cal failed");
- open_limit_b = 1'b0;
- close_limit_b = 1'b0;
- end
- endtask
- task issue_request;
- input [7:0] pct;
- begin
- @(negedge clk);
- requested_percent_in = pct;
- request_valid = 1'b1;
- @(negedge clk);
- request_valid = 1'b0;
- end
- endtask
- task settle_motion;
- integer i;
- begin
- for (i = 0; i < 400; i = i + 1) begin
- pulse_tick();
- // Mirror STEP → gated when limits clear
- 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 else begin
- gated_step = 1'b0;
- gated_step_rise = 1'b0;
- end
- if ((status_state == ST_IDLE) && !step && !planner_req)
- i = 400;
- end
- // Drain residual high
- if (step) begin
- pulse_tick();
- gated_step = 1'b0;
- gated_step_rise = 1'b0;
- end
- repeat (4) @(negedge clk);
- end
- endtask
- task drive_to_percent;
- input [7:0] pct;
- begin
- issue_request(pct);
- settle_motion();
- 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
- // Same-cycle limit + request; assert position snapped before planner uses it.
- task collide_close_and_request;
- input [7:0] pct;
- input step_high;
- begin
- if (step_high) begin
- // Force STEP high without advancing position
- @(negedge clk);
- // Enter a move briefly then freeze step high via force path:
- // simply set gated_step high with no rise while we poke step through
- // a mid motion tick. Easier: wait until step is high during motion.
- end
- @(negedge clk);
- if (step_high && !step) begin
- // Start a dummy close move to get STEP high, then inject collision
- requested_percent_in = 8'd80;
- request_valid = 1'b1;
- @(negedge clk);
- request_valid = 1'b0;
- begin : wait_high
- integer k;
- for (k = 0; k < 80; k = k + 1) begin
- pulse_tick();
- if (step && !open_limit && !close_limit) begin
- @(negedge clk);
- gated_step = step;
- gated_step_rise = 1'b0; // hold high, no rise this cycle
- k = 80;
- end
- end
- end
- if (!step)
- $fatal(1, "failed to reach STEP high before close collide");
- end else if (!step_high) begin
- // Ensure STEP low
- if (step) begin
- pulse_tick();
- gated_step = 1'b0;
- gated_step_rise = 1'b0;
- end
- repeat (2) @(negedge clk);
- if (step)
- $fatal(1, "precondition STEP low failed");
- end
- // Corrupt internal estimate away from true close endpoint, then
- // assert close_limit + new request same cycle (mechanical is at close).
- // Position was driven to 70 earlier; close_limit snaps to travel.
- @(negedge clk);
- close_limit = 1'b1;
- open_limit = 1'b0;
- requested_percent_in = pct;
- request_valid = 1'b1;
- if (step_high) begin
- gated_step = 1'b1;
- gated_step_rise = 1'b0;
- end else begin
- gated_step = 1'b0;
- gated_step_rise = 1'b0;
- end
- @(negedge clk);
- request_valid = 1'b0;
- // Position must already be snapped this cycle (visible after edge).
- if (status_current !== status_travel)
- $fatal(1, "close collide pct=%0d step_high=%0d: current=%0d want travel=%0d",
- pct, step_high, status_current, status_travel);
- if (status_state !== ST_WAIT_REPLAN && status_state !== ST_IDLE)
- $fatal(1, "close collide expected WAIT_REPLAN/IDLE, state=%0d", status_state);
- // Hold limit a few cycles then release; planner must still use travel.
- repeat (3) @(negedge clk);
- if (status_current !== status_travel)
- $fatal(1, "close limit hold lost snap: current=%0d", status_current);
- @(negedge clk);
- close_limit = 1'b0;
- settle_motion();
- if (status_current !== pct_to_steps(pct, status_travel))
- $fatal(1, "after close collide settle current=%0d want=%0d",
- status_current, pct_to_steps(pct, status_travel));
- end
- endtask
- task collide_open_and_request;
- input [7:0] pct;
- input step_high;
- begin
- if (step_high && !step) begin
- requested_percent_in = 8'd20;
- request_valid = 1'b1;
- @(negedge clk);
- request_valid = 1'b0;
- begin : wait_high_o
- integer k;
- for (k = 0; k < 80; k = k + 1) begin
- pulse_tick();
- if (step && !open_limit && !close_limit) begin
- @(negedge clk);
- gated_step = step;
- gated_step_rise = 1'b0;
- k = 80;
- end
- end
- end
- if (!step)
- $fatal(1, "failed to reach STEP high before open collide");
- end else if (!step_high) begin
- if (step) begin
- pulse_tick();
- gated_step = 1'b0;
- gated_step_rise = 1'b0;
- end
- repeat (2) @(negedge clk);
- if (step)
- $fatal(1, "open collide precondition STEP low failed");
- end
- @(negedge clk);
- open_limit = 1'b1;
- close_limit = 1'b0;
- requested_percent_in = pct;
- request_valid = 1'b1;
- if (step_high) begin
- gated_step = 1'b1;
- gated_step_rise = 1'b0;
- end else begin
- gated_step = 1'b0;
- gated_step_rise = 1'b0;
- end
- @(negedge clk);
- request_valid = 1'b0;
- if (status_current !== 13'd0)
- $fatal(1, "open collide pct=%0d step_high=%0d: current=%0d want 0",
- pct, step_high, status_current);
- repeat (3) @(negedge clk);
- if (status_current !== 13'd0)
- $fatal(1, "open limit hold lost snap");
- @(negedge clk);
- open_limit = 1'b0;
- settle_motion();
- if (status_current !== pct_to_steps(pct, status_travel))
- $fatal(1, "after open collide settle current=%0d want=%0d",
- status_current, pct_to_steps(pct, status_travel));
- end
- endtask
- integer i;
- reg [12:0] peer_pos_before;
- initial begin
- #2000000 $fatal(1, "door_axis_request_limit_race_tb timeout");
- end
- initial begin
- repeat (2) @(posedge clk);
- rst_n = 1'b1;
- calibrate_axis0();
- calibrate_axis1();
- // Peer at mid 40% undisturbed baseline
- @(negedge clk);
- requested_percent_in_b = 8'd40;
- request_valid_b = 1'b1;
- @(negedge clk);
- request_valid_b = 1'b0;
- for (i = 0; i < 400; i = i + 1) begin
- pulse_tick();
- if (step_b && !open_limit_b && !close_limit_b) begin
- @(negedge clk);
- gated_step_b = 1'b1;
- gated_step_rise_b = 1'b1;
- @(negedge clk);
- gated_step_rise_b = 1'b0;
- gated_step_b = 1'b0;
- end
- if ((status_state_b == ST_IDLE) && !step_b && !planner_req_b)
- i = 400;
- end
- peer_pos_before = status_current_b;
- // --- close_limit + material mid target, STEP low ---
- drive_to_percent(8'd70);
- collide_close_and_request(8'd50, 1'b0);
- // --- close_limit + material mid target, STEP high ---
- drive_to_percent(8'd70);
- collide_close_and_request(8'd50, 1'b1);
- // --- close_limit + same-percent heartbeat ---
- drive_to_percent(8'd70);
- collide_close_and_request(8'd70, 1'b0);
- // --- open_limit mirror, STEP low ---
- drive_to_percent(8'd30);
- collide_open_and_request(8'd50, 1'b0);
- // --- open_limit mirror, STEP high ---
- drive_to_percent(8'd30);
- collide_open_and_request(8'd50, 1'b1);
- // Peer axis must still be at its mid target (limit on axis0 only)
- if (status_current_b !== peer_pos_before)
- $fatal(1, "peer axis disturbed: current=%0d was=%0d",
- status_current_b, peer_pos_before);
- if (faulted_b)
- $fatal(1, "peer axis faulted");
- $display("door_axis_request_limit_race_tb PASS");
- $finish;
- end
- endmodule
|