door_motion_three_axis_overlap_tb.v 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. `timescale 1ns/1ps
  2. // MSE: after startup, SET_ALL three mid percents; assert sustained three-axis
  3. // overlap motion and >=2 gated external STEP rises per axis in overlap window.
  4. // Gated rises follow STEP 0→1 only (one rise per pulse), matching the position contract.
  5. module door_motion_three_axis_overlap_tb;
  6. localparam integer CAL_TRAVEL = 32;
  7. localparam integer MID_PCT = 50;
  8. reg clk = 1'b0;
  9. reg rst_n = 1'b0;
  10. reg tick_500us = 1'b0;
  11. reg limit_sync_valid = 1'b0;
  12. reg [2:0] open_limit = 3'b000;
  13. reg [2:0] close_limit = 3'b111;
  14. reg [2:0] gated_step = 3'b000;
  15. reg [2:0] gated_step_rise = 3'b000;
  16. reg motor_request_valid = 1'b0;
  17. reg [7:0] percent_x = 8'd0;
  18. reg [7:0] percent_y = 8'd0;
  19. reg [7:0] percent_z = 8'd0;
  20. reg watchdog_stop = 1'b0;
  21. wire [2:0] step;
  22. wire [2:0] dir_close;
  23. wire startup_complete;
  24. wire [2:0] calibrated_mask;
  25. wire [2:0] fault_mask;
  26. wire x_moving = dut.u_axis_x.moving;
  27. wire y_moving = dut.u_axis_y.moving;
  28. wire z_moving = dut.u_axis_z.moving;
  29. integer overlap_cycles;
  30. integer overlap_rise_x;
  31. integer overlap_rise_y;
  32. integer overlap_rise_z;
  33. integer sustained_overlap;
  34. integer overlap_done;
  35. integer ax;
  36. integer i;
  37. integer poll;
  38. always #1 clk = ~clk;
  39. door_motion_controller #(
  40. .MAX_CAL_STEPS(64),
  41. .MIN_TRAVEL(2),
  42. .ENDPOINT_GUARD_EXTRA(8),
  43. .DIR_SETUP_TICKS(1),
  44. .PERCENT_MAX(100)
  45. ) dut (
  46. .clk(clk), .rst_n(rst_n),
  47. .tick_500us(tick_500us),
  48. .limit_sync_valid(limit_sync_valid),
  49. .open_limit(open_limit), .close_limit(close_limit),
  50. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  51. .motor_request_valid(motor_request_valid),
  52. .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z),
  53. .watchdog_stop(watchdog_stop),
  54. .watchdog_tripped(1'b0),
  55. .step(step), .dir_close(dir_close),
  56. .startup_complete(startup_complete),
  57. .calibrated_mask(calibrated_mask),
  58. .fault_mask(fault_mask),
  59. .status_capture(1'b0),
  60. .status_rd_addr(8'd0),
  61. .status_rd_data()
  62. );
  63. task pulse_tick;
  64. begin
  65. @(negedge clk); tick_500us = 1'b1;
  66. @(negedge clk); tick_500us = 1'b0;
  67. end
  68. endtask
  69. task force_rise_axis;
  70. input integer axis;
  71. input integer n;
  72. integer k;
  73. begin
  74. for (k = 0; k < n; k = k + 1) begin
  75. @(negedge clk);
  76. gated_step[axis] = 1'b1;
  77. gated_step_rise[axis] = 1'b1;
  78. @(negedge clk);
  79. gated_step[axis] = 1'b0;
  80. gated_step_rise[axis] = 1'b0;
  81. repeat (4) @(negedge clk);
  82. end
  83. end
  84. endtask
  85. task cal_one_axis;
  86. input integer axis;
  87. input integer travel;
  88. begin
  89. repeat (4) pulse_tick();
  90. force_rise_axis(axis, travel);
  91. @(negedge clk);
  92. open_limit[axis] = 1'b1;
  93. close_limit[axis] = 1'b0;
  94. @(negedge clk);
  95. open_limit[axis] = 1'b0;
  96. close_limit[axis] = 1'b0;
  97. repeat (8) @(negedge clk);
  98. end
  99. endtask
  100. // STEP rising-edge detector (1 clk wide).
  101. reg [2:0] step_d;
  102. always @(posedge clk or negedge rst_n) begin
  103. if (!rst_n)
  104. step_d <= 3'b000;
  105. else
  106. step_d <= step;
  107. end
  108. wire [2:0] step_rise = step & ~step_d;
  109. initial begin
  110. #8000000 $fatal(1, "door_motion_three_axis_overlap_tb timeout");
  111. end
  112. initial begin
  113. overlap_cycles = 0;
  114. overlap_rise_x = 0;
  115. overlap_rise_y = 0;
  116. overlap_rise_z = 0;
  117. sustained_overlap = 0;
  118. overlap_done = 0;
  119. poll = 0;
  120. repeat (2) @(posedge clk);
  121. rst_n = 1'b1;
  122. @(negedge clk);
  123. limit_sync_valid = 1'b1;
  124. cal_one_axis(0, CAL_TRAVEL);
  125. cal_one_axis(1, CAL_TRAVEL);
  126. cal_one_axis(2, CAL_TRAVEL);
  127. repeat (300) @(negedge clk);
  128. if (!startup_complete)
  129. $fatal(1, "startup_complete missing after serial cal + profile");
  130. if (calibrated_mask !== 3'b111)
  131. $fatal(1, "calibrated_mask=%b", calibrated_mask);
  132. if (fault_mask !== 3'b000)
  133. $fatal(1, "fault_mask=%b", fault_mask);
  134. open_limit = 3'b000;
  135. close_limit = 3'b000;
  136. @(negedge clk);
  137. percent_x = MID_PCT;
  138. percent_y = MID_PCT;
  139. percent_z = MID_PCT;
  140. motor_request_valid = 1'b1;
  141. @(negedge clk);
  142. motor_request_valid = 1'b0;
  143. for (poll = 0; poll < 400000 && !overlap_done; poll = poll + 1) begin
  144. @(posedge clk);
  145. tick_500us = ((poll % 8) == 0);
  146. if (x_moving && y_moving && z_moving) begin
  147. overlap_cycles = overlap_cycles + 1;
  148. if (overlap_cycles >= 8)
  149. sustained_overlap = 1;
  150. end
  151. for (ax = 0; ax < 3; ax = ax + 1) begin
  152. if (step_rise[ax]) begin
  153. gated_step[ax] = 1'b1;
  154. gated_step_rise[ax] = 1'b1;
  155. if (x_moving && y_moving && z_moving) begin
  156. case (ax)
  157. 0: overlap_rise_x = overlap_rise_x + 1;
  158. 1: overlap_rise_y = overlap_rise_y + 1;
  159. default: overlap_rise_z = overlap_rise_z + 1;
  160. endcase
  161. end
  162. end else begin
  163. gated_step[ax] = 1'b0;
  164. gated_step_rise[ax] = 1'b0;
  165. end
  166. end
  167. if (sustained_overlap &&
  168. overlap_rise_x >= 2 &&
  169. overlap_rise_y >= 2 &&
  170. overlap_rise_z >= 2)
  171. overlap_done = 1;
  172. end
  173. if (!sustained_overlap)
  174. $fatal(1, "no sustained x&&y&&z moving overlap (cycles=%0d x=%b y=%b z=%b)",
  175. overlap_cycles, x_moving, y_moving, z_moving);
  176. if (overlap_rise_x < 2 || overlap_rise_y < 2 || overlap_rise_z < 2)
  177. $fatal(1, "overlap gated rises x=%0d y=%0d z=%0d",
  178. overlap_rise_x, overlap_rise_y, overlap_rise_z);
  179. $display("door_motion_three_axis_overlap_tb PASS overlap_cycles=%0d rises=%0d/%0d/%0d",
  180. overlap_cycles, overlap_rise_x, overlap_rise_y, overlap_rise_z);
  181. $finish;
  182. end
  183. endmodule