Memory and Caches in One Picture

A large memory is slow, and a processor asks it something in almost every cycle. A cache keeps copies of recently used boxes close by.

You Will Be Able To

  • Tell ROM, RAM and flash apart, and say why a large memory answers slowly.
  • Split an address into a line number and a tag, and decide hit or miss by hand.
  • Explain when a cache helps, and when it does not.
Lesson
S0.7
Time
25 min
Runs on
Your browser
Check
5 questions

S0.6 counted the bus transfers of a small program: 9 of 13 were fetches from memory. Now make that memory large enough for real software. It becomes slow, and the processor waits on almost every instruction.

Before You Start

You need S0.1: memory boxes and addresses. You need S0.6: fetch, load and store. You also need binary numbers, to split an address into two groups of bits.

Five questions this page answers
Question
1How do ROM, RAM and flash differ?
2Why does a large memory answer more slowly than a small one?
3What is a cache, and what are a hit and a miss?
4How does a cache know whose copy a line holds?
5When does a cache save time, and when does it save none?

The Idea: Keep a Copy Close

A chip holds more than one kind of memory, because no single kind does every job.

Three kinds of memory
KindKeeps its content with the power off?Can a program write it?Typical job
ROM (read-only memory)yesno: its content is fixed when the chip is madethe first program a chip runs
RAM (random-access memory)noyes, as fast as it readsthe working data of a program
Flashyesyes, but slowly, and a whole block must be erased firstthe program of a product, which may be updated

The Boot ROM and the SRAM of ap_soc, met in S0.2, are a ROM and a RAM.

All three share one problem: size costs speed. S0.1 gave the reason. Picking one box of sixteen takes a small multiplexer. Picking one of millions takes many more gates in a row, and far longer wires. A large memory may need many cycles for one read.

Programs offer a way out. They use the same few addresses again and again: a loop fetches the same instructions on every round. So a chip puts a small, fast memory next to the processor. It keeps copies of the boxes used most recently. That small memory is a cache.

When the processor asks for an address, the cache looks for a copy first. Finding one is a hit, and the answer comes at once. Not finding one is a miss. The cache then reads the large memory, hands the value over, and keeps a copy for next time.

New Words

New words on this page
WordMeaning
ROM, RAM, flashThe three kinds of memory in the table above.
CacheA small, fast memory that holds copies of recently used boxes of a larger, slower memory.
HitThe cache holds a copy of the address asked for. The answer is fast.
MissThe cache holds no copy. The value comes from the slow memory, and a copy is kept.
Cache lineOne place in the cache, called a line for short. It holds one copy and remembers whose copy it is.
TagThe part of the address that a line stores beside its copy, to record whose copy it is.
Valid bitOne bit for each line. 0 means the line is empty. Every line starts at 0.

The cache on this page has eight lines. It sits in front of a memory of 64 boxes, so an address has six bits. The cache splits every address in two.

How the cache splits a six-bit address
BitsNameJobAddress 8 (binary 001 000)Address 16 (binary 010 000)
bottom threeline numberpicks one of the eight lines000: line 0000: line 0
top threetagstored in the line, and compared on every read001: tag 1010: tag 2

Every address has exactly one line it may use. Eight different addresses share each line, and the tag says which one is there now.

One Picture: Miss, Hit, Replace

A Small Fast Memory in Front of a Large Slow OneStep through the picture. It starts with no cache, adds one, and then follows three reads.
  • Processor
  • Memory
