Last Minute Notes

Digital Logic and Computer Architecture

From transistors to CPUs: logic gates and Boolean algebra, combinational and sequential circuits, adders, multiplexers, flip-flops, the von Neumann architecture, CPU components (ALU, registers, control unit), the fetch-decode-execute cycle, instruction set architectures (RISC vs CISC, x86, ARM, RISC-V), addressing modes, pipelining and hazards, branch prediction, superscalar and out-of-order execution, and performance equations.

📘 Digital Logic and Computer Architecture

This page climbs from single transistors to a modern out-of-order CPU. You do not need to design chips, but knowing how a CPU executes instructions explains why some code is fast and some is slow.

Table of Contents

  1. Transistors and Logic Gates
  2. Boolean Algebra
  3. Combinational Circuits
  4. Sequential Circuits
  5. The von Neumann Architecture
  6. Inside the CPU
  7. The Instruction Cycle
  8. Instruction Set Architectures
  9. Pipelining
  10. Branch Prediction and Speculation
  11. Superscalar and Out-of-Order Execution
  12. Measuring CPU Performance
  13. Questions

1. Transistors and Logic Gates

A transistor is an electrically controlled switch. CMOS chips pair complementary transistors so that almost no current flows except while switching. A modern high-end chip has tens of billions of them; Apple's M3 Max has about 92 billion, and large GPUs over 200 billion across multiple dies.

Gates combine transistors into Boolean functions:

GateSymbolOutput is 1 whenTruth table (A B -> out)
NOT¬AA is 00 -> 1, 1 -> 0
ANDA · BBoth are 100->0, 01->0, 10->0, 11->1
ORA + BEither is 100->0, 01->1, 10->1, 11->1
NAND¬(A · B)Not both 100->1, 01->1, 10->1, 11->0
NOR¬(A + B)Both are 000->1, 01->0, 10->0, 11->0
XORA ⊕ BThey differ00->0, 01->1, 10->1, 11->0
XNOR¬(A ⊕ B)They are equal00->1, 01->0, 10->0, 11->1

NAND and NOR are universal: any Boolean function can be built from NAND gates alone (or NOR alone). For example, NOT A = A NAND A, and A AND B = NOT (A NAND B).


2. Boolean Algebra

LawAND formOR form
IdentityA · 1 = AA + 0 = A
NullA · 0 = 0A + 1 = 1
IdempotentA · A = AA + A = A
ComplementA · ¬A = 0A + ¬A = 1
CommutativeA · B = B · AA + B = B + A
DistributiveA · (B + C) = A·B + A·CA + B·C = (A + B)(A + C)
AbsorptionA · (A + B) = AA + A·B = A
De Morgan¬(A · B) = ¬A + ¬B¬(A + B) = ¬A · ¬B

De Morgan's laws show up in everyday code: !(a && b) equals !a || !b.

Circuits are simplified with algebra or Karnaugh maps (grids that group adjacent 1s), producing fewer gates, less area, and lower delay. Real chip design uses hardware description languages (Verilog, VHDL, Chisel) and synthesis tools that do this automatically.


3. Combinational Circuits

Output depends only on current inputs; no memory.

CircuitDoesUsed for
Half adderAdds 2 bits: sum = A ⊕ B, carry = A · BBuilding block
Full adderAdds 2 bits plus a carry-in: sum = A ⊕ B ⊕ Cin, carry = A·B + Cin·(A ⊕ B)Chained into multi-bit adders
Ripple-carry addern full adders in a chainSimple but slow: carry must ripple through all bits
Carry-lookahead adderComputes carries in parallelFast adders in real ALUs
Multiplexer (MUX)Selects one of 2^n inputs using n select linesChoosing operands, data paths
DemultiplexerRoutes one input to one of many outputs
Decodern inputs to one of 2^n active outputsSelecting a memory row or register
EncoderReverse of decoderPriority encoders for interrupts
ComparatorA = B, A < BBranch conditions

Subtraction reuses the adder: A - B = A + (~B) + 1, which is why two's complement won (see Number Systems).


4. Sequential Circuits

Output depends on inputs and stored state; this is where memory comes from.

ElementBehavior
SR latchTwo cross-coupled NOR (or NAND) gates hold one bit; Set and Reset inputs
D latchStores the D input while enabled (level triggered)
D flip-flopStores D on the clock edge (edge triggered); the basic register bit
JK and T flip-flopsVariants; T toggles, used in counters
Registern flip-flops sharing a clock, holding an n-bit value
CounterRegister that increments each clock
Shift registerShifts bits along each clock; serial to parallel conversion
Finite state machineState register plus logic for next state and outputs; controllers, protocol handlers

The clock synchronizes everything: on each tick, flip-flops capture new values computed by combinational logic since the last tick. The clock period must be longer than the slowest path through the logic (critical path), which limits the frequency. Clock speeds have hovered around 3 to 6 GHz since the mid-2000s because power density (heat) grows with frequency; performance gains now come from parallelism, caches, and specialized units instead.

