prod2u_p4_alt_config_startup_tb.v 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. `timescale 1ns/1ps
  2. // Alternate-config STARTUP behavior TB against REAL top.
  3. // Self-configures from dut.LIMIT_ACTIVE_HIGH / DIR_CLOSE_LEVEL / CLOSE_LIMIT_IS_LS.
  4. // Counts external STEP ball rising edges (not internal start/done alone).
  5. // Covers configured close already-hit, mid-search hit, found_close latch,
  6. // 5000 miss + 500 backoff, open-limit early stop, and per-axis independence.
  7. module prod2u_p4_alt_config_startup_tb;
  8. reg sys_clk = 1'b0;
  9. reg host_uart_rx = 1'b1;
  10. wire host_uart_tx;
  11. wire [7:0] ptc_pwm_out;
  12. wire [3:0] fan_pwm_out;
  13. wire motor_en_n;
  14. wire x_step, x_dir, y_step, y_dir, z_step, z_dir;
  15. reg x_limit_ls = 1'b0;
  16. reg x_limit_hs = 1'b0;
  17. reg y_limit_ls = 1'b0;
  18. reg y_limit_hs = 1'b0;
  19. reg z_limit_ls = 1'b0;
  20. reg z_limit_hs = 1'b0;
  21. tri temp_scl;
  22. tri temp_sda;
  23. localparam integer FAST_TICK_DIV = 10;
  24. localparam integer CLOSE_STEPS = 5000;
  25. localparam integer BACKOFF_STEPS = 500;
  26. localparam integer HOST_STEPS = 450;
  27. reg [31:0] fast_tick_div;
  28. reg fast_tick;
  29. integer x_steps, y_steps, z_steps;
  30. integer x_close_dir_steps, x_open_dir_steps;
  31. reg prev_x_step, prev_y_step, prev_z_step;
  32. reg counting;
  33. wire [2:0] AH = dut.LIMIT_ACTIVE_HIGH;
  34. wire [2:0] DCL = dut.DIR_CLOSE_LEVEL;
  35. wire [2:0] CLS = dut.CLOSE_LIMIT_IS_LS;
  36. always #1 sys_clk = ~sys_clk;
  37. prod2u_p4_normal_v3_top dut (
  38. .sys_clk(sys_clk), .host_uart_rx(host_uart_rx), .host_uart_tx(host_uart_tx),
  39. .ptc_pwm_out(ptc_pwm_out), .fan_pwm_out(fan_pwm_out),
  40. .motor_en_n(motor_en_n),
  41. .x_step(x_step), .x_dir(x_dir), .y_step(y_step), .y_dir(y_dir),
  42. .z_step(z_step), .z_dir(z_dir),
  43. .x_limit_ls(x_limit_ls), .x_limit_hs(x_limit_hs),
  44. .y_limit_ls(y_limit_ls), .y_limit_hs(y_limit_hs),
  45. .z_limit_ls(z_limit_ls), .z_limit_hs(z_limit_hs),
  46. .temp_scl(temp_scl), .temp_sda(temp_sda)
  47. );
  48. initial begin
  49. fast_tick = 1'b0;
  50. fast_tick_div = 0;
  51. force dut.tick_500us = fast_tick;
  52. force dut.watchdog_timeout_event = 1'b0;
  53. end
  54. always @(posedge sys_clk) begin
  55. if (fast_tick_div + 1 >= FAST_TICK_DIV) begin
  56. fast_tick_div <= 0;
  57. fast_tick <= 1'b1;
  58. end else begin
  59. fast_tick_div <= fast_tick_div + 1'b1;
  60. fast_tick <= 1'b0;
  61. end
  62. end
  63. always @(posedge sys_clk) begin
  64. prev_x_step <= x_step;
  65. prev_y_step <= y_step;
  66. prev_z_step <= z_step;
  67. if (counting) begin
  68. if (!prev_x_step && x_step) begin
  69. x_steps <= x_steps + 1;
  70. if (x_dir === DCL[0])
  71. x_close_dir_steps <= x_close_dir_steps + 1;
  72. else if (x_dir === ~DCL[0])
  73. x_open_dir_steps <= x_open_dir_steps + 1;
  74. end
  75. if (!prev_y_step && y_step)
  76. y_steps <= y_steps + 1;
  77. if (!prev_z_step && z_step)
  78. z_steps <= z_steps + 1;
  79. end
  80. end
  81. function automatic phys_inactive;
  82. input ah_bit;
  83. begin
  84. phys_inactive = ~ah_bit;
  85. end
  86. endfunction
  87. function automatic phys_active;
  88. input ah_bit;
  89. begin
  90. phys_active = ah_bit;
  91. end
  92. endfunction
  93. task drive_inactive_all;
  94. begin
  95. x_limit_ls = phys_inactive(AH[0]);
  96. x_limit_hs = phys_inactive(AH[0]);
  97. y_limit_ls = phys_inactive(AH[1]);
  98. y_limit_hs = phys_inactive(AH[1]);
  99. z_limit_ls = phys_inactive(AH[2]);
  100. z_limit_hs = phys_inactive(AH[2]);
  101. end
  102. endtask
  103. task set_close_phys;
  104. input [1:0] axis;
  105. input active;
  106. reg level;
  107. begin
  108. level = active ? phys_active(AH[axis]) : phys_inactive(AH[axis]);
  109. if (CLS[axis]) begin
  110. if (axis == 2'd0) x_limit_ls = level;
  111. else if (axis == 2'd1) y_limit_ls = level;
  112. else z_limit_ls = level;
  113. end else begin
  114. if (axis == 2'd0) x_limit_hs = level;
  115. else if (axis == 2'd1) y_limit_hs = level;
  116. else z_limit_hs = level;
  117. end
  118. end
  119. endtask
  120. task set_open_phys;
  121. input [1:0] axis;
  122. input active;
  123. reg level;
  124. begin
  125. level = active ? phys_active(AH[axis]) : phys_inactive(AH[axis]);
  126. if (CLS[axis]) begin
  127. if (axis == 2'd0) x_limit_hs = level;
  128. else if (axis == 2'd1) y_limit_hs = level;
  129. else z_limit_hs = level;
  130. end else begin
  131. if (axis == 2'd0) x_limit_ls = level;
  132. else if (axis == 2'd1) y_limit_ls = level;
  133. else z_limit_ls = level;
  134. end
  135. end
  136. endtask
  137. task drain_limit_sync;
  138. begin
  139. repeat (4) @(posedge sys_clk);
  140. end
  141. endtask
  142. task clear_step_counters;
  143. begin
  144. x_steps = 0;
  145. y_steps = 0;
  146. z_steps = 0;
  147. x_close_dir_steps = 0;
  148. x_open_dir_steps = 0;
  149. prev_x_step = 1'b0;
  150. prev_y_step = 1'b0;
  151. prev_z_step = 1'b0;
  152. end
  153. endtask
  154. task wait_startup_done;
  155. integer guard;
  156. begin
  157. guard = 0;
  158. while (!dut.startup_done) begin
  159. @(posedge sys_clk);
  160. guard = guard + 1;
  161. if (guard > 20000000)
  162. $fatal(1, "timeout waiting startup_done");
  163. end
  164. end
  165. endtask
  166. task pulse_por;
  167. begin
  168. // Cold start: release forces that might linger; rely on simulator POR.
  169. counting = 1'b0;
  170. clear_step_counters();
  171. // Force async reset via internal rst_n if present after sync window.
  172. // Top uses power-on reset counter; wait it out with inactive limits.
  173. drain_limit_sync();
  174. end
  175. endtask
  176. // ---- Scenario helpers that re-elaborate via $finish/$stop not available;
  177. // instead we use force on u_startup_close_seek.rst path by re-running from
  178. // a single initial with staged cases that each begin after POR settles,
  179. // then force found_close/state only when needed. Prefer limit pin control. ----
  180. task force_soft_restart;
  181. begin
  182. // Restart POR + startup FSM via the POR flop (drives sys_rst_n).
  183. // Caller owns limit pins BEFORE calling this; pins are not cleared here.
  184. counting = 1'b0;
  185. force dut.u_por.por_reset_n = 1'b0;
  186. force dut.u_por.por_count = 0;
  187. repeat (8) @(posedge sys_clk);
  188. clear_step_counters();
  189. // Count from the first post-reset STEP edge so 5000/500 totals stay exact.
  190. counting = 1'b1;
  191. release dut.u_por.por_count;
  192. release dut.u_por.por_reset_n;
  193. while (!dut.sys_rst_n) @(posedge sys_clk);
  194. while (!dut.limit_sync_valid) @(posedge sys_clk);
  195. end
  196. endtask
  197. task start_host_motor;
  198. input [2:0] mask;
  199. input [2:0] dir_mask;
  200. begin
  201. @(negedge sys_clk);
  202. force dut.host_motor_active_mask = mask;
  203. force dut.host_motor_dir_mask = dir_mask;
  204. force dut.host_motor_start = 1'b1;
  205. @(posedge sys_clk);
  206. @(negedge sys_clk);
  207. force dut.host_motor_start = 1'b0;
  208. end
  209. endtask
  210. task release_host_motor;
  211. begin
  212. release dut.host_motor_start;
  213. release dut.host_motor_active_mask;
  214. release dut.host_motor_dir_mask;
  215. end
  216. endtask
  217. task case_already_on_close;
  218. begin
  219. $display("CASE already_on_close");
  220. drive_inactive_all();
  221. set_close_phys(2'd0, 1'b1);
  222. set_close_phys(2'd1, 1'b1);
  223. set_close_phys(2'd2, 1'b1);
  224. force_soft_restart();
  225. wait_startup_done();
  226. counting = 1'b0;
  227. if (x_steps !== 0 || y_steps !== 0 || z_steps !== 0)
  228. $fatal(1, "already-on-close should be 0 steps, got x=%0d y=%0d z=%0d",
  229. x_steps, y_steps, z_steps);
  230. if (motor_en_n !== 1'b1)
  231. $fatal(1, "EN must stay permanent_high");
  232. $display("PASS_SECTION_ALREADY_ON_CLOSE");
  233. end
  234. endtask
  235. task case_mid_search_hit_no_backoff;
  236. integer hit_at;
  237. begin
  238. $display("CASE mid_search_hit_no_backoff");
  239. drive_inactive_all();
  240. force_soft_restart();
  241. hit_at = 40;
  242. while (x_close_dir_steps < hit_at) @(posedge sys_clk);
  243. if (x_dir !== DCL[0])
  244. $fatal(1, "close search DIR mismatch: got %b expect %b", x_dir, DCL[0]);
  245. set_close_phys(2'd0, 1'b1);
  246. set_close_phys(2'd1, 1'b1);
  247. set_close_phys(2'd2, 1'b1);
  248. drain_limit_sync();
  249. wait_startup_done();
  250. counting = 1'b0;
  251. if (x_open_dir_steps !== 0)
  252. $fatal(1, "mid-hit must not backoff, open_dir_steps=%0d", x_open_dir_steps);
  253. if (x_close_dir_steps >= CLOSE_STEPS)
  254. $fatal(1, "mid-hit should stop before 5000, got %0d", x_close_dir_steps);
  255. if (x_close_dir_steps < hit_at)
  256. $fatal(1, "mid-hit never reached hit_at, got %0d", x_close_dir_steps);
  257. $display("PASS_SECTION_MID_HIT steps=%0d", x_close_dir_steps);
  258. end
  259. endtask
  260. task case_brief_hit_latches_found_close;
  261. integer hit_at;
  262. begin
  263. $display("CASE brief_hit_latches_found_close");
  264. drive_inactive_all();
  265. force_soft_restart();
  266. hit_at = 25;
  267. while (x_close_dir_steps < hit_at) @(posedge sys_clk);
  268. set_close_phys(2'd0, 1'b1);
  269. set_close_phys(2'd1, 1'b1);
  270. set_close_phys(2'd2, 1'b1);
  271. drain_limit_sync();
  272. // Release immediately after sync — found_close must stay latched.
  273. drive_inactive_all();
  274. drain_limit_sync();
  275. wait_startup_done();
  276. counting = 1'b0;
  277. if (x_open_dir_steps !== 0)
  278. $fatal(1, "brief hit released but backoff ran: open=%0d", x_open_dir_steps);
  279. if (dut.u_startup_close_seek.found_close !== 3'b111)
  280. $fatal(1, "found_close not latched after brief hit: %b",
  281. dut.u_startup_close_seek.found_close);
  282. $display("PASS_SECTION_BRIEF_HIT_LATCH");
  283. end
  284. endtask
  285. task case_full_miss_then_backoff;
  286. begin
  287. $display("CASE full_miss_then_backoff");
  288. drive_inactive_all();
  289. force_soft_restart();
  290. // Stay inactive through entire close search + backoff.
  291. wait_startup_done();
  292. counting = 1'b0;
  293. if (x_close_dir_steps !== CLOSE_STEPS)
  294. $fatal(1, "expected exactly 5000 close steps, got %0d", x_close_dir_steps);
  295. if (x_open_dir_steps !== BACKOFF_STEPS)
  296. $fatal(1, "expected exactly 500 open backoff steps, got %0d", x_open_dir_steps);
  297. $display("PASS_SECTION_FULL_MISS_BACKOFF close=%0d open=%0d",
  298. x_close_dir_steps, x_open_dir_steps);
  299. end
  300. endtask
  301. task case_backoff_open_limit_early_stop;
  302. integer seen_open_dir;
  303. begin
  304. $display("CASE backoff_open_limit_early_stop");
  305. drive_inactive_all();
  306. force_soft_restart();
  307. // Wait until close search finishes (5000) and backoff begins.
  308. while (x_close_dir_steps < CLOSE_STEPS) @(posedge sys_clk);
  309. // Wait for DIR to switch to logical open.
  310. seen_open_dir = 0;
  311. while (seen_open_dir == 0) begin
  312. @(posedge sys_clk);
  313. if (x_dir === ~DCL[0] && x_step)
  314. seen_open_dir = 1;
  315. if (dut.startup_done)
  316. $fatal(1, "startup finished before backoff open DIR observed");
  317. end
  318. while (x_open_dir_steps < 30) @(posedge sys_clk);
  319. set_open_phys(2'd0, 1'b1);
  320. set_open_phys(2'd1, 1'b1);
  321. set_open_phys(2'd2, 1'b1);
  322. drain_limit_sync();
  323. wait_startup_done();
  324. counting = 1'b0;
  325. if (x_close_dir_steps !== CLOSE_STEPS)
  326. $fatal(1, "close search not 5000 before early open stop: %0d", x_close_dir_steps);
  327. if (x_open_dir_steps >= BACKOFF_STEPS)
  328. $fatal(1, "open limit did not early-stop backoff: %0d", x_open_dir_steps);
  329. if (x_open_dir_steps < 30)
  330. $fatal(1, "backoff never reached sample point: %0d", x_open_dir_steps);
  331. $display("PASS_SECTION_BACKOFF_EARLY_OPEN open_steps=%0d", x_open_dir_steps);
  332. end
  333. endtask
  334. task case_axis_independent_profiles;
  335. begin
  336. $display("CASE axis_independent_profiles");
  337. // X: already on close (0 steps)
  338. // Y: mid-hit during close search
  339. // Z: full miss → 5000+500
  340. drive_inactive_all();
  341. set_close_phys(2'd0, 1'b1);
  342. force_soft_restart();
  343. // Y hits after some steps; Z never; X stays on close.
  344. while (y_steps < 35) @(posedge sys_clk);
  345. set_close_phys(2'd1, 1'b1);
  346. drain_limit_sync();
  347. wait_startup_done();
  348. counting = 1'b0;
  349. if (x_steps !== 0)
  350. $fatal(1, "X should be 0 steps (pre-hit), got %0d", x_steps);
  351. if (y_steps >= CLOSE_STEPS)
  352. $fatal(1, "Y should mid-stop, got %0d", y_steps);
  353. if (y_steps < 35)
  354. $fatal(1, "Y never reached mid-hit, got %0d", y_steps);
  355. if (z_steps !== (CLOSE_STEPS + BACKOFF_STEPS))
  356. $fatal(1, "Z should be 5000+500, got %0d", z_steps);
  357. $display("PASS_SECTION_AXIS_INDEPENDENT x=%0d y=%0d z=%0d",
  358. x_steps, y_steps, z_steps);
  359. end
  360. endtask
  361. task case_host_jog_after_startup;
  362. begin
  363. $display("CASE host_jog_after_startup");
  364. drive_inactive_all();
  365. set_close_phys(2'd0, 1'b1);
  366. set_close_phys(2'd1, 1'b1);
  367. set_close_phys(2'd2, 1'b1);
  368. force_soft_restart();
  369. wait_startup_done();
  370. drive_inactive_all();
  371. drain_limit_sync();
  372. clear_step_counters();
  373. counting = 1'b1;
  374. start_host_motor(3'b001, 3'b001);
  375. while (!dut.u_motor_engine.done) @(posedge sys_clk);
  376. release_host_motor();
  377. counting = 1'b0;
  378. if (x_steps !== HOST_STEPS)
  379. $fatal(1, "post-startup host jog expected 450, got %0d", x_steps);
  380. if (motor_en_n !== 1'b1)
  381. $fatal(1, "EN disturbed after startup");
  382. $display("PASS_SECTION_HOST_JOG_AFTER_STARTUP");
  383. end
  384. endtask
  385. initial begin
  386. counting = 1'b0;
  387. clear_step_counters();
  388. drive_inactive_all();
  389. $display("ALT_STARTUP_MASKS AH=%b DCL=%b CLS=%b", AH, DCL, CLS);
  390. if (`P4_STARTUP_CLOSE_STEPS !== CLOSE_STEPS)
  391. $fatal(1, "P4_STARTUP_CLOSE_STEPS drift");
  392. if (`P4_STARTUP_BACKOFF_STEPS !== BACKOFF_STEPS)
  393. $fatal(1, "P4_STARTUP_BACKOFF_STEPS drift");
  394. // Let initial POR complete once with inactive limits, then drive cases
  395. // via soft restart (force rst_n).
  396. counting = 1'b1;
  397. repeat (2000) @(posedge sys_clk);
  398. counting = 1'b0;
  399. case_already_on_close();
  400. case_mid_search_hit_no_backoff();
  401. case_brief_hit_latches_found_close();
  402. case_full_miss_then_backoff();
  403. case_backoff_open_limit_early_stop();
  404. case_axis_independent_profiles();
  405. case_host_jog_after_startup();
  406. $display("PASS prod2u_p4_alt_config_startup_tb AH=%b DCL=%b CLS=%b", AH, DCL, CLS);
  407. $finish;
  408. end
  409. endmodule