CallValueReturnWaiting
A Small Fast Memory in Front of a Large Slow OneStep through the picture. It starts with no cache, adds one, and then follows three reads. component Processor asks for one addressat a time component Cache 8 lines: answers in 1cycle component Memory 64 boxes: answers in10 cycles component Line 0 of thecache a valid bit, a tag andone copy the addressthe valueonly on a missthe value, kept as acopythe address picks theline
  1. component Processorasks for one address at a time
    • the address → Cache
  2. component Memory64 boxes: answers in 10 cycles
    • the value, kept as a copy → Cache
  3. component Cache8 lines: answers in 1 cycle
    • the value → Processor
    • only on a miss → Memory
    • the address picks the line → Line 0 of the cache
  4. component Line 0 of the cachea valid bit, a tag and one copy
  1. Without a cache

    Every read walks to the large memory and waits 10 cycles for the answer.

  2. Add a small memory in between

    The cache has only eight lines, so it answers in one cycle. Each line can hold a copy of one memory box.

  3. Read box 8: a miss

    The cache starts empty, so it has no copy. It reads box 8 from memory, which takes 10 cycles. It keeps a copy in line 0.

    Follow the calls, values and waits
    1. data 108: Memory → Cache
  4. Read box 8 again: a hit

    Line 0 holds the copy. The cache answers in one cycle, and memory is not asked at all.

    Follow the calls, values and waits
    1. data 108: Cache → Processor
  5. Read box 16: a miss that replaces

    Box 16 also belongs in line 0, and the copy there is of box 8. The cache reads box 16 from memory and overwrites the line.

    Follow the calls, values and waits
    1. data 116: Memory → Cache
Read the diagram as text
  • Processor: asks for one address at a time
  • Cache: 8 lines: answers in 1 cycle
  • Memory: 64 boxes: answers in 10 cycles
  • Line 0 of the cache: a valid bit, a tag and one copy
  • Processor → Cache: the address
  • Cache → Processor: the value
  • Cache → Memory: only on a miss
  • Memory → Cache: the value, kept as a copy
  • Cache → Line 0 of the cache: the address picks the line

In this example box n holds the number 100 + n. The costs of 1 and 10 cycles are choices of this page.

The processor never changed what it does. It asks for an address and waits for the value. The cache only changes how long the wait is.

The Smallest Example, Worked by Hand

A program reads boxes 8, 9 and 10, twice round. Then it reads box 16, and then box 8 again. The cache starts empty. For each read, split the address, look at that line, and compare the tag.

Eight reads, by hand
ReadAddress in binaryLineTagWhat the line heldResultCycles
box 8001 00001emptymiss10
box 9001 00111emptymiss10
box 10001 01021emptymiss10
box 8001 00001tag 1hit1
box 9001 00111tag 1hit1
box 10001 01021tag 1hit1
box 16010 00002tag 1miss: replaces the copy of box 810
box 8001 00001tag 2miss: replaces the copy of box 1610

Three hits and five misses cost 3 + 50 = 53 cycles. With no cache, eight reads cost 80. The second round of the loop was almost free. The last two reads show the weak spot: boxes 8 and 16 want the same line.

The Rules

  1. Rule 1: A cache holds copies.

    The large memory still holds every box. The cache holds a few of them a second time, close to the processor.

  2. Rule 2: The address picks the line.

    The bottom bits of the address are the line number. Each address has exactly one line it may use.

  3. Rule 3: A read is a hit when the line is valid and its tag equals the tag of the address.

    Then the copy is the answer, and the large memory is not asked.

  4. Rule 4: Any other read is a miss.

    The value comes from the large memory. The line then takes the copy and the new tag, and its old content is lost.

  5. Rule 5: A cache saves time only when addresses repeat.

    The first read of any box is always a miss.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy. Why a small memory in front of a large one works, and the habit of programs to reuse addresses: section 5.1. Memory technologies: section 5.2. The cache with one possible line for each address, its tag and its valid bit: section 5.3.

ROM, RAM and flash as circuits: the textbook "Digital Design and Computer Architecture" by Harris and Harris, section 5.5. Caches: the same book, section 8.3.

The textbooks call the line number the index, and call this kind of cache direct-mapped. The eight-line cache, the 64-box memory and the costs of 1 and 10 cycles are our own teaching example.

Now Run It

The lab at the end of this page is this cache in SystemVerilog, in front of a 64-box memory. It has no clock. It adds up the cycles that each read would cost: 1 for a hit and 10 for a miss.

