pwm_commit_handshake_tb.v 16 KB

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