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.

A Write and a Read of Word 5 at One EdgeEach cycle ends at a rising clock edge. rdata shows what the edge before produced.
012clkwewaddr5awdataa53craddr5rdataxxa5after the write edge: still xx
Scroll horizontally ↔
  1. Cycle 0

    The bench writes a5 to word 5 and reads word 5 in the same cycle. No edge has come yet.

  2. Cycle 1

    One edge later rdata is xx. The read took the old content of word 5, and that word was never written.

  3. Cycle 2

    After the next edge rdata is 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 bitsAddresses
420 to 3
1640 to f
256800 to ff
1,02410000 to 3ff
65,536160000 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.

A four-word ROM. The words are the seven-segment codes of the digits 0 to 3 (G4.1).
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
end

This 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.

The RAM of this lesson: 16 words of 8 bits, with a synchronous read.
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
end
Three Writes Light Three WordsThe grid draws the 16 words as 4 rows of 4. The row label is the address of its first word. lane is the position in the row, taken from the low two address bits.
  • Test
  • Storage
  • I/O
wewaddrwdatardataBenchdrives both portsmem: 16 wordslane01230x00x40x80xcReadersees rdata
we→ mem: 16 wordswaddr→ mem: 16 wordswdata→ mem: 16 wordsrdata→ ReaderBenchdrives both portsmem: 16 wordslane01230x00x40x80xcReadersees rdata

A token on wdata means the next edge writes that word. A lit cell is a word that has been written.

0123456clkwewaddr5a5lane001001000010wdataa53c7eraddr5a50rdataxxa53c7exx
Scroll horizontally ↔
  1. Cycle 0

    we is 1, so the edge at the end of this cycle writes a5 to word 5. Word 5 is in row 0x4, lane 1.

  2. Cycle 1

    Word 5 now holds a5. rdata is xx: the read at that edge took the old content of word 5.

  3. Cycle 2

    rdata is a5, one edge after the write. This cycle writes 3c to word a: row 0x8, lane 2.

  4. Cycle 3

    A second write to word 5 replaces a5 with 7e. The read address is now a.

  5. Cycle 4

    rdata is 3c, the content of word a. we is 0, so no word changes at this edge.

  6. Cycle 6

    rdata is xx again. 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.

An 8 x 8 register file: 8 words of 8 bits, one write port and two asynchronous read ports.
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
endmodule

A 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 fileFlip-flopsCells in allLogic levels
8 x 8642585
16 x 81285617
32 x 82561,1629
64 x 85122,35911
32 x 321,0244,4749

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.

The Six-Transistor Memory CellP1 and N1 form one inverter. P2 and N2 form the other. Each inverter drives the input of the other one. A1 and A2 are the access transistors. WL is the word line and BL, BLB are the two bit lines.
The six-transistor memory cellVDD1.8 VP1N1P2N2A1A2WLWLBLBLBQQB
  1. VDD · supply1.8 Vvdd → VDD
  2. P1 · PMOS transistorgate → QB · source → VDD · drain → Q
  3. N1 · NMOS transistorgate → QB · drain → Q · source → 0
  4. P2 · PMOS transistorgate → Q · source → VDD · drain → QB
  5. N2 · NMOS transistorgate → Q · drain → QB · source → 0
  6. gnd · groundgnd → 0
  7. A1 · NMOS transistorgate → n1 · drain → Q · source → n2
  8. A2 · NMOS transistorgate → n3 · drain → n4 · source → QB
  9. WL · portterminal → n1
  10. WL · portterminal → n3
  11. BL · portterminal → n2
  12. BLB · portterminal → n4
  1. Hold: the word line is 0

    A1 and A2 are off, so the bit lines cannot reach the cell. Q is 1, so N2 holds QB at 0. QB is 0, so P1 holds Q at 1. The loop keeps the bit while the supply is on.

  2. Write a 0: the word line is 1

    The write circuit drives BL to 0 and BLB to 1. A1 turns on and pulls Q down, against P1. Then the loop flips: QB rises to 1 and N1 holds Q at 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
  1. 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).

  2. 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.

  3. 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.

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: 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=xx

Original 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

Predict the outputThe bench changes wdata before the second edge. Predict the exact line the program prints.
module tb;
  logic       clk = 0, we = 0;
  logic [3:0] addr = 4'd7;
  logic [7:0] wdata = 8'hc3;
  logic [7:0] mem [16];
  always_ff @(posedge clk)
    if (we) mem[addr] <= wdata;
  initial begin
    we = 1;
    #1 clk = 1; #1 clk = 0;     // edge 1
    we = 0; wdata = 8'h4d;
    #1 clk = 1; #1 clk = 0;     // edge 2
    $display("%h", mem[addr]);
  end
endmodule
Spot the bugEach option is the body of a 16-word memory. The bench sets raddr and looks at rdata 1 ns later, with no clock edge between. Which body fails?
ConceptA register file has 32 words of 32 bits and is built from flip-flops. How many flip-flops hold its data?
Read the waveformThe RAM has a synchronous read. Each cycle ends at a rising edge. Cycle 0 writes 3c to word a, and raddr is a in cycle 1. In which cycle does rdata first show 3c?
A write to word a, then a read of it
012clkwewaddra5wdata3c7eraddr5a5rdataa53c
Scroll horizontally ↔

Cycles 2 to 4 of the measured trace above, numbered here from 0.

ConceptWhat holds the bit in a six-transistor memory cell while the word line is 0?

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.

Before
logic [7:0] mem [15];   // words 0 to 14
After
logic [7:0] mem [16];   // words 0 to 15

Wrong 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.

Before
logic [7:0] mem [16];
logic [2:0] addr;   // reaches words 0 to 7 only
After
logic [7:0] mem [16];
logic [3:0] addr;   // reaches words 0 to 15

Wrong 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 case statement 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.

Practice

Report a problem with this page