door_axis_request_limit_race_tb.v 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. `timescale 1ns/1ps
  2. // DUT: same-cycle single-limit + host request must snap position before replan.
  3. // Covers close/open mirrors, STEP high/low, heartbeat vs material target,
  4. // limit hold then release, and peer-axis independence.
  5. module door_axis_request_limit_race_tb;
  6. reg clk = 1'b0;
  7. always #1 clk = ~clk;
  8. // Axis 0 under test
  9. reg rst_n = 1'b0;
  10. reg open_limit = 1'b0;
  11. reg close_limit = 1'b1;
  12. reg limit_sync_valid = 1'b0;
  13. reg gated_step = 1'b0;
  14. reg gated_step_rise = 1'b0;
  15. reg tick_500us = 1'b0;
  16. reg request_valid = 1'b0;
  17. reg [7:0] requested_percent_in = 8'd0;
  18. reg watchdog_stop = 1'b0;
  19. wire planner_req;
  20. wire [7:0] planner_percent;
  21. wire [12:0] planner_travel;
  22. reg planner_done = 1'b0;
  23. reg [1:0] planner_done_axis = 2'd0;
  24. reg [7:0] planner_done_percent = 8'd0;
  25. reg [12:0] planner_result = 13'd0;
  26. wire step;
  27. wire dir_close;
  28. wire [2:0] status_state;
  29. wire [7:0] status_flags;
  30. wire [7:0] status_requested_percent;
  31. wire [12:0] status_current;
  32. wire [12:0] status_target;
  33. wire [12:0] status_travel;
  34. wire [7:0] status_fault_code;
  35. wire calibrated;
  36. wire faulted;
  37. wire terminal;
  38. // Peer axis 1 (must stay undisturbed when axis0 hits limit)
  39. reg open_limit_b = 1'b0;
  40. reg close_limit_b = 1'b1;
  41. reg gated_step_b = 1'b0;
  42. reg gated_step_rise_b = 1'b0;
  43. reg request_valid_b = 1'b0;
  44. reg [7:0] requested_percent_in_b = 8'd0;
  45. wire planner_req_b;
  46. wire [7:0] planner_percent_b;
  47. wire [12:0] planner_travel_b;
  48. reg planner_done_b = 1'b0;
  49. reg [1:0] planner_done_axis_b = 2'd1;
  50. reg [7:0] planner_done_percent_b = 8'd0;
  51. reg [12:0] planner_result_b = 13'd0;
  52. wire step_b;
  53. wire dir_close_b;
  54. wire [2:0] status_state_b;
  55. wire [7:0] status_flags_b;
  56. wire [7:0] status_requested_percent_b;
  57. wire [12:0] status_current_b;
  58. wire [12:0] status_target_b;
  59. wire [12:0] status_travel_b;
  60. wire [7:0] status_fault_code_b;
  61. wire calibrated_b;
  62. wire faulted_b;
  63. wire terminal_b;
  64. localparam [2:0] ST_IDLE = 3'd2;
  65. localparam [2:0] ST_MOVE_OPEN = 3'd3;
  66. localparam [2:0] ST_MOVE_CLOSE = 3'd4;
  67. localparam [2:0] ST_WAIT_REPLAN = 3'd5;
  68. localparam integer TRAVEL = 100;
  69. door_axis_controller #(
  70. .AXIS_ID(0),
  71. .MAX_CAL_STEPS(256),
  72. .MIN_TRAVEL(2),
  73. .ENDPOINT_GUARD_EXTRA(8),
  74. .DIR_SETUP_TICKS(1),
  75. .PERCENT_MAX(100)
  76. ) dut0 (
  77. .clk(clk), .rst_n(rst_n),
  78. .open_limit(open_limit), .close_limit(close_limit),
  79. .limit_sync_valid(limit_sync_valid),
  80. .cal_enable(1'b1),
  81. .gated_step(gated_step), .gated_step_rise(gated_step_rise),
  82. .tick_500us(tick_500us),
  83. .step_toggle_pulse(tick_500us),
  84. .rate_quantum_in(21'd0),
  85. .rate_quantum_valid(1'b0),
  86. .request_valid(request_valid),
  87. .requested_percent_in(requested_percent_in),
  88. .watchdog_stop(watchdog_stop),
  89. .planner_req(planner_req),
  90. .planner_percent(planner_percent),
  91. .planner_travel(planner_travel),
  92. .planner_done(planner_done),
  93. .planner_done_axis(planner_done_axis),
  94. .planner_done_percent(planner_done_percent),
  95. .planner_result(planner_result),
  96. .step(step), .dir_close(dir_close),
  97. .status_state(status_state), .status_flags(status_flags),
  98. .status_requested_percent(status_requested_percent),
  99. .status_current(status_current), .status_target(status_target),
  100. .status_travel(status_travel), .status_fault_code(status_fault_code),
  101. .calibrated(calibrated), .faulted(faulted), .terminal(terminal),
  102. .axis_moving(), .axis_allow_rise(), .axis_in_calibration(), .axis_endpoint_crawl(),
  103. .axis_dda_run(),
  104. .profile_level(), .profile_distance(), .profile_done_steps(), .profile_ramp_unit(),
  105. .rate_quantum()
  106. );
  107. door_axis_controller #(
  108. .AXIS_ID(1),
  109. .MAX_CAL_STEPS(256),
  110. .MIN_TRAVEL(2),
  111. .ENDPOINT_GUARD_EXTRA(8),
  112. .DIR_SETUP_TICKS(1),
  113. .PERCENT_MAX(100)
  114. ) dut1 (
  115. .clk(clk), .rst_n(rst_n),
  116. .open_limit(open_limit_b), .close_limit(close_limit_b),
  117. .limit_sync_valid(limit_sync_valid),
  118. .cal_enable(1'b1),
  119. .gated_step(gated_step_b), .gated_step_rise(gated_step_rise_b),
  120. .tick_500us(tick_500us),
  121. .step_toggle_pulse(tick_500us),
  122. .rate_quantum_in(21'd0),
  123. .rate_quantum_valid(1'b0),
  124. .request_valid(request_valid_b),
  125. .requested_percent_in(requested_percent_in_b),
  126. .watchdog_stop(1'b0),
  127. .planner_req(planner_req_b),
  128. .planner_percent(planner_percent_b),
  129. .planner_travel(planner_travel_b),
  130. .planner_done(planner_done_b),
  131. .planner_done_axis(planner_done_axis_b),
  132. .planner_done_percent(planner_done_percent_b),
  133. .planner_result(planner_result_b),
  134. .step(step_b), .dir_close(dir_close_b),
  135. .status_state(status_state_b), .status_flags(status_flags_b),
  136. .status_requested_percent(status_requested_percent_b),
  137. .status_current(status_current_b), .status_target(status_target_b),
  138. .status_travel(status_travel_b), .status_fault_code(status_fault_code_b),
  139. .calibrated(calibrated_b), .faulted(faulted_b), .terminal(terminal_b),
  140. .axis_moving(), .axis_allow_rise(), .axis_in_calibration(), .axis_endpoint_crawl(),
  141. .axis_dda_run(),
  142. .profile_level(), .profile_distance(), .profile_done_steps(), .profile_ramp_unit(),
  143. .rate_quantum()
  144. );
  145. // Half-up mid percent → steps (matches door_percent_planner contract).
  146. function automatic [12:0] pct_to_steps;
  147. input [7:0] pct;
  148. input [12:0] travel;
  149. reg [31:0] num;
  150. begin
  151. if (pct == 8'd0)
  152. pct_to_steps = 13'd0;
  153. else if (pct >= 8'd100)
  154. pct_to_steps = travel;
  155. else begin
  156. num = (travel * pct) + 32'd50;
  157. pct_to_steps = num / 32'd100;
  158. if (pct_to_steps < 13'd1)
  159. pct_to_steps = 13'd1;
  160. if (pct_to_steps >= travel)
  161. pct_to_steps = travel - 13'd1;
  162. end
  163. end
  164. endfunction
  165. always @(posedge clk) begin
  166. planner_done <= 1'b0;
  167. if (planner_req) begin
  168. planner_done <= 1'b1;
  169. planner_done_axis <= 2'd0;
  170. planner_done_percent <= planner_percent;
  171. planner_result <= pct_to_steps(planner_percent, planner_travel);
  172. end
  173. planner_done_b <= 1'b0;
  174. if (planner_req_b) begin
  175. planner_done_b <= 1'b1;
  176. planner_done_axis_b <= 2'd1;
  177. planner_done_percent_b <= planner_percent_b;
  178. planner_result_b <= pct_to_steps(planner_percent_b, planner_travel_b);
  179. end
  180. end
  181. task pulse_tick;
  182. begin
  183. @(negedge clk); tick_500us = 1'b1;
  184. @(negedge clk); tick_500us = 1'b0;
  185. end
  186. endtask
  187. task force_rise;
  188. begin
  189. @(negedge clk);
  190. gated_step = 1'b1;
  191. gated_step_rise = 1'b1;
  192. @(negedge clk);
  193. gated_step_rise = 1'b0;
  194. gated_step = 1'b0;
  195. end
  196. endtask
  197. task force_rise_b;
  198. begin
  199. @(negedge clk);
  200. gated_step_b = 1'b1;
  201. gated_step_rise_b = 1'b1;
  202. @(negedge clk);
  203. gated_step_rise_b = 1'b0;
  204. gated_step_b = 1'b0;
  205. end
  206. endtask
  207. task calibrate_axis0;
  208. integer i;
  209. begin
  210. @(negedge clk);
  211. open_limit = 1'b0;
  212. close_limit = 1'b1;
  213. limit_sync_valid = 1'b1;
  214. // Already on close → measure-open.
  215. // TRAVEL-1 rises then open_limit+rise ⇒ travel_steps = TRAVEL.
  216. repeat (4) pulse_tick();
  217. for (i = 0; i < (TRAVEL - 1); i = i + 1) begin
  218. force_rise();
  219. pulse_tick();
  220. end
  221. @(negedge clk);
  222. open_limit = 1'b1;
  223. close_limit = 1'b0;
  224. gated_step = 1'b1;
  225. gated_step_rise = 1'b1;
  226. @(negedge clk);
  227. gated_step = 1'b0;
  228. gated_step_rise = 1'b0;
  229. repeat (8) @(negedge clk);
  230. if (!calibrated)
  231. $fatal(1, "axis0 cal failed");
  232. if (status_travel !== TRAVEL)
  233. $fatal(1, "axis0 travel=%0d want %0d", status_travel, TRAVEL);
  234. open_limit = 1'b0;
  235. close_limit = 1'b0;
  236. end
  237. endtask
  238. task calibrate_axis1;
  239. integer i;
  240. begin
  241. @(negedge clk);
  242. open_limit_b = 1'b0;
  243. close_limit_b = 1'b1;
  244. repeat (4) pulse_tick();
  245. for (i = 0; i < (TRAVEL - 1); i = i + 1) begin
  246. force_rise_b();
  247. pulse_tick();
  248. end
  249. @(negedge clk);
  250. open_limit_b = 1'b1;
  251. close_limit_b = 1'b0;
  252. gated_step_b = 1'b1;
  253. gated_step_rise_b = 1'b1;
  254. @(negedge clk);
  255. gated_step_b = 1'b0;
  256. gated_step_rise_b = 1'b0;
  257. repeat (8) @(negedge clk);
  258. if (!calibrated_b)
  259. $fatal(1, "axis1 cal failed");
  260. open_limit_b = 1'b0;
  261. close_limit_b = 1'b0;
  262. end
  263. endtask
  264. task issue_request;
  265. input [7:0] pct;
  266. begin
  267. @(negedge clk);
  268. requested_percent_in = pct;
  269. request_valid = 1'b1;
  270. @(negedge clk);
  271. request_valid = 1'b0;
  272. end
  273. endtask
  274. task settle_motion;
  275. integer i;
  276. begin
  277. for (i = 0; i < 400; i = i + 1) begin
  278. pulse_tick();
  279. // Mirror STEP → gated when limits clear
  280. if (step && !open_limit && !close_limit) begin
  281. @(negedge clk);
  282. gated_step = 1'b1;
  283. gated_step_rise = 1'b1;
  284. @(negedge clk);
  285. gated_step_rise = 1'b0;
  286. gated_step = 1'b0;
  287. end else begin
  288. gated_step = 1'b0;
  289. gated_step_rise = 1'b0;
  290. end
  291. if ((status_state == ST_IDLE) && !step && !planner_req)
  292. i = 400;
  293. end
  294. // Drain residual high
  295. if (step) begin
  296. pulse_tick();
  297. gated_step = 1'b0;
  298. gated_step_rise = 1'b0;
  299. end
  300. repeat (4) @(negedge clk);
  301. end
  302. endtask
  303. task drive_to_percent;
  304. input [7:0] pct;
  305. begin
  306. issue_request(pct);
  307. settle_motion();
  308. if (status_current !== pct_to_steps(pct, status_travel))
  309. $fatal(1, "drive_to %0d: current=%0d want=%0d",
  310. pct, status_current, pct_to_steps(pct, status_travel));
  311. end
  312. endtask
  313. // Same-cycle limit + request; assert position snapped before planner uses it.
  314. task collide_close_and_request;
  315. input [7:0] pct;
  316. input step_high;
  317. begin
  318. if (step_high) begin
  319. // Force STEP high without advancing position
  320. @(negedge clk);
  321. // Enter a move briefly then freeze step high via force path:
  322. // simply set gated_step high with no rise while we poke step through
  323. // a mid motion tick. Easier: wait until step is high during motion.
  324. end
  325. @(negedge clk);
  326. if (step_high && !step) begin
  327. // Start a dummy close move to get STEP high, then inject collision
  328. requested_percent_in = 8'd80;
  329. request_valid = 1'b1;
  330. @(negedge clk);
  331. request_valid = 1'b0;
  332. begin : wait_high
  333. integer k;
  334. for (k = 0; k < 80; k = k + 1) begin
  335. pulse_tick();
  336. if (step && !open_limit && !close_limit) begin
  337. @(negedge clk);
  338. gated_step = step;
  339. gated_step_rise = 1'b0; // hold high, no rise this cycle
  340. k = 80;
  341. end
  342. end
  343. end
  344. if (!step)
  345. $fatal(1, "failed to reach STEP high before close collide");
  346. end else if (!step_high) begin
  347. // Ensure STEP low
  348. if (step) begin
  349. pulse_tick();
  350. gated_step = 1'b0;
  351. gated_step_rise = 1'b0;
  352. end
  353. repeat (2) @(negedge clk);
  354. if (step)
  355. $fatal(1, "precondition STEP low failed");
  356. end
  357. // Corrupt internal estimate away from true close endpoint, then
  358. // assert close_limit + new request same cycle (mechanical is at close).
  359. // Position was driven to 70 earlier; close_limit snaps to travel.
  360. @(negedge clk);
  361. close_limit = 1'b1;
  362. open_limit = 1'b0;
  363. requested_percent_in = pct;
  364. request_valid = 1'b1;
  365. if (step_high) begin
  366. gated_step = 1'b1;
  367. gated_step_rise = 1'b0;
  368. end else begin
  369. gated_step = 1'b0;
  370. gated_step_rise = 1'b0;
  371. end
  372. @(negedge clk);
  373. request_valid = 1'b0;
  374. // Position must already be snapped this cycle (visible after edge).
  375. if (status_current !== status_travel)
  376. $fatal(1, "close collide pct=%0d step_high=%0d: current=%0d want travel=%0d",
  377. pct, step_high, status_current, status_travel);
  378. if (status_state !== ST_WAIT_REPLAN && status_state !== ST_IDLE)
  379. $fatal(1, "close collide expected WAIT_REPLAN/IDLE, state=%0d", status_state);
  380. // Hold limit a few cycles then release; planner must still use travel.
  381. repeat (3) @(negedge clk);
  382. if (status_current !== status_travel)
  383. $fatal(1, "close limit hold lost snap: current=%0d", status_current);
  384. @(negedge clk);
  385. close_limit = 1'b0;
  386. settle_motion();
  387. if (status_current !== pct_to_steps(pct, status_travel))
  388. $fatal(1, "after close collide settle current=%0d want=%0d",
  389. status_current, pct_to_steps(pct, status_travel));
  390. end
  391. endtask
  392. task collide_open_and_request;
  393. input [7:0] pct;
  394. input step_high;
  395. begin
  396. if (step_high && !step) begin
  397. requested_percent_in = 8'd20;
  398. request_valid = 1'b1;
  399. @(negedge clk);
  400. request_valid = 1'b0;
  401. begin : wait_high_o
  402. integer k;
  403. for (k = 0; k < 80; k = k + 1) begin
  404. pulse_tick();
  405. if (step && !open_limit && !close_limit) begin
  406. @(negedge clk);
  407. gated_step = step;
  408. gated_step_rise = 1'b0;
  409. k = 80;
  410. end
  411. end
  412. end
  413. if (!step)
  414. $fatal(1, "failed to reach STEP high before open collide");
  415. end else if (!step_high) begin
  416. if (step) begin
  417. pulse_tick();
  418. gated_step = 1'b0;
  419. gated_step_rise = 1'b0;
  420. end
  421. repeat (2) @(negedge clk);
  422. if (step)
  423. $fatal(1, "open collide precondition STEP low failed");
  424. end
  425. @(negedge clk);
  426. open_limit = 1'b1;
  427. close_limit = 1'b0;
  428. requested_percent_in = pct;
  429. request_valid = 1'b1;
  430. if (step_high) begin
  431. gated_step = 1'b1;
  432. gated_step_rise = 1'b0;
  433. end else begin
  434. gated_step = 1'b0;
  435. gated_step_rise = 1'b0;
  436. end
  437. @(negedge clk);
  438. request_valid = 1'b0;
  439. if (status_current !== 13'd0)
  440. $fatal(1, "open collide pct=%0d step_high=%0d: current=%0d want 0",
  441. pct, step_high, status_current);
  442. repeat (3) @(negedge clk);
  443. if (status_current !== 13'd0)
  444. $fatal(1, "open limit hold lost snap");
  445. @(negedge clk);
  446. open_limit = 1'b0;
  447. settle_motion();
  448. if (status_current !== pct_to_steps(pct, status_travel))
  449. $fatal(1, "after open collide settle current=%0d want=%0d",
  450. status_current, pct_to_steps(pct, status_travel));
  451. end
  452. endtask
  453. integer i;
  454. reg [12:0] peer_pos_before;
  455. initial begin
  456. #2000000 $fatal(1, "door_axis_request_limit_race_tb timeout");
  457. end
  458. initial begin
  459. repeat (2) @(posedge clk);
  460. rst_n = 1'b1;
  461. calibrate_axis0();
  462. calibrate_axis1();
  463. // Peer at mid 40% undisturbed baseline
  464. @(negedge clk);
  465. requested_percent_in_b = 8'd40;
  466. request_valid_b = 1'b1;
  467. @(negedge clk);
  468. request_valid_b = 1'b0;
  469. for (i = 0; i < 400; i = i + 1) begin
  470. pulse_tick();
  471. if (step_b && !open_limit_b && !close_limit_b) begin
  472. @(negedge clk);
  473. gated_step_b = 1'b1;
  474. gated_step_rise_b = 1'b1;
  475. @(negedge clk);
  476. gated_step_rise_b = 1'b0;
  477. gated_step_b = 1'b0;
  478. end
  479. if ((status_state_b == ST_IDLE) && !step_b && !planner_req_b)
  480. i = 400;
  481. end
  482. peer_pos_before = status_current_b;
  483. // --- close_limit + material mid target, STEP low ---
  484. drive_to_percent(8'd70);
  485. collide_close_and_request(8'd50, 1'b0);
  486. // --- close_limit + material mid target, STEP high ---
  487. drive_to_percent(8'd70);
  488. collide_close_and_request(8'd50, 1'b1);
  489. // --- close_limit + same-percent heartbeat ---
  490. drive_to_percent(8'd70);
  491. collide_close_and_request(8'd70, 1'b0);
  492. // --- open_limit mirror, STEP low ---
  493. drive_to_percent(8'd30);
  494. collide_open_and_request(8'd50, 1'b0);
  495. // --- open_limit mirror, STEP high ---
  496. drive_to_percent(8'd30);
  497. collide_open_and_request(8'd50, 1'b1);
  498. // Peer axis must still be at its mid target (limit on axis0 only)
  499. if (status_current_b !== peer_pos_before)
  500. $fatal(1, "peer axis disturbed: current=%0d was=%0d",
  501. status_current_b, peer_pos_before);
  502. if (faulted_b)
  503. $fatal(1, "peer axis faulted");
  504. $display("door_axis_request_limit_race_tb PASS");
  505. $finish;
  506. end
  507. endmodule