temp_ram_tb.v 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. `timescale 1ns/1ps
  2. // Unit testbench for temp_ram. Expected values are independent constants:
  3. // the TB knows which addresses are channels, which values were written and
  4. // what a same-address write/read must return. Nothing is read back from the
  5. // DUT to build an expectation.
  6. //
  7. // Proven here:
  8. // * 64x8 single-write-port / synchronous-read RAM with a registered rd_valid.
  9. // * BMC 0..23 and CT 32..58 are channels; holes read FF with rd_valid low.
  10. // * a never-sampled channel reads exactly 8'hFF (never X), and a real
  11. // 0 C sample (0x00) survives as a valid byte.
  12. // * same-address write/read returns the OLD value in that cycle and the new
  13. // value one clock later (read-first semantics).
  14. // * two back-to-back reads of different channels return their own bytes
  15. // (no mixed channel).
  16. module temp_ram_tb;
  17. reg clk = 0;
  18. reg rst_n = 0;
  19. reg wr_en = 0;
  20. reg [5:0] wr_addr = 0;
  21. reg [7:0] wr_data = 0;
  22. reg [5:0] rd_addr = 0;
  23. wire [7:0] rd_data;
  24. wire rd_valid;
  25. integer failures = 0;
  26. integer i;
  27. always #5 clk = ~clk;
  28. temp_ram #(.DEPTH(64), .ADDR_WIDTH(6)) dut (
  29. .clk(clk), .rst_n(rst_n), .wr_en(wr_en), .wr_addr(wr_addr), .wr_data(wr_data),
  30. .rd_addr(rd_addr), .rd_data(rd_data), .rd_valid(rd_valid)
  31. );
  32. function channel_expected;
  33. input [5:0] addr;
  34. begin
  35. channel_expected = (addr <= 6'd23) || ((addr >= 6'd32) && (addr <= 6'd58));
  36. end
  37. endfunction
  38. task fail;
  39. input [8*48-1:0] message;
  40. begin
  41. $display("FAIL temp_ram_tb: %0s", message);
  42. failures = failures + 1;
  43. end
  44. endtask
  45. // Present addr, then check rd_data/rd_valid exactly one clock later.
  46. task read_check;
  47. input [5:0] addr;
  48. input [7:0] expect_data;
  49. input expect_valid;
  50. begin
  51. @(negedge clk);
  52. rd_addr = addr;
  53. @(posedge clk); #1;
  54. if ($isunknown(rd_data)) begin
  55. $display("FAIL temp_ram_tb: rd_data is X at address %0d", addr);
  56. failures = failures + 1;
  57. end else if (rd_data !== expect_data) begin
  58. $display("FAIL temp_ram_tb: address %0d reads %02h, expected %02h",
  59. addr, rd_data, expect_data);
  60. failures = failures + 1;
  61. end
  62. if (rd_valid !== (expect_valid ? 1'b1 : 1'b0)) begin
  63. $display("FAIL temp_ram_tb: rd_valid=%b at address %0d, expected %b",
  64. rd_valid, addr, expect_valid);
  65. failures = failures + 1;
  66. end
  67. end
  68. endtask
  69. task write_byte;
  70. input [5:0] addr;
  71. input [7:0] value;
  72. begin
  73. @(negedge clk);
  74. wr_en = 1; wr_addr = addr; wr_data = value;
  75. @(negedge clk);
  76. wr_en = 0; wr_data = 0;
  77. end
  78. endtask
  79. initial begin
  80. repeat (4) @(negedge clk);
  81. rst_n = 1;
  82. repeat (2) @(negedge clk);
  83. // 1. Every address is defined before any write; holes are not channels.
  84. for (i = 0; i < 64; i = i + 1)
  85. read_check(i[5:0], 8'hff, channel_expected(i[5:0]));
  86. // 2. Writes land in their own channel only.
  87. write_byte(6'd0, 8'h00); // real 0 C, must stay a valid 00
  88. write_byte(6'd5, 8'hff); // explicit invalid
  89. write_byte(6'd23, 8'hc4);
  90. write_byte(6'd32, 8'hfe);
  91. write_byte(6'd58, 8'h7f);
  92. write_byte(6'd24, 8'h5a); // hole: dropped
  93. write_byte(6'd31, 8'h5a); // hole: dropped
  94. write_byte(6'd59, 8'h5a); // hole: dropped
  95. write_byte(6'd63, 8'h5a); // hole: dropped
  96. read_check(6'd0, 8'h00, 1'b1);
  97. read_check(6'd5, 8'hff, 1'b1);
  98. read_check(6'd23, 8'hc4, 1'b1);
  99. read_check(6'd32, 8'hfe, 1'b1);
  100. read_check(6'd58, 8'h7f, 1'b1);
  101. read_check(6'd24, 8'hff, 1'b0);
  102. read_check(6'd31, 8'hff, 1'b0);
  103. read_check(6'd59, 8'hff, 1'b0);
  104. read_check(6'd63, 8'hff, 1'b0);
  105. // 3. Same-address write/read: old value during the write cycle, new
  106. // value one clock later.
  107. read_check(6'd7, 8'hff, 1'b1); // never sampled before
  108. @(negedge clk);
  109. rd_addr = 6'd7;
  110. wr_en = 1; wr_addr = 6'd7; wr_data = 8'h2a;
  111. @(posedge clk); #1; // write and read coincide
  112. if (rd_data !== 8'hff) begin
  113. $display("FAIL temp_ram_tb: same-address write/read returned %02h, expected old value ff",
  114. rd_data);
  115. failures = failures + 1;
  116. end
  117. @(negedge clk);
  118. wr_en = 0;
  119. @(posedge clk); #1; // one clock after the write
  120. if (rd_data !== 8'h2a) begin
  121. $display("FAIL temp_ram_tb: new value not visible one clock after the write: %02h",
  122. rd_data);
  123. failures = failures + 1;
  124. end
  125. read_check(6'd7, 8'h2a, 1'b1);
  126. // 4. Adjacent different channels are never mixed.
  127. write_byte(6'd8, 8'ha1);
  128. write_byte(6'd9, 8'hb2);
  129. write_byte(6'd33, 8'hc3);
  130. read_check(6'd8, 8'ha1, 1'b1);
  131. read_check(6'd9, 8'hb2, 1'b1);
  132. read_check(6'd33, 8'hc3, 1'b1);
  133. read_check(6'd7, 8'h2a, 1'b1);
  134. // 5. A channel that was never written after reset still reads FF even
  135. // while other channels hold real data.
  136. read_check(6'd10, 8'hff, 1'b1);
  137. read_check(6'd57, 8'hff, 1'b1);
  138. if (failures == 0) $display("temp_ram_tb PASS");
  139. else $fatal(1, "temp_ram_tb FAIL (%0d checks)", failures);
  140. $finish;
  141. end
  142. endmodule