Skip to content

Cache Mapping

  • Each main memory block maps to exactly one fixed cache line.
  • CacheΒ Line=(BlockΒ Number)β€Šmodβ€Š(NumberΒ ofΒ CacheΒ Lines)\text{Cache Line} = (\text{Block Number}) \bmod (\text{Number of Cache Lines})
  • Cache is divided into sets; a block maps to one set but can occupy any line within that set.
  • SetΒ Number=(BlockΒ Number)β€Šmodβ€Š(NumberΒ ofΒ Sets)\text{Set Number} = (\text{Block Number}) \bmod (\text{Number of Sets})
  • Any main memory block can be placed in any cache line.

Comparisons:

PropertyDirect mappedFully associativek-way set associative
No. of sets= no. of lines1lines / k
Ways per set1= no. of linesk
Index fieldCache line bitsNoneSet bits
Tag sizeSmallestLargestMiddle
Hardware costCheapestMost expensiveModerate
Conflict missesHighestNoneReduced
Search on lookupDirect (1 compare)All linesk compares
Replacement policyNot neededRequiredRequired

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)
  • 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 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Formulas :

  • Offset Bits : Represent block size (words/bytes inside a block)
    • Offset Bits = logβ‚‚(Block Size)
  • Tag Bits : Represents number of main memory blocks competing for one cache line
    • Tag Bits = logβ‚‚(Memory Size / Cache Size) or logβ‚‚(No. of Main Memory Blocks / No. of Cache Blocks)
  • 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)
  • Address Bits : Represent addressable memory size
    • Main Memory Address Bits = logβ‚‚(Main Memory Size)
    • Cache Memory Address Bits = logβ‚‚(Cache Size)

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 block
Main Memory Block Number
= [ TAG | INDEX ]
INDEX β†’ selects the cache line
TAG β†’ identifies which memory block is in that line
OFFSET β†’ selects data inside the block

Main 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)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Tag β”‚ Set index β”‚ Byte Offset β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key relationship

No. of sets = No. of cache lines / k

Where 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

FieldFormula
Offset bitslogβ‚‚(block size)
Set index bitslogβ‚‚(no. of sets) = logβ‚‚(cache lines / k)
Tag bitsMM addr bits βˆ’ set bits βˆ’ offset bits

How placement works

  1. Use set index bits β†’ find the set.
  2. Search all k lines in that set for a tag match.
  3. 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 setsEquivalent to
1= no. of cache linesDirect mapped
= no. of cache lines1Fully associative
2, 4, 8 …cache lines / kk-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.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Tag β”‚ Byte Offset β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

No index/line field at all. The entire main memory block number becomes the tag.

Formulas

FieldFormula
Offset bitslogβ‚‚(block size)
Tag bitslogβ‚‚(no. of MM blocks) = MM addr bits βˆ’ offset bits
Index bits0 β€” there is no index

Mental model

No. of sets = 1 (entire cache is one big set)
Ways per set = no. of cache lines
Tag = entire MM block number

Any 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 n comparators for n cache 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).