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.
| Question | |
|---|---|
| 1 | Into which three fields does a cache split an address? |
| 2 | How do the sizes of a cache fix the width of each field? |
| 3 | When exactly is a read a hit? |
| 4 | How does the cache pick one word out of a block? |
| 5 | What 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
| Word | Meaning |
|---|---|
| RTL | Register-transfer level: a circuit described as registers and the logic between them. It is the level at which a designer writes Verilog. |
| Offset | The low bits of an address. They say where a byte sits inside its block. |
| Index | The middle bits of an address. They are the number of the one line that this block may use. |
| Direct-mapped cache | A cache in which every block has exactly one line that it may use. |
| Word select | The upper bits of the offset. They pick one word out of the block. |
| Hit path | The logic from the address to the answer "hit or miss" and to the word. It must fit into one clock cycle. |
| Tag compare | The check whether the tag stored in a line equals the tag of the address. |
| Power of two | A 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
- Processor
- Memory
- Control / FSM
- Bus
- 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.
- 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.
- 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.
- Hit
It is a hit when the valid bit is 1 and the two tags are equal. Anything else is a miss.
- 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.
- 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.
| Field | What it must number | Bits | Which bits of the address |
|---|---|---|---|
| Offset | 16 bytes in a block | 4 | bits 3 to 0; bits 3 and 2 are the word select |
| Index | 4 lines | 2 | bits 5 and 4 |
| Tag | everything else | 10 − 4 − 2 = 4 | bits 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.
| Address | Binary | Tag | Index | Word | Line 2 holds | Result |
|---|---|---|---|---|---|---|
| 0x1a4 | 0110 10 0100 | 6 | 2 | 1 | valid 0 | miss: the block of tag 6 comes in |
| 0x1a4 | 0110 10 0100 | 6 | 2 | 1 | valid 1, tag 6 | hit |
| 0x1ac | 0110 10 1100 | 6 | 2 | 3 | valid 1, tag 6 | hit: another word of the same block |
| 0x2a8 | 1010 10 1000 | a | 2 | 2 | valid 1, tag 6 | miss: the tags differ, and tag a replaces tag 6 |
| 0x1a0 | 0110 10 0000 | 6 | 2 | 0 | valid 1, tag a | miss: its block was pushed out |
The binary column is written as tag, index, offset. Two hits and three misses.
The Rules
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.
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.
Rule 3: The tag is every bit above the index.
The line stores it, beside the block and the valid bit.
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.
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.
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.
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=65Rules 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| One line for each block, picked by the index | A block may use any line | One compare, and no search. Two blocks with the same index push each other out. |
| Fields cut at powers of two | A cache of 5 lines, or blocks of 3 words | Each field is a few wires of the address. Other sizes would need a divider. |
| The tag stored in the line | The whole address stored | Fewer bits in each line. The index and the offset are known from the place. |
| Reset clears only the valid bits | Reset clears tags and blocks too | A 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:
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=22The 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 section looks ahead. A real cache has the same three fields, with larger numbers.
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.
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.
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.
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.
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=65Check Yourself
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.