A Parameterized Memory Model
FIFO queues, processor cache memories, register files, and bus controllers require embedded Random Access Memory (RAM) blocks. Developing a robust, parameterized RAM model provides a reusable building block for digital system design. This memory architecture forms the storage core for the FIFO structures developed in Section 7.4.
Parameterized Single-Port RAM
A single-port RAM shares an address bus between read and write transactions:
`timescale 1ns / 1ps
`default_nettype none
module ram_sp #(
parameter DATA_WIDTH = 8,
parameter DEPTH = 256,
parameter ADDR_WIDTH = $clog2(DEPTH)
) (
input wire clk,
input wire we,
input wire [ADDR_WIDTH-1:0] addr,
input wire [DATA_WIDTH-1:0] data_in,
output reg [DATA_WIDTH-1:0] data_out
);
reg [DATA_WIDTH-1:0] mem [0:DEPTH-1];
always @(posedge clk) begin
if (we) mem[addr] <= data_in;
data_out <= mem[addr]; // Synchronous registered read
end
endmodule
Parameters define each dimension.
The parameter DATA_WIDTH configures word width.
The parameter DEPTH defines the word count.
The parameter ADDR_WIDTH derives automatically using $clog2(DEPTH).
Read-During-Write Memory Behavior
Examine the always procedural block during write cycles.
When read and write operations access the identical address in the same clock cycle, non-blocking assignments cause data_out to capture the previous memory value.
This configuration implements read-first memory behavior.
Hardware memory generators require designers to specify one of three collision behaviors:
| Collision Mode | Behavior During Simultaneous Read and Write | Common Application |
|---|---|---|
| Read-first | data_out outputs the old stored value; write operation completes normally. |
FIFO buffers and pipeline stages requiring previous data access. |
| Write-first | data_out forwards the newly written value immediately. |
Processor register files requiring same-cycle data forwarding. |
| No-change | data_out retains its previous registered value during writes. |
Low-power designs and ROM structures with inactive read ports. |
Downstream logic expects a specific collision behavior. Using an incorrect model causes functional mismatches when replacing simulation models with physical silicon SRAM macros.
To implement write-first behavior, use explicit bypass logic:
always @(posedge clk) begin
if (we) begin
mem[addr] <= data_in;
data_out <= data_in; // Write-first: forward new write data immediately
end else begin
data_out <= mem[addr];
end
end
Always select and document the required collision behavior during architectural planning.
Parameterized Dual-Port RAM
Dual-port memories provide separate read and write address ports. This structure allows simultaneous write and read operations at different memory locations:
module ram_dp #(
parameter DATA_WIDTH = 8,
parameter DEPTH = 256,
parameter ADDR_WIDTH = $clog2(DEPTH)
) (
input wire clk,
input wire we,
input wire [ADDR_WIDTH-1:0] waddr,
input wire [DATA_WIDTH-1:0] wdata,
input wire [ADDR_WIDTH-1:0] raddr,
output reg [DATA_WIDTH-1:0] rdata
);
reg [DATA_WIDTH-1:0] mem [0:DEPTH-1];
always @(posedge clk) begin
if (we) mem[waddr] <= wdata;
rdata <= mem[raddr]; // Independent read port
end
endmodule
Synchronous FIFOs require this dual-port architecture.
The write pointer drives waddr while the read pointer drives raddr independently.
Preloading Simulation Memory
Verification environments often require preloading memory arrays with test images, firmware binaries, or lookup tables.
Use the $readmemh system task inside an initial block:
initial begin
$readmemh("init_data.hex", mem);
end
The simulator reads hexadecimal values sequentially from the text file starting at index 0.
Most FPGA synthesis tools support $readmemh to initialize block RAM memories in generated bitstreams.
Verification Exercise
Develop a self-checking testbench for ram_dp.
Write distinct data patterns across all addresses from 0 to 15.
Read all data back across the next 16 clock cycles to verify data integrity.
Finally, execute a simultaneous read and write transaction to the identical address to verify read-first behavior.
View Solution
`timescale 1ns / 1ps
`default_nettype none
module tb_ram_dp;
parameter DATA_WIDTH = 8;
parameter DEPTH = 16;
reg clk = 0, we;
reg [$clog2(DEPTH)-1:0] waddr, raddr;
reg [DATA_WIDTH-1:0] wdata;
wire [DATA_WIDTH-1:0] rdata;
ram_dp #(.DATA_WIDTH(DATA_WIDTH), .DEPTH(DEPTH)) dut (
.clk(clk), .we(we), .waddr(waddr), .wdata(wdata),
.raddr(raddr), .rdata(rdata)
);
always #5 clk = ~clk;
integer i, errors = 0;
initial begin
$dumpfile("tb.vcd");
$dumpvars(0, tb_ram_dp);
we = 0; waddr = 0; wdata = 0; raddr = 0;
@(negedge clk);
// Write unique values across all memory addresses
we = 1;
for (i = 0; i < DEPTH; i = i + 1) begin
waddr = i; wdata = i * 2 + 1;
@(negedge clk);
end
we = 0;
// Read and verify stored values
for (i = 0; i < DEPTH; i = i + 1) begin
raddr = i;
@(negedge clk);
if (rdata !== i * 2 + 1) begin
$display("FAIL: addr %0d expected %0d got %0d", i, i * 2 + 1, rdata);
errors = errors + 1;
end
end
// Simultaneous read and write transaction at address 5
waddr = 5; wdata = 8'hFF; we = 1; raddr = 5;
@(negedge clk);
we = 0;
if (rdata === 5 * 2 + 1)
$display("PASS: Verified read-first behavior, rdata=%0d", rdata);
else begin
$display("FAIL: Expected previous value %0d, got %0d", 5 * 2 + 1, rdata);
errors = errors + 1;
end
if (errors == 0) $display("ALL TESTS PASSED");
$finish;
end
endmodule
Synchronous read operations introduce a one-clock-cycle latency.
The output signal rdata reflects the address requested on the previous rising clock edge.
The testbench applies stimulus on negative clock edges to avoid setup timing ambiguity.
Parameterizing data widths and address depths creates modular memory components. The next article applies parameterization techniques to multi-function arithmetic datapaths.