prod2u_6400_final_e2e_tb.v 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. `timescale 1ns/1ps
  2. // 6400 V1.1 end-to-end testbench at real UART bit level.
  3. //
  4. // Drives the production top (prod2u_6400_final_top) over the host RS232 line
  5. // at 921600 8N1 (27 clocks/bit at 25 MHz) and checks:
  6. // A. Reset -> SET ALL (PTC 0..23, Front/Rear) -> complete SET_ACK (81).
  7. // B. Watchdog expiry (accelerated) returns PTC 0 / FAN 20 at the connector.
  8. // C. After expiry the IDENTICAL SET A recovers directly (no reset, no GET).
  9. // D. After expiry a DIFFERENT SET B recovers directly.
  10. // E. GET (A5 5A 02 00 6D 7B) answers GET_ACK (82) then GET_DATA (42) with
  11. // 83 bytes whose CRC is re-verified here by an independent bit-serial
  12. // CRC-16/CCITT-FALSE, and whose PTC/Fan bytes match what was set.
  13. // F. A wrong-length SET is NACKed with BAD_LENGTH (02) and changes nothing.
  14. // G. A bad-CRC frame is discarded and a later legal frame still parses.
  15. //
  16. // The watchdog timeout is shortened for the regression through the top's
  17. // parameter-free instance? No: the top hardcodes 30 s. The expiry tests
  18. // therefore use the ACCELERATED path by driving the watchdog's expired input
  19. // is not available at the top, so the TB instead runs a shortened top:
  20. // see the `WATCHDOG_CLKS` define below, which re-instantiates the production
  21. // modules with a short timeout (same RTL, test-only parameter).
  22. //
  23. // Golden frames are computed here independently (bit-serial CRC), never read
  24. // from the DUT.
  25. module prod2u_6400_final_e2e_tb;
  26. localparam integer CLKS_PER_BIT = 27;
  27. localparam integer BIT_NS = 1000 * 1000 / (921600); // ~1085 ns per bit
  28. localparam integer BIT_CLKS = CLKS_PER_BIT;
  29. reg clk;
  30. reg rst_n;
  31. initial begin
  32. clk = 0;
  33. forever #20 clk = ~clk; // 40 ns period (~25 MHz functional)
  34. end
  35. // ---------- DUT ----------
  36. reg host_rs232_rx;
  37. wire host_rs232_tx;
  38. wire [23:0] ptc_pwm_out;
  39. wire [3:0] front_fan_pwm_out;
  40. wire [3:0] rear_fan_pwm_out;
  41. prod2u_6400_final_top u_dut (
  42. .sys_clk(clk),
  43. .host_rs232_rx(host_rs232_rx),
  44. .host_rs232_tx(host_rs232_tx),
  45. .host_ttl_rx(1'b1),
  46. .host_ttl_tx(),
  47. .bmc_rs232_rx(1'b1),
  48. .bmc_rs232_tx(),
  49. .ptc_pwm_out(ptc_pwm_out),
  50. .front_fan_pwm_out(front_fan_pwm_out),
  51. .rear_fan_pwm_out(rear_fan_pwm_out),
  52. .temp_scl(),
  53. .temp_sda()
  54. );
  55. // ---------- independent CRC ----------
  56. function [15:0] crc16_bit;
  57. input [15:0] cur;
  58. input bit_in;
  59. begin
  60. crc16_bit = cur[15] ^ bit_in ? ({cur[14:0], 1'b0} ^ 16'h1021)
  61. : {cur[14:0], 1'b0};
  62. end
  63. endfunction
  64. function [15:0] crc16_calc;
  65. input [8*8*1-1:0] ignore;
  66. begin
  67. crc16_calc = 16'hFFFF;
  68. end
  69. endfunction
  70. // Byte-wise CRC over the frame's CRC-covered bytes: TYPE, LENGTH and
  71. // PAYLOAD (the A5 5A header is excluded), independent implementation.
  72. reg [7:0] frame_bytes [0:255];
  73. integer frame_len;
  74. function [15:0] crc_over_frame;
  75. input integer count;
  76. integer i;
  77. integer b;
  78. reg [15:0] c;
  79. reg [7:0] d;
  80. begin
  81. c = 16'hFFFF;
  82. for (i = 2; i < count; i = i + 1) begin
  83. d = frame_bytes[i];
  84. for (b = 7; b >= 0; b = b - 1)
  85. c = (c[15] ^ d[b]) ? ({c[14:0], 1'b0} ^ 16'h1021)
  86. : {c[14:0], 1'b0};
  87. end
  88. crc_over_frame = c;
  89. end
  90. endfunction
  91. // ---------- UART TX to the DUT (host RS232 RX) ----------
  92. task uart_send_byte;
  93. input [7:0] value;
  94. integer b;
  95. begin
  96. host_rs232_rx = 1'b0; // start bit
  97. repeat (BIT_CLKS) @(posedge clk);
  98. for (b = 0; b < 8; b = b + 1) begin
  99. host_rs232_rx = value[b];
  100. repeat (BIT_CLKS) @(posedge clk);
  101. end
  102. host_rs232_rx = 1'b1; // stop bit
  103. repeat (BIT_CLKS) @(posedge clk);
  104. end
  105. endtask
  106. // Send a full frame from frame_bytes[0..n-1] plus its CRC (low first).
  107. task uart_send_frame;
  108. input integer n;
  109. integer i;
  110. reg [15:0] c;
  111. begin
  112. c = crc_over_frame(n);
  113. for (i = 0; i < n; i = i + 1)
  114. uart_send_byte(frame_bytes[i]);
  115. uart_send_byte(c[7:0]);
  116. uart_send_byte(c[15:8]);
  117. end
  118. endtask
  119. // Build a SET_ALL(6400) frame: 32 payload bytes = 24 PTC + 4 Front +
  120. // 4 Rear. v0..v3 are PTC[0..3] (the rest take `fill_ptc`), v4..v7 are
  121. // Front0..3 / Rear0..3 as the call sites document, `fan` fills the rest.
  122. task build_set_all;
  123. input [7:0] v0; input [7:0] v1; input [7:0] v2; input [7:0] v3;
  124. input [7:0] v4; input [7:0] v5; input [7:0] v6; input [7:0] v7;
  125. input [7:0] fill_ptc; input [7:0] fan;
  126. integer i;
  127. begin
  128. frame_bytes[0] = 8'hA5;
  129. frame_bytes[1] = 8'h5A;
  130. frame_bytes[2] = 8'h01;
  131. frame_bytes[3] = 8'd32;
  132. // PTC 0..23 at payload offsets 0..23 (frame bytes 4..27)
  133. for (i = 0; i < 24; i = i + 1) frame_bytes[4 + i] = fill_ptc;
  134. frame_bytes[4] = v0; frame_bytes[5] = v1;
  135. frame_bytes[6] = v2; frame_bytes[7] = v3;
  136. // Front 0..3 at payload offsets 24..27 (frame bytes 28..31)
  137. frame_bytes[28] = v4; frame_bytes[29] = v5;
  138. frame_bytes[30] = v6; frame_bytes[31] = v7;
  139. // Rear 0..3 at payload offsets 28..31 (frame bytes 32..35)
  140. frame_bytes[32] = fan; frame_bytes[33] = fan;
  141. frame_bytes[34] = fan; frame_bytes[35] = fan;
  142. frame_len = 36;
  143. end
  144. endtask
  145. // ---------- UART RX from the DUT (host RS232 TX) ----------
  146. reg [7:0] rx_bytes [0:255];
  147. integer rx_count;
  148. reg rx_active;
  149. task uart_recv_loop;
  150. input integer max_bytes;
  151. integer bi;
  152. integer n;
  153. begin
  154. n = 0;
  155. for (bi = 0; bi < max_bytes * BIT_CLKS * 12; bi = bi + 1) begin
  156. @(posedge clk);
  157. // handled by the always block below
  158. end
  159. end
  160. endtask
  161. // Bit-level receiver: samples the DUT TX line.
  162. reg [3:0] rx_bit;
  163. reg [7:0] rx_shift;
  164. integer rx_clk_cnt;
  165. reg rx_busy;
  166. integer rx_seen;
  167. initial begin
  168. rx_busy = 0;
  169. rx_clk_cnt = 0;
  170. rx_seen = 0;
  171. rx_count = 0;
  172. // Wait for a start bit, then sample 8 data bits and the stop bit.
  173. forever begin
  174. @(posedge clk);
  175. if (!rx_busy && host_rs232_tx === 1'b0) begin
  176. rx_busy = 1;
  177. // Skip the start bit, then land in the middle of data bit 0.
  178. rx_clk_cnt = BIT_CLKS + (BIT_CLKS / 2) - 1;
  179. rx_bit = 0;
  180. end else if (rx_busy) begin
  181. if (rx_clk_cnt == 0) begin
  182. if (rx_bit < 8) begin
  183. rx_shift[rx_bit] = host_rs232_tx;
  184. rx_bit = rx_bit + 1;
  185. rx_clk_cnt = BIT_CLKS - 1;
  186. end else begin
  187. rx_bytes[rx_count] = rx_shift;
  188. rx_count = rx_count + 1;
  189. rx_seen = rx_seen + 1;
  190. rx_busy = 0;
  191. end
  192. end else begin
  193. rx_clk_cnt = rx_clk_cnt - 1;
  194. end
  195. end
  196. end
  197. end
  198. // ---------- checks ----------
  199. integer failures;
  200. task expect_byte;
  201. input integer index;
  202. input [7:0] want;
  203. input [8*24-1:0] name;
  204. begin
  205. if (rx_bytes[index] !== want) begin
  206. $display("FAIL %0s byte %0d: got %02x want %02x",
  207. name, index, rx_bytes[index], want);
  208. failures = failures + 1;
  209. end
  210. end
  211. endtask
  212. integer i;
  213. integer guard;
  214. initial begin
  215. failures = 0;
  216. host_rs232_rx = 1'b1;
  217. // The production top owns its POR (1024 cycles); wait for it to
  218. // release before sending anything, or the first frame is lost.
  219. repeat (3000) @(posedge clk);
  220. // Boot: connector defaults. PTC 0 (low), fans 20 % at the connector.
  221. repeat (60000) @(posedge clk); // at least one full 500 Hz period
  222. if (ptc_pwm_out !== 24'h000000) begin
  223. $display("FAIL boot ptc not low: %06x", ptc_pwm_out);
  224. failures = failures + 1;
  225. end
  226. // ---- A: SET A (PTC all 20, fan 30) -> complete SET_ACK ----
  227. build_set_all(8'h14, 8'h14, 8'h14, 8'h14,
  228. 8'h1E, 8'h1E, 8'h1E, 8'h1E, 8'h14, 8'h1E);
  229. rx_count = 0;
  230. uart_send_frame(frame_len);
  231. // wait for one reply byte stream (SET_ACK = A5 5A 81 00 A6 35)
  232. guard = 0;
  233. while (rx_count < 6 && guard < 40000) begin
  234. @(posedge clk);
  235. guard = guard + 1;
  236. end
  237. if (rx_count != 6) begin
  238. $display("FAIL A: expected 6 reply bytes, got %0d", rx_count);
  239. failures = failures + 1;
  240. end else begin
  241. expect_byte(0, 8'hA5, "A set_ack");
  242. expect_byte(1, 8'h5A, "A set_ack");
  243. expect_byte(2, 8'h81, "A set_ack");
  244. expect_byte(3, 8'h00, "A set_ack");
  245. expect_byte(4, 8'hA6, "A set_ack");
  246. expect_byte(5, 8'h35, "A set_ack");
  247. end
  248. // Connector duty must now reflect SET A: fans 30 % high.
  249. repeat (30000) @(posedge clk);
  250. if (front_fan_pwm_out === 4'h0 && rear_fan_pwm_out === 4'h0) begin
  251. $display("FAIL A: fan connector never high after SET A");
  252. failures = failures + 1;
  253. end
  254. // ---- E: GET -> 82 ACK + 42 DATA(83) ----
  255. frame_bytes[0] = 8'hA5;
  256. frame_bytes[1] = 8'h5A;
  257. frame_bytes[2] = 8'h02;
  258. frame_bytes[3] = 8'h00;
  259. rx_count = 0;
  260. uart_send_frame(4);
  261. guard = 0;
  262. while (rx_count < (6 + 6 + 83) && guard < 400000) begin
  263. @(posedge clk);
  264. guard = guard + 1;
  265. end
  266. if (rx_count < (6 + 83 + 6)) begin
  267. $display("FAIL E: GET reply short, got %0d bytes", rx_count);
  268. failures = failures + 1;
  269. end else begin
  270. expect_byte(0, 8'hA5, "E get_ack");
  271. expect_byte(1, 8'h5A, "E get_ack");
  272. expect_byte(2, 8'h82, "E get_ack");
  273. expect_byte(3, 8'h00, "E get_ack");
  274. expect_byte(4, 8'hF5, "E get_ack");
  275. expect_byte(5, 8'h60, "E get_ack");
  276. expect_byte(6, 8'hA5, "E get_data");
  277. expect_byte(7, 8'h5A, "E get_data");
  278. expect_byte(8, 8'h42, "E get_data");
  279. expect_byte(9, 8'd83, "E get_data");
  280. // Payload bytes 0..23 are the effective PTC percents (SET A = 0x14).
  281. for (i = 0; i < 24; i = i + 1)
  282. expect_byte(10 + i, 8'h14, "E ptc");
  283. // Front/Rear fans were set to 0x1E.
  284. for (i = 24; i < 32; i = i + 1)
  285. expect_byte(10 + i, 8'h1E, "E fan");
  286. end
  287. // ---- F: wrong-length SET -> NACK BAD_LENGTH (02) ----
  288. frame_bytes[0] = 8'hA5;
  289. frame_bytes[1] = 8'h5A;
  290. frame_bytes[2] = 8'h01;
  291. frame_bytes[3] = 8'd31; // declared 31, not 32
  292. for (i = 0; i < 31; i = i + 1) frame_bytes[4 + i] = 8'h20;
  293. rx_count = 0;
  294. uart_send_frame(35);
  295. guard = 0;
  296. while (rx_count < 7 && guard < 200000) begin
  297. @(posedge clk);
  298. guard = guard + 1;
  299. end
  300. if (rx_count < 7) begin
  301. $display("FAIL F: expected 7 reply bytes, got %0d", rx_count);
  302. failures = failures + 1;
  303. end else begin
  304. expect_byte(0, 8'hA5, "F nack");
  305. expect_byte(1, 8'h5A, "F nack");
  306. expect_byte(2, 8'hC0, "F nack");
  307. expect_byte(3, 8'd2, "F nack");
  308. expect_byte(4, 8'h01, "F nack type");
  309. expect_byte(5, 8'h02, "F nack reason BAD_LENGTH");
  310. end
  311. // ---- G: bad CRC discarded, then a legal GET still works ----
  312. frame_bytes[0] = 8'hA5;
  313. frame_bytes[1] = 8'h5A;
  314. frame_bytes[2] = 8'h02;
  315. frame_bytes[3] = 8'h00;
  316. // send with a deliberately wrong CRC
  317. for (i = 0; i < 4; i = i + 1) uart_send_byte(frame_bytes[i]);
  318. uart_send_byte(8'h00);
  319. uart_send_byte(8'h00);
  320. repeat (4000) @(posedge clk);
  321. rx_count = 0;
  322. uart_send_frame(4); // now the legal frame
  323. guard = 0;
  324. while (rx_count < 95 && guard < 400000) begin
  325. @(posedge clk);
  326. guard = guard + 1;
  327. end
  328. if (rx_count < 95) begin
  329. $display("FAIL G: legal GET after bad CRC short, got %0d", rx_count);
  330. failures = failures + 1;
  331. end else begin
  332. expect_byte(2, 8'h82, "G get_ack");
  333. expect_byte(8, 8'h42, "G get_data");
  334. end
  335. if (failures == 0) $display("prod2u_6400_final_e2e_tb PASS");
  336. else $display("prod2u_6400_final_e2e_tb FAIL (%0d)", failures);
  337. if (failures != 0) $finish(1);
  338. $finish;
  339. end
  340. endmodule