door_axis_source_limit_linger_tb.v 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. `timescale 1ns/1ps
  2. // Round-12: source-endpoint limit lingering must NOT overwrite gated_step_rise ±1.
  3. // On broken RTL, continuous close_limit during MOVE_OPEN snaps position back each cycle.
  4. module door_axis_source_limit_linger_tb;
  5. reg clk = 1'b0;
  6. always #1 clk = ~clk;
  7. reg rst_n = 1'b0;
  8. reg open_limit = 1'b0;
  9. reg close_limit = 1'b0;
  10. reg limit_sync_valid = 1'b0;
  11. reg gated_step = 1'b0;
  12. reg gated_step_rise = 1'b0;
  13. reg tick_500us = 1'b0;
  14. reg request_valid = 1'b0;
  15. reg [7:0] requested_percent_in = 8'd0;
  16. reg watchdog_stop = 1'b0;
  17. wire planner_req;
  18. wire [7:0] planner_percent;
  19. wire [12:0] planner_travel;
  20. reg planner_done = 1'b0;
  21. reg [1:0] planner_done_axis = 2'd0;
  22. reg [7:0] planner_done_percent = 8'd0;
  23. reg [12:0] planner_result = 13'd0;
  24. wire step;
  25. wire dir_close;
  26. wire [2:0] status_state;
  27. wire [7:0] status_flags;
  28. wire [7:0] status_requested_percent;
  29. wire [12:0] status_current;
  30. wire [12:0] status_target;
  31. wire [12:0] status_travel;
  32. wire [7:0] status_fault_code;
  33. wire calibrated;
  34. wire faulted;
  35. localparam [2:0] ST_IDLE = 3'd2;
  36. localparam [2:0] ST_MOVE_OPEN = 3'd3;
  37. localparam [2:0] ST_MOVE_CLOSE = 3'd4;
  38. localparam integer TRAVEL = 100;
  39. reg auto_planner = 1'b0;
  40. reg planner_req_q = 1'b0;
  41. integer gated_rise_total;
  42. integer i;
  43. door_axis_controller #(
  44. .AXIS_ID(0), .MAX_CAL_STEPS(256), .MIN_TRAVEL(2),
  45. .ENDPOINT_GUARD_EXTRA(8), .DIR_SETUP_TICKS(1), .PERCENT_MAX(100)
  46. ) dut (
  47. .clk(clk), .rst_n(rst_n),
  48. .open_limit(open_limit), .close_limit(close_limit),
  49. .limit_sync_valid(limit_sync_valid),
  50. .cal_enable(1'b1),
  51. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  52. .tick_500us(tick_500us),
  53. .step_toggle_pulse(tick_500us),
  54. .rate_quantum_in(21'd0),
  55. .rate_quantum_valid(1'b0),
  56. .request_valid(request_valid),
  57. .requested_percent_in(requested_percent_in),
  58. .watchdog_stop(watchdog_stop),
  59. .planner_req(planner_req),
  60. .planner_percent(planner_percent),
  61. .planner_travel(planner_travel),
  62. .planner_done(planner_done),
  63. .planner_done_axis(planner_done_axis),
  64. .planner_done_percent(planner_done_percent),
  65. .planner_result(planner_result),
  66. .step(step), .dir_close(dir_close),
  67. .status_state(status_state), .status_flags(status_flags),
  68. .status_requested_percent(status_requested_percent),
  69. .status_current(status_current), .status_target(status_target),
  70. .status_travel(status_travel), .status_fault_code(status_fault_code),
  71. .calibrated(calibrated), .faulted(faulted), .terminal()
  72. );
  73. function automatic [12:0] pct_to_steps;
  74. input [7:0] pct;
  75. input [12:0] travel;
  76. reg [31:0] num;
  77. begin
  78. if (pct == 8'd0)
  79. pct_to_steps = 13'd0;
  80. else if (pct >= 8'd100)
  81. pct_to_steps = travel;
  82. else begin
  83. num = (travel * pct) + 32'd50;
  84. pct_to_steps = num / 32'd100;
  85. if (pct_to_steps < 13'd1)
  86. pct_to_steps = 13'd1;
  87. if (pct_to_steps >= travel)
  88. pct_to_steps = travel - 13'd1;
  89. end
  90. end
  91. endfunction
  92. always @(posedge clk) begin
  93. if (auto_planner) begin
  94. planner_done <= 1'b0;
  95. if (planner_req_q) begin
  96. planner_done <= 1'b1;
  97. planner_done_axis <= 2'd0;
  98. planner_done_percent <= planner_percent;
  99. planner_result <= pct_to_steps(planner_percent, planner_travel);
  100. end
  101. end
  102. planner_req_q <= planner_req;
  103. end
  104. task pulse_tick;
  105. begin
  106. @(negedge clk); tick_500us = 1'b1;
  107. @(negedge clk); tick_500us = 1'b0;
  108. end
  109. endtask
  110. task force_rise;
  111. begin
  112. @(negedge clk);
  113. gated_step = 1'b1;
  114. gated_step_rise = 1'b1;
  115. @(negedge clk);
  116. gated_step_rise = 1'b0;
  117. gated_step = 1'b0;
  118. end
  119. endtask
  120. task calibrate_axis;
  121. integer k;
  122. begin
  123. open_limit = 1'b0;
  124. close_limit = 1'b1;
  125. limit_sync_valid = 1'b1;
  126. repeat (4) pulse_tick();
  127. for (k = 0; k < (TRAVEL - 1); k = k + 1) begin
  128. force_rise();
  129. pulse_tick();
  130. end
  131. @(negedge clk);
  132. open_limit = 1'b1;
  133. close_limit = 1'b0;
  134. gated_step = 1'b1;
  135. gated_step_rise = 1'b1;
  136. @(negedge clk);
  137. gated_step = 1'b0;
  138. gated_step_rise = 1'b0;
  139. repeat (8) @(negedge clk);
  140. if (!calibrated)
  141. $fatal(1, "calibrate_axis failed");
  142. open_limit = 1'b0;
  143. close_limit = 1'b0;
  144. end
  145. endtask
  146. task setup_at_close_endpoint;
  147. integer k;
  148. begin
  149. auto_planner = 1'b1;
  150. open_limit = 1'b0;
  151. close_limit = 1'b0;
  152. requested_percent_in = 8'd100;
  153. request_valid = 1'b1;
  154. @(negedge clk);
  155. request_valid = 1'b0;
  156. if (status_requested_percent !== 8'd100)
  157. $fatal(1, "setup: requested=%0d want 100", status_requested_percent);
  158. for (k = 0; k < 1400; k = k + 1) begin
  159. pulse_tick();
  160. if (step) begin
  161. @(negedge clk);
  162. gated_step = 1'b1;
  163. gated_step_rise = 1'b1;
  164. @(negedge clk);
  165. gated_step_rise = 1'b0;
  166. gated_step = 1'b0;
  167. end
  168. if (status_current >= (TRAVEL - 13'd3))
  169. close_limit = 1'b1;
  170. if ((status_state == ST_IDLE) && close_limit && (status_current == TRAVEL))
  171. k = 1400;
  172. if (faulted)
  173. $fatal(1, "setup_at_close fault fc=%0d cur=%0d", status_fault_code, status_current);
  174. end
  175. auto_planner = 1'b0;
  176. planner_done = 1'b0;
  177. if ((status_current !== TRAVEL) || (status_requested_percent !== 8'd100))
  178. $fatal(1, "setup_at_close: cur=%0d req=%0d", status_current, status_requested_percent);
  179. close_limit = 1'b1;
  180. open_limit = 1'b0;
  181. end
  182. endtask
  183. task depart_close_with_linger;
  184. input integer linger_rises;
  185. input [7:0] pct;
  186. reg [12:0] target;
  187. reg [12:0] expect_pos;
  188. integer rises;
  189. begin
  190. target = pct_to_steps(pct, TRAVEL);
  191. close_limit = 1'b0;
  192. auto_planner = 1'b0;
  193. planner_done = 1'b0;
  194. requested_percent_in = pct;
  195. request_valid = 1'b1;
  196. @(negedge clk);
  197. request_valid = 1'b0;
  198. for (i = 0; i < 80; i = i + 1) begin
  199. pulse_tick();
  200. if (planner_req)
  201. i = 80;
  202. end
  203. if (!planner_req)
  204. $fatal(1, "depart: planner_req never asserted");
  205. @(negedge clk);
  206. planner_done = 1'b1;
  207. planner_done_axis = 2'd0;
  208. planner_done_percent = pct;
  209. planner_result = target;
  210. @(negedge clk);
  211. planner_done = 1'b0;
  212. for (i = 0; i < 80; i = i + 1) begin
  213. pulse_tick();
  214. if (status_state == ST_MOVE_OPEN && !status_flags[4]) begin
  215. close_limit = 1'b1;
  216. i = 80;
  217. end
  218. if (faulted)
  219. $fatal(1, "depart: fault fc=%0d st=%0d ep=%0d",
  220. status_fault_code, status_state, status_flags[4]);
  221. end
  222. if (status_state !== ST_MOVE_OPEN || status_flags[4])
  223. $fatal(1, "depart: want mid MOVE_OPEN ep=0 got st=%0d ep=%0d",
  224. status_state, status_flags[4]);
  225. rises = 0;
  226. expect_pos = TRAVEL;
  227. gated_rise_total = 0;
  228. while (rises < linger_rises) begin
  229. pulse_tick();
  230. if (step) begin
  231. @(negedge clk);
  232. gated_step = 1'b1;
  233. gated_step_rise = 1'b1;
  234. @(negedge clk);
  235. gated_step_rise = 1'b0;
  236. gated_step = 1'b0;
  237. rises = rises + 1;
  238. gated_rise_total = gated_rise_total + 1;
  239. expect_pos = expect_pos - 13'd1;
  240. if (status_current !== expect_pos)
  241. $fatal(1,
  242. "close linger %0d rise %0d: pos=%0d want=%0d (snap overwrote step)",
  243. linger_rises, rises, status_current, expect_pos);
  244. end
  245. end
  246. close_limit = 1'b0;
  247. for (i = 0; i < 900; i = i + 1) begin
  248. pulse_tick();
  249. if (step && !open_limit && !close_limit) begin
  250. @(negedge clk);
  251. gated_step = 1'b1;
  252. gated_step_rise = 1'b1;
  253. @(negedge clk);
  254. gated_step_rise = 1'b0;
  255. gated_step = 1'b0;
  256. gated_rise_total = gated_rise_total + 1;
  257. end
  258. if ((status_state == ST_IDLE) && (status_current == target))
  259. i = 900;
  260. end
  261. auto_planner = 1'b0;
  262. planner_done = 1'b0;
  263. if (status_current !== target)
  264. $fatal(1, "close linger %0d: final pos=%0d target=%0d rises=%0d",
  265. linger_rises, status_current, target, gated_rise_total);
  266. if (gated_rise_total !== (TRAVEL - target))
  267. $fatal(1, "close linger %0d: total rises=%0d want=%0d",
  268. linger_rises, gated_rise_total, TRAVEL - target);
  269. end
  270. endtask
  271. task setup_at_open_endpoint;
  272. integer k;
  273. begin
  274. auto_planner = 1'b1;
  275. open_limit = 1'b0;
  276. close_limit = 1'b0;
  277. requested_percent_in = 8'd0;
  278. request_valid = 1'b1;
  279. @(negedge clk);
  280. request_valid = 1'b0;
  281. for (k = 0; k < 1200; k = k + 1) begin
  282. pulse_tick();
  283. if (step) begin
  284. @(negedge clk);
  285. gated_step = 1'b1;
  286. gated_step_rise = 1'b1;
  287. @(negedge clk);
  288. gated_step_rise = 1'b0;
  289. gated_step = 1'b0;
  290. end
  291. if (status_current <= 13'd3)
  292. open_limit = 1'b1;
  293. if ((status_state == ST_IDLE) && open_limit && (status_current == 13'd0))
  294. k = 1200;
  295. if (faulted)
  296. $fatal(1, "setup_at_open fault fc=%0d cur=%0d", status_fault_code, status_current);
  297. end
  298. auto_planner = 1'b0;
  299. planner_done = 1'b0;
  300. if (status_current !== 13'd0)
  301. $fatal(1, "setup_at_open: cur=%0d", status_current);
  302. open_limit = 1'b1;
  303. close_limit = 1'b0;
  304. end
  305. endtask
  306. task depart_open_with_linger;
  307. input integer linger_rises;
  308. input [7:0] pct;
  309. reg [12:0] target;
  310. reg [12:0] expect_pos;
  311. integer rises;
  312. begin
  313. target = pct_to_steps(pct, TRAVEL);
  314. open_limit = 1'b0;
  315. auto_planner = 1'b0;
  316. planner_done = 1'b0;
  317. requested_percent_in = pct;
  318. request_valid = 1'b1;
  319. @(negedge clk);
  320. request_valid = 1'b0;
  321. for (i = 0; i < 80; i = i + 1) begin
  322. pulse_tick();
  323. if (planner_req)
  324. i = 80;
  325. end
  326. if (!planner_req)
  327. $fatal(1, "open depart: planner_req never asserted");
  328. @(negedge clk);
  329. planner_done = 1'b1;
  330. planner_done_axis = 2'd0;
  331. planner_done_percent = pct;
  332. planner_result = target;
  333. @(negedge clk);
  334. planner_done = 1'b0;
  335. for (i = 0; i < 80; i = i + 1) begin
  336. pulse_tick();
  337. if (status_state == ST_MOVE_CLOSE && !status_flags[4]) begin
  338. open_limit = 1'b1;
  339. i = 80;
  340. end
  341. if (faulted)
  342. $fatal(1, "open depart fault fc=%0d", status_fault_code);
  343. end
  344. if (status_state !== ST_MOVE_CLOSE || status_flags[4])
  345. $fatal(1, "open depart: want mid MOVE_CLOSE got st=%0d ep=%0d",
  346. status_state, status_flags[4]);
  347. rises = 0;
  348. expect_pos = 13'd0;
  349. gated_rise_total = 0;
  350. while (rises < linger_rises) begin
  351. pulse_tick();
  352. if (step) begin
  353. @(negedge clk);
  354. gated_step = 1'b1;
  355. gated_step_rise = 1'b1;
  356. @(negedge clk);
  357. gated_step_rise = 1'b0;
  358. gated_step = 1'b0;
  359. rises = rises + 1;
  360. gated_rise_total = gated_rise_total + 1;
  361. expect_pos = expect_pos + 13'd1;
  362. if (status_current !== expect_pos)
  363. $fatal(1,
  364. "open linger %0d rise %0d: pos=%0d want=%0d (snap overwrote step)",
  365. linger_rises, rises, status_current, expect_pos);
  366. end
  367. end
  368. open_limit = 1'b0;
  369. for (i = 0; i < 900; i = i + 1) begin
  370. pulse_tick();
  371. if (step && !open_limit && !close_limit) begin
  372. @(negedge clk);
  373. gated_step = 1'b1;
  374. gated_step_rise = 1'b1;
  375. @(negedge clk);
  376. gated_step_rise = 1'b0;
  377. gated_step = 1'b0;
  378. gated_rise_total = gated_rise_total + 1;
  379. end
  380. if ((status_state == ST_IDLE) && (status_current == target))
  381. i = 900;
  382. end
  383. auto_planner = 1'b0;
  384. planner_done = 1'b0;
  385. if (status_current !== target)
  386. $fatal(1, "open linger %0d: final pos=%0d target=%0d",
  387. linger_rises, status_current, target);
  388. if (gated_rise_total !== target)
  389. $fatal(1, "open linger %0d: total rises=%0d want=%0d",
  390. linger_rises, gated_rise_total, target);
  391. end
  392. endtask
  393. initial begin
  394. repeat (200_000) @(negedge clk);
  395. $fatal(1, "door_axis_source_limit_linger_tb timeout");
  396. end
  397. initial begin
  398. gated_rise_total = 0;
  399. repeat (4) @(negedge clk);
  400. rst_n = 1'b1;
  401. calibrate_axis();
  402. setup_at_close_endpoint();
  403. depart_close_with_linger(1, 8'd50);
  404. gated_rise_total = 0;
  405. setup_at_close_endpoint();
  406. depart_close_with_linger(2, 8'd50);
  407. gated_rise_total = 0;
  408. setup_at_close_endpoint();
  409. depart_close_with_linger(4, 8'd50);
  410. gated_rise_total = 0;
  411. setup_at_close_endpoint();
  412. depart_close_with_linger(8, 8'd50);
  413. setup_at_open_endpoint();
  414. depart_open_with_linger(1, 8'd50);
  415. setup_at_open_endpoint();
  416. depart_open_with_linger(4, 8'd50);
  417. setup_at_open_endpoint();
  418. depart_open_with_linger(8, 8'd50);
  419. // same-percent heartbeat while lingering must not replan-stop motion
  420. setup_at_close_endpoint();
  421. close_limit = 1'b1;
  422. requested_percent_in = 8'd50;
  423. request_valid = 1'b1;
  424. auto_planner = 1'b1;
  425. @(negedge clk);
  426. request_valid = 1'b0;
  427. repeat (4) pulse_tick();
  428. requested_percent_in = 8'd50;
  429. request_valid = 1'b1;
  430. @(negedge clk);
  431. request_valid = 1'b0;
  432. if (status_state !== ST_MOVE_OPEN)
  433. $fatal(1, "same-percent heartbeat aborted linger move");
  434. repeat (6) begin
  435. pulse_tick();
  436. if (step) force_rise();
  437. end
  438. $display("door_axis_source_limit_linger_tb PASS");
  439. $finish;
  440. end
  441. endmodule