Memories in an SoC: SRAM, ROM, Flash, OTP and DRAM

A chip stores bits in six different ways, and each way is right for one kind of job. Three questions tell you which.

You Will Be Able To

  • Choose a kind of memory for a job by three questions.
  • Say what goes into a ROM, and why a ROM is kept small.
  • Tell a register file from an SRAM, and say when each is the right one.
Lesson
S3.1
Time
24 min
Runs on
Your browser
Check
6 questions

A block diagram shows six boxes, and each one says "memory". They are built in six different ways. Swap any two of them, and the product stops working, or cannot be updated, or costs twice as much.

Before You Start

You need S0.7: ROM, RAM and flash, and why a large memory is slow. You need S0.6: the registers inside a processor.

You also need flip-flops, and sizes in bytes.

Five questions this page answers
Question
1Which three questions pick a kind of memory for a job?
2Which six kinds does a chip use, and what is each one for?
3What goes into a ROM, and why is a ROM kept small?
4How does a register file differ from an SRAM?
5When are flip-flops the right memory, and when a ready-made memory block?

The Idea: Six Ways to Store Bits, Chosen by Three Questions

S0.7 gave three kinds of memory. A real chip uses six, and it chooses with three questions. Must the content survive with the power off? Who writes it, and how often? How many bits are there? Sizes below are in kibibytes (KiB) of 1,024 bytes.

Six ways to store bits
KindSurvives power off?Who writes, and how oftenTypical sizeWhat it is forIn ap_soc
ROMyesnobody: fixed when the chip is madekibibytesthe first instructions after a resetthe Boot ROM, 4 KiB at 0x00000000
One-time programmable bitsyesonce: a bit that is set stays seta few thousand bitskeys, identity numbers, settings of one chipnone
Flashyesa program, slowly, one erased block at a timemebibytesthe program of the product, and its settingsnone on the chip: a flash chip on the board
Register filenoin every cycle, from several sides at oncea few dozen wordsthe working registers of a processorthe registers inside ap_rv32
SRAMnoany program, at full speedkibibytes to a few mebibytesworking data, and cachesthe SRAM, 64 KiB at 0x80000000
DRAMnoany program, and it must be refreshedgigabytes, in chips of their ownthe main memory of a phone or a computernone

The memories of ap_soc are three of the rows. The sizes come from its memory map.

Each answer has a reason behind it, and a price. Memory that survives is slow to write, or cannot be written at all. Memory that is fast to write forgets. Memory with very many bits is slow, and needs help from outside. No kind does everything, so a chip uses several.

This page goes deeper into two of the six. The first is the ROM. Its content is part of the chip itself, like the wiring. That makes it the one memory a chip can trust at its first instruction, and the one memory nobody can ever fix.

The second is the register file. It is built from flip-flops, so it can do several things in one cycle. An SRAM packs its bits far more tightly, and pays for that: it does one thing at each clock edge. Knowing where one ends and the other begins is a daily choice in chip design.

Flash and one-time bits, DRAM, and the pins of an SRAM each get a page of their own. They are the next three pages of this module.

New Words

New words on this page
WordMeaning
Volatile memoryMemory that loses its content when the power goes off. Memory that keeps it is non-volatile.
SRAMStatic RAM. Each bit is a small loop of transistors that holds its value for as long as the power is on.
DRAMDynamic RAM. Each bit is a tiny electric charge, which leaks away. A later page of this module explains it.
OTPOne-time programmable memory: bits that can each be set once and never cleared.
Contents of a ROMThe words that a ROM holds. They are given as a file when the chip is designed, and are built into it.
Boot codeThe first program that runs after a reset. It prepares the chip and then starts the real program.
Register fileA very small memory inside a processor, built from flip-flops. It can be read in two places and written in one, all in one cycle.
Memory macroA ready-made memory block, made by a generator tool for one size. The designer gets its pins and timing, not its inside.
Port of a memoryOne set of address, data and control pins. One port does one access at a time.
Read port and write portA port that can only read, and a port that can only write.

One Picture: The Three Questions

Three Questions Sort Six Kinds of MemoryStep through the picture. Each step asks one question, and the six kinds fall into place.
  • Processor
  • Memory
