pwm_commit_handshake_tb.v 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. `timescale 1ns/1ps
  2. // Production-top duty readback and accelerated watchdog recovery regression.
  3. //
  4. // Drives UART SET/GET, checks response headers and selected duty bytes, checks
  5. // duty-bank validity over ten accelerated expiry/recovery rounds, and samples
  6. // SSR1/2/4 at the beginning and end. Bad-CRC input is followed by eventual
  7. // expiry and a successful GET.
  8. //
  9. // Scope: this bench does not monitor the first PWM output transition, prove
  10. // period-boundary channel atomicity, calculate response CRCs, measure connector
  11. // duty, or drive SET/GET exactly on watchdog expiry. The bad-CRC case waits a
  12. // whole timeout after injection, so it does not prove the exact reload edge.
  13. // The independent connector and nominal-UART fixtures supply their stated
  14. // physical-pin, full-period-duty, CRC and complete-stop checks separately.
  15. module pwm_commit_handshake_tb;
  16. localparam integer CLKS_PER_BIT = 27; // 921600 baud at 25 MHz
  17. localparam integer BIT_CLKS = CLKS_PER_BIT;
  18. // 98-byte GET reply takes ~26,460 clocks at 921600 baud.
  19. // Set WD_CLKS = 35,000 (~1.4 ms) so replies finish cleanly before accelerated expiry.
  20. localparam integer WD_CLKS = 35000;
  21. reg clk;
  22. initial begin
  23. clk = 0;
  24. forever #20 clk = ~clk; // 25 MHz (40 ns)
  25. end
  26. reg host_ttl_rx;
  27. wire host_ttl_tx;
  28. wire [23:0] ptc_pwm_out;
  29. wire [3:0] front_fan_pwm_out;
  30. wire [3:0] rear_fan_pwm_out;
  31. wire temp_scl;
  32. wire temp_sda;
  33. wire ssr1_out, ssr2_out, ssr4_out;
  34. wire buzzer_out;
  35. pullup(temp_scl);
  36. pullup(temp_sda);
  37. prod2u_6400_v2_top #(
  38. .FAN_BOARD_INVERT_MASK(8'hFF),
  39. .WD_TIMEOUT_CLKS(WD_CLKS)
  40. ) u_dut (
  41. .sys_clk(clk),
  42. .host_ttl_rx(host_ttl_rx),
  43. .host_ttl_tx(host_ttl_tx),
  44. .host_rs232_rx(1'b1),
  45. .host_rs232_tx(),
  46. .bmc_rs232_rx(1'b1),
  47. .bmc_rs232_tx(),
  48. .ptc_pwm_out(ptc_pwm_out),
  49. .ptc_spare_zero(),
  50. .front_fan_pwm_out(front_fan_pwm_out),
  51. .rear_fan_pwm_out(rear_fan_pwm_out),
  52. .temp_scl(temp_scl),
  53. .temp_sda(temp_sda),
  54. .temp_scl_alt(),
  55. .temp_sda_alt(),
  56. .ssr1_out(ssr1_out),
  57. .ssr2_out(ssr2_out),
  58. .ssr4_out(ssr4_out),
  59. .ssr_unused_zero(),
  60. .estop_btn_in(1'b0),
  61. .hood_open_btn_in(1'b0),
  62. .hood_close_btn_in(1'b0),
  63. .buzzer_out(buzzer_out)
  64. );
  65. // CRC-16/CCITT-FALSE
  66. function [15:0] calc_crc;
  67. input integer count;
  68. integer i, b;
  69. reg [15:0] c;
  70. reg [7:0] d;
  71. begin
  72. c = 16'hFFFF;
  73. for (i = 2; i < count; i = i + 1) begin
  74. d = tx_buf[i];
  75. for (b = 7; b >= 0; b = b - 1)
  76. c = (c[15] ^ d[b]) ? ({c[14:0], 1'b0} ^ 16'h1021) : {c[14:0], 1'b0};
  77. end
  78. calc_crc = c;
  79. end
  80. endfunction
  81. reg [7:0] tx_buf [0:255];
  82. task uart_send_byte;
  83. input [7:0] val;
  84. integer b;
  85. begin
  86. host_ttl_rx = 1'b0;
  87. repeat (BIT_CLKS) @(posedge clk);
  88. for (b = 0; b < 8; b = b + 1) begin
  89. host_ttl_rx = val[b];
  90. repeat (BIT_CLKS) @(posedge clk);
  91. end
  92. host_ttl_rx = 1'b1;
  93. repeat (BIT_CLKS) @(posedge clk);
  94. end
  95. endtask
  96. task uart_send_frame;
  97. input integer n;
  98. integer i;
  99. reg [15:0] crc;
  100. begin
  101. crc = calc_crc(n);
  102. for (i = 0; i < n; i = i + 1) uart_send_byte(tx_buf[i]);
  103. uart_send_byte(crc[7:0]);
  104. uart_send_byte(crc[15:8]);
  105. end
  106. endtask
  107. task uart_send_bad_crc_frame;
  108. input integer n;
  109. integer i;
  110. reg [15:0] crc;
  111. begin
  112. crc = calc_crc(n) ^ 16'hA5A5; // corrupt CRC
  113. for (i = 0; i < n; i = i + 1) uart_send_byte(tx_buf[i]);
  114. uart_send_byte(crc[7:0]);
  115. uart_send_byte(crc[15:8]);
  116. end
  117. endtask
  118. // Receiver
  119. reg [7:0] rx_buf [0:255];
  120. integer rx_count;
  121. reg [3:0] rx_bit;
  122. reg [7:0] rx_shift;
  123. integer rx_clk_cnt;
  124. reg rx_busy;
  125. initial begin
  126. rx_busy = 0;
  127. rx_clk_cnt = 0;
  128. rx_count = 0;
  129. forever begin
  130. @(posedge clk);
  131. if (!rx_busy && host_ttl_tx === 1'b0) begin
  132. rx_busy = 1;
  133. rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1;
  134. rx_bit = 0;
  135. end else if (rx_busy) begin
  136. if (rx_clk_cnt == 0) begin
  137. if (rx_bit < 8) begin
  138. rx_shift[rx_bit] = host_ttl_tx;
  139. rx_bit = rx_bit + 1;
  140. rx_clk_cnt = BIT_CLKS - 1;
  141. end else begin
  142. rx_buf[rx_count] = rx_shift;
  143. rx_count = rx_count + 1;
  144. rx_busy = 0;
  145. end
  146. end else begin
  147. rx_clk_cnt = rx_clk_cnt - 1;
  148. end
  149. end
  150. end
  151. end
  152. task send_set_all_uniform;
  153. input [7:0] ptc_val;
  154. input [7:0] front_val;
  155. input [7:0] rear_val;
  156. integer i;
  157. begin
  158. tx_buf[0] = 8'hA5;
  159. tx_buf[1] = 8'h5A;
  160. tx_buf[2] = 8'h01;
  161. tx_buf[3] = 8'd36;
  162. for (i = 0; i < 24; i = i + 1) tx_buf[4 + i] = ptc_val;
  163. for (i = 0; i < 4; i = i + 1) tx_buf[28 + i] = front_val;
  164. for (i = 0; i < 4; i = i + 1) tx_buf[32 + i] = rear_val;
  165. tx_buf[36] = 100; tx_buf[37] = 0; tx_buf[38] = 100;
  166. tx_buf[39] = 1; // buzzer ON
  167. rx_count = 0;
  168. uart_send_frame(40);
  169. end
  170. endtask
  171. task send_get_all;
  172. begin
  173. tx_buf[0] = 8'hA5;
  174. tx_buf[1] = 8'h5A;
  175. tx_buf[2] = 8'h02;
  176. tx_buf[3] = 8'd0;
  177. rx_count = 0;
  178. uart_send_frame(4);
  179. end
  180. endtask
  181. task wait_for_set_ack;
  182. output ok;
  183. begin
  184. while (rx_count < 6) @(posedge clk);
  185. if (rx_buf[0] == 8'hA5 && rx_buf[1] == 8'h5A &&
  186. rx_buf[2] == 8'h81 && rx_buf[3] == 8'h00) begin
  187. ok = 1;
  188. end else begin
  189. ok = 0;
  190. $display("ERROR: Bad SET_ACK: %02h %02h %02h %02h", rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3]);
  191. end
  192. end
  193. endtask
  194. task wait_for_get_reply;
  195. output ok;
  196. begin
  197. while (rx_count < 102) @(posedge clk);
  198. if (rx_buf[0] == 8'hA5 && rx_buf[1] == 8'h5A && rx_buf[2] == 8'h82 &&
  199. rx_buf[6] == 8'hA5 && rx_buf[7] == 8'h5A && rx_buf[8] == 8'h42 &&
  200. rx_buf[9] == 8'd90) begin
  201. ok = 1;
  202. end else begin
  203. ok = 0;
  204. $display("ERROR: Bad GET reply (len=%0d)", rx_buf[9]);
  205. end
  206. end
  207. endtask
  208. integer failures = 0;
  209. integer round;
  210. reg ack_ok;
  211. initial begin
  212. host_ttl_rx = 1'b1;
  213. repeat (2000) @(posedge clk); // Allow POR (1024 cycles) to release sys_rst_n
  214. $display("=== START PWM Commit Handshake & Watchdog Test ===");
  215. // Test 1: SSR boot defaults zero
  216. if (ssr1_out !== 1'b0 || ssr2_out !== 1'b0 || ssr4_out !== 1'b0) begin
  217. $display("FAIL: SSRs not zero initially");
  218. failures = failures + 1;
  219. end else begin
  220. $display("PASS: SSR1/2/4 boot zero");
  221. end
  222. // Test 2: Initial defaults (PTC 0%, Fan 20%) via GET_ALL before any SET
  223. $display("--- Testing Initial Default Outputs via GET_ALL ---");
  224. send_get_all();
  225. wait_for_get_reply(ack_ok);
  226. if (!ack_ok) failures = failures + 1;
  227. else begin
  228. for (round = 0; round < 24; round = round + 1) begin
  229. if (rx_buf[10 + round] !== 8'd0) begin
  230. $display("FAIL: Default PTC[%0d] = %0d != 0", round, rx_buf[10 + round]);
  231. failures = failures + 1;
  232. end
  233. end
  234. for (round = 0; round < 8; round = round + 1) begin
  235. if (rx_buf[34 + round] !== 8'd20) begin
  236. $display("FAIL: Default FAN[%0d] = %0d != 20", round, rx_buf[34 + round]);
  237. failures = failures + 1;
  238. end
  239. end
  240. $display("PASS: Initial defaults confirmed (PTC 0%%, FAN 20%%)");
  241. end
  242. // Test 3: SET all channels to100 and check subsequent GET readback
  243. $display("--- Testing All-100 Duty Commit Readback ---");
  244. send_set_all_uniform(8'd100, 8'd100, 8'd100);
  245. wait_for_set_ack(ack_ok);
  246. if (!ack_ok) begin
  247. $display("FAIL: SET_ACK for All 100 failed");
  248. failures = failures + 1;
  249. end else begin
  250. $display("PASS: SET_ACK received for All 100");
  251. end
  252. send_get_all();
  253. wait_for_get_reply(ack_ok);
  254. if (!ack_ok) failures = failures + 1;
  255. else begin
  256. for (round = 0; round < 24; round = round + 1) begin
  257. if (rx_buf[10 + round] !== 8'd100) begin
  258. $display("FAIL: PTC[%0d] = %0d != 100", round, rx_buf[10 + round]);
  259. failures = failures + 1;
  260. end
  261. end
  262. for (round = 0; round < 8; round = round + 1) begin
  263. if (rx_buf[34 + round] !== 8'd100) begin
  264. $display("FAIL: FAN[%0d] = %0d != 100", round, rx_buf[34 + round]);
  265. failures = failures + 1;
  266. end
  267. end
  268. $display("PASS: Output confirmed 100%% on all channels");
  269. end
  270. // Test 4: Watchdog expiry to defaults & consecutive GET checks
  271. $display("--- Testing Expiry to Safe Defaults & Consecutive GETs ---");
  272. repeat (WD_CLKS + 50) @(posedge clk);
  273. if (u_dut.u_duty_bank.valid_r !== 1'b0) begin
  274. $display("FAIL: Duty bank not invalidated on expiry");
  275. failures = failures + 1;
  276. end else begin
  277. $display("PASS: Duty bank invalidated to safe defaults on expiry");
  278. end
  279. // GET 1: should return defaulted outputs
  280. send_get_all();
  281. wait_for_get_reply(ack_ok);
  282. if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin
  283. $display("FAIL: Post-expiry GET did not return default PTC 0 / FAN 20");
  284. failures = failures + 1;
  285. end else begin
  286. $display("PASS: Post-expiry GET returns PTC 0 / FAN 20 defaults");
  287. end
  288. // GET 2: consecutive GET must NOT restore pre-expiry 100% duty
  289. send_get_all();
  290. wait_for_get_reply(ack_ok);
  291. if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin
  292. $display("FAIL: Consecutive GET improperly restored old bank");
  293. failures = failures + 1;
  294. end else begin
  295. $display("PASS: Consecutive GET preserved default outputs");
  296. end
  297. // Test 5: Direct recovery with identical bytes SET A
  298. $display("--- Testing Direct Recovery with Identical SET A ---");
  299. send_set_all_uniform(8'd100, 8'd100, 8'd100);
  300. wait_for_set_ack(ack_ok);
  301. if (!ack_ok) begin
  302. $display("FAIL: SET_ACK on direct recovery failed");
  303. failures = failures + 1;
  304. end else begin
  305. $display("PASS: SET_ACK received on direct recovery");
  306. end
  307. send_get_all();
  308. wait_for_get_reply(ack_ok);
  309. if (!ack_ok || rx_buf[10] !== 8'd100 || rx_buf[34] !== 8'd100) begin
  310. $display("FAIL: Output not restored after direct SET A");
  311. failures = failures + 1;
  312. end else begin
  313. $display("PASS: Output 100%% restored after direct SET A");
  314. end
  315. // Test 6: Direct recovery with SET B (different values)
  316. $display("--- Testing SET B Recovery ---");
  317. repeat (WD_CLKS + 50) @(posedge clk); // let expire
  318. send_set_all_uniform(8'd50, 8'd30, 8'd70);
  319. wait_for_set_ack(ack_ok);
  320. if (!ack_ok) begin
  321. $display("FAIL: SET_ACK for SET B failed");
  322. failures = failures + 1;
  323. end else begin
  324. $display("PASS: SET_ACK received for SET B");
  325. end
  326. send_get_all();
  327. wait_for_get_reply(ack_ok);
  328. if (!ack_ok || rx_buf[10] !== 8'd50 || rx_buf[34] !== 8'd30 || rx_buf[38] !== 8'd70) begin
  329. $display("FAIL: SET B values not applied: PTC=%0d F=%0d R=%0d",
  330. rx_buf[10], rx_buf[34], rx_buf[38]);
  331. failures = failures + 1;
  332. end else begin
  333. $display("PASS: SET B output applied cleanly");
  334. end
  335. // Test 7: Bad CRC followed by eventual watchdog expiry and legal GET
  336. $display("--- Testing Bad CRC Input, Eventual Expiry and Recovery GET ---");
  337. // Frame with bad CRC
  338. tx_buf[0] = 8'hA5; tx_buf[1] = 8'h5A; tx_buf[2] = 8'h01; tx_buf[3] = 8'd35;
  339. for (round = 0; round < 35; round = round + 1) tx_buf[4 + round] = 8'd99;
  340. rx_count = 0;
  341. uart_send_bad_crc_frame(39);
  342. repeat (WD_CLKS + 50) @(posedge clk);
  343. send_get_all();
  344. wait_for_get_reply(ack_ok);
  345. if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin
  346. $display("FAIL: Output did not reach defaults after bad CRC and full timeout!");
  347. failures = failures + 1;
  348. end else begin
  349. $display("PASS: Defaults observed after bad CRC input and full timeout");
  350. end
  351. // Test 8: Accelerated Expiry & Direct Recovery Rounds
  352. $display("--- Testing Repeated Rounds of Accelerated Expiry & Direct Recovery ---");
  353. for (round = 1; round <= 10; round = round + 1) begin
  354. // Direct SET recovery (same values: 60%, 40%, 80%)
  355. send_set_all_uniform(8'd60, 8'd40, 8'd80);
  356. wait_for_set_ack(ack_ok);
  357. if (!ack_ok) begin
  358. $display("FAIL: Round %0d SET_ACK failed", round);
  359. failures = failures + 1;
  360. end
  361. if (u_dut.u_duty_bank.valid_r !== 1'b1) begin
  362. $display("FAIL: Round %0d duty bank not valid after commit", round);
  363. failures = failures + 1;
  364. end
  365. // Wait for watchdog expiry
  366. repeat (WD_CLKS + 50) @(posedge clk);
  367. if (u_dut.u_duty_bank.valid_r !== 1'b0) begin
  368. $display("FAIL: Round %0d duty bank not invalidated on expiry", round);
  369. failures = failures + 1;
  370. end
  371. if ({ssr4_out,ssr2_out,ssr1_out} !== 3'b0 || buzzer_out !== 1'b0) begin
  372. $display("FAIL: SSR or buzzer not zero at watchdog expiry");
  373. failures = failures + 1;
  374. end
  375. $display(" Progress: %0d/10 recovery rounds passed", round);
  376. end
  377. $display("PASS: All 10 accelerated recovery rounds passed");
  378. // Verify final state via full GET_ALL
  379. send_set_all_uniform(8'd75, 8'd45, 8'd65);
  380. wait_for_set_ack(ack_ok);
  381. send_get_all();
  382. wait_for_get_reply(ack_ok);
  383. if (!ack_ok || rx_buf[10] !== 8'd75 || rx_buf[34] !== 8'd45 || rx_buf[38] !== 8'd65) begin
  384. $display("FAIL: Final GET_ALL verification failed");
  385. failures = failures + 1;
  386. end else begin
  387. $display("PASS: Final GET_ALL matches applied SET");
  388. end
  389. // Final SSR check
  390. if (ssr1_out !== 1'b1 || ssr2_out !== 1'b0 || ssr4_out !== 1'b1) begin
  391. $display("FAIL: SSRs differ from100/0/100");
  392. failures = failures + 1;
  393. end else begin
  394. $display("PASS: SSRs match100/0/100 at final check");
  395. end
  396. #2000;
  397. if (failures == 0) begin
  398. $display("pwm_commit_handshake_tb PASS");
  399. end else begin
  400. $fatal(1, "pwm_commit_handshake_tb FAIL (%0d errors)", failures);
  401. end
  402. $finish;
  403. end
  404. initial begin
  405. #600000000; // 600 ms timeout
  406. $fatal(1, "pwm_commit_handshake_tb TIMEOUT");
  407. end
  408. endmodule