Fetch is where every instruction starts: the core sends the program counter, the PC, to the instruction memory and gets back the instruction to execute. The PC then has to move on, either to the next instruction in memory or, after a taken branch or a jump, somewhere else. This article specifies cvr32_pc, the module that holds the PC and chooses its next value, for exercise E06. It is small, but it is the heartbeat of the core: if it is wrong, nothing else gets a chance to run.

8 min read | Beginner to Intermediate

What You'll Learn

  • Describe what the PC register does each cycle and how it connects to the instruction memory
  • Explain why the next PC is PC + 4 in this core, and when it is a target instead
  • Write an asynchronous, active-low reset with a parameter for the reset address
  • Predict the PC cycle by cycle for a sequence of redirects, and pass E06

Before You Start

You need Verilog's clocked logic and parameters; the Verilog Tutorial covers both. The Instruction Cycle explains the role of the PC in general and how a processor picks the next instruction. The Core's Specification defines the reset behavior used here. The PC exercise does not depend on the building-block exercises, but it opens the chapter in which your blocks become a core, so pass E01 to E05 first.

What the PC Does

The PC is a 32-bit register that holds the address of the instruction being executed. In a single-cycle core, its value goes straight to the instruction memory's address port, imem_addr, and the memory answers with the instruction word in the same cycle. The rest of the core decodes and executes that instruction, and at the rising clock edge that ends the cycle, the PC takes its next value. Then the next cycle starts with a new instruction.

The cvr32_pc module: an adder computes PC + 4, a multiplexer controlled by redirect chooses PC + 4 or target, and the PC register takes the chosen value at the clock edge. The PC drives the instruction memory, which returns the instruction in the same cycle.

There are only two possible next values:

  • PC + 4, the next instruction in memory. Every RV32I instruction is 4 bytes long, and the PC holds a byte address, so the next instruction is 4 bytes further on. (RISC-V also has a compressed extension with 2-byte instructions, which would need PC + 2 as well; this core does not implement it. ISA Design Choices: RISC, CISC and Extensions explains why it exists.)
  • A target, when the current instruction is a taken branch or a jump. The PC module does not compute the target; the branch logic does, in Branches and Jumps, and tells the PC module with a 1-bit redirect signal.

Until the core has branches, redirect is tied to 0 and the PC simply counts 0, 4, 8, 12 and so on.

The Ports

module cvr32_pc #(
    parameter logic [31:0] RESET_PC = 32'h0000_0000
) (
    input  logic        clk,
    input  logic        rst_n,      // active-low reset
    input  logic        redirect,   // 1: go to target next, 0: go to PC + 4
    input  logic [31:0] target,
    output logic [31:0] pc
);

The behavior:

  • While rst_n is 0, pc is RESET_PC. The reset is asynchronous: pc takes RESET_PC as soon as rst_n goes low, without waiting for a clock edge, and holds it through any clock edges while reset stays low.
  • At each rising edge of clk with rst_n = 1, pc becomes target if redirect is 1, and pc + 4 otherwise.

redirect and target describe the instruction being executed in the current cycle; they take effect at the edge that ends it. This is the one timing point to get right: the PC module does not register target and use it a cycle later. It uses it at the very next edge.

Reset and the Reset Address

A processor has to start somewhere, and the reset address, or reset vector, is where. In this core it is 0x0000_0000, where the start-up code is placed. Making it a parameter costs nothing and lets the same module work in a system where the program sits at another address: an instance can write #(.RESET_PC(32'h0000_0080)) to start at 0x80. The lab's boot template checks this: when a simulation starts, it prints the address of the first fetch next to the address the program was loaded at, because a core that starts in the wrong place does not fail, it just runs whatever is there.

The reset is active-low because that is the convention of most chips and of the lab's template (the "_n" in the name marks it). It is asynchronous for a practical reason: the core's outputs are defined from the moment reset is applied, even before the clock runs. In Verilog, an asynchronous reset is a clocked block that is also triggered by the falling edge of the reset, with the reset branch first: the pattern The Core's Specification shows in its coding rules.