Printed by the lab, one row for each read. Box n holds 100 + n, so every value can be checked.
read 8  line=0 tag=1  miss  value=108  (the line was empty)
read 9  line=1 tag=1  miss  value=109  (the line was empty)
read 10  line=2 tag=1  miss  value=110  (the line was empty)
read 8  line=0 tag=1  hit   value=108
read 9  line=1 tag=1  hit   value=109
read 10  line=2 tag=1  hit   value=110
read 16  line=0 tag=2  miss  value=116  (replaces the copy of box 8)
read 8  line=0 tag=1  miss  value=108  (replaces the copy of box 16)
8 reads: 3 hits, 5 misses, 53 cycles (80 with no cache)

Compare it with your hand table. Rule 3 at work: the fourth, fifth and sixth rows are hits, because the line is valid and the tags match. Rule 4 at work: the read of box 16 replaces the copy of box 8, and the last row pays for it.

The last row is the bill: 53 cycles against 80. Every value is still correct. A cache never changes what a read returns. It changes only how long the read takes.

This lab leaves out writes. A store must reach the copy and the large memory, in some order. Later lessons on caches cover that.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
A small memory in front of a large oneOne memory, large and fastSpeed close to the small one, at the size of the large one. A large fast memory cannot be built at a sane cost.
The address picks the lineSearch every line for a copyOne line to look at and one tag to compare, so a hit is quick. Two busy addresses may fight over one line.
Store a tag beside each copyStore the whole addressFewer bits in each line. The line number is already known from where the copy sits.
Keep the newest copy on a missKeep the old copyThe address just used is the one most likely to be used again.

The Trap: Two Boxes, One Line

A program reads box 8 and box 16 in turn, three times. The cache has eight lines and seven of them are empty. It still does not help at all.

The same lab with six reads: boxes 8 and 16 in turn.
read 8  line=0 tag=1  miss  value=108  (the line was empty)
read 16  line=0 tag=2  miss  value=116  (replaces the copy of box 8)
read 8  line=0 tag=1  miss  value=108  (replaces the copy of box 16)
read 16  line=0 tag=2  miss  value=116  (replaces the copy of box 8)
read 8  line=0 tag=1  miss  value=108  (replaces the copy of box 16)
read 16  line=0 tag=2  miss  value=116  (replaces the copy of box 8)
6 reads: 0 hits, 6 misses, 60 cycles (60 with no cache)

Every read is a miss. Both boxes map to line 0, so each read throws out the copy that the next read needs. The bill is 60 cycles, the same as with no cache. Engineers who meet a slow loop on a fast chip learn to check for this.

At Scale

This page8 lines, 1 box in each
A large chipthousands of lines, many bytes in each

This section looks ahead. A phone chip has several caches of many thousand lines each. The ideas of this page carry over unchanged.

  1. Longer lines

    A real line holds several neighbouring bytes, often 64. Programs tend to use neighbours soon after each other, so one miss brings in data for several later hits.

  2. More than one line to choose from

    Large caches let each address use one of several lines. That softens the trap above, and costs more comparing.

  3. Caches behind caches

    A large chip puts a small, very fast cache next to each processor, and larger, slower ones behind it. The lesson The Cache Hierarchy of a Core Cluster shows the layout.

  4. No cache at all

    A small chip, such as the one in a thermostat, often has none. Its memory is small and on the chip, so it answers quickly anyway.

Try It

Run the lab. Match each printed row to one row of your hand table.

Add a ninth read, of box 9. Predict hit or miss before you run it.

Change SLOW to 50. Predict the two numbers of cycles in the last row.

Replace the read of box 16 by a read of box 17. Predict which later read changes from a miss to a hit.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: read 8 line=0 tag=1 miss value=108 (the line was empty) read 9 line=1 tag=1 miss value=109 (the line was empty) read 10 line=2 tag=1 miss value=110 (the line was empty) read 8 line=0 tag=1 hit value=108 read 9 line=1 tag=1 hit value=109 read 10 line=2 tag=1 hit value=110 read 16 line=0 tag=2 miss value=116 (replaces the copy of box 8) read 8 line=0 tag=1 miss value=108 (replaces the copy of box 16) 8 reads: 3 hits, 5 misses, 53 cycles (80 with no cache)

