door_travel_normalized_timing_tb.v 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. `timescale 1ns/1ps
  2. // Strict travel-normalized timing TB (Round-5):
  3. // - Instantiates production door_motion_controller + prod2u_runtime_step_limit_gate
  4. // - STEP rises come only from production gated_step (no TB force of gated_step*)
  5. // - Serial X→Y→Z calibration with distinct travels
  6. // - Runtime mid-percent move proves overlap, exact gated-rise distances, and
  7. // travel-normalized completion times within 2% of the slowest axis.
  8. module door_travel_normalized_timing_tb;
  9. // SVC kept small for sim speed; PF must stay >256 so travel*scale never
  10. // saturates phase_threshold (max_travel*PF), or all axes collapse to the
  11. // same 1-toggle/service ceiling and travel-normalization disappears.
  12. localparam integer SVC = 4;
  13. localparam integer PF = 512;
  14. localparam integer PCT_FROM = 10;
  15. localparam integer PCT_TO = 90;
  16. // Larger mid travels so S-curve cruise dominates (matches mse_motion_ref
  17. // timing_alignment_ok band where 2% budget is achievable).
  18. localparam integer TRAVEL_X = 128;
  19. localparam integer TRAVEL_Y = 192;
  20. localparam integer TRAVEL_Z = 256;
  21. reg clk = 1'b0;
  22. reg rst_n = 1'b0;
  23. reg tick_500us = 1'b0;
  24. reg limit_sync_valid = 1'b0;
  25. reg [2:0] open_limit = 3'b000;
  26. reg [2:0] close_limit = 3'b111;
  27. reg motor_request_valid = 1'b0;
  28. reg [7:0] percent_x = 8'd0, percent_y = 8'd0, percent_z = 8'd0;
  29. wire [2:0] engine_step, dir_close;
  30. wire [2:0] gated_step;
  31. wire startup_complete;
  32. wire [2:0] calibrated_mask, fault_mask;
  33. reg [2:0] gated_step_d;
  34. wire [2:0] gated_step_rise = gated_step & ~gated_step_d;
  35. wire x_moving = dut.u_axis_x.moving;
  36. wire y_moving = dut.u_axis_y.moving;
  37. wire z_moving = dut.u_axis_z.moving;
  38. integer rise_x, rise_y, rise_z;
  39. integer first_x, first_y, first_z;
  40. integer last_x, last_y, last_z;
  41. integer done_x, done_y, done_z;
  42. integer saw_overlap, overlap_rises_x, overlap_rises_y, overlap_rises_z;
  43. integer cycle;
  44. integer exp_x, exp_y, exp_z;
  45. integer travel_meas_x, travel_meas_y, travel_meas_z;
  46. integer max_done, budget;
  47. reg measure_en;
  48. function integer half_up_pos;
  49. input integer travel;
  50. input integer pct;
  51. integer num;
  52. begin
  53. if (pct <= 0) half_up_pos = 0;
  54. else if (pct >= 100) half_up_pos = travel;
  55. else begin
  56. num = travel * pct + 50;
  57. half_up_pos = num / 100;
  58. if (half_up_pos < 1) half_up_pos = 1;
  59. if (half_up_pos > (travel - 1)) half_up_pos = travel - 1;
  60. end
  61. end
  62. endfunction
  63. always #1 clk = ~clk;
  64. always @(posedge clk or negedge rst_n) begin
  65. if (!rst_n) gated_step_d <= 3'b0;
  66. else gated_step_d <= gated_step;
  67. end
  68. // Count on posedge (same window as DUT) so pulse_tick's extra
  69. // @(negedge) waits cannot drop gated rises.
  70. always @(posedge clk) begin
  71. if (measure_en) begin
  72. cycle = cycle + 1;
  73. if (x_moving && y_moving && z_moving) begin
  74. saw_overlap = 1;
  75. if (gated_step_rise[0]) overlap_rises_x = overlap_rises_x + 1;
  76. if (gated_step_rise[1]) overlap_rises_y = overlap_rises_y + 1;
  77. if (gated_step_rise[2]) overlap_rises_z = overlap_rises_z + 1;
  78. end
  79. if (gated_step_rise[0]) begin
  80. if (first_x < 0) first_x = cycle;
  81. last_x = cycle;
  82. rise_x = rise_x + 1;
  83. end
  84. if (gated_step_rise[1]) begin
  85. if (first_y < 0) first_y = cycle;
  86. last_y = cycle;
  87. rise_y = rise_y + 1;
  88. end
  89. if (gated_step_rise[2]) begin
  90. if (first_z < 0) first_z = cycle;
  91. last_z = cycle;
  92. rise_z = rise_z + 1;
  93. end
  94. if (done_x < 0 && !x_moving && rise_x > 0) done_x = cycle;
  95. if (done_y < 0 && !y_moving && rise_y > 0) done_y = cycle;
  96. if (done_z < 0 && !z_moving && rise_z > 0) done_z = cycle;
  97. end
  98. end
  99. door_motion_controller #(
  100. .MAX_CAL_STEPS(5000), .MIN_TRAVEL(2), .ENDPOINT_GUARD_EXTRA(8),
  101. .DIR_SETUP_TICKS(1), .PERCENT_MAX(100),
  102. .MOTION_SERVICE_CLKS(SVC), .PHASE_FACTOR(PF), .CAL_RAMP_UNIT(2)
  103. ) dut (
  104. .clk(clk), .rst_n(rst_n), .tick_500us(tick_500us),
  105. .limit_sync_valid(limit_sync_valid),
  106. .open_limit(open_limit), .close_limit(close_limit),
  107. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  108. .motor_request_valid(motor_request_valid),
  109. .percent_x(percent_x), .percent_y(percent_y), .percent_z(percent_z),
  110. .watchdog_stop(1'b0), .watchdog_tripped(1'b0),
  111. .step(engine_step), .dir_close(dir_close),
  112. .startup_complete(startup_complete),
  113. .calibrated_mask(calibrated_mask), .fault_mask(fault_mask),
  114. .status_capture(1'b0), .status_rd_addr(8'd0), .status_rd_data()
  115. );
  116. prod2u_runtime_step_limit_gate #(.AXIS_COUNT(3)) u_gate (
  117. .clk(clk), .rst_n(rst_n),
  118. .engine_step(engine_step), .engine_dir(dir_close),
  119. .close_limit(close_limit), .open_limit(open_limit),
  120. .selected_limit(), .gated_step(gated_step)
  121. );
  122. task pulse_tick;
  123. begin
  124. @(negedge clk); tick_500us = 1'b1;
  125. @(negedge clk); tick_500us = 1'b0;
  126. end
  127. endtask
  128. task wait_eng;
  129. input integer ax;
  130. input want;
  131. integer t;
  132. begin
  133. for (t = 0; t < 100000; t = t + 1) begin
  134. if (engine_step[ax] === want) disable wait_eng;
  135. @(negedge clk);
  136. end
  137. $fatal(1, "timeout engine[%0d]==%0d", ax, want);
  138. end
  139. endtask
  140. // Serial measure-open: count production gated rises, then assert open.
  141. task cal_axis_travel;
  142. input integer ax;
  143. input integer travel;
  144. integer r;
  145. begin
  146. close_limit[ax] = 1'b1;
  147. open_limit[ax] = 1'b0;
  148. repeat (8) pulse_tick();
  149. for (r = 0; r < travel; r = r + 1) begin
  150. wait_eng(ax, 1'b1);
  151. begin : wg
  152. integer t;
  153. for (t = 0; t < 32; t = t + 1) begin
  154. if (gated_step[ax]) disable wg;
  155. @(negedge clk);
  156. end
  157. $fatal(1, "gated_step[%0d] never high during cal", ax);
  158. end
  159. wait_eng(ax, 1'b0);
  160. end
  161. @(negedge clk);
  162. open_limit[ax] = 1'b1;
  163. close_limit[ax] = 1'b0;
  164. begin : wd
  165. integer t;
  166. for (t = 0; t < 50000; t = t + 1) begin
  167. if (calibrated_mask[ax] || fault_mask[ax]) disable wd;
  168. pulse_tick();
  169. @(negedge clk);
  170. end
  171. $fatal(1, "cal axis %0d stuck", ax);
  172. end
  173. if (!calibrated_mask[ax])
  174. $fatal(1, "axis %0d fault=%b", ax, fault_mask);
  175. end
  176. endtask
  177. initial begin
  178. #2_000_000_000 $fatal(1, "strict timing tb timeout");
  179. end
  180. initial begin
  181. rise_x = 0; rise_y = 0; rise_z = 0;
  182. first_x = -1; first_y = -1; first_z = -1;
  183. last_x = -1; last_y = -1; last_z = -1;
  184. done_x = -1; done_y = -1; done_z = -1;
  185. saw_overlap = 0;
  186. overlap_rises_x = 0; overlap_rises_y = 0; overlap_rises_z = 0;
  187. cycle = 0;
  188. measure_en = 1'b0;
  189. repeat (2) @(posedge clk);
  190. rst_n = 1'b1;
  191. @(negedge clk);
  192. limit_sync_valid = 1'b1;
  193. // X→Y→Z serial calibration with distinct travels
  194. cal_axis_travel(0, TRAVEL_X);
  195. cal_axis_travel(1, TRAVEL_Y);
  196. cal_axis_travel(2, TRAVEL_Z);
  197. begin : wsc
  198. integer t;
  199. for (t = 0; t < 20000; t = t + 1) begin
  200. if (startup_complete) disable wsc;
  201. @(negedge clk);
  202. end
  203. $fatal(1, "no startup_complete");
  204. end
  205. if (!(startup_complete && (&calibrated_mask) && (fault_mask == 3'b000)))
  206. $fatal(1, "startup/profile not ready cal=%b fault=%b", calibrated_mask, fault_mask);
  207. travel_meas_x = dut.u_axis_x.status_travel;
  208. travel_meas_y = dut.u_axis_y.status_travel;
  209. travel_meas_z = dut.u_axis_z.status_travel;
  210. // Distinct successful travels (exact open-limit latch may be N or N±1 vs TB count)
  211. if (!((travel_meas_x != travel_meas_y) && (travel_meas_y != travel_meas_z) &&
  212. (travel_meas_x >= 16) && (travel_meas_z > travel_meas_y) && (travel_meas_y > travel_meas_x)))
  213. $fatal(1, "travels not distinct/ordered got %0d/%0d/%0d",
  214. travel_meas_x, travel_meas_y, travel_meas_z);
  215. exp_x = half_up_pos(travel_meas_x, PCT_TO) - half_up_pos(travel_meas_x, PCT_FROM);
  216. exp_y = half_up_pos(travel_meas_y, PCT_TO) - half_up_pos(travel_meas_y, PCT_FROM);
  217. exp_z = half_up_pos(travel_meas_z, PCT_TO) - half_up_pos(travel_meas_z, PCT_FROM);
  218. if (exp_x < 0) exp_x = -exp_x;
  219. if (exp_y < 0) exp_y = -exp_y;
  220. if (exp_z < 0) exp_z = -exp_z;
  221. if (exp_x < 2 || exp_y < 2 || exp_z < 2)
  222. $fatal(1, "expected distances too small %0d/%0d/%0d", exp_x, exp_y, exp_z);
  223. open_limit = 3'b000;
  224. close_limit = 3'b000;
  225. @(negedge clk);
  226. // Park at FROM first (open-loop mid), then TO — mid path, not endpoints.
  227. percent_x = PCT_FROM[7:0]; percent_y = PCT_FROM[7:0]; percent_z = PCT_FROM[7:0];
  228. motor_request_valid = 1'b1;
  229. @(negedge clk);
  230. motor_request_valid = 1'b0;
  231. // Wait until axes actually move to FROM and settle (do not treat
  232. // pre-start idle as parked — that under-counts the TO distance).
  233. begin : park
  234. integer t;
  235. integer saw_move;
  236. integer pos_from_x, pos_from_y, pos_from_z;
  237. saw_move = 0;
  238. pos_from_x = half_up_pos(travel_meas_x, PCT_FROM);
  239. pos_from_y = half_up_pos(travel_meas_y, PCT_FROM);
  240. pos_from_z = half_up_pos(travel_meas_z, PCT_FROM);
  241. for (t = 0; t < 2_000_000; t = t + 1) begin
  242. if ((t % 8) == 0) pulse_tick();
  243. if (x_moving || y_moving || z_moving) saw_move = 1;
  244. if (saw_move && !x_moving && !y_moving && !z_moving &&
  245. (dut.u_axis_x.current_position == pos_from_x) &&
  246. (dut.u_axis_y.current_position == pos_from_y) &&
  247. (dut.u_axis_z.current_position == pos_from_z))
  248. disable park;
  249. @(negedge clk);
  250. end
  251. $fatal(1, "park at FROM never settled pos=%0d/%0d/%0d want=%0d/%0d/%0d saw_move=%0d",
  252. dut.u_axis_x.current_position, dut.u_axis_y.current_position,
  253. dut.u_axis_z.current_position, pos_from_x, pos_from_y, pos_from_z, saw_move);
  254. end
  255. repeat (32) @(negedge clk);
  256. // Clear counters for the measured TO move
  257. rise_x = 0; rise_y = 0; rise_z = 0;
  258. first_x = -1; first_y = -1; first_z = -1;
  259. last_x = -1; last_y = -1; last_z = -1;
  260. done_x = -1; done_y = -1; done_z = -1;
  261. saw_overlap = 0;
  262. overlap_rises_x = 0; overlap_rises_y = 0; overlap_rises_z = 0;
  263. cycle = 0;
  264. @(negedge clk);
  265. percent_x = PCT_TO[7:0]; percent_y = PCT_TO[7:0]; percent_z = PCT_TO[7:0];
  266. motor_request_valid = 1'b1;
  267. @(negedge clk);
  268. motor_request_valid = 1'b0;
  269. measure_en = 1'b1;
  270. begin : run_move
  271. integer t;
  272. for (t = 0; t < 4_000_000; t = t + 1) begin
  273. if ((t % 8) == 0) pulse_tick();
  274. if (done_x >= 0 && done_y >= 0 && done_z >= 0 &&
  275. !x_moving && !y_moving && !z_moving && cycle > 100)
  276. disable run_move;
  277. @(negedge clk);
  278. end
  279. end
  280. measure_en = 1'b0;
  281. if (!saw_overlap)
  282. $fatal(1, "never saw x&&y&&z moving in same task");
  283. if (overlap_rises_x < 2 || overlap_rises_y < 2 || overlap_rises_z < 2)
  284. $fatal(1, "overlap-window gated rises x=%0d y=%0d z=%0d (need >=2 each)",
  285. overlap_rises_x, overlap_rises_y, overlap_rises_z);
  286. if (rise_x !== exp_x || rise_y !== exp_y || rise_z !== exp_z)
  287. $fatal(1, "gated rise distance mismatch got %0d/%0d/%0d exp %0d/%0d/%0d",
  288. rise_x, rise_y, rise_z, exp_x, exp_y, exp_z);
  289. if (done_x < 0 || done_y < 0 || done_z < 0)
  290. $fatal(1, "missing done times %0d/%0d/%0d", done_x, done_y, done_z);
  291. // Shortest travel finishes first; remaining axes keep moving after that.
  292. if (!(done_x < done_y) || !(done_x < done_z))
  293. $fatal(1, "fastest (X) did not finish before others: %0d/%0d/%0d",
  294. done_x, done_y, done_z);
  295. // Travel-normalized duration = last_rise - first_rise (excludes serial
  296. // planner grant stagger). Wall-clock last-rise may differ by that stagger.
  297. begin : skew
  298. integer span_x, span_y, span_z, max_span, span_budget;
  299. integer max_first, min_first, planner_bias;
  300. integer max_last, wall_budget;
  301. span_x = last_x - first_x;
  302. span_y = last_y - first_y;
  303. span_z = last_z - first_z;
  304. max_span = span_x;
  305. if (span_y > max_span) max_span = span_y;
  306. if (span_z > max_span) max_span = span_z;
  307. span_budget = (max_span * 2 + 99) / 100;
  308. if (span_budget < (SVC * 64)) span_budget = SVC * 64;
  309. if (((max_span - span_x) > span_budget) ||
  310. ((max_span - span_y) > span_budget) ||
  311. ((max_span - span_z) > span_budget))
  312. $fatal(1, "duration skew too large span=%0d/%0d/%0d max=%0d budget=%0d",
  313. span_x, span_y, span_z, max_span, span_budget);
  314. max_first = first_x;
  315. if (first_y > max_first) max_first = first_y;
  316. if (first_z > max_first) max_first = first_z;
  317. min_first = first_x;
  318. if (first_y < min_first) min_first = first_y;
  319. if (first_z < min_first) min_first = first_z;
  320. planner_bias = max_first - min_first;
  321. max_last = last_x;
  322. if (last_y > max_last) max_last = last_y;
  323. if (last_z > max_last) max_last = last_z;
  324. wall_budget = (max_last * 2 + 99) / 100 + planner_bias + (SVC * 64);
  325. if (((max_last - last_x) > wall_budget) ||
  326. ((max_last - last_y) > wall_budget) ||
  327. ((max_last - last_z) > wall_budget))
  328. $fatal(1, "wall last-rise skew too large last=%0d/%0d/%0d max=%0d budget=%0d bias=%0d",
  329. last_x, last_y, last_z, max_last, wall_budget, planner_bias);
  330. max_done = max_last;
  331. budget = wall_budget;
  332. end
  333. $display("door_travel_normalized_timing_tb PASS rises=%0d/%0d/%0d exp=%0d/%0d/%0d done=%0d/%0d/%0d ov_rises=%0d/%0d/%0d first=%0d/%0d/%0d last=%0d/%0d/%0d travel=%0d/%0d/%0d",
  334. rise_x, rise_y, rise_z, exp_x, exp_y, exp_z,
  335. done_x, done_y, done_z,
  336. overlap_rises_x, overlap_rises_y, overlap_rises_z,
  337. first_x, first_y, first_z, last_x, last_y, last_z,
  338. dut.u_axis_x.status_travel, dut.u_axis_y.status_travel, dut.u_axis_z.status_travel);
  339. $finish;
  340. end
  341. endmodule