door_motion_three_axis_overlap_tb.v 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. `timescale 1ns/1ps
  2. // Production gate path overlap: distinct travels; x&&y&&z moving with >=2 gated rises each.
  3. module door_motion_three_axis_overlap_tb;
  4. localparam integer SVC = 4;
  5. localparam integer PF = 64;
  6. reg clk = 1'b0;
  7. reg rst_n = 1'b0;
  8. reg tick_500us = 1'b0;
  9. reg limit_sync_valid = 1'b0;
  10. reg [2:0] open_limit = 3'b000;
  11. reg [2:0] close_limit = 3'b111;
  12. reg motor_request_valid = 1'b0;
  13. reg [7:0] percent_x = 8'd0, percent_y = 8'd0, percent_z = 8'd0;
  14. wire [2:0] engine_step, dir_close;
  15. wire [2:0] gated_step;
  16. wire startup_complete;
  17. wire [2:0] calibrated_mask, fault_mask;
  18. reg [2:0] gated_step_d;
  19. wire [2:0] gated_step_rise = gated_step & ~gated_step_d;
  20. wire x_moving = dut.u_axis_x.moving;
  21. wire y_moving = dut.u_axis_y.moving;
  22. wire z_moving = dut.u_axis_z.moving;
  23. integer overlap_cycles, rise_x, rise_y, rise_z, saw_overlap;
  24. always #1 clk = ~clk;
  25. always @(posedge clk or negedge rst_n) begin
  26. if (!rst_n) gated_step_d <= 3'b0;
  27. else gated_step_d <= gated_step;
  28. end
  29. door_motion_controller #(
  30. .MAX_CAL_STEPS(5000), .MIN_TRAVEL(2), .ENDPOINT_GUARD_EXTRA(8),
  31. .DIR_SETUP_TICKS(1), .PERCENT_MAX(100),
  32. .MOTION_SERVICE_CLKS(SVC), .PHASE_FACTOR(PF), .CAL_RAMP_UNIT(2)
  33. ) dut (
  34. .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us),
  35. .limit_sync_valid(limit_sync_valid),
  36. .open_limit(open_limit), .close_limit(close_limit),
  37. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  38. .motor_request_valid(motor_request_valid),
  39. .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z),
  40. .watchdog_stop(1'b0), .watchdog_tripped(1'b0),
  41. .step(engine_step), .dir_close(dir_close),
  42. .startup_complete(startup_complete),
  43. .calibrated_mask(calibrated_mask), .fault_mask(fault_mask),
  44. .status_capture(1'b0), .status_rd_addr(8'd0), .status_rd_data()
  45. );
  46. prod2u_runtime_step_limit_gate #(.AXIS_COUNT(3)) u_gate (
  47. .clk(clk), .rst_n(rst_n),
  48. .engine_step(engine_step), .engine_dir(dir_close),
  49. .close_limit(close_limit), .open_limit(open_limit),
  50. .selected_limit(), .gated_step(gated_step)
  51. );
  52. task pulse_tick;
  53. begin
  54. @(negedge clk); tick_500us = 1'b1;
  55. @(negedge clk); tick_500us = 1'b0;
  56. end
  57. endtask
  58. task wait_eng;
  59. input integer ax;
  60. input want;
  61. integer t;
  62. begin
  63. for (t = 0; t < 200000; t = t + 1) begin
  64. if (engine_step[ax] === want) disable wait_eng;
  65. @(negedge clk);
  66. end
  67. $fatal(1, "timeout engine[%0d]==%0d", ax, want);
  68. end
  69. endtask
  70. // Measure-open: N gated rises with open=0, then assert open while STEP low.
  71. task cal_axis_travel;
  72. input integer ax;
  73. input integer travel;
  74. integer r;
  75. begin
  76. close_limit[ax] = 1'b1;
  77. open_limit[ax] = 1'b0;
  78. repeat (6) pulse_tick();
  79. for (r = 0; r < travel; r = r + 1) begin
  80. wait_eng(ax, 1'b1);
  81. begin : wg
  82. integer t;
  83. for (t = 0; t < 16; t = t + 1) begin
  84. if (gated_step[ax]) disable wg;
  85. @(negedge clk);
  86. end
  87. $fatal(1, "gated_step[%0d] never high during cal", ax);
  88. end
  89. wait_eng(ax, 1'b0);
  90. end
  91. // Complete on open assert without requiring another rise.
  92. @(negedge clk);
  93. open_limit[ax] = 1'b1;
  94. close_limit[ax] = 1'b0;
  95. begin : wd
  96. integer t;
  97. for (t = 0; t < 20000; t = t + 1) begin
  98. if (calibrated_mask[ax] || fault_mask[ax]) disable wd;
  99. pulse_tick();
  100. @(negedge clk);
  101. end
  102. $fatal(1, "cal axis %0d stuck", ax);
  103. end
  104. if (!calibrated_mask[ax])
  105. $fatal(1, "axis %0d fault=%b", ax, fault_mask);
  106. end
  107. endtask
  108. initial begin
  109. #40_000_000 $fatal(1, "overlap tb timeout");
  110. end
  111. initial begin
  112. overlap_cycles = 0; rise_x = 0; rise_y = 0; rise_z = 0; saw_overlap = 0;
  113. repeat (2) @(posedge clk);
  114. rst_n = 1'b1;
  115. @(negedge clk);
  116. limit_sync_valid = 1'b1;
  117. // Larger travels so the triple-moving window can accumulate ≥2 gated
  118. // rises per axis (Round-5 same-task overlap semantics).
  119. cal_axis_travel(0, 32);
  120. cal_axis_travel(1, 48);
  121. cal_axis_travel(2, 64);
  122. begin : wsc
  123. integer t;
  124. for (t = 0; t < 10000; t = t + 1) begin
  125. if (startup_complete) disable wsc;
  126. @(negedge clk);
  127. end
  128. $fatal(1, "no startup_complete");
  129. end
  130. if (!((dut.u_axis_x.status_travel != dut.u_axis_y.status_travel) ||
  131. (dut.u_axis_y.status_travel != dut.u_axis_z.status_travel)))
  132. $fatal(1, "travels not distinct");
  133. open_limit = 3'b000;
  134. close_limit = 3'b000;
  135. @(negedge clk);
  136. // Same-task overlap: rises must accrue while x&&y&&z moving (not a
  137. // long post-request window that stitches unrelated phases).
  138. percent_x = 8'd90; percent_y = 8'd90; percent_z = 8'd90;
  139. motor_request_valid = 1'b1;
  140. @(negedge clk);
  141. motor_request_valid = 1'b0;
  142. begin : run_ov
  143. integer t;
  144. integer ov_rx, ov_ry, ov_rz;
  145. ov_rx = 0; ov_ry = 0; ov_rz = 0;
  146. for (t = 0; t < 800000; t = t + 1) begin
  147. if ((t % 8) == 0) pulse_tick();
  148. if (x_moving && y_moving && z_moving) begin
  149. saw_overlap = 1;
  150. overlap_cycles = overlap_cycles + 1;
  151. if (gated_step_rise[0]) begin rise_x = rise_x + 1; ov_rx = ov_rx + 1; end
  152. if (gated_step_rise[1]) begin rise_y = rise_y + 1; ov_ry = ov_ry + 1; end
  153. if (gated_step_rise[2]) begin rise_z = rise_z + 1; ov_rz = ov_rz + 1; end
  154. end
  155. if (saw_overlap && ov_rx >= 2 && ov_ry >= 2 && ov_rz >= 2 &&
  156. overlap_cycles >= 2)
  157. disable run_ov;
  158. @(negedge clk);
  159. end
  160. if (!(ov_rx >= 2 && ov_ry >= 2 && ov_rz >= 2))
  161. $fatal(1, "overlap-window rises x=%0d y=%0d z=%0d", ov_rx, ov_ry, ov_rz);
  162. end
  163. if (!saw_overlap) $fatal(1, "never saw triple moving");
  164. if (rise_x < 2 || rise_y < 2 || rise_z < 2)
  165. $fatal(1, "gated rises x=%0d y=%0d z=%0d", rise_x, rise_y, rise_z);
  166. $display("door_motion_three_axis_overlap_tb PASS rises=%0d/%0d/%0d travel=%0d/%0d/%0d ov_cyc=%0d",
  167. rise_x, rise_y, rise_z,
  168. dut.u_axis_x.status_travel, dut.u_axis_y.status_travel, dut.u_axis_z.status_travel,
  169. overlap_cycles);
  170. $finish;
  171. end
  172. endmodule