Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4: Cache Mapping, Policies & AMAT

Lesson 12 of 17 in the free Logical Organization of Computer-II notes on Siksha Sarovar, written by Rohit Jangra.

2.1 The Mapping Problem

Cache holds a few thousand lines (blocks); main memory holds millions. Mapping answers: where may a given memory block live in the cache, and how do we recognise it there? Every scheme splits the physical address into fields — mastering that split solves 90% of cache numericals.

Running example: 64 KB byte-addressable main memory (16-bit addresses), 2 KB cache, 16-byte blocks. Cache lines = 2048 / 16 = 128 lines. Offset bits = log₂16 = 4.

2.2 Direct Mapping

Each memory block can live in exactly one line: line = (block number) mod 128.

Address (16 bits) = | TAG 5 | INDEX (line) 7 | OFFSET 4 |
index bits = log2(128) = 7,  tag = 16 - 7 - 4 = 5

Address 0x2A47 = 0010 1010 0100 0111
TAG    = 00101      (0x05)
INDEX  = 010 0100   (line 36)
OFFSET = 0111       (byte 7 within the block)

Lookup: go straight to line 36, compare its stored tag with 00101; equal → hit, else → miss and refill. Why it's cheap: one comparator, no search. Why it hurts: two hot blocks with the same index (e.g., addresses 0x2A47 and 0x0A47) evict each other forever — conflict misses even when the cache is mostly empty.

2.3 Fully Associative Mapping

A block may live in any line: address = | TAG 12 | OFFSET 4 |. Lookup compares the tag against all 128 lines simultaneously, needing content-addressable memory (one comparator per line). Zero conflict misses, maximal hardware cost — practical only for tiny structures (e.g., TLBs).

2.4 Set-Associative Mapping (the Compromise)

Group lines into sets; a block maps to exactly one set but may occupy any of the k lines within it (k-way). For 2-way: sets = 128 / 2 = 64 → set bits = 6.

Address = | TAG 6 | SET 6 | OFFSET 4 |
0x2A47 = 0010 1010 0100 0111
TAG = 001010 (10), SET = 100100 (36), OFFSET = 0111

Only k comparators per lookup, and two conflicting blocks can now coexist in one set. Real CPUs use 4–16 way L1/L2 caches.

CriterionDirectk-way Set-AssocFully Assoc
Placement freedom1 linek linesanywhere
Comparators1kone per line
Conflict missesworstlownone
Cost/speedcheapest, fastestbalancedcostliest

2.5 Replacement and Write Policies

On a miss in a full set, evict which line? LRU (least recently used — best locality match, needs usage bits), FIFO (oldest in — simpler, ignores reuse), Random (surprisingly decent, trivial hardware). Example: 2-way set, accesses A, B, A, C → LRU evicts B (A was touched more recently); FIFO evicts A (loaded first).

PolicyWrite hit actionWrite miss (typical pairing)Trade-off
Write-throughwrite cache and memoryno-write-allocatememory always consistent; heavy bus traffic (add a write buffer)
Write-backwrite cache only, set dirty bit; write memory on evictionwrite-allocateminimal traffic; memory temporarily stale — DMA/multicore must beware

2.6 Cache Performance: AMAT

AMAT = hit time + miss rate x miss penalty
Example: tc = 2 ns, h = 0.9, penalty = 50 ns
AMAT = 2 + 0.1 x 50 = 7 ns

Two-level: L1 2 ns, L1 miss 10%; L2 10 ns, L2 miss 20%; memory 100 ns
AMAT = 2 + 0.1 x (10 + 0.2 x 100) = 2 + 0.1 x 30 = 5 ns

Misses come in three flavours — compulsory (first touch), capacity (working set exceeds cache), conflict (mapping collisions; the kind associativity removes). Classifying a miss stream is a rising exam favourite.

🎯 Exam Focus

  1. A 4 KB cache with 32-byte lines serves a 1 MB byte-addressable memory. Give the tag/index/offset widths for direct mapping, and for 4-way set-associative mapping.
  2. For direct mapping in Section 2.2, which line and tag does address 0x1B6C use? Show the binary split.
  3. Compare direct, associative and set-associative mapping on placement, comparators and conflict misses.
  4. Differentiate write-through and write-back caches; what is a dirty bit and why does DMA complicate write-back?
  5. Cache hit time 4 ns, miss penalty 80 ns: what hit ratio keeps AMAT below 10 ns? (Solve for h.)
  6. Explain compulsory, capacity and conflict misses with one scenario each.