axis_travel_monitor_tb.v 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. `timescale 1ns/1ps
  2. module axis_travel_monitor_tb;
  3. reg clk;
  4. reg reset_n;
  5. reg start;
  6. reg signed [31:0] current_position;
  7. reg signed [31:0] target_travel;
  8. reg [31:0] tolerance;
  9. reg limit_pos;
  10. reg limit_neg;
  11. wire signed [31:0] start_position;
  12. wire signed [31:0] target_position;
  13. wire signed [31:0] travel;
  14. wire busy;
  15. wire in_position;
  16. wire done;
  17. wire limit_hit;
  18. wire limit_pos_hit;
  19. wire limit_neg_hit;
  20. integer errors;
  21. integer checks;
  22. integer i;
  23. integer random_start;
  24. integer random_distance;
  25. integer random_tolerance;
  26. integer random_target;
  27. integer random_middle;
  28. reg [31:0] lfsr;
  29. axis_travel_monitor dut (
  30. .clk(clk),
  31. .reset_n(reset_n),
  32. .start(start),
  33. .current_position(current_position),
  34. .target_travel(target_travel),
  35. .tolerance(tolerance),
  36. .limit_pos(limit_pos),
  37. .limit_neg(limit_neg),
  38. .start_position(start_position),
  39. .target_position(target_position),
  40. .travel(travel),
  41. .busy(busy),
  42. .in_position(in_position),
  43. .done(done),
  44. .limit_hit(limit_hit),
  45. .limit_pos_hit(limit_pos_hit),
  46. .limit_neg_hit(limit_neg_hit)
  47. );
  48. // ------------------------------------------------------------
  49. // Clock
  50. // ------------------------------------------------------------
  51. initial clk = 0;
  52. always #5 clk = ~clk;
  53. // ------------------------------------------------------------
  54. // Reset
  55. // ------------------------------------------------------------
  56. task reset_dut;
  57. begin
  58. reset_n = 0;
  59. start = 0;
  60. current_position = 0;
  61. target_travel = 0;
  62. tolerance = 0;
  63. limit_pos = 0;
  64. limit_neg = 0;
  65. repeat (3)
  66. @(posedge clk);
  67. #1;
  68. reset_n = 1;
  69. @(posedge clk);
  70. #1;
  71. end
  72. endtask
  73. // ------------------------------------------------------------
  74. // Generate one-cycle START pulse
  75. // ------------------------------------------------------------
  76. task start_move;
  77. input integer position;
  78. input integer distance;
  79. input integer tolerance_value;
  80. begin
  81. current_position = position;
  82. target_travel = distance;
  83. tolerance = tolerance_value;
  84. limit_pos = 0;
  85. limit_neg = 0;
  86. @(negedge clk);
  87. start = 1;
  88. @(posedge clk);
  89. #1;
  90. start = 0;
  91. end
  92. endtask
  93. // ------------------------------------------------------------
  94. // Check helper
  95. // ------------------------------------------------------------
  96. task expect_value;
  97. input integer condition;
  98. input [8*80-1:0] message;
  99. begin
  100. checks = checks + 1;
  101. if (!condition) begin
  102. $display(
  103. "FAIL: %0s",
  104. message
  105. );
  106. errors = errors + 1;
  107. end
  108. end
  109. endtask
  110. // ------------------------------------------------------------
  111. // MAIN
  112. // ------------------------------------------------------------
  113. initial begin
  114. errors = 0;
  115. checks = 0;
  116. lfsr = 32'h1ACE_B00C;
  117. reset_dut();
  118. $display("");
  119. $display("============================================================");
  120. $display(" AXIS TRAVEL MONITOR AUTOMATED VERIFICATION");
  121. $display("============================================================");
  122. // ========================================================
  123. // TEST A
  124. // Positive travel
  125. // ========================================================
  126. $display("");
  127. $display("[A] Positive travel");
  128. start_move(
  129. 1000,
  130. 500,
  131. 5
  132. );
  133. expect_value(
  134. start_position == 1000,
  135. "start_position should be 1000"
  136. );
  137. expect_value(
  138. target_position == 1500,
  139. "target_position should be 1500"
  140. );
  141. expect_value(
  142. busy == 1,
  143. "positive move should be busy"
  144. );
  145. current_position = 1200;
  146. #1;
  147. expect_value(
  148. travel == 200,
  149. "travel should be 200"
  150. );
  151. expect_value(
  152. in_position == 0,
  153. "1200 must not be in target window"
  154. );
  155. current_position = 1494;
  156. #1;
  157. expect_value(
  158. in_position == 0,
  159. "1494 must be outside tolerance"
  160. );
  161. current_position = 1495;
  162. #1;
  163. expect_value(
  164. in_position == 1,
  165. "1495 must be inside tolerance"
  166. );
  167. @(posedge clk);
  168. #1;
  169. expect_value(
  170. done == 1,
  171. "positive travel should complete"
  172. );
  173. expect_value(
  174. busy == 0,
  175. "busy should clear after completion"
  176. );
  177. $display("PASS [A] Positive travel");
  178. // ========================================================
  179. // TEST B
  180. // Negative travel
  181. // ========================================================
  182. $display("");
  183. $display("[B] Negative travel");
  184. start_move(
  185. 3000,
  186. -500,
  187. 5
  188. );
  189. expect_value(
  190. start_position == 3000,
  191. "negative start_position"
  192. );
  193. expect_value(
  194. target_position == 2500,
  195. "negative target_position"
  196. );
  197. current_position = 2506;
  198. #1;
  199. expect_value(
  200. in_position == 0,
  201. "2506 must be outside tolerance"
  202. );
  203. current_position = 2505;
  204. #1;
  205. expect_value(
  206. in_position == 1,
  207. "2505 must be inside tolerance"
  208. );
  209. @(posedge clk);
  210. #1;
  211. expect_value(
  212. done == 1,
  213. "negative travel should complete"
  214. );
  215. expect_value(
  216. travel == -495,
  217. "negative travel calculation"
  218. );
  219. $display("PASS [B] Negative travel");
  220. // ========================================================
  221. // TEST C
  222. // Exact tolerance boundaries
  223. // ========================================================
  224. $display("");
  225. $display("[C] Position tolerance window");
  226. start_move(
  227. 1000,
  228. 500,
  229. 5
  230. );
  231. current_position = 1494;
  232. #1;
  233. expect_value(
  234. in_position == 0,
  235. "target-6 must fail"
  236. );
  237. current_position = 1495;
  238. #1;
  239. expect_value(
  240. in_position == 1,
  241. "target-5 must pass"
  242. );
  243. current_position = 1505;
  244. #1;
  245. expect_value(
  246. in_position == 1,
  247. "target+5 must pass"
  248. );
  249. current_position = 1506;
  250. #1;
  251. expect_value(
  252. in_position == 0,
  253. "target+6 must fail"
  254. );
  255. $display("PASS [C] Tolerance window");
  256. // ========================================================
  257. // TEST D
  258. // Positive limit during positive travel
  259. // ========================================================
  260. $display("");
  261. $display("[D] Positive limit");
  262. start_move(
  263. 1000,
  264. 2000,
  265. 5
  266. );
  267. current_position = 1300;
  268. limit_pos = 1;
  269. @(posedge clk);
  270. #1;
  271. expect_value(
  272. busy == 0,
  273. "positive limit must terminate travel"
  274. );
  275. expect_value(
  276. done == 0,
  277. "limit termination must not be normal DONE"
  278. );
  279. expect_value(
  280. limit_hit == 1,
  281. "limit_hit must latch"
  282. );
  283. expect_value(
  284. limit_pos_hit == 1,
  285. "positive limit flag must latch"
  286. );
  287. expect_value(
  288. limit_neg_hit == 0,
  289. "negative limit flag must remain clear"
  290. );
  291. limit_pos = 0;
  292. $display("PASS [D] Positive limit");
  293. // ========================================================
  294. // TEST E
  295. // Negative limit during negative travel
  296. // ========================================================
  297. $display("");
  298. $display("[E] Negative limit");
  299. start_move(
  300. 3000,
  301. -2000,
  302. 5
  303. );
  304. current_position = 2700;
  305. limit_neg = 1;
  306. @(posedge clk);
  307. #1;
  308. expect_value(
  309. busy == 0,
  310. "negative limit must terminate travel"
  311. );
  312. expect_value(
  313. done == 0,
  314. "negative limit must not generate DONE"
  315. );
  316. expect_value(
  317. limit_hit == 1,
  318. "limit_hit must latch on negative limit"
  319. );
  320. expect_value(
  321. limit_neg_hit == 1,
  322. "negative limit flag must latch"
  323. );
  324. limit_neg = 0;
  325. $display("PASS [E] Negative limit");
  326. // ========================================================
  327. // TEST F
  328. // Wrong-direction limit must not terminate movement
  329. // ========================================================
  330. $display("");
  331. $display("[F] Opposite limit immunity");
  332. start_move(
  333. 1000,
  334. 1000,
  335. 5
  336. );
  337. limit_neg = 1;
  338. @(posedge clk);
  339. #1;
  340. expect_value(
  341. busy == 1,
  342. "negative limit should not stop positive travel"
  343. );
  344. expect_value(
  345. limit_hit == 0,
  346. "wrong-direction limit must not latch"
  347. );
  348. limit_neg = 0;
  349. $display("PASS [F] Opposite limit immunity");
  350. // ========================================================
  351. // TEST G
  352. // Reset during travel
  353. // ========================================================
  354. $display("");
  355. $display("[G] Reset during active travel");
  356. start_move(
  357. 5000,
  358. 1000,
  359. 5
  360. );
  361. current_position = 5300;
  362. reset_n = 0;
  363. #1;
  364. expect_value(
  365. busy == 0,
  366. "reset must clear busy"
  367. );
  368. expect_value(
  369. done == 0,
  370. "reset must clear done"
  371. );
  372. expect_value(
  373. limit_hit == 0,
  374. "reset must clear limit_hit"
  375. );
  376. reset_n = 1;
  377. @(posedge clk);
  378. #1;
  379. $display("PASS [G] Reset");
  380. // ========================================================
  381. // TEST H
  382. // New start while previous travel is still active
  383. // ========================================================
  384. $display("");
  385. $display("[H] Restart / new segment");
  386. start_move(
  387. 1000,
  388. 1000,
  389. 5
  390. );
  391. current_position = 1300;
  392. // New travel begins from 1300.
  393. start_move(
  394. 1300,
  395. 200,
  396. 2
  397. );
  398. expect_value(
  399. start_position == 1300,
  400. "restart must capture new start position"
  401. );
  402. expect_value(
  403. target_position == 1500,
  404. "restart target should be 1500"
  405. );
  406. expect_value(
  407. travel == 0,
  408. "new segment travel must restart from zero"
  409. );
  410. expect_value(
  411. busy == 1,
  412. "new segment should be busy"
  413. );
  414. current_position = 1500;
  415. @(posedge clk);
  416. #1;
  417. expect_value(
  418. done == 1,
  419. "restarted travel should complete"
  420. );
  421. $display("PASS [H] Restart");
  422. // ========================================================
  423. // TEST I
  424. // Zero travel
  425. // ========================================================
  426. $display("");
  427. $display("[I] Zero-distance travel");
  428. start_move(
  429. 12345,
  430. 0,
  431. 0
  432. );
  433. expect_value(
  434. start_position == 12345,
  435. "zero travel start position"
  436. );
  437. expect_value(
  438. target_position == 12345,
  439. "zero travel target position"
  440. );
  441. expect_value(
  442. busy == 0,
  443. "zero travel must not remain busy"
  444. );
  445. expect_value(
  446. done == 1,
  447. "zero travel should complete immediately"
  448. );
  449. expect_value(
  450. in_position == 1,
  451. "zero travel should be in position"
  452. );
  453. $display("PASS [I] Zero-distance travel");
  454. // ========================================================
  455. // TEST J
  456. // Negative absolute positions
  457. // ========================================================
  458. $display("");
  459. $display("[J] Negative absolute positions");
  460. start_move(
  461. -5000,
  462. 750,
  463. 3
  464. );
  465. expect_value(
  466. target_position == -4250,
  467. "negative-coordinate target"
  468. );
  469. current_position = -4250;
  470. @(posedge clk);
  471. #1;
  472. expect_value(
  473. done == 1,
  474. "negative-coordinate move should complete"
  475. );
  476. expect_value(
  477. travel == 750,
  478. "travel across negative coordinates"
  479. );
  480. $display("PASS [J] Negative absolute positions");
  481. // ========================================================
  482. // TEST K
  483. // 500 deterministic pseudo-random travel segments
  484. // ========================================================
  485. $display("");
  486. $display("[K] 500 randomized travel segments");
  487. for (i = 0; i < 500; i = i + 1) begin
  488. // LFSR
  489. lfsr = {
  490. lfsr[30:0],
  491. lfsr[31] ^
  492. lfsr[21] ^
  493. lfsr[1] ^
  494. lfsr[0]
  495. };
  496. random_start =
  497. $signed(lfsr[15:0]);
  498. // Make distance between 50 and 1049.
  499. random_distance =
  500. 50 + (lfsr[25:16] % 1000);
  501. // Random direction.
  502. if (lfsr[26])
  503. random_distance = -random_distance;
  504. random_tolerance =
  505. lfsr[30:27] % 6;
  506. random_target =
  507. random_start +
  508. random_distance;
  509. random_middle =
  510. random_start +
  511. random_distance / 2;
  512. start_move(
  513. random_start,
  514. random_distance,
  515. random_tolerance
  516. );
  517. checks = checks + 3;
  518. if (start_position != random_start) begin
  519. $display(
  520. "FAIL RANDOM START: case=%0d expected=%0d actual=%0d",
  521. i,
  522. random_start,
  523. start_position
  524. );
  525. errors = errors + 1;
  526. end
  527. if (target_position != random_target) begin
  528. $display(
  529. "FAIL RANDOM TARGET: case=%0d expected=%0d actual=%0d",
  530. i,
  531. random_target,
  532. target_position
  533. );
  534. errors = errors + 1;
  535. end
  536. if (!busy) begin
  537. $display(
  538. "FAIL RANDOM BUSY: case=%0d",
  539. i
  540. );
  541. errors = errors + 1;
  542. end
  543. // Halfway should still be active.
  544. current_position = random_middle;
  545. @(posedge clk);
  546. #1;
  547. checks = checks + 1;
  548. if (!busy) begin
  549. $display(
  550. "FAIL RANDOM EARLY DONE: case=%0d",
  551. i
  552. );
  553. errors = errors + 1;
  554. end
  555. // Move exactly to target.
  556. current_position = random_target;
  557. @(posedge clk);
  558. #1;
  559. checks = checks + 3;
  560. if (!done) begin
  561. $display(
  562. "FAIL RANDOM DONE: case=%0d start=%0d distance=%0d target=%0d current=%0d",
  563. i,
  564. random_start,
  565. random_distance,
  566. random_target,
  567. current_position
  568. );
  569. errors = errors + 1;
  570. end
  571. if (busy) begin
  572. $display(
  573. "FAIL RANDOM BUSY CLEAR: case=%0d",
  574. i
  575. );
  576. errors = errors + 1;
  577. end
  578. if (!in_position) begin
  579. $display(
  580. "FAIL RANDOM IN_POSITION: case=%0d",
  581. i
  582. );
  583. errors = errors + 1;
  584. end
  585. if ((i % 100) == 0) begin
  586. $display(
  587. "Random progress: %0d / 500",
  588. i
  589. );
  590. end
  591. end
  592. $display("PASS [K] 500 randomized travel segments");
  593. // ========================================================
  594. // FINAL RESULT
  595. // ========================================================
  596. $display("");
  597. $display("============================================================");
  598. $display("TOTAL CHECKS : %0d", checks);
  599. $display("TOTAL ERRORS : %0d", errors);
  600. $display("============================================================");
  601. if (errors == 0) begin
  602. $display("");
  603. $display("ALL AXIS TRAVEL TESTS PASSED");
  604. $display("");
  605. $display("Verified:");
  606. $display(" - positive travel");
  607. $display(" - negative travel");
  608. $display(" - start-position capture");
  609. $display(" - relative travel calculation");
  610. $display(" - target-position calculation");
  611. $display(" - tolerance / in-position window");
  612. $display(" - positive limit");
  613. $display(" - negative limit");
  614. $display(" - opposite-limit handling");
  615. $display(" - reset");
  616. $display(" - restart during travel");
  617. $display(" - zero-distance travel");
  618. $display(" - negative coordinates");
  619. $display(" - 500 randomized travel segments");
  620. $display("");
  621. $display("============================================================");
  622. $finish;
  623. end else begin
  624. $display("");
  625. $display(
  626. "AXIS TRAVEL TEST FAILED: %0d error(s)",
  627. errors
  628. );
  629. $display("============================================================");
  630. $fatal(1);
  631. end
  632. end
  633. endmodule