Cache Memory, Mapping Techniques, and Associative Memory (CAM)
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:
(e.g., loop counter
i in for(i=0; i<1000; i++))(e.g., array elements
arr[0], arr[1], arr[2])💡 Real-Life Indian Student Analogy:
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!
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:
Formula:
Line = Block mod Total LinesZero conflicts, but needs expensive search hardware.
The perfect middle ground used in all modern CPUs!
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!”
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.
💡 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.