| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- `timescale 1ns/1ps
- module prod2u_p4_axis_fast_glitch_contract_tb;
- localparam [2:0] AXIS_CLOSED = 3'd1;
- localparam [2:0] AXIS_OPEN = 3'd3;
- localparam [2:0] AXIS_INTERMEDIATE = 3'd5;
- localparam [2:0] AXIS_LOCKED_FAULT = 3'd7;
- reg clk;
- reg rst_n;
- reg limit_fast;
- reg limit_active;
- reg jog_start;
- reg standard_start;
- reg standard_target_open;
- reg mark_closed;
- wire step_out;
- wire busy;
- wire drive_active;
- wire fault;
- wire [2:0] axis_state;
- integer guard;
- prod2u_p4_axis_executor #(
- .HAS_LIMIT(1),
- .STANDARD_STEPS(4),
- .RECOVERY_STEPS(2),
- .LIMIT_CONFIRM_US(4),
- .DIR_SETUP_US(2),
- .SPEED0_HALF_US(2),
- .SPEED1_HALF_US(2),
- .SPEED2_HALF_US(2),
- .SPEED3_HALF_US(2)
- ) dut (
- .clk(clk), .rst_n(rst_n), .tick_1us(1'b1), .force_stop(1'b0),
- .limit_fast(limit_fast), .limit_active(limit_active),
- .jog_start(jog_start), .jog_dir(1'b1), .jog_speed_code(2'd3),
- .jog_steps(16'd8), .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(), .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()
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- task wait_idle;
- begin
- guard = 0;
- while ((busy || dut.terminal_pending) && guard < 300) begin
- @(posedge clk);
- #1;
- guard = guard + 1;
- end
- if (busy || dut.terminal_pending || drive_active || step_out || guard >= 300) begin
- $display("fast-glitch axis did not settle idle and low");
- $fatal;
- end
- end
- endtask
- task release_limit_after_first_complete_step;
- begin
- guard = 0;
- while (!step_out && guard < 80) begin
- @(posedge clk);
- #1;
- guard = guard + 1;
- end
- if (!step_out) begin
- $display("fast-glitch OPEN did not emit the first STEP");
- $fatal;
- end
- while (step_out && guard < 160) begin
- @(posedge clk);
- #1;
- guard = guard + 1;
- end
- if (step_out) begin
- $display("fast-glitch OPEN first STEP did not complete");
- $fatal;
- end
- @(negedge clk);
- limit_fast = 1'b0;
- limit_active = 1'b0;
- end
- endtask
- task prepare_open;
- begin
- rst_n = 1'b0;
- limit_fast = 1'b1;
- limit_active = 1'b1;
- jog_start = 1'b0;
- standard_start = 1'b0;
- standard_target_open = 1'b0;
- mark_closed = 1'b0;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- @(negedge clk);
- mark_closed = 1'b1;
- @(negedge clk);
- mark_closed = 1'b0;
- if (axis_state !== AXIS_CLOSED) begin
- $display("fast-glitch setup MARK failed");
- $fatal;
- end
- while (dut.terminal_pending) begin
- @(posedge clk);
- #1;
- end
- standard_target_open = 1'b1;
- standard_start = 1'b1;
- @(posedge clk);
- #1;
- @(negedge clk);
- standard_start = 1'b0;
- release_limit_after_first_complete_step();
- wait_idle();
- if (axis_state !== AXIS_OPEN) begin
- $display("fast-glitch setup did not reach OPEN state=%0d", axis_state);
- $fatal;
- end
- end
- endtask
- task drop_unconfirmed_fast;
- begin
- guard = 0;
- while (!(busy && !drive_active && !step_out) && guard < 100) begin
- @(posedge clk);
- #1;
- guard = guard + 1;
- end
- if (guard >= 100) begin
- $display("fast-glitch path never entered confirmation tail");
- $fatal;
- end
- repeat (2) @(posedge clk);
- @(negedge clk);
- limit_fast = 1'b0;
- wait_idle();
- if (axis_state !== AXIS_INTERMEDIATE || fault) begin
- $display("unconfirmed fast glitch did not stop at INTERMEDIATE state=%0d fault=%0d",
- axis_state, fault);
- $fatal;
- end
- repeat (8) begin
- @(posedge clk);
- #1;
- if (step_out || drive_active || busy) begin
- $display("unconfirmed fast glitch automatically resumed motion");
- $fatal;
- end
- end
- end
- endtask
- initial begin
- // An idle OPEN axis can see contact noise. Fast trip must block motion,
- // but a pulse shorter than confirmation is not a stuck-limit fault.
- prepare_open();
- @(negedge clk);
- limit_fast = 1'b1;
- drop_unconfirmed_fast();
- // The same rule applies while JOG continues in the opening direction.
- prepare_open();
- @(negedge clk);
- jog_start = 1'b1;
- @(negedge clk);
- jog_start = 1'b0;
- guard = 0;
- while (!step_out && guard < 40) begin
- @(posedge clk);
- #1;
- guard = guard + 1;
- end
- if (!step_out) begin
- $display("opening-direction JOG did not emit a STEP");
- $fatal;
- end
- @(negedge clk);
- limit_fast = 1'b1;
- drop_unconfirmed_fast();
- // A confirmed high after release is a real stuck/reasserted limit fault.
- prepare_open();
- @(negedge clk);
- limit_fast = 1'b1;
- repeat (2) @(posedge clk);
- limit_active = 1'b1;
- @(posedge clk);
- #1;
- while (dut.terminal_pending) begin
- @(posedge clk);
- #1;
- end
- if (axis_state !== AXIS_LOCKED_FAULT || !fault || busy || drive_active || step_out) begin
- $display("confirmed post-release limit did not lock the axis");
- $fatal;
- end
- $display("PASS: prod2u_p4_axis_fast_glitch_contract_tb");
- $finish;
- end
- endmodule
|