| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- `timescale 1ns/1ps
- module prod2u_p4_axis_open_release_same_edge_tb;
- localparam [2:0] AXIS_CLOSED = 3'd1;
- localparam [2:0] AXIS_OPEN = 3'd3;
- localparam [2:0] PHASE_STEP_HIGH = 3'd1;
- localparam [2:0] PHASE_STEP_LOW = 3'd2;
- reg clk;
- reg rst_n;
- reg limit_raw;
- reg standard_start;
- reg standard_target_open;
- reg mark_closed;
- wire limit_guard;
- wire limit_fast;
- wire limit_active;
- wire limit_ready;
- wire step_out;
- wire busy;
- wire drive_active;
- wire position_valid;
- wire fault;
- wire [2:0] axis_state;
- wire terminal_valid;
- wire [63:0] terminal_data;
- integer guard;
- integer rise_count;
- reg previous_step;
- reg terminal_seen;
- reg [3:0] last_stop_reason;
- time boundary_time;
- prod2u_p4_limit_filter #(
- .CLK_HZ(1000000),
- .STABLE_MS(0)
- ) u_limit (
- .clk(clk),
- .rst_n(rst_n),
- .tick_1us(1'b1),
- .limit_raw(limit_raw),
- .limit_guard(limit_guard),
- .limit_fast(limit_fast),
- .limit_active(limit_active),
- .limit_ready(limit_ready)
- );
- prod2u_p4_axis_executor #(
- .HAS_LIMIT(1),
- .USE_LIMIT_GUARD(1),
- .STANDARD_STEPS(1),
- .RECOVERY_STEPS(2),
- .RELEASE_TIMEOUT_US(8),
- .LIMIT_CONFIRM_US(4),
- .DIR_SETUP_US(0),
- .SPEED0_HALF_US(4),
- .SPEED1_HALF_US(4),
- .SPEED2_HALF_US(4),
- .SPEED3_HALF_US(4)
- ) u_axis (
- .clk(clk),
- .rst_n(rst_n),
- .tick_1us(1'b1),
- .force_stop(1'b0),
- .force_stop_reason(4'd4),
- .limit_guard(limit_guard),
- .limit_fast(limit_fast),
- .limit_active(limit_active),
- .limit_ready(limit_ready),
- .jog_start(1'b0),
- .jog_dir(1'b0),
- .jog_speed_code(2'd3),
- .jog_steps(16'd0),
- .standard_start(standard_start),
- .standard_target_open(standard_target_open),
- .mark_closed(mark_closed),
- .mark_close_dir(1'b0),
- .terminal_ready(1'b1),
- .step_out(step_out),
- .dir_out(),
- .busy(busy),
- .drive_active(drive_active),
- .position_valid(position_valid),
- .current_aperture(),
- .target_aperture(),
- .close_dir(),
- .close_dir_valid(),
- .fault(fault),
- .move_kind(),
- .remaining_steps(),
- .axis_state(axis_state),
- .jog_can_accept(),
- .standard_can_accept(),
- .terminal_pending(),
- .terminal_valid(terminal_valid),
- .terminal_meta(),
- .terminal_data(terminal_data)
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- always @(posedge clk) begin
- if (!rst_n) begin
- rise_count = 0;
- previous_step = 1'b0;
- terminal_seen = 1'b0;
- last_stop_reason = 4'd0;
- end else begin
- if (!previous_step && step_out) rise_count = rise_count + 1;
- previous_step = step_out;
- if (terminal_valid) begin
- terminal_seen = 1'b1;
- last_stop_reason = terminal_data[51:48];
- end
- end
- end
- task wait_idle;
- begin
- guard = 0;
- while (busy && (guard < 64)) begin
- @(negedge clk);
- guard = guard + 1;
- end
- if (busy) begin
- $display("FAIL: axis did not drain terminal state");
- $fatal(1);
- end
- end
- endtask
- initial begin
- rst_n = 1'b0;
- limit_raw = 1'b1;
- standard_start = 1'b0;
- standard_target_open = 1'b0;
- mark_closed = 1'b0;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- guard = 0;
- while (!(limit_ready && limit_active && limit_fast && limit_guard) &&
- (guard < 32)) begin
- @(negedge clk);
- guard = guard + 1;
- end
- if (!(limit_ready && limit_active && limit_fast && limit_guard)) begin
- $display("FAIL: closed-high limit setup did not qualify");
- $fatal(1);
- end
- @(negedge clk);
- mark_closed = 1'b1;
- @(negedge clk);
- mark_closed = 1'b0;
- wait_idle();
- if (axis_state !== AXIS_CLOSED || fault || !position_valid) begin
- $display("FAIL: MARK_CLOSED setup state=%0d fault=%0d valid=%0d",
- axis_state, fault, position_valid);
- $fatal(1);
- end
- rise_count = 0;
- terminal_seen = 1'b0;
- last_stop_reason = 4'd0;
- @(negedge clk);
- standard_target_open = 1'b1;
- standard_start = 1'b1;
- @(negedge clk);
- standard_start = 1'b0;
- guard = 0;
- while (!((u_axis.g_limited_axis.u_limited.motion_phase == PHASE_STEP_HIGH) &&
- (u_axis.g_limited_axis.u_limited.phase_remaining_us == 16'd1)) &&
- (guard < 32)) begin
- @(negedge clk);
- guard = guard + 1;
- end
- if (guard >= 32) begin
- $display("FAIL: OPEN did not reach the final STEP high boundary");
- $fatal(1);
- end
- // Sampling raw low on this edge makes the one-tick confirmed release
- // commit on the same clock as the final STEP low phase completes.
- limit_raw = 1'b0;
- guard = 0;
- while (!((u_axis.g_limited_axis.u_limited.motion_phase == PHASE_STEP_LOW) &&
- (u_axis.g_limited_axis.u_limited.phase_remaining_us == 16'd1)) &&
- (guard < 16)) begin
- @(negedge clk);
- guard = guard + 1;
- end
- if (guard >= 16 || !limit_active || limit_fast || limit_guard) begin
- $display("FAIL: boundary setup mismatch phase=%0d remaining_us=%0d active=%0d fast=%0d guard=%0d",
- u_axis.g_limited_axis.u_limited.motion_phase,
- u_axis.g_limited_axis.u_limited.phase_remaining_us,
- limit_active, limit_fast, limit_guard);
- $fatal(1);
- end
- boundary_time = $time + 5;
- @(posedge clk);
- #1;
- if (limit_active) begin
- $display("FAIL: limit did not confirm low on final STEP-low edge t=%0t",
- boundary_time);
- $fatal(1);
- end
- wait_idle();
- if (axis_state !== AXIS_OPEN || fault || !position_valid ||
- step_out || drive_active || (rise_count != 1) ||
- !terminal_seen || (last_stop_reason != 4'd1)) begin
- $display("RED: same-edge confirmed OPEN release misclassified t=%0t state=%0d fault=%0d valid=%0d rises=%0d step=%0d drive=%0d active=%0d fast=%0d guard=%0d stop_reason=%0d release_seen=%0d",
- boundary_time, axis_state, fault, position_valid, rise_count,
- step_out, drive_active, limit_active, limit_fast, limit_guard,
- last_stop_reason, u_axis.g_limited_axis.u_limited.release_seen);
- $fatal(1);
- end
- $display("PASS: prod2u_p4_axis_open_release_same_edge_tb");
- $finish;
- end
- endmodule
|