How computers work

Core blocks: what each part of a processor core does

A core runs a program instruction by instruction: read an instruction, figure out what it means, take numbers from registers, compute, write the result. Each of these actions is handled by its own block. There are seven of them here — the same seven that work in the educational 64-bit RISC-V datapath on the “Core” level: the ALU, the registers, the decoder, the immediate generator, the program counter, the branch unit and the memory port. You can open each one and look at the values of the current program step.

Open in 3D →

Seven blocks and one instruction

The educational core understands 19 instructions from the RV64I subset: arithmetic and logic on registers (add, sub, and, or, xor, sll, srl, slt), adding a constant (addi), loads and stores (lw, ld, sw, sd), conditional branches (beq, bne), unconditional jumps (jal, jalr) and instructions for large constants (lui, auipc). Each instruction is a 32-bit word, while the registers, the ALU and the addresses are 64-bit, just as in a real modern processor.

The execution step is the same for every instruction. The program counter PC puts out an address. The 4 bytes of the instruction are read from memory at that address. The decoder slices the word into fields and turns on the right control signals. The register file outputs the values of the two source registers, and the immediate generator outputs the number encoded in the instruction. The ALU computes a result or an address. The memory port, if needed, reads or writes data. The result is written into a register, and the PC moves on to the next instruction — or to the branch target address.

In the single-cycle model all of this happens in one clock cycle: the signals pass through every block, and on the clock edge the result is latched into the registers and the PC. Real cores split this path into dozens of pipeline stages, but the blocks are essentially the same.

Below, each block is examined separately — first in the order data flows through the ALU, then the blocks that prepare its operands and decide what to do next.

ALU: inputs and the B inverter

The ALU (arithmetic logic unit) receives two 64-bit operands. A always comes from register rs1. B comes from register rs2 or from the constant imm: the choice is made by the B multiplexer in the core, based on a signal from the decoder. For example, in add x3, x1, x2 both operands come from registers; in addi x1, x0, 5 the second operand is the number 5 from the instruction itself; and in ld x5, 16(x2) it is the offset 16, and the ALU computes the address x2 + 16.

In front of the adder sits the B inverter — 64 XOR gates, one per bit. The second input of every XOR receives a shared “invert” signal. If it is 0, the bit of B passes through unchanged (b ⊕ 0 = b); if it is 1, the bit is flipped (b ⊕ 1 = NOT b). The same signal is fed into the carry-in of the lowest bit. As a result, the adder computes A + (NOT B) + 1 = A − B. The inverter is turned on for sub, slt and for the comparison in branches.

Every block in the model is laid out across its full width — 64 strips, one per bit, from the least significant on the right to the most significant on the left. The highlighting shows the bits of the current program instruction: light blue is 1, dark is 0. Wire colors are shared across the whole model: light blue is data, amber is addresses, purple is control.

ALU: adder, logic, shifter

Inside the ALU three circuits work in parallel. The adder is 64 full adders chained together with ripple carry: the carry out of each bit goes into its neighbor on the left. Double-clicking a bit opens its five-gate circuit — the very one shown on the “Full adder” level. The adder performs add, addi, sub and slt, and also computes addresses for loads and stores.

The logic unit is 64 independent cells that compute bitwise AND, OR and XOR. Each result bit depends only on the bits of A and B in the same position; there are no carries, so logic operations are the fastest. They are used to extract and clear bits: x AND 0xFF keeps the low byte, and x XOR x always gives 0.

The shifter shifts A left (sll) or right (srl) by the number of positions given by the low bits of B; in RV64 that is 6 bits, a shift from 0 to 63. A left shift by k is multiplication by 2ᵏ; a logical right shift is unsigned division by 2ᵏ with the remainder discarded. In hardware this is a barrel shifter: several layers of multiplexers, each of which either shifts or does not shift by 1, 2, 4, 8, 16 and 32 positions. Six layers produce any shift from 0 to 63 in a single pass — no loops.

All three circuits compute simultaneously on every step, even if only one result is needed. This is simpler and faster: there is no need to wait for the right circuit to switch on. The price is wasted energy, so real cores sometimes cut unused blocks off from their inputs.

ALU: multiplexer, result, flags, control

The result multiplexer passes the answer of one of the circuits to the output, according to the ALU control signal. A multiplexer is a gate circuit that acts as a switch: for each of the 64 bits there are several inputs, and the select code opens exactly one of them.

The ALU operation code is produced by the decoder from the instruction fields: opcode distinguishes register arithmetic from addi or a load, funct3 sets the operation (000 — addition or subtraction, 100 — xor, 110 — or, 111 — and, 001 — shift left, 101 — shift right, 010 — slt), and bit 30 of the funct7 field distinguishes sub from add.

