---
title: SRAM, DRAM and flash
heading: Three ways to store a billion bits
eyebrow: C7 · Lesson 44 · SRAM, DRAM and flash
xp: 40
---

::: hook
A flip-flop takes about twenty transistors to hold one bit, far too many for gigabytes. Memories use much smaller cells, each trading something away. SRAM keeps a tiny latch (six transistors) in each cell: fast, but bulky. DRAM stores each bit as charge on a capacitor behind one transistor: dense and cheap, but the charge leaks away, so every row has to be read and rewritten dozens of times a second. Flash traps charge on an isolated gate (B9.7): it keeps its data with the power off, but writing is slow and wears the cell out.

::: play
- A DRAM cell's capacitor, charged to a 1, leaks through its transistor. The sense amplifier reads a 1 only while the voltage stays above the threshold.
- How long does the charge last? Change the leakage resistance (it varies with temperature).
- Set a refresh period. Each refresh tops the cell back up. What's the longest refresh period that's still safe?
:::
:::

```sim plot
view: graph
x: t = 0..200 ms
y: v = 0..1.1 V
slider: tau = 20..400 = 100 ms log
slider: Tr = 8..128 = 64 ms log
slider: Vth = 0.3..0.8 step 0.05 = 0.5 V
plot: exp(-(t - Tr*floor(t/Tr))/tau) | label: cell voltage, refreshed
plot: exp(-t/tau) | dashed | color: ch2 | label: without refresh
plot: Vth | dashed | color: ch3 | label: read threshold
readout: data lost after (no refresh) in ms = tau*ln(1/Vth)
readout: lowest voltage before each refresh = exp(-Tr/tau) V
```

::: predict
## A DRAM cell starts at 1 V and loses charge with a time constant of 100 ms. Its contents read correctly while it's above 0.5 V. How often must it be refreshed?

- [ ] Every second
- [ ] Every 100 ms
- [x] At least every 69 ms
- [ ] Every 5 ms

::: reveal
At least every 69 ms: the voltage reaches 0.5 V at $\tau\ln 2 = 69$ ms. Real DRAM is specified at 64 ms for every row, with margin for hot chips, whose leakage is much faster. During refresh the memory can't answer other requests, which costs a few percent of its bandwidth.
:::
:::

::: nameit
## Memory technologies

| | SRAM | DRAM | Flash |
|---|---|---|---|
| cell | 6 transistors (a latch) | 1 transistor + 1 capacitor | 1 floating-gate transistor |
| keeps data without power | no | no | yes, for years |
| refresh needed | no | yes, every 64 ms | no |
| read time | ~1 ns | ~10s of ns | ~10s of µs |
| write | fast | fast | slow; erase in blocks; wears out |
| used for | caches, registers | main memory | storage (SSDs, phones) |

**Organization.** A memory is a grid of cells: an address selects a row (word line) and the cells' contents appear on the columns (bit lines), where sense amplifiers detect the small signals. With n address bits and words of w bits, the capacity is $2^n \times w$ bits.

**DRAM's tiny signal.** A cell's capacitor (a few femtofarads) shares its charge with a long bit line many times larger, so the bit line moves only a fraction of a volt; the sense amplifier detects that difference and drives the line fully, which also rewrites the cell.

**Flash's bits per cell.** Multi-level cells store 2, 3 or 4 bits by distinguishing 4, 8 or 16 charge levels on the floating gate, trading endurance and speed for density.
:::


::: example
## A 1 GiB memory has 64-bit words. How many address lines does it need?

::: solution
1. **Count the words.** 1 GiB is $2^{30}$ bytes, and each word is 8 bytes:
   $$\frac{2^{30}}{8} = 2^{27}$$
2. **Count the address lines.** $2^{27}$ words need 27 address lines.

**Answer:** 27 address lines.
:::
:::

::: example
## A DRAM must refresh all 8192 of its rows every 64 ms. How often does it refresh a row?

::: solution
1. **Divide the period by the rows.**
   $$\frac{64\ \text{ms}}{8192} = 7.8\ \mu\text{s}$$
2. **Schedule the refreshes.** One row refresh every 7.8 µs, interleaved with normal accesses.

**Answer:** one row every 7.8 µs.
:::
:::

::: example
## A memory channel is 64 bits wide and transfers 3200 million times a second. What's its peak bandwidth? And how big a ROM would hold an 8-bit by 8-bit multiplication table?

::: solution
1. **Peak bandwidth.** Each transfer moves 8 bytes:
   $$8 \times 3.2 \times 10^9 = 25.6\ \text{GB/s}$$
2. **ROM size.** The multiplier has 16 input bits and a 16-bit product, so $2^{16}$ words of 16 bits:
   $$2^{16} \times 16 = 1\ \text{Mbit}$$
   or 128 KiB.
3. **Why ROMs stay small.** A ROM can compute anything, but its size doubles with every input bit, which is why it's used as logic only for small functions, as in an FPGA's LUTs.

