door_motor_engine_stop_tb.v 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. `timescale 1ns/1ps
  2. module door_motor_engine_stop_tb;
  3. reg clk = 1'b0;
  4. reg rst_n = 1'b0;
  5. reg start = 1'b0;
  6. reg [1:0] active_mask = 2'b11;
  7. reg [1:0] dir_mask = 2'b11;
  8. reg tick_500us = 1'b0;
  9. reg abort = 1'b0;
  10. reg [1:0] stop_mask = 2'b00;
  11. wire busy;
  12. wire done;
  13. wire [1:0] step;
  14. wire [1:0] dir;
  15. integer x_rises;
  16. integer y_rises;
  17. integer edge_count;
  18. reg prev_x;
  19. reg prev_y;
  20. always #1 clk = ~clk;
  21. door_motor_engine #(
  22. .FIXED_STEPS(450), .DIR_SETUP_TICKS(2)
  23. ) dut (
  24. .clk(clk), .rst_n(rst_n), .start(start),
  25. .active_mask(active_mask), .dir_mask(dir_mask),
  26. .tick_500us(tick_500us), .abort(abort),
  27. .stop_mask(stop_mask),
  28. .busy(busy), .done(done), .step(step), .dir(dir)
  29. );
  30. always @(posedge clk) begin
  31. prev_x <= step[0];
  32. prev_y <= step[1];
  33. if (step[0] && !prev_x) x_rises <= x_rises + 1;
  34. if (step[1] && !prev_y) y_rises <= y_rises + 1;
  35. end
  36. task pulse_tick;
  37. begin
  38. @(negedge clk);
  39. tick_500us = 1'b1;
  40. @(negedge clk);
  41. tick_500us = 1'b0;
  42. end
  43. endtask
  44. task reset;
  45. begin
  46. rst_n = 1'b0;
  47. start = 1'b0;
  48. stop_mask = 2'b00;
  49. abort = 1'b0;
  50. active_mask = 2'b11;
  51. x_rises = 0;
  52. y_rises = 0;
  53. prev_x = 1'b0;
  54. prev_y = 1'b0;
  55. repeat (3) @(posedge clk);
  56. rst_n = 1'b1;
  57. @(posedge clk);
  58. end
  59. endtask
  60. task start_motor;
  61. begin
  62. @(negedge clk);
  63. start = 1'b1;
  64. @(negedge clk);
  65. start = 1'b0;
  66. end
  67. endtask
  68. task wait_for_done;
  69. begin
  70. while (!done) pulse_tick();
  71. @(posedge clk);
  72. end
  73. endtask
  74. initial begin
  75. #10000000 $fatal(1, "door_motor_engine_stop_tb timeout");
  76. end
  77. initial begin
  78. // Scenario 1: X stopped while STEP is low. No new X rising edge; final STEP=0.
  79. reset();
  80. start_motor();
  81. // Wait for the first X high pulse to complete and step to become low.
  82. while (step[0] !== 1'b1) pulse_tick();
  83. while (step[0] !== 1'b0) pulse_tick();
  84. stop_mask = 2'b01;
  85. edge_count = x_rises;
  86. // Allow many ticks; X should not rise again.
  87. repeat (30) pulse_tick();
  88. if (x_rises !== edge_count)
  89. $fatal(1, "scenario1: new X rising edge after stop low, x_rises=%0d expected %0d", x_rises, edge_count);
  90. if (y_rises <= edge_count)
  91. $fatal(1, "scenario1: Y did not continue");
  92. wait_for_done();
  93. if (step !== 2'b00)
  94. $fatal(1, "scenario1: final step not zero");
  95. // Scenario 2: X stopped while STEP is high. Current pulse must complete, no narrow pulse.
  96. reset();
  97. start_motor();
  98. while (step[0] !== 1'b1) pulse_tick();
  99. stop_mask = 2'b01;
  100. edge_count = x_rises;
  101. // Wait for the current high phase to finish and observe no new X rising edge.
  102. while (step[0] !== 1'b0) pulse_tick();
  103. if (x_rises !== edge_count + 1)
  104. $fatal(1, "scenario2: X pulse not counted as completed");
  105. edge_count = x_rises;
  106. repeat (20) pulse_tick();
  107. if (x_rises !== edge_count)
  108. $fatal(1, "scenario2: new X rising edge after high-phase stop");
  109. // Scenario 3: X/Y run; X stopped first, Y reaches 450.
  110. reset();
  111. start_motor();
  112. stop_mask = 2'b01; // stop X immediately (during setup)
  113. wait_for_done();
  114. if (y_rises !== 450)
  115. $fatal(1, "scenario3: Y did not reach 450, got %0d", y_rises);
  116. if (x_rises !== 0)
  117. $fatal(1, "scenario3: X should not step, got %0d", x_rises);
  118. // Scenario 4: X and Y stopped at different times.
  119. reset();
  120. start_motor();
  121. while (x_rises < 3) pulse_tick();
  122. stop_mask = 2'b01; // stop X
  123. edge_count = x_rises;
  124. repeat (30) pulse_tick(); // Y continues
  125. if (x_rises !== edge_count)
  126. $fatal(1, "scenario4: X moved after stop");
  127. if (y_rises <= edge_count)
  128. $fatal(1, "scenario4: Y did not continue");
  129. stop_mask = 2'b11; // stop Y too
  130. wait_for_done();
  131. if (x_rises !== edge_count)
  132. $fatal(1, "scenario4: X moved after Y stop");
  133. // Scenario 5: stop asserted during DIR setup. Zero steps on that axis.
  134. reset();
  135. start_motor();
  136. stop_mask = 2'b01;
  137. wait_for_done();
  138. if (x_rises !== 0)
  139. $fatal(1, "scenario5: X stepped during setup stop");
  140. if (y_rises !== 450)
  141. $fatal(1, "scenario5: Y did not reach 450");
  142. // Scenario 6: stop at the 450th pulse boundary. No 451st rising edge.
  143. reset();
  144. start_motor();
  145. // Run until 449 complete pulses on both axes (step low after 449th pulse).
  146. while (x_rises < 449 || y_rises < 449) pulse_tick();
  147. while (step[0] !== 1'b0 || step[1] !== 1'b0) pulse_tick();
  148. // Stop X at the low boundary before the 450th high phase.
  149. stop_mask = 2'b01;
  150. wait_for_done();
  151. if (x_rises > 449)
  152. $fatal(1, "scenario6: X produced more than 449 rising edges");
  153. if (y_rises !== 450)
  154. $fatal(1, "scenario6: Y did not finish 450, got %0d", y_rises);
  155. // Scenario 7: host mode (stop_mask=0). Original 450-step behavior unchanged.
  156. reset();
  157. start_motor();
  158. stop_mask = 2'b00;
  159. wait_for_done();
  160. if (x_rises !== 450 || y_rises !== 450)
  161. $fatal(1, "scenario7: host mode count mismatch x=%0d y=%0d", x_rises, y_rises);
  162. $display("door_motor_engine_stop_tb PASS");
  163. $finish;
  164. end
  165. endmodule