The result — 64 bits — goes on to be written into register rd, to the memory port as an address, or to the branch unit.

Flags briefly describe the result: Z — zero, N — negative (the top bit, bit 63, is 1), C — there was a carry out of the top bit, V — signed overflow. In x86 and ARM the flags are written into a separate register, and conditional branches check them. RISC-V has no flags register: comparisons put 0 or 1 straight into an ordinary register (slt) or decide the branch inside the instruction itself (beq, bne). The signals are still computed inside the ALU, though — for example, slt takes the sign of the difference corrected for overflow: N ⊕ V.

Registers x0–x31

The register file is 32 registers of 64 bits each, 256 bytes in total. It is the fastest memory in the core: a value is ready within a single clock cycle. All computation happens between registers, and only load and store instructions work with memory — this is called a load/store architecture, and RISC-V and ARM follow it.

In each step two values are read from the file (read ports rs1 and rs2) and one is written (write port rd). Reading is a 32 → 1 multiplexer per port; writing happens on the clock edge at the end of the step, so the instruction add x1, x1, x1 reads the old value of x1 and writes the new one without any confusion.

Register x0 is always zero: writes to it are ignored. This saves instructions: copying a register is addi x2, x1, 0, loading a constant is addi x1, x0, 5, an unconditional jump without saving a return address is jal x0, label. The other registers have software names defined by the calling convention: x1 is ra (return address), x2 is sp (stack pointer), x10–x17 are a0–a7 (function arguments and results).

x86-64 has 16 architectural integer registers, ARM64 has 31. Modern cores have hundreds of physical registers: register renaming lets independent instructions execute at the same time (the “Core” level). The register files of such cores have 8–12 read ports, and it is the ports, not the bits, that determine their area and energy.

Instruction decoder

A RISC-V instruction is a 32-bit word. The decoder slices it into fields: opcode in bits 6–0 (which instruction this is), rd in bits 11–7, funct3 in bits 14–12, rs1 in bits 19–15, rs2 in bits 24–20, funct7 in bits 31–25, plus the constant bits. In the model the word is colored by the fields of its format.

There are six formats: R (three registers — add), I (a register and a 12-bit constant — addi, ld, jalr), S (store — sd), B (conditional branch — beq), U (a 20-bit upper constant — lui, auipc), J (jump — jal). Register numbers are always in the same positions, and the sign of the constant is always in bit 31. So the decoder can start reading registers and extending the sign before it even knows which format it is dealing with: this is one of the reasons RISC-V is simple and fast in hardware.

Example: addi x1, x0, 5 is encoded as 0x00500093. The low 7 bits 0010011 are the opcode “operation with a constant”; bits 11–7 = 00001 — rd = x1; funct3 = 000 — addition; rs1 = 00000 — x0; bits 31–20 = 000000000101 — the constant 5. And add x3, x1, x2 is 0x002081B3: opcode 0110011, rd = 3, rs1 = 1, rs2 = 2.

The decoder’s outputs are control signals: whether to write a register, whether to take B from the constant, whether to read or write memory, what exactly to write into rd (the ALU result, data from memory or PC + 4), and whether this is a branch. The lamps in the model show which of them are on for the current instruction. In x86 the decoder is far more complex: instructions are 1 to 15 bytes long, and even finding where the next instruction begins is a task of its own.

Immediate generator imm

Many instructions carry a number inside themselves: addi x1, x0, 5, the address offset in sd x5, 8(x2), the branch offset in beq. The immediate generator gathers the imm bits from different places in the instruction and sign-extends them to 64 bits.

Each format arranges the bits in its own way. In the I format the constant is simply bits 31–20. In the S format it is split into bits 31–25 and 11–7, because in other instructions positions 11–7 hold rd, and here no destination register is needed. In B and J the bits are shuffled even more oddly, and the lowest bit is not stored at all: instruction addresses are always even. This layout was chosen so that each bit of the constant comes from the same position of the word as often as possible — that means fewer multiplexers in hardware.

Sign extension: if the top bit of a 12-bit constant is 1, the number is negative, and all 52 upper bits of the result are filled with ones. So −1 in 12 bits (0xFFF) becomes the 64-bit 0xFFFF_FFFF_FFFF_FFFF. The amber wires in the model are exactly the copies of the sign bit.

The bit wiring in the model shows which bit of the instruction becomes which bit of the constant — it is pure rearranging and duplicating of wires, with no gates. For large constants there is lui: it puts 20 bits into bits 31–12 of a register, and addi adds the low 12 — two instructions produce any 32-bit number.

Program counter PC

