| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- `timescale 1ns/1ps
- // Testbench for bmc_ascii_poll + temp_ram with a virtual BMC UART model.
- //
- // The model accepts TX bytes only through a real valid/ready handshake (ready
- // is deliberately pulled low in a fixed pattern, so a byte that is not held
- // would be lost) and compares every byte with the required 43-byte command.
- // The reply is pushed byte by byte on the RX side. Expected temperature bytes
- // are literal constants - nothing is derived from a DUT output.
- //
- // Rounds:
- // 1. normal three-line 48-token reply with echo and prompt -> exact 24 bytes
- // 2. a second consecutive good round with different values -> exact 24 bytes
- // 3. reply with a truncated last line -> 24 x 0xFF
- // 4. reply with an oversized hex token -> 24 x 0xFF
- // 5. no reply at all (round timeout) -> 24 x 0xFF
- // 6. late full reply from round 5 before this round -> exact 24 bytes
- // (the stale reply must be discarded, never concatenated)
- // 7. 48 good tokens plus a further 16 (concatenated round) -> 24 x 0xFF
- // 8. data-only reply without echo or prompt -> exact 24 bytes
- // 9. non-hex garbage inside a data line -> 24 x 0xFF
- module bmc_ascii_poll_tb;
- localparam integer CLOCK_HZ = 10000;
- localparam integer POLL_SECONDS = 1;
- localparam integer ROUND_TIMEOUT_CLKS = 5000;
- localparam integer INTER_BYTE_TIMEOUT_CLKS = 64;
- reg clk = 0;
- reg rst_n = 0;
- always #5 clk = ~clk;
- integer failures = 0;
- integer i;
- reg [7:0] value;
- integer polled = 0;
- wire tx_valid;
- wire [7:0] tx_data;
- reg tx_ready = 1;
- reg rx_valid = 0;
- reg [7:0] rx_data = 0;
- wire wr_en;
- wire [5:0] wr_addr;
- wire [7:0] wr_data;
- wire [15:0] poll_count;
- wire round_failed;
- reg [5:0] temp_rd_addr = 0;
- wire [7:0] temp_rd_data;
- wire temp_rd_valid;
- bmc_ascii_poll #(
- .CLOCK_HZ(CLOCK_HZ),
- .POLL_SECONDS(POLL_SECONDS),
- .ROUND_TIMEOUT_CLKS(ROUND_TIMEOUT_CLKS),
- .INTER_BYTE_TIMEOUT_CLKS(INTER_BYTE_TIMEOUT_CLKS)
- ) dut (
- .clk(clk), .rst_n(rst_n),
- .tx_valid(tx_valid), .tx_data(tx_data), .tx_ready(tx_ready),
- .rx_valid(rx_valid), .rx_data(rx_data),
- .wr_en(wr_en), .wr_addr(wr_addr), .wr_data(wr_data),
- .poll_count(poll_count), .round_failed(round_failed)
- );
- temp_ram #(.DEPTH(64), .ADDR_WIDTH(6)) u_ram (
- .clk(clk), .rst_n(rst_n), .wr_en(wr_en), .wr_addr(wr_addr), .wr_data(wr_data),
- .rd_addr(temp_rd_addr), .rd_data(temp_rd_data), .rd_valid(temp_rd_valid)
- );
- // ---------------- virtual UART ----------------
- localparam [8*43-1:0] COMMAND =
- {"ipmitool raw 0x36 0x6c 0xdf 0xdd 0x00 0x03", 8'h0a};
- function [7:0] command_byte;
- input [5:0] index;
- begin
- command_byte = COMMAND[(8 * (6'd42 - index)) +: 8];
- end
- endfunction
- integer tx_count = 0;
- integer busy_counter = 0;
- reg [7:0] captured_tx [0:511];
- // Deterministic busy pattern: the UART is not always ready.
- always @(posedge clk) begin
- busy_counter <= (busy_counter >= 3) ? 0 : busy_counter + 1;
- tx_ready <= (busy_counter != 0);
- if (tx_valid && tx_ready) begin
- captured_tx[tx_count] = tx_data;
- if (tx_data !== command_byte(tx_count % 43)) begin
- $display("FAIL bmc_ascii_poll_tb: command byte %0d = %02h, expected %02h",
- tx_count % 43, tx_data, command_byte(tx_count % 43));
- failures = failures + 1;
- end
- tx_count <= tx_count + 1;
- end
- end
- always @(posedge clk) polled = poll_count;
- // ---------------- raw device values and expected bytes ----------------
- // set A: pairs as the BMC prints them (first = integer C, second = fraction)
- reg [7:0] raw_first [0:23];
- reg [7:0] raw_second [0:23];
- reg [7:0] want_a [0:23];
- reg [7:0] want_b [0:23];
- reg [7:0] want_ff [0:23];
- task load_tables;
- begin
- // first second expected final byte
- raw_first[0] = 8'h28; raw_second[0] = 8'h32; want_a[0] = 8'h28; // 28 32 -> 28
- raw_first[1] = 8'h2a; raw_second[1] = 8'h4b; want_a[1] = 8'h2a; // 2A 4B -> 2A
- raw_first[2] = 8'h2b; raw_second[2] = 8'h19; want_a[2] = 8'h2b; // 2B 19 -> 2B
- raw_first[3] = 8'hff; raw_second[3] = 8'hff; want_a[3] = 8'hff; // FF FF -> FF
- raw_first[4] = 8'h00; raw_second[4] = 8'h00; want_a[4] = 8'h00; // real 0 C
- raw_first[5] = 8'h00; raw_second[5] = 8'h63; want_a[5] = 8'h00;
- raw_first[6] = 8'h7f; raw_second[6] = 8'h00; want_a[6] = 8'h7f;
- raw_first[7] = 8'hfe; raw_second[7] = 8'h63; want_a[7] = 8'hfe;
- raw_first[8] = 8'h80; raw_second[8] = 8'h00; want_a[8] = 8'h80;
- raw_first[9] = 8'hff; raw_second[9] = 8'h00; want_a[9] = 8'hff; // FF integer
- raw_first[10] = 8'h28; raw_second[10] = 8'hff; want_a[10] = 8'hff; // fraction 255
- raw_first[11] = 8'h28; raw_second[11] = 8'h64; want_a[11] = 8'hff; // fraction 100
- raw_first[12] = 8'h28; raw_second[12] = 8'h63; want_a[12] = 8'h28; // fraction 99
- raw_first[13] = 8'h0a; raw_second[13] = 8'h05; want_a[13] = 8'h0a;
- raw_first[14] = 8'h14; raw_second[14] = 8'h14; want_a[14] = 8'h14;
- raw_first[15] = 8'h1e; raw_second[15] = 8'h32; want_a[15] = 8'h1e;
- raw_first[16] = 8'h32; raw_second[16] = 8'h4b; want_a[16] = 8'h32;
- raw_first[17] = 8'h3c; raw_second[17] = 8'h19; want_a[17] = 8'h3c;
- raw_first[18] = 8'h46; raw_second[18] = 8'h00; want_a[18] = 8'h46;
- raw_first[19] = 8'h50; raw_second[19] = 8'h63; want_a[19] = 8'h50;
- raw_first[20] = 8'h5a; raw_second[20] = 8'h32; want_a[20] = 8'h5a;
- raw_first[21] = 8'h64; raw_second[21] = 8'h4b; want_a[21] = 8'h64;
- raw_first[22] = 8'h6e; raw_second[22] = 8'h19; want_a[22] = 8'h6e;
- raw_first[23] = 8'h78; raw_second[23] = 8'h00; want_a[23] = 8'h78;
- for (i = 0; i < 24; i = i + 1) begin
- want_b[i] = 8'd100 + i[7:0];
- want_ff[i] = 8'hff;
- end
- end
- endtask
- // ---------------- UART helpers ----------------
- task rx_byte;
- input [7:0] b;
- integer gap;
- begin
- gap = 0;
- @(negedge clk);
- rx_valid = 1; rx_data = b;
- @(negedge clk);
- rx_valid = 0;
- repeat (2) @(negedge clk);
- end
- endtask
- function [7:0] hex_char;
- input [3:0] value;
- input lower;
- begin
- if (value < 10) hex_char = "0" + value;
- else if (lower) hex_char = "a" + value - 10;
- else hex_char = "A" + value - 10;
- end
- endfunction
- task send_hex_byte;
- input [7:0] b;
- input lower;
- begin
- rx_byte(hex_char(b[7:4], lower));
- rx_byte(hex_char(b[3:0], lower));
- end
- endtask
- task send_echo;
- begin
- for (i = 0; i < 43; i = i + 1) rx_byte(command_byte(i[5:0]));
- end
- endtask
- task send_prompt;
- begin
- rx_byte("i"); rx_byte("p"); rx_byte("m"); rx_byte("i");
- rx_byte(">"); rx_byte(" ");
- end
- endtask
- task send_prompt_terminated;
- begin
- send_prompt();
- rx_byte(8'h0d); rx_byte(8'h0a);
- end
- endtask
- task send_real_prompt;
- begin
- rx_byte("r"); rx_byte("o"); rx_byte("o"); rx_byte("t");
- rx_byte("@"); rx_byte("S"); rx_byte("T"); rx_byte("0");
- rx_byte("0"); rx_byte("5"); rx_byte("2"); rx_byte("0");
- rx_byte("0"); rx_byte("0"); rx_byte("0"); rx_byte("0");
- rx_byte("1"); rx_byte("6"); rx_byte("4"); rx_byte("8");
- rx_byte("0"); rx_byte("0"); rx_byte("0"); rx_byte("6");
- rx_byte(":"); rx_byte("~"); rx_byte("#"); rx_byte(" ");
- end
- endtask
- task send_real_prompt_terminated;
- begin
- send_real_prompt();
- rx_byte(8'h0d); rx_byte(8'h0a);
- end
- endtask
- // Send both tokens of every pair in [first_pair .. last_pair], so the full
- // 48-token reply is 3 x send_tokens(0,7), send_tokens(8,15), (16,23).
- task send_tokens;
- input integer first_pair;
- input integer last_pair;
- input lower;
- begin
- for (i = first_pair; i <= last_pair; i = i + 1) begin
- send_hex_byte(raw_first[i], lower);
- rx_byte(" ");
- send_hex_byte(raw_second[i], lower);
- if (i != last_pair) rx_byte(" ");
- end
- end
- endtask
- task send_line;
- input integer first_pair;
- input integer last_pair;
- input lower;
- begin
- send_tokens(first_pair, last_pair, lower);
- rx_byte(8'h0d); rx_byte(8'h0a);
- end
- endtask
- task send_good_reply;
- input lower;
- begin
- send_echo();
- send_prompt();
- send_line(0, 7, lower);
- send_line(8, 15, lower);
- send_line(16, 23, lower);
- send_prompt_terminated();
- end
- endtask
- reg [7:0] tmp_byte;
- // Values of set B: first byte 100+i, fraction 32 (all valid, all < 0x80).
- task send_good_reply_b;
- begin
- send_echo();
- for (i = 0; i < 24; i = i + 1) begin
- tmp_byte = 100 + i;
- send_hex_byte(tmp_byte, 0);
- rx_byte(" ");
- send_hex_byte(8'h32, 0);
- if (i == 7 || i == 15) begin
- rx_byte(8'h0d); rx_byte(8'h0a);
- end else if (i != 23) begin
- rx_byte(" ");
- end
- end
- rx_byte(8'h0d); rx_byte(8'h0a);
- end
- endtask
- // ---------------- checks ----------------
- task read_ram;
- input [5:0] addr;
- output [7:0] result;
- begin
- @(negedge clk);
- temp_rd_addr = addr;
- @(posedge clk); #1;
- result = temp_rd_data;
- if (temp_rd_valid !== 1'b1) begin
- $display("FAIL bmc_ascii_poll_tb: rd_valid low at BMC address %0d", addr);
- failures = failures + 1;
- end
- end
- endtask
- task check_all;
- input [8*48-1:0] what;
- input integer use_b;
- input integer use_ff;
- begin
- for (i = 0; i < 24; i = i + 1) begin
- read_ram(i[5:0], value);
- if (use_ff) begin
- if (value !== 8'hff) begin
- $display("FAIL bmc_ascii_poll_tb: %0s channel %0d = %02h, expected FF",
- what, i, value);
- failures = failures + 1;
- end
- end else if (use_b) begin
- if (value !== want_b[i]) begin
- $display("FAIL bmc_ascii_poll_tb: %0s channel %0d = %02h, expected %02h",
- what, i, value, want_b[i]);
- failures = failures + 1;
- end
- end else begin
- if (value !== want_a[i]) begin
- $display("FAIL bmc_ascii_poll_tb: %0s channel %0d = %02h, expected %02h",
- what, i, value, want_a[i]);
- failures = failures + 1;
- end
- end
- end
- end
- endtask
- task wait_round;
- input integer round_number;
- begin
- wait (tx_count == 43 * round_number);
- end
- endtask
- // ---------------- main ----------------
- initial begin
- load_tables();
- // Guard the expected command itself: 42 characters plus LF, exactly the
- // text required by AGENTS.md. A typo here would make the TX comparison
- // vacuous, so it is checked before any round runs.
- if (command_byte(0) !== "i" || command_byte(8) !== " " ||
- command_byte(9) !== "r" || command_byte(12) !== " " ||
- command_byte(13) !== "0" || command_byte(14) !== "x" ||
- command_byte(41) !== "3" || command_byte(42) !== 8'h0a) begin
- $display("FAIL bmc_ascii_poll_tb: expected command text is wrong");
- failures = failures + 1;
- end
- repeat (4) @(negedge clk);
- rst_n = 1;
- // Before any round the BMC bytes must read FF, never X.
- check_all("never-sampled", 0, 1);
- // Round 1: normal reply with echo and prompt.
- wait_round(1);
- send_good_reply(0);
- wait (polled == 1);
- if (round_failed !== 1'b0) begin
- $display("FAIL bmc_ascii_poll_tb: round 1 reported failed");
- failures = failures + 1;
- end
- check_all("round1", 0, 0);
- // Round 2: a second consecutive good round with different values.
- wait_round(2);
- send_good_reply_b();
- wait (polled == 2);
- if (round_failed !== 1'b0) begin
- $display("FAIL bmc_ascii_poll_tb: round 2 reported failed");
- failures = failures + 1;
- end
- check_all("round2", 1, 0);
- // Round 3: reply with a truncated last line (40 tokens only).
- wait_round(3);
- send_echo();
- send_prompt();
- send_line(0, 7, 1);
- send_line(8, 15, 1);
- send_tokens(16, 19, 1);
- // no line end: the inter-byte gap closes and fails the round
- wait (polled == 3);
- if (round_failed !== 1'b1) begin
- $display("FAIL bmc_ascii_poll_tb: truncated round 3 not reported failed");
- failures = failures + 1;
- end
- check_all("round3-truncated", 0, 1);
- // Round 4: an oversized (more than two digit) hex token.
- wait_round(4);
- send_echo();
- send_prompt();
- send_line(0, 7, 1);
- rx_byte("1"); rx_byte("2"); rx_byte("3"); rx_byte("4"); // oversized token
- rx_byte(" ");
- send_tokens(8, 15, 1);
- rx_byte(8'h0d); rx_byte(8'h0a);
- send_tokens(16, 23, 1);
- rx_byte(8'h0d); rx_byte(8'h0a);
- wait (polled == 4);
- if (round_failed !== 1'b1) begin
- $display("FAIL bmc_ascii_poll_tb: garbage round 4 not reported failed");
- failures = failures + 1;
- end
- check_all("round4-garbage", 0, 1);
- // Round 5: no reply at all, bounded by the round timeout.
- wait_round(5);
- wait (polled == 5);
- if (round_failed !== 1'b1) begin
- $display("FAIL bmc_ascii_poll_tb: timed out round 5 not reported failed");
- failures = failures + 1;
- end
- check_all("round5-timeout", 0, 1);
- // A late, complete reply from round 5 arrives before round 6's command.
- send_good_reply(0);
- // Round 6: good reply (set B); the stale set A bytes must be gone.
- wait_round(6);
- send_good_reply_b();
- wait (polled == 6);
- if (round_failed !== 1'b0) begin
- $display("FAIL bmc_ascii_poll_tb: round 6 reported failed");
- failures = failures + 1;
- end
- check_all("round6-after-late", 1, 0);
- // Round 7: 48 good tokens followed by 16 more (concatenated round).
- wait_round(7);
- send_echo();
- send_line(0, 7, 1);
- send_line(8, 15, 1);
- send_line(16, 23, 1);
- send_tokens(0, 7, 1); // 16 further tokens from a "next" round
- rx_byte(8'h0d); rx_byte(8'h0a);
- wait (polled == 7);
- if (round_failed !== 1'b1) begin
- $display("FAIL bmc_ascii_poll_tb: concatenated round 7 not reported failed");
- failures = failures + 1;
- end
- check_all("round7-concatenated", 0, 1);
- // Round 8: data only (no echo, no prompt) must still be accepted.
- wait_round(8);
- send_line(0, 7, 0);
- send_line(8, 15, 0);
- send_line(16, 23, 0);
- wait (polled == 8);
- if (round_failed !== 1'b0) begin
- $display("FAIL bmc_ascii_poll_tb: data-only round 8 reported failed");
- failures = failures + 1;
- end
- check_all("round8-data-only", 0, 0);
- // Round 9: non-hex garbage inside a line that already carries data.
- wait_round(9);
- send_echo();
- send_tokens(0, 7, 1);
- rx_byte(" "); rx_byte("z"); rx_byte("z"); // garbage after data on this line
- rx_byte(8'h0d); rx_byte(8'h0a);
- send_tokens(8, 15, 1);
- rx_byte(8'h0d); rx_byte(8'h0a);
- send_tokens(16, 23, 1);
- rx_byte(8'h0d); rx_byte(8'h0a);
- wait (polled == 9);
- if (round_failed !== 1'b1) begin
- $display("FAIL bmc_ascii_poll_tb: in-line garbage round 9 not reported failed");
- failures = failures + 1;
- end
- // Round 10: real prompt root@ST00520000016480006:~# before and after data
- wait_round(10);
- send_echo();
- send_real_prompt();
- send_line(0, 7, 0);
- send_line(8, 15, 0);
- send_line(16, 23, 0);
- send_real_prompt_terminated();
- wait (polled == 10);
- if (round_failed !== 1'b0) begin
- $display("FAIL bmc_ascii_poll_tb: real-prompt round 10 reported failed");
- failures = failures + 1;
- end
- check_all("round10-real-prompt", 0, 0);
- if (tx_count != 43 * 10) begin
- $display("FAIL bmc_ascii_poll_tb: %0d command bytes sent, expected %0d",
- tx_count, 43 * 10);
- failures = failures + 1;
- end
- // The complete captured stream must be exactly eight copies of the
- // required 43-byte command: no prompt, no CR wake-up, no extra byte.
- for (i = 0; i < tx_count; i = i + 1) begin
- if (captured_tx[i] !== command_byte(i % 43)) begin
- $display("FAIL bmc_ascii_poll_tb: captured byte %0d = %02h, expected %02h",
- i, captured_tx[i], command_byte(i % 43));
- failures = failures + 1;
- end
- end
- if (failures == 0) $display("bmc_ascii_poll_tb PASS");
- else $fatal(1, "bmc_ascii_poll_tb FAIL (%0d checks)", failures);
- $finish;
- end
- initial begin
- #40000000;
- $fatal(1, "bmc_ascii_poll_tb FAIL: timeout waiting for the poll rounds");
- end
- endmodule
|