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.

Five questions this page answers
Question
1Why does a cache need a controller?
2Which states does a read pass through, on a hit and on a miss?
3How is the processor made to wait?
4What goes over the bus in a refill, and how many cycles does it take?
5Why 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

New words on this page
WordMeaning
Cache controllerThe state machine that runs a cache: it holds the processor, fetches blocks, and updates the lines.
IDLEThe state in which the controller waits for a request from the processor.
COMPAREThe state in which the hit path decides. A hit is answered here.
ALLOCATEThe state in which the controller fetches a block from memory into a line.
Ready of a cacheThe signal from the cache to the processor. At 1 it says: your request is done in this cycle.
StallTo make the processor wait, with its request held, for as long as ready is 0.
RefillThe fetch of one whole block from memory into a cache line, after a miss.
First word of a blockThe word at the address of the block with all offset bits at 0. A refill starts there.

One Picture: Hit, Miss and Refill

Three States Between the Processor and the BusStep through the picture. It follows one read that hits, and then one read that misses.
  • Processor
  • Control / FSM
  • Bus
  • Memory
Three States Between the Processor and the BusStep through the picture. It follows one read that hits, and then one read that misses. component Processor holds its requestuntil ready component IDLE waits for a request component COMPARE hit or miss? component ALLOCATE fetches the block component Fast bus one word afteranother component Memory holds every block a requestnext cyclehit: ready, donemissfour readsaddresses out, wordsbackblock is in: compareagain
  1. component Processorholds its request until ready
    • a request → IDLE
  2. component IDLEwaits for a request
    • next cycle → COMPARE
  3. component COMPAREhit or miss?
    • hit: ready, done → IDLE
    • miss → ALLOCATE
  4. component ALLOCATEfetches the block
    • four reads → Fast bus
    • block is in: compare again → COMPARE
  5. component Fast busone word after another
    • addresses out, words back → Memory
  6. component Memoryholds every block
  1. A request arrives

    The processor shows an address and holds it. The controller is in IDLE. It sees the request and moves on.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

One read that misses, edge by edge
EdgeStateOn the busReady
e1IDLEnothing: the request is seen0
e2COMPAREnothing: line 2 is not valid, so it is a miss0
e3ALLOCATEaddress of word 680
e4ALLOCATEaddress of word 69, data of word 680
e5ALLOCATEaddress of word 6a, data of word 690
e6ALLOCATEaddress of word 6b, data of word 6a0
e7ALLOCATEdata of word 6b: the line is filled0
e8COMPAREnothing: the line is valid with tag 6, so it is a hit1

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

  1. Rule 1: The controller is in one of three states: IDLE, COMPARE or ALLOCATE.

    IDLE waits for a request. COMPARE decides. ALLOCATE fetches.

  2. Rule 2: A hit is answered in COMPARE.

    Ready is 1 there, and the controller returns to IDLE.

  3. Rule 3: On a miss, ready stays at 0 and the processor waits.

    It keeps its request on its pins for the whole miss.

  4. 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.

  5. Rule 5: The line becomes valid when its last word has arrived, and not before.

    Its tag is stored at the same edge.

  6. 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.

Printed by the lab on Icarus. Two reads: address 1a4, and then 1a8 in the same block.
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 edges

Rules 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 choiceThe alternativeWhat the choice buys, and costs
The processor waits for the whole missThe processor does other work meanwhileA simple processor and a simple controller. Every miss is lost time.
Fetch the whole blockFetch only the word that was asked forThe neighbours are there for the next reads. The miss takes longer.
Valid only after the last wordValid as soon as the first word is inNo read can ever see half a block. The processor waits for words it did not ask for.
Compare again after the refillA special state that answers the processorNo 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:

The same script with ready raised at the last edge of ALLOCATE. The read is of address 1ac, the last word of its block.
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=c0de006b

Look 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 page3 states, 1 request at a time
A real cachedozens of states, several requests in flight

This section looks ahead. Real controllers have the same skeleton and many more states.

  1. Writes

    A cache that takes writes needs a fourth state. The next lesson adds it.

  2. A block in one request

    Real buses can send one address and get a whole block back. The refill then takes fewer bus cycles.

  3. 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.

  4. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 edges

Check Yourself

Predict the outputThe cache is empty. The script reads 040, then 044 in the same block, then 090 in another line. What does the last row say?
// A direct-mapped cache with its controller, acted out one clock edge at a time.
// This is a script in test code: it says what the controller does at each edge. It is not the circuit.
module tb;
  // 4 lines, blocks of 4 words, addresses of 10 bits: tag = bits 9 to 6, index = bits 5 and 4, word = bits 3 and 2
  logic        valid [0:3];
  logic [3:0]  tags  [0:3];
  logic [31:0] block [0:3][0:3];
  logic [31:0] mem [0:255];          // the memory behind the bus, in words. Word n starts as c0de0000 + n
  string state = "IDLE";
  int edge_no = 0;

  task automatic tick(input string what);          // one clock edge, and one row
    edge_no++;
    $display("e%0d %-10s | %s", edge_no, state, what);
    #10;
  endtask

  // The miss path. The processor waits for all of it.
  task automatic miss_path(input logic [3:0] tag, input logic [1:0] index);
    logic [7:0] first, out, back;
    state = "ALLOCATE";
    first = {tag, index, 2'b00};                   // the first word of the block: the offset bits are 0
    for (int k = 0; k <= 4; k++) begin             // the address of word k goes out while the data of word k-1 comes back
      out = first + k;  back = first + k - 1;
      if (k > 0) block[index][k - 1] = mem[back];
      if (k == 0)     tick($sformatf("bus: address of word %02h", out));
      else if (k < 4) tick($sformatf("bus: address of word %02h, data of word %02h = %08h", out, back, mem[back]));
      else            tick($sformatf("bus: data of word %02h = %08h. The line is filled", back, mem[back]));
    end
    tags[index] = tag;  valid[index] = 1;
    state = "COMPARE";
  endtask

  task automatic read(input logic [9:0] address);
    logic [3:0] tag;
    logic [1:0] index, word;
    tag = address[9:6];  index = address[5:4];  word = address[3:2];
    state = "IDLE";     tick($sformatf("the processor asks to read %03h", address));
    state = "COMPARE";
    if (!(valid[index] && tags[index] == tag)) begin
      tick("miss: ready stays 0, and the processor waits");
      miss_path(tag, index);
    end
    tick($sformatf("hit: ready=1 data=%08h", block[index][word]));
    state = "IDLE";
  endtask

  initial begin
    for (int n = 0; n < 256; n++) mem[n] = 32'hc0de_0000 + n;
    for (int i = 0; i < 4; i++) begin valid[i] = 0; end
    read(10'h040);
    read(10'h044);
    read(10'h090);
    $display("3 reads took %0d edges", edge_no);
  end
endmodule
ConceptWhy does a cache need a controller, when the hit path is plain logic?
ConceptThrough which states does a read that misses pass, in order?
ConceptHow does the controller make the processor wait?
ConceptA refill fetches a block of 4 words over a bus whose phases overlap. How many cycles does it take?
ConceptWhy does the controller go back to COMPARE after the refill?

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.

Practice

Report a problem with this page