← Back to blogCPU architecture

Visualizing the Fetch-Decode-Execute Cycle in a Simulator

Single-step a transparent teaching CPU, trace every register transfer, and distinguish instruction phases from clock cycles and modern processor pipelines.

DennyDenny
9 min read
Chalkboard feature illustration for “Visualizing the Fetch-Decode-Execute Cycle in a Simulator”

TL;DR: Fetch-decode-execute is a conceptual sequence, not a promise that every instruction takes exactly three clocks. In a transparent multicycle CPU, fetch selects and reads an instruction, decode interprets its fields and prepares operands, and execute performs the operation and commits architectural state. The most useful simulator exercise is to predict each register transfer before stepping, then explain every observed change from an explicit microoperation.

A CPU schematic becomes much easier to understand when it is treated as a sequence of state changes rather than a page of boxes and wires. A simulator lets you pause after every clock edge and ask:

  • Which register was allowed to load?
  • Which source drove each bus?
  • Which control signals were asserted?
  • Which value changed, and why?
  • What will the program counter select next?

This case study uses a deliberately small, word-addressed, multicycle teaching processor. It is not a description of x86, Arm, RISC-V, or every internal detail of the DigiSim template. Its purpose is to establish a trace method you can apply to the Sequential Instruction Executor or to a CPU you build yourself.

For the architecture-wide treatment—including caches, variable-length instructions, pipelining, speculation, and ISA differences—read the fetch-decode-execute deep dive.

The Teaching Datapath

Our trace assumes these visible blocks:

BlockRole in this model
Program counter (PC)Holds the word address used for the next sequential instruction fetch
Memory-address register (MAR)Holds an address while the memory operation completes
Memory-data register (MDR)Holds a word returned by memory or prepared for a write
Instruction register (IR)Preserves the current instruction while it is decoded and executed
Register fileHolds programmer-visible operands and results
ALUPerforms arithmetic, logic, and comparisons
Flags registerOptionally preserves status such as zero, carry, sign, and overflow
Control sequencerSelects sources, destinations, ALU operation, and memory intent for each microstep

These registers are a teaching decomposition. A real implementation may combine, rename, duplicate, pipeline, or omit them. For example, a processor can hold multiple fetched instructions in queues rather than one instruction register, and a memory interface can hold requests in protocol state rather than one MAR.

Define the Model Before Tracing It

The trace is only meaningful if its conventions are written down. We will assume:

  • memory is word-addressed
  • every instruction occupies one word
  • the normal next address is therefore PC + 1
  • memory read data is captured in a later microstep
  • register writes occur on the active clock edge when their load enable is asserted
  • the example register file has two read ports and one write port
  • instructions update flags only when their definition says so

Byte-addressed or variable-length machines use a different PC increment. A faster memory can combine microsteps; a slower memory can insert wait states. Those are design choices, not contradictions.

Fetch as Explicit Microoperations

One possible three-step fetch is:

F0: MAR <- PC
F1: MDR <- MEM[MAR],  PC <- PC + 1
F2: IR  <- MDR

F0: Select the instruction address

The PC drives the path into the MAR, and MAR_LOAD is asserted. After the edge:

MARnew=PColdMAR_{new}=PC_{old}

Nothing requires the PC to change in this microstep.

F1: Read memory and prepare the fall-through address

Memory returns the word selected by the MAR. The MDR captures it, while the PC incrementer prepares the next sequential word address:

MDRnew=MEM[MAR]MDR_{new}=MEM[MAR] PCnew=PCold+1PC_{new}=PC_{old}+1

Those transfers can occur together because they write different registers and use different datapath resources in this model.

F2: Preserve the instruction

The IR captures the MDR:

IRnew=MDRIR_{new}=MDR

The memory data path is now free for a later load or store while the instruction bits remain stable for decode.

This sequence assumes the memory result is valid in F1. A handshake-based design would remain in a wait state until READY or an equivalent response arrives.

Decode Is More Than Naming an Opcode

Decode establishes what the instruction means under the teaching ISA:

  1. identify the instruction format
  2. determine whether the encoding is legal
  3. extract register indexes or an immediate value
  4. select source operands
  5. choose an ALU function or memory operation
  6. determine which architectural state may be updated

An instruction format might contain:

register form:  opcode | source A | source B | destination
immediate form: opcode | destination | immediate

The bit positions and widths are ISA-specific. Do not transfer field labels from one format to another without checking the opcode first. The instruction-register and decode guide explains fixed- and variable-length formats in more detail.

In this small processor, decode may be combinational once the IR is loaded. “Decode phase” does not necessarily mean a dedicated clock cycle; it means the work that turns encoded bits into operand and control intent.

Execute and Commit

Execute depends on the instruction class.

Register arithmetic

For:

ADD R2, R0, R1

the logical work is:

A <- REG[R0]
B <- REG[R1]
R <- A + B
REG[R2] <- R
FLAGS <- status(R, carries)

With two combinational read ports, the register operands can reach the ALU in one microstep and the destination can capture the result on the following edge. A single-bus CPU may need separate transfers into temporary operand registers.

Load immediate

For:

LDI R0, 5

decode extracts the immediate field, extends it according to the instruction definition, and routes it to the destination write port:

REG[0]new=5REG[0]_{new}=5

Whether the immediate is zero-extended or sign-extended must be specified. The same raw bits can represent different values under those two rules.

Memory operation

A load or store can require:

  1. effective-address calculation
  2. memory request
  3. wait for response
  4. data capture or write completion
  5. optional register write-back

