`timescale 1ns/1ps // Unit testbench for temp_ram. Expected values are independent constants: // the TB knows which addresses are channels, which values were written and // what a same-address write/read must return. Nothing is read back from the // DUT to build an expectation. // // Proven here: // * 64x8 single-write-port / synchronous-read RAM with a registered rd_valid. // * BMC 0..23 and CT 32..58 are channels; holes read FF with rd_valid low. // * a never-sampled channel reads exactly 8'hFF (never X), and a real // 0 C sample (0x00) survives as a valid byte. // * same-address write/read returns the OLD value in that cycle and the new // value one clock later (read-first semantics). // * two back-to-back reads of different channels return their own bytes // (no mixed channel). module temp_ram_tb; reg clk = 0; reg rst_n = 0; reg wr_en = 0; reg [5:0] wr_addr = 0; reg [7:0] wr_data = 0; reg [5:0] rd_addr = 0; wire [7:0] rd_data; wire rd_valid; integer failures = 0; integer i; always #5 clk = ~clk; temp_ram #(.DEPTH(64), .ADDR_WIDTH(6)) dut ( .clk(clk), .rst_n(rst_n), .wr_en(wr_en), .wr_addr(wr_addr), .wr_data(wr_data), .rd_addr(rd_addr), .rd_data(rd_data), .rd_valid(rd_valid) ); function channel_expected; input [5:0] addr; begin channel_expected = (addr <= 6'd23) || ((addr >= 6'd32) && (addr <= 6'd58)); end endfunction task fail; input [8*48-1:0] message; begin $display("FAIL temp_ram_tb: %0s", message); failures = failures + 1; end endtask // Present addr, then check rd_data/rd_valid exactly one clock later. task read_check; input [5:0] addr; input [7:0] expect_data; input expect_valid; begin @(negedge clk); rd_addr = addr; @(posedge clk); #1; if ($isunknown(rd_data)) begin $display("FAIL temp_ram_tb: rd_data is X at address %0d", addr); failures = failures + 1; end else if (rd_data !== expect_data) begin $display("FAIL temp_ram_tb: address %0d reads %02h, expected %02h", addr, rd_data, expect_data); failures = failures + 1; end if (rd_valid !== (expect_valid ? 1'b1 : 1'b0)) begin $display("FAIL temp_ram_tb: rd_valid=%b at address %0d, expected %b", rd_valid, addr, expect_valid); failures = failures + 1; end end endtask task write_byte; input [5:0] addr; input [7:0] value; begin @(negedge clk); wr_en = 1; wr_addr = addr; wr_data = value; @(negedge clk); wr_en = 0; wr_data = 0; end endtask initial begin repeat (4) @(negedge clk); rst_n = 1; repeat (2) @(negedge clk); // 1. Every address is defined before any write; holes are not channels. for (i = 0; i < 64; i = i + 1) read_check(i[5:0], 8'hff, channel_expected(i[5:0])); // 2. Writes land in their own channel only. write_byte(6'd0, 8'h00); // real 0 C, must stay a valid 00 write_byte(6'd5, 8'hff); // explicit invalid write_byte(6'd23, 8'hc4); write_byte(6'd32, 8'hfe); write_byte(6'd58, 8'h7f); write_byte(6'd24, 8'h5a); // hole: dropped write_byte(6'd31, 8'h5a); // hole: dropped write_byte(6'd59, 8'h5a); // hole: dropped write_byte(6'd63, 8'h5a); // hole: dropped read_check(6'd0, 8'h00, 1'b1); read_check(6'd5, 8'hff, 1'b1); read_check(6'd23, 8'hc4, 1'b1); read_check(6'd32, 8'hfe, 1'b1); read_check(6'd58, 8'h7f, 1'b1); read_check(6'd24, 8'hff, 1'b0); read_check(6'd31, 8'hff, 1'b0); read_check(6'd59, 8'hff, 1'b0); read_check(6'd63, 8'hff, 1'b0); // 3. Same-address write/read: old value during the write cycle, new // value one clock later. read_check(6'd7, 8'hff, 1'b1); // never sampled before @(negedge clk); rd_addr = 6'd7; wr_en = 1; wr_addr = 6'd7; wr_data = 8'h2a; @(posedge clk); #1; // write and read coincide if (rd_data !== 8'hff) begin $display("FAIL temp_ram_tb: same-address write/read returned %02h, expected old value ff", rd_data); failures = failures + 1; end @(negedge clk); wr_en = 0; @(posedge clk); #1; // one clock after the write if (rd_data !== 8'h2a) begin $display("FAIL temp_ram_tb: new value not visible one clock after the write: %02h", rd_data); failures = failures + 1; end read_check(6'd7, 8'h2a, 1'b1); // 4. Adjacent different channels are never mixed. write_byte(6'd8, 8'ha1); write_byte(6'd9, 8'hb2); write_byte(6'd33, 8'hc3); read_check(6'd8, 8'ha1, 1'b1); read_check(6'd9, 8'hb2, 1'b1); read_check(6'd33, 8'hc3, 1'b1); read_check(6'd7, 8'h2a, 1'b1); // 5. A channel that was never written after reset still reads FF even // while other channels hold real data. read_check(6'd10, 8'hff, 1'b1); read_check(6'd57, 8'hff, 1'b1); if (failures == 0) $display("temp_ram_tb PASS"); else $fatal(1, "temp_ram_tb FAIL (%0d checks)", failures); $finish; end endmodule