prod2u_p4_axis_force_jog_v1_tb.v 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. `timescale 1ns/1ps
  2. module prod2u_p4_axis_force_jog_v1_tb;
  3. localparam integer DIR_SETUP_CYCLES = 3;
  4. localparam integer STEP_HALF_CYCLES = 2;
  5. reg clk = 1'b0;
  6. reg rst_n = 1'b0;
  7. reg command_start = 1'b0;
  8. reg command_dir = 1'b0;
  9. reg [15:0] command_steps = 16'd1;
  10. wire step_out;
  11. wire dir_out;
  12. wire started_pulse;
  13. reg prod_command_start = 1'b0;
  14. reg prod_command_dir = 1'b0;
  15. reg [15:0] prod_command_steps = 16'd2;
  16. wire prod_step_out;
  17. wire prod_dir_out;
  18. wire prod_started_pulse;
  19. integer failures = 0;
  20. integer cycle_index = 0;
  21. integer rise_count = 0;
  22. integer fall_count = 0;
  23. integer started_count = 0;
  24. integer last_rise_cycle = -1;
  25. integer last_fall_cycle = -1;
  26. integer command_cycle;
  27. integer guard;
  28. reg previous_step = 1'b0;
  29. reg check_widths = 1'b1;
  30. time prod_command_time;
  31. time prod_first_rise_time;
  32. time prod_first_fall_time;
  33. time prod_second_rise_time;
  34. time prod_second_fall_time;
  35. always #5 clk = ~clk;
  36. prod2u_p4_axis_executor #(
  37. .DIR_SETUP_CYCLES(DIR_SETUP_CYCLES),
  38. .STEP_HALF_CYCLES(STEP_HALF_CYCLES)
  39. ) dut (
  40. .clk(clk),
  41. .rst_n(rst_n),
  42. .command_start(command_start),
  43. .command_dir(command_dir),
  44. .command_steps(command_steps),
  45. .step_out(step_out),
  46. .dir_out(dir_out),
  47. .started_pulse(started_pulse)
  48. );
  49. prod2u_p4_axis_executor prod_dut (
  50. .clk(clk),
  51. .rst_n(rst_n),
  52. .command_start(prod_command_start),
  53. .command_dir(prod_command_dir),
  54. .command_steps(prod_command_steps),
  55. .step_out(prod_step_out),
  56. .dir_out(prod_dir_out),
  57. .started_pulse(prod_started_pulse)
  58. );
  59. task fail;
  60. input [8*120-1:0] message;
  61. begin
  62. failures = failures + 1;
  63. $display("AXIS_FIXED_FAIL: %0s", message);
  64. end
  65. endtask
  66. always @(posedge clk) begin
  67. #1;
  68. cycle_index = cycle_index + 1;
  69. if (!rst_n) begin
  70. previous_step = 1'b0;
  71. end else begin
  72. if (!previous_step && step_out) begin
  73. rise_count = rise_count + 1;
  74. last_rise_cycle = cycle_index;
  75. if (!started_pulse && (started_count == 0)) begin
  76. fail("first STEP rise did not assert started_pulse");
  77. end
  78. if (check_widths && (last_fall_cycle >= 0) &&
  79. ((cycle_index - last_fall_cycle) != STEP_HALF_CYCLES)) begin
  80. fail("inter-step low phase had the wrong width");
  81. end
  82. end
  83. if (previous_step && !step_out) begin
  84. fall_count = fall_count + 1;
  85. if (check_widths &&
  86. ((cycle_index - last_rise_cycle) != STEP_HALF_CYCLES)) begin
  87. fail("STEP high phase had the wrong width");
  88. end
  89. last_fall_cycle = cycle_index;
  90. end
  91. if (started_pulse) begin
  92. started_count = started_count + 1;
  93. if (!step_out) begin
  94. fail("started_pulse asserted without STEP high");
  95. end
  96. if ((rise_count != 1) && check_widths) begin
  97. fail("started_pulse was not limited to the first STEP rise");
  98. end
  99. end
  100. end
  101. previous_step = step_out;
  102. end
  103. task reset_axis;
  104. begin
  105. rst_n = 1'b0;
  106. command_start = 1'b0;
  107. command_dir = 1'b0;
  108. command_steps = 16'd1;
  109. rise_count = 0;
  110. fall_count = 0;
  111. started_count = 0;
  112. last_rise_cycle = -1;
  113. last_fall_cycle = -1;
  114. previous_step = 1'b0;
  115. check_widths = 1'b1;
  116. repeat (3) @(negedge clk);
  117. rst_n = 1'b1;
  118. repeat (2) @(negedge clk);
  119. if (step_out || started_pulse) begin
  120. fail("reset did not leave STEP and started_pulse low");
  121. end
  122. end
  123. endtask
  124. task issue_command;
  125. input direction;
  126. input [15:0] steps;
  127. begin
  128. @(negedge clk);
  129. command_dir = direction;
  130. command_steps = steps;
  131. command_start = 1'b1;
  132. @(posedge clk);
  133. #1;
  134. command_cycle = cycle_index;
  135. if (step_out || started_pulse || (dir_out !== direction)) begin
  136. fail("command acceptance did not force STEP low and latch DIR");
  137. end
  138. @(negedge clk);
  139. command_start = 1'b0;
  140. end
  141. endtask
  142. task wait_for_rises;
  143. input integer expected;
  144. input integer maximum_cycles;
  145. begin
  146. guard = 0;
  147. while ((rise_count < expected) && (guard < maximum_cycles)) begin
  148. @(negedge clk);
  149. guard = guard + 1;
  150. end
  151. if (rise_count != expected) begin
  152. fail("command did not produce the requested STEP rises");
  153. end
  154. end
  155. endtask
  156. task run_exact_count;
  157. input direction;
  158. input [15:0] steps;
  159. integer first_rise_cycle;
  160. begin
  161. reset_axis();
  162. issue_command(direction, steps);
  163. wait_for_rises(steps, (steps * STEP_HALF_CYCLES * 2) +
  164. DIR_SETUP_CYCLES + 32);
  165. first_rise_cycle = command_cycle + DIR_SETUP_CYCLES;
  166. if ((last_rise_cycle < first_rise_cycle) ||
  167. (started_count != 1) || (dir_out !== direction)) begin
  168. fail("exact-count command violated setup, started, or DIR contract");
  169. end
  170. guard = 0;
  171. while ((fall_count < steps) && (guard < STEP_HALF_CYCLES + 8)) begin
  172. @(negedge clk);
  173. guard = guard + 1;
  174. end
  175. repeat (STEP_HALF_CYCLES + 2) @(negedge clk);
  176. if ((rise_count != steps) || (fall_count != steps) || step_out ||
  177. started_pulse || (started_count != 1)) begin
  178. fail("axis did not retire after the exact raw 16-bit step count");
  179. end
  180. end
  181. endtask
  182. initial begin
  183. // Values above the former 800-step cap must execute in full.
  184. run_exact_count(1'b0, 16'd1);
  185. run_exact_count(1'b1, 16'd799);
  186. run_exact_count(1'b0, 16'd800);
  187. run_exact_count(1'b1, 16'd801);
  188. run_exact_count(1'b0, 16'd8191);
  189. run_exact_count(1'b1, 16'hffff);
  190. // A setup-phase replacement restarts setup and suppresses the old move.
  191. reset_axis();
  192. issue_command(1'b0, 16'd7);
  193. @(negedge clk);
  194. issue_command(1'b1, 16'd2);
  195. wait_for_rises(2, 64);
  196. repeat (STEP_HALF_CYCLES * 2 + 4) @(negedge clk);
  197. if ((rise_count != 2) || (fall_count != 2) ||
  198. (started_count != 1) || !dir_out) begin
  199. fail("DIR_SETUP replacement did not fully supersede the old command");
  200. end
  201. // A high-phase replacement has absolute priority: STEP goes low on the
  202. // acceptance edge, then the replacement waits a fresh setup interval.
  203. reset_axis();
  204. issue_command(1'b0, 16'd7);
  205. wait_for_rises(1, 32);
  206. check_widths = 1'b0;
  207. issue_command(1'b1, 16'd3);
  208. if (step_out || started_pulse || !dir_out) begin
  209. fail("STEP_HIGH replacement did not immediately force STEP low");
  210. end
  211. rise_count = 0;
  212. fall_count = 0;
  213. started_count = 0;
  214. previous_step = 1'b0;
  215. last_rise_cycle = -1;
  216. last_fall_cycle = -1;
  217. check_widths = 1'b1;
  218. wait_for_rises(3, 64);
  219. repeat (STEP_HALF_CYCLES * 2 + 4) @(negedge clk);
  220. if ((rise_count != 3) || (fall_count != 3) ||
  221. (started_count != 1) || !dir_out) begin
  222. fail("STEP_HIGH replacement did not execute only the new command");
  223. end
  224. // A low-phase replacement also starts a fresh setup and discards tail.
  225. reset_axis();
  226. issue_command(1'b0, 16'd7);
  227. wait_for_rises(1, 32);
  228. while (step_out) @(negedge clk);
  229. issue_command(1'b1, 16'd2);
  230. rise_count = 0;
  231. fall_count = 0;
  232. started_count = 0;
  233. previous_step = 1'b0;
  234. last_rise_cycle = -1;
  235. last_fall_cycle = -1;
  236. wait_for_rises(2, 64);
  237. repeat (STEP_HALF_CYCLES * 2 + 4) @(negedge clk);
  238. if ((rise_count != 2) || (fall_count != 2) ||
  239. (started_count != 1) || !dir_out) begin
  240. fail("STEP_LOW replacement did not execute only the new command");
  241. end
  242. // Default production parameters at 25 MHz: 25,000 cycles of setup,
  243. // followed by 12,500 high and 12,500 low cycles per STEP.
  244. reset_axis();
  245. @(negedge clk);
  246. prod_command_dir = 1'b1;
  247. prod_command_steps = 16'd2;
  248. prod_command_start = 1'b1;
  249. @(posedge clk);
  250. prod_command_time = $time;
  251. #1;
  252. if (prod_step_out || prod_started_pulse || !prod_dir_out) begin
  253. fail("production command did not latch with STEP low");
  254. end
  255. @(negedge clk);
  256. prod_command_start = 1'b0;
  257. @(posedge prod_step_out);
  258. prod_first_rise_time = $time;
  259. #1;
  260. if (!prod_started_pulse) begin
  261. fail("production first STEP rise lacked started_pulse");
  262. end
  263. @(negedge prod_step_out);
  264. prod_first_fall_time = $time;
  265. @(posedge prod_step_out);
  266. prod_second_rise_time = $time;
  267. #1;
  268. if (prod_started_pulse) begin
  269. fail("production second STEP rise repeated started_pulse");
  270. end
  271. @(negedge prod_step_out);
  272. prod_second_fall_time = $time;
  273. if ((prod_first_rise_time - prod_command_time) != 250000ns) begin
  274. fail("production DIR setup was not exactly 25,000 clocks");
  275. end
  276. if ((prod_first_fall_time - prod_first_rise_time) != 125000ns) begin
  277. fail("production STEP high phase was not exactly 12,500 clocks");
  278. end
  279. if ((prod_second_rise_time - prod_first_fall_time) != 125000ns) begin
  280. fail("production STEP low phase was not exactly 12,500 clocks");
  281. end
  282. if ((prod_second_fall_time - prod_second_rise_time) != 125000ns) begin
  283. fail("production second STEP high phase was not exactly 12,500 clocks");
  284. end
  285. if (failures != 0) begin
  286. $fatal(1, "prod2u_p4_axis_force_jog_v1_tb failures=%0d", failures);
  287. end
  288. $display("PASS: prod2u_p4_axis_force_jog_v1_tb");
  289. $finish;
  290. end
  291. endmodule