pwm_commit_handshake_tb.v 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. // 95-byte GET reply takes ~25,650 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'd32;
  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. rx_count = 0;
  159. uart_send_frame(36);
  160. end
  161. endtask
  162. task send_get_all;
  163. begin
  164. tx_buf[0] = 8'hA5;
  165. tx_buf[1] = 8'h5A;
  166. tx_buf[2] = 8'h02;
  167. tx_buf[3] = 8'd0;
  168. rx_count = 0;
  169. uart_send_frame(4);
  170. end
  171. endtask
  172. task wait_for_set_ack;
  173. output ok;
  174. begin
  175. while (rx_count < 6) @(posedge clk);
  176. if (rx_buf[0] == 8'hA5 && rx_buf[1] == 8'h5A &&
  177. rx_buf[2] == 8'h81 && rx_buf[3] == 8'h00) begin
  178. ok = 1;
  179. end else begin
  180. ok = 0;
  181. $display("ERROR: Bad SET_ACK: %02h %02h %02h %02h", rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3]);
  182. end
  183. end
  184. endtask
  185. task wait_for_get_reply;
  186. output ok;
  187. begin
  188. while (rx_count < 95) @(posedge clk);
  189. if (rx_buf[0] == 8'hA5 && rx_buf[1] == 8'h5A && rx_buf[2] == 8'h82 &&
  190. rx_buf[6] == 8'hA5 && rx_buf[7] == 8'h5A && rx_buf[8] == 8'h42) begin
  191. ok = 1;
  192. end else begin
  193. ok = 0;
  194. $display("ERROR: Bad GET reply");
  195. end
  196. end
  197. endtask
  198. integer failures = 0;
  199. integer round;
  200. reg ack_ok;
  201. initial begin
  202. host_ttl_rx = 1'b1;
  203. repeat (2000) @(posedge clk); // Allow POR (1024 cycles) to release sys_rst_n
  204. $display("=== START PWM Commit Handshake & Watchdog Test ===");
  205. // Test 1: SSRs constant 1'b1
  206. if (ssr1_out !== 1'b1 || ssr2_out !== 1'b1 || ssr4_out !== 1'b1) begin
  207. $display("FAIL: SSRs not 1'b1 initially");
  208. failures = failures + 1;
  209. end else begin
  210. $display("PASS: SSR1/2/4 constant 1'b1");
  211. end
  212. // Test 2: Initial defaults (PTC 0%, Fan 20%) via GET_ALL before any SET
  213. $display("--- Testing Initial Default Outputs via GET_ALL ---");
  214. send_get_all();
  215. wait_for_get_reply(ack_ok);
  216. if (!ack_ok) failures = failures + 1;
  217. else begin
  218. for (round = 0; round < 24; round = round + 1) begin
  219. if (rx_buf[10 + round] !== 8'd0) begin
  220. $display("FAIL: Default PTC[%0d] = %0d != 0", round, rx_buf[10 + round]);
  221. failures = failures + 1;
  222. end
  223. end
  224. for (round = 0; round < 8; round = round + 1) begin
  225. if (rx_buf[34 + round] !== 8'd20) begin
  226. $display("FAIL: Default FAN[%0d] = %0d != 20", round, rx_buf[34 + round]);
  227. failures = failures + 1;
  228. end
  229. end
  230. $display("PASS: Initial defaults confirmed (PTC 0%%, FAN 20%%)");
  231. end
  232. // Test 3: SET all channels to100 and check subsequent GET readback
  233. $display("--- Testing All-100 Duty Commit Readback ---");
  234. send_set_all_uniform(8'd100, 8'd100, 8'd100);
  235. wait_for_set_ack(ack_ok);
  236. if (!ack_ok) begin
  237. $display("FAIL: SET_ACK for All 100 failed");
  238. failures = failures + 1;
  239. end else begin
  240. $display("PASS: SET_ACK received for All 100");
  241. end
  242. send_get_all();
  243. wait_for_get_reply(ack_ok);
  244. if (!ack_ok) failures = failures + 1;
  245. else begin
  246. for (round = 0; round < 24; round = round + 1) begin
  247. if (rx_buf[10 + round] !== 8'd100) begin
  248. $display("FAIL: PTC[%0d] = %0d != 100", round, rx_buf[10 + round]);
  249. failures = failures + 1;
  250. end
  251. end
  252. for (round = 0; round < 8; round = round + 1) begin
  253. if (rx_buf[34 + round] !== 8'd100) begin
  254. $display("FAIL: FAN[%0d] = %0d != 100", round, rx_buf[34 + round]);
  255. failures = failures + 1;
  256. end
  257. end
  258. $display("PASS: Output confirmed 100%% on all channels");
  259. end
  260. // Test 4: Watchdog expiry to defaults & consecutive GET checks
  261. $display("--- Testing Expiry to Safe Defaults & Consecutive GETs ---");
  262. repeat (WD_CLKS + 50) @(posedge clk);
  263. if (u_dut.u_duty_bank.valid_r !== 1'b0) begin
  264. $display("FAIL: Duty bank not invalidated on expiry");
  265. failures = failures + 1;
  266. end else begin
  267. $display("PASS: Duty bank invalidated to safe defaults on expiry");
  268. end
  269. // GET 1: should return defaulted outputs
  270. send_get_all();
  271. wait_for_get_reply(ack_ok);
  272. if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin
  273. $display("FAIL: Post-expiry GET did not return default PTC 0 / FAN 20");
  274. failures = failures + 1;
  275. end else begin
  276. $display("PASS: Post-expiry GET returns PTC 0 / FAN 20 defaults");
  277. end
  278. // GET 2: consecutive GET must NOT restore pre-expiry 100% duty
  279. send_get_all();
  280. wait_for_get_reply(ack_ok);
  281. if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin
  282. $display("FAIL: Consecutive GET improperly restored old bank");
  283. failures = failures + 1;
  284. end else begin
  285. $display("PASS: Consecutive GET preserved default outputs");
  286. end
  287. // Test 5: Direct recovery with identical bytes SET A
  288. $display("--- Testing Direct Recovery with Identical SET A ---");
  289. send_set_all_uniform(8'd100, 8'd100, 8'd100);
  290. wait_for_set_ack(ack_ok);
  291. if (!ack_ok) begin
  292. $display("FAIL: SET_ACK on direct recovery failed");
  293. failures = failures + 1;
  294. end else begin
  295. $display("PASS: SET_ACK received on direct recovery");
  296. end
  297. send_get_all();
  298. wait_for_get_reply(ack_ok);
  299. if (!ack_ok || rx_buf[10] !== 8'd100 || rx_buf[34] !== 8'd100) begin
  300. $display("FAIL: Output not restored after direct SET A");
  301. failures = failures + 1;
  302. end else begin
  303. $display("PASS: Output 100%% restored after direct SET A");
  304. end
  305. // Test 6: Direct recovery with SET B (different values)
  306. $display("--- Testing SET B Recovery ---");
  307. repeat (WD_CLKS + 50) @(posedge clk); // let expire
  308. send_set_all_uniform(8'd50, 8'd30, 8'd70);
  309. wait_for_set_ack(ack_ok);
  310. if (!ack_ok) begin
  311. $display("FAIL: SET_ACK for SET B failed");
  312. failures = failures + 1;
  313. end else begin
  314. $display("PASS: SET_ACK received for SET B");
  315. end
  316. send_get_all();
  317. wait_for_get_reply(ack_ok);
  318. if (!ack_ok || rx_buf[10] !== 8'd50 || rx_buf[34] !== 8'd30 || rx_buf[38] !== 8'd70) begin
  319. $display("FAIL: SET B values not applied: PTC=%0d F=%0d R=%0d",
  320. rx_buf[10], rx_buf[34], rx_buf[38]);
  321. failures = failures + 1;
  322. end else begin
  323. $display("PASS: SET B output applied cleanly");
  324. end
  325. // Test 7: Bad CRC followed by eventual watchdog expiry and legal GET
  326. $display("--- Testing Bad CRC Input, Eventual Expiry and Recovery GET ---");
  327. // Frame with bad CRC
  328. tx_buf[0] = 8'hA5; tx_buf[1] = 8'h5A; tx_buf[2] = 8'h01; tx_buf[3] = 8'd32;
  329. for (round = 0; round < 32; round = round + 1) tx_buf[4 + round] = 8'd99;
  330. rx_count = 0;
  331. uart_send_bad_crc_frame(36);
  332. repeat (WD_CLKS + 50) @(posedge clk);
  333. send_get_all();
  334. wait_for_get_reply(ack_ok);
  335. if (!ack_ok || rx_buf[10] !== 8'd0 || rx_buf[34] !== 8'd20) begin
  336. $display("FAIL: Output did not reach defaults after bad CRC and full timeout!");
  337. failures = failures + 1;
  338. end else begin
  339. $display("PASS: Defaults observed after bad CRC input and full timeout");
  340. end
  341. // Test 8: Accelerated Expiry & Direct Recovery Rounds
  342. $display("--- Testing Repeated Rounds of Accelerated Expiry & Direct Recovery ---");
  343. for (round = 1; round <= 10; round = round + 1) begin
  344. // Direct SET recovery (same values: 60%, 40%, 80%)
  345. send_set_all_uniform(8'd60, 8'd40, 8'd80);
  346. wait_for_set_ack(ack_ok);
  347. if (!ack_ok) begin
  348. $display("FAIL: Round %0d SET_ACK failed", round);
  349. failures = failures + 1;
  350. end
  351. if (u_dut.u_duty_bank.valid_r !== 1'b1) begin
  352. $display("FAIL: Round %0d duty bank not valid after commit", round);
  353. failures = failures + 1;
  354. end
  355. // Wait for watchdog expiry
  356. repeat (WD_CLKS + 50) @(posedge clk);
  357. if (u_dut.u_duty_bank.valid_r !== 1'b0) begin
  358. $display("FAIL: Round %0d duty bank not invalidated on expiry", round);
  359. failures = failures + 1;
  360. end
  361. $display(" Progress: %0d/10 recovery rounds passed", round);
  362. end
  363. $display("PASS: All 10 accelerated recovery rounds passed");
  364. // Verify final state via full GET_ALL
  365. send_set_all_uniform(8'd75, 8'd45, 8'd65);
  366. wait_for_set_ack(ack_ok);
  367. send_get_all();
  368. wait_for_get_reply(ack_ok);
  369. if (!ack_ok || rx_buf[10] !== 8'd75 || rx_buf[34] !== 8'd45 || rx_buf[38] !== 8'd65) begin
  370. $display("FAIL: Final GET_ALL verification failed");
  371. failures = failures + 1;
  372. end else begin
  373. $display("PASS: Final GET_ALL matches applied SET");
  374. end
  375. // Final SSR check
  376. if (ssr1_out !== 1'b1 || ssr2_out !== 1'b1 || ssr4_out !== 1'b1) begin
  377. $display("FAIL: SSRs dropped during tests");
  378. failures = failures + 1;
  379. end else begin
  380. $display("PASS: SSRs were 1'b1 at the final check");
  381. end
  382. #2000;
  383. if (failures == 0) begin
  384. $display("pwm_commit_handshake_tb PASS");
  385. end else begin
  386. $fatal(1, "pwm_commit_handshake_tb FAIL (%0d errors)", failures);
  387. end
  388. $finish;
  389. end
  390. initial begin
  391. #600000000; // 600 ms timeout
  392. $fatal(1, "pwm_commit_handshake_tb TIMEOUT");
  393. end
  394. endmodule