SRAM (caches, registers) stores a bit in a 6-transistor latch: fast, large, expensive. DRAM (main memory) stores a bit as charge in a capacitor with 1 transistor: dense and cheap, but it leaks and must be refreshed every few milliseconds, and reads are slower.


5. The von Neumann Architecture

Almost every computer follows the design John von Neumann described in 1945: instructions and data live in the same memory, and the CPU fetches both over the same path.

ArchitectureMemoryProsCons
von NeumannOne memory for code and dataSimple, flexible, programs can load programsvon Neumann bottleneck: one path for instructions and data
HarvardSeparate code and data memoriesFetch both at onceLess flexible
Modified Harvard (all modern CPUs)Unified main memory, but separate L1 instruction and data cachesBest of both

The stored-program idea is what makes software possible: a program is just data that the CPU executes, which is also why code injection attacks exist and why pages are marked non-executable.

Buses:

  • Address bus: which location (its width limits addressable memory).
  • Data bus: the value.
  • Control bus: read or write, interrupts, clock.

Modern systems replace shared buses with point-to-point links: PCIe for devices, DDR channels for memory, and on-chip networks between cores.


6. Inside the CPU

ComponentRole
ALUInteger arithmetic and logic
FPU / vector unitsFloating point and SIMD math
RegistersTiny, fastest storage inside the core (x86-64 has 16 general-purpose, ARM64 has 31)
Program counter (PC / IP)Address of the next instruction
Instruction registerThe instruction being decoded
Stack pointerTop of the current stack
Flags / status registerZero, carry, overflow, sign from the last operation
Control unitDecodes instructions and drives the data path; hardwired (fast, RISC) or microprogrammed (flexible, used for complex x86 instructions)
MMU and TLBVirtual to physical address translation (see Memory Management)
CachesL1, L2 per core, L3 shared (see Memory Hierarchy and Caches)

A modern chip packs many cores, each a complete CPU, plus shared cache, memory controllers, often a GPU, and neural engines on one die or several chiplets in one package.


7. The Instruction Cycle

Example: ADD R1, R2, R3 (R1 = R2 + R3).

  1. Fetch: send the PC to memory (via the cache), load the instruction, increment PC.
  2. Decode: opcode says ADD; source registers R2, R3; destination R1.
  3. Execute: the ALU adds the two register values.
  4. Memory: nothing to do for ADD.
  5. Write back: store the sum in R1.

Addressing modes

ModeExampleOperand is
ImmediateMOV R1, #5The constant in the instruction
RegisterADD R1, R2A register's value
DirectLOAD R1, [1000]Memory at a fixed address
Register indirectLOAD R1, [R2]Memory at the address in R2 (a pointer dereference)
Base + offsetLOAD R1, [R2 + 8]A struct field or stack variable
Indexed / scaledmov rax, [rbx + rcx*8]Array element a[i] with 8-byte elements
PC-relativeJMP +16, lea rax, [rip + label]Relative to the current instruction, for position-independent code

8. Instruction Set Architectures

The ISA is the contract between software and hardware: instructions, registers, memory model, and encoding. The microarchitecture is how a particular chip implements it (Intel and AMD both implement x86-64 very differently).

RISCCISC
InstructionsFew, simple, fixed lengthMany, complex, variable length
Memory accessOnly load and store touch memoryMany instructions operate on memory directly
Cycles per instructionMostly one (pipelined)Varies
DecodingSimpleComplex
Code densityLowerHigher
ExamplesARM, RISC-V, MIPS, PowerPCx86, x86-64, historic VAX

The line has blurred: modern x86 chips decode CISC instructions into RISC-like micro-ops internally, and ARM added some complex instructions.

ISAWhereNotes
x86-64Most PCs and serversBackward compatible to 1978; licensed only to Intel and AMD (plus a few); AVX-512 and AVX10 vector extensions
ARM64 (AArch64)All phones, Apple silicon Macs, AWS Graviton, Google Axion, Microsoft Cobalt, NVIDIA GraceLicensed design; strong performance per watt
RISC-VMicrocontrollers, accelerators, growing into Linux-class chipsOpen, royalty-free ISA with modular extensions (vector "V" extension, RVA23 profile)

Apple's move to ARM (2020) and cloud providers' ARM server chips shifted the industry: ARM servers often cost 20 to 40 percent less for the same work, so multi-architecture container images (linux/amd64, linux/arm64) are now standard.


9. Pipelining

Like a laundry line (wash, dry, fold), a pipeline overlaps the stages of consecutive instructions.

Cycle:        1    2    3    4    5    6    7    8
Instr 1:      IF   ID   EX   MEM  WB
Instr 2:           IF   ID   EX   MEM  WB
Instr 3:                IF   ID   EX   MEM  WB
Instr 4:                     IF   ID   EX   MEM  WB

Ideal: after filling, one instruction completes per cycle. Without pipelining 4 instructions take 4 x 5 = 20 cycles; with it they take 5 + 3 = 8. For n instructions in a k-stage pipeline: k + (n - 1) cycles; speedup approaches k for large n.

