CPU and Memory Basics


CPU and Memory Basics

Simon
Let's begin with the central processor, or CPU. Whether we're talking about a personal computer, a smartphone, or even a programmable calculator, the CPU is at the heart of the device. Its main job is to execute programs, which means reading and carrying out executable code.

Maria
And execution begins as soon as power is applied. The processor starts consuming data from external memory, much like someone reading a book one word at a time. The CPU reads the program byte by byte, and as long as power remains applied, program execution continues.

Simon
That comparison with a book is useful because memory is also arranged as an ordered sequence. Computer memory is an array of integer values called bytes, and each byte can hold a value from 0 through 255. Since the CPU needs to identify a particular cell, every cell receives a number called its address.

Maria
The numbering starts at zero, which is a small detail with major consequences. So if someone refers to memory cell number 300 in ordinary counting, its address is 299, because address zero identifies the first cell. In other words, an address is the cell's ordinal position in zero-based numbering.

Simon
Memory is similar to a sheet of paper: information can be written and then read later. The two basic types here are read-only memory, or ROM, and random-access memory, or RAM. ROM usually stores pre-programmed code for the CPU to execute, while RAM stores temporary data generated by the CPU or loaded during execution.

Maria
The difference also appears when power is removed. ROM retains its contents when the computer is turned off, whereas RAM is erased whenever power is interrupted. A DIMM stick in a PC or laptop is an example of RAM, and a BIOS chip is an example of ROM.

Simon
BIOS is a small program executed every time the system boots. To see how this works, imagine a very simple device containing just a CPU and a ROM chip. The ROM contains firmware, a piece of software intended to run on that particular device.

Maria
When power is applied, the CPU immediately addresses the ROM and begins looking for code it can execute. To read a particular memory cell, it has to specify that cell's unique address. The communication system used to send those addresses is called the address bus.

Simon
An address bus is basically a group of electrical wires. The CPU uses binary values—ones and zeros—on those wires to encode the address of the desired memory cell. If the CPU wants address 44206, it converts that decimal number into binary and places the resulting bits on the address bus.

Maria
In the example, that address requires 16 bits, so the bus has lines A0 through A15. The least significant bit, or LSB, goes on line A0, while the most significant bit, or MSB, goes on line A15. The numbering of the address lines also starts at zero.

Simon
The address bus is unidirectional because addresses travel from the CPU to external devices, not in the opposite direction. The CPU's address lines are outputs, and the address pins on a ROM chip are inputs. Devices on the motherboard can therefore be connected to the address bus in parallel.

Maria
But an address alone does not tell memory what operation the CPU wants. The CPU might want to read data, or it might want to write data. That is why computers also use a control bus, whose important signals include READ, usually written RD, and WRITE, written WR.

Simon
Both of these signals are active low. That means the CPU requests a read by pulling the READ line low, and requests a write by pulling the WRITE line low. The two signals are mutually exclusive: a single operation cannot be a read and a write at the same time.

Maria
The use of active-low control signals is connected to power-consumption considerations. In our example, the CPU places the binary form of 44206 on the address bus and then pulls READ low. That tells the addressed memory device to provide the contents of that cell.

Simon
To transfer the actual contents, the system needs another group of wires: the data bus. Unlike the address bus, the data bus is bidirectional. Data travels from memory to the CPU during a read, and from the CPU to memory during a write.

Maria
The example computer has an 8-bit data bus. In that system, one memory cell is one byte, or 8 bits. So if the ROM cell at address 44206 contains the value 112, the ROM puts the binary representation of 112 on the data bus, and the CPU switches its data lines to input mode to receive it.

Simon
That value is not necessarily ordinary data. Because the ROM contains firmware written in machine code, the value 112 could be an instruction telling the CPU to perform some task. Such machine-code commands are called instructions, operation codes, or opcodes.

