CSC213 ยท 2081 ยท 5 marks
What is meant by cache mapping? Explain working of direct mapping with suitable block diagram.
What is meant by cache mapping? Explain working of direct mapping with suitable block diagram. [5]
Quick answer
Cache mapping refers to the technique or procedure by which the contents of main memory blocks are mapped (assigned) to cache memory locations. Since cache memory is much smaller than main memory, a mapping function is needed to determine which cache location a particular main memory block will occupy when it is brought into cache.
Cache Mapping
Definition
Direct Mapping
Concept
In direct mapping, each block of main memory can be placed in one and only one specific location in the cache. Main memory is divided into pages that correspond in size with the cache. Each memory address is divided into three fields:
| Field | Purpose |
|---|---|
| Tag | Identifies which page/block of main memory is currently in cache |
| Index (Line/Block number) | Specifies which cache line the block maps to |
| Word (Block offset) | Identifies the specific word within the block |
Address Division
For a memory address of k bits:
| TAG | INDEX (Cache Line) | WORD (Offset) |
| t | d | w |
- Number of cache lines = 2^d
- Number of words per block = 2^w
- Tag bits = k - d - w
The cache line number is determined by:
Cache Line = (Main Memory Block Number) mod (Number of Cache Lines)
Block Diagram of Direct Mapping
Main Memory Address
+--------+----------+--------+
| TAG | INDEX | WORD |
+--------+----------+--------+
| | |
| | |
| +----v----+ |
| | Cache | |
| | Index | |
| | (Select | |
| | Line) | |
| +----+----+ |
| | |
| +----v-----------------------+
| | Cache Line |
| | +-------++--------------+ |
| | | Valid || TAG | DATA | |
| | +-------++--------------+ |
| +----+-----------+----------+
| | |
+--v--+ Compare Block
| TAG |----+ Data
+-----+ | |
v |
+------+ |
| Hit? | |
+--+---+ |
| |
YES | NO |
| (Fetch |
| from v
| Main +------+
| Memory)| |
v | WORD |
+------+ | MUX |
| Data |<------+------+
| Out |
+------+
Working Steps
- The CPU generates a memory address which is split into Tag, Index, and Word fields.
- The Index field is used to directly select a specific cache line.
- The Tag stored in that cache line is compared with the Tag from the CPU address.
- If the Valid bit = 1 and Tags match โ Cache HIT: the required word is read from the cache using the Word field.
- If the Tags do not match or Valid bit = 0 โ Cache MISS: the block is fetched from main memory, loaded into the indicated cache line, and the tag is updated.
Example
Suppose main memory has 32 blocks and cache has 8 lines:
- Block 0 maps to Cache Line 0 (0 mod 8 = 0)
- Block 1 maps to Cache Line 1 (1 mod 8 = 1)
- Block 9 maps to Cache Line 1 (9 mod 8 = 1)
- Block 17 maps to Cache Line 1 (17 mod 8 = 1)
Note: Blocks 1, 9, and 17 all compete for the same cache line, which is the main disadvantage of direct mapping.
Advantages and Disadvantages
| Advantages | Disadvantages |
|---|---|
| Simple and fast to implement | Two blocks with the same index cannot reside in cache simultaneously |
| Low hardware cost | High miss rate if frequently used blocks share the same cache line |
| Straightforward tag comparison | Inflexible placement of blocks |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.