Direct-Mapped Cache Lookup
What You'll Learn
You'll learn how the CPU determines whether requested data is in the cache (a hit) or must be fetched from main memory (a miss).
The Lookup Process
When the CPU needs data, it must quickly decide: "Is this already in my cache?" In a direct-mapped cache, each memory address maps to exactly one cache line, making this check straightforward.
Remember from the previous lesson that every address splits into three parts: tag, index, and offset. Here's how the lookup works:
Step-by-Step Lookup
- Extract the index: The CPU uses the index bits to go directly to one specific cache line
- Check the valid bit: Is there actually data in this line? If the valid bit is 0, it's automatically a cache miss
- Compare tags: If the line is valid, compare the tag stored in that cache line with the tag from the requested address
- Match? → Cache hit! The data is there; use the offset to find the exact byte(s)
- No match? → Cache miss — different data occupies this line
Analogy: The Library Shelf
Imagine a library where each book title tells you exactly which shelf to check. You walk to that shelf (index), see if there's a book there (valid bit), then check if the book's title matches what you want (tag comparison). If it matches, you open to the right page (offset). If not, you must request it from storage.
Why This Is Fast
The beauty of direct-mapped caches is that all three steps happen in parallel hardware circuits — the comparison is just matching bit patterns, which takes nanoseconds.
Key Takeaway: Direct-mapped cache lookup uses the index to find one cache line, then compares the stored tag with the address tag; matching tags plus a valid bit of 1 means a cache hit, otherwise it's a miss requiring memory access.