ROM, RAM and Register Files
Describe a memory by its address, data and control ports, and build a small ROM, RAM and register file.
You Will Be Able To
- Work out the address width a memory of a given depth needs.
- Write a ROM, a RAM and a register file in SystemVerilog.
- Predict the read data of a synchronous RAM at the write edge.
- Explain why a large memory is a macro and not a block of flip-flops.
- Lesson
- G8.1
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A designer wrote a word to a RAM and read the same address in that cycle. The next stage used the read data one clock later and got the old word. The RAM was correct. Its read port samples the memory at the edge, before the write lands.
rdata shows what the edge before produced.- Cycle 0
The bench writes a5 to word 5 and reads word 5 in the same cycle. No edge has come yet.
- Cycle 1
One edge later
rdataisxx. The read took the old content of word 5, and that word was never written. - Cycle 2
After the next edge
rdatais a5. The new word arrives one clock later than the designer expected.
Measured on Icarus Verilog: the first three lines of the example.
Address, Data and Control
A memory is a row of numbered words. The number of a word is its address. The number of words is the depth, and the number of bits in a word is the width.
Every memory port has three kinds of signal. The address picks one word. The data lines carry that word. A control signal such as the write enable we says what to do.
An address of n bits reaches 2n words. The depth therefore sets the address width.
| Depth (words) | Address bits | Addresses |
|---|---|---|
| 4 | 2 | 0 to 3 |
| 16 | 4 | 0 to f |
| 256 | 8 | 00 to ff |
| 1,024 | 10 | 000 to 3ff |
| 65,536 | 16 | 0000 to ffff |
Each extra address bit doubles the depth. In code, $clog2(DEPTH) gives the number of address bits.
In SystemVerilog a memory is an array: logic [7:0] mem [16]; declares 16 words of 8 bits. The expression mem[addr] is the word at that address.
A ROM Is a Lookup Table
A ROM (read-only memory) holds fixed words. The address goes in and the stored word comes out. A ROM has no write port and needs no clock.
always_comb begin
case (addr)
2'd0: data = 8'h3f;
2'd1: data = 8'h06;
2'd2: data = 8'h5b;
2'd3: data = 8'h4f;
endcase
endThis is the case statement of G4.1, read as a table. Every address has an arm, so the block makes no latch. Synthesis turns a small ROM into gates.
A larger ROM is an array that a file fills at the start of the simulation. The system task $readmemh reads that file.
Writing and Reading a RAM
A RAM can be written as well as read. A write happens at the clock edge while we is 1. The word at waddr then takes the value of wdata.
logic [7:0] mem [16];
always_ff @(posedge clk) begin
if (we) mem[waddr] <= wdata;
rdata <= mem[raddr]; // reads the word as it was before this edge
endlane is the position in the row, taken from the low two address bits.- Test
- Storage
- I/O
A token on wdata means the next edge writes that word. A lit cell is a word that has been written.
- Cycle 0
weis 1, so the edge at the end of this cycle writes a5 to word 5. Word 5 is in row 0x4, lane 1. - Cycle 1
Word 5 now holds a5.
rdataisxx: the read at that edge took the old content of word 5. - Cycle 2
rdatais a5, one edge after the write. This cycle writes 3c to word a: row 0x8, lane 2. - Cycle 3
A second write to word 5 replaces a5 with 7e. The read address is now a.
- Cycle 4
rdatais 3c, the content of word a.weis 0, so no word changes at this edge. - Cycle 6
rdataisxxagain. Word 0 was never written, and this memory has no reset.
Measured on Icarus Verilog: the values are the lines the example prints for edges 0 to 5.
The read is synchronous: it happens at the clock edge, like the write. At one edge the read takes the old word and the write stores the new one. The new word shows one edge later.
An asynchronous read uses no clock. The word follows the address in the same cycle, like a ROM.
Synchronous read
always_ff @(posedge clk) begin
if (we) mem[waddr] <= wdata;
rdata <= mem[raddr];
end
Icarus: a write and a read of word 5 at edge 0 give rdata=xx. Edge 1 gives a5. Yosys: 136 flip-flops.
Asynchronous read
always_ff @(posedge clk)
if (we) mem[waddr] <= wdata;
assign rdata = mem[raddr];
Icarus: right after edge 0 the line already shows rdata=a5. Yosys: 128 flip-flops.
The synchronous version has 8 more flip-flops: the register that holds rdata. Large RAM blocks have a synchronous read, so plan for the extra clock.
The Register File
A register file is a small memory made of flip-flops. A processor keeps its working values there. Many register files have one write port and two read ports, so one instruction can read two operands.
module regfile (
input logic clk, we,
input logic [2:0] waddr, raddr1, raddr2,
input logic [7:0] wdata,
output logic [7:0] rdata1, rdata2
);
logic [7:0] mem [8];
always_ff @(posedge clk)
if (we) mem[waddr] <= wdata;
assign rdata1 = mem[raddr1]; // read port 1
assign rdata2 = mem[raddr2]; // read port 2
endmoduleA read port is a mux that picks one word. Two read ports are two muxes on the same storage. Both ports may read the same word in one cycle.
| Register file | Flip-flops | Cells in all | Logic levels |
|---|---|---|---|
| 8 x 8 | 64 | 258 | 5 |
| 16 x 8 | 128 | 561 | 7 |
| 32 x 8 | 256 | 1,162 | 9 |
| 64 x 8 | 512 | 2,359 | 11 |
| 32 x 32 | 1,024 | 4,474 | 9 |
Measured with Yosys, the synthesis tool of this site, with two read ports each. Every doubling of the depth adds two logic levels to the read path.
Every bit costs one flip-flop. The other cells are the read muxes and the write decoder. Both grow with the depth, and the read path gets slower.
Lesson R5.1 in the RTL Design track continues with read ports and memory inference.
Why Big Memories Are Macros
A chip does not build a large RAM from flip-flops. The designer places a memory macro: a ready-made block with its own small storage cells. The common cell has six transistors.
WL is the word line and BL, BLB are the two bit lines.- Hold: the word line is 0
A1 and A2 are off, so the bit lines cannot reach the cell.
Qis 1, so N2 holdsQBat 0.QBis 0, so P1 holdsQat 1. The loop keeps the bit while the supply is on. - Write a 0: the word line is 1
The write circuit drives
BLto 0 andBLBto 1. A1 turns on and pullsQdown, against P1. Then the loop flips:QBrises to 1 and N1 holdsQat 0.pulls Q to 0
Read the circuit as text
- P1 PMOS transistor: gate →
QB, source →VDD, drain →Q - N1 NMOS transistor: gate →
QB, drain →Q, source →0 - P2 PMOS transistor: gate →
Q, source →VDD, drain →QB - N2 NMOS transistor: gate →
Q, drain →QB, source →0 - A1 NMOS transistor: gate →
n1, drain →Q, source →n2 - A2 NMOS transistor: gate →
n3, drain →n4, source →QB - WL port: terminal →
n1 - WL port: terminal →
n3 - BL port: terminal →
n2 - BLB port: terminal →
n4
Measured with ngspice on SKY130: with WL at 0 the cell held Q at 1.8 V. A 1 ns pulse on WL wrote the 0.
Two inverters in a loop hold the bit, as in the bistable of G5.4. Two access transistors connect the loop to the bit lines when the word line is 1. The address decoder raises one word line at a time.
A flip-flop is two latches. Built from transmission gates and inverters (G9.3), each latch has eight transistors. A static flip-flop built this way has 16 for one bit, and the cell needs 6.
Asked in interviews
How many address bits does a memory of 1,024 words need?
Ten bits, because 210 is 1,024. In general the address width is the base-2 logarithm of the depth, rounded up. In code that is
$clog2(DEPTH).What does a synchronous RAM return when you read and write one address in the same cycle?
The answer depends on the RAM. The one in this lesson returns the old word, which is called read-first. The new word shows one edge later. Always check the behaviour of the memory you use.
Why is a large memory not built from flip-flops?
A flip-flop needs many more transistors than a six-transistor memory cell. A flip-flop memory also needs a wide read mux for each port. A memory macro packs the cells in a dense array and shares the bit lines.
Try It
Each line shows the ports that the edge sampled and the rdata it produced. Edge 0 writes a5 to word 5 and reads word 5. The line shows rdata=xx: the read took the old content. Edge 1 shows a5.
Edge 3 writes word 5 again, and edge 4 reads 7e from it. Edge 5 reads word 0. That word was never written, so the line shows xx.
Make the read asynchronous. Delete the line rdata <= mem[raddr]; and add assign rdata = mem[raddr]; before endmodule. Run again: the line for edge 0 now shows rdata=a5. Press Synthesize both times: Yosys builds 136 flip-flops, then 128.
Synthesize the design to measure its cells and inspect the mapped gates.
before any edge: rdata=xx e0 we=1 waddr=5 wdata=a5 raddr=5 rdata=xx e1 we=0 waddr=5 wdata=a5 raddr=5 rdata=a5 e2 we=1 waddr=a wdata=3c raddr=5 rdata=a5 e3 we=1 waddr=5 wdata=7e raddr=a rdata=3c e4 we=0 waddr=5 wdata=7e raddr=5 rdata=7e e5 we=0 waddr=5 wdata=7e raddr=0 rdata=xxOriginal design · 289 cells · 136 flip-flops · 0 latches · 4 logic levels. Synthesis uses Yosys; counts depend on the toolchain.
Read included file: tb.sv
// tb.sv: the bench (read-only). It makes the clock, drives the ports and prints one line per edge.
`timescale 1ns/1ps
module tb;
logic clk = 0;
logic we = 0;
logic [3:0] waddr = 4'h0, raddr = 4'h0;
logic [7:0] wdata = 8'h00;
logic [7:0] rdata;
int edge_n = -1;
always #5 clk = ~clk; // a 10 ns clock: rising edges at 5, 15, 25, ... ns
always @(posedge clk) edge_n = edge_n + 1; // counts the rising edges from 0
ram16x8 dut (.clk(clk), .we(we), .waddr(waddr), .wdata(wdata), .raddr(raddr), .rdata(rdata));
// Wait for the next rising edge, let the memory update, then print one line.
task automatic tick();
@(posedge clk); #1;
$display("e%0d we=%b waddr=%h wdata=%h raddr=%h rdata=%h", edge_n, we, waddr, wdata, raddr, rdata);
endtask
initial begin
#1 $display("before any edge: rdata=%h", rdata);
we = 1; waddr = 4'h5; wdata = 8'ha5; raddr = 4'h5; tick(); // edge 0: write word 5 and read word 5
we = 0; tick(); // edge 1: read word 5 again
we = 1; waddr = 4'ha; wdata = 8'h3c; tick(); // edge 2: write word a
waddr = 4'h5; wdata = 8'h7e; raddr = 4'ha; tick(); // edge 3: write word 5 again, read word a
we = 0; raddr = 4'h5; tick(); // edge 4: read word 5
raddr = 4'h0; tick(); // edge 5: read word 0, never written
$finish(0);
end
endmodule
Check Yourself
Common Mistakes
Wrong Declare 16 words as mem [15].
The number in the brackets is the count of words, so the last word is 14: Icarus reads xx from word 15.
Fix Write the depth itself: mem [16] holds words 0 to 15.
logic [7:0] mem [15]; // words 0 to 14logic [7:0] mem [16]; // words 0 to 15Wrong Give the address one bit too few.
The top address bit is lost, so address 13 of a 16-word memory reads word 5.
Fix Use $clog2(DEPTH) address bits: 4 bits for 16 words.
logic [7:0] mem [16];
logic [2:0] addr; // reaches words 0 to 7 onlylogic [7:0] mem [16];
logic [3:0] addr; // reaches words 0 to 15Wrong Read a word that was never written.
The RAM of this lesson has no reset, so the word holds x: the example reads xx from word 0 on Icarus.
Fix Write every word before the first read, or track which words are valid.
Wrong Build a large RAM from flip-flops.
Every bit costs a flip-flop and part of a read mux: 64 words of 8 bits are 2,359 cells in Yosys.
Fix Use flip-flops for small register files and a memory macro for large memories.
Remember
- A memory port has an address, data and control. n address bits reach 2n words.
- A ROM is a lookup table: a
casestatement with one arm per address. - A synchronous read gives the word one edge after the address, and the old word at a write edge.
- A register file is a small flip-flop memory, often with two read ports.
- Large memories are macros of six-transistor cells; flip-flops cost far more per bit.