The Direct-Mapped Cache in RTL

A cache must decide hit or miss within one clock cycle. The simplest cache does it by cutting the address into three fields and looking at exactly one line.

You Will Be Able To

  • Split an address into tag, index and offset for a cache of a given size.
  • Describe the hit path: one line read, one tag compared, one word picked.
  • Say what a reset of a cache must clear, and what it may leave alone.
Lesson
S3.3
Time
26 min
Runs on
Your browser
Check
6 questions

A new chip boots, and its first read from the cache returns deadbeef. The memory behind the cache holds the right word. Nothing wrote the wrong one. The cache answered before anyone had put anything into it.

Before You Start

You need S0.7: a cache, a hit, a miss, a line, a tag and a valid bit. You need S3.2: a line holds a block of neighbouring words.

You also need binary and hexadecimal numbers, and powers of two: 16 is two to the power of four.

Five questions this page answers
Question
1Into which three fields does a cache split an address?
2How do the sizes of a cache fix the width of each field?
3When exactly is a read a hit?
4How does the cache pick one word out of a block?
5What must a reset clear in a cache, and why only that?

The Idea: One Address, Three Fields, One Line

In S0.7 a line held one word, and the address was cut in two: a line number and a tag. In S3.2 a line grew to a block of neighbours. A real cache puts the two together, and cuts the address in three.

The low bits say where a byte sits inside its block. They are the offset. The next bits say which line this block must use. They are the index, the usual name for the line number. All the bits above are the tag. The line stores the tag, so that it knows whose block it holds.

This gives the simplest of all caches. Every block has exactly one line that it may use, fixed by its index. So the cache never searches. It reads one line, compares one tag, and knows. That is why it is called direct-mapped, and why it is fast. As a circuit it is a few registers, one comparator and one multiplexer.

The price was shown in S0.7. Two blocks with the same index fight over one line, even when every other line is empty.

One more thing belongs to the hardware. After power-up, the lines hold whatever their cells happened to settle to. A tag of junk can match a real address by chance. So each line has its valid bit, and a reset must clear all of them before the first read.

New Words

New words on this page
WordMeaning
RTLRegister-transfer level: a circuit described as registers and the logic between them. It is the level at which a designer writes Verilog.
OffsetThe low bits of an address. They say where a byte sits inside its block.
IndexThe middle bits of an address. They are the number of the one line that this block may use.
Direct-mapped cacheA cache in which every block has exactly one line that it may use.
Word selectThe upper bits of the offset. They pick one word out of the block.
Hit pathThe logic from the address to the answer "hit or miss" and to the word. It must fit into one clock cycle.
Tag compareThe check whether the tag stored in a line equals the tag of the address.
Power of twoA number that is 2 multiplied by itself some times: 2, 4, 8, 16. Sixteen things need four bits to number them.

One Picture: The Hit Path

From an Address to One Word of One LineStep through the picture. It follows one address through the cache, field by field.
  • Processor
  • Memory
  • Control / FSM
  • Bus
From an Address to One Word of One LineStep through the picture. It follows one address through the cache, field by field. component Address tag, index, offset component The lines each: a valid bit, atag, a block component One line the line that theindex picks component Tag compare stored tag againstaddress tag component Hit valid, and the tagsare equal component Word select one word of the block component Reset clears every valid bit the indexread outthe tagstored tag, valid bitequal?the blockvalid bits to 0
  1. component Addresstag, index, offset
    • the index → The lines
    • the tag → Tag compare
  2. component The lineseach: a valid bit, a tag, a block
    • read out → One line
  3. component One linethe line that the index picks
    • stored tag, valid bit → Tag compare
    • the block → Word select
  4. component Tag comparestored tag against address tag
    • equal? → Hit
  5. component Hitvalid, and the tags are equal
  6. component Word selectone word of the block
  7. component Resetclears every valid bit
    • valid bits to 0 → The lines
  1. Three fields

    The cache cuts every address into three fields. The low bits are the offset, the middle bits are the index, and the high bits are the tag.

  2. The index picks one line

    The index is used as the number of a line. That one line is read out: its valid bit, its tag and its block. No other line is looked at.

  3. The tag is compared

    Many addresses share this line. The stored tag says which block is there now. The cache compares it with the tag of the address.

  4. Hit

    It is a hit when the valid bit is 1 and the two tags are equal. Anything else is a miss.

  5. The offset picks the word

    The block holds several words. The upper bits of the offset pick one of them. This runs beside the compare, not after it.

  6. What a reset clears

    After power-up the lines hold junk. A reset sets every valid bit to 0. The tags and the blocks can stay as they are: nobody trusts them.

