| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- `timescale 1ns/1ps
- // MSE: after startup, SET_ALL three mid percents; assert sustained three-axis
- // overlap motion and >=2 gated external STEP rises per axis in overlap window.
- // Gated rises follow STEP 0→1 only (one rise per pulse), matching the position contract.
- module door_motion_three_axis_overlap_tb;
- localparam integer CAL_TRAVEL = 32;
- localparam integer MID_PCT = 50;
- 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 [2:0] gated_step = 3'b000;
- reg [2:0] gated_step_rise = 3'b000;
- reg motor_request_valid = 1'b0;
- reg [7:0] percent_x = 8'd0;
- reg [7:0] percent_y = 8'd0;
- reg [7:0] percent_z = 8'd0;
- reg watchdog_stop = 1'b0;
- wire [2:0] step;
- wire [2:0] dir_close;
- wire startup_complete;
- wire [2:0] calibrated_mask;
- wire [2:0] fault_mask;
- 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;
- integer overlap_rise_x;
- integer overlap_rise_y;
- integer overlap_rise_z;
- integer sustained_overlap;
- integer overlap_done;
- integer ax;
- integer i;
- integer poll;
- always #1 clk = ~clk;
- door_motion_controller #(
- .MAX_CAL_STEPS(64),
- .MIN_TRAVEL(2),
- .ENDPOINT_GUARD_EXTRA(8),
- .DIR_SETUP_TICKS(1),
- .PERCENT_MAX(100)
- ) 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(watchdog_stop),
- .watchdog_tripped(1'b0),
- .step(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()
- );
- task pulse_tick;
- begin
- @(negedge clk); tick_500us = 1'b1;
- @(negedge clk); tick_500us = 1'b0;
- end
- endtask
- task force_rise_axis;
- input integer axis;
- input integer n;
- integer k;
- begin
- for (k = 0; k < n; k = k + 1) begin
- @(negedge clk);
- gated_step[axis] = 1'b1;
- gated_step_rise[axis] = 1'b1;
- @(negedge clk);
- gated_step[axis] = 1'b0;
- gated_step_rise[axis] = 1'b0;
- repeat (4) @(negedge clk);
- end
- end
- endtask
- task cal_one_axis;
- input integer axis;
- input integer travel;
- begin
- repeat (4) pulse_tick();
- force_rise_axis(axis, travel);
- @(negedge clk);
- open_limit[axis] = 1'b1;
- close_limit[axis] = 1'b0;
- @(negedge clk);
- open_limit[axis] = 1'b0;
- close_limit[axis] = 1'b0;
- repeat (8) @(negedge clk);
- end
- endtask
- // STEP rising-edge detector (1 clk wide).
- reg [2:0] step_d;
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n)
- step_d <= 3'b000;
- else
- step_d <= step;
- end
- wire [2:0] step_rise = step & ~step_d;
- initial begin
- #8000000 $fatal(1, "door_motion_three_axis_overlap_tb timeout");
- end
- initial begin
- overlap_cycles = 0;
- overlap_rise_x = 0;
- overlap_rise_y = 0;
- overlap_rise_z = 0;
- sustained_overlap = 0;
- overlap_done = 0;
- poll = 0;
- repeat (2) @(posedge clk);
- rst_n = 1'b1;
- @(negedge clk);
- limit_sync_valid = 1'b1;
- cal_one_axis(0, CAL_TRAVEL);
- cal_one_axis(1, CAL_TRAVEL);
- cal_one_axis(2, CAL_TRAVEL);
- repeat (300) @(negedge clk);
- if (!startup_complete)
- $fatal(1, "startup_complete missing after serial cal + profile");
- if (calibrated_mask !== 3'b111)
- $fatal(1, "calibrated_mask=%b", calibrated_mask);
- if (fault_mask !== 3'b000)
- $fatal(1, "fault_mask=%b", fault_mask);
- open_limit = 3'b000;
- close_limit = 3'b000;
- @(negedge clk);
- percent_x = MID_PCT;
- percent_y = MID_PCT;
- percent_z = MID_PCT;
- motor_request_valid = 1'b1;
- @(negedge clk);
- motor_request_valid = 1'b0;
- for (poll = 0; poll < 400000 && !overlap_done; poll = poll + 1) begin
- @(posedge clk);
- tick_500us = ((poll % 8) == 0);
- if (x_moving && y_moving && z_moving) begin
- overlap_cycles = overlap_cycles + 1;
- if (overlap_cycles >= 8)
- sustained_overlap = 1;
- end
- for (ax = 0; ax < 3; ax = ax + 1) begin
- if (step_rise[ax]) begin
- gated_step[ax] = 1'b1;
- gated_step_rise[ax] = 1'b1;
- if (x_moving && y_moving && z_moving) begin
- case (ax)
- 0: overlap_rise_x = overlap_rise_x + 1;
- 1: overlap_rise_y = overlap_rise_y + 1;
- default: overlap_rise_z = overlap_rise_z + 1;
- endcase
- end
- end else begin
- gated_step[ax] = 1'b0;
- gated_step_rise[ax] = 1'b0;
- end
- end
- if (sustained_overlap &&
- overlap_rise_x >= 2 &&
- overlap_rise_y >= 2 &&
- overlap_rise_z >= 2)
- overlap_done = 1;
- end
- if (!sustained_overlap)
- $fatal(1, "no sustained x&&y&&z moving overlap (cycles=%0d x=%b y=%b z=%b)",
- overlap_cycles, x_moving, y_moving, z_moving);
- if (overlap_rise_x < 2 || overlap_rise_y < 2 || overlap_rise_z < 2)
- $fatal(1, "overlap gated rises x=%0d y=%0d z=%0d",
- overlap_rise_x, overlap_rise_y, overlap_rise_z);
- $display("door_motion_three_axis_overlap_tb PASS overlap_cycles=%0d rises=%0d/%0d/%0d",
- overlap_cycles, overlap_rise_x, overlap_rise_y, overlap_rise_z);
- $finish;
- end
- endmodule
|