Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 112 of 9362. Instruction Set Architecture and CPU MicroarchitectureFree lesson

Designing the Instruction Fetch Logic

You will implement the program counter and instruction memory logic to correctly retrieve and increment instructions for sequential program execution.

Designing the Instruction Fetch Logic

In this lesson, you will learn how the CPU automatically retrieves and advances through instructions to execute a program sequentially.

The Role of the Program Counter (PC)

At the heart of the Instruction Fetch logic is a register called the Program Counter (PC). Think of the PC as a pointer that keeps track of where the CPU is currently "looking" in your program. It holds the memory address of the next instruction to be executed.

When the CPU starts, the PC is initialized to the address of the first instruction. To keep the program moving, the fetch logic must perform two distinct actions in every clock cycle:

  1. Fetch: Use the address currently held in the PC to read the instruction from the Instruction Memory.
  2. Increment: Update the PC to point to the next instruction in line.

Implementing the Logic

If our instructions are 4 bytes long (a common standard in RISC architectures), we don't just add 1 to the PC to get the next instruction; we add 4 to ensure we jump over the current instruction's bytes.

Here is a simplified conceptual representation of this hardware logic:

// Simplified Fetch Logic
always @(posedge clk) begin
    // 1. Fetch: Get instruction from memory at the current PC
    instruction = instruction_memory[PC]; 
    
    // 2. Increment: Prepare the PC for the next cycle
    PC <= PC + 4; 
end

Why This Matters

This sequential logic is what allows a program to run automatically from start to finish without human intervention. By decoupling the "Fetch" (getting the data) from the "Increment" (preparing the path), the hardware ensures that the CPU always knows exactly which line of code to process next.

If we didn't have this auto-increment logic, the CPU would simply execute the same instruction over and over again, effectively getting "stuck" at the starting line. By automating the PC update, we create a reliable heartbeat for the processor, moving steadily through the instruction stream until the program reaches a branch or a halt.

Key Takeaway: The Instruction Fetch logic sustains sequential program execution by using the Program Counter to retrieve the current instruction from memory and immediately incrementing it to prepare the address for the next operation.