A 512K × 8 memory is to be built from 64K × 1 RAM chips. How many chips are needed and how are they arranged? How many address lines does the memory need, and how is one row of chips selected?
Memory organization is how a computer combines small fast memories with large slow ones. It covers the memory hierarchy, SRAM, DRAM and ROM chips, building memory from chips, interleaving, cache mapping, replacement and write policies, and keeping multiprocessor caches coherent.
A modern processor can use a new operand every fraction of a nanosecond, but no memory technology is fast, huge, cheap and permanent all at once. Fast static RAM costs far more per bit than a disk, and a hard disk is millions of times slower than static RAM. Computers therefore arrange several kinds of memory in a memory hierarchy and keep copies of the data in current use in the small, fast levels close to the CPU.
| Level | Typical size | Typical access time | Technology | Managed by |
|---|---|---|---|---|
| CPU registers | Tens to hundreds of bytes | Under 1 ns (one clock) | Flip-flops inside the CPU | Compiler / programmer |
| L1 cache | 32–64 KB per core | About 1 ns | SRAM on the CPU chip | Hardware |
| L2 and L3 cache | 256 KB to tens of MB | About 3–20 ns | SRAM | Hardware |
| Main memory | 4–64 GB | About 50–100 ns | DRAM | Operating system |
| Secondary storage | Hundreds of GB to several TB | About 0.1 ms (SSD), 5–10 ms (hard disk) | Flash, magnetic disk | OS file system |
| Archival (tertiary) | Very large, removable | Seconds to minutes | Magnetic tape, optical disc | Backup software / operator |
Going down the hierarchy, four things change together: capacity rises, cost per bit falls, access time grows, and the CPU uses that level less often. Registers, caches and main memory are volatile (they forget without power); flash, disks and tape are non-volatile. In the usual inclusive design each level holds a subset of the level below it, so a slower copy always exists to fall back on.
Three measures describe any memory. Access time (latency) is the delay from presenting an address to receiving the data. Cycle time is the minimum gap between two successive accesses; for DRAM it is longer than the access time because the cells must be restored after every read. Bandwidth (transfer rate) is the number of bytes moved per second. Memories also differ in access method: random access (RAM and ROM — any location in the same time), sequential access (tape), direct access (disk — jump near the data, then wait for it to rotate past) and associative access (search by content, as in a CAM).
The hierarchy works only because programs are predictable. The principle of locality of reference says that in any short interval a program touches only a small part of its address space. Temporal locality: an item used now is likely to be used again soon — loop variables, the instructions of a loop body, the top of the stack. Spatial locality: items close to a recently used address are likely to be used soon — the next instruction, the next element of an array. Caches exploit temporal locality by keeping recently used data, and spatial locality by fetching a whole block of neighbouring words on every miss.
RAM (random-access memory) is read–write memory in which any word can be reached in the same time, and it is volatile. ROM is randomly accessible too, so the real contrast is read–write and volatile (RAM) versus read-mostly and non-volatile (ROM). RAM is built with two very different circuits, and the difference decides where each is used.
A DRAM chip is a grid of rows and columns. To save pins, the row address and then the column address are sent over the same pins and latched by RAS̅ (row address strobe) and CAS̅ (column address strobe). This address multiplexing halves the number of address pins. Opening a row copies the whole row into sense amplifiers and writes it back — which is exactly how refresh works: the controller simply opens every row in turn.
A chip with 8192 rows that must all be refreshed within 64 ms needs one row refresh about every 7.8 µs (distributed refresh); refreshing all rows in one go is burst refresh. SDRAM works in step with the bus clock, and DDR SDRAM transfers data on both clock edges, doubling the data rate. The one-transistor DRAM cell was invented by Robert Dennard at IBM (patented 1968); the first commercial DRAM chip was the Intel 1103 (1970).
| Type | Programmed by | Erased by | Typical use |
|---|---|---|---|
| Mask ROM | The manufacturer, using a photomask during fabrication | Cannot be erased | Large volumes of fixed code or tables |
| PROM (one-time programmable) | The user, once, by blowing fuses (or antifuses) in a PROM programmer | Cannot be erased | Small production runs, fixed look-up tables |
| EPROM (UV-EPROM) | The user, electrically, by trapping charge on floating gates | Ultraviolet light through a quartz window — whole chip, about 10–20 minutes, out of the circuit | Firmware development (27xx series) |
| EEPROM (E²PROM) | Electrically, in the circuit | Electrically, byte by byte, in the circuit | Configuration and calibration data, smart cards |
| Flash memory | Electrically, in the circuit | Electrically, a whole block (sector) at a time — fast | BIOS/firmware, USB drives, memory cards, SSDs |
Flash, invented by Fujio Masuoka at Toshiba in the early 1980s, is EEPROM that erases large blocks at once, which makes it dense and cheap. NOR flash allows random byte reads, so code can run directly from it; NAND flash is denser and is read in pages, which suits SSDs and memory cards. The EPROM itself was invented by Dov Frohman at Intel in 1971.
Memory chips and modules are described as words × bits per word. A 64K × 8 chip stores 65,536 words of 8 bits (512 Kbit = 64 KB); it needs log₂(64K) = 16 address lines and 8 data lines, plus control inputs such as chip select (CS̅), output enable and write enable. In memory sizes K = 2¹⁰ = 1024, M = 2²⁰ and G = 2³⁰, so 128K bytes means 131,072 bytes.
Two kinds of expansion are combined. In width (bit) expansion, chips sit side by side, share every address line and the same chip select, and each supplies a slice of the data word — two 1K × 4 chips make 1K × 8. In depth (word) expansion, rows of chips share the data lines, and the extra high-order address bits go to a decoder whose outputs drive the chip-select inputs, so exactly one row answers — four 1K × 8 chips with a 2-to-4 decoder make 4K × 8. Unselected chips hold their tri-state outputs in the high-impedance state, so they do not fight over the data bus.
| Chip | A15 A14 | Address range |
|---|---|---|
| Chip 0 | 0 0 | 0000H – 3FFFH |
| Chip 1 | 0 1 | 4000H – 7FFFH |
| Chip 2 | 1 0 | 8000H – BFFFH |
| Chip 3 | 1 1 | C000H – FFFFH |
Full (absolute) decoding uses every high-order address bit, so each location answers to exactly one address. Partial (linear) decoding leaves some address bits out to save logic; the chips then also respond at alias (mirror or fold-back) addresses. That is harmless if the aliases are never used, but it wastes address space and makes later expansion awkward.
A DRAM bank needs tens of nanoseconds for each access and some recovery time before the next one, so reading consecutive words from a single bank is slow. Memory interleaving splits main memory into m independent modules (banks), each with its own address and data registers, so several accesses can be in progress at once.
A 16- or 32-bit word occupies several byte addresses, so the bytes need an agreed order. Big-endian stores the most significant byte at the lowest address (‘big end first’). Little-endian stores the least significant byte at the lowest address, so lower addresses hold less significant bytes. Intel 8086 and all x86 processors are little-endian; Motorola 68000, IBM mainframes and network byte order (TCP/IP headers) are big-endian; ARM, MIPS and PowerPC can be set either way (bi-endian). The names come from Gulliver's Travels, by way of Danny Cohen's 1980 note ‘On Holy Wars and a Plea for Peace’.
| Address | Big-endian | Little-endian |
|---|---|---|
| 1000 | 12 | 78 |
| 1001 | 34 | 56 |
| 1002 | 56 | 34 |
| 1003 | 78 | 12 |
A cache is a small, fast SRAM memory between the CPU and main memory that holds copies of recently used blocks (also called lines) of main memory, typically 32–128 bytes each. On every access the cache controller checks, in hardware and invisibly to the program, whether the addressed block is present. If it is, that is a hit and the word comes from the cache. If not, it is a miss: the block is copied from main memory into the cache, replacing an older one, and the word is passed to the CPU.
Besides the data, every line stores a tag that says which memory block it holds, plus status bits: a valid bit and, in write-back caches, a dirty bit. Maurice Wilkes described such ‘slave memories’ in 1965, and the IBM System/360 Model 85 (1968) was the first commercial computer with a cache.
The hierarchical and simultaneous formulas differ only in what a miss costs. If the cache is checked first and memory is started only after a miss, a miss costs Tc + Tm; if both start in parallel, a miss costs only Tm. For example, Tc = 10 ns, Tm = 100 ns and h = 0.9 give 10 + 0.1 × 100 = 20 ns hierarchically and 0.9 × 10 + 0.1 × 100 = 19 ns simultaneously — about five times faster than memory alone. When a whole block must arrive before the word is delivered, Tm is replaced by the miss penalty, the time to transfer the block.
| Miss type | Cause | Reduced by |
|---|---|---|
| Compulsory (cold start) | First reference to a block — it happens even in an infinitely large cache | Larger blocks, prefetching |
| Capacity | The program's working set is larger than the cache | A larger cache |
| Conflict (collision) | Too many blocks map to the same line or set while space is free elsewhere (direct-mapped and set-associative caches only) | Higher associativity, a victim cache |
| Coherence (multiprocessors) | Another processor's write invalidated the line | Less write-sharing; avoiding false sharing |
Modern processors use two or three levels. A small L1 is split into an instruction cache and a data cache so both can be used in the same cycle (a modified Harvard arrangement), a larger unified L2 catches most L1 misses, and a big L3 is shared by all cores. L1 is designed for a short hit time, the lower levels for a high hit ratio. The local miss rate of L2 is its misses divided by the accesses that reach it; the global miss rate is its misses divided by all CPU accesses, MR₁ × MR₂. With HT₁ = 1 ns, MR₁ = 0.1, HT₂ = 10 ns, MR₂ = 0.2 and MP₂ = 100 ns, AMAT = 1 + 0.1 × (10 + 0.2 × 100) = 4 ns.
Main memory has far more blocks than the cache has lines, so a mapping function decides where a block may be placed and how the controller finds it again. Split every address into a block number and a word offset, where the offset has log₂(block size) bits. With L lines in the cache, block number j is placed as follows.
| Mapping | Where block j may go | Address fields | Tag comparisons | Replacement choice |
|---|---|---|---|---|
| Direct | Only line j mod L | Tag | line (index) | offset | 1 | None — the line is fixed |
| Fully associative | Any line | Tag | offset | L, all in parallel (associative memory) | Needed: LRU, FIFO, random… |
| k-way set-associative | Any of the k lines of set j mod S, where S = L ÷ k | Tag | set | offset | k, in parallel | Needed, within the set |
Set-associative mapping is the compromise almost every modern CPU uses, with 2, 4, 8 or 16 ways. Each doubling of associativity halves the number of sets, so one bit moves from the set field into the tag. The tags need storage too: the tag directory holds L × (tag + valid + dirty + any LRU bits) bits. KPSC once asked how many address bits reach a byte inside a cache of 1024 lines × 8 bytes: 10 line bits + 3 offset bits = 13.
When a miss needs a line but every candidate line is occupied, a replacement policy chooses the victim. Direct mapping has no choice to make; associative and set-associative caches use one of these policies:
A write miss needs its own decision. Write-allocate (fetch on write) brings the block into the cache and then writes it, betting that nearby words will be written soon. No-write-allocate (write-around) sends the write straight to memory and leaves the cache untouched.
A write-back cache pays extra on some misses: a dirty victim must be written back before the new block arrives. With an 80 ns block fetch and 30% of victims dirty, the average miss penalty is 80 × 1.3 = 104 ns, so a cache with a 2 ns hit time and a 5% miss rate has AMAT = 2 + 0.05 × 104 = 7.2 ns.
Virtual memory applies the same caching idea one level lower: main memory acts as a cache for the disk. The unit is a page (typically 4 KB) and a miss is a page fault. A fault costs millions of cycles, so it is handled by the operating system, placement is fully associative through the page table, and writes are always write-back. The TLB (translation look-aside buffer) is itself a small, highly associative cache of page-table entries. Paging, page tables and page-replacement algorithms are covered in the Memory management topic of the OS group.
| Feature | Cache | Virtual memory |
|---|---|---|
| Unit moved | Block (line), 32–128 bytes | Page, typically 4 KB |
| A miss is called | Cache miss | Page fault |
| Handled by | Cache-controller hardware | Operating system, helped by the MMU |
| Miss penalty | Tens to hundreds of CPU cycles | Millions of cycles (SSD or disk access) |
| Placement | Direct, set-associative or fully associative | Fully associative, through the page table |
| Write policy | Write-through or write-back | Always write-back (dirty bit in the page-table entry) |
In a shared-memory multiprocessor every core has its own cache, so one memory block can sit in several caches at once. Suppose memory holds X = 5 and processors P1 and P2 both read it; then P1 writes 7 into its write-back cache. When P2 reads X it still gets 5 from its own cache — the copies are incoherent. Write-through would not cure this, because P2's cached copy would still say 5; DMA causes the same problem when a device changes memory behind a cache.
The cache coherence problem is keeping all copies consistent. A memory system is coherent when (1) a processor that reads X after its own write sees that value if nobody else wrote in between, (2) every write eventually becomes visible to all processors (write propagation), and (3) all processors see the writes to one location in the same order (write serialisation). Hardware solutions come in two families.
| State | Meaning | Other copies? | Memory up to date? | On a write by this processor |
|---|---|---|---|---|
| M — Modified | Line is dirty; this cache has the only valid copy | No | No (stale) | Write locally; stay M |
| E — Exclusive | Clean, and no other cache holds it | No | Yes | Write locally; go to M with no bus transaction |
| S — Shared | Clean; other caches may hold it too | Possibly | Yes | Broadcast an invalidation (upgrade); go to M |
| I — Invalid | Holds no valid data | — | — | Write miss: a read-for-ownership invalidates the others; go to M |
On a read miss the requesting cache loads the line in E if no other cache has it, and in S if one does; a cache holding it in E or M drops to S, and an M holder first supplies its dirty data. Any snooped write or read-for-ownership sends the other copies to I. The E state is MESI's gain over plain MSI: private data can be read and then written with no bus traffic at all. AMD's MOESI adds Owned (dirty data shared without writing it back) and Intel's MESIF adds Forward (one sharer is chosen to answer requests). False sharing, where two processors write different words of the same block, makes that block ping-pong between their caches.
Try the problem first, then reveal the working one step at a time.
A 512K × 8 memory is to be built from 64K × 1 RAM chips. How many chips are needed and how are they arranged? How many address lines does the memory need, and how is one row of chips selected?
A cache has an access time of 10 ns and main memory 80 ns per word; the hit ratio is 0.95. (a) Find the average access time if memory is accessed only after a cache miss. (b) Find it if cache and memory are accessed simultaneously. (c) In the hierarchical system, suppose each miss must first bring a 4-word block from memory, one word at a time; find the average access time. (d) What speed-up does (a) give over a system with no cache?
A byte-addressable machine has 16 MB of main memory and a 64 KB cache with 4-byte blocks. Give the tag, index (or set) and offset widths for (a) direct mapping, (b) 2-way and 4-way set-associative mapping and (c) fully associative mapping. Then decode the address 16339CH under direct mapping and find the size of the direct-mapped tag store.
A cache holds 4 one-word blocks. The CPU references main-memory blocks 0, 8, 0, 6, 8 in that order. Count the misses for (a) a direct-mapped cache, (b) a 2-way set-associative cache with LRU replacement and (c) a fully associative cache with LRU replacement.
Four 8K × 8 RAM chips are to fill addresses 0000H–7FFFH of a processor with a 16-bit address bus. (a) Which address lines go to every chip and which to the decoder? (b) Give each chip's address range. (c) Which chip, and which location inside it, answers address 5A3FH? (d) If A15 is left out of the decoding, at what other address does that same location also appear?
A processor has an L1 cache with a hit time of 1 cycle and a miss rate of 5%, an L2 cache with a hit time of 10 cycles and a local miss rate of 40%, and main memory with an access time of 100 cycles. Find (a) the average memory access time, (b) the global miss rate of L2 and (c) the average access time if L2 were removed.
A machine has 32-bit byte addresses and a 256 KB, 8-way set-associative, write-back cache with 64-byte lines; every line has one valid bit and one dirty bit. Find (a) the offset, set and tag widths, (b) the total size of the tag directory and (c) the same quantities if the cache were direct-mapped.
A 64K-word memory is built from 8 modules of 8K words each. (a) With low-order interleaving, which module and which word inside it hold address 1234 (decimal)? (b) Which, with high-order interleaving? (c) A cache fetches the block of words 1232–1239. Which arrangement lets all eight words be read with overlapped accesses, and why?
Three processors P1, P2 and P3 use the MESI snooping protocol, and block X is initially cached nowhere. Give the state of X in each cache after each step, and say what happens on the bus: (1) P1 reads X; (2) P2 reads X; (3) P3 writes X; (4) P1 writes X; (5) P2 reads X.
12 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.