---
title: Main memory and buses
heading: How long DRAM really takes, and how data crosses the bus
eyebrow: C8 · Lesson 48 · Main memory and buses
xp: 40
---

::: hook
A memory module's label says DDR4-3200, and its datasheet promises 25.6 GB/s. Yet a single cache miss still takes tens of nanoseconds, about as long as it did twenty years ago. Both numbers are true. DRAM is organized in rows that must be opened before they can be read, so the time to the first byte depends on what the memory was doing before, while the rate at which bytes stream out once a row is open has grown enormously. Processors are built around that difference.
:::

::: play
- The curve is the time to get a whole 64-byte line from DRAM, against the fraction of accesses that find their row already open in the row buffer.
- A row hit costs just the CAS latency CL. A row conflict must close the old row (tRP), open the new one (tRCD), then read (CL). Then the 64 bytes stream out in a burst of 8 transfers.
- Raise the transfer rate: the burst shrinks, but the latency hardly changes. Row hits are what make memory fast.
:::

```sim plot
view: graph
x: h = 0..1
y: t = 0..60 ns
slider: CL = 10..20 step 0.25 = 13.75
slider: RCD = 10..20 step 0.25 = 13.75
slider: RP = 10..20 step 0.25 = 13.75
slider: R = 1600..6400 step 400 = 3200
plot: h*CL + (1 - h)*(RP + RCD + CL) + 8000/R | label: time for a 64-byte line
readout: peak bandwidth, GB/s = R*8/1000 | digits: 3
readout: burst of 8 transfers, ns = 8000/R | digits: 3
readout: row conflict to first data, ns = RP + RCD + CL | digits: 4
```

::: predict
## DDR4-3200 makes 3200 million transfers a second on a 64-bit (8-byte) channel. How long does a 64-byte cache line take to stream out, once the data starts arriving?

- [ ] 20 ns
- [ ] 10 ns
- [x] 2.5 ns
- [ ] 0.3 ns

::: reveal
2.5 ns: 64 bytes is 8 transfers of 8 bytes, each 0.3125 ns. The wait before the first transfer is the long part: about 14 ns for a row hit and 41 ns for a row conflict at this module's timings.
:::
:::

::: nameit
## How DRAM is organized

A DRAM chip holds its bits in **banks**, each a grid of rows and columns. To read, the controller first **activates** a row: the whole row, several kilobytes, is copied into the bank's **row buffer** by sense amplifiers. Then it reads columns from the buffer. Before a different row can be opened, the current one must be written back and the bank **precharged**.

| Timing | Meaning |
|---|---|
| CL (CAS latency) | column read to first data, with the row already open |
| tRCD | activate a row before a column can be read |
| tRP | precharge, closing the open row |

So an access costs CL on a **row hit**, tRCD + CL to an idle bank, and tRP + tRCD + CL on a **row conflict**. Timings are quoted in clock cycles: DDR4-3200 CL22 runs a 1600 MHz clock, so 22 cycles is 13.75 ns.

## Bandwidth

DDR ("double data rate") transfers on both clock edges. Peak bandwidth is

$$\text{bandwidth} = \text{transfers per second} \times \text{bytes per transfer}$$

A cache line arrives as a **burst**: 8 transfers of 8 bytes fill a 64-byte line. Several banks, and several channels, can work on different requests at once, which is how sustained bandwidth approaches the peak.

## Latency, bandwidth and parallelism

Little's law ties them together: to keep a memory system with latency T busy at bandwidth B, the processor must have

$$N = \frac{B\,T}{\text{line size}}$$

misses in flight at once. That's why processors track many outstanding misses, prefetch, and run ahead of stalled loads.

## Buses and address decoding

On a microcontroller, the processor, DMA controllers, flash, RAM and peripherals share one or more buses. An **address decoder** looks at the top address bits to choose which device answers: on many ARM chips, 0x0… is flash, 0x2… is SRAM and 0x4… the peripherals. When two masters want the bus at once, an **arbiter** chooses, so a busy DMA controller can slow the processor.

## Wait states

