| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- `timescale 1ns/1ps
- // Production gate path overlap: distinct travels; x&&y&&z moving with >=2 gated rises each.
- module door_motion_three_axis_overlap_tb;
- localparam integer SVC = 4;
- localparam integer PF = 64;
- 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 overlap_cycles, rise_x, rise_y, rise_z, saw_overlap;
- 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
- 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 < 200000; 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
- // Measure-open: N gated rises with open=0, then assert open while STEP low.
- task cal_axis_travel;
- input integer ax;
- input integer travel;
- integer r;
- begin
- close_limit[ax] = 1'b1;
- open_limit[ax] = 1'b0;
- repeat (6) pulse_tick();
- for (r = 0; r < travel; r = r + 1) begin
- wait_eng(ax, 1'b1);
- begin : wg
- integer t;
- for (t = 0; t < 16; 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
- // Complete on open assert without requiring another rise.
- @(negedge clk);
- open_limit[ax] = 1'b1;
- close_limit[ax] = 1'b0;
- begin : wd
- integer t;
- for (t = 0; t < 20000; 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
- #40_000_000 $fatal(1, "overlap tb timeout");
- end
- initial begin
- overlap_cycles = 0; rise_x = 0; rise_y = 0; rise_z = 0; saw_overlap = 0;
- repeat (2) @(posedge clk);
- rst_n = 1'b1;
- @(negedge clk);
- limit_sync_valid = 1'b1;
- // Larger travels so the triple-moving window can accumulate ≥2 gated
- // rises per axis (Round-5 same-task overlap semantics).
- cal_axis_travel(0, 32);
- cal_axis_travel(1, 48);
- cal_axis_travel(2, 64);
- begin : wsc
- integer t;
- for (t = 0; t < 10000; t = t + 1) begin
- if (startup_complete) disable wsc;
- @(negedge clk);
- end
- $fatal(1, "no startup_complete");
- end
- if (!((dut.u_axis_x.status_travel != dut.u_axis_y.status_travel) ||
- (dut.u_axis_y.status_travel != dut.u_axis_z.status_travel)))
- $fatal(1, "travels not distinct");
- open_limit = 3'b000;
- close_limit = 3'b000;
- @(negedge clk);
- // Same-task overlap: rises must accrue while x&&y&&z moving (not a
- // long post-request window that stitches unrelated phases).
- percent_x = 8'd90; percent_y = 8'd90; percent_z = 8'd90;
- motor_request_valid = 1'b1;
- @(negedge clk);
- motor_request_valid = 1'b0;
- begin : run_ov
- integer t;
- integer ov_rx, ov_ry, ov_rz;
- ov_rx = 0; ov_ry = 0; ov_rz = 0;
- for (t = 0; t < 800000; t = t + 1) begin
- if ((t % 8) == 0) pulse_tick();
- if (x_moving && y_moving && z_moving) begin
- saw_overlap = 1;
- overlap_cycles = overlap_cycles + 1;
- if (gated_step_rise[0]) begin rise_x = rise_x + 1; ov_rx = ov_rx + 1; end
- if (gated_step_rise[1]) begin rise_y = rise_y + 1; ov_ry = ov_ry + 1; end
- if (gated_step_rise[2]) begin rise_z = rise_z + 1; ov_rz = ov_rz + 1; end
- end
- if (saw_overlap && ov_rx >= 2 && ov_ry >= 2 && ov_rz >= 2 &&
- overlap_cycles >= 2)
- disable run_ov;
- @(negedge clk);
- end
- if (!(ov_rx >= 2 && ov_ry >= 2 && ov_rz >= 2))
- $fatal(1, "overlap-window rises x=%0d y=%0d z=%0d", ov_rx, ov_ry, ov_rz);
- end
- if (!saw_overlap) $fatal(1, "never saw triple moving");
- if (rise_x < 2 || rise_y < 2 || rise_z < 2)
- $fatal(1, "gated rises x=%0d y=%0d z=%0d", rise_x, rise_y, rise_z);
- $display("door_motion_three_axis_overlap_tb PASS rises=%0d/%0d/%0d travel=%0d/%0d/%0d ov_cyc=%0d",
- rise_x, rise_y, rise_z,
- dut.u_axis_x.status_travel, dut.u_axis_y.status_travel, dut.u_axis_z.status_travel,
- overlap_cycles);
- $finish;
- end
- endmodule
|