bmc_round_bram_tb.v 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. `timescale 1ns/1ps
  2. // Public-interface regression for the synchronous round buffer. No force of
  3. // parser or RAM internals. Exercises complete/empty/short rounds and timeout
  4. // flushing of the very first pair (read-before-write risk) without a delimiter.
  5. module bmc_round_bram_tb;
  6. reg clk=0;
  7. always #20 clk=~clk;
  8. reg rst_n=0,rx_valid=0;
  9. reg [7:0] rx_data=0;
  10. wire tx_valid,wr_en,round_failed,poll_start,poll_done,raw_rx_valid;
  11. wire [7:0] tx_data,wr_data,raw_rx_data;
  12. wire [5:0] wr_addr;
  13. wire [15:0] poll_count;
  14. wire [1:0] eol_mode;
  15. wire [4:0] valid_count;
  16. bmc_ascii_poll #(.CLOCK_HZ(115200),.POLL_SECONDS(1),
  17. .ROUND_TIMEOUT_CLKS(2000)) dut(
  18. .clk(clk),.rst_n(rst_n),.tx_valid(tx_valid),.tx_data(tx_data),.tx_ready(1'b1),
  19. .rx_valid(rx_valid),.rx_data(rx_data),.wr_en(wr_en),.wr_addr(wr_addr),.wr_data(wr_data),
  20. .poll_count(poll_count),.round_failed(round_failed),.eol_mode(eol_mode),
  21. .poll_start(poll_start),.poll_done(poll_done),.valid_count(valid_count),
  22. .raw_rx_valid(raw_rx_valid),.raw_rx_data(raw_rx_data));
  23. reg [7:0] captured[0:23],expected[0:23];
  24. integer writes=0,rounds=0,i;
  25. always @(negedge clk) if(rst_n && wr_en) begin
  26. if(wr_addr !== writes[5:0] || writes>=24) begin
  27. $display("FAIL BMC write ordering round=%0d addr=%0d expected=%0d",rounds,wr_addr,writes);
  28. $fatal(1);
  29. end
  30. captured[wr_addr]=wr_data;
  31. writes=writes+1;
  32. end
  33. function [7:0] hexchar;
  34. input [3:0] nibble;
  35. begin hexchar=(nibble<10) ? "0"+nibble : "a"+nibble-10; end
  36. endfunction
  37. task byte_in;
  38. input [7:0] value;
  39. begin
  40. @(negedge clk);rx_data=value;rx_valid=1;
  41. @(negedge clk);rx_valid=0;
  42. end
  43. endtask
  44. task token;
  45. input [7:0] value;
  46. input delimiter;
  47. begin
  48. byte_in(hexchar(value[7:4]));byte_in(hexchar(value[3:0]));
  49. if(delimiter)byte_in(" ");
  50. end
  51. endtask
  52. task pair;
  53. input [7:0] first,second;
  54. input delimiter;
  55. begin token(first,1'b1);token(second,delimiter); end
  56. endtask
  57. task begin_round;
  58. begin
  59. @(posedge poll_start);
  60. writes=0;
  61. for(i=0;i<24;i=i+1)expected[i]=8'hff;
  62. // 43 command bytes plus fixed 10-clock drain have completed.
  63. repeat(60)@(posedge clk);
  64. end
  65. endtask
  66. task check_round;
  67. input integer good;
  68. begin
  69. @(posedge poll_done);
  70. @(negedge clk);#1;
  71. if(writes!==24 || valid_count!==good[4:0] || round_failed!==(good==0) || poll_count!==rounds+1) begin
  72. $display("FAIL BMC metadata r=%0d writes=%0d valid=%0d fail=%b count=%0d",rounds,writes,valid_count,round_failed,poll_count);
  73. $fatal(1);
  74. end
  75. for(i=0;i<24;i=i+1)if(captured[i]!==expected[i])begin
  76. $display("FAIL BMC round=%0d index=%0d got=%02x want=%02x",rounds,i,captured[i],expected[i]);
  77. $fatal(1);
  78. end
  79. rounds=rounds+1;
  80. end
  81. endtask
  82. initial begin
  83. repeat(5)@(posedge clk);@(negedge clk);rst_n=1;
  84. // Full normal round fills every address, making stale tail reuse visible.
  85. begin_round;
  86. for(i=0;i<24;i=i+1)begin expected[i]=30+i;pair(30+i,0,1);end
  87. check_round(24);
  88. // No response must replace all previous values with FF.
  89. begin_round;check_round(0);
  90. // First complete pair ends at timeout, without any trailing delimiter.
  91. begin_round;expected[0]=8'd77;pair(77,0,0);check_round(1);
  92. // Short response plus empty/out-of-range DIMMs; unwritten tail stays FF.
  93. begin_round;
  94. expected[0]=8'd25;pair(25,0,1);
  95. pair(8'hff,8'hff,1);
  96. pair(126,0,1);
  97. expected[3]=8'd125;pair(125,99,1);
  98. check_round(2);
  99. // Unmatched first token is not a completed pair.
  100. begin_round;token(55,1);check_round(0);
  101. // Full range with final pair flushed by timeout, rather than delimiter.
  102. begin_round;
  103. for(i=0;i<24;i=i+1)begin expected[i]=80+i;pair(80+i,0,i!=23);end
  104. check_round(24);
  105. // Another empty round proves previous full data is masked again.
  106. begin_round;check_round(0);
  107. // Zero Celsius remains a valid sample.
  108. begin_round;expected[0]=0;pair(0,0,1);check_round(1);
  109. $display("PASS bmc_round_bram_tb rounds=%0d",rounds);
  110. $finish;
  111. end
  112. initial begin
  113. repeat(2000000)@(posedge clk);
  114. $display("FAIL BMC test timeout");$fatal(1);
  115. end
  116. endmodule