CPU Architecture and the Memory Hierarchy
IB Computer Science · Computer Organization
Inside the processor. The central processing unit contains a control unit that fetches and decodes instructions and issues control signals, an arithmetic and logic unit that performs calculations and comparisons, and a small set of very fast registers. Four registers matter for examination answers: the program counter holds the address of the next instruction; the memory address register holds the address currently being accessed; the memory data register holds the data or instruction just read from or about to be written to memory; and the accumulator holds the result of the most recent operation.
The buses. The address bus carries an address from the CPU to memory and is unidirectional, because memory never originates an address. Its width determines the maximum number of addressable locations: an n-bit address bus can address 2 to the power n locations. The data bus is bidirectional, because data is both read and written, and its width determines how much data moves per transfer. The control bus carries signals such as read, write and interrupt requests.
The machine instruction cycle. Fetch: the program counter's value is copied to the memory address register; the address travels on the address bus; the control unit asserts a read signal; the contents return on the data bus into the memory data register; the program counter is incremented. Decode: the control unit separates the instruction into an opcode saying what to do and an operand saying where the data is. Execute: the arithmetic and logic unit performs the operation, or data is transferred, with the result typically left in the accumulator.
The memory hierarchy. Memory technologies trade speed against cost and capacity, so machines use several levels. Registers are fastest and tiny. Cache is small, fast and expensive per byte; it works because of the locality of reference — programs tend to reuse the same data soon (temporal locality) and to use data stored nearby (spatial locality), so keeping recently used blocks close to the processor pays off. Primary memory (RAM) is larger, slower and volatile: contents are lost when power is removed. ROM is non-volatile and holds the bootstrap program that starts the machine, which is why a computer can start at all when RAM is empty. Secondary storage is non-volatile, far larger and far slower, and is not directly addressable by the CPU: data must be loaded into primary memory before it can be processed. This last point is the reason a program with insufficient RAM slows down dramatically rather than simply failing.
Worked example: Why adding a cache is much faster than you might predict
A system has a cache access time of 2 ns and a main memory access time of 60 ns. The cache hit rate is 95%. (a) Calculate the average access time. (b) Calculate the average time if the hit rate falls to 80%. (c) Explain why locality of access makes a high hit rate normal rather than lucky.
Solution
(a) Hit rate of 95%. The average time equals the hit rate times the cache time plus the miss rate times the main memory time. Substituting: 0.95 times 2 plus 0.05 times 60 equals 1.9 plus 3.0 equals 4.9 ns. Compared with 60 ns without a cache, the system is about 12 times faster.
(b) Hit rate of 80%. 0.8 times 2 plus 0.2 times 60 equals 1.6 plus 12.0 equals 13.6 ns. This result is worth noting: the hit rate fell by only 15 percentage points yet the average time almost tripled. The reason is that the contribution of the misses is multiplied by a very large factor, so it is the miss rate that dominates rather than the hit rate. This is also why processor designers monitor the miss rate rather than the hit rate.
(c) Why a high hit rate is normal. If a program accessed memory randomly, a small cache would be almost useless. But real programs do not access randomly, for two reasons that have their own names.
Temporal locality: a memory cell that has just been used is very likely to be used again soon afterward. A loop counter variable is read and written on every iteration; a total variable is accumulated thousands of times; the very code in the loop body is fetched again on every pass.
Spatial locality: if one cell is used, the neighboring cells are very likely to be used right after. Traversing an array goes through consecutive elements in memory; the instructions of a program lie next to one another so after executing the instruction at address 100, the one at 101 is almost certainly executed next. Because the cache loads a whole block rather than one byte at a time, one miss is followed by several hits in a row.
The consequence to state in an answer. Because the mechanism relies on locality, programs that break locality will have a high miss rate however large the cache is — for example traversing a matrix by column in a row-major language, or following a linked list whose nodes are scattered through memory. This is why contiguous data structures such as arrays are often faster than linked lists in practice, even when the theoretical analysis gives the same complexity.
Worked example: Deducing the maximum memory capacity from the bus width
A processor has a 20-bit address bus and a 16-bit data bus, and each memory cell holds 1 byte. (a) Calculate the maximum number of memory cells and the total addressable capacity. (b) If the address bus is widened to 32 bits, how does the capacity change? (c) What does widening the data bus affect?
Solution
(a) With a 20-bit address bus. Each line of the bus carries one bit, so the total number of distinct address combinations is 2 to the power of 20 equals 1,048,576. Since each cell holds 1 byte, the maximum addressable capacity is 1,048,576 bytes, that is, 1 MiB.
(b) Widening to 32 bits. The number of cells becomes 2 to the power of 32 equals 4,294,967,296, that is, 4 GiB. The point to recognize is that the relationship is exponential: adding 12 wires makes the capacity increase by 2 to the power of 12 equals 4,096 times. This is also the standard explanation of why 32-bit systems cannot use more than 4 GiB of main memory without additional techniques, and why moving to a 64-bit architecture removes that limit entirely.
(c) What the data bus affects. The data bus does NOT affect the maximum memory capacity — this is the point most often confused. It determines how much data can be transferred in one transfer. With a 16-bit data bus, the machine reads 2 bytes per cycle; widening to 32 bits, it reads 4 bytes per cycle, so at the same clock speed, memory bandwidth doubles.
How to tell them apart so as not to confuse them in the exam room. The address bus answers the question how many places are there to refer to; the data bus answers the question how much is fetched each time. An analogy: the address bus is like the maximum number of house numbers a street can have, while the data bus is like the amount of cargo carried per trip of the truck.
Common mistake
Trap 1 — naming the three steps of the instruction cycle without saying which registers the data passes through. Marks are awarded for the path: program counter to address register, out on the address bus, the read signal, data back to the data register, counter incremented by one.Trap 2 — saying the data bus determines the maximum memory capacity. That is the ADDRESS bus, with capacity equal to 2 to the power of its width. The data bus determines the amount of data transferred each time.
Trap 3 — saying the cache is faster so it should completely replace main memory. The cache is far more expensive per byte, so it must be small; its value comes from locality of access, not from capacity.