Slow devices make the bus wait. On-chip flash might need 40 ns per read, while a 72 MHz core's cycle is 13.9 ns, so each flash read takes 3 cycles: 2 **wait states**. Flash interfaces fetch wide words, 128 bits or more, into a prefetch buffer, so straight-line code still runs near one instruction a cycle; branches pay the wait states again.
:::

::: example
## DDR4-3200 CL22 has tRCD = tRP = 22 cycles too, at a 1600 MHz clock. What does a row hit, an access to an idle bank, and a row conflict each cost? What's the average if 60% of accesses hit and the rest conflict?

::: solution
1. **Each timing.**
   $$22 \times 0.625\ \text{ns} = 13.75\ \text{ns}$$
2. **The three cases.** A hit costs 13.75 ns, an idle bank 27.5 ns, and a conflict 41.25 ns.
3. **The average.**
   $$\begin{aligned} t &= 0.6 \times 13.75 \\ &\quad + 0.4 \times 41.25 \\ &= 24.75\ \text{ns} \end{aligned}$$
4. **In practice.** That's before the 2.5 ns burst and the time spent in the processor's caches and memory controller, which in practice bring a miss to 60 to 100 ns.

**Answer:** 13.75 ns for a row hit, 27.5 ns for an idle bank, 41.25 ns for a row conflict, and 24.75 ns on average.
:::
:::

::: example
## A core has 80 ns of memory latency and a 25.6 GB/s channel. How many 64-byte misses must be in flight to use the full bandwidth?

::: solution
1. **Misses in flight.**
   $$\begin{aligned} N &= \frac{25.6 \times 10^9 \times 80 \times 10^{-9}}{64} \\ &= 32 \end{aligned}$$
2. **One at a time.** One miss at a time would reach only 64 bytes per 80 ns, 0.8 GB/s, 3% of the channel.
3. **Real cores.** Out-of-order cores track 10 to 20 misses each, and hardware prefetchers add more, which is why streaming code gets close to peak and pointer-chasing code doesn't.

**Answer:** 32 misses must be in flight.
:::
:::

::: example
## A 72 MHz microcontroller reads flash with a 40 ns access time. How many wait states does it need, and what does a prefetch buffer of four instructions do for straight-line code?

::: solution
1. **The cycle time.** A cycle is 13.9 ns.
2. **Cycles per flash read.**
   $$\left\lceil \frac{40}{13.9} \right\rceil = 3$$
   so the read needs 2 wait states.
3. **Without help.** Every instruction fetch would take 3 cycles.
4. **With the prefetch buffer.** A 128-bit fetch brings in four 32-bit instructions per 3-cycle read, which covers four 1-cycle instructions, so sequential code runs at full speed and only taken branches stall while the buffer refills.

**Answer:** 2 wait states; the prefetch buffer lets straight-line code run at full speed, and only taken branches stall.
:::
:::

::: check
kind: parts
given: R = 1600 | 3200 | 6400; w = 32 | 64; L = 32 | 64

## A DDR channel makes {R} million transfers a second, {w} bits wide.

::: part
answer: R*w/8/1000
unit: GB/s
tolerance: 0.5%

### What's its peak bandwidth?

::: misses
- [R*w/1000] That's gigabits; divide by 8 for bytes.
- [any] Transfers per second × bytes per transfer.
:::
:::

::: part
answer: L/(w/8)
tolerance: 0

### How many transfers does a {L}-byte line take?

::: misses
- [any] Line size ÷ bytes per transfer.
:::
:::
:::

::: check
kind: parts
given: t = 12 | 13.75 | 16; h = 40 | 60 | 80

## A DRAM has CL = tRCD = tRP = {t} ns. {h}% of accesses are row hits; the rest are row conflicts.

::: part
answer: 3*t
unit: ns
tolerance: 0.5%

### What does a row conflict cost to the first data?

::: misses
- [2*t] A conflict must also close the old row.
- [any] Precharge, activate, then read.
:::
:::

::: part
answer: h/100*t + (1 - h/100)*3*t
unit: ns
tolerance: 0.5%

### What's the average?

::: misses
- [any] Weight each case by how often it happens.
:::
:::
:::

::: check
kind: parts
given: a = 25 | 40 | 50; f = 48 | 72 | 120

