Cache Memory, Mapping Techniques, and Associative Memory (CAM)
With four direct-mapped cache lines, blocks 1 and 5 both map to line 1 using the block number modulo four. Alternating between them causes repeated replacements. A set-associative cache can offer more than one place within the selected set.
What is Cache Memory? (In Simple Words)
Cache Memory is an extremely fast, small SRAM storage buffer built right inside the CPU chip.
- Its only job is to store copies of the data and instructions the CPU will need next.
- When the CPU finds its data in cache, it answers in 1 to 2 clock ticks instead of waiting 50 to 100 ticks for slow RAM!
1. The Principle of Locality of Reference
Why does a tiny 8 MB cache work so well when your computer has 16,000 MB (16 GB) of RAM?
Because computer software naturally follows the Locality of Reference:
Temporal locality means returning to recently used data. Spatial locality means using nearby addresses soon after one another.
- 1. Temporal Locality (Time) — If you access a variable now, you will likely access it again very soon. (e.g., loop counter i in for(i=0; i<1000; i++) )
- 2. Spatial Locality (Space) — If you access one address, its nearby neighbor addresses will be accessed next. (e.g., array elements arr[0], arr[1], arr[2] )
An everyday example
Think of doing your college homework:
- Temporal Locality: You keep your favorite pen on your notebook because you keep using it every 10 seconds.
- Spatial Locality: You open chapter 3 of your book; once you finish page 45, you naturally read page 46 next!
View diagram · The Two Dimensions of Locality
2. Hit Ratio and Average Access Time Formula
- Cache Hit: The CPU looks for data in Cache and finds it immediately!
- Cache Miss: The data is not in Cache; the CPU must fetch it from slow RAM.
- Hit Ratio (H):
H = (Number of Cache Hits) / (Total Memory Requests)
Average Memory Access Time (T_avg):
T_avg = (H × T_c) + ((1 - H) × T_m)
- Where
T_cis Cache speed (e.g. 2 ns) andT_mis RAM speed (e.g. 50 ns). - If your system has a 95% hit rate (
H = 0.95):
T_avg = (0.95 × 2) + (0.05 × 50) = 1.9 + 2.5 = 4.4 ns
(The computer runs more than 11 times faster thanks to cache!).
3. Cache Mapping Techniques
How does a block from large RAM get placed into the smaller cache? There are 3 mapping methods:
Direct mapping gives a block one possible cache line. Set-associative mapping gives a group of choices and fully associative mapping allows any line.
- 1. Direct Mapping — Each RAM block maps to strictly ONE fixed cache line. Formula: Line = Block mod Total Lines
- 2. Associative Mapping — Any RAM block can be placed in ANY free cache line. Zero conflicts, but needs expensive search hardware.
- 3. Set-Associative Mapping — Cache is divided into sets (e.g., 2-way or 4-way). The perfect middle ground used in all modern CPUs!
View diagram · Cache Mapping Comparison
4. Associative Memory (Content Addressable Memory - CAM)
In normal RAM, you provide an address number to get data: “Give me what is stored at address 500”.
Associative Memory (CAM) works backwards: you search by data content:
“Search the whole memory at once and tell me which row contains the roll number 105!”
Associative memory compares a search value against stored entries in parallel. Match signals identify entries whose selected bits agree.
Hardware Registers:
- Argument Register (A): Holds the specific word or number you are searching for.
- Key / Mask Register (K): Chooses which bits to compare and which bits to ignore.
- Match Register (M): Has 1 bit for every row in memory. If Row 4 matches your search, bit
M[4]becomes 1!
Where is CAM Used?
- TLB (Translation Lookaside Buffer): In Virtual Memory to translate page addresses in 1 nanosecond.
- Internet Routers: To match destination IP addresses at Gigabit network speeds.
View diagram · Associative Memory Hardware Organization
💡 DAVV Exam Preparation Tips
5-Mark Question: “Explain Temporal and Spatial Locality of Reference with examples.”
- Define both clearly:
- Temporal: re-accessing same data recently used.
- Spatial: accessing neighboring memory locations.
- Give the C-programming example:
for(int i = 0; i < N; i++) { sum += A[i]; }
Explain that variablesiandsumshow Temporal locality, while arrayA[i]shows Spatial locality.10-Mark Question: “Explain Direct Mapping, Associative Mapping, and Set-Associative Cache Mapping techniques with block diagrams and address formats.”
- Show the memory address format for all 3: Tag, Index/Set, and Word offset.
- Explain why Direct Mapping is simple but causes conflict misses if two active blocks fight for the same line.
- Explain how Set-Associative mapping (2-way, 4-way) gives the flexibility of associative mapping with the speed of direct mapping.