TactivergeLite

Lite / Independent Engineering Research / 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 · Full interactive version

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.

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?

  1. Almost every access hits
  2. Every access misses: they evict each other
  3. Half hit, half miss
  4. The cache stores both in one line
Guess first, then reveal

a[i] and b[i] map to the same line, so each access evicts the other: 100% misses, even though the cache has plenty of room. A 2-way set-associative cache holds both and the misses vanish after the first touches.

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.

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

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

Intuitive

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.

Technical

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.

Mathematical

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.

Applied

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

bits

bits

hits

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?

XNORs

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.

Reflect

What part of a cache's design felt most like ordinary digital logic you already knew?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Locality and the memory hierarchyAverage memory access time →