Course contentsShow
← Computer Science
Lesson 1128 of 2,87226. Memory Hierarchy and CachingFree lesson

Direct-Mapped Cache Lookup

Master the process of checking cache hits and misses in direct-mapped caches using tag comparison.

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

  1. Extract the index: The CPU uses the index bits to go directly to one specific cache line
  2. Check the valid bit: Is there actually data in this line? If the valid bit is 0, it's automatically a cache miss
  3. 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.