prod2u_p4_motor_hub_axis_isolation_tb.v 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. `timescale 1ns/1ps
  2. module prod2u_p4_motor_hub_axis_isolation_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 jog_start = 1'b0;
  8. reg [1:0] jog_axis = 2'd0;
  9. reg jog_dir = 1'b0;
  10. reg [15:0] jog_steps = 16'd1;
  11. wire [1:0] axis_started_pulse;
  12. wire [2:0] step_out;
  13. wire [2:0] dir_out;
  14. integer failures = 0;
  15. integer x_rises = 0;
  16. integer y_rises = 0;
  17. integer x_started = 0;
  18. integer y_started = 0;
  19. integer guard;
  20. always #5 clk = ~clk;
  21. always @(posedge step_out[0]) x_rises = x_rises + 1;
  22. always @(posedge step_out[1]) y_rises = y_rises + 1;
  23. always @(posedge clk) begin
  24. #1;
  25. if (axis_started_pulse[0]) x_started = x_started + 1;
  26. if (axis_started_pulse[1]) y_started = y_started + 1;
  27. if (step_out[2] || dir_out[2]) begin
  28. failures = failures + 1;
  29. $display("HUB_FAIL: reserved Z outputs were not fixed low");
  30. end
  31. if (axis_started_pulse[0] && !step_out[0]) begin
  32. failures = failures + 1;
  33. $display("HUB_FAIL: X started pulse did not coincide with X STEP high");
  34. end
  35. if (axis_started_pulse[1] && !step_out[1]) begin
  36. failures = failures + 1;
  37. $display("HUB_FAIL: Y started pulse did not coincide with Y STEP high");
  38. end
  39. end
  40. prod2u_p4_motor_hub #(
  41. .DIR_SETUP_CYCLES(DIR_SETUP_CYCLES),
  42. .STEP_HALF_CYCLES(STEP_HALF_CYCLES)
  43. ) dut (
  44. .clk(clk),
  45. .rst_n(rst_n),
  46. .jog_start(jog_start),
  47. .jog_axis(jog_axis),
  48. .jog_dir(jog_dir),
  49. .jog_steps(jog_steps),
  50. .axis_started_pulse(axis_started_pulse),
  51. .step_out(step_out),
  52. .dir_out(dir_out)
  53. );
  54. task issue_jog;
  55. input [1:0] axis;
  56. input direction;
  57. input [15:0] steps;
  58. begin
  59. @(negedge clk);
  60. jog_axis = axis;
  61. jog_dir = direction;
  62. jog_steps = steps;
  63. jog_start = 1'b1;
  64. @(posedge clk);
  65. #1;
  66. if ((axis == 2'd0) && (step_out[0] || (dir_out[0] !== direction))) begin
  67. failures = failures + 1;
  68. $display("HUB_FAIL: X command did not latch with STEP low");
  69. end
  70. if ((axis == 2'd1) && (step_out[1] || (dir_out[1] !== direction))) begin
  71. failures = failures + 1;
  72. $display("HUB_FAIL: Y command did not latch with STEP low");
  73. end
  74. @(negedge clk);
  75. jog_start = 1'b0;
  76. end
  77. endtask
  78. task wait_counts;
  79. input integer expected_x;
  80. input integer expected_y;
  81. begin
  82. guard = 0;
  83. while (((x_rises < expected_x) || (y_rises < expected_y)) &&
  84. (guard < 512)) begin
  85. @(negedge clk);
  86. guard = guard + 1;
  87. end
  88. if ((x_rises != expected_x) || (y_rises != expected_y)) begin
  89. failures = failures + 1;
  90. $display("HUB_FAIL: count mismatch x=%0d/%0d y=%0d/%0d",
  91. x_rises, expected_x, y_rises, expected_y);
  92. end
  93. end
  94. endtask
  95. initial begin
  96. repeat (3) @(negedge clk);
  97. rst_n = 1'b1;
  98. repeat (2) @(negedge clk);
  99. // X and Y are separate executors and must overlap without interference.
  100. issue_jog(2'd0, 1'b0, 16'd8);
  101. wait_counts(1, 0);
  102. issue_jog(2'd1, 1'b1, 16'd3);
  103. wait_counts(8, 3);
  104. repeat (8) @(negedge clk);
  105. if ((x_started != 1) || (y_started != 1) ||
  106. (x_rises != 8) || (y_rises != 3)) begin
  107. failures = failures + 1;
  108. $display("HUB_FAIL: independent overlap count/started mismatch");
  109. end
  110. // Replacing X while it is high must immediately lower only X; Y keeps
  111. // its own schedule and state.
  112. x_rises = 0;
  113. y_rises = 0;
  114. x_started = 0;
  115. y_started = 0;
  116. issue_jog(2'd0, 1'b0, 16'd9);
  117. issue_jog(2'd1, 1'b0, 16'd9);
  118. wait_counts(1, 1);
  119. issue_jog(2'd0, 1'b1, 16'd2);
  120. if (step_out[0] || !dir_out[0]) begin
  121. failures = failures + 1;
  122. $display("HUB_FAIL: same-axis overwrite did not force X STEP low");
  123. end
  124. guard = 0;
  125. while ((y_rises < 9) && (guard < 128)) begin
  126. @(negedge clk);
  127. guard = guard + 1;
  128. end
  129. repeat (16) @(negedge clk);
  130. if ((x_rises != 3) || (y_rises != 9) ||
  131. (x_started != 2) || (y_started != 1)) begin
  132. failures = failures + 1;
  133. $display("HUB_FAIL: overwrite/peer isolation x=%0d y=%0d sx=%0d sy=%0d",
  134. x_rises, y_rises, x_started, y_started);
  135. end
  136. // Reserved axis values are ignored and cannot disturb X/Y.
  137. issue_jog(2'd2, 1'b1, 16'd5);
  138. issue_jog(2'd3, 1'b1, 16'd5);
  139. repeat (16) @(negedge clk);
  140. if ((x_rises != 3) || (y_rises != 9) ||
  141. (x_started != 2) || (y_started != 1)) begin
  142. failures = failures + 1;
  143. $display("HUB_FAIL: reserved axis command changed X/Y motion");
  144. end
  145. if (failures != 0) begin
  146. $fatal(1, "prod2u_p4_motor_hub_axis_isolation_tb failures=%0d",
  147. failures);
  148. end
  149. $display("PASS: prod2u_p4_motor_hub_axis_isolation_tb");
  150. $finish;
  151. end
  152. endmodule