The PC (program counter) is a 64-bit register holding the address of the current instruction. Normally the next address is PC + 4 (instructions are 4 bytes long); it is computed by a separate small adder so the ALU is not tied up. On a branch or jump the target address is chosen instead: PC + offset for jal and conditional branches, or rs1 + imm for jalr. The address-select multiplexer decides which address becomes the new PC on the next clock edge.

This is exactly how a program “walks” through memory and makes loops: jumping back to the start of a loop is simply writing a smaller address into the PC. A function call is jal ra, function: PC + 4 is saved in ra, and the PC receives the function’s address. A return is jalr x0, 0(ra): the PC is taken from ra.

auipc rd, imm adds the PC to a constant shifted by 12 bits — this is how addresses of data relative to the code are obtained, so the same program works wherever it is loaded in memory.

An instruction address must be a multiple of 4 (2 with the compressed-instruction extension). In the model, a jump to a misaligned address or outside memory stops the program with an error — in a real processor this would be an exception, and the operating system would take control.

Branch unit

The conditional branch beq rs1, rs2, label is taken if the registers are equal; bne is taken if they are not equal. The branch unit receives both operands and subtracts: rs1 − rs2. A difference of zero means the registers are equal. Checking a 64-bit number for zero is an OR tree over all the bits followed by an inversion.

In parallel, the address adder computes where to jump: the PC of this instruction plus the offset from the instruction. A B-format offset is a 13-bit signed number whose lowest bit is always zero, so a branch reaches ±4 KB from the current instruction. For longer jumps, jal (±1 MB) or the pair auipc + jalr is used.

The outcome goes to the program counter: if the condition holds, the branch target address goes there; otherwise the program continues at PC + 4. Full RISC-V also has blt, bge, bltu, bgeu — signed and unsigned “less than” and “greater than or equal” comparisons; the educational core only has equality and inequality, and “less than” is obtained via slt.

In pipelined cores the outcome of a branch is known late, yet the next instruction has to be chosen right away. So the processor guesses it in advance with a predictor — see the “Pipeline” and “Core” levels. In a typical program a branch occurs every 5–7 instructions, so a great deal depends on the predictor.

Memory port

The instructions lw and ld read memory (4 and 8 bytes), sw and sd write it. The address is computed by the ALU: register + offset; for example, ld x5, 16(x2) reads 8 bytes at address x2 + 16. The memory port passes on the address and the data; when reading, lw sign-extends the 32-bit word to 64 bits, and ld reads all 64 bits.

Bytes are stored in little-endian order: the least significant byte at the lowest address. If sd writes the number 0x1122334455667788 at address 0x40, byte 0x40 will hold 0x88, 0x41 will hold 0x77, and so on up to 0x47, which will hold 0x11. x86 and ARM in their normal mode store numbers the same way.

In the educational model memory is 256 bytes, 16 per row; green bytes are being written, amber ones are being read, and brightness shows the byte’s value. The model requires alignment: ld and sd at an address that is a multiple of 8, lw and sw at a multiple of 4, otherwise the program stops with an error. Real x86 processors and most ARM ones do perform misaligned accesses, though they may be slower; in RISC-V it depends on the implementation.

In a real processor there are L1, L2 and L3 caches and a memory controller between the core and memory, and addresses are virtual and translated into physical ones through page tables. See the “L1 cache”, “Die” and “Memory (RAM)” levels.

Example: four instructions through all the blocks

addi x1, x0, 5. PC = 0 supplies the address, and 0x00500093 is read from memory. The decoder sees the I format: rd = 1, rs1 = 0, and turns on “B from constant” and “write register”. The register file outputs x0 = 0, the immediate generator outputs 5. The ALU adds 0 + 5, the multiplexer passes the adder’s result, and at the end of the step x1 = 5 and PC = 4.

add x3, x1, x2 (suppose x2 = 7). Both operands come from registers, and the B multiplexer selects rs2. The ALU adds 5 + 7 = 12, x3 = 12. A sub in the same place would differ by a single bit, bit 30 — and would turn on the B inverter with a carry-in of 1.

sd x3, 128(x0). S format: no destination register is written. The ALU computes the address 0 + 128 = 0x80, and the memory port writes the 8 bytes of x3’s value into bytes 0x80–0x87: 0x0C into byte 0x80 and zeros into the other seven. The program itself sits in the same memory starting at address 0, so data is placed well away from the code.

beq x1, x2, label. The branch unit subtracts 5 − 7 = −2; that is not zero, the condition is false — the PC becomes PC + 4. If the registers were equal, the PC would receive the address PC + offset. Run the program on the “Core” level and open any block: you will see the same values on its wires.

Open in 3D →

All articles

Overviews

Logic: from transistor to arithmetic

Processor

Computer

Servers and data centers

Networks and the internet

Software