Cache Mapping
1. Direct Mapping
Section titled β1. Direct Mappingβ- Each main memory block maps to exactly one fixed cache line.
2. Set Associative Mapping
Section titled β2. Set Associative Mappingβ- Cache is divided into sets; a block maps to one set but can occupy any line within that set.
3. Fully Associative Mapping:
Section titled β3. Fully Associative Mapping:β- Any main memory block can be placed in any cache line.
Comparisons:
| Property | Direct mapped | Fully associative | k-way set associative |
|---|---|---|---|
| No. of sets | = no. of lines | 1 | lines / k |
| Ways per set | 1 | = no. of lines | k |
| Index field | Cache line bits | None | Set bits |
| Tag size | Smallest | Largest | Middle |
| Hardware cost | Cheapest | Most expensive | Moderate |
| Conflict misses | Highest | None | Reduced |
| Search on lookup | Direct (1 compare) | All lines | k compares |
| Replacement policy | Not needed | Required | Required |
Quick Derivation Steps (for exam problems)
Given: Main memory size, Cache size, Block size, k (ways).
- Step 1 -
Offset bits = logβ(block size) - Step 2 -
No. of MM blocks = MM size / block sizeβMM block bits = logβ(no. of MM blocks - Step 3 -
No. of cache lines = Cache size / block size - Step 4 -
No. of sets = cache lines / kβSet bits = logβ(no. of sets)- If
k = 1βset bits = cache line bits(direct mapped) - If
k = all linesβset bits = 0(fully associative)
- If
- Step 5 -
Tag bits = MM addr bits β set bits β offset bits - Verify: Tag + Set + Offset = total MM address bits β
Main Memory Address :ββββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββ Main Memory Block no β Byte Offset βββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββ β¬ β¬ β¬ββββββββββββ¬ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββ Tag β Cache Line β Byte Offset βββββββββββββ΄ββββββββββββββββββββββββββββ΄βββββββββββββββββββββ1. Direct Mapping
Section titled β1. Direct MappingβFormulas :
- Offset Bits : Represent block size (words/bytes inside a block)
- Offset Bits =
logβ(Block Size)
- Offset Bits =
- Tag Bits : Represents number of main memory blocks competing for one cache line
- Tag Bits =
logβ(Memory Size / Cache Size)orlogβ(No. of Main Memory Blocks / No. of Cache Blocks)
- Tag Bits =
- Block No. / Line : Represent No. of Blocks
- Main Memory Block Bits =
logβ(No. of Main Memory Blocks) - Cache Line Bits =
logβ(Number of Cache Blocks)
- Main Memory Block Bits =
- Address Bits : Represent addressable memory size
- Main Memory Address Bits =
logβ(Main Memory Size) - Cache Memory Address Bits =
logβ(Cache Size)
- Main Memory Address Bits =
Mental model
β’ Byte offset for Cache and Main Memory is SAME β Block size is SAME β MM Block Size = CM Block Size = Block Size
β’ Main Memory Block Bits β₯ Cache Line Bits β No. MM Blocks β₯ No. CM Blocks
β’ Main Memory Address Bits β₯ Cache Address Bits β MM Blocks Γ Block Size > CM Blocks Γ Block Size β Total Main Memory Size > Total Cache Size
β’ Tag Bits: β For ONE cache block, how many main memory blocks can map to it β 2^(Tag Bits) = No. of MM blocks per cache blockMain Memory Block Number= [ TAG | INDEX ]
INDEX β selects the cache lineTAG β identifies which memory block is in that lineOFFSET β selects data inside the blockMain Memory Address
ββββββββββββ¬ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββ Tag β Cache Line β Byte Offset βββββββββββββ΄ββββββββββββββββββββββββββββ΄βββββββββββββββββββββ<---------------- Main Memory Address Bits ----------------> β log2(Main Memory Size)
-> Main Memory Address Bits - Offset Bits = logβ(Main Memory Size) - logβ(Block Size) = logβ(Main Memory Size / Block Size) = = logβ(Number of Main Memory Blocks) = Main Memory Block Bits
ββββββββββββ¬ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββ Tag β Cache Line β Byte Offset βββββββββββββ΄ββββββββββββββββββββββββββββ΄βββββββββββββββββββββ<------- Main Memory Block Bits -------> <--- Offset Bits ---> β β logβ(No. of Main Memory Blocks) logβ(Block Size)Cache Memory Address
ββββββββββββ¬ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββ Tag β Cache Line β Byte Offset βββββββββββββ΄ββββββββββββββββββββββββββββ΄βββββββββββββββββββββ <---------- Cache Memory Address Bits -----------> β log2(Cache Memory Size)
-> Cache Memory Address Bits - Offset Bits = logβ(Cache Size) - logβ(Block Size) = logβ(Cache Size / Block Size) = = logβ(Number of Cache Blocks) = Cache Line Bits
ββββββββββββ¬ββββββββββββββββββββββββββββ¬ββββββββββββββββββββββ Tag β Cache Line β Byte Offset βββββββββββββ΄ββββββββββββββββββββββββββββ΄βββββββββββββββββββββ <---- Cache Block Bits ---> <--- Offset Bits ---> β β logβ(No. of Cache Blocks) logβ(Block Size)2. Set Associative Mapping (k-way)
Section titled β2. Set Associative Mapping (k-way)βAddress Format
Section titled βAddress Formatβββββββββββββββββ¬ββββββββββββββββββ¬ββββββββββββββββββββ Tag β Set index β Byte Offset βββββββββββββββββ΄ββββββββββββββββββ΄βββββββββββββββββββKey relationship
No. of sets = No. of cache lines / kWhere k = associativity (ways per set). Each MM block maps to exactly one set (via set index), but can go in any of the k lines within that set.
Formulas
| Field | Formula |
|---|---|
| Offset bits | logβ(block size) |
| Set index bits | logβ(no. of sets) = logβ(cache lines / k) |
| Tag bits | MM addr bits β set bits β offset bits |
How placement works
- Use set index bits β find the set.
- Search all k lines in that set for a tag match.
- If hit β return data. If miss β load block into one of the k lines (use replacement policy).
Special cases β this is the one to memorize
| k (ways) | No. of sets | Equivalent to |
|---|---|---|
| 1 | = no. of cache lines | Direct mapped |
| = no. of cache lines | 1 | Fully associative |
| 2, 4, 8 β¦ | cache lines / k | k-way set associative |
Direct mapped is 1-way set associative. Fully associative is n-way set associative. All three are the same structure β just different values of k.
Key properties :
- Reduces conflict misses vs direct mapped (k blocks can share a set without evicting each other).
- Cheaper hardware than fully associative (only k comparators per set, not n total).
- Replacement policy needed within each set.
- Most commonly used in practice β L1/L2/L3 caches are typically 4-way, 8-way, or 16-way.
3. Fully Associative Mapping
Section titled β3. Fully Associative MappingβAddress Format
Section titled βAddress Formatβββββββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββ Tag β Byte Offset βββββββββββββββββββββββββββββββββββββββ΄βββββββββββββββββββNo index/line field at all. The entire main memory block number becomes the tag.
Formulas
| Field | Formula |
|---|---|
| Offset bits | logβ(block size) |
| Tag bits | logβ(no. of MM blocks) = MM addr bits β offset bits |
| Index bits | 0 β there is no index |
Mental model
No. of sets = 1 (entire cache is one big set)Ways per set = no. of cache linesTag = entire MM block numberAny MM block can be placed in any cache line. On a lookup, all cache lines are searched in parallel using dedicated comparators β one per line.
Key properties
- No conflict misses β blocks never evict each other just because they share an index.
- Most expensive hardware β requires
ncomparators forncache lines. - Replacement policy is critical β since any line can hold any block, you need LRU/FIFO/Random to decide eviction.
- Tag size is the largest of the three types (holds full block number).