Sub topic 7
Direct-Mapped Cache
Assume you know binary and hexadecimal numbers, powers of two, byte-addressable memory, and how a CPU reads data from memory. This chapter explains how a memory block is placed in a direct-mapped cache and how the CPU finds it again.
Core idea
A cache stores copies of blocks from main memory. A cache line is one location that stores one memory block, its tag, and a valid bit.
In a direct-mapped cache, each memory block has exactly one possible cache line:
\[ \text{cache line} = \text{memory block number} \bmod \text{number of cache lines} \]
This rule makes lookup fast. However, two frequently used blocks can map to the same line and repeatedly evict each other.
Address fields
A physical address is divided into three fields:
- Block offset selects a byte inside the block.
- Index selects the cache line.
- Tag identifies which memory block currently occupies that line.
Suppose:
- The address size is 16 bits.
- The cache has 16 lines.
- Each line holds a 16-byte block.
The block offset needs:
\[ \log_2(16)=4\text{ bits} \]
The index needs:
\[ \log_2(16)=4\text{ bits} \]
The remaining bits form the tag:
\[ 16-4-4=8\text{ tag bits} \]
The address format is therefore:
| Tag | Index | Block offset |
|---|---|---|
| 8 bits | 4 bits | 4 bits |
The index chooses a line. The cache then compares the address tag with the stored tag in that line. A matching valid tag is a hit. Otherwise, the access is a miss.
def split_address(addr, block_size, num_lines):
offset_bits = block_size.bit_length() - 1
index_bits = num_lines.bit_length() - 1
offset = addr & (block_size - 1)
index = (addr >> offset_bits) & (num_lines - 1)
tag = addr >> (offset_bits + index_bits)
return tag, index, offset
print(split_address(0x1234, 16, 16))
# (18, 3, 4): tag=0x12, index=3, offset=4For address 0x1234, the byte offset is 4, the cache-line index is 3, and the tag is 0x12.
Mapping example
Consider a cache with four lines and one-byte blocks. Since the block size is one byte, the offset has zero bits. Memory blocks map as follows:
| Memory block numbers | Cache line |
|---|---|
| 0, 4, 8, 12, ... | 0 |
| 1, 5, 9, 13, ... | 1 |
| 2, 6, 10, 14, ... | 2 |
| 3, 7, 11, 15, ... | 3 |
Blocks 0 and 4 both map to line 0. If the CPU accesses 0 and then 4, block 4 replaces block 0. Accessing 0 again causes another miss.
> callout note > The index does not identify a unique memory block. It only identifies the line where that block may be stored. The tag provides the remaining identity.
Strengths and weakness
Direct mapping has simple hardware and fast lookup because it checks only one line. Its main weakness is a conflict miss: two active blocks may need the same line even when other cache lines are unused.
GATE calculation pattern
For a cache with capacity \(C\), block size \(B\), and address size \(A\):
\[ \text{number of lines}=\frac{C}{B} \]
\[ \text{offset bits}=\log_2 B \]
\[ \text{index bits}=\log_2\left(\frac{C}{B}\right) \]
\[ \text{tag bits}=A-\text{offset bits}-\text{index bits} \]
Always convert capacity and block size to the same unit before applying these formulas.
Exercise
A 32-bit processor has a direct-mapped cache of 1 KiB with 16-byte blocks. How many index bits and tag bits does each address contain?
Show answer
The cache has 1024/16 = 64 lines, so the index has log2(64) = 6 bits. The block offset has log2(16) = 4 bits. The tag has 32 - 6 - 4 = 22 bits.