Read the diagram as text
  • Address: tag, index, offset
  • The lines: each: a valid bit, a tag, a block
  • One line: the line that the index picks
  • Tag compare: stored tag against address tag
  • Hit: valid, and the tags are equal
  • Word select: one word of the block
  • Reset: clears every valid bit
  • Address → The lines: the index
  • The lines → One line: read out
  • Address → Tag compare: the tag
  • One line → Tag compare: stored tag, valid bit
  • Tag compare → Hit: equal?
  • One line → Word select: the block
  • Reset → The lines: valid bits to 0

The hit path of a direct-mapped cache. The lab below acts it out for a cache of four lines.

The Smallest Example, Worked by Hand

Take a small cache. Addresses have 10 bits and count bytes. The cache has 4 lines. A line holds a block of 4 words, and a word is 4 bytes, so a block is 16 bytes.

The widths of the three fields
FieldWhat it must numberBitsWhich bits of the address
Offset16 bytes in a block4bits 3 to 0; bits 3 and 2 are the word select
Index4 lines2bits 5 and 4
Tageverything else10 − 4 − 2 = 4bits 9 to 6

Sixteen is two to the power of four, so the offset has four bits. Four is two to the power of two, so the index has two.

Now split one address. 0x1a4 is binary 01 1010 0100. Cut it from the right: the offset is 0100, the index is 10, and the tag is 0110. So this read uses line 2, expects tag 6, and wants word 1 of the block.

Here are five reads in a row. For each one, split the address, look at line 2, and decide. Cover the last two columns first.

Five reads, all on line 2
AddressBinaryTagIndexWordLine 2 holdsResult
0x1a40110 10 0100621valid 0miss: the block of tag 6 comes in
0x1a40110 10 0100621valid 1, tag 6hit
0x1ac0110 10 1100623valid 1, tag 6hit: another word of the same block
0x2a81010 10 1000a22valid 1, tag 6miss: the tags differ, and tag a replaces tag 6
0x1a00110 10 0000620valid 1, tag amiss: its block was pushed out

The binary column is written as tag, index, offset. Two hits and three misses.

The Rules

  1. Rule 1: The offset is the low bits: as many as it takes to number the bytes of a block.

    A block of 16 bytes needs 4 bits. The upper bits of the offset are the word select.

  2. Rule 2: The index is the next bits: as many as it takes to number the lines.

    A cache of 4 lines needs 2 bits. The index picks one line, and only that line is read.

  3. Rule 3: The tag is every bit above the index.

    The line stores it, beside the block and the valid bit.

  4. Rule 4: A read is a hit when the valid bit is 1 and the stored tag equals the tag of the address.

    Both conditions are needed. Either one alone is a miss.

  5. Rule 5: The word select picks the word, beside the tag compare.

    The cache does not wait for the compare. On a miss the word is simply not used.

  6. Rule 6: A reset clears every valid bit, and nothing else.

    With the valid bit at 0, the tag and the block of a line are never trusted. So they need no reset.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.3, "Accessing a Cache": the index, the tag, the valid bit, and the count of bits in a cache.

The textbook "Digital Design and Computer Architecture" by Harris and Harris, sections 8.3.1 and 8.3.2: the block, the direct-mapped cache, and the fields of the address.

The cache of four lines with blocks of four words, and the addresses of 10 bits, are our own teaching example. So are the costs of 1 cycle for a look and 20 more for a miss, as on the page before.

Books use "block offset" and "byte offset" for the two parts of the offset. This page says "word select" for the upper part.

Now Run It

The lab at the end of this page is test code that acts out this cache, one read at a time. Behind it is a slow memory in which word n holds c0de0000 + n. Each row shows the three fields, what the line held before the read, and the result.