Maria
For example, we can imagine that the opcode tells the CPU to write the value 205 into the memory cell at address 62088. At that point, the CPU would need to carry out a write operation, which requires RAM or another writable device.

Simon
Suppose we add a RAM chip and connect it in parallel to both the address bus and the data bus. That creates a problem: when the CPU performs an operation, how do the ROM and RAM know which one is being addressed? If both respond at once, their outputs could collide on the data bus.

Maria
The solution is address decoding. Here, decoding does not mean encryption or deciphering a secret message. It means using logic gates that recognize a particular pattern of ones and zeros on address lines and produce an output—typically a logic zero—when that pattern appears.

Simon
To make this work, the total address space is divided into fixed ranges. One range is assigned to ROM, another to RAM, much like chapters in a printed book occupy specific pages. Once the computer is designed, those device ranges are fixed, and the CPU selects a device simply by placing an address within that device's range.

Maria
The size of the address space depends on the number of address lines. With 16 address lines, there are 2 to the power of 16 possible combinations, giving 65,536 addressable bytes, or 64 kilobytes. The addresses therefore run from 0 through 65,535.

Simon
In the simple design, ROM occupies the lower half of that space, from address 0 through 32,767, and RAM occupies the upper half, from 32,768 through 65,535. ROM is placed at the beginning because a typical CPU begins execution at address zero after a reset.

Maria
Reset zeroes the program counter, or PC. The program counter is the CPU's address pointer for the next instruction, so after reset it points to address zero. That is why the first firmware instruction must be located there.

Simon
Binary is perfect for the circuitry, but long strings of ones and zeros are difficult for people to read. Programmers therefore use hexadecimal notation, which has sixteen digits: 0 through 9 and A through F. Hexadecimal is essentially a compact way to represent binary.

Maria
Each hexadecimal digit corresponds to four binary digits. Four bits are called a nibble, eight bits are called a byte, and sixteen bits or more are called a word. That means 8-bit data takes two hex digits, while 16-bit data takes four.

Simon
For the 16-bit address space, the ROM range can be written as 0x0000 through 0x7FFF, and the RAM range as 0x8000 through 0xFFFF. These values must be pronounced as hexadecimal digits, because 8000 in hexadecimal is not simply the decimal number eight thousand.

Maria
Hexadecimal has different notations in different programming contexts. The C language uses a prefix such as 0x, while assembly language commonly appends the letter H, as in 8000H. These designations identify the value as hexadecimal.

Simon
The key difference between the two memory ranges is the most significant address bit, A15. Any address below 0x8000 has A15 equal to zero, while an address from 0x8000 upward has A15 equal to one. That single bit can therefore be used to select ROM or RAM.

Maria
Memory chips include a control input called CS, or Chip Select, also known as CE, or Chip Enable. CS is active low. In the simple circuit, ROM is selected when A15 is low, while RAM is selected when A15 is high, so an inverter is used to create the opposite signal for RAM.

Simon
When an integrated circuit is not selected, its outputs enter a high-impedance, or Z-state. You can think of that as the output being effectively disconnected from the bus. These are also called tri-state outputs, and they prevent an inactive chip from interfering with the active chip.

Maria
The memory chips also have an Output Enable, or OE, signal. For ROM, which only sends data, CS and OE have closely related meanings. For RAM, an active OE means that data is being read from the memory.

Simon
The rest of the simple wiring follows the same logic. READ connects to the Output Enable inputs of both chips, while WRITE connects to the Write Enable input of RAM. WRITE is not used with ROM because ROM cannot be written by the CPU.

Maria
Let's walk through the circuit after reset. The program counter is zero, so all address lines are low. The CPU pulls READ low, and because A15 is also low, ROM's chip-enable input becomes active. ROM places the contents of address zero on the data bus.

Simon
RAM is connected to the same buses, but it does not interfere. The inverter gives RAM an inactive chip-enable signal, so its data lines remain in the high-impedance state. Only ROM drives the bus during that read.

