prod2u_p4_axis_open_release_same_edge_tb.v 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. `timescale 1ns/1ps
  2. module prod2u_p4_axis_open_release_same_edge_tb;
  3. localparam [2:0] AXIS_CLOSED = 3'd1;
  4. localparam [2:0] AXIS_OPEN = 3'd3;
  5. localparam [2:0] PHASE_STEP_HIGH = 3'd1;
  6. localparam [2:0] PHASE_STEP_LOW = 3'd2;
  7. reg clk;
  8. reg rst_n;
  9. reg limit_raw;
  10. reg standard_start;
  11. reg standard_target_open;
  12. reg mark_closed;
  13. wire limit_guard;
  14. wire limit_fast;
  15. wire limit_active;
  16. wire limit_ready;
  17. wire step_out;
  18. wire busy;
  19. wire drive_active;
  20. wire position_valid;
  21. wire fault;
  22. wire [2:0] axis_state;
  23. wire terminal_valid;
  24. wire [63:0] terminal_data;
  25. integer guard;
  26. integer rise_count;
  27. reg previous_step;
  28. reg terminal_seen;
  29. reg [3:0] last_stop_reason;
  30. time boundary_time;
  31. prod2u_p4_limit_filter #(
  32. .CLK_HZ(1000000),
  33. .STABLE_MS(0)
  34. ) u_limit (
  35. .clk(clk),
  36. .rst_n(rst_n),
  37. .tick_1us(1'b1),
  38. .limit_raw(limit_raw),
  39. .limit_guard(limit_guard),
  40. .limit_fast(limit_fast),
  41. .limit_active(limit_active),
  42. .limit_ready(limit_ready)
  43. );
  44. prod2u_p4_axis_executor #(
  45. .HAS_LIMIT(1),
  46. .USE_LIMIT_GUARD(1),
  47. .STANDARD_STEPS(1),
  48. .RECOVERY_STEPS(2),
  49. .RELEASE_TIMEOUT_US(8),
  50. .LIMIT_CONFIRM_US(4),
  51. .DIR_SETUP_US(0),
  52. .SPEED0_HALF_US(4),
  53. .SPEED1_HALF_US(4),
  54. .SPEED2_HALF_US(4),
  55. .SPEED3_HALF_US(4)
  56. ) u_axis (
  57. .clk(clk),
  58. .rst_n(rst_n),
  59. .tick_1us(1'b1),
  60. .force_stop(1'b0),
  61. .force_stop_reason(4'd4),
  62. .limit_guard(limit_guard),
  63. .limit_fast(limit_fast),
  64. .limit_active(limit_active),
  65. .limit_ready(limit_ready),
  66. .jog_start(1'b0),
  67. .jog_dir(1'b0),
  68. .jog_speed_code(2'd3),
  69. .jog_steps(16'd0),
  70. .standard_start(standard_start),
  71. .standard_target_open(standard_target_open),
  72. .mark_closed(mark_closed),
  73. .mark_close_dir(1'b0),
  74. .terminal_ready(1'b1),
  75. .step_out(step_out),
  76. .dir_out(),
  77. .busy(busy),
  78. .drive_active(drive_active),
  79. .position_valid(position_valid),
  80. .current_aperture(),
  81. .target_aperture(),
  82. .close_dir(),
  83. .close_dir_valid(),
  84. .fault(fault),
  85. .move_kind(),
  86. .remaining_steps(),
  87. .axis_state(axis_state),
  88. .jog_can_accept(),
  89. .standard_can_accept(),
  90. .terminal_pending(),
  91. .terminal_valid(terminal_valid),
  92. .terminal_meta(),
  93. .terminal_data(terminal_data)
  94. );
  95. initial begin
  96. clk = 1'b0;
  97. forever #5 clk = ~clk;
  98. end
  99. always @(posedge clk) begin
  100. if (!rst_n) begin
  101. rise_count = 0;
  102. previous_step = 1'b0;
  103. terminal_seen = 1'b0;
  104. last_stop_reason = 4'd0;
  105. end else begin
  106. if (!previous_step && step_out) rise_count = rise_count + 1;
  107. previous_step = step_out;
  108. if (terminal_valid) begin
  109. terminal_seen = 1'b1;
  110. last_stop_reason = terminal_data[51:48];
  111. end
  112. end
  113. end
  114. task wait_idle;
  115. begin
  116. guard = 0;
  117. while (busy && (guard < 64)) begin
  118. @(negedge clk);
  119. guard = guard + 1;
  120. end
  121. if (busy) begin
  122. $display("FAIL: axis did not drain terminal state");
  123. $fatal(1);
  124. end
  125. end
  126. endtask
  127. initial begin
  128. rst_n = 1'b0;
  129. limit_raw = 1'b1;
  130. standard_start = 1'b0;
  131. standard_target_open = 1'b0;
  132. mark_closed = 1'b0;
  133. repeat (4) @(negedge clk);
  134. rst_n = 1'b1;
  135. guard = 0;
  136. while (!(limit_ready && limit_active && limit_fast && limit_guard) &&
  137. (guard < 32)) begin
  138. @(negedge clk);
  139. guard = guard + 1;
  140. end
  141. if (!(limit_ready && limit_active && limit_fast && limit_guard)) begin
  142. $display("FAIL: closed-high limit setup did not qualify");
  143. $fatal(1);
  144. end
  145. @(negedge clk);
  146. mark_closed = 1'b1;
  147. @(negedge clk);
  148. mark_closed = 1'b0;
  149. wait_idle();
  150. if (axis_state !== AXIS_CLOSED || fault || !position_valid) begin
  151. $display("FAIL: MARK_CLOSED setup state=%0d fault=%0d valid=%0d",
  152. axis_state, fault, position_valid);
  153. $fatal(1);
  154. end
  155. rise_count = 0;
  156. terminal_seen = 1'b0;
  157. last_stop_reason = 4'd0;
  158. @(negedge clk);
  159. standard_target_open = 1'b1;
  160. standard_start = 1'b1;
  161. @(negedge clk);
  162. standard_start = 1'b0;
  163. guard = 0;
  164. while (!((u_axis.g_limited_axis.u_limited.motion_phase == PHASE_STEP_HIGH) &&
  165. (u_axis.g_limited_axis.u_limited.phase_remaining_us == 16'd1)) &&
  166. (guard < 32)) begin
  167. @(negedge clk);
  168. guard = guard + 1;
  169. end
  170. if (guard >= 32) begin
  171. $display("FAIL: OPEN did not reach the final STEP high boundary");
  172. $fatal(1);
  173. end
  174. // Sampling raw low on this edge makes the one-tick confirmed release
  175. // commit on the same clock as the final STEP low phase completes.
  176. limit_raw = 1'b0;
  177. guard = 0;
  178. while (!((u_axis.g_limited_axis.u_limited.motion_phase == PHASE_STEP_LOW) &&
  179. (u_axis.g_limited_axis.u_limited.phase_remaining_us == 16'd1)) &&
  180. (guard < 16)) begin
  181. @(negedge clk);
  182. guard = guard + 1;
  183. end
  184. if (guard >= 16 || !limit_active || limit_fast || limit_guard) begin
  185. $display("FAIL: boundary setup mismatch phase=%0d remaining_us=%0d active=%0d fast=%0d guard=%0d",
  186. u_axis.g_limited_axis.u_limited.motion_phase,
  187. u_axis.g_limited_axis.u_limited.phase_remaining_us,
  188. limit_active, limit_fast, limit_guard);
  189. $fatal(1);
  190. end
  191. boundary_time = $time + 5;
  192. @(posedge clk);
  193. #1;
  194. if (limit_active) begin
  195. $display("FAIL: limit did not confirm low on final STEP-low edge t=%0t",
  196. boundary_time);
  197. $fatal(1);
  198. end
  199. wait_idle();
  200. if (axis_state !== AXIS_OPEN || fault || !position_valid ||
  201. step_out || drive_active || (rise_count != 1) ||
  202. !terminal_seen || (last_stop_reason != 4'd1)) begin
  203. $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",
  204. boundary_time, axis_state, fault, position_valid, rise_count,
  205. step_out, drive_active, limit_active, limit_fast, limit_guard,
  206. last_stop_reason, u_axis.g_limited_axis.u_limited.release_seen);
  207. $fatal(1);
  208. end
  209. $display("PASS: prod2u_p4_axis_open_release_same_edge_tb");
  210. $finish;
  211. end
  212. endmodule