目录 ← 首页
CS61C

Direct Mapped Cache

Learning Outcomes

  • Using address terminology, describe how to find a block in a direct-mapped cache: tag, index, and offset.
  • For a given pattern of memory accesses to a direct-mapped cache, identify if each memory access is a cache hit or cache miss.
  • Contrast direct-mapped caches with fully associative caches.

In an earlier section, we explained why hardware costs make fully associative caches rather uncommon in modern processors. We now introduce the other end of the spectrum policy: a direct mapped cache. With this new cache, we consider again the cache design policies and walk through an example. 见 #sec-cache-design-policy

Placement Policy

Identification

Consider our visualization for a 16B, direct-mapped cache with 4B blocks in #fig-dm-valid.

Cold direct-mapped cache table with valid and dirty bits and empty data contents.

A cold snapshot of a 16B direct-mapped cache with 4B blocks and a dirty bit for write-back.

On the surface, the direct mapped cache looks very similar to that of our fully associative cache. We discuss how the direct mapped placement policy shortens the tag width and impacts the identification procedure to determine a cache hit.

Tag, Index, and Offset

The mapping of pretty much all direct-mapped caches is simple:

(Block address) modulo (number of blocks in cache)\text{(Block address) modulo (number of blocks in cache)}

Like before direct-mapped caches copy in data from memory at the granularity of blocks. We can then translate from byte address to block addresses.

As an example, we can connect the direct-mapped cache in #fig-dm-valid to the 12-bit memory address in #fig-dm-address.

Direct-mapped address decomposition into fields: tag at bits 11 through 4, index at bits 3 through 2, and block-offset at bits 1 through 0.

For a direct-mapped cache, the memory address is split into three fields: the tag, the index, and the offset. For the cache in #fig-dm-valid, a 12-bit memory address is split into an 8-bit tag, a 2-bit index, and a 2-bit offset.

  • In a direct-mapped cache, the index is used to select the block.
  • In direct-mapped caches, the tag is the upper bits of the address, excluding the bits for the index and the offset. The tag is used to check the cache block.
  • As with all caches, the offset is the portion of the address needed to describe the byte offset within a block. These are always the lowest bits of the memory byte address.
  • The block address is the tag concatenated with the index.

Replacement Policy

Write Policy

Walkthrough

The following animation traces through four memory accesses to a 12-bit address space on our 16B direct-mapped cache with 4B blocks. Assume a write-back policy. Assume the cache starts out cold, like in #fig-dm-valid.

Warming up a direct-mapped cache.

Contrast this direct-mapped cache walkthrough with the one for fully associative caches:

  • Identification of a cache hit occurs by checking exactly one tag: the tag at the indexed cache entry.
  • Memory accesses 2 and 3 create cache entries in cache entries 3 and 2, respectively; these cache entries share the same tag. However, the blocks in these entries have different block addresses.
  • Memory access 4 still incurred a block replacement (and a cache write-back) even though the cache was not filled. The cache entry at index 3 was occupied by a block with a different tag.

Direct Mapped: Hardware and Performance

Implementing a direct-mapped cache in hardware is much simpler than the fully associative cache.

  • Because the block can only be in one location, on a cache hit we need just one comparator to check the tag in the target line (and one mux to get said tag). We no longer need one comparator per cache block/entry.
  • Because the index is now used to select the location of the block in the cache, it is not encoded into the bits of the tag. This reduces the width of each tag, thereby reducing the overhead per cache entry.

Hardware block diagram of a direct-mapped cache. A 32-bit address is broken into tag, index, and offset. Arrows connect the three fields of the address to where they are used in the memory space diagram to depict index selection, tag check, and data output path.

Hardware implementation of a direct-mapped cache.