prod2u_p4_motor_status_payload_tb.v 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. `timescale 1ns/1ps
  2. module prod2u_p4_motor_status_payload_tb;
  3. localparam integer STANDARD_STEPS = 8;
  4. reg clk = 1'b0;
  5. reg rst_n = 1'b0;
  6. reg force_stop = 1'b0;
  7. reg disabled = 1'b0;
  8. reg watchdog_timeout = 1'b0;
  9. reg x_limit_raw = 1'b0;
  10. reg y_limit_raw = 1'b0;
  11. reg snapshot_valid = 1'b0;
  12. reg [23:0] snapshot_targets = 24'd0;
  13. reg [2:0] snapshot_busy_at_accept = 3'd0;
  14. reg jog_valid = 1'b0;
  15. reg [1:0] jog_axis = 2'd0;
  16. reg jog_dir = 1'b0;
  17. reg [15:0] jog_steps = 16'd1;
  18. wire [2:0] axis_active_mask;
  19. wire [2:0] axis_busy_mask;
  20. wire [2:0] step_out;
  21. wire en_n;
  22. wire [79:0] status_payload;
  23. integer failures = 0;
  24. integer guard;
  25. always #5 clk = ~clk;
  26. prod2u_p4_motor_hub #(
  27. .STANDARD_STEPS(STANDARD_STEPS),
  28. .LIMIT_CONFIRM_US(8),
  29. .DIR_SETUP_US(3),
  30. .SPEED0_HALF_US(2),
  31. .SPEED1_HALF_US(2),
  32. .SPEED2_HALF_US(2),
  33. .SPEED3_HALF_US(2)
  34. ) dut (
  35. .clk(clk),
  36. .rst_n(rst_n),
  37. .tick_1us(1'b1),
  38. .force_stop(force_stop),
  39. .disabled(disabled),
  40. .watchdog_timeout(watchdog_timeout),
  41. .x_limit_raw(x_limit_raw),
  42. .y_limit_raw(y_limit_raw),
  43. .snapshot_valid(snapshot_valid),
  44. .snapshot_targets(snapshot_targets),
  45. .snapshot_busy_at_accept(snapshot_busy_at_accept),
  46. .snapshot_result_valid(),
  47. .snapshot_busy_mask(),
  48. .snapshot_invalid_mask(),
  49. .snapshot_accepted_mask(),
  50. .jog_valid(jog_valid),
  51. .jog_axis(jog_axis),
  52. .jog_dir(jog_dir),
  53. .jog_speed_code(2'd3),
  54. .jog_steps(jog_steps),
  55. .jog_result_valid(),
  56. .jog_accepted(),
  57. .axis_active_mask(axis_active_mask),
  58. .axis_busy_mask(axis_busy_mask),
  59. .step_out(step_out),
  60. .dir_out(),
  61. .en_n(en_n),
  62. .status_payload(status_payload)
  63. );
  64. task fail;
  65. input [8*96-1:0] message;
  66. begin
  67. failures = failures + 1;
  68. $display("MOTION_STATUS_FAIL: %0s payload=%020h", message,
  69. status_payload);
  70. end
  71. endtask
  72. task expect_status;
  73. input [7:0] flags;
  74. input [7:0] valid_mask;
  75. input [7:0] busy_mask;
  76. input [7:0] x_current;
  77. input [7:0] y_current;
  78. input [7:0] z_current;
  79. input [7:0] x_target;
  80. input [7:0] y_target;
  81. input [7:0] z_target;
  82. input [7:0] fault_mask;
  83. input [8*72-1:0] label_text;
  84. reg [79:0] expected;
  85. begin
  86. expected = {fault_mask, z_target, y_target, x_target,
  87. z_current, y_current, x_current,
  88. busy_mask, valid_mask, flags};
  89. #1;
  90. if (status_payload !== expected) begin
  91. $display("STATUS_MISMATCH label=%0s actual=%020h expected=%020h",
  92. label_text, status_payload, expected);
  93. fail(label_text);
  94. end
  95. end
  96. endtask
  97. task reset_hub;
  98. begin
  99. rst_n = 1'b0;
  100. force_stop = 1'b0;
  101. disabled = 1'b0;
  102. watchdog_timeout = 1'b0;
  103. x_limit_raw = 1'b0;
  104. y_limit_raw = 1'b0;
  105. snapshot_valid = 1'b0;
  106. snapshot_busy_at_accept = 3'd0;
  107. jog_valid = 1'b0;
  108. repeat (4) @(negedge clk);
  109. rst_n = 1'b1;
  110. repeat (3) @(negedge clk);
  111. end
  112. endtask
  113. task start_x_standard;
  114. input [7:0] target;
  115. begin
  116. @(negedge clk);
  117. snapshot_targets = {8'd50, 8'd50, target};
  118. snapshot_busy_at_accept = 3'd0;
  119. snapshot_valid = 1'b1;
  120. @(posedge clk);
  121. #1;
  122. @(negedge clk);
  123. snapshot_valid = 1'b0;
  124. end
  125. endtask
  126. task start_x_jog;
  127. input direction;
  128. input [15:0] steps;
  129. begin
  130. @(negedge clk);
  131. jog_axis = 2'd0;
  132. jog_dir = direction;
  133. jog_steps = steps;
  134. jog_valid = 1'b1;
  135. @(posedge clk);
  136. #1;
  137. @(negedge clk);
  138. jog_valid = 1'b0;
  139. end
  140. endtask
  141. task wait_idle;
  142. begin
  143. guard = 0;
  144. while ((axis_active_mask != 3'b000) && guard < 2048) begin
  145. @(negedge clk);
  146. guard = guard + 1;
  147. end
  148. if (axis_active_mask != 3'b000 || axis_busy_mask != 3'b000) begin
  149. fail("axis did not retire");
  150. end
  151. end
  152. endtask
  153. task wait_x_step;
  154. begin
  155. guard = 0;
  156. while (!step_out[0] && guard < 256) begin
  157. @(negedge clk);
  158. guard = guard + 1;
  159. end
  160. if (!step_out[0]) fail("X did not emit first STEP");
  161. end
  162. endtask
  163. initial begin
  164. reset_hub();
  165. expect_status(8'h00, 8'h00, 8'h00,
  166. 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
  167. "reset unknown status");
  168. start_x_standard(8'd100);
  169. expect_status(8'h02, 8'h00, 8'h00,
  170. 8'hfe, 8'hff, 8'hff, 8'd100, 8'hff, 8'hff, 8'h00,
  171. "standard OPEN setup status");
  172. wait_x_step();
  173. expect_status(8'h03, 8'h00, 8'h01,
  174. 8'hfe, 8'hff, 8'hff, 8'd100, 8'hff, 8'hff, 8'h00,
  175. "standard OPEN first-STEP busy status");
  176. wait_idle();
  177. expect_status(8'h00, 8'h01, 8'h00,
  178. 8'd100, 8'hff, 8'hff, 8'd100, 8'hff, 8'hff, 8'h00,
  179. "standard OPEN completion status");
  180. start_x_standard(8'd0);
  181. expect_status(8'h02, 8'h00, 8'h00,
  182. 8'hfe, 8'hff, 8'hff, 8'd0, 8'hff, 8'hff, 8'h00,
  183. "standard CLOSE setup status");
  184. wait_x_step();
  185. expect_status(8'h03, 8'h00, 8'h01,
  186. 8'hfe, 8'hff, 8'hff, 8'd0, 8'hff, 8'hff, 8'h00,
  187. "standard CLOSE first-STEP busy status");
  188. wait_idle();
  189. expect_status(8'h00, 8'h01, 8'h00,
  190. 8'd0, 8'hff, 8'hff, 8'd0, 8'hff, 8'hff, 8'h00,
  191. "standard CLOSE completion status");
  192. start_x_jog(1'b0, 16'd4);
  193. expect_status(8'h02, 8'h00, 8'h00,
  194. 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
  195. "JOG setup unknown status");
  196. wait_x_step();
  197. expect_status(8'h03, 8'h00, 8'h01,
  198. 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
  199. "JOG first-STEP busy unknown status");
  200. wait_idle();
  201. expect_status(8'h00, 8'h00, 8'h00,
  202. 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
  203. "JOG completion unknown status");
  204. start_x_jog(1'b1, 16'd8);
  205. wait_x_step();
  206. x_limit_raw = 1'b1;
  207. wait_idle();
  208. expect_status(8'h00, 8'h00, 8'h00,
  209. 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
  210. "limit-ignored JOG completion status");
  211. reset_hub();
  212. start_x_jog(1'b0, 16'd32);
  213. disabled = 1'b1;
  214. #1;
  215. if (step_out != 3'b000 || !en_n) fail("DISABLE did not gate pins");
  216. @(posedge clk);
  217. #1;
  218. expect_status(8'h04, 8'h00, 8'h00,
  219. 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
  220. "DISABLE status");
  221. reset_hub();
  222. start_x_jog(1'b0, 16'd32);
  223. disabled = 1'b1;
  224. watchdog_timeout = 1'b1;
  225. #1;
  226. if (step_out != 3'b000 || !en_n) fail("watchdog did not gate pins");
  227. @(posedge clk);
  228. #1;
  229. expect_status(8'h0c, 8'h00, 8'h00,
  230. 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
  231. "watchdog timeout status");
  232. if (failures != 0) begin
  233. $fatal(1, "prod2u_p4_motor_status_payload_tb failures=%0d",
  234. failures);
  235. end
  236. $display("PASS: prod2u_p4_motor_status_payload_tb");
  237. $finish;
  238. end
  239. endmodule