Three Questions Sort Six Kinds of MemoryStep through the picture. Each step asks one question, and the six kinds fall into place. component A storage job some bits the chipmust hold component ROM never written component One-time bits written once component Flash written now and then component Register file a few dozen words,several ports component SRAM kibibytes tomebibytes, on the chip component DRAM gigabytes, in chips ofits own must survive poweroffmay be lostmore writingmore writingmore bitsmore bits
  1. component A storage jobsome bits the chip must hold
    • must survive power off → ROM
    • may be lost → Register file
  2. component ROMnever written
    • more writing → One-time bits
  3. component Register filea few dozen words, several ports
    • more bits → SRAM
  4. component One-time bitswritten once
    • more writing → Flash
  5. component Flashwritten now and then
  6. component SRAMkibibytes to mebibytes, on the chip
    • more bits → DRAM
  7. component DRAMgigabytes, in chips of its own
  1. A job, and no memory yet

    Every memory on a chip starts as a job: some bits that must be held. The kind is chosen afterwards.

  2. Question 1: must it survive with the power off?

    The answer splits the six kinds into two rows of three. The top row keeps its content, and the bottom row loses it.

  3. Question 2: who writes it, and how often?

    Among the kinds that survive, this question decides. Never is a ROM, once is one-time bits, now and then is flash.

  4. Question 3: how many bits?

    Among the kinds that are written at full speed, size decides. A few dozen words, some kibibytes, or gigabytes.

  5. All six

    Three questions, six kinds. This page works on the ROM and on the register file. Each other kind has a page of its own in this module.

Read the diagram as text
  • A storage job: some bits the chip must hold
  • ROM: never written
  • One-time bits: written once
  • Flash: written now and then
  • Register file: a few dozen words, several ports
  • SRAM: kibibytes to mebibytes, on the chip
  • DRAM: gigabytes, in chips of its own
  • A storage job → ROM: must survive power off
  • A storage job → Register file: may be lost
  • ROM → One-time bits: more writing
  • One-time bits → Flash: more writing
  • Register file → SRAM: more bits
  • SRAM → DRAM: more bits

The kinds of memory of a chip. ap_soc has a ROM, an SRAM and the registers of its processor.

The Smallest Example, Worked by Hand

First the three questions, on four jobs. Cover the last column and try each row.

Four jobs, three questions
JobSurvive power off?Who writes, how oftenHow many bitsKind
The first instructions after a resetyesnobody, ever4 KiBROM
The identity number of this one chipyesthe factory, once64 bitsone-time bits
The stack of a running programnothe program, all the time64 KiBSRAM
Two numbers to add and a place for the sumnothe processor, in every cyclea few wordsregister file

Now the ROM of ap_soc. It has 4 KiB, which is 4,096 bytes or 1,024 words of four bytes. That is all the code the chip can ever run before it reaches another memory. So the boot code does very little: it sets up the chip, finds the real program in flash, and jumps to it.

Last, the register file against the SRAM. The job is the last row of the table: add word 1 and word 2, and store the sum in word 4. Count the clock edges that each memory needs.

One sum, two memories
MemoryPortsEdge 1Edge 2Edge 3Edges
Register filetwo read ports, one write portread word 1, read word 2, write word 41
SRAMone portread word 1read word 2write word 43

The register file reads without waiting for an edge, so the sum is ready to be written at edge 1.

The register file wins this job by three to one. It would lose badly on size. Its eight words are 256 flip-flops. The 64 KiB of SRAM would be 524,288 flip-flops, and a flip-flop takes several times the area of an SRAM bit.

The Rules

  1. Rule 1: Choose a memory by three questions.

    Must it survive with the power off, who writes it and how often, and how many bits are there.

  2. Rule 2: The contents of a ROM are fixed when the chip is made.

    A ROM has an address input and a data output. It has no write side at all.

  3. Rule 3: Put into a ROM only what must be there first, and keep it small.

    A mistake in a ROM cannot be repaired. It costs a new version of the chip.

  4. Rule 4: A register file is flip-flops with several ports.

    It can read in two places and write in one, in a single cycle. Each bit is large.

  5. Rule 5: A memory macro packs bits tightly behind one port.

    It does one access at each clock edge.

  6. Rule 6: Use flip-flops for a few dozen words, and a macro for the rest.

    Flip-flops are right when the memory is tiny, or when it needs several accesses in one cycle.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.2: SRAM, DRAM and flash, and what each costs.

The textbook "CMOS VLSI Design" by Weste and Harris: SRAM and memories with several ports in section 12.2, DRAM in 12.3, ROM and flash in 12.4, fuses and antifuses in 3.4.3.8.

The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 5.5: memory arrays, register files (5.5.5) and ROM (5.5.6).

The limit of "a few dozen words" in rule 6 is a rule of thumb, not a number from a book. The real limit depends on the process and on the memory generator. Our reference library holds no data sheet of a memory generator.

