The Cache Controller: The Miss Path
The hit path answers in one cycle. Everything else a cache does takes many cycles, and a small state machine must run it while the processor waits.
You Will Be Able To
- Name the states of a cache controller, and follow a hit and a miss through them.
- Explain how the processor is made to wait, and when it may go on.
- Count the cycles of a refill over the bus, and of a whole miss.
- Lesson
- S3.5
- Time
- 26 min
- Runs on
- Your browser
- Check
- 6 questions
The processor asks for one word, and the cache does not have it. For the next six cycles the processor must not move. Suppose it takes whatever is on the data wires one cycle too early. It then runs on with junk, and nothing reports an error.
Before You Start
You need S3.3: tag, index and offset, and the hit path. You need S0.5: the address phase and the data phase of the fast bus, and how they overlap.
You also need simple state machines: a circuit that is in one state at a time and moves at clock edges.
| Question | |
|---|---|
| 1 | Why does a cache need a controller? |
| 2 | Which states does a read pass through, on a hit and on a miss? |
| 3 | How is the processor made to wait? |
| 4 | What goes over the bus in a refill, and how many cycles does it take? |
| 5 | Why does the controller compare again after the refill? |
The Idea: A State Machine That Makes the Processor Wait
The hit path of the last lessons is pure logic. An address goes in, and "hit" and a word come out, with no memory of what happened before. A miss is different. It takes many cycles, and something must remember how far it has come. That something is the cache controller, a small state machine.
It has three jobs. First, it must hold the processor. The processor expects its word in a fixed time. On a miss the controller keeps a signal called ready at 0, and the processor stands still until ready is 1. This is the same idea as HREADY on the bus.
Second, it must fetch the block. The cache holds whole blocks, so a miss fetches the whole block, not only the word that was asked for. The controller becomes a manager on the bus and reads the words one after another. This is called the refill.
Third, it must finish cleanly. Only when the last word has arrived may the line be marked valid. The simplest way to end is to compare again: the request is still there, the line now holds the block, and so the request is a hit.
New Words
| Word | Meaning |
|---|---|
| Cache controller | The state machine that runs a cache: it holds the processor, fetches blocks, and updates the lines. |
| IDLE | The state in which the controller waits for a request from the processor. |
| COMPARE | The state in which the hit path decides. A hit is answered here. |
| ALLOCATE | The state in which the controller fetches a block from memory into a line. |
| Ready of a cache | The signal from the cache to the processor. At 1 it says: your request is done in this cycle. |
| Stall | To make the processor wait, with its request held, for as long as ready is 0. |
| Refill | The fetch of one whole block from memory into a cache line, after a miss. |
| First word of a block | The word at the address of the block with all offset bits at 0. A refill starts there. |
One Picture: Hit, Miss and Refill
- Processor
- Control / FSM
- Bus
- Memory
- A request arrives
The processor shows an address and holds it. The controller is in IDLE. It sees the request and moves on.
- A hit
In COMPARE the hit path gives its answer. On a hit the controller raises ready, the processor takes the word, and the controller returns to IDLE.
- A miss: the processor waits
On a miss, ready stays at 0. The processor keeps its request on its pins and does nothing else. It does not know why it waits.
- The refill
In ALLOCATE the controller acts as a manager on the bus. It reads the whole block from memory, word by word, starting at the first word of the block.
- Compare again
When the last word is in, the line gets its tag and its valid bit. The controller goes back to COMPARE. The same request is now a hit.
Read the diagram as text
- Processor: holds its request until ready
- IDLE: waits for a request
- COMPARE: hit or miss?
- ALLOCATE: fetches the block
- Fast bus: one word after another
- Memory: holds every block
- Processor → IDLE: a request
- IDLE → COMPARE: next cycle
- COMPARE → IDLE: hit: ready, done
- COMPARE → ALLOCATE: miss
- ALLOCATE → Fast bus: four reads
- Fast bus → Memory: addresses out, words back
- ALLOCATE → COMPARE: block is in: compare again
The controller of a read-only cache. A fourth state, for writes, comes in the next lesson.
The Smallest Example, Worked by Hand
Take the cache of the lesson on the hit path: 4 lines, blocks of 4 words, addresses of 10 bits. It is empty, and the processor reads address 0x1a4. That is tag 6, index 2, word 1. The block starts at address 0x1a0, which is word 0x68 of memory.
On the bus, the address of one word goes out while the data of the word before comes back. So four words take five cycles. Fill in the table edge by edge.
| Edge | State | On the bus | Ready |
|---|---|---|---|
| e1 | IDLE | nothing: the request is seen | 0 |
| e2 | COMPARE | nothing: line 2 is not valid, so it is a miss | 0 |
| e3 | ALLOCATE | address of word 68 | 0 |
| e4 | ALLOCATE | address of word 69, data of word 68 | 0 |
| e5 | ALLOCATE | address of word 6a, data of word 69 | 0 |
| e6 | ALLOCATE | address of word 6b, data of word 6a | 0 |
| e7 | ALLOCATE | data of word 6b: the line is filled | 0 |
| e8 | COMPARE | nothing: the line is valid with tag 6, so it is a hit | 1 |
The miss takes 8 cycles: 1 in IDLE, 1 in COMPARE, 5 in ALLOCATE and 1 more in COMPARE.
A read that hits takes 2 cycles: IDLE, and then COMPARE with ready at 1. So this miss costs 6 cycles more than a hit. The word that the processor asked for, word 69, arrived at e5. The processor still waits until e8, because the line is not complete before e7.
The Rules
Rule 1: The controller is in one of three states: IDLE, COMPARE or ALLOCATE.
IDLE waits for a request. COMPARE decides. ALLOCATE fetches.
Rule 2: A hit is answered in COMPARE.
Ready is 1 there, and the controller returns to IDLE.
Rule 3: On a miss, ready stays at 0 and the processor waits.
It keeps its request on its pins for the whole miss.
Rule 4: A refill fetches the whole block, from its first word.
The words come over the bus one after another. Four words take five cycles when the phases overlap.
Rule 5: The line becomes valid when its last word has arrived, and not before.
Its tag is stored at the same edge.
Rule 6: After the refill the controller compares again.
The request is now a hit, and it is answered like any other hit.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.9, "FSM for a Simple Cache Controller": the states Idle, Compare Tag, Allocate and Write-Back. Section 5.3, "Handling Cache Misses", gives the steps of a miss and the stall of the processor.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.3: a miss fetches the block from memory and stores it in the cache.
Arm AMBA AHB Protocol Specification, IHI 0033 issue C, section 3.1: the address phase of one transfer overlaps the data phase of the one before.
The three states follow the textbook controller, which also has a fourth state for writes. In that design a hit takes two cycles, one in each of the first two states. Many real caches answer a hit within one cycle. The two cycles here are a choice that keeps the machine simple.
The bus in the lab answers every word with no wait state. That is a choice of the lab as well.
Now Run It
The lab at the end of this page acts the controller out. It is a script in test code: a list of what happens at each clock edge, printed as one row for each edge. It is not the circuit, and it has no state register. Behind the bus is a memory in which word n holds c0de0000 + n.
e1 IDLE | the processor asks to read 1a4
e2 COMPARE | miss: ready stays 0, and the processor waits
e3 ALLOCATE | bus: address of word 68
e4 ALLOCATE | bus: address of word 69, data of word 68 = c0de0068
e5 ALLOCATE | bus: address of word 6a, data of word 69 = c0de0069
e6 ALLOCATE | bus: address of word 6b, data of word 6a = c0de006a
e7 ALLOCATE | bus: data of word 6b = c0de006b. The line is filled
e8 COMPARE | hit: ready=1 data=c0de0069
e9 IDLE | the processor asks to read 1a8
e10 COMPARE | hit: ready=1 data=c0de006a
2 reads took 10 edgesRules 1 and 3 at work: rows e1 and e2. The request is seen in IDLE, and COMPARE finds a miss. Ready stays at 0.
Rule 4 at work: rows e3 to e7. The refill starts at word 68, the first word of the block, although the processor asked for word 69. From e4 on, each row has the address of one word and the data of the word before.
Rules 5 and 6 at work: rows e7 and e8. The line is filled at e7. At e8 the controller is in COMPARE again, and this time it is a hit: ready is 1 and the data is c0de0069.
Rule 2 at work: rows e9 and e10. The second read is in the same block, so it is a hit, and it takes two edges. The last row adds up: 8 edges and 2 edges.
This lab leaves out writes, and with them a fourth state. It also leaves out the hit path itself, which you built as a circuit in an earlier practice problem.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| The processor waits for the whole miss | The processor does other work meanwhile | A simple processor and a simple controller. Every miss is lost time. |
| Fetch the whole block | Fetch only the word that was asked for | The neighbours are there for the next reads. The miss takes longer. |
| Valid only after the last word | Valid as soon as the first word is in | No read can ever see half a block. The processor waits for words it did not ask for. |
| Compare again after the refill | A special state that answers the processor | No extra logic: the hit path does the work. One more cycle on every miss. |
The Trap: Ready, One Edge Too Early
A designer wants to save that last cycle. The block is complete at the last edge of ALLOCATE, so ready is raised right there. In simulation the first tests pass. Then a read of the last word of a block returns junk. Here is that read in the lab:
e1 IDLE | the processor asks to read 1ac
e2 COMPARE | miss: ready stays 0, and the processor waits
e3 ALLOCATE | bus: address of word 68
e4 ALLOCATE | bus: address of word 69, data of word 68 = c0de0068
e5 ALLOCATE | bus: address of word 6a, data of word 69 = c0de0069
e6 ALLOCATE | bus: address of word 6b, data of word 6a = c0de006a
fault: ready is given at this edge, and the processor takes data=xxxxxxxx
e7 ALLOCATE | bus: data of word 6b = c0de006b. The line is filled
e8 COMPARE | hit: ready=1 data=c0de006bLook at the line that says "fault". At that edge the last word, word 6b, is still on the bus. It is not in the line yet. The processor takes what the line holds, and that is x: nothing. One edge later, in row e8, the line would have given c0de006b.
Reads of the first three words did not show the fault, because those words were in the line early. Rule 5 and rule 6 together prevent this: answer from the line, and only after the line is complete.
At Scale
This section looks ahead. Real controllers have the same skeleton and many more states.
Writes
A cache that takes writes needs a fourth state. The next lesson adds it.
A block in one request
Real buses can send one address and get a whole block back. The refill then takes fewer bus cycles.
The asked word first
Some caches fetch the word that was asked for first and hand it to the processor at once. The rest of the block follows.
Not waiting
A large processor does other work during a miss. Its cache must then track several misses at the same time.
Try It
Run the lab. Check rows e1 to e8 against the hand table.
Add read(10'h2a8) at the end. Predict how many edges it takes, and which words are fetched.
Count the edges that the processor spends waiting in this run, and the edges in which it gets a word.
e1 IDLE | the processor asks to read 1a4 e2 COMPARE | miss: ready stays 0, and the processor waits e3 ALLOCATE | bus: address of word 68 e4 ALLOCATE | bus: address of word 69, data of word 68 = c0de0068 e5 ALLOCATE | bus: address of word 6a, data of word 69 = c0de0069 e6 ALLOCATE | bus: address of word 6b, data of word 6a = c0de006a e7 ALLOCATE | bus: data of word 6b = c0de006b. The line is filled e8 COMPARE | hit: ready=1 data=c0de0069 e9 IDLE | the processor asks to read 1a8 e10 COMPARE | hit: ready=1 data=c0de006a 2 reads took 10 edgesCheck Yourself
Common Mistakes
Wrong Raise ready when the asked word arrives on the bus.
The line is not complete, and the hit path reads from the line.
Fix Fill the line, then compare again and answer from the line.
Wrong Start the refill at the address that the processor asked for.
A line holds a whole block, from its first word.
Fix Clear the offset bits, and fetch from the first word of the block.
Wrong Mark the line valid at the start of the refill.
A read could then hit on a line that is half old and half new.
Fix Set the valid bit and the tag when the last word has arrived.
Remember
- A cache controller is a state machine: IDLE waits, COMPARE decides, ALLOCATE fetches.
- On a miss, ready stays at 0 and the processor waits with its request held.
- A refill fetches the whole block from its first word. Four words take five bus cycles.
- The line becomes valid after its last word. Then the controller compares again, and the request hits.