pwm_commit_handshake_tb.v 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. pullup(temp_scl);
  35. pullup(temp_sda);
  36. prod2u_6400_v2_top #(
  37. .FAN_BOARD_INVERT_MASK(8'hFF),
  38. .WD_TIMEOUT_CLKS(WD_CLKS)
  39. ) u_dut (
  40. .sys_clk(clk),
  41. .host_ttl_rx(host_ttl_rx),
  42. .host_ttl_tx(host_ttl_tx),
  43. .host_rs232_rx(1'b1),
  44. .host_rs232_tx(),
  45. .bmc_rs232_rx(1'b1),
  46. .bmc_rs232_tx(),
  47. .ptc_pwm_out(ptc_pwm_out),
  48. .front_fan_pwm_out(front_fan_pwm_out),
  49. .rear_fan_pwm_out(rear_fan_pwm_out),
  50. .temp_scl(temp_scl),
  51. .temp_sda(temp_sda),
  52. .temp_scl_alt(),
  53. .temp_sda_alt(),
  54. .ssr1_out(ssr1_out),
  55. .ssr2_out(ssr2_out),
  56. .ssr4_out(ssr4_out)
  57. );
  58. // CRC-16/CCITT-FALSE
  59. function [15:0] calc_crc;
  60. input integer count;
  61. integer i, b;
  62. reg [15:0] c;
  63. reg [7:0] d;
  64. begin
  65. c = 16'hFFFF;
  66. for (i = 2; i < count; i = i + 1) begin
  67. d = tx_buf[i];
  68. for (b = 7; b >= 0; b = b - 1)
  69. c = (c[15] ^ d[b]) ? ({c[14:0], 1'b0} ^ 16'h1021) : {c[14:0], 1'b0};
  70. end
  71. calc_crc = c;
  72. end
  73. endfunction
  74. reg [7:0] tx_buf [0:255];
  75. task uart_send_byte;
  76. input [7:0] val;
  77. integer b;
  78. begin
  79. host_ttl_rx = 1'b0;
  80. repeat (BIT_CLKS) @(posedge clk);
  81. for (b = 0; b < 8; b = b + 1) begin
  82. host_ttl_rx = val[b];
  83. repeat (BIT_CLKS) @(posedge clk);
  84. end
  85. host_ttl_rx = 1'b1;
  86. repeat (BIT_CLKS) @(posedge clk);
  87. end
  88. endtask
  89. task uart_send_frame;
  90. input integer n;
  91. integer i;
  92. reg [15:0] crc;
  93. begin
  94. crc = calc_crc(n);
  95. for (i = 0; i < n; i = i + 1) uart_send_byte(tx_buf[i]);
  96. uart_send_byte(crc[7:0]);
  97. uart_send_byte(crc[15:8]);
  98. end
  99. endtask
  100. task uart_send_bad_crc_frame;
  101. input integer n;
  102. integer i;
  103. reg [15:0] crc;
  104. begin
  105. crc = calc_crc(n) ^ 16'hA5A5; // corrupt CRC
  106. for (i = 0; i < n; i = i + 1) uart_send_byte(tx_buf[i]);
  107. uart_send_byte(crc[7:0]);
  108. uart_send_byte(crc[15:8]);
  109. end
  110. endtask
  111. // Receiver
  112. reg [7:0] rx_buf [0:255];
  113. integer rx_count;
  114. reg [3:0] rx_bit;
  115. reg [7:0] rx_shift;
  116. integer rx_clk_cnt;
  117. reg rx_busy;
  118. initial begin
  119. rx_busy = 0;
  120. rx_clk_cnt = 0;
  121. rx_count = 0;
  122. forever begin
  123. @(posedge clk);
  124. if (!rx_busy && host_ttl_tx === 1'b0) begin
  125. rx_busy = 1;
  126. rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1;
  127. rx_bit = 0;
  128. end else if (rx_busy) begin
  129. if (rx_clk_cnt == 0) begin
  130. if (rx_bit < 8) begin
  131. rx_shift[rx_bit] = host_ttl_tx;
  132. rx_bit = rx_bit + 1;
  133. rx_clk_cnt = BIT_CLKS - 1;
  134. end else begin
  135. rx_buf[rx_count] = rx_shift;
  136. rx_count = rx_count + 1;
  137. rx_busy = 0;
  138. end
  139. end else begin
  140. rx_clk_cnt = rx_clk_cnt - 1;
  141. end
  142. end
  143. end
  144. end
  145. task send_set_all_uniform;
  146. input [7:0] ptc_val;
  147. input [7:0] front_val;
  148. input [7:0] rear_val;
  149. integer i;
  150. begin
  151. tx_buf[0] = 8'hA5;
  152. tx_buf[1] = 8'h5A;
  153. tx_buf[2] = 8'h01;
  154. tx_buf[3] = 8'd35;
  155. for (i = 0; i < 24; i = i + 1) tx_buf[4 + i] = ptc_val;
  156. for (i = 0; i < 4; i = i + 1) tx_buf[28 + i] = front_val;
  157. for (i = 0; i < 4; i = i + 1) tx_buf[32 + i] = rear_val;
  158. tx_buf[36] = 100; tx_buf[37] = 0; tx_buf[38] = 100;
  159. rx_count = 0;
  160. uart_send_frame(39);
  161. end
  162. endtask
  163. task send_get_all;
  164. begin
  165. tx_buf[0] = 8'hA5;
  166. tx_buf[1] = 8'h5A;
  167. tx_buf[2] = 8'h02;
  168. tx_buf[3] = 8'd0;
  169. rx_count = 0;
  170. uart_send_frame(4);
  171. end
  172. endtask
  173. task wait_for_set_ack;
  174. output ok;
  175. begin
  176. while (rx_count < 6) @(posedge clk);
  177. if (rx_buf[0] == 8'hA5 && rx_buf[1] == 8'h5A &&
  178. rx_buf[2] == 8'h81 && rx_buf[3] == 8'h00) begin
  179. ok = 1;
  180. end else begin
  181. ok = 0;
  182. $display("ERROR: Bad SET_ACK: %02h %02h %02h %02h", rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3]);
  183. end
  184. end
  185. endtask
  186. task wait_for_get_reply;
  187. output ok;
  188. begin
  189. while (rx_count < 98) @(posedge clk);
  190. if (rx_buf[0] == 8'hA5 && rx_buf[1] == 8'h5A && rx_buf[2] == 8'h82 &&
  191. rx_buf[6] == 8'hA5 && rx_buf[7] == 8'h5A && rx_buf[8] == 8'h42) begin
  192. ok = 1;
  193. end else begin
  194. ok = 0;
  195. $display("ERROR: Bad GET reply");
  196. end
  197. end
  198. endtask
  199. integer failures = 0;
  200. integer round;
  201. reg ack_ok;
  202. initial begin
  203. host_ttl_rx = 1'b1;
  204. repeat (2000) @(posedge clk); // Allow POR (1024 cycles) to release sys_rst_n
  205. $display("=== START PWM Commit Handshake & Watchdog Test ===");
  206. // Test 1: SSR boot defaults zero
  207. if (ssr1_out !== 1'b0 || ssr2_out !== 1'b0 || ssr4_out !== 1'b0) begin
  208. $display("FAIL: SSRs not zero initially");
  209. failures = failures + 1;
  210. end else begin
  211. $display("PASS: SSR1/2/4 boot zero");
  212. end
  213. // Test 2: Initial defaults (PTC 0%, Fan 20%) via GET_ALL before any SET
  214. $display("--- Testing Initial Default Outputs via GET_ALL ---");
  215. send_get_all();
  216. wait_for_get_reply(ack_ok);
  217. if (!ack_ok) failures = failures + 1;
  218. else begin
  219. for (round = 0; round < 24; round = round + 1) begin
  220. if (rx_buf[10 + round] !== 8'd0) begin
  221. $display("FAIL: Default PTC[%0d] = %0d != 0", round, rx_buf[10 + round]);
  222. failures = failures + 1;
  223. end
  224. end
  225. for (round = 0; round < 8; round = round + 1) begin
  226. if (rx_buf[34 + round] !== 8'd20) begin
  227. $display("FAIL: Default FAN[%0d] = %0d != 20", round, rx_buf[34 + round]);
  228. failures = failures + 1;
  229. end
  230. end
  231. $display("PASS: Initial defaults confirmed (PTC 0%%, FAN 20%%)");
  232. end
  233. // Test 3: SET all channels to100 and check subsequent GET readback
  234. $display("--- Testing All-100 Duty Commit Readback ---");
  235. send_set_all_uniform(8'd100, 8'd100, 8'd100);
  236. wait_for_set_ack(ack_ok);
  237. if (!ack_ok) begin
  238. $display("FAIL: SET_ACK for All 100 failed");
  239. failures = failures + 1;
  240. end else begin
  241. $display("PASS: SET_ACK received for All 100");
  242. end
  243. send_get_all();
  244. wait_for_get_reply(ack_ok);
  245. if (!ack_ok) failures = failures + 1;
  246. else begin
  247. for (round = 0; round < 24; round = round + 1) begin
  248. if (rx_buf[10 + round] !== 8'd100) begin
  249. $display("FAIL: PTC[%0d] = %0d != 100", round, rx_buf[10 + round]);
  250. failures = failures + 1;
  251. end
  252. end
  253. for (round = 0; round < 8; round = round + 1) begin
  254. if (rx_buf[34 + round] !== 8'd100) begin
  255. $display("FAIL: FAN[%0d] = %0d != 100", round, rx_buf[34 + round]);
  256. failures = failures + 1;
  257. end
  258. end
  259. $display("PASS: Output confirmed 100%% on all channels");
  260. end
  261. // Test 4: Watchdog expiry to defaults & consecutive GET checks
  262. $display("--- Testing Expiry to Safe Defaults & Consecutive GETs ---");
  263. repeat (WD_CLKS + 50) @(posedge clk);
  264. if (u_dut.u_duty_bank.valid_r !== 1'b0) begin
  265. $display("FAIL: Duty bank not invalidated on expiry");
  266. failures = failures + 1;
  267. end else begin
  268. $display("PASS: Duty bank invalidated to safe defaults on expiry");
  269. end
  270. // GET 1: should return defaulted outputs
  271. send_get_all();
  272. wait_for_get_reply(ack_ok);
  273. if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin
  274. $display("FAIL: Post-expiry GET did not return default PTC 0 / FAN 20");
  275. failures = failures + 1;
  276. end else begin
  277. $display("PASS: Post-expiry GET returns PTC 0 / FAN 20 defaults");
  278. end
  279. // GET 2: consecutive GET must NOT restore pre-expiry 100% duty
  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: Consecutive GET improperly restored old bank");
  284. failures = failures + 1;
  285. end else begin
  286. $display("PASS: Consecutive GET preserved default outputs");
  287. end
  288. // Test 5: Direct recovery with identical bytes SET A
  289. $display("--- Testing Direct Recovery with Identical SET A ---");
  290. send_set_all_uniform(8'd100, 8'd100, 8'd100);
  291. wait_for_set_ack(ack_ok);
  292. if (!ack_ok) begin
  293. $display("FAIL: SET_ACK on direct recovery failed");
  294. failures = failures + 1;
  295. end else begin
  296. $display("PASS: SET_ACK received on direct recovery");
  297. end
  298. send_get_all();
  299. wait_for_get_reply(ack_ok);
  300. if (!ack_ok || rx_buf[10] !== 8'd100 || rx_buf[34] !== 8'd100) begin
  301. $display("FAIL: Output not restored after direct SET A");
  302. failures = failures + 1;
  303. end else begin
  304. $display("PASS: Output 100%% restored after direct SET A");
  305. end
  306. // Test 6: Direct recovery with SET B (different values)
  307. $display("--- Testing SET B Recovery ---");
  308. repeat (WD_CLKS + 50) @(posedge clk); // let expire
  309. send_set_all_uniform(8'd50, 8'd30, 8'd70);
  310. wait_for_set_ack(ack_ok);
  311. if (!ack_ok) begin
  312. $display("FAIL: SET_ACK for SET B failed");
  313. failures = failures + 1;
  314. end else begin
  315. $display("PASS: SET_ACK received for SET B");
  316. end
  317. send_get_all();
  318. wait_for_get_reply(ack_ok);
  319. if (!ack_ok || rx_buf[10] !== 8'd50 || rx_buf[34] !== 8'd30 || rx_buf[38] !== 8'd70) begin
  320. $display("FAIL: SET B values not applied: PTC=%0d F=%0d R=%0d",
  321. rx_buf[10], rx_buf[34], rx_buf[38]);
  322. failures = failures + 1;
  323. end else begin
  324. $display("PASS: SET B output applied cleanly");
  325. end
  326. // Test 7: Bad CRC followed by eventual watchdog expiry and legal GET
  327. $display("--- Testing Bad CRC Input, Eventual Expiry and Recovery GET ---");
  328. // Frame with bad CRC
  329. tx_buf[0] = 8'hA5; tx_buf[1] = 8'h5A; tx_buf[2] = 8'h01; tx_buf[3] = 8'd35;
  330. for (round = 0; round < 35; round = round + 1) tx_buf[4 + round] = 8'd99;
  331. rx_count = 0;
  332. uart_send_bad_crc_frame(39);
  333. repeat (WD_CLKS + 50) @(posedge clk);
  334. send_get_all();
  335. wait_for_get_reply(ack_ok);
  336. if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin
  337. $display("FAIL: Output did not reach defaults after bad CRC and full timeout!");
  338. failures = failures + 1;
  339. end else begin
  340. $display("PASS: Defaults observed after bad CRC input and full timeout");
  341. end
  342. // Test 8: Accelerated Expiry & Direct Recovery Rounds
  343. $display("--- Testing Repeated Rounds of Accelerated Expiry & Direct Recovery ---");
  344. for (round = 1; round <= 10; round = round + 1) begin
  345. // Direct SET recovery (same values: 60%, 40%, 80%)
  346. send_set_all_uniform(8'd60, 8'd40, 8'd80);
  347. wait_for_set_ack(ack_ok);
  348. if (!ack_ok) begin
  349. $display("FAIL: Round %0d SET_ACK failed", round);
  350. failures = failures + 1;
  351. end
  352. if (u_dut.u_duty_bank.valid_r !== 1'b1) begin
  353. $display("FAIL: Round %0d duty bank not valid after commit", round);
  354. failures = failures + 1;
  355. end
  356. // Wait for watchdog expiry
  357. repeat (WD_CLKS + 50) @(posedge clk);
  358. if (u_dut.u_duty_bank.valid_r !== 1'b0) begin
  359. $display("FAIL: Round %0d duty bank not invalidated on expiry", round);
  360. failures = failures + 1;
  361. end
  362. if ({ssr4_out,ssr2_out,ssr1_out} !== 3'b0) begin
  363. $display("FAIL: SSR not zero at watchdog expiry");
  364. failures = failures + 1;
  365. end
  366. $display(" Progress: %0d/10 recovery rounds passed", round);
  367. end
  368. $display("PASS: All 10 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'b0 || ssr4_out !== 1'b1) begin
  382. $display("FAIL: SSRs differ from100/0/100");
  383. failures = failures + 1;
  384. end else begin
  385. $display("PASS: SSRs match100/0/100 at final check");
  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