S3 · Lesson 43 · Caches in hardware
Tag and data arrays, address fields, comparators, write policies and miss handling
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.
- 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 − (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.
Predict
A 32 KB, 8-way cache with 64-byte lines serves 32-bit addresses. How wide is each tag?
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.
Name it
Address fields
For a cache of S bytes, lines of B bytes, W ways and A-bit addresses:
- offset: bits;
- sets: , index: bits;
- tag: bits.
The arrays and the hit path
- Data array: bytes. Tag array: one entry per line, 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:
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.
Worked 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?
Show the solution
- Bits per line. 512 lines, each with 512 data bits, a 20-bit tag, a valid bit and a dirty bit:
- Total. a 4.3% overhead (before replacement bits).
- 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.
Worked 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?
Show the solution
- Miss penalty. The latency plus the transfer of a 64-byte line:
- AMAT.
- With critical word first. The processor resumes after about 51 cycles:
- 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.
Worked 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.
Show the solution
- Offset. 32-byte lines need 5 bits.
- Index. There are lines, in 512 sets: 9 index bits.
- Tag.
- Tag array. Each line stores tag, valid and dirty, 28 bits: which is 7 KB, 11% on top of the data.
- 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
A 64 KB, 16-way cache has 32-byte lines and 40-bit addresses. How many tag bits?
Check
How many index bits does a 32 KB, 8-way cache with 32-byte lines need?
Check
A write-back cache of 16 KB, 8-way, with 32-byte lines and 48-bit addresses keeps a valid and a dirty bit per line. How many SRAM bits does it hold in all?
Check
A miss waits 80 cycles of latency and then moves a 32-byte line over a 16-byte bus, one transfer per cycle. What's the miss penalty?
Check
A cache hits in 2 cycles and misses 0.01 of the time. A miss waits 150 cycles and then moves a 64-byte line over a 16-byte bus. What's the AMAT?
Check
A core at 1 GHz executes one instruction per cycle, 0.08 of them stores of 8 bytes. How much write traffic does a write-through cache send to the next level?
Check
A 32 KB, 8-way cache with 32-byte lines serves 40-bit addresses.
(a)
How many offset bits?
(b)
How many index bits?
(c)
How many tag bits?
Check
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 1
What is the miss penalty?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the average memory access time?
Step 3
And with critical word first?
Explain why
Why does making a first-level cache more associative eventually slow down every access, even the hits?
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
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.