Ek direct-mapped cache sabse simple cache organization hai jahan main memory ka har block exactly ek cache line par map hota hai. Cache location memory address se determine hoti hai using:
Cache Index=(Block Address)mod(Number of Cache Lines)
Problem: Multiple memory blocks usi cache line par map hote hain (jaise blocks 0, 2c, 2⋅2c, ... sab line 0 par map hote hain). Hum kaise jaanein ki abhi kaun sa block store hai?
Solution: Tag store karo (remaining high-order bits). Tag un blocks ko distinguish karta hai jo same index par collide karte hain.
Formula:Tag=⌊2b+cAddress⌋
Kyun kaam karta hai: Agar do addresses ka index same hai lekin tags alag hain, toh wo different blocks hain jo ek hi cache line ke liye compete kar rahe hain. Tag ek "full identifier" ki tarah kaam karta hai jab hum ek cache line par aa jaate hain.
1. Extract index bits → cache line L dhundho
2. Valid bit check karo: agar V=0, toh MISS hai (cold miss)
3. Stored tag ko address se compare karo:
- Match? → HIT, offset use karke data extract karo
- No match? → MISS (conflict miss), evict karo aur naya block fetch karo
Socho tumhare paas ek chhoti bookshelf hai jisme 4 shelves hain, lekin ek badi library hai 100 books ki. Sab fit nahi ho sakti, toh ek rule banate ho: "Book number decide karta hai shelf."
Books 0, 4, 8, 12, ... shelf 0 par jaati hain
Books 1, 5, 9, 13, ... shelf 1 par jaati hain
Books 2, 6, 10, 14, ... shelf 2 par jaati hain
Books 3, 7, 11, 15, ... shelf 3 par jaati hain
Shelf number index ki tarah hai (book number mod 4). Lekin jab aap shelf 2 par aate ho, toh wahan ek book dikhai deti hai—kya ye book 2 hai, 6 hai, 10 hai, ya 14? Aap book ki spine par label (tag) check karte ho. Agar wo match karta hai jo aap dhundh rahe ho, bahut accha! Agar nahi, toh books swap karo (purani nikalo, nayi laao).
Problem? Agar aap baar baar book 2 maangne lago, phir 10, phir book 2, phir book 10... dono shelf 2 ke liye ladte hain. Har baar jab ek chahiye, doosri wahan hoti hai, toh swap karna padta hai. Yahi hai conflict miss—space khatam nahi hua, balki tumhara simple rule (book number mod 4) unhe ek shelf share karwa deta hai.
Cache Performance Metrics - Hit rate, miss rate, AMAT directly-mapped caches ke conflict misses par heavily depend karte hain
Set-Associative Caches - Conflict problem solve karte hain multiple blocks per index allow karke (direct-mapped 1-way set-associative hai)
Memory Addressing - Address decomposition (tag/index/offset) spatial aur temporal locality exploitation ki foundation hai
Cache Replacement Policies - Direct-mapped mein koi choice nahi hoti (forced eviction), lekin ye associative caches mein LRU/FIFO se connect hota hai
Cache Write Policies - Write-through vs. write-back direct-mapped caches par apply hoti hain conflicts ke saath additional complexity ke saath
#flashcards/hardware
Direct-mapped cache mein memory address ke teen components kaun se hain? :: Tag (identify karta hai kaun sa memory block), Index (cache line select karta hai), Block Offset (block ke andar byte select karta hai)
Cache index ke liye modulo arithmetic kyun use karte hain?
Infinite memory addresses ko finite cache lines par map karne ke liye wrapping se: blocks 0, C, 2C, ... sab line 0 par map hote hain
Direct-mapped cache mein conflict miss kya hota hai?
Jab do alag memory blocks same cache line par map hote hain (same index, alag tags), repeated evictions cause karte hain chahe baaki lines empty hon
Direct-mapped cache mein cache index ka formula :: Index = (Address / 2^b) mod 2^c, jahan b = block offset bits, c = index bits
Cache access pe agar valid bit = 0 ho toh kya hota hai?
Cold miss (compulsory miss) - line kabhi fill nahi hui; memory se fetch karna padega
Har cache line mein tag kyun store karte hain?
Multiple memory blocks same index par map hote hain; tag distinguish karta hai ki abhi kaun sa block us line mein store hai
Agar cache mein 256 lines hain aur 16-byte blocks hain, toh kitne index bits?
8 bits (log₂(256) = 8) index ke liye, 4 bits (log₂(16) = 4) offset ke liye
Direct-mapped cache mein thrashing kya hai?
Same index lekin alag tags wale addresses par repeated access constant evictions aur reloads cause karta hai, 0% hit rate ke saath
Calculate karo tag bits: 32-bit address, 512 lines, 64-byte blocks :: Tag = 32 - log₂(512) - log₂(64) = 32 - 9 - 6 = 17 bits
Direct-mapped cache fully associative se faster kyun hai?
Sirf ek comparison chahiye (indexed line par tag check karo) vs. sab lines ke tags parallel ya sequentially compare karna