Pipelining improves throughput, not the latency of a single instruction.

Hazards

HazardCauseExampleFixes
StructuralTwo stages need the same hardwareOne memory port for fetch and loadSeparate I and D caches, more units
DataAn instruction needs a result not yet writtenADD R1, R2, R3 then SUB R4, R1, R5Forwarding / bypassing, stalls (bubbles), compiler scheduling
ControlThe next instruction depends on a branch outcomeif, loops, callsBranch prediction, speculation

Data hazard types: RAW (read after write, the true dependency), WAR and WAW (false dependencies removed by register renaming).

A load-use hazard cannot be fully forwarded: the loaded value arrives at the end of MEM, so the dependent instruction must stall at least a cycle.


10. Branch Prediction and Speculation

Pipelines are 15 to 20 stages deep in modern CPUs, so waiting for every branch to resolve would waste most cycles. The CPU predicts the branch and speculatively executes the predicted path. On a misprediction it throws away the speculative work, costing roughly 15 to 20 cycles.

PredictorIdea
StaticBackward branches taken (loops), forward not taken
2-bit saturating counterNeeds two wrong guesses in a row to flip
Global history / correlatingUses the outcomes of recent branches
TAGE, perceptronModern predictors; accuracy often above 95 percent
Branch target bufferPredicts where a jump goes, for indirect calls and virtual methods
Return stack bufferPredicts return addresses

The classic demonstration: summing values above a threshold in an array runs several times faster when the array is sorted, because the branch becomes predictable. Branchless code (cmov, arithmetic masks) avoids the problem when data is random.

Spectre and Meltdown (2018) showed that speculative execution leaves traces in the cache that can leak secrets across security boundaries. Mitigations (retpolines, kernel page-table isolation, speculation barriers) cost some performance, especially for system calls.


11. Superscalar and Out-of-Order Execution

TechniqueIdea
SuperscalarFetch, decode, and issue several instructions per cycle (modern cores: 6 to 10 wide)
Out-of-order executionExecute instructions as soon as their inputs are ready, not in program order; a reorder buffer retires them in order so the program sees sequential behavior
Register renamingMap a few architectural registers to hundreds of physical ones to remove false dependencies
SMT / hyper-threadingTwo hardware threads share one core's units to fill idle slots
SIMDOne instruction operates on many values (see Parallelism and Modern Hardware)
VLIWCompiler bundles parallel operations explicitly (Itanium, DSPs); lost in general-purpose CPUs

The practical lesson: modern CPUs can do several operations per cycle, but only if the code has independent work and data is in cache. A chain of dependent operations (each needing the previous result) or a cache miss stalls everything.


12. Measuring CPU Performance

The CPU performance equation:

CPU time = Instruction count x CPI x Clock period
         = Instructions x (Cycles / Instruction) x (Seconds / Cycle)
FactorAffected by
Instruction countAlgorithm, compiler, ISA
CPI (cycles per instruction), or IPC (its inverse)Microarchitecture, cache misses, branch mispredictions, dependencies
Clock rateProcess technology, power budget

Example: a program runs 2 billion instructions at a CPI of 1.5 on a 3 GHz CPU: 2 x 10^9 x 1.5 / (3 x 10^9) = 1 second.

LawStatement
Amdahl's lawSpeedup is limited by the part you do not improve: 1 / ((1 - p) + p / s)
Gustafson's lawWith more processors, people solve bigger problems, so parallel speedup scales with problem size
Moore's lawTransistor counts double about every two years; slowing but not dead thanks to 3D stacking and chiplets
Dennard scalingPower per area stayed constant as transistors shrank, until about 2006; its end forced the multi-core era

Benchmarks: SPEC CPU for general compute, Geekbench for consumer comparisons, and your own workload for anything that matters. perf stat shows instructions, cycles, IPC, cache misses, and branch mispredictions for a real program.


13. Questions

QuestionShort answer
Why are NAND and NOR universal?Any Boolean function can be built from either alone
State De Morgan's laws.¬(A·B) = ¬A + ¬B; ¬(A+B) = ¬A · ¬B
Combinational vs sequential circuits?Output from inputs only vs inputs plus stored state
Latch vs flip-flop?Level triggered vs edge triggered
SRAM vs DRAM?6-transistor latch, fast, caches vs capacitor, dense, needs refresh, main memory
What is the von Neumann bottleneck?Shared path for instructions and data limits throughput
Explain fetch-decode-execute.Fetch at PC, decode opcode and operands, execute, memory, write back
RISC vs CISC?Simple fixed-length load-store vs complex variable-length; x86 decodes into micro-ops
What is pipelining?Overlapping instruction stages to raise throughput
Pipeline hazards and fixes?Structural, data (forwarding, stalls), control (prediction)
Why is sorted data faster in a branchy loop?The branch becomes predictable, no misprediction flushes
What is out-of-order execution?Run instructions when inputs are ready, retire in order
CPU performance equation?Instructions x CPI x clock period

Next: Memory Hierarchy and Caches.

On this page