prod2u_p4_production_limit_mapping_tb.v 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. `timescale 1ns/1ps
  2. module prod2u_p4_production_limit_mapping_tb;
  3. localparam [2:0] AXIS_CLOSED = 3'd1;
  4. reg clk = 1'b0;
  5. reg host_uart_rx = 1'b1;
  6. reg x_limit_raw = 1'b0;
  7. reg y_limit_raw = 1'b0;
  8. wire host_uart_tx;
  9. wire [23:0] ptc_pwm_out;
  10. wire [3:0] fan_pwm_out;
  11. wire solenoid_out;
  12. wire motor_en_n;
  13. wire x_step;
  14. wire x_dir;
  15. wire y_step;
  16. wire y_dir;
  17. wire z_step;
  18. wire z_dir;
  19. integer guard;
  20. integer x_step_rises = 0;
  21. integer y_step_rises = 0;
  22. integer x_rises_after_close;
  23. reg previous_x_step = 1'b0;
  24. reg previous_y_step = 1'b0;
  25. always #5 clk = ~clk;
  26. always @(posedge clk) begin
  27. if (!previous_x_step && x_step) x_step_rises <= x_step_rises + 1;
  28. if (!previous_y_step && y_step) y_step_rises <= y_step_rises + 1;
  29. previous_x_step <= x_step;
  30. previous_y_step <= y_step;
  31. end
  32. prod2u_p4_normal_v3_top #(
  33. .CLK_HZ(1000000),
  34. .HOST_UART_CLKS_PER_BIT(4),
  35. .POR_CYCLES(4),
  36. .INTER_BYTE_TIMEOUT_MS(2),
  37. .PTC_STEP_CLKS(1),
  38. .FAN_STEP_CLKS(1),
  39. .SOLENOID_ACTIVE_HIGH(1),
  40. .BUILD_ID(32'h1234abcd)
  41. ) dut (
  42. .sys_clk(clk),
  43. .host_uart_rx(host_uart_rx),
  44. .host_uart_tx(host_uart_tx),
  45. .ptc_pwm_out(ptc_pwm_out),
  46. .fan_pwm_out(fan_pwm_out),
  47. .solenoid_out(solenoid_out),
  48. .motor_en_n(motor_en_n),
  49. .x_step(x_step),
  50. .x_dir(x_dir),
  51. .y_step(y_step),
  52. .y_dir(y_dir),
  53. .z_step(z_step),
  54. .z_dir(z_dir),
  55. .x_limit_raw(x_limit_raw),
  56. .y_limit_raw(y_limit_raw)
  57. );
  58. task issue_jog;
  59. input [1:0] axis;
  60. input direction;
  61. reg [63:0] payload;
  62. begin
  63. payload = {16'd0, 16'd100, 8'd3, {7'd0, direction},
  64. {6'd0, axis}, 8'h35};
  65. guard = 0;
  66. while (!dut.request_ready && guard < 100000) begin
  67. @(posedge clk);
  68. guard = guard + 1;
  69. end
  70. if (guard >= 100000) begin
  71. $display("FAIL: core request path never became ready axis=%0d", axis);
  72. $fatal(1);
  73. end
  74. @(negedge clk);
  75. force dut.request_type = 8'h11;
  76. force dut.request_length = 6'd8;
  77. force dut.request_payload = payload;
  78. force dut.request_crc16 = {8'h5a, 6'd0, axis};
  79. force dut.request_valid = 1'b1;
  80. @(posedge clk);
  81. #1;
  82. @(negedge clk);
  83. release dut.request_valid;
  84. release dut.request_type;
  85. release dut.request_length;
  86. release dut.request_payload;
  87. release dut.request_crc16;
  88. end
  89. endtask
  90. task wait_initial_limits_low;
  91. begin
  92. guard = 0;
  93. while (!(dut.x_limit_ready && dut.y_limit_ready &&
  94. !dut.x_limit_guard && !dut.y_limit_guard &&
  95. !dut.x_limit_fast && !dut.y_limit_fast &&
  96. !dut.x_limit_active && !dut.y_limit_active) &&
  97. guard < 10000) begin
  98. @(posedge clk);
  99. guard = guard + 1;
  100. end
  101. if (guard >= 10000) begin
  102. $display("FAIL: active-high limits did not confirm raw-low inactive X=%0d/%0d/%0d/%0d Y=%0d/%0d/%0d/%0d",
  103. dut.x_limit_guard, dut.x_limit_fast, dut.x_limit_active,
  104. dut.x_limit_ready, dut.y_limit_guard, dut.y_limit_fast,
  105. dut.y_limit_active, dut.y_limit_ready);
  106. $fatal(1);
  107. end
  108. end
  109. endtask
  110. task wait_axis_step;
  111. input [1:0] axis;
  112. integer before_count;
  113. begin
  114. before_count = (axis == 0) ? x_step_rises : y_step_rises;
  115. guard = 0;
  116. while ((((axis == 0) ? x_step_rises : y_step_rises) == before_count) &&
  117. guard < 10000) begin
  118. @(posedge clk);
  119. guard = guard + 1;
  120. end
  121. if (guard >= 10000) begin
  122. $display("FAIL: axis %0d JOG emitted no STEP", axis);
  123. $fatal(1);
  124. end
  125. end
  126. endtask
  127. task wait_axis_closed;
  128. input [1:0] axis;
  129. begin
  130. guard = 0;
  131. while (!(((axis == 0) ? dut.x_limit_active : dut.y_limit_active) &&
  132. ((axis == 0) ?
  133. (dut.u_core.u_motors.x_axis_state == AXIS_CLOSED) :
  134. (dut.u_core.u_motors.y_axis_state == AXIS_CLOSED)) &&
  135. !dut.u_core.u_motors.axis_command_busy[axis]) &&
  136. guard < 50000) begin
  137. @(posedge clk);
  138. guard = guard + 1;
  139. end
  140. if (guard >= 50000) begin
  141. $display("FAIL: axis %0d did not close from its own active-high raw limit Xstate=%0d Ystate=%0d Xraw=%0d Yraw=%0d Xactive=%0d Yactive=%0d",
  142. axis, dut.u_core.u_motors.x_axis_state,
  143. dut.u_core.u_motors.y_axis_state, x_limit_raw, y_limit_raw,
  144. dut.x_limit_active, dut.y_limit_active);
  145. $fatal(1);
  146. end
  147. end
  148. endtask
  149. initial begin
  150. wait (dut.sys_rst_n === 1'b1);
  151. wait_initial_limits_low();
  152. issue_jog(2'd0, 1'b1);
  153. wait_axis_step(2'd0);
  154. if (y_step_rises != 0) begin
  155. $display("FAIL: Y moved during X-only JOG");
  156. $fatal(1);
  157. end
  158. @(negedge clk);
  159. x_limit_raw = 1'b1;
  160. wait_axis_closed(2'd0);
  161. if (!dut.u_core.u_motors.axis_close_dir_valid[0] ||
  162. dut.u_core.u_motors.axis_close_dir[0] !== 1'b1 ||
  163. dut.u_core.u_motors.axis_close_dir_valid[1] ||
  164. dut.y_limit_guard || dut.y_limit_fast || dut.y_limit_active ||
  165. y_step_rises != 0) begin
  166. $display("FAIL: X raw high affected the wrong axis");
  167. $fatal(1);
  168. end
  169. x_rises_after_close = x_step_rises;
  170. issue_jog(2'd1, 1'b0);
  171. wait_axis_step(2'd1);
  172. if (x_step_rises != x_rises_after_close) begin
  173. $display("FAIL: X moved during Y-only JOG");
  174. $fatal(1);
  175. end
  176. @(negedge clk);
  177. y_limit_raw = 1'b1;
  178. wait_axis_closed(2'd1);
  179. if (!dut.u_core.u_motors.axis_close_dir_valid[1] ||
  180. dut.u_core.u_motors.axis_close_dir[1] !== 1'b0 ||
  181. dut.u_core.u_motors.x_axis_state !== AXIS_CLOSED ||
  182. dut.u_core.u_motors.y_axis_state !== AXIS_CLOSED ||
  183. !dut.x_limit_guard || !dut.x_limit_fast || !dut.x_limit_active ||
  184. !dut.y_limit_guard || !dut.y_limit_fast || !dut.y_limit_active ||
  185. x_step_rises != x_rises_after_close) begin
  186. $display("FAIL: Y raw high affected the wrong axis or disturbed X");
  187. $fatal(1);
  188. end
  189. if (!motor_en_n || x_step || y_step || z_step) begin
  190. $display("FAIL: production limit mapping did not finish with safe motor pins");
  191. $fatal(1);
  192. end
  193. $display("PASS: prod2u_p4_production_limit_mapping_tb");
  194. $finish;
  195. end
  196. endmodule