Printed by the lab on Icarus, one row for each read. An x means: no value yet.
read 1a4: tag=6 index=2 word=1 | line 2 holds valid=0 tag=x | miss | data=c0de0069
read 1a4: tag=6 index=2 word=1 | line 2 holds valid=1 tag=6 | hit  | data=c0de0069
read 1ac: tag=6 index=2 word=3 | line 2 holds valid=1 tag=6 | hit  | data=c0de006b
read 2a8: tag=a index=2 word=2 | line 2 holds valid=1 tag=6 | miss | data=c0de00aa
read 1a0: tag=6 index=2 word=0 | line 2 holds valid=1 tag=a | miss | data=c0de0068
reads=5 hits=2 misses=3 cycles=65

Rules 1 to 3 at work: every row splits its address as the hand table did. Address 1a4 gives tag 6, index 2, word 1.

Rule 4 at work: the first row. Line 2 has valid=0, so the read is a miss, whatever the tag says. In the fourth row the line is valid, but it holds tag 6 and the address has tag a. That is a miss too.

Rule 5 at work: the third row. Address 1ac is in the same block as 1a4, so it is a hit, and the word select 3 picks c0de006b.

The last row counts 2 hits and 3 misses. At 1 cycle for a look and 20 more for a miss, that is 65 cycles for five reads.

This lab leaves out the clock. A real cache does all of this as a circuit, in one cycle. It then fetches a missing block over the bus, in many more. The lab also leaves out writes. Both come in the next lessons of this module.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
One line for each block, picked by the indexA block may use any lineOne compare, and no search. Two blocks with the same index push each other out.
Fields cut at powers of twoA cache of 5 lines, or blocks of 3 wordsEach field is a few wires of the address. Other sizes would need a divider.
The tag stored in the lineThe whole address storedFewer bits in each line. The index and the offset are known from the place.
Reset clears only the valid bitsReset clears tags and blocks tooA few flip-flops with a reset, not thousands. Junk stays in the lines, behind a valid bit of 0.

The Trap: The Hit on Junk

Back to the chip from the top of the page. Its designer saved a little logic: the valid bits had no reset. In simulation they started as unknown, and the tests happened to write every line before reading it. On silicon, each bit powers up as 0 or 1 by chance. Here is the lab with no reset, and with junk in every line:

The same lab with no reset. The bench first sets every line to valid=1, tag 6 and the word deadbeef, as an example of junk.
read 1a4: tag=6 index=2 word=1 | line 2 holds valid=1 tag=6 | hit  | data=deadbeef
read 2a8: tag=a index=2 word=2 | line 2 holds valid=1 tag=6 | miss | data=c0de00aa
reads=2 hits=1 misses=1 cycles=22

The first row is the failure. Line 2 says it is valid, and its junk tag happens to be 6. The read of 1a4 is a hit, and it returns deadbeef. The memory holds c0de0069 at that address. Rule 6 was broken, and rule 4 did the rest.

The second row is a miss, so it fetches the block and returns the right word. That is what makes this bug so hard to find: most reads are right, and the wrong ones change from one power-up to the next.

At Scale

This page4 lines of 16 bytes
A real level-1 cachehundreds of lines of 64 bytes

This section looks ahead. A real cache has the same three fields, with larger numbers.

  1. Real widths

    Take addresses of 32 bits, 64 lines and blocks of 16 bytes. The offset has 4 bits, the index has 6 and the tag has 22.

  2. Tags and blocks in SRAM

    The tags and the blocks sit in memory macros. A macro cannot clear all its words at one edge. So the valid bits are often flip-flops.

  3. More than one line for a block

    The fight over one line is eased by giving each index two or four lines. That is the next lesson of this module.

  4. A shared level

    Large chips stack several caches: The Cache Hierarchy of a Core Cluster.

Try It

Run the lab. Check each row against the hand table.