That is why “execute” often spans several cycles in a multicycle CPU.

Branch

A conditional branch computes or obtains:

  • a branch condition
  • a target address
  • a fall-through address

Then a multiplexer selects the next PC value. If the branch is not taken, the incremented PC remains. If taken:

PCnew=targetPC_{new}=target

Flags are one possible condition source. Base RISC-V instead compares registers as part of its branch instructions. The program-counter guide covers these update sources.

Worked Program: Load, Load, Add

Use this abstract program:

Word addressInstructionMeaning
0LDI R0, 5place 5 in R0
1LDI R1, 3place 3 in R1
2ADD R2, R0, R1place R0 + R1 in R2
3HALTstop accepting new instruction work

The exact binary encoding depends on the template or CPU you choose. Write its opcode table beside the trace rather than assuming the hex values from another teaching ISA.

Initial state

Assume reset establishes:

PC = 0
R0 = 0
R1 = 0
R2 = 0
controller = F0

Do not assume every memory or register clears unless reset or initialization explicitly provides it.

Trace

StepMicrooperationImportant state after the edge
1MAR <- PCMAR=0
2MDR <- MEM[MAR], PC <- PC+1MDR=LDI R0,5, PC=1
3IR <- MDRIR=LDI R0,5
4execute immediate writeR0=5
5MAR <- PCMAR=1
6MDR <- MEM[MAR], PC <- PC+1MDR=LDI R1,3, PC=2
7IR <- MDRIR=LDI R1,3
8execute immediate writeR1=3
9MAR <- PCMAR=2
10MDR <- MEM[MAR], PC <- PC+1MDR=ADD R2,R0,R1, PC=3
11IR <- MDRIR=ADD R2,R0,R1
12execute ALU writeR2=8; flags update if specified

This program contains three instructions but twelve illustrated microsteps. Calling each instruction a “cycle” would hide the distinction between instruction count, microsteps, and clock cycles.

What to Observe in the Simulator

For every clock step, record:

Signal or stateQuestion
controller stateWhich microoperation is intended?
PCDoes it identify the correct next word?
MARWhich memory address is held stable?
MDRWhich memory word was captured?
IRWhich instruction remains active?
source enablesIs exactly the intended source selected?
load enablesWhich registers will change on this edge?
ALU operationDoes it match the decoded instruction?
write address/dataIs the correct destination receiving the correct value?
flagsAre they updated, held, or irrelevant for this instruction?

A reliable trace has one explicit cause for every state change. If two sources are enabled onto a modeled shared bus, or if a register changes without a load enable, stop and diagnose the control logic.

A Better Lab Procedure

Pass 1: Predict

Before pressing Step:

  1. write the current controller state
  2. list asserted control signals
  3. calculate the next value of every register that can load
  4. identify registers that must hold

Pass 2: Observe

Advance one edge and compare actual values with the prediction. Record the first divergence rather than continuing with corrupted state.

Pass 3: Explain

For each mismatch, classify it:

  • wrong instruction encoding
  • wrong source selection
  • wrong destination or load enable
  • incorrect ALU function
  • reset or initialization mismatch
  • off-by-one PC update
  • memory response timing mismatch
  • branch condition or target error

Pass 4: Mutate one thing

Change ADD to SUB, change one immediate, or change one destination register. Predict exactly which microsteps and values should differ. A good architecture model makes local program changes produce explainable trace changes.

Verification Beyond One Program

The three-instruction trace is a worked example, not sufficient CPU verification. A useful test plan includes:

  • every legal opcode
  • representative legal register indexes and immediates
  • illegal or reserved encodings
  • arithmetic boundary cases
  • load/store at address boundaries
  • taken and not-taken branches
  • branch target and fall-through behavior
  • reset from several controller states
  • halt or idle behavior
  • wait states if memory can delay
  • control invariants such as one bus source and one intentional write destination

For each instruction, define its architectural result independently of the microsteps. Then check that the final visible state matches even if the internal sequencing changes.

What This Model Does Not Claim

Modern processors do not generally fetch one instruction, finish it completely, then begin the next. They can:

  • fetch several bytes or instructions together
  • predict future control flow
  • decode into internal operations
  • overlap many instructions
  • execute independent operations out of order
  • wait on caches while other work proceeds
  • retire results in architectural order

Fetch-decode-execute remains a valuable causal model, but a high-performance implementation divides and overlaps those responsibilities. “Three phases” is not a three-cycle performance equation.

The simulation also does not establish physical timing. A functional model verifies value and sequencing behavior. Maximum clock rate, setup/hold closure, clock skew, and routed delay belong to the target implementation and its timing tools.

Classroom Use

A concise 30-minute activity:

  1. Give students the instruction table and initial state.
  2. Trace the first LDI together.
  3. Ask pairs to predict the second LDI.
  4. Have each pair trace the ADD and explain every enable.
  5. Replace ADD with SUB and compare only the affected signals.
  6. Finish with one taken branch and one not-taken branch.

Grade the reasoning trace, not just the final value. A student who can explain why the PC, IR, source operands, and destination changed has learned more than one who reached R2=8 by trial and error.

Continue Learning

Open the Sequential Instruction Executor, identify its actual registers and instruction encoding, and adapt the trace table to what the template exposes. Treat the template’s labels and behavior as authoritative for that circuit.

Then study the components separately:

The exercise is complete when you can pause at any edge and predict every next-state change before the simulator reveals it.

Apply the concept in a simulator model.