The sizes of the Boot ROM and of the SRAM come from the memory map of ap_soc.

Now Run It

The lab at the end of this page puts three memories side by side. The ROM and the register file are a few lines of test code. The SRAM is the model of the SRAM of ap_soc. Both writable memories start with 5 in word 1 and 7 in word 2.

Printed by the lab on Icarus.
rom   address 2 gives b0070022, with no clock edge
regs  word 4 = 12 after 1 edge
sram  word 4 = 12 after 3 edges

Rule 2 at work: the first row. The ROM gets address 2 and gives its word, b0070022. Look at its code in the lab: there is an address and a word, and nothing to write with. The four words are made up for this page.

Rule 4 at work: the row "regs". The register file read two words and wrote their sum, 12, at one edge. Rule 5 at work: the row "sram". The same sum took three edges, as in the hand table.

This lab leaves out how an SRAM is driven pin by pin. A later page of this module does that.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Boot code in a ROMBoot code in flashIt is there at the first instruction, and no program can damage it. It can never be updated.
A small ROM that jumps to flashThe whole program in ROMA bug in the program is fixed by an update. The chip needs a second memory.
A register file of flip-flopsA small memory macroThree accesses in one cycle. Each bit takes several times the area.
A memory macro for working dataThe same words as flip-flopsSeveral times more bits in the same area. One access at each edge, and fixed timing.

The Trap: The Bug That Was Cast in Metal

A team puts a large start-up program into the ROM: a protocol for a cable, a check of the product code, a menu. It is 3,000 lines of code, and it works in simulation. The chips come back, and one board in fifty stops at start-up. The fault is a wait loop in that program, one count too short.

Nobody can patch it. The first row of the lab showed why: a ROM has an address and a word, and no way in. Rule 3 names the price. The team must make the chip again, which costs months.

A careful boot ROM is short enough to be read line by line by several people. It does only what cannot wait, and it leaves every clever feature to code that an update can replace.

At Scale

ap_soc1 ROM, 1 SRAM, 1 register file
A phone chiphundreds of macros, DRAM and flash outside

This section looks ahead. A large chip has hundreds of memory macros, and all six kinds around it.

  1. The boot chain

    The Boot ROM hands over to a program in flash: The Boot Flow.

  2. Testing every bit

    A macro cannot be reached by scan. So the chip gets logic that tests each macro: Memory Self-Test.

  3. Macros with two ports

    Generators also make macros with two ports, for a queue between two clocks. Each extra port makes every bit larger.

  4. The other three kinds

    Next come flash and one-time bits, then DRAM, then the pins of an SRAM.

Try It

Run the lab. Check the edges of the two sums against the hand table.

Change the ROM word at address 2 and run it again. Then ask what this change would mean on a finished chip.

Add word 3 to the sum on the SRAM. Predict the number of edges first.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: rom address 2 gives b0070022, with no clock edge regs word 4 = 12 after 1 edge sram word 4 = 12 after 3 edges
Read included file: ap_sram.sv
// A model of the on-chip SRAM of ap_soc: one port, words of 32 bits, one strobe for each byte.
// Everything is sampled on the rising clock edge. A read shows its word after that edge.
// This is a model for simulation. A real chip uses a block made by a memory generator, with these pins.
module ap_sram #(parameter int WORDS = 16384) (
  input  logic                     clk,
  input  logic                     ce,     // chip enable: 1 = this edge is an access
  input  logic                     we,     // 1 = write, 0 = read
  input  logic [3:0]               be,     // byte strobes of a write. Bit 0 is the lowest byte
  input  logic [$clog2(WORDS)-1:0] addr,   // a word address, not a byte address
  input  logic [31:0]              wdata,
  output logic [31:0]              rdata
);
  logic [31:0] mem [0:WORDS-1];
  always_ff @(posedge clk) begin
    if (ce && we) begin
      for (int i = 0; i < 4; i++)
        if (be[i]) mem[addr][8*i +: 8] <= wdata[8*i +: 8];   // only the strobed bytes change
    end
    if (ce && !we) rdata <= mem[addr];                       // rdata changes on a read edge only
  end
endmodule

Check Yourself