Maria
Now imagine the CPU executes an instruction to write 0xAA to address 0xB000. The address 0xB000 is in the RAM range, and 0xAA is 170 in decimal, or 10101010 in binary. The CPU places the address on the address bus and the data value on the data bus.

Simon
Next, the CPU pulls WRITE low. Because the address has A15 high, the inverter makes RAM's chip-enable signal low, activating RAM. RAM then stores 0xAA in address 0xB000. The alternating-bit values 0xAA and 0x55 are often used for memory testing.

Maria
To read the value back, the CPU places 0xB000 on the address bus and pulls READ low. RAM becomes active and puts the contents of that cell onto the data bus, while the CPU switches its data lines to input mode and receives 0xAA.

Simon
From the CPU's perspective, the internal arrangement is invisible. It simply supplies an address and receives or sends data. The individual memory chips respond according to the address range assigned to them.

Maria
Real systems use more complicated decoders. A decoder may use several address lines and additional control signals to produce narrower ranges, and it can select not only memory chips but also input-output devices. The system designer decides which ranges belong to which devices, and firmware programmers must know that layout.

Simon
One important example is display memory, or video RAM. It is ordinary random-access memory, but writing values into it changes what appears on the screen. Typically, the cells correspond to screen positions from left to right and then top to bottom, so lower addresses represent the upper-left area and higher addresses the lower-right area, although exceptions can exist.

Maria
A computer also needs to communicate with the outside world through input-output ports. These are used for devices such as keyboards, mice, and gamepads. The CPU still performs read and write operations, but ports use a separate address space and separate machine-language instructions.

Simon
The address bus itself is shared between memory and ports. To distinguish them, the control bus includes two additional active-low signals: IOREQ, meaning input-output request, and MEMREQ, meaning memory request. Memory chips must be enabled only when the memory-request signal is active, so a port operation does not accidentally affect memory.

Maria
We can demonstrate the idea with a one-bit output port controlling an LED. Use a D-type flip-flop, connect its data input to the data bus, and assign the port address 0xFF. Since 0xFF is eight binary ones, an eight-input AND gate can detect that exact address on address lines A0 through A7.

Simon
Because this port is write-only, the circuit also decodes the WRITE signal and the IOREQ signal. That matters because address 0xFF could otherwise refer either to a memory cell or to the port. Without distinguishing an input-output request from a memory request, a CPU write could affect both.

Maria
To turn the LED on, the CPU executes an instruction that writes the value 1 to port 0xFF. It places eight ones on the address lines, puts the desired logic value on the D0 data line, and pulls WRITE and IOREQ low. The flip-flop is clocked, copies the data, and the LED illuminates.

Simon
To turn the LED off, the CPU performs the same port-write operation but sends a zero instead. The LED's state follows the least significant bit connected to D0. This is a simple example of how software can control hardware through decoded addresses and bus signals.

Maria
The same general principle has been used in computer expansion buses for decades. In ISA and PCI systems, slots are connected in parallel, and decoding takes place on the expansion cards. Configuring an older ISA Sound Blaster card's address and interrupt line was effectively configuring its decoder.

Simon
That meant an expansion card could be inserted into different slots and still operate through its assigned addresses. PCI-Express changed this approach: instead of relying on the same shared parallel arrangement, each motherboard slot has independent communication lines for its individual expansion board.

Maria
So the big picture is a CPU communicating through three main bus systems. The address bus selects a location and travels outward from the CPU, the control bus specifies operations such as read, write, memory request, or input-output request, and the bidirectional data bus carries the actual values.

Simon
And address decoding makes the shared system safe and organized. It ensures that only the device assigned to a particular address range becomes active, while all other devices remain disconnected through their high-impedance outputs. With those ideas, we can understand how a CPU reads firmware, stores data in RAM, accesses ports, and controls external hardware.

Ask anything
We've read this whole study set, so questions about it get the best answers.