Sub topic 7
Direct-mapped cache
This chapter assumes you can read binary and hexadecimal, know that cache sizes are powers of two, and have seen the idea of a cache block from the earlier chapters. A direct-mapped cache is the simplest way to decide where a memory block may live in the cache. Each block has exactly one possible line. That makes lookup cheap and fast. It also causes a specific kind of misses, which GATE questions test often.
The mapping rule
Main memory is divided into blocks of B bytes. The cache holds L lines, and each line stores one block. In a direct-mapped cache, memory block number n can be placed only in line:
line = n mod LMany blocks share a line. Blocks 0, L, 2L, and so on all compete for line 0. The cache tells them apart with a tag, which is the part of the block number that the modulo discards. The tag is n div L.
Because B and L are powers of two, the modulo and division are free. They are just bit slices of the address.
Address fields
For a byte address, the fields are, from low bits to high bits:
| Field | Width | Purpose |
|---|---|---|
| Offset | log2(B) bits | Selects the byte inside the block |
| Index | log2(L) bits | Selects the cache line |
| Tag | remaining bits | Identifies which block currently occupies the line |
The helper below does this split. It is the same code used in the address-breakdown chapter. In a direct-mapped cache, the number of sets equals the number of lines.
def split_address(addr, block_size, num_sets):
# block_size and num_sets must be powers of two
offset_bits = block_size.bit_length() - 1
index_bits = num_sets.bit_length() - 1
offset = addr & (block_size - 1)
index = (addr >> offset_bits) & (num_sets - 1)
tag = addr >> (offset_bits + index_bits)
return tag, index, offset
# 16-byte blocks, 16 lines
print(split_address(0x1234, 16, 16)) # (18, 3, 4), i.e. tag=0x12, index=3, offset=4Read the result as follows. The low 4 bits (0x4) pick the byte. The next 4 bits (0x3) pick line 3. The remaining bits (0x12) are stored as the tag.
How a lookup works
Every line holds three things: a valid bit, a tag, and the data block. The valid bit is 0 after reset, so a line that was never filled cannot give a false hit on a tag that happens to be 0.
A lookup does the following:
- Use the index bits to select one line. No searching is involved.
- Compare the line's stored tag with the address tag. Check that the valid bit is 1.
- If both conditions hold, it is a hit. Use the offset to pick the requested byte or word from the line.
- Otherwise it is a miss. Fetch the block from the next level, store it in that line with its new tag, set the valid bit, and replace whatever was there.
Hardware needs only one tag comparator and no replacement logic. There is no choice of victim, because the incoming block can go to only one line. That is why direct-mapped caches have the shortest hit time among the mapping schemes.
Tracing a reference string
Take a cache with 8 lines of 16 bytes each, which is 128 bytes of data. The trace is 0x0000, 0x0004, 0x0008, 0x0080, 0x0000, 0x0084. The simulator below prints the block number, index, tag, and the outcome of every access.
def simulate_direct(addresses, block_size, num_lines):
lines = [None] * num_lines # each entry holds a tag, or None if invalid
hits = 0
for a in addresses:
block = a // block_size
index = block % num_lines
tag = block // num_lines
hit = lines[index] == tag
if hit:
hits += 1
else:
lines[index] = tag
print(f"{a:#06x} block={block} index={index} tag={tag} {'hit' if hit else 'miss'}")
return hits, len(addresses) - hits
trace = [0x0000, 0x0004, 0x0008, 0x0080, 0x0000, 0x0084]
print(simulate_direct(trace, 16, 8))The output is:
0x0000 block=0 index=0 tag=0 miss
0x0004 block=0 index=0 tag=0 hit
0x0008 block=0 index=0 tag=0 hit
0x0080 block=8 index=0 tag=1 miss
0x0000 block=0 index=0 tag=0 miss
0x0084 block=8 index=0 tag=1 miss
(2, 4)Notice three things:
- The first access is a miss because the cache is empty. The next two hit because they fall in the same 16-byte block. That is spatial locality at work.
- Blocks 0 and 8 both map to line 0, because 8 mod 8 = 0. Each evicts the other.
- The last two accesses miss even though the cache is mostly empty. Lines 1 to 7 were never used.
Conflict misses
A conflict miss happens when a block was evicted only because another block maps to the same line, while other lines are unused or could have held both. The trace above shows it. Alternating between addresses 0x0000 and 0x0080 in a loop gives a hit ratio of zero. This is called thrashing.
A fully associative cache of the same size would keep both blocks and hit on every repeat. Direct mapping trades a higher miss ratio in some access patterns for a simpler, faster lookup. Set-associative caches sit between the two. They are covered in later chapters.
The spine's simulator for this comparison runs with block size 1 byte and uses the trace [0, 4, 0, 4] on a 4-line direct-mapped cache. Addresses 0 and 4 both map to line 0, so the result is 0 hits and 4 misses. A 2-way cache with 2 sets keeps both addresses and gets 2 hits.
Which pairs of addresses conflict? Two addresses conflict when their index bits are equal and their tag bits differ. Equivalently, their block numbers differ by a nonzero multiple of L, so their addresses differ by a multiple of the cache size (L × B), but not within the same block.
Storage cost
The cache stores more than data. For each line it also stores a tag and a valid bit. Consider a 32 KB direct-mapped cache with 64-byte blocks and 32-bit addresses:
- Number of lines: 32 KB / 64 B = 512 = 2^9.
- Offset bits: log2(64) = 6.
- Index bits: log2(512) = 9.
- Tag bits: 32 − 9 − 6 = 17.
- Bits per line for metadata: 17 tag bits + 1 valid bit = 18.
- Metadata total: 512 × 18 = 9216 bits.
- Data total: 32 KB × 8 = 262144 bits. Total storage is 271360 bits.
If the cache uses write-back, each line also needs a dirty bit. That detail is covered in the write-policy chapter. Questions often ask only for the number of tag bits or the tag-array size, so read what is asked.
Edge cases and common errors
- Using block size where the cache size is meant. Lines = cache size / block size. Do not divide the address space by the cache size to get the index width.
- Word addresses versus byte addresses. If memory is word-addressable, the offset counts words, not bytes. Check what the question says about the addressing unit and the block size unit.
- Forgetting the valid bit. A tag of 0 matches an empty line unless validity is checked. Cold-start misses occur even when the tag field is zero.
- Physical versus virtual address. GATE problems say "physical address" for cache indexing. The index and tag come from that address, not from a virtual one.
- Single-line cache. With
L = 1, the index has zero bits and the tag is the entire block number. This behaves like a fully associative cache with one entry. - Cache as large as memory. If
Lis at least the number of memory blocks, the tag has zero bits and no conflicts exist.
Exercise
A direct-mapped cache has 64 KB of data, 32-byte blocks, and a 32-bit physical address. How many lines does it have, and how many bits are in the offset, index, and tag?
Show answer
Lines = 64 KB / 32 B = 2048 = 2^11. Offset = log2(32) = 5 bits. Index = 11 bits. Tag = 32 − 11 − 5 = 16 bits.
Exercise
The cache has 8 lines of 16 bytes. Do the addresses 0x0130 and 0x01B0 map to the same line? If so, does accessing them alternately cause conflict misses?
Show answer
0x0130 = 304, so block 19, index 19 mod 8 = 3, tag 2. 0x01B0 = 432, so block 27, index 27 mod 8 = 3, tag 3. They share index 3 but have different tags, so each access evicts the other. Alternating accesses miss every time.