Predict the outputThe SRAM holds 5, 7 and 9 in words 1, 2 and 3. The bench adds all three and stores the sum in word 4. What does the last row say?
// Three memories of a chip, side by side: a ROM, a register file and the SRAM.
module tb;
  logic clk = 0;
  int edges = 0;
  task automatic tick;  #5 clk = 1;  edges++;  #5 clk = 0;  endtask

  // 1. A ROM: four words that are part of the design. An address goes in and a word comes out.
  //    There is no write enable and no write data: nothing can change these words.
  logic [1:0]  rom_addr = 0;
  logic [31:0] rom_word;
  always_comb case (rom_addr)
    2'd0: rom_word = 32'hb007_0000;
    2'd1: rom_word = 32'hb007_0011;
    2'd2: rom_word = 32'hb007_0022;
    2'd3: rom_word = 32'hb007_0033;
  endcase

  // 2. A register file: eight words of flip-flops, with two read ports and one write port.
  logic [31:0] regs [0:7];
  logic [2:0]  read_a = 0, read_b = 0, write_to = 0;
  logic        write_en = 0;
  logic [31:0] write_word = 0;
  wire  [31:0] word_a = regs[read_a];          // a read needs no clock edge
  wire  [31:0] word_b = regs[read_b];
  always_ff @(posedge clk) if (write_en) regs[write_to] <= write_word;

  // 3. The SRAM of ap_soc, in the second file: one port, so one access at each edge.
  logic        ce = 0, we = 0;
  logic [3:0]  be = 4'b1111;
  logic [13:0] addr = 0;
  logic [31:0] wdata = 0;
  wire  [31:0] rdata;
  ap_sram sram (.*);
  logic [31:0] kept;

  initial begin
    rom_addr = 2;  #1 $display("rom   address 2 gives %08h, with no clock edge", rom_word);

    // Both memories start with 5 in word 1, 7 in word 2 and 9 in word 3.
    regs[1] = 5;  regs[2] = 7;  regs[3] = 9;
    sram.mem[1] = 5;  sram.mem[2] = 7;  sram.mem[3] = 9;

    // The job: add words 1, 2 and 3, and store the sum in word 4. The SRAM only.
    edges = 0;
    ce = 1;  we = 0;  addr = 1;  tick;
    #1 kept = rdata;
    addr = 2;  tick;
    #1 kept = kept + rdata;
    addr = 3;  tick;
    #1 we = 1;  addr = 4;  wdata = kept + rdata;
    tick;  ce = 0;
    #1 $display("sram  word 4 = %0d after %0d edges", sram.mem[4], edges);
  end
endmodule
Read included file: ap_sram.sv
// A model of the on-chip SRAM of ap_soc: one port, words of 32 bits, one strobe for each byte.
// Everything is sampled on the rising clock edge. A read shows its word after that edge.
// This is a model for simulation. A real chip uses a block made by a memory generator, with these pins.
module ap_sram #(parameter int WORDS = 16384) (
  input  logic                     clk,
  input  logic                     ce,     // chip enable: 1 = this edge is an access
  input  logic                     we,     // 1 = write, 0 = read
  input  logic [3:0]               be,     // byte strobes of a write. Bit 0 is the lowest byte
  input  logic [$clog2(WORDS)-1:0] addr,   // a word address, not a byte address
  input  logic [31:0]              wdata,
  output logic [31:0]              rdata
);
  logic [31:0] mem [0:WORDS-1];
  always_ff @(posedge clk) begin
    if (ce && we) begin
      for (int i = 0; i < 4; i++)
        if (be[i]) mem[addr][8*i +: 8] <= wdata[8*i +: 8];   // only the strobed bytes change
    end
    if (ce && !we) rdata <= mem[addr];                       // rdata changes on a read edge only
  end
endmodule
ConceptWhich of these is one of the three questions that pick a kind of memory?
ConceptA count must survive with the power off, and may only ever go up. Which kind of memory fits?
ConceptWhy is a boot ROM kept small?
ConceptWhat can a register file do that a one-port SRAM cannot?
ConceptA block needs 16 KiB of working data, one access in each cycle. What should hold it?

Common Mistakes

Wrong Put a large, clever start-up program into the ROM.

A ROM can never be repaired.

Fix Keep the ROM short, and let it jump to code that can be updated.

Wrong Build a memory of thousands of words from flip-flops.

A flip-flop takes several times the area of an SRAM bit.

Fix Use a memory macro, and plan for one access at each edge.

Wrong Use a one-port macro for the registers of a processor.

The processor reads two registers and writes one in each cycle.

Fix Use a register file with two read ports and one write port.

Remember

  • Choose a memory by three questions: power off, who writes and how often, and how many bits.
  • The contents of a ROM are fixed with the chip, so a ROM holds only the first, short piece of code.
  • A register file is flip-flops with several ports. A macro packs bits tightly behind one port.
  • Flip-flops for a few dozen words or several accesses in a cycle. A macro for everything larger.

Practice

Report a problem with this page