Loading lessons...
Memory Hierarchy, Main & Auxiliary Memory, Associative Memory & Cache Basics
Memory Hierarchy, Main & Auxiliary Memory, Associative Memory & Cache Basics
No single memory is fast, large and cheap at once. A computer therefore uses a hierarchy of memories: a little very fast memory near the CPU and a lot of slow, cheap memory further away. This lesson covers the hierarchy, how main memory chips are connected to the CPU, disks, associative (content-addressable) memory, and the idea of a cache.
1. The Memory Hierarchy
Memory hierarchy
| Level | Typical access time | Typical size | Managed by |
|---|---|---|---|
| CPU registers | under 1 ns | tens of words | compiler |
| Cache memory | 1 to 10 ns | KB to MB | hardware |
| Main memory | 50 to 100 ns | GB | operating system |
| Auxiliary memory (disk, SSD) | µs to ms | hundreds of GB to TB | operating system |
| Backup (tape, optical) | seconds | TB | operator / OS |
- Main memory holds the programs and data the CPU is using now; the CPU addresses it directly.
- Auxiliary memory (disks, tapes) holds everything else; it is reached through I/O, and data must be brought into main memory before use.
- The cache is a small, fast memory between the CPU and main memory that holds the most-used parts of main memory.
The hierarchy works because of locality of reference: in a short time a program uses only a small part of its code and data. Temporal locality: a word just used will likely be used again soon (loops). Spatial locality: words near it will likely be used soon (arrays, sequential code).
2. Main Memory
Main memory is built from RAM chips (read-write) and ROM chips (read-only; holds the bootstrap loader that starts the computer). RAM is static (SRAM: flip-flops, fast, used for caches) or dynamic (DRAM: capacitors, dense and cheap, needs refreshing, used for main memory).
A chip has a chip select input, so many chips can share one bus. The designer draws a memory address map showing which addresses each chip uses.
A computer needs 512 bytes of RAM and 512 bytes of ROM. RAM chips are 128 × 8 and the ROM chip is 512 × 8. Draw the address map and give the address lines.
- RAM chips needed = 512 / 128 = 4. Each needs 7 address lines (27 = 128).
- ROM needs 9 address lines (29 = 512).
- Total memory 1024 bytes needs 10 lines. Line 10 picks RAM (0) or ROM (1); lines 9 and 8 pick one of the four RAM chips through a 2 × 4 decoder.
| Component | Hex address | Lines 10 9 8 | Lines 7..1 |
|---|---|---|---|
| RAM 1 | 0000 to 007F | 0 0 0 | x x x x x x x |
| RAM 2 | 0080 to 00FF | 0 0 1 | x x x x x x x |
| RAM 3 | 0100 to 017F | 0 1 0 | x x x x x x x |
| RAM 4 | 0180 to 01FF | 0 1 1 | x x x x x x x |
| ROM | 0200 to 03FF | 1 x x | x x x x x x x |
Memory connection to the CPU
The decoder below implements the map: address line 10 selects the ROM, lines 9 and 8 pick a RAM chip, and the rest address a byte inside the chip:
// Memory address map (Mano): four 128 x 8 RAM chips at 0000-01FF and one
// 512 x 8 ROM at 0200-03FF, decoded from a 10-bit address.
module memory_decoder (
input [9:0] addr, // address lines 10..1 are addr[9:0]
output [3:0] ram_cs, // chip select of RAM 1..4
output rom_cs,
output [6:0] ram_addr, // address inside a RAM chip
output [8:0] rom_addr // address inside the ROM
);
wire rom = addr[9]; // line 10: 0 = RAM, 1 = ROM
// lines 9 and 8 go to a 2 x 4 decoder enabled by line 10 = 0
assign ram_cs = rom ? 4'b0000 : (4'b0001 << addr[8:7]);
assign rom_cs = rom;
assign ram_addr = addr[6:0];
assign rom_addr = addr[8:0];
endmodule
module tb;
reg [9:0] addr;
wire [3:0] ram_cs;
wire rom_cs;
wire [6:0] ram_addr;
wire [8:0] rom_addr;
memory_decoder u (addr, ram_cs, rom_cs, ram_addr, rom_addr);
integer i;
reg [9:0] tests [0:5];
initial begin
tests[0] = 10'h000; tests[1] = 10'h07F; tests[2] = 10'h080;
tests[3] = 10'h1A5; tests[4] = 10'h200; tests[5] = 10'h3FF;
$display("address chip location in chip");
for (i = 0; i < 6; i = i + 1) begin
addr = tests[i];
#1;
if (rom_cs)
$display(" %h ROM %0d", addr, rom_addr);
else
$display(" %h RAM %0d %0d", addr,
ram_cs[0] ? 1 : ram_cs[1] ? 2 : ram_cs[2] ? 3 : 4, ram_addr);
end
$finish;
end
endmoduleAddress 1A5H = 01 1010 0101: line 10 is 0, lines 9 and 8 are 11 (RAM 4), and the low 7 bits are 37:
address chip location in chip
000 RAM 1 0
07f RAM 1 127
080 RAM 2 0
1a5 RAM 4 37
200 ROM 0
3ff ROM 511How many 128 × 8 RAM chips are needed for 2048 bytes? How many address lines go to each chip and to the decoder?
- Chips = 2048 / 128 = 16.
- Total address lines = log2 2048 = 11. Each chip uses 7.
- Decoder inputs = 11 − 7 = 4, so a 4 × 16 decoder selects the chip.
3. Auxiliary Memory
Magnetic disks are platters coated with magnetic material. Each surface is divided into concentric tracks, each track into sectors; the same track on all surfaces forms a cylinder. A read/write head on an arm moves to the track.
| Term | Meaning |
|---|---|
| Seek time | time to move the head to the right track |
| Rotational latency | time for the sector to rotate under the head (on average half a revolution) |
| Transfer time | time to read the sector as it passes |
| Access time | seek time + rotational latency + transfer time |
A disk has 16 surfaces, 1024 tracks per surface, 256 sectors per track and 512 bytes per sector. Find its capacity.
A disk turns at 7200 rpm with an average seek time of 8 ms and 400 sectors per track. Find the average time to read one sector.
- One revolution = 60 / 7200 s = 8.33 ms.
- Average rotational latency = 8.33 / 2 = 4.17 ms.
- Transfer time of one sector = 8.33 / 400 = 0.021 ms.
- Access time = 8 + 4.17 + 0.021 = 12.19 ms.
Magnetic tape stores data in records along a long strip, separated by gaps. It is read sequentially, so it is slow to reach but cheap; it is used for backup. Optical disks (CD, DVD) and solid-state drives (flash memory, no moving parts) are other auxiliary memories.
4. Associative Memory
Ordinary memory is accessed by address. An associative memory, or content-addressable memory (CAM), is accessed by content: you give part of a word and it finds every stored word that matches, all at once. It is used where a fast search is needed, such as the cache tag store and the TLB (lesson 45).
Block diagram of associative memory
- The argument register A holds the word to search for.
- The key register K is a mask: only bit positions where K = 1 are compared.
- Every word in the array is compared with A in parallel. Bit Mi of the match register is set if word i matches in all unmasked positions.
For bit j of word i, the match is xj = Aj Fij + overline{Aj} · overline{Fij} (equality, the XNOR). With the key bit:
A masked bit (Kj = 0) always counts as a match.
A = 101 111100 and K = 111 000000, so only the three leftmost bits are compared. Which of these words match?
| Word | Value | Leftmost 3 bits | Match? |
|---|---|---|---|
| 1 | 100 111100 | 100 | no |
| 2 | 101 000001 | 101 | yes |
Word 2 matches even though the rest of it differs from A, because those bits are masked.
The CAM below computes Mi for four words with the formula above, using three different keys:
// Associative memory (CAM): every word is compared with the argument
// register A in parallel, only in the bit positions where the key
// register K has 1s. M(i) = 1 for every word that matches.
module cam #(parameter W = 9, parameter N = 4) (
input [W-1:0] a, // argument register
input [W-1:0] k, // key (mask) register
input [W*N-1:0] words, // the N stored words, word 1 lowest
output [N-1:0] m // match register
);
genvar i;
generate
for (i = 0; i < N; i = i + 1) begin : match_logic
wire [W-1:0] w = words[W*i +: W];
// x_j = A_j XNOR F_ij; M_i = AND over j of (x_j + K_j')
assign m[i] = &(~(a ^ w) | ~k);
end
endgenerate
endmodule
module tb;
reg [8:0] a, k;
wire [3:0] m;
reg [8:0] w1, w2, w3, w4;
cam u (a, k, {w4, w3, w2, w1}, m);
task show;
$display("A=%b K=%b -> M4..M1 = %b", a, k, m);
endtask
initial begin
w1 = 9'b100111100;
w2 = 9'b101000001;
w3 = 9'b101111100;
w4 = 9'b011111100;
$display("word1=%b word2=%b word3=%b word4=%b", w1, w2, w3, w4);
a = 9'b101111100;
k = 9'b111000000; #1 show; // compare the first 3 bits only
k = 9'b000111111; #1 show; // compare the last 6 bits only
k = 9'b111111111; #1 show; // compare all bits
$finish;
end
endmoduleWith K on the first three bits, words 2 and 3 match; with K on the last six bits, words 1, 3 and 4 match; with all bits compared, only word 3 (which equals A) matches:
word1=100111100 word2=101000001 word3=101111100 word4=011111100
A=101111100 K=111000000 -> M4..M1 = 0110
A=101111100 K=000111111 -> M4..M1 = 1101
A=101111100 K=111111111 -> M4..M1 = 0100| Random-access memory | Associative memory | |
|---|---|---|
| Accessed by | address | content |
| Search | one word at a time | all words in parallel |
| Cost | low | high: compare logic in every cell |
| Use | main memory | cache tags, TLB, routing tables |
5. Cache Memory: The Idea
The cache keeps copies of recently used main-memory words. When the CPU needs a word, it looks in the cache first:
- Hit: the word is in the cache; it is read at cache speed.
- Miss: the word is not there; it is read from main memory, and a block containing it is copied into the cache for next time.
The hit ratio h is the fraction of accesses that hit: h = hits / (hits + misses).
Average memory access time. With cache time tc and main memory time tm:
- If main memory is accessed only after a miss (hierarchical, Mano's form): tavg = tc + (1 - h) tm.
- If both are started together (simultaneous): tavg = h tc + (1 - h) tm.
The cache takes 100 ns, main memory 1000 ns, and the hit ratio is 0.9. Find the average access time.
- Hierarchical: 100 + 0.1 × 1000 = 200 ns. (A hit costs 100 ns; a miss costs 100 + 1000 = 1100 ns; 0.9 × 100 + 0.1 × 1100 = 200 ns.)
- Simultaneous: 0.9 × 100 + 0.1 × 1000 = 190 ns.
- Without a cache every access takes 1000 ns, so the cache makes memory 5 times faster.
A program makes 3000 memory references, of which 2400 hit in the cache. Find the hit ratio. With tc = 20 ns and tm = 200 ns (hierarchical), what hit ratio gives an average of 30 ns?
- h = 2400 / 3000 = 0.8.
- 30 = 20 + (1 - h) × 200, so 1 - h = 0.05 and h = 0.95.
The ways to decide where a block goes in the cache (mapping), and what to do on writes, are the subject of lesson 44.
Key points to remember
- Hierarchy: registers, cache, main, auxiliary; faster and costlier per bit at the top.
- Locality of reference (temporal and spatial) makes caches work.
- Chips needed = memory size / chip size; decoder inputs = total address lines − lines per chip.
- Disk access time = seek + average rotational latency (half a revolution) + transfer.
- CAM: argument A, key K, match bits Mi = ∏ (xj + Kj').
- tavg = tc + (1 - h) tm (hierarchical) or h tc + (1 - h) tm (simultaneous).
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Explain the memory hierarchy. Why is it needed? (5 Marks)
- Q2. A computer needs 512 bytes of RAM (128 × 8 chips) and 512 bytes of ROM (512 × 8). Draw the memory address map and the connection to the CPU. (10 Marks)
- Q3. What is associative memory? Draw its block diagram and derive the match logic. (10 Marks)
- Write: A, K, M registers, xj, Mi = ∏ (xj + Kj'), and an example search.
- Q4. Define hit ratio. Cache 100 ns, main memory 1000 ns, hit ratio 0.9: find the average access time. (5 Marks)
- Q5. Define seek time, rotational latency and transfer time. Find the access time of a 7200 rpm disk with 8 ms seek. (5 Marks)