Cache Schemes: Direct-Mapped Cache

The CPU does the following check on a given RAM address:

  1. The CPU extracts the block bits from the RAM address.
  2. Using these block bits, the CPU accesses the cache block whose number is given by the block bits.
  3. Each block in cache stores a tag number. The CPU compares the tag of the RAM address with the tag of the cache block:
    • If both tags are the same, it means that the variable is inside cache: hurray! We have a cache hit scenario, so the CPU can access it by plugging the offset number into cache.
    • If the tags are different, this means that a different RAM block is currently in cache (not the one that we are looking for.) We have a cache miss scenario, so we have to access main memory and copy the entire wanted RAM block into cache so that the variable we are looking for is successfully put in cache.

The reason we copy entire RAM blocks into cache rather than individual variables is because of the locality principle: if we use a certain variable, chances are that we'll need to use this variable again (so we'd better put it in cache.) Moreover, chances are that we'll also need to use variables located nearby the current variable (in the same block,) so we copy the entire block of data.