bmc_ascii_poll_tb.v 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. `timescale 1ns/1ps
  2. // Testbench for bmc_ascii_poll + temp_ram with a virtual BMC UART model.
  3. //
  4. // The model accepts TX bytes only through a real valid/ready handshake (ready
  5. // is deliberately pulled low in a fixed pattern, so a byte that is not held
  6. // would be lost) and compares every byte with the required 43-byte command.
  7. // The reply is pushed byte by byte on the RX side. Expected temperature bytes
  8. // are literal constants - nothing is derived from a DUT output.
  9. //
  10. // Rounds:
  11. // 1. normal three-line 48-token reply with echo and prompt -> exact 24 bytes
  12. // 2. a second consecutive good round with different values -> exact 24 bytes
  13. // 3. reply with a truncated last line -> 24 x 0xFF
  14. // 4. reply with an oversized hex token -> 24 x 0xFF
  15. // 5. no reply at all (round timeout) -> 24 x 0xFF
  16. // 6. late full reply from round 5 before this round -> exact 24 bytes
  17. // (the stale reply must be discarded, never concatenated)
  18. // 7. 48 good tokens plus a further 16 (concatenated round) -> 24 x 0xFF
  19. // 8. data-only reply without echo or prompt -> exact 24 bytes
  20. // 9. non-hex garbage inside a data line -> 24 x 0xFF
  21. module bmc_ascii_poll_tb;
  22. localparam integer CLOCK_HZ = 10000;
  23. localparam integer POLL_SECONDS = 1;
  24. localparam integer ROUND_TIMEOUT_CLKS = 5000;
  25. localparam integer INTER_BYTE_TIMEOUT_CLKS = 64;
  26. reg clk = 0;
  27. reg rst_n = 0;
  28. always #5 clk = ~clk;
  29. integer failures = 0;
  30. integer i;
  31. reg [7:0] value;
  32. integer polled = 0;
  33. wire tx_valid;
  34. wire [7:0] tx_data;
  35. reg tx_ready = 1;
  36. reg rx_valid = 0;
  37. reg [7:0] rx_data = 0;
  38. wire wr_en;
  39. wire [5:0] wr_addr;
  40. wire [7:0] wr_data;
  41. wire [15:0] poll_count;
  42. wire round_failed;
  43. reg [5:0] temp_rd_addr = 0;
  44. wire [7:0] temp_rd_data;
  45. wire temp_rd_valid;
  46. bmc_ascii_poll #(
  47. .CLOCK_HZ(CLOCK_HZ),
  48. .POLL_SECONDS(POLL_SECONDS),
  49. .ROUND_TIMEOUT_CLKS(ROUND_TIMEOUT_CLKS),
  50. .INTER_BYTE_TIMEOUT_CLKS(INTER_BYTE_TIMEOUT_CLKS)
  51. ) dut (
  52. .clk(clk), .rst_n(rst_n),
  53. .tx_valid(tx_valid), .tx_data(tx_data), .tx_ready(tx_ready),
  54. .rx_valid(rx_valid), .rx_data(rx_data),
  55. .wr_en(wr_en), .wr_addr(wr_addr), .wr_data(wr_data),
  56. .poll_count(poll_count), .round_failed(round_failed)
  57. );
  58. temp_ram #(.DEPTH(64), .ADDR_WIDTH(6)) u_ram (
  59. .clk(clk), .rst_n(rst_n), .wr_en(wr_en), .wr_addr(wr_addr), .wr_data(wr_data),
  60. .rd_addr(temp_rd_addr), .rd_data(temp_rd_data), .rd_valid(temp_rd_valid)
  61. );
  62. // ---------------- virtual UART ----------------
  63. localparam [8*43-1:0] COMMAND =
  64. {"ipmitool raw 0x36 0x6c 0xdf 0xdd 0x00 0x03", 8'h0a};
  65. function [7:0] command_byte;
  66. input [5:0] index;
  67. begin
  68. command_byte = COMMAND[(8 * (6'd42 - index)) +: 8];
  69. end
  70. endfunction
  71. integer tx_count = 0;
  72. integer busy_counter = 0;
  73. reg [7:0] captured_tx [0:511];
  74. // Deterministic busy pattern: the UART is not always ready.
  75. always @(posedge clk) begin
  76. busy_counter <= (busy_counter >= 3) ? 0 : busy_counter + 1;
  77. tx_ready <= (busy_counter != 0);
  78. if (tx_valid && tx_ready) begin
  79. captured_tx[tx_count] = tx_data;
  80. if (tx_data !== command_byte(tx_count % 43)) begin
  81. $display("FAIL bmc_ascii_poll_tb: command byte %0d = %02h, expected %02h",
  82. tx_count % 43, tx_data, command_byte(tx_count % 43));
  83. failures = failures + 1;
  84. end
  85. tx_count <= tx_count + 1;
  86. end
  87. end
  88. always @(posedge clk) polled = poll_count;
  89. // ---------------- raw device values and expected bytes ----------------
  90. // set A: pairs as the BMC prints them (first = integer C, second = fraction)
  91. reg [7:0] raw_first [0:23];
  92. reg [7:0] raw_second [0:23];
  93. reg [7:0] want_a [0:23];
  94. reg [7:0] want_b [0:23];
  95. reg [7:0] want_ff [0:23];
  96. task load_tables;
  97. begin
  98. // first second expected final byte
  99. raw_first[0] = 8'h28; raw_second[0] = 8'h32; want_a[0] = 8'h28; // 28 32 -> 28
  100. raw_first[1] = 8'h2a; raw_second[1] = 8'h4b; want_a[1] = 8'h2a; // 2A 4B -> 2A
  101. raw_first[2] = 8'h2b; raw_second[2] = 8'h19; want_a[2] = 8'h2b; // 2B 19 -> 2B
  102. raw_first[3] = 8'hff; raw_second[3] = 8'hff; want_a[3] = 8'hff; // FF FF -> FF
  103. raw_first[4] = 8'h00; raw_second[4] = 8'h00; want_a[4] = 8'h00; // real 0 C
  104. raw_first[5] = 8'h00; raw_second[5] = 8'h63; want_a[5] = 8'h00;
  105. raw_first[6] = 8'h7f; raw_second[6] = 8'h00; want_a[6] = 8'h7f;
  106. raw_first[7] = 8'hfe; raw_second[7] = 8'h63; want_a[7] = 8'hfe;
  107. raw_first[8] = 8'h80; raw_second[8] = 8'h00; want_a[8] = 8'h80;
  108. raw_first[9] = 8'hff; raw_second[9] = 8'h00; want_a[9] = 8'hff; // FF integer
  109. raw_first[10] = 8'h28; raw_second[10] = 8'hff; want_a[10] = 8'hff; // fraction 255
  110. raw_first[11] = 8'h28; raw_second[11] = 8'h64; want_a[11] = 8'hff; // fraction 100
  111. raw_first[12] = 8'h28; raw_second[12] = 8'h63; want_a[12] = 8'h28; // fraction 99
  112. raw_first[13] = 8'h0a; raw_second[13] = 8'h05; want_a[13] = 8'h0a;
  113. raw_first[14] = 8'h14; raw_second[14] = 8'h14; want_a[14] = 8'h14;
  114. raw_first[15] = 8'h1e; raw_second[15] = 8'h32; want_a[15] = 8'h1e;
  115. raw_first[16] = 8'h32; raw_second[16] = 8'h4b; want_a[16] = 8'h32;
  116. raw_first[17] = 8'h3c; raw_second[17] = 8'h19; want_a[17] = 8'h3c;
  117. raw_first[18] = 8'h46; raw_second[18] = 8'h00; want_a[18] = 8'h46;
  118. raw_first[19] = 8'h50; raw_second[19] = 8'h63; want_a[19] = 8'h50;
  119. raw_first[20] = 8'h5a; raw_second[20] = 8'h32; want_a[20] = 8'h5a;
  120. raw_first[21] = 8'h64; raw_second[21] = 8'h4b; want_a[21] = 8'h64;
  121. raw_first[22] = 8'h6e; raw_second[22] = 8'h19; want_a[22] = 8'h6e;
  122. raw_first[23] = 8'h78; raw_second[23] = 8'h00; want_a[23] = 8'h78;
  123. for (i = 0; i < 24; i = i + 1) begin
  124. want_b[i] = 8'd100 + i[7:0];
  125. want_ff[i] = 8'hff;
  126. end
  127. end
  128. endtask
  129. // ---------------- UART helpers ----------------
  130. task rx_byte;
  131. input [7:0] b;
  132. integer gap;
  133. begin
  134. gap = 0;
  135. @(negedge clk);
  136. rx_valid = 1; rx_data = b;
  137. @(negedge clk);
  138. rx_valid = 0;
  139. repeat (2) @(negedge clk);
  140. end
  141. endtask
  142. function [7:0] hex_char;
  143. input [3:0] value;
  144. input lower;
  145. begin
  146. if (value < 10) hex_char = "0" + value;
  147. else if (lower) hex_char = "a" + value - 10;
  148. else hex_char = "A" + value - 10;
  149. end
  150. endfunction
  151. task send_hex_byte;
  152. input [7:0] b;
  153. input lower;
  154. begin
  155. rx_byte(hex_char(b[7:4], lower));
  156. rx_byte(hex_char(b[3:0], lower));
  157. end
  158. endtask
  159. task send_echo;
  160. begin
  161. for (i = 0; i < 43; i = i + 1) rx_byte(command_byte(i[5:0]));
  162. end
  163. endtask
  164. task send_prompt;
  165. begin
  166. rx_byte("i"); rx_byte("p"); rx_byte("m"); rx_byte("i");
  167. rx_byte(">"); rx_byte(" ");
  168. end
  169. endtask
  170. task send_prompt_terminated;
  171. begin
  172. send_prompt();
  173. rx_byte(8'h0d); rx_byte(8'h0a);
  174. end
  175. endtask
  176. task send_real_prompt;
  177. begin
  178. rx_byte("r"); rx_byte("o"); rx_byte("o"); rx_byte("t");
  179. rx_byte("@"); rx_byte("S"); rx_byte("T"); rx_byte("0");
  180. rx_byte("0"); rx_byte("5"); rx_byte("2"); rx_byte("0");
  181. rx_byte("0"); rx_byte("0"); rx_byte("0"); rx_byte("0");
  182. rx_byte("1"); rx_byte("6"); rx_byte("4"); rx_byte("8");
  183. rx_byte("0"); rx_byte("0"); rx_byte("0"); rx_byte("6");
  184. rx_byte(":"); rx_byte("~"); rx_byte("#"); rx_byte(" ");
  185. end
  186. endtask
  187. task send_real_prompt_terminated;
  188. begin
  189. send_real_prompt();
  190. rx_byte(8'h0d); rx_byte(8'h0a);
  191. end
  192. endtask
  193. // Send both tokens of every pair in [first_pair .. last_pair], so the full
  194. // 48-token reply is 3 x send_tokens(0,7), send_tokens(8,15), (16,23).
  195. task send_tokens;
  196. input integer first_pair;
  197. input integer last_pair;
  198. input lower;
  199. begin
  200. for (i = first_pair; i <= last_pair; i = i + 1) begin
  201. send_hex_byte(raw_first[i], lower);
  202. rx_byte(" ");
  203. send_hex_byte(raw_second[i], lower);
  204. if (i != last_pair) rx_byte(" ");
  205. end
  206. end
  207. endtask
  208. task send_line;
  209. input integer first_pair;
  210. input integer last_pair;
  211. input lower;
  212. begin
  213. send_tokens(first_pair, last_pair, lower);
  214. rx_byte(8'h0d); rx_byte(8'h0a);
  215. end
  216. endtask
  217. task send_good_reply;
  218. input lower;
  219. begin
  220. send_echo();
  221. send_prompt();
  222. send_line(0, 7, lower);
  223. send_line(8, 15, lower);
  224. send_line(16, 23, lower);
  225. send_prompt_terminated();
  226. end
  227. endtask
  228. reg [7:0] tmp_byte;
  229. // Values of set B: first byte 100+i, fraction 32 (all valid, all < 0x80).
  230. task send_good_reply_b;
  231. begin
  232. send_echo();
  233. for (i = 0; i < 24; i = i + 1) begin
  234. tmp_byte = 100 + i;
  235. send_hex_byte(tmp_byte, 0);
  236. rx_byte(" ");
  237. send_hex_byte(8'h32, 0);
  238. if (i == 7 || i == 15) begin
  239. rx_byte(8'h0d); rx_byte(8'h0a);
  240. end else if (i != 23) begin
  241. rx_byte(" ");
  242. end
  243. end
  244. rx_byte(8'h0d); rx_byte(8'h0a);
  245. end
  246. endtask
  247. // ---------------- checks ----------------
  248. task read_ram;
  249. input [5:0] addr;
  250. output [7:0] result;
  251. begin
  252. @(negedge clk);
  253. temp_rd_addr = addr;
  254. @(posedge clk); #1;
  255. result = temp_rd_data;
  256. if (temp_rd_valid !== 1'b1) begin
  257. $display("FAIL bmc_ascii_poll_tb: rd_valid low at BMC address %0d", addr);
  258. failures = failures + 1;
  259. end
  260. end
  261. endtask
  262. task check_all;
  263. input [8*48-1:0] what;
  264. input integer use_b;
  265. input integer use_ff;
  266. begin
  267. for (i = 0; i < 24; i = i + 1) begin
  268. read_ram(i[5:0], value);
  269. if (use_ff) begin
  270. if (value !== 8'hff) begin
  271. $display("FAIL bmc_ascii_poll_tb: %0s channel %0d = %02h, expected FF",
  272. what, i, value);
  273. failures = failures + 1;
  274. end
  275. end else if (use_b) begin
  276. if (value !== want_b[i]) begin
  277. $display("FAIL bmc_ascii_poll_tb: %0s channel %0d = %02h, expected %02h",
  278. what, i, value, want_b[i]);
  279. failures = failures + 1;
  280. end
  281. end else begin
  282. if (value !== want_a[i]) begin
  283. $display("FAIL bmc_ascii_poll_tb: %0s channel %0d = %02h, expected %02h",
  284. what, i, value, want_a[i]);
  285. failures = failures + 1;
  286. end
  287. end
  288. end
  289. end
  290. endtask
  291. task wait_round;
  292. input integer round_number;
  293. begin
  294. wait (tx_count == 43 * round_number);
  295. end
  296. endtask
  297. // ---------------- main ----------------
  298. initial begin
  299. load_tables();
  300. // Guard the expected command itself: 42 characters plus LF, exactly the
  301. // text required by AGENTS.md. A typo here would make the TX comparison
  302. // vacuous, so it is checked before any round runs.
  303. if (command_byte(0) !== "i" || command_byte(8) !== " " ||
  304. command_byte(9) !== "r" || command_byte(12) !== " " ||
  305. command_byte(13) !== "0" || command_byte(14) !== "x" ||
  306. command_byte(41) !== "3" || command_byte(42) !== 8'h0a) begin
  307. $display("FAIL bmc_ascii_poll_tb: expected command text is wrong");
  308. failures = failures + 1;
  309. end
  310. repeat (4) @(negedge clk);
  311. rst_n = 1;
  312. // Before any round the BMC bytes must read FF, never X.
  313. check_all("never-sampled", 0, 1);
  314. // Round 1: normal reply with echo and prompt.
  315. wait_round(1);
  316. send_good_reply(0);
  317. wait (polled == 1);
  318. if (round_failed !== 1'b0) begin
  319. $display("FAIL bmc_ascii_poll_tb: round 1 reported failed");
  320. failures = failures + 1;
  321. end
  322. check_all("round1", 0, 0);
  323. // Round 2: a second consecutive good round with different values.
  324. wait_round(2);
  325. send_good_reply_b();
  326. wait (polled == 2);
  327. if (round_failed !== 1'b0) begin
  328. $display("FAIL bmc_ascii_poll_tb: round 2 reported failed");
  329. failures = failures + 1;
  330. end
  331. check_all("round2", 1, 0);
  332. // Round 3: reply with a truncated last line (40 tokens only).
  333. wait_round(3);
  334. send_echo();
  335. send_prompt();
  336. send_line(0, 7, 1);
  337. send_line(8, 15, 1);
  338. send_tokens(16, 19, 1);
  339. // no line end: the inter-byte gap closes and fails the round
  340. wait (polled == 3);
  341. if (round_failed !== 1'b1) begin
  342. $display("FAIL bmc_ascii_poll_tb: truncated round 3 not reported failed");
  343. failures = failures + 1;
  344. end
  345. check_all("round3-truncated", 0, 1);
  346. // Round 4: an oversized (more than two digit) hex token.
  347. wait_round(4);
  348. send_echo();
  349. send_prompt();
  350. send_line(0, 7, 1);
  351. rx_byte("1"); rx_byte("2"); rx_byte("3"); rx_byte("4"); // oversized token
  352. rx_byte(" ");
  353. send_tokens(8, 15, 1);
  354. rx_byte(8'h0d); rx_byte(8'h0a);
  355. send_tokens(16, 23, 1);
  356. rx_byte(8'h0d); rx_byte(8'h0a);
  357. wait (polled == 4);
  358. if (round_failed !== 1'b1) begin
  359. $display("FAIL bmc_ascii_poll_tb: garbage round 4 not reported failed");
  360. failures = failures + 1;
  361. end
  362. check_all("round4-garbage", 0, 1);
  363. // Round 5: no reply at all, bounded by the round timeout.
  364. wait_round(5);
  365. wait (polled == 5);
  366. if (round_failed !== 1'b1) begin
  367. $display("FAIL bmc_ascii_poll_tb: timed out round 5 not reported failed");
  368. failures = failures + 1;
  369. end
  370. check_all("round5-timeout", 0, 1);
  371. // A late, complete reply from round 5 arrives before round 6's command.
  372. send_good_reply(0);
  373. // Round 6: good reply (set B); the stale set A bytes must be gone.
  374. wait_round(6);
  375. send_good_reply_b();
  376. wait (polled == 6);
  377. if (round_failed !== 1'b0) begin
  378. $display("FAIL bmc_ascii_poll_tb: round 6 reported failed");
  379. failures = failures + 1;
  380. end
  381. check_all("round6-after-late", 1, 0);
  382. // Round 7: 48 good tokens followed by 16 more (concatenated round).
  383. wait_round(7);
  384. send_echo();
  385. send_line(0, 7, 1);
  386. send_line(8, 15, 1);
  387. send_line(16, 23, 1);
  388. send_tokens(0, 7, 1); // 16 further tokens from a "next" round
  389. rx_byte(8'h0d); rx_byte(8'h0a);
  390. wait (polled == 7);
  391. if (round_failed !== 1'b1) begin
  392. $display("FAIL bmc_ascii_poll_tb: concatenated round 7 not reported failed");
  393. failures = failures + 1;
  394. end
  395. check_all("round7-concatenated", 0, 1);
  396. // Round 8: data only (no echo, no prompt) must still be accepted.
  397. wait_round(8);
  398. send_line(0, 7, 0);
  399. send_line(8, 15, 0);
  400. send_line(16, 23, 0);
  401. wait (polled == 8);
  402. if (round_failed !== 1'b0) begin
  403. $display("FAIL bmc_ascii_poll_tb: data-only round 8 reported failed");
  404. failures = failures + 1;
  405. end
  406. check_all("round8-data-only", 0, 0);
  407. // Round 9: non-hex garbage inside a line that already carries data.
  408. wait_round(9);
  409. send_echo();
  410. send_tokens(0, 7, 1);
  411. rx_byte(" "); rx_byte("z"); rx_byte("z"); // garbage after data on this line
  412. rx_byte(8'h0d); rx_byte(8'h0a);
  413. send_tokens(8, 15, 1);
  414. rx_byte(8'h0d); rx_byte(8'h0a);
  415. send_tokens(16, 23, 1);
  416. rx_byte(8'h0d); rx_byte(8'h0a);
  417. wait (polled == 9);
  418. if (round_failed !== 1'b1) begin
  419. $display("FAIL bmc_ascii_poll_tb: in-line garbage round 9 not reported failed");
  420. failures = failures + 1;
  421. end
  422. // Round 10: real prompt root@ST00520000016480006:~# before and after data
  423. wait_round(10);
  424. send_echo();
  425. send_real_prompt();
  426. send_line(0, 7, 0);
  427. send_line(8, 15, 0);
  428. send_line(16, 23, 0);
  429. send_real_prompt_terminated();
  430. wait (polled == 10);
  431. if (round_failed !== 1'b0) begin
  432. $display("FAIL bmc_ascii_poll_tb: real-prompt round 10 reported failed");
  433. failures = failures + 1;
  434. end
  435. check_all("round10-real-prompt", 0, 0);
  436. if (tx_count != 43 * 10) begin
  437. $display("FAIL bmc_ascii_poll_tb: %0d command bytes sent, expected %0d",
  438. tx_count, 43 * 10);
  439. failures = failures + 1;
  440. end
  441. // The complete captured stream must be exactly eight copies of the
  442. // required 43-byte command: no prompt, no CR wake-up, no extra byte.
  443. for (i = 0; i < tx_count; i = i + 1) begin
  444. if (captured_tx[i] !== command_byte(i % 43)) begin
  445. $display("FAIL bmc_ascii_poll_tb: captured byte %0d = %02h, expected %02h",
  446. i, captured_tx[i], command_byte(i % 43));
  447. failures = failures + 1;
  448. end
  449. end
  450. if (failures == 0) $display("bmc_ascii_poll_tb PASS");
  451. else $fatal(1, "bmc_ascii_poll_tb FAIL (%0d checks)", failures);
  452. $finish;
  453. end
  454. initial begin
  455. #40000000;
  456. $fatal(1, "bmc_ascii_poll_tb FAIL: timeout waiting for the poll rounds");
  457. end
  458. endmodule