---
title: Caches in hardware
heading: Tag and data arrays, address fields, comparators, write policies and miss handling
eyebrow: S3 · Lesson 43 · Caches in hardware
xp: 50
---

::: hook
C8 treated a cache as a policy: which lines it keeps and how often it hits. Built in RTL, a cache is two SRAM arrays (one of tags, one of data), a row of comparators, a multiplexer to pick the hitting way, and a state machine that handles misses by fetching a line from memory while the processor waits. The hardware view adds costs the policy view hides. Every line carries a tag, a valid bit and usually a dirty bit, so a 32 KB cache is really more than 33 KB of SRAM. Every extra way adds a comparator and a wider multiplexer on the hit path, and every miss costs the memory's latency plus the time to move a whole line.
:::

::: play
- The storage overhead of a cache, the tag, valid and dirty bits as a percentage of the data bits, against the line size.
- The tag width depends only on the address width and the size of one way: tag = address bits − $\log_2$(bytes per way). Bigger lines spread it over more data bits.
- More ways at the same size shrink each way, so the tags grow, and each way needs its own comparator.
:::
:::

```sim plot
view: graph
x: B = 4..256 bytes log
y: o = 0..50 %
slider: S = 4..1024 = 32 KB log
slider: W = 1..16 step 1 = 8
slider: A = 32..48 step 1 = 32
plot: 100*(A - log(S*1024/W)/log(2) + 2)/(8*B) | samples: 300 | label: overhead, %
readout: tag bits = A - log(S*1024/W)/log(2)
readout: total SRAM with 64 B lines, KB = S*(1 + (A - log(S*1024/W)/log(2) + 2)/512)
```

::: predict
## A 32 KB, 8-way cache with 64-byte lines serves 32-bit addresses. How wide is each tag?

- [ ] 6 bits
- [ ] 12 bits
- [x] 20 bits
- [ ] 26 bits

::: reveal
20 bits. The offset within a 64-byte line takes 6 bits. There are 32768 / (64 × 8) = 64 sets, so the index takes 6 more. The remaining 32 − 6 − 6 = 20 bits are the tag, stored beside every one of the 512 lines and compared, 8 at a time, on every access.
:::
:::

::: nameit
## Address fields

For a cache of S bytes, lines of B bytes, W ways and A-bit addresses:

- offset: $\log_2B$ bits;
- sets: $\frac{S}{BW}$, index: $\log_2\frac{S}{BW}$ bits;
- tag: $A - \text{index} - \text{offset}$ bits.

## The arrays and the hit path

- **Data array:** $S$ bytes. **Tag array:** one entry per line, $\frac{S}{B}$ entries of tag + valid (+ dirty for write-back, + replacement state).
- **Hit path:** the index reads all W tags and all W data words of a set at once; W comparators check the tags in parallel; a W-to-1 multiplexer picks the data. Associativity adds comparators and multiplexer delay to every access, which is why first-level caches stay at 4 to 12 ways.
- Large caches access tags first and then only the matching data way, saving power at the cost of a cycle.

## Writes

- **Write-through:** every store also goes to the next level, usually through a write buffer. Simple, but the store traffic is the full store bandwidth.
- **Write-back:** stores update only the cache and set the dirty bit; a dirty line is written back when evicted.
- **Write-allocate** or not: whether a store miss brings the line in.

## Misses

A miss state machine requests the line, waits out the memory latency, then moves the line over a bus of width w bytes:

$$\text{penalty} = t_\text{latency} + \frac{B}{w}\ \text{cycles}$$

**Critical word first** returns the requested word first and lets the processor go on, cutting the wait to about latency + 1. **Non-blocking** caches keep handling hits while misses are outstanding, tracked in miss status holding registers (MSHRs). AMAT = hit time + miss rate × penalty.
:::

::: example
## For the 32 KB, 8-way, 64-byte-line cache with 32-bit addresses and write-back, how much SRAM is there in all, and how many comparator bits?

::: solution
1. **Bits per line.** 512 lines, each with 512 data bits, a 20-bit tag, a valid bit and a dirty bit:
   $$512 + 20 + 1 + 1 = 534\ \text{bits}$$
2. **Total.**
   $$\begin{aligned} N &= 512 \times 534 \\ &= 273{,}408\ \text{bits} \\ &= 33.4\ \text{KB} \end{aligned}$$
   a 4.3% overhead (before replacement bits).
3. **Hit path.** 8 comparators of 20 bits each, 160 bits of comparison on every access.

**Answer:** 273,408 bits (33.4 KB) of SRAM, and 160 comparator bits.
:::
:::

::: example
## A miss costs 50 cycles of latency, and the line comes over a 16-byte bus. With a 1-cycle hit and a 3% miss rate, what's the AMAT, with and without critical word first?

::: solution
1. **Miss penalty.** The latency plus the transfer of a 64-byte line:
   $$50 + \frac{64}{16} = 54\ \text{cycles}$$
2. **AMAT.**
   $$\begin{aligned} \text{AMAT} &= 1 + 0.03 \times 54 \\ &= 2.62\ \text{cycles} \end{aligned}$$
3. **With critical word first.** The processor resumes after about 51 cycles:
   $$\begin{aligned} \text{AMAT} &= 1 + 0.03 \times 51 \\ &= 2.53\ \text{cycles} \end{aligned}$$
4. **What dominates.** The latency does; a bigger cache or prefetching would help more than a wider bus.

**Answer:** 2.62 cycles, or 2.53 with critical word first.
:::
:::

::: example
## A 64 KB, 4-way cache with 32-byte lines serves 40-bit physical addresses and is write-back. Find its address fields and the size of its tag array.

::: solution
1. **Offset.** 32-byte lines need 5 bits.
2. **Index.** There are $65536/32 = 2048$ lines, in 512 sets: 9 index bits.
3. **Tag.**
   $$40 - 9 - 5 = 26\ \text{bits}$$
4. **Tag array.** Each line stores tag, valid and dirty, 28 bits:
   $$2048 \times 28 = 57\,344\ \text{bits}$$
   which is 7 KB, 11% on top of the data.
5. **Longer lines.** They would shrink that overhead and raise the miss penalty.

**Answer:** 5 offset bits, 9 index bits and a 26-bit tag; the tag array holds 57 344 bits (7 KB).
:::
:::

::: check
given: S = 16 | 64 | 256; B = 32 | 64; W = 2 | 4 | 16; A = 32 | 40
answer: A - log(S*1024/W)/log(2)
tolerance: 0.4

## A {S} KB, {W}-way cache has {B}-byte lines and {A}-bit addresses. How many tag bits?

::: right
Right. A − index − offset = {answer}.
:::

::: misses
- [A - log(S*1024/B)/log(2)] That leaves out the offset bits and counts lines instead of sets.
- [A - log(S*1024)/log(2)] With W ways, each way is S/W bytes: the index is smaller.
- [any] Not yet. Offset bits, then index bits, then the rest.
:::
:::

::: check
given: S = 8 | 32 | 128; B = 32 | 64; W = 2 | 4 | 8
answer: log(S*1024/(B*W))/log(2)
tolerance: 0.4

## How many index bits does a {S} KB, {W}-way cache with {B}-byte lines need?

::: right
Right. $\log_2\frac{S}{BW}$ = {answer}.
:::

::: misses
- [log(S*1024/B)/log(2)] Divide by the ways: the index chooses a set.
- [any] Not yet. Count the sets.
:::
:::

::: check
given: S = 16 | 64; B = 32 | 64; W = 2 | 8; A = 32 | 48
answer: S*1024/B*(8*B + A - log(S*1024/W)/log(2) + 2)
tolerance: 0.5%

## A write-back cache of {S} KB, {W}-way, with {B}-byte lines and {A}-bit addresses keeps a valid and a dirty bit per line. How many SRAM bits does it hold in all?

::: right
Right. Lines × (data + tag + 2) = {answer} bits.
:::

::: misses
- [S*1024*8] Include each line's tag, valid and dirty bits.
- [any] Not yet. Count the bits in one line, times the number of lines.
:::
:::

::: check
given: L = 40 | 80 | 200; B = 32 | 64 | 128; w = 8 | 16
answer: L + B/w
tolerance: 0.5%

## A miss waits {L} cycles of latency and then moves a {B}-byte line over a {w}-byte bus, one transfer per cycle. What's the miss penalty?

::: right
Right. $t_\text{lat} + B/w$ = {answer} cycles.
:::

::: misses
- [L] Moving the line takes cycles too.
- [any] Not yet. Latency plus transfer time.
:::
:::

::: check
given: h = 1 | 2 | 4; m = 0.01 | 0.05; L = 50 | 150; B = 64; w = 16
answer: h + m*(L + B/w)
tolerance: 0.5%

## A cache hits in {h} cycles and misses {m} of the time. A miss waits {L} cycles and then moves a {B}-byte line over a {w}-byte bus. What's the AMAT?

::: right
Right. $h + m(t_\text{lat} + B/w)$ = {answer} cycles.
:::

::: misses
- [h + m*L] Include the line transfer in the penalty.
- [any] Not yet. Hit time plus miss rate times penalty.
:::
:::

::: check
given: f = 1 | 3; s = 0.08 | 0.12; w = 4 | 8
answer: f*s*w
unit: GB/s
tolerance: 0.5%

## A core at {f} GHz executes one instruction per cycle, {s} of them stores of {w} bytes. How much write traffic does a write-through cache send to the next level?

::: right
Right. {answer} GB/s, every store passed through.
:::

::: misses
- [any] Not yet. Stores per second times bytes per store.
:::
:::

::: check
kind: parts
given: S = 16 | 32 | 64; B = 32 | 64; W = 2 | 4 | 8; A = 32 | 40

## A {S} KB, {W}-way cache with {B}-byte lines serves {A}-bit addresses.

::: part
answer: round(log(B)/log(2))
unit: bits
tolerance: 0

### How many offset bits?

::: misses
- [any] $\log_2B$.
:::
:::

::: part
answer: round(log(S*1024/(B*W))/log(2))
unit: bits
tolerance: 0

### How many index bits?

::: misses
- [round(log(S*1024/B)/log(2))] Divide the lines among W ways first.
- [any] $\log_2(S/BW)$.
:::
:::

::: part
answer: A - round(log(S*1024/(B*W))/log(2)) - round(log(B)/log(2))
unit: bits
tolerance: 0

### How many tag bits?

::: misses
- [any] What's left of the address.
:::
:::
:::

::: check
kind: derivation
vars: h = 1..4, m = 0.001..0.2, L = 10..300, B = 16..256, w = 4..64

## A cache hits in h cycles and misses with probability m; a miss waits L cycles of latency and moves a B-byte line over a w-byte bus.

::: step
answer: L + B/w

### What is the miss penalty?
:::

::: step
answer: h + m*(L + B/w)

### What is the average memory access time?
:::

::: step
answer: h + m*(L + 1)

### And with critical word first?
:::

::: right
Right. Critical word first hides the transfer time, which matters most when lines are long and buses narrow.
:::
:::

::: explain
## Why does making a first-level cache more associative eventually slow down every access, even the hits?

::: model
On every access, the index selects a set, and the cache has to check every way in that set before it knows whether it hit and which data to deliver. That means one tag comparator per way working in parallel, and a multiplexer that picks one of W data words using the comparator results. More ways mean more comparators loading the tag lines, more data read and thrown away, and a wider multiplexer whose select signals arrive late. All of that sits on the hit path, so the hit time grows with associativity even though the miss rate falls, and first-level caches, where hit time matters most, stop at a modest number of ways.
:::
:::

::: practice

::: check
given: S = 32 | 512; B = 64 | 128; W = 4 | 8; A = 36 | 48
answer: A - log(S*1024/W)/log(2)
tolerance: 0.4

## Tag bits: {S} KB, {W}-way, {B}-byte lines, {A}-bit addresses?

::: misses
- [any] A − log₂(bytes per way).
:::
:::

::: check
given: L = 60 | 120; B = 64 | 128; w = 8 | 32
answer: L + B/w
tolerance: 0.5%

## Miss penalty: {L} cycles latency, {B}-byte line, {w}-byte bus?

::: misses
- [any] $t_\text{lat} + B/w$.
:::
:::

::: check
given: h = 1 | 3; m = 0.02 | 0.08; L = 40 | 100; B = 64; w = 8
answer: h + m*(L + B/w)
tolerance: 0.5%

## AMAT: hit {h}, miss rate {m}, latency {L}, {B}-byte line, {w}-byte bus?

::: misses
- [any] $h + m(t_\text{lat} + B/w)$.
:::
:::
:::
