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.
| Question | |
|---|---|
| 1 | Which three questions pick a kind of memory for a job? |
| 2 | Which six kinds does a chip use, and what is each one for? |
| 3 | What goes into a ROM, and why is a ROM kept small? |
| 4 | How does a register file differ from an SRAM? |
| 5 | When 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.
| Kind | Survives power off? | Who writes, and how often | Typical size | What it is for | In ap_soc |
|---|---|---|---|---|---|
| ROM | yes | nobody: fixed when the chip is made | kibibytes | the first instructions after a reset | the Boot ROM, 4 KiB at 0x00000000 |
| One-time programmable bits | yes | once: a bit that is set stays set | a few thousand bits | keys, identity numbers, settings of one chip | none |
| Flash | yes | a program, slowly, one erased block at a time | mebibytes | the program of the product, and its settings | none on the chip: a flash chip on the board |
| Register file | no | in every cycle, from several sides at once | a few dozen words | the working registers of a processor | the registers inside ap_rv32 |
| SRAM | no | any program, at full speed | kibibytes to a few mebibytes | working data, and caches | the SRAM, 64 KiB at 0x80000000 |
| DRAM | no | any program, and it must be refreshed | gigabytes, in chips of their own | the main memory of a phone or a computer | none |
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
| Word | Meaning |
|---|---|
| Volatile memory | Memory that loses its content when the power goes off. Memory that keeps it is non-volatile. |
| SRAM | Static RAM. Each bit is a small loop of transistors that holds its value for as long as the power is on. |
| DRAM | Dynamic RAM. Each bit is a tiny electric charge, which leaks away. A later page of this module explains it. |
| OTP | One-time programmable memory: bits that can each be set once and never cleared. |
| Contents of a ROM | The words that a ROM holds. They are given as a file when the chip is designed, and are built into it. |
| Boot code | The first program that runs after a reset. It prepares the chip and then starts the real program. |
| Register file | A 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 macro | A 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 memory | One set of address, data and control pins. One port does one access at a time. |
| Read port and write port | A port that can only read, and a port that can only write. |
One Picture: The Three Questions
- Processor
- Memory
- 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.
- 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.
- 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.
- Question 3: how many bits?
Among the kinds that are written at full speed, size decides. A few dozen words, some kibibytes, or gigabytes.
- 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.
| Job | Survive power off? | Who writes, how often | How many bits | Kind |
|---|---|---|---|---|
| The first instructions after a reset | yes | nobody, ever | 4 KiB | ROM |
| The identity number of this one chip | yes | the factory, once | 64 bits | one-time bits |
| The stack of a running program | no | the program, all the time | 64 KiB | SRAM |
| Two numbers to add and a place for the sum | no | the processor, in every cycle | a few words | register 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.
| Memory | Ports | Edge 1 | Edge 2 | Edge 3 | Edges |
|---|---|---|---|---|---|
| Register file | two read ports, one write port | read word 1, read word 2, write word 4 | 1 | ||
| SRAM | one port | read word 1 | read word 2 | write word 4 | 3 |
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
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.
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.
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.
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.
Rule 5: A memory macro packs bits tightly behind one port.
It does one access at each clock edge.
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.
rom address 2 gives b0070022, with no clock edge
regs word 4 = 12 after 1 edge
sram word 4 = 12 after 3 edgesRule 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Boot code in a ROM | Boot code in flash | It is there at the first instruction, and no program can damage it. It can never be updated. |
| A small ROM that jumps to flash | The whole program in ROM | A bug in the program is fixed by an update. The chip needs a second memory. |
| A register file of flip-flops | A small memory macro | Three accesses in one cycle. Each bit takes several times the area. |
| A memory macro for working data | The same words as flip-flops | Several 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
This section looks ahead. A large chip has hundreds of memory macros, and all six kinds around it.
The boot chain
The Boot ROM hands over to a program in flash: The Boot Flow.
Testing every bit
A macro cannot be reached by scan. So the chip gets logic that tests each macro: Memory Self-Test.
Macros with two ports
Generators also make macros with two ports, for a queue between two clocks. Each extra port makes every bit larger.
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.
rom address 2 gives b0070022, with no clock edge regs word 4 = 12 after 1 edge sram word 4 = 12 after 3 edgesRead 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
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.