Add read(10'h0f4) at the end. Predict its tag, index and word, and whether it hits.

Change the fourth read to 10'h2e8. Predict which rows change, and why.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: read 1a4: tag=6 index=2 word=1 | line 2 holds valid=0 tag=x | miss | data=c0de0069 read 1a4: tag=6 index=2 word=1 | line 2 holds valid=1 tag=6 | hit | data=c0de0069 read 1ac: tag=6 index=2 word=3 | line 2 holds valid=1 tag=6 | hit | data=c0de006b read 2a8: tag=a index=2 word=2 | line 2 holds valid=1 tag=6 | miss | data=c0de00aa read 1a0: tag=6 index=2 word=0 | line 2 holds valid=1 tag=a | miss | data=c0de0068 reads=5 hits=2 misses=3 cycles=65

Check Yourself

Predict the outputThis run makes four reads: 040, 044, 140 and 040 again. All four use line 0. What does the last row say?
// A direct-mapped read cache in front of a slow memory. Addresses have 10 bits and count bytes.
module tb;
  // This cache has 4 lines. Each line holds a block of 4 words, and a word is 4 bytes. So an address splits as
  //   bits 9 to 6: tag  |  bits 5 and 4: index  |  bits 3 to 0: offset (bits 3 and 2 pick the word)
  localparam LINES = 4, WORDS = 4;
  localparam T_HIT = 1, T_MISS = 20;      // cycles for a look in the cache, and for a miss on top: choices of this lab

  logic        valid [0:LINES-1];
  logic [3:0]  tags  [0:LINES-1];
  logic [31:0] block [0:LINES-1][0:WORDS-1];
  int reads = 0, hits = 0, misses = 0, cycles = 0;

  // The slow memory has 256 words. Word n holds c0de0000 + n, so every answer can be checked by eye.
  function automatic logic [31:0] memory(input logic [7:0] word_no);
    return 32'hc0de_0000 + word_no;
  endfunction

  task automatic reset_cache;             // a reset clears the valid bits, and nothing else
    for (int i = 0; i < LINES; i++) valid[i] = 0;
  endtask

  task automatic read(input logic [9:0] address);
    logic [3:0] tag;
    logic [1:0] index, word;
    logic       hit;
    tag = address[9:6];  index = address[5:4];  word = address[3:2];
    $write("read %03h: tag=%h index=%0d word=%0d | line %0d holds valid=%b tag=%h | ", address, tag, index, word, index, valid[index], tags[index]);
    hit = valid[index] && (tags[index] == tag);          // one line is looked at, and no other
    reads++;
    if (hit) begin
      hits++;  cycles += T_HIT;
    end else begin
      misses++;  cycles += T_HIT + T_MISS;
      for (int w = 0; w < WORDS; w++) block[index][w] = memory({tag, index, 2'(w)});   // the whole block comes in
      tags[index] = tag;  valid[index] = 1;
    end
    $display("%s | data=%08h", hit ? "hit " : "miss", block[index][word]);
  endtask

  initial begin
    reset_cache;
    read(10'h040);
    read(10'h044);
    read(10'h140);
    read(10'h040);
    $display("reads=%0d hits=%0d misses=%0d cycles=%0d", reads, hits, misses, cycles);
  end
endmodule
ConceptInto which three fields does a cache split an address, from the high bits to the low bits?
ConceptAddresses have 32 bits. A cache has 128 lines and blocks of 32 bytes. How wide are offset, index and tag?
ConceptA line has valid=1 and tag 6. A read arrives with this index and tag a. What is it?
ConceptWhat picks one word out of the block of a line?
ConceptWhat must a reset clear in a cache?

Common Mistakes

Wrong Take the index from the lowest bits of the address.

The lowest bits are the offset inside the block.

Fix Cut the offset off first. The index is the next field.

Wrong Call a read a hit when the tags are equal.

A line that was never filled holds a junk tag.

Fix Require the valid bit as well.

Wrong Leave the valid bits without a reset.

They power up as 0 or 1 by chance, and a junk line can hit.

Fix Clear every valid bit at reset. The tags and blocks need none.

Remember

  • An address splits into tag, index and offset. The sizes of the cache fix the three widths.
  • The index picks one line. A hit needs its valid bit at 1 and an equal tag.
  • The upper bits of the offset pick one word of the block, beside the tag compare.
  • A reset clears the valid bits, and nothing else.

Practice

Report a problem with this page