prod2u_p4_axis_fast_glitch_contract_tb.v 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. `timescale 1ns/1ps
  2. module prod2u_p4_axis_fast_glitch_contract_tb;
  3. localparam [2:0] AXIS_CLOSED = 3'd1;
  4. localparam [2:0] AXIS_OPEN = 3'd3;
  5. localparam [2:0] AXIS_INTERMEDIATE = 3'd5;
  6. localparam [2:0] AXIS_LOCKED_FAULT = 3'd7;
  7. reg clk;
  8. reg rst_n;
  9. reg limit_fast;
  10. reg limit_active;
  11. reg jog_start;
  12. reg standard_start;
  13. reg standard_target_open;
  14. reg mark_closed;
  15. wire step_out;
  16. wire busy;
  17. wire drive_active;
  18. wire fault;
  19. wire [2:0] axis_state;
  20. integer guard;
  21. prod2u_p4_axis_executor #(
  22. .HAS_LIMIT(1),
  23. .STANDARD_STEPS(4),
  24. .RECOVERY_STEPS(2),
  25. .LIMIT_CONFIRM_US(4),
  26. .DIR_SETUP_US(2),
  27. .SPEED0_HALF_US(2),
  28. .SPEED1_HALF_US(2),
  29. .SPEED2_HALF_US(2),
  30. .SPEED3_HALF_US(2)
  31. ) dut (
  32. .clk(clk), .rst_n(rst_n), .tick_1us(1'b1), .force_stop(1'b0),
  33. .limit_fast(limit_fast), .limit_active(limit_active),
  34. .jog_start(jog_start), .jog_dir(1'b1), .jog_speed_code(2'd3),
  35. .jog_steps(16'd8), .standard_start(standard_start),
  36. .standard_target_open(standard_target_open),
  37. .mark_closed(mark_closed), .mark_close_dir(1'b0),
  38. .terminal_ready(1'b1),
  39. .step_out(step_out), .dir_out(), .busy(busy), .drive_active(drive_active),
  40. .position_valid(), .current_aperture(), .target_aperture(),
  41. .close_dir(), .close_dir_valid(), .fault(fault), .move_kind(),
  42. .remaining_steps(), .axis_state(axis_state), .jog_can_accept(),
  43. .standard_can_accept()
  44. );
  45. initial begin
  46. clk = 1'b0;
  47. forever #5 clk = ~clk;
  48. end
  49. task wait_idle;
  50. begin
  51. guard = 0;
  52. while ((busy || dut.terminal_pending) && guard < 300) begin
  53. @(posedge clk);
  54. #1;
  55. guard = guard + 1;
  56. end
  57. if (busy || dut.terminal_pending || drive_active || step_out || guard >= 300) begin
  58. $display("fast-glitch axis did not settle idle and low");
  59. $fatal;
  60. end
  61. end
  62. endtask
  63. task release_limit_after_first_complete_step;
  64. begin
  65. guard = 0;
  66. while (!step_out && guard < 80) begin
  67. @(posedge clk);
  68. #1;
  69. guard = guard + 1;
  70. end
  71. if (!step_out) begin
  72. $display("fast-glitch OPEN did not emit the first STEP");
  73. $fatal;
  74. end
  75. while (step_out && guard < 160) begin
  76. @(posedge clk);
  77. #1;
  78. guard = guard + 1;
  79. end
  80. if (step_out) begin
  81. $display("fast-glitch OPEN first STEP did not complete");
  82. $fatal;
  83. end
  84. @(negedge clk);
  85. limit_fast = 1'b0;
  86. limit_active = 1'b0;
  87. end
  88. endtask
  89. task prepare_open;
  90. begin
  91. rst_n = 1'b0;
  92. limit_fast = 1'b1;
  93. limit_active = 1'b1;
  94. jog_start = 1'b0;
  95. standard_start = 1'b0;
  96. standard_target_open = 1'b0;
  97. mark_closed = 1'b0;
  98. repeat (4) @(negedge clk);
  99. rst_n = 1'b1;
  100. @(negedge clk);
  101. mark_closed = 1'b1;
  102. @(negedge clk);
  103. mark_closed = 1'b0;
  104. if (axis_state !== AXIS_CLOSED) begin
  105. $display("fast-glitch setup MARK failed");
  106. $fatal;
  107. end
  108. while (dut.terminal_pending) begin
  109. @(posedge clk);
  110. #1;
  111. end
  112. standard_target_open = 1'b1;
  113. standard_start = 1'b1;
  114. @(posedge clk);
  115. #1;
  116. @(negedge clk);
  117. standard_start = 1'b0;
  118. release_limit_after_first_complete_step();
  119. wait_idle();
  120. if (axis_state !== AXIS_OPEN) begin
  121. $display("fast-glitch setup did not reach OPEN state=%0d", axis_state);
  122. $fatal;
  123. end
  124. end
  125. endtask
  126. task drop_unconfirmed_fast;
  127. begin
  128. guard = 0;
  129. while (!(busy && !drive_active && !step_out) && guard < 100) begin
  130. @(posedge clk);
  131. #1;
  132. guard = guard + 1;
  133. end
  134. if (guard >= 100) begin
  135. $display("fast-glitch path never entered confirmation tail");
  136. $fatal;
  137. end
  138. repeat (2) @(posedge clk);
  139. @(negedge clk);
  140. limit_fast = 1'b0;
  141. wait_idle();
  142. if (axis_state !== AXIS_INTERMEDIATE || fault) begin
  143. $display("unconfirmed fast glitch did not stop at INTERMEDIATE state=%0d fault=%0d",
  144. axis_state, fault);
  145. $fatal;
  146. end
  147. repeat (8) begin
  148. @(posedge clk);
  149. #1;
  150. if (step_out || drive_active || busy) begin
  151. $display("unconfirmed fast glitch automatically resumed motion");
  152. $fatal;
  153. end
  154. end
  155. end
  156. endtask
  157. initial begin
  158. // An idle OPEN axis can see contact noise. Fast trip must block motion,
  159. // but a pulse shorter than confirmation is not a stuck-limit fault.
  160. prepare_open();
  161. @(negedge clk);
  162. limit_fast = 1'b1;
  163. drop_unconfirmed_fast();
  164. // The same rule applies while JOG continues in the opening direction.
  165. prepare_open();
  166. @(negedge clk);
  167. jog_start = 1'b1;
  168. @(negedge clk);
  169. jog_start = 1'b0;
  170. guard = 0;
  171. while (!step_out && guard < 40) begin
  172. @(posedge clk);
  173. #1;
  174. guard = guard + 1;
  175. end
  176. if (!step_out) begin
  177. $display("opening-direction JOG did not emit a STEP");
  178. $fatal;
  179. end
  180. @(negedge clk);
  181. limit_fast = 1'b1;
  182. drop_unconfirmed_fast();
  183. // A confirmed high after release is a real stuck/reasserted limit fault.
  184. prepare_open();
  185. @(negedge clk);
  186. limit_fast = 1'b1;
  187. repeat (2) @(posedge clk);
  188. limit_active = 1'b1;
  189. @(posedge clk);
  190. #1;
  191. while (dut.terminal_pending) begin
  192. @(posedge clk);
  193. #1;
  194. end
  195. if (axis_state !== AXIS_LOCKED_FAULT || !fault || busy || drive_active || step_out) begin
  196. $display("confirmed post-release limit did not lock the axis");
  197. $fatal;
  198. end
  199. $display("PASS: prod2u_p4_axis_fast_glitch_contract_tb");
  200. $finish;
  201. end
  202. endmodule