**Answer:** 25.6 GB/s; the multiplication table takes 1 Mbit, 128 KiB.
:::
:::

::: check
given: tau = 50 | 100 | 200; V = 0.4 | 0.5 | 0.7
answer: tau*ln(1/V)
unit: ms
tolerance: 0.5%

## A DRAM cell decays as $e^{-t/\tau}$ with τ = {tau} ms from 1 V. When does it fall to {V} V?

::: right
Right. $\tau\ln(1/V)$ = {answer}.
:::

::: misses
- [tau*V] It's exponential: $\tau\ln(1/V)$.
- [tau*ln(V)] That's negative: invert inside the log.
- [any] Not yet. Solve $e^{-t/\tau} = V$.
:::
:::

::: check
given: n = 10 | 16 | 20 | 30; w = 8 | 32 | 64
answer: 2^n*w/8
unit: B
tolerance: 0.5%

## A memory has {n} address bits and {w}-bit words. How many bytes does it hold?

::: right
Right. $2^n$ words of {w} bits: {answer} bytes.
:::

::: misses
- [2^n*w] That's in bits; divide by 8.
- [n*w/8] Each address bit doubles the number of words: $2^n$.
- [any] Not yet. $2^n \times w$ bits, ÷ 8.
:::
:::

::: check
given: b = 1 | 2 | 3 | 4
answer: 2^b
tolerance: 0

## A flash cell stores {b} bits. How many charge levels must it tell apart?

::: right
Right: $2^b$ = {answer}.
:::

::: misses
- [b] Each extra bit doubles the levels.
- [any] Not yet. $2^b$.
:::
:::

::: check
given: rows = 1024 | 2048 | 4096 | 8192 | 16384 | 32768; T = 64
answer: T/rows*1e6
unit: ns
tolerance: 0.5%

## A DRAM chip must refresh all {rows} rows every 64 ms, one at a time, evenly spread. How often does it refresh a row?

::: right
Right. $64\text{ ms}/\given{rows}$ = {answer}.
:::

::: misses
- [T*rows] Divide the period among the rows.
- [any] Not yet. 64 ms ÷ the number of rows.
:::
:::

::: check
kind: match

## Match each memory to its cell.

- SRAM :: a six-transistor latch
- DRAM :: one transistor and a capacitor
- flash :: a transistor with a floating gate
- a register in a processor :: a row of flip-flops

::: right
Right. Denser cells trade away speed, or endurance, or the need for refresh.
:::
:::

::: check
kind: parts
given: G = 1 | 4 | 16; w = 16 | 32 | 64

## A {G} GiB memory has {w}-bit words.

::: part
answer: 30 + log(G)/log(2) - log(w/8)/log(2)
tolerance: 0

### How many address lines does it need?

::: misses
- [30 + log(G)/log(2)] That addresses bytes; divide by the word size first.
- [any] $\log_2\frac{\text{bytes}}{\text{bytes per word}}$.
:::
:::

::: part
answer: log(w/8)/log(2)
tolerance: 0

### How many more address lines would it need if it were byte-addressed?

::: misses
- [any] $\log_2$ of the bytes per word.
:::
:::
:::

::: check
kind: derivation
vars: w = 8..512, r = 1000000..10000000000, n = 1..16, t = 0.001..1, R = 512..1000000

## A memory channel is w bits wide at r transfers per second, with n channels; separately, a DRAM must refresh R rows every t seconds.

::: step
answer: w/8*r*n

### What's the peak bandwidth in bytes per second?
:::

::: step
answer: t/R

### How often must it refresh a row?
:::

::: right
Right. Refresh steals a small, steady share of the DRAM's time.
:::
:::

::: explain
## Why does DRAM need refreshing while SRAM doesn't?

::: model
A DRAM cell stores its bit as charge on a tiny capacitor, and that charge slowly leaks away through the access transistor and the silicon, so after tens of milliseconds a stored 1 would fade below the level the sense amplifier can recognize. Refresh reads each row and writes it back at full strength before that happens. An SRAM cell is a latch: two cross-coupled inverters that actively hold each other in their state as long as power is on, so there is nothing to leak away.
:::
:::

::: practice

::: check
given: n = 10 | 20 | 30
answer: 2^n
tolerance: 0

## How many words can {n} address bits select?

::: misses
- [any] $2^n$.
:::
:::

::: check
given: tau = 50 | 80 | 100 | 150 | 200 | 400
answer: tau*ln(2)
unit: ms
tolerance: 0.5%

## τ = {tau} ms. Time to fall to half?

::: misses
- [tau/2] It's $\tau\ln 2$.
- [any] $\tau\ln 2$.
:::
:::

::: check
kind: choice

## Which memory keeps its data with the power off?

- [ ] SRAM
- [ ] DRAM
- [x] Flash
- [ ] All of them

::: right
Right. Charge trapped on the floating gate stays for years.
:::

::: misses
- [1] SRAM loses its data when power goes.
- [2] DRAM fades within milliseconds.
- [4] Only flash (of these) is non-volatile.
:::
:::
:::
