| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- `timescale 1ns/1ps
- // Strict travel-normalized timing TB (Round-5):
- // - Instantiates production door_motion_controller + prod2u_runtime_step_limit_gate
- // - STEP rises come only from production gated_step (no TB force of gated_step*)
- // - Serial X→Y→Z calibration with distinct travels
- // - Runtime mid-percent move proves overlap, exact gated-rise distances, and
- // travel-normalized completion times within 2% of the slowest axis.
- module door_travel_normalized_timing_tb;
- // SVC kept small for sim speed; PF must stay >256 so travel*scale never
- // saturates phase_threshold (max_travel*PF), or all axes collapse to the
- // same 1-toggle/service ceiling and travel-normalization disappears.
- localparam integer SVC = 4;
- localparam integer PF = 512;
- localparam integer PCT_FROM = 10;
- localparam integer PCT_TO = 90;
- // Larger mid travels so S-curve cruise dominates (matches mse_motion_ref
- // timing_alignment_ok band where 2% budget is achievable).
- localparam integer TRAVEL_X = 128;
- localparam integer TRAVEL_Y = 192;
- localparam integer TRAVEL_Z = 256;
- 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 motor_request_valid = 1'b0;
- reg [7:0] percent_x = 8'd0, percent_y = 8'd0, percent_z = 8'd0;
- wire [2:0] engine_step, dir_close;
- wire [2:0] gated_step;
- wire startup_complete;
- wire [2:0] calibrated_mask, fault_mask;
- reg [2:0] gated_step_d;
- wire [2:0] gated_step_rise = gated_step & ~gated_step_d;
- wire x_moving = dut.u_axis_x.moving;
- wire y_moving = dut.u_axis_y.moving;
- wire z_moving = dut.u_axis_z.moving;
- integer rise_x, rise_y, rise_z;
- integer first_x, first_y, first_z;
- integer last_x, last_y, last_z;
- integer done_x, done_y, done_z;
- integer saw_overlap, overlap_rises_x, overlap_rises_y, overlap_rises_z;
- integer cycle;
- integer exp_x, exp_y, exp_z;
- integer travel_meas_x, travel_meas_y, travel_meas_z;
- integer max_done, budget;
- reg measure_en;
- function integer half_up_pos;
- input integer travel;
- input integer pct;
- integer num;
- begin
- if (pct <= 0) half_up_pos = 0;
- else if (pct >= 100) half_up_pos = travel;
- else begin
- num = travel * pct + 50;
- half_up_pos = num / 100;
- if (half_up_pos < 1) half_up_pos = 1;
- if (half_up_pos > (travel - 1)) half_up_pos = travel - 1;
- end
- end
- endfunction
- always #1 clk = ~clk;
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) gated_step_d <= 3'b0;
- else gated_step_d <= gated_step;
- end
- // Count on posedge (same window as DUT) so pulse_tick's extra
- // @(negedge) waits cannot drop gated rises.
- always @(posedge clk) begin
- if (measure_en) begin
- cycle = cycle + 1;
- if (x_moving && y_moving && z_moving) begin
- saw_overlap = 1;
- if (gated_step_rise[0]) overlap_rises_x = overlap_rises_x + 1;
- if (gated_step_rise[1]) overlap_rises_y = overlap_rises_y + 1;
- if (gated_step_rise[2]) overlap_rises_z = overlap_rises_z + 1;
- end
- if (gated_step_rise[0]) begin
- if (first_x < 0) first_x = cycle;
- last_x = cycle;
- rise_x = rise_x + 1;
- end
- if (gated_step_rise[1]) begin
- if (first_y < 0) first_y = cycle;
- last_y = cycle;
- rise_y = rise_y + 1;
- end
- if (gated_step_rise[2]) begin
- if (first_z < 0) first_z = cycle;
- last_z = cycle;
- rise_z = rise_z + 1;
- end
- if (done_x < 0 && !x_moving && rise_x > 0) done_x = cycle;
- if (done_y < 0 && !y_moving && rise_y > 0) done_y = cycle;
- if (done_z < 0 && !z_moving && rise_z > 0) done_z = cycle;
- end
- end
- door_motion_controller #(
- .MAX_CAL_STEPS(5000), .MIN_TRAVEL(2), .ENDPOINT_GUARD_EXTRA(8),
- .DIR_SETUP_TICKS(1), .PERCENT_MAX(100),
- .MOTION_SERVICE_CLKS(SVC), .PHASE_FACTOR(PF), .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(1'b0), .watchdog_tripped(1'b0),
- .step(engine_step), .dir_close(dir_close),
- .startup_complete(startup_complete),
- .calibrated_mask(calibrated_mask), .fault_mask(fault_mask),
- .status_capture(1'b0), .status_rd_addr(8'd0), .status_rd_data()
- );
- prod2u_runtime_step_limit_gate #(.AXIS_COUNT(3)) u_gate (
- .clk(clk), .rst_n(rst_n),
- .engine_step(engine_step), .engine_dir(dir_close),
- .close_limit(close_limit), .open_limit(open_limit),
- .selected_limit(), .gated_step(gated_step)
- );
- task pulse_tick;
- begin
- @(negedge clk); tick_500us = 1'b1;
- @(negedge clk); tick_500us = 1'b0;
- end
- endtask
- task wait_eng;
- input integer ax;
- input want;
- integer t;
- begin
- for (t = 0; t < 100000; t = t + 1) begin
- if (engine_step[ax] === want) disable wait_eng;
- @(negedge clk);
- end
- $fatal(1, "timeout engine[%0d]==%0d", ax, want);
- end
- endtask
- // Serial measure-open: count production gated rises, then assert open.
- task cal_axis_travel;
- input integer ax;
- input integer travel;
- integer r;
- begin
- close_limit[ax] = 1'b1;
- open_limit[ax] = 1'b0;
- repeat (8) pulse_tick();
- for (r = 0; r < travel; r = r + 1) begin
- wait_eng(ax, 1'b1);
- begin : wg
- integer t;
- for (t = 0; t < 32; t = t + 1) begin
- if (gated_step[ax]) disable wg;
- @(negedge clk);
- end
- $fatal(1, "gated_step[%0d] never high during cal", ax);
- end
- wait_eng(ax, 1'b0);
- end
- @(negedge clk);
- open_limit[ax] = 1'b1;
- close_limit[ax] = 1'b0;
- begin : wd
- integer t;
- for (t = 0; t < 50000; t = t + 1) begin
- if (calibrated_mask[ax] || fault_mask[ax]) disable wd;
- pulse_tick();
- @(negedge clk);
- end
- $fatal(1, "cal axis %0d stuck", ax);
- end
- if (!calibrated_mask[ax])
- $fatal(1, "axis %0d fault=%b", ax, fault_mask);
- end
- endtask
- initial begin
- #2_000_000_000 $fatal(1, "strict timing tb timeout");
- end
- initial begin
- rise_x = 0; rise_y = 0; rise_z = 0;
- first_x = -1; first_y = -1; first_z = -1;
- last_x = -1; last_y = -1; last_z = -1;
- done_x = -1; done_y = -1; done_z = -1;
- saw_overlap = 0;
- overlap_rises_x = 0; overlap_rises_y = 0; overlap_rises_z = 0;
- cycle = 0;
- measure_en = 1'b0;
- repeat (2) @(posedge clk);
- rst_n = 1'b1;
- @(negedge clk);
- limit_sync_valid = 1'b1;
- // X→Y→Z serial calibration with distinct travels
- cal_axis_travel(0, TRAVEL_X);
- cal_axis_travel(1, TRAVEL_Y);
- cal_axis_travel(2, TRAVEL_Z);
- begin : wsc
- integer t;
- for (t = 0; t < 20000; t = t + 1) begin
- if (startup_complete) disable wsc;
- @(negedge clk);
- end
- $fatal(1, "no startup_complete");
- end
- if (!(startup_complete && (&calibrated_mask) && (fault_mask == 3'b000)))
- $fatal(1, "startup/profile not ready cal=%b fault=%b", calibrated_mask, fault_mask);
- travel_meas_x = dut.u_axis_x.status_travel;
- travel_meas_y = dut.u_axis_y.status_travel;
- travel_meas_z = dut.u_axis_z.status_travel;
- // Distinct successful travels (exact open-limit latch may be N or N±1 vs TB count)
- if (!((travel_meas_x != travel_meas_y) && (travel_meas_y != travel_meas_z) &&
- (travel_meas_x >= 16) && (travel_meas_z > travel_meas_y) && (travel_meas_y > travel_meas_x)))
- $fatal(1, "travels not distinct/ordered got %0d/%0d/%0d",
- travel_meas_x, travel_meas_y, travel_meas_z);
- exp_x = half_up_pos(travel_meas_x, PCT_TO) - half_up_pos(travel_meas_x, PCT_FROM);
- exp_y = half_up_pos(travel_meas_y, PCT_TO) - half_up_pos(travel_meas_y, PCT_FROM);
- exp_z = half_up_pos(travel_meas_z, PCT_TO) - half_up_pos(travel_meas_z, PCT_FROM);
- if (exp_x < 0) exp_x = -exp_x;
- if (exp_y < 0) exp_y = -exp_y;
- if (exp_z < 0) exp_z = -exp_z;
- if (exp_x < 2 || exp_y < 2 || exp_z < 2)
- $fatal(1, "expected distances too small %0d/%0d/%0d", exp_x, exp_y, exp_z);
- open_limit = 3'b000;
- close_limit = 3'b000;
- @(negedge clk);
- // Park at FROM first (open-loop mid), then TO — mid path, not endpoints.
- percent_x = PCT_FROM[7:0]; percent_y = PCT_FROM[7:0]; percent_z = PCT_FROM[7:0];
- motor_request_valid = 1'b1;
- @(negedge clk);
- motor_request_valid = 1'b0;
- // Wait until axes actually move to FROM and settle (do not treat
- // pre-start idle as parked — that under-counts the TO distance).
- begin : park
- integer t;
- integer saw_move;
- integer pos_from_x, pos_from_y, pos_from_z;
- saw_move = 0;
- pos_from_x = half_up_pos(travel_meas_x, PCT_FROM);
- pos_from_y = half_up_pos(travel_meas_y, PCT_FROM);
- pos_from_z = half_up_pos(travel_meas_z, PCT_FROM);
- for (t = 0; t < 2_000_000; t = t + 1) begin
- if ((t % 8) == 0) pulse_tick();
- if (x_moving || y_moving || z_moving) saw_move = 1;
- if (saw_move && !x_moving && !y_moving && !z_moving &&
- (dut.u_axis_x.current_position == pos_from_x) &&
- (dut.u_axis_y.current_position == pos_from_y) &&
- (dut.u_axis_z.current_position == pos_from_z))
- disable park;
- @(negedge clk);
- end
- $fatal(1, "park at FROM never settled pos=%0d/%0d/%0d want=%0d/%0d/%0d saw_move=%0d",
- dut.u_axis_x.current_position, dut.u_axis_y.current_position,
- dut.u_axis_z.current_position, pos_from_x, pos_from_y, pos_from_z, saw_move);
- end
- repeat (32) @(negedge clk);
- // Clear counters for the measured TO move
- rise_x = 0; rise_y = 0; rise_z = 0;
- first_x = -1; first_y = -1; first_z = -1;
- last_x = -1; last_y = -1; last_z = -1;
- done_x = -1; done_y = -1; done_z = -1;
- saw_overlap = 0;
- overlap_rises_x = 0; overlap_rises_y = 0; overlap_rises_z = 0;
- cycle = 0;
- @(negedge clk);
- percent_x = PCT_TO[7:0]; percent_y = PCT_TO[7:0]; percent_z = PCT_TO[7:0];
- motor_request_valid = 1'b1;
- @(negedge clk);
- motor_request_valid = 1'b0;
- measure_en = 1'b1;
- begin : run_move
- integer t;
- for (t = 0; t < 4_000_000; t = t + 1) begin
- if ((t % 8) == 0) pulse_tick();
- if (done_x >= 0 && done_y >= 0 && done_z >= 0 &&
- !x_moving && !y_moving && !z_moving && cycle > 100)
- disable run_move;
- @(negedge clk);
- end
- end
- measure_en = 1'b0;
- if (!saw_overlap)
- $fatal(1, "never saw x&&y&&z moving in same task");
- if (overlap_rises_x < 2 || overlap_rises_y < 2 || overlap_rises_z < 2)
- $fatal(1, "overlap-window gated rises x=%0d y=%0d z=%0d (need >=2 each)",
- overlap_rises_x, overlap_rises_y, overlap_rises_z);
- if (rise_x !== exp_x || rise_y !== exp_y || rise_z !== exp_z)
- $fatal(1, "gated rise distance mismatch got %0d/%0d/%0d exp %0d/%0d/%0d",
- rise_x, rise_y, rise_z, exp_x, exp_y, exp_z);
- if (done_x < 0 || done_y < 0 || done_z < 0)
- $fatal(1, "missing done times %0d/%0d/%0d", done_x, done_y, done_z);
- // Shortest travel finishes first; remaining axes keep moving after that.
- if (!(done_x < done_y) || !(done_x < done_z))
- $fatal(1, "fastest (X) did not finish before others: %0d/%0d/%0d",
- done_x, done_y, done_z);
- // Travel-normalized duration = last_rise - first_rise (excludes serial
- // planner grant stagger). Wall-clock last-rise may differ by that stagger.
- begin : skew
- integer span_x, span_y, span_z, max_span, span_budget;
- integer max_first, min_first, planner_bias;
- integer max_last, wall_budget;
- span_x = last_x - first_x;
- span_y = last_y - first_y;
- span_z = last_z - first_z;
- max_span = span_x;
- if (span_y > max_span) max_span = span_y;
- if (span_z > max_span) max_span = span_z;
- span_budget = (max_span * 2 + 99) / 100;
- if (span_budget < (SVC * 64)) span_budget = SVC * 64;
- if (((max_span - span_x) > span_budget) ||
- ((max_span - span_y) > span_budget) ||
- ((max_span - span_z) > span_budget))
- $fatal(1, "duration skew too large span=%0d/%0d/%0d max=%0d budget=%0d",
- span_x, span_y, span_z, max_span, span_budget);
- max_first = first_x;
- if (first_y > max_first) max_first = first_y;
- if (first_z > max_first) max_first = first_z;
- min_first = first_x;
- if (first_y < min_first) min_first = first_y;
- if (first_z < min_first) min_first = first_z;
- planner_bias = max_first - min_first;
- max_last = last_x;
- if (last_y > max_last) max_last = last_y;
- if (last_z > max_last) max_last = last_z;
- wall_budget = (max_last * 2 + 99) / 100 + planner_bias + (SVC * 64);
- if (((max_last - last_x) > wall_budget) ||
- ((max_last - last_y) > wall_budget) ||
- ((max_last - last_z) > wall_budget))
- $fatal(1, "wall last-rise skew too large last=%0d/%0d/%0d max=%0d budget=%0d bias=%0d",
- last_x, last_y, last_z, max_last, wall_budget, planner_bias);
- max_done = max_last;
- budget = wall_budget;
- end
- $display("door_travel_normalized_timing_tb PASS rises=%0d/%0d/%0d exp=%0d/%0d/%0d done=%0d/%0d/%0d ov_rises=%0d/%0d/%0d first=%0d/%0d/%0d last=%0d/%0d/%0d travel=%0d/%0d/%0d",
- rise_x, rise_y, rise_z, exp_x, exp_y, exp_z,
- done_x, done_y, done_z,
- overlap_rises_x, overlap_rises_y, overlap_rises_z,
- first_x, first_y, first_z, last_x, last_y, last_z,
- dut.u_axis_x.status_travel, dut.u_axis_y.status_travel, dut.u_axis_z.status_travel);
- $finish;
- end
- endmodule
|