Loading lessons...
Cache Mapping (Direct, Associative, Set-Associative), Write Policies & Numericals
Cache Mapping (Direct, Associative, Set-Associative), Write Policies & Numericals
A cache holds only a few of main memory's blocks, so it needs a rule that says where a block may be stored and how to find it again. This rule is the mapping. This lesson works through the three mappings on Mano's example, then on modern byte addresses, and covers writing into the cache and replacing blocks.
Mano's example system: main memory is 32K × 12 (a 15-bit address, written as 5 octal digits), and the cache is 512 × 12 (a 9-bit index).
1. Associative Mapping
The cache is an associative memory (lesson 43) that stores both the address and the data of each word. The CPU's 15-bit address goes into the argument register, and all stored addresses are compared at once.
| Address (octal) | Data (octal) |
|---|---|
| 01000 | 3450 |
| 02777 | 6710 |
| 22345 | 1234 |
- Any word can go in any location: the most flexible mapping.
- But every location needs its own comparator, which makes it expensive.
- When the cache is full, a replacement algorithm picks the word to remove.
2. Direct Mapping
The 15-bit address is split in two: the low 9 bits are the index (the cache location) and the high 6 bits are the tag. Each cache word stores the tag and the data. A memory word can go to only one location, the one its index names.
| Tag (6 bits) | Index (9 bits) |
|---|---|
| 2 octal digits | 3 octal digits |
To read: use the index to pick the cache word, then compare its stored tag with the address's tag. Equal means a hit; otherwise it is a miss, the word is read from memory, and it replaces the old word along with its new tag.
Main memory holds 1220 at 00000 and 5670 at 02000; the cache has tag 00 and data 1220 at index 000. The CPU reads address 02000. What happens?
- Split 02000: tag = 02, index = 000.
- The cache word at index 000 has tag 00 ≠ 02: a miss.
- Memory word 02000 (5670) is read and stored at index 000 with tag 02.
- A later read of 00000 (tag 00, index 000) will now miss.
The weakness: two addresses with the same index but different tags (such as 00000 and 02000) keep replacing each other, even if the rest of the cache is empty. The hit ratio can drop sharply when a program uses such addresses in turn.
Direct mapping with blocks. If the cache stores blocks of 8 words, the 9-bit index is split into a 6-bit block number and a 3-bit word number: Tag 6 | Block 6 | Word 3. The cache has 64 blocks; all 8 words of a block share one tag, and a miss brings the whole block in, which uses spatial locality.
Direct-mapped cache lookup
3. Set-Associative Mapping
Set-associative mapping lets each index hold several tag-data pairs (a set). A word can go into any line of its set. In a 2-way set-associative cache, every index stores two words with their tags:
| Index | Tag | Data | Tag | Data |
|---|---|---|---|---|
| 000 | 01 | 3450 | 02 | 5670 |
| 777 | 02 | 6710 | 00 | 2340 |
Now 01000 and 02000 (both index 000) can be in the cache together. A read compares the tag with all tags in the set at once (an associative search within the set). A k-way cache needs k comparators.
One scale covers all three: direct mapping is 1-way set-associative, and fully associative mapping is one set containing every line.
| Direct | Set-associative (k-way) | Fully associative | |
|---|---|---|---|
| Where a block can go | exactly one line | any of k lines in one set | any line |
| Comparators | 1 | k | one per line |
| Hit ratio | lowest (conflicts) | between | highest |
| Cost and speed | cheapest, fastest | between | most expensive |
4. Address Fields for Byte Addresses
Modern caches store blocks (lines) of bytes. A byte address splits into three fields:
- Offset = log2(block size) bits: the byte inside the block.
- Index (line or set) = log2(number of lines, or of sets) bits.
- Tag = the remaining bits.
Address fields for the three mappings
A 32-bit address, a 64 KB cache and 16-byte blocks. Find the tag, index and offset widths for direct, 4-way set-associative and fully associative mapping.
- Offset = log2 16 = 4 bits. Lines = 64 KB / 16 B = 4096.
- Direct: index = log2 4096 = 12; tag = 32 − 12 − 4 = 16.
- 4-way: sets = 4096 / 4 = 1024; index = 10; tag = 32 − 10 − 4 = 18.
- Fully associative: no index; tag = 32 − 4 = 28.
For the direct-mapped cache in Example 2, each line also has a valid bit. How many bits of tag memory are needed?
- Bits per line = 16 (tag) + 1 (valid) = 17.
- Total = 4096 × 17 = 69632 bits = 8704 bytes = 8.5 KB.
Main memory has 64K words; the cache has 256 lines of 8 words each. (a) Give the fields for direct and 2-way set-associative mapping. (b) Split the address C3A7H for direct mapping.
- Address = log2 65536 = 16 bits. Word = log2 8 = 3 bits.
- Direct: line = log2 256 = 8 bits; tag = 16 − 8 − 3 = 5 bits.
- 2-way: sets = 128, set = 7 bits; tag = 16 − 7 − 3 = 6 bits.
- C3A7H = 1100 0011 1010 0111. Tag = 11000, line = 0111 0100 (74H = 116), word = 111 (7).
5. Tracing Hits and Misses
A cache has 4 lines. The CPU asks for main-memory blocks 0, 4, 0, 4, 8, 0, 1, 4. Find the hits for direct mapping (line = block mod 4), 2-way set-associative (set = block mod 2, LRU) and fully associative (LRU).
| Block | Direct line | Direct | 2-way set | 2-way | Fully |
|---|---|---|---|---|---|
| 0 | 0 | miss | 0 | miss | miss |
| 4 | 0 | miss (replaces 0) | 0 | miss | miss |
| 0 | 0 | miss (replaces 4) | 0 | hit | hit |
| 4 | 0 | miss | 0 | hit | hit |
| 8 | 0 | miss | 0 | miss (replaces 0, LRU) | miss |
| 0 | 0 | miss | 0 | miss (replaces 4) | hit |
| 1 | 1 | miss | 1 | miss | miss |
| 4 | 0 | miss | 0 | miss (replaces 8) | hit |
Blocks 0, 4 and 8 all map to line 0 in the direct cache, so they evict each other every time. The 2-way cache holds two of them, and the fully associative cache holds all three.
The simulator below runs the same string through all three caches. It also splits the address 1234ABCDH as in Example 2:
// A 4-line cache of blocks, simulated three ways for the same reference
// string: direct mapped, 2-way set associative and fully associative
// (both associative ones replace the least recently used block).
module cache_sim #(parameter WAYS = 1) (
input clk,
input rst,
input req,
input [7:0] block, // main-memory block number
output reg hit
);
localparam LINES = 4;
localparam SETS = LINES / WAYS;
reg [7:0] tag [0:LINES-1]; // stores the whole block number
reg valid [0:LINES-1];
reg [7:0] age [0:LINES-1]; // larger = used longer ago
integer i, base, found, victim;
always @(posedge clk)
if (rst) begin
for (i = 0; i < LINES; i = i + 1) begin
valid[i] <= 0;
age[i] <= 0;
end
end else if (req) begin
base = (block % SETS) * WAYS; // first line of the set
found = -1;
for (i = base; i < base + WAYS; i = i + 1)
if (valid[i] && tag[i] == block)
found = i;
hit <= (found >= 0);
if (found < 0) begin // miss: pick a victim
victim = base;
for (i = base; i < base + WAYS; i = i + 1)
if (!valid[i] || (valid[victim] && age[i] > age[victim]))
victim = i;
found = victim;
tag[victim] <= block;
valid[victim] <= 1;
end
for (i = base; i < base + WAYS; i = i + 1)
age[i] <= (i == found) ? 8'd0 : age[i] + 1'b1;
end
endmodule
module tb;
reg clk = 0, rst = 1, req = 0;
reg [7:0] block;
wire h1, h2, h4;
cache_sim #(1) direct (clk, rst, req, block, h1);
cache_sim #(2) twoway (clk, rst, req, block, h2);
cache_sim #(4) fully (clk, rst, req, block, h4);
always #5 clk = ~clk;
reg [7:0] refs [0:7];
integer k, n1, n2, n4;
// address split for a 32-bit address, 64 KB direct-mapped cache,
// 16-byte blocks: tag 16 bits, index 12 bits, offset 4 bits
reg [31:0] a = 32'h1234ABCD;
task result(input h, input pad); // pad: line up the columns
if (!h)
$write("miss");
else if (pad)
$write("hit ");
else
$write("hit");
endtask
initial begin
refs[0] = 0; refs[1] = 4; refs[2] = 0; refs[3] = 4;
refs[4] = 8; refs[5] = 0; refs[6] = 1; refs[7] = 4;
n1 = 0; n2 = 0; n4 = 0;
@(negedge clk) rst = 0;
$display("block direct 2-way fully");
for (k = 0; k < 8; k = k + 1) begin
block = refs[k];
req = 1;
@(negedge clk);
$write(" %0d ", block);
result(h1, 1);
$write(" ");
result(h2, 1);
$write(" ");
result(h4, 0);
$display("");
n1 = n1 + h1; n2 = n2 + h2; n4 = n4 + h4;
end
$display("hits %0d/8 %0d/8 %0d/8", n1, n2, n4);
$display("address %h: tag=%h index=%h offset=%h", a, a[31:16], a[15:4], a[3:0]);
$finish;
end
endmoduleIts columns match the table row for row:
block direct 2-way fully
0 miss miss miss
4 miss miss miss
0 miss hit hit
4 miss hit hit
8 miss miss miss
0 miss miss hit
1 miss miss miss
4 miss miss hit
hits 0/8 2/8 4/8
address 1234abcd: tag=1234 index=abc offset=d6. Writing into the Cache
Reads leave memory unchanged, but a write must keep the cache and main memory consistent. There are two policies:
- Write-through: every write updates main memory and the cache. Memory is always up to date, which matters when a DMA device reads memory. But every write costs a memory access.
- Write-back (copy-back): only the cache is updated, and the line's dirty bit is set. Main memory is updated only when a dirty line is replaced. Fewer memory writes, but memory may be out of date.
On a write miss, a write-allocate cache first brings the block in, then writes it (usual with write-back). A no-write-allocate cache writes only to memory (usual with write-through).
| Write-through | Write-back | |
|---|---|---|
| Memory updated | on every write | when a dirty line is replaced |
| Extra bit per line | none | dirty bit |
| Memory traffic | high | low |
| Memory always valid | yes | no |
A program writes the same variable 100 times while its line stays in the cache, and the line is then replaced. How many main-memory writes does each policy make?
- Write-through: one per write = 100.
- Write-back: none while the line stays; one when the dirty line is replaced = 1.
Cache initialisation. At power-on the cache holds random bits. Each line has a valid bit, cleared at start-up; it is set when a block is loaded. A line with valid = 0 always misses.
7. Replacement Algorithms
With associative or set-associative mapping, a miss in a full set must choose a line to evict:
- Random: pick any line. Simple, and surprisingly good.
- FIFO: evict the line that has been in the cache longest (a round-robin pointer per set).
- LRU (least recently used): evict the line not used for the longest time. Best hit ratio, but needs per-line usage information (for 2-way, a single bit per set).
Direct mapping needs no algorithm: there is only one possible line.
A cache has a 2 ns hit time and a 5% miss rate. A miss loads an 8-word block, and main memory takes 10 ns per word. Find the average access time.
- Miss penalty = 8 × 10 = 80 ns.
- Average = hit time + miss rate × miss penalty = 2 + 0.05 × 80 = 6 ns.
Key points to remember
- Offset = log2(block size); index = log2(lines or sets); tag = rest.
- Direct: one possible line, 1 comparator, conflict misses. Fully associative: any line, a comparator per line. k-way: any of k lines in a set.
- Write-through updates memory every time; write-back uses a dirty bit.
- Valid bit marks a line as holding real data. Replacement: random, FIFO, LRU.
- Average time = hit time + miss rate × miss penalty.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Explain associative, direct and set-associative cache mapping with diagrams. Compare them. (10 Marks)
- Q2. A 32-bit address, 64 KB cache and 16-byte blocks: find the fields for direct, 4-way and fully associative mapping. (5 Marks)
- Q3. For a 4-line cache and blocks 0, 4, 0, 4, 8, 0, 1, 4, find the hits with direct, 2-way LRU and fully associative LRU mapping. (10 Marks)
- Write: the table for each mapping and the hit count.
- Q4. Differentiate between write-through and write-back. What is a dirty bit? (5 Marks)
- Q5. Explain cache replacement algorithms. Why does direct mapping not need one? (5 Marks)