Check Yourself

Predict the outputA loop reads boxes 8, 9 and 10, four times round. The cache starts empty. What is the last line the lab prints?
// A small cache in front of a large, slow memory. This lab models reads only.
module tb;
  // ---- The slow memory: 64 boxes. One read costs SLOW cycles (a choice of this lab). ----
  localparam SLOW = 10;
  logic [7:0] mem [0:63];

  // ---- The cache: 8 lines. A line holds one copied box, and remembers whose copy it is. ----
  logic       valid [0:7];     // 1 = this line holds a copy
  logic [2:0] tag   [0:7];     // the top three bits of the address the copy came from
  logic [7:0] data  [0:7];     // the copy itself

  int hits = 0, misses = 0, cycles = 0;
  logic [2:0] line, t;         // the two parts of the address being read

  // One read, as the processor would ask for it. The address has six bits.
  task read(input logic [5:0] addr);
    line = addr[2:0];          // the bottom three bits pick the line
    t    = addr[5:3];          // the top three bits are the tag
    if (valid[line] && tag[line] == t) begin
      hits++;  cycles += 1;                                   // HIT: the copy answers in one cycle
      $display("read %0d  line=%0d tag=%0d  hit   value=%0d", addr, line, t, data[line]);
    end else begin
      misses++;  cycles += SLOW;                              // MISS: go to the slow memory
      if (valid[line]) $display("read %0d  line=%0d tag=%0d  miss  value=%0d  (replaces the copy of box %0d)", addr, line, t, mem[addr], {tag[line], line});
      else             $display("read %0d  line=%0d tag=%0d  miss  value=%0d  (the line was empty)", addr, line, t, mem[addr]);
      valid[line] = 1;  tag[line] = t;  data[line] = mem[addr];   // keep a copy for next time
    end
  endtask

  initial begin
    for (int i = 0; i < 64; i++) mem[i] = 100 + i;   // box n holds 100 + n, so values are easy to check
    for (int i = 0; i < 8; i++)  valid[i] = 0;       // the cache starts empty
    read(8);
    read(9);
    read(10);
    read(8);
    read(9);
    read(10);
    read(8);
    read(9);
    read(10);
    read(8);
    read(9);
    read(10);
    $display("%0d reads: %0d hits, %0d misses, %0d cycles (%0d with no cache)", hits + misses, hits, misses, cycles, (hits + misses) * SLOW);
  end
endmodule
ConceptWhich memory keeps its content with the power off and can also be rewritten by the product?
ConceptWhy does a large memory answer more slowly than a small one?
ConceptA read finds its line valid, but the stored tag differs from the tag of the address. What happens?
ConceptA program reads 1,000 different boxes, each exactly once. How much time does the cache save?

Common Mistakes

Wrong Expect the first read of a box to be a hit.

The cache holds only copies of boxes that were read before.

Fix Count the first read of every box as a miss.

Wrong Judge a hit by the line number alone.

Eight addresses share each line. The line may hold a copy of a different box.

Fix Check the valid bit, then compare the stored tag with the tag of the address.

Wrong Assume a nearly empty cache cannot miss.

Two addresses with the same line number replace each other, however empty the other lines are.

Fix Look at which lines the busy addresses use, not at how full the cache is.

Remember

  • ROM keeps its content and cannot be written; RAM can be written and forgets; flash keeps its content and is rewritten slowly.
  • A large memory is slow, because picking one box among many takes more logic and longer wires.
  • A cache is a small fast memory holding copies of recently used boxes. A hit is fast; a miss fetches and keeps a copy.
  • The bottom bits of an address pick the line; the stored tag says whose copy the line holds.
  • A cache helps only when addresses repeat, and two busy addresses that share a line can defeat it.

Practice

Report a problem with this page