## Flash takes {a} ns per read, and the core runs at {f} MHz.

::: part
answer: ceil(a*f/1000)
tolerance: 0

### How many cycles does a read take?

::: misses
- [any] Access time ÷ cycle time, rounded up.
:::
:::

::: part
answer: ceil(a*f/1000) - 1
tolerance: 0

### How many wait states is that?

::: misses
- [any] The cycles beyond the first.
:::
:::
:::

::: check
kind: parts
given: A = 1073811456 | 536875008 | 134217744 | 1073741828

## On a chip whose address decoder uses bits 31:28 to pick the region, an access goes to {A:hex8}.

::: part
answer: floor(A/2^28)
tolerance: 0

### Which region number (0 to 15) does it select?

::: misses
- [any] The top hex digit.
:::
:::

::: part
answer: A - 2^28*floor(A/2^28)
tolerance: 0
base: 16
width: 7

### What's its offset within the region?

::: misses
- [any] The other 28 bits.
:::
:::
:::

::: check
kind: derivation
vars: B = 1000000000..100000000000, T = 0.00000001..0.0000003, L = 32..128

## A memory system has latency T and bandwidth B, and moves L-byte lines.

::: step
answer: L/T

### What bandwidth does one miss at a time reach?
:::

::: step
answer: B*T/L

### How many misses must be in flight to reach B?
:::

::: right
Right. Little's law: the number in flight is rate × time in the system.
:::
:::

::: check
kind: parts
given: B = 12.8 | 25.6 | 51.2; T = 60 | 80 | 100; L = 64

## A channel delivers {B} GB/s with {T} ns of latency and 64-byte lines.

::: part
answer: B*T/64
tolerance: 0.5%

### How many misses must be in flight to use it fully?

::: misses
- [any] $BT/L$.
:::
:::

::: part
answer: 64/T/B*100
unit: %
tolerance: 0.5%

### What share of the bandwidth does one miss at a time use?

::: misses
- [any] One line per latency, against B.
:::
:::
:::

::: check
kind: choice

## Why has DRAM latency in nanoseconds barely improved for decades, while bandwidth has grown many times?

- [ ] Manufacturers haven't tried to reduce latency
- [x] Latency is set by charging long row and bit lines and sensing tiny capacitors, while bandwidth can grow with wider, faster interfaces and more banks working at once
- [ ] Faster DRAM would need more refresh
- [ ] Bandwidth is measured differently now

::: misses
- [1] Latency has been worked on hard; the physics of the array resists it.
- [3] Refresh is a small overhead either way.
- [4] It's the same quantity, bytes per second.
:::
:::

::: explain
## Why does a processor need many cache misses outstanding at once to use a memory channel's bandwidth?

::: model
Each miss spends tens of nanoseconds waiting for the DRAM to open a row and return data, but once data flows a 64-byte line streams out in a few nanoseconds. If the processor waits for one miss before starting the next, the channel sits idle almost all the time: one line per latency is a few percent of the peak. Bandwidth is only used if new requests keep arriving while earlier ones are still in progress, so that the channel and the banks always have work. By Little's law the number of requests in flight must equal bandwidth × latency ÷ line size, which for a modern channel is dozens. Out-of-order execution, non-blocking caches and prefetchers exist to keep that many misses going.
:::
:::

::: practice
::: check
given: R = 2400 | 3200 | 4800; w = 64
answer: R*w/8/1000
unit: GB/s
tolerance: 0.5%

## Peak bandwidth of {R} MT/s on a {w}-bit channel?

::: misses
- [any] Transfers per second × 8 bytes.
:::
:::

::: check
given: a = 30 | 45; f = 64 | 100
answer: ceil(a*f/1000) - 1
tolerance: 0

## Flash of {a} ns at {f} MHz: how many wait states?

::: misses
- [any] Cycles per read, less one.
:::
:::

::: check
given: B = 20 | 40; T = 70 | 90
answer: B*T/64
tolerance: 0.5%

## {B} GB/s, {T} ns, 64-byte lines: misses in flight needed?

::: misses
- [any] $BT/64$.
:::
:::
:::