Quick Check

Q: Reset is released before cycle 1. redirect is 0 in cycles 1 and 2, 1 in cycle 3 with target = 0x0000_0040, and 0 in cycle 4. What is pc in cycles 1 to 5?

Show Answer

0x0, 0x4, 0x8, 0x40, 0x44. The redirect in cycle 3 takes effect at the edge between cycles 3 and 4, so the instruction at 0x8 is the one that redirected, and counting resumes from the target.

How It Is Tested

The hidden test instantiates your module twice, once with the default reset address and once with RESET_PC = 0x80, and drives both with the same clock, reset, redirect and target. It holds reset low through a clock edge and checks that both PCs hold their reset values, then releases reset and runs 400 clock edges. Before each edge it picks redirect at random, set about one time in eight, and a random word-aligned target, and after the edge it compares both PCs with a model. Halfway through it asserts reset again and checks that both PCs return to their reset values at once. That is 804 checks.

Three typical mistakes were run against the test, and each fails with a message that points at it:

Output
FAIL: clock edge during reset (PC must hold its reset value) (RESET_PC = 0x80): expected PC 0x00000080, got 0x00000000
FAIL: after a redirect to 0x4a061f50: expected PC 0x4a061f50, got 0x0000000c
FAIL: after a step without redirect: expected PC 0x00000004, got 0x00000001

The first is a module that resets to a hard-coded 0 instead of RESET_PC; only the second instance shows it. The second ignores redirect. The third steps by 1 instead of 4, a classic slip from thinking in instructions instead of bytes.

Where Does This Matter?

Real chips set the reset vector in different ways: fixed in hardware, chosen by pins sampled at reset, or held in a register that a boot ROM fills. The idea is always the same as your parameter: the first instruction must be where the first code is. Getting it wrong is a classic first-silicon bug, which is why boot code is usually the very first thing a new chip is tested with. Boot ROM and the Boot Flow in the SoC Design tutorial describes the process.

Quick Check

Q: A student's PC module adds 4 to target on a redirect, "because the branch is already done". What happens to a program?

Show Answer

Every taken branch and jump skips the first instruction at its destination. A loop would skip its first instruction on every iteration after the first, and a call would skip the first instruction of the function. The target is the address of the next instruction to execute, so the PC takes it exactly.

Common Mistakes

Mistake #1: Hard-coding the reset address

Writing pc <= 0 in the reset branch passes with the default parameter and fails as soon as anyone sets RESET_PC. Use the parameter.

Mistake #2: Counting in instructions instead of bytes

The PC is a byte address and instructions are 4 bytes, so the step is 4. A PC that steps by 1 fetches the same word four times, because the memory ignores the low two address bits.

Mistake #3: Delaying the redirect

Capturing target in a register and using it one cycle later executes one extra instruction after every taken branch. In this single-cycle core the redirect takes effect at the next clock edge.

Your Turn in the Lab

The exercise opens with a visible test, testbench.v, next to your design: a short list of directed checks that prints ok or FAIL for each. Simulate it as you work and add your own checks to it. When you submit, it runs together with the hidden tests, and your edits to it do not change the grading.

Write cvr32_pc in E06. It is the last block the first core needs: with your ALU, register file, decoder and immediate generator, you can now build a core that runs ALU instructions, in Executing ALU Instructions. Keep the passed version as cvr32_pc.sv.

Open exercise E06 in ChipVerify Lab

Summary

  • The PC holds the address of the current instruction and drives the instruction memory, which answers in the same cycle.
  • At each rising clock edge the PC becomes PC + 4, or the target when redirect is 1; RV32I instructions are 4 bytes and addresses count bytes.
  • Reset is active-low and asynchronous and sets the PC to the RESET_PC parameter, 0x0000_0000 by default.
  • The redirect and target of the current instruction take effect at the very next clock edge.
  • The hidden test checks two instances, one with RESET_PC = 0x80, through reset, 400 random steps and a second reset.
Did you find what you were looking for?

Quiz

No quiz questions available for this article.