School/✦ Research Engineer/Week 21
Inside a cache: tags, sets and ways
A cache holds a tiny fraction of memory. When the CPU asks for address 0x1F40, how does the cache know in one nanosecond whether it has it?
About 50 min · 3 goals · Checked by automated tests
1.Why should I care?
Cache organisation is a precise piece of digital design: a few bits of the address choose where to look, the rest are compared to check you found the right thing. Getting the bit split right is the first step in designing, simulating or debugging any memory system.
- ◇ Split an address into tag, index and offset bits for a given cache size, block size and associativity.
- ◇ Trace a direct-mapped and a 2-way set-associative cache with LRU through an address stream.
- ◇ Explain how tag comparison is built from logic gates and why associativity costs hardware.
2.Predict
Two arrays exactly one cache-size apart are read alternately (a[0], b[0], a[1], b[1], …) in a direct-mapped cache. What happens?
Guess first. Being wrong here is how the surprise works.
3.Discover
In the logic lab, build a 2-bit equality comparator: XNOR each pair of tag bits, then AND the results. This is the 'hit' signal of one cache way. Count the gates, then work out how many a 4-way cache with 18-bit tags needs.
Explorer 05 — Digital logic
Build answers out of ones and zeros
Every digital chip is built from a handful of simple gates. Flip the inputs to watch signals travel through a circuit, then wire your own gates to meet a target truth table.
- 1 (high)
- 0 (low)
- Unknown
- Inverting output
A single two-input gate. Change its type and watch the truth table change.
Inputs
Gates (1 of 8)
G1 = 0
Add a gate
Outputs
- Gates
- 1
- Depth
- 1
- Longest chain of gates a signal passes through
- Rows
- 4
- 2^2 input combinations
- Check
- —
| Set inputs | A | B | Y |
|---|---|---|---|
| 0 | 0 | 0 | |
| 0 | 1 | 0 | |
| 1 | 0 | 0 | |
| 1 | 1 | 1 |
State
Inputs A = 0, B = 0. Outputs Y = 0.
Inputs A = 0, B = 0. Outputs Y = 0.
How to read this
Signals flow left to right. Circles on the left are the inputs, boxes are gates and squares on the right are outputs. A thick bright wire carries a 1, a thin grey wire carries a 0, and each gate shows the value it outputs. A small circle on a gate's output means it inverts (NOT, NAND, NOR, XNOR).
The truth table lists every possible input combination and what the circuit outputs for each, so it describes the circuit completely. Two circuits with the same truth table are equivalent, however differently they are built. That is why NAND alone can make any other gate. Choose a row to set the inputs to it.
Assumptions & limitations
- Ideal logic: every wire is exactly 0 or 1. Real gates use voltage ranges (for example near 0 V and near 3.3 V or 5 V) and an input between them is undefined.
- Gates switch instantly here. Real gates take nanoseconds each, so a deeper circuit is slower, and outputs can glitch briefly while signals race through different paths.
- Only combinational circuits are allowed: no output may feed back into its own input. Feedback is how memory (latches and flip-flops) works, but it needs timing this model does not simulate, so loops are reported as an error.
- Gates here have one input (NOT) or two inputs. Real chips also have three- and four-input gates, and limits on how many inputs one output can drive (fan-out).
- Gate counts are a simple measure of size; real designs also weigh speed, power and the number of transistors per gate.
Can't use the interactive? Read a description instead
The logic lab task can be done as a truth table: a 2-bit comparator outputs 1 only when A1 = B1 and A0 = B0, built as AND(XNOR(A1, B1), XNOR(A0, B0)). Each cache way needs one such comparator, as wide as the tag.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
A 32 KB 4-way set-associative cache has 16-byte blocks and 64-bit byte addresses. How many offset bits are there?
5.Understand
Offset, index, tag
With B-byte blocks, the lowest log₂ B bits are the offset (which byte in the block). With S sets, the next log₂ S bits are the index (which set to look in). The remaining high bits are the tag, stored with the block and compared on every access. Sets S = cache size ÷ (block size × ways).
Direct-mapped versus set-associative
Direct-mapped (1 way): each block has exactly one possible line. Fast and simple: one tag comparison. N-way set-associative: each set holds N blocks, so N comparisons happen in parallel and a multiplexer picks the hit way. Conflict misses fall; area, energy and sometimes hit time rise. Fully associative is one set holding everything.
Replacement and writes
When a set is full, LRU (least recently used) evicts the block untouched for longest; real caches often approximate it. Stores need a policy too: write-through sends every write to the next level; write-back marks the block dirty and writes it only on eviction, saving bandwidth.
The hardware cost
Each way needs a tag-width equality comparator: one XNOR per bit plus an AND tree, and a valid bit. A 4-way cache with 18-bit tags needs 4 × 18 = 72 XNORs before the AND trees and way multiplexer. That is the area and energy behind 'just add associativity'.
Explain it another way
A cloakroom with numbered hooks. Direct-mapped: your ticket number decides the one hook your coat must go on, even if it's taken. 2-way: each number has two hooks, so two coats with the same number can both stay.
Lookup: index selects a set; all ways' tags are read and compared with the address tag in parallel; hit = OR over ways of (valid AND tag match); data multiplexer selects the hitting way; offset selects the word.
For a 32 KB, 8-way cache with 64 B blocks and 48-bit addresses: offset = 6, sets = 32768/(64×8) = 64, index = 6, tag = 48 − 12 = 36 bits.
Many microcontrollers have no data cache at all, only a small flash prefetch buffer, so their timing is predictable for real-time control. Application processors use 4- to 16-way caches because conflict misses would cost more than the comparators.
Worked example 1
A 32 KB direct-mapped cache, 64 B blocks, 32-bit addresses. Split the address.
Offset = log₂ 64 = 6. Sets = 32768/64 = 512, index = 9. Tag = 32 − 9 − 6 = 17 bits.
Worked example 2
Same cache made 2-way. What changes?
Sets halve to 256, so index = 8 and tag = 18. Two 18-bit comparators run in parallel per access.
Common mix-ups
- Higher associativity always makes a cache better.
✓ It cuts conflict misses but adds comparators, energy and sometimes hit time. Gains shrink beyond 8 ways for many workloads. - The index bits are the high bits of the address.
✓ Index comes just above the offset, so consecutive blocks fall in consecutive sets.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A 8 KB direct-mapped cache has 128-byte blocks and 32-bit byte addresses. How many offset bits are there?
Practice 3 · a fresh version every time
A direct-mapped cache has 4 lines of 16 bytes and starts empty. The CPU reads these byte addresses in order: 124, 272, 272, 368, 20, 20, 272, 144, 144, 144. How many reads hit?
Practice 4
Going from direct-mapped to 4-way set-associative at the same capacity mainly costs…
Practice 5
A single set with 2 ways and LRU replacement sees blocks A, B, A, C, B. Which accesses hit?
Practice 6
How many single-bit XNOR gates do the tag comparators of an 8-way cache with 20-bit tags need?
7.Apply it
Using the cache simulator, find an access stream of at most 12 addresses that has more misses in a 2-way cache than in a direct-mapped one of the same size. (Hint: LRU is not always kind.) Explain why it happens.
8.Reflect
Concepts: Memory hierarchy and caches · Logic gates and Boolean algebra · Binary, hexadecimal and digital signals