Sub topic 8
Fully Associative Cache
Prerequisites and working model
This chapter assumes you can represent numbers in binary and hexadecimal, calculate powers of two, and distinguish a byte address from a memory block. You should also know that a cache stores recently used copies of data from main memory.
A fully associative cache allows any memory block to occupy any cache line. There is no fixed line selected by index bits. The cache compares the requested block's tag with the tags of all valid lines, usually in parallel.
Address format and mapping
Suppose memory is byte-addressable, the address size is \(m\) bits, and each block contains \(2^b\) bytes. The lowest \(b\) address bits are the block offset. The remaining \(m-b\) bits form the tag.
A fully associative cache has no index field:
| Address field | Number of bits | Purpose |
|---|---|---|
| Tag | \(m-b\) | Identifies the memory block |
| Index | 0 | No fixed cache line is selected |
| Block offset | \(b\) | Selects a byte within the block |
For example, consider a 16-bit address and 16-byte blocks. Since \(16=2^4\), the offset has 4 bits. The tag has \(16-4=12\) bits. Any memory block can be placed in any cache line.
The number of memory blocks is
\[ \frac{2^m}{2^b}=2^{m-b}. \]
If the cache contains \(L\) lines, it can hold \(L\) different blocks at once. On a miss, the cache must choose one line to replace when all lines are occupied.
def fully_associative_lookup(address, block_size, tags):
"""Return the matching line, or None on a miss."""
block_number = address // block_size
for line, stored_block in enumerate(tags):
if stored_block == block_number:
return line
return None
tags = [12, 7, 25, None]
print(fully_associative_lookup(25 * 16 + 3, 16, tags)) # 2
print(fully_associative_lookup(9 * 16, 16, tags)) # NoneWhy use it?
Fully associative mapping eliminates conflict misses caused by a fixed index. Two frequently used blocks can remain in the cache even if their addresses would map to the same line in a direct-mapped cache.
The cost is hardware complexity. Every lookup must compare the requested tag with every line. A large fully associative cache therefore needs many comparators and more control logic. It also needs a replacement policy such as least recently used (LRU), first-in-first-out (FIFO), or random replacement.
| Mapping method | Possible location of a block | Replacement decision |
|---|---|---|
| Direct-mapped | Exactly one line | None |
| Fully associative | Any line | Required on a full miss |
| \(k\)-way set-associative | Any of \(k\) lines in one set | Required within that set |
A fully associative cache with \(L\) lines is equivalent to a one-set, \(L\)-way set-associative cache. This relationship helps in GATE problems: use zero index bits and compare the tag against every line.
Worked reasoning
Assume a cache has 4 lines and a reference string of block numbers:
\[ 0,\ 4,\ 0,\ 4 \]
In a fully associative cache, blocks 0 and 4 can occupy different lines. After the first two compulsory misses, both blocks remain available. The final two references are hits, regardless of which specific lines were selected.
If the cache had only one line, block 4 would replace block 0, and the later reference to block 0 would miss. The mapping is still fully associative, but capacity limits what can remain resident.
Exercise
A 20-bit byte address uses 32-byte blocks in a fully associative cache. How many offset and tag bits are there, and how many index bits are used?
Show answer
A 32-byte block needs log₂(32) = 5 offset bits. The tag has 20 - 5 = 15 bits. A fully associative cache has one set, so it uses 0 index bits.