2079 1

CSC213 ยท 2079 ยท 10 marks

What is cache memory? Explain the mapping process. Differentiate between direct mapping and associate mapping.

What is cache memory? Explain the mapping process. Differentiate between direct mapping and associate mapping.[10]

Computer ArchitectureChapter 9: Memory OrganizationCache Memory

Quick answer

Cache memory is a small, high-speed memory placed between the CPU and main memory (RAM). Its purpose is to store frequently used instructions and data so that the CPU can access them faster, reducing the average time needed to access data from main memory.

Cache Memory and Mapping Process

1. What is Cache Memory? (2 marks)

Key characteristics:

  • Faster than main memory but smaller in size
  • Sits closest to the CPU in the memory hierarchy
  • Transparent to the programmer (managed by hardware)
  • Reduces the speed mismatch between CPU and main memory

2. The Mapping Process (3 marks)

: "The transformation of data from main memory to cache memory is referred to as a mapping process."

When the CPU needs data, it first checks the cache:

  • Cache Hit: Data found in cache -- CPU reads directly (fast)
  • Cache Miss: Data not in cache -- fetched from main memory and a copy is placed in cache

Since cache is much smaller than main memory, a mapping function is needed to determine where a block of main memory will be placed in cache.

The three practical mapping procedures are:

  1. Direct Mapping
  2. Associative Mapping
  3. Set-Associative Mapping

3. Direct Mapping (2 marks)

: "Main memory locations can only be copied into one location in the cache. This is accomplished by dividing main memory into pages that correspond in size with the cache."

How it works:

  • Main memory is divided into pages of the same size as the cache.
  • Each block of main memory maps to exactly one fixed location in the cache.
  • The memory address is divided into three fields:
| TAG | INDEX (Cache Line) | BLOCK OFFSET |
  • Tag: Identifies which page of main memory the block came from
  • Index: Specifies the cache line where the block must be stored
  • Offset: Identifies the specific word within the block

Example:

If cache has 128 lines and main memory has 4096 blocks:

  • Block 0, 128, 256, ... all map to cache line 0
  • Block 1, 129, 257, ... all map to cache line 1

Advantage:

  • Simple and inexpensive to implement

Disadvantage:

  • Two blocks with the same index but different tags cannot reside in cache simultaneously, causing frequent replacements (cache thrashing)

4. Associative Mapping (2 marks)

In associative (fully associative) mapping, any block of main memory can be placed in any line of the cache. There is no fixed mapping.

How it works:

  • The memory address is divided into only two fields:
| TAG | BLOCK OFFSET |
  • The entire tag is stored with each cache entry.
  • When the CPU requests data, all cache tags are searched simultaneously (using associative/content-addressable memory) to find a match.

Advantage:

  • Flexible: Any memory block can go into any cache line
  • Eliminates the thrashing problem of direct mapping
  • Higher hit rate

Disadvantage:

  • Expensive and complex hardware required for simultaneous tag comparison
  • Replacement policy (LRU, FIFO) must be implemented

5. Set-Associative Mapping (1 mark)

: "Set-Associative mapping is an improvement over direct mapping organization. Each word of cache can store two or more words of memory under the same index address."

  • It is a compromise between direct and associative mapping.
  • Cache is divided into sets, each set containing a fixed number of lines (called the set size or k-way).
  • A memory block maps to a specific set (like direct mapping) but can go into any line within that set (like associative mapping).

6. Differentiation: Direct Mapping vs. Associative Mapping (Table) (marks covered above)

FeatureDirect MappingAssociative Mapping
PlacementEach memory block maps to exactly one fixed cache lineA memory block can be placed in any cache line
Address FieldsTag + Index + OffsetTag + Offset (no index)
Search MethodUses index to locate line, then checks tagSearches all tags simultaneously
Hardware CostSimple and inexpensiveComplex and expensive (needs CAM)
FlexibilityLow -- fixed mappingHigh -- fully flexible
Conflict/ThrashingHigh -- same index blocks conflictNone -- no fixed slot restriction
Hit RateLower (due to conflicts)Higher
SpeedFast lookup via indexSlightly slower due to parallel search overhead
Replacement PolicyNot needed (only one possible location)Required (LRU, FIFO, etc.)

Summary

Cache memory bridges the speed gap between the CPU and main memory. The mapping process determines how main memory blocks are placed into cache. Direct mapping is simple but suffers from conflicts since each block has only one possible cache location. Associative mapping is flexible and conflict-free but requires costly hardware. Set-associative mapping is the practical middle ground used in modern processors.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.