What you practice
Branch condition compare unit (2.4). Implement the branch condition compare logic for RV32I branch instructions (BEQ, BNE, BLT, BGE, BLTU, BGEU) in the rv32i_branch module. It is in Branch compare unit — Part 2 · Execute Path of the RISC-V RV32I Core Masterclass.
The RV32I core is Verilog you simulate in the browser IDE. Verilator runs the program; the waveform shows PC, the instruction, and the register write.
What you will learn
You will implement the logic for the RV32I branch condition compare unit. This unit evaluates branch instructions like BEQ and BLT based on the funct3 opcode field. Mastering this logic is essential for correct control flow in a RISC-V processor.
Learning objectives
- Implement signed and unsigned comparison logic for branch instructions.
- Map
funct3values to specific comparison operators in SystemVerilog. - Verify branch behavior using a testbench with specific operand pairs.
Theory
The branch condition unit determines if a branch should be taken based on two register values and a function code. The funct3 field selects the comparison type. Signed comparisons use $signed() casting to interpret the 32-bit values as two's complement integers. Unsigned comparisons treat the bits as raw magnitudes. The table below maps funct3 codes to their operations.
| funct3 | Instruction | Operation | Signed/Unsigned |
|---|---|---|---|
| 000 | BEQ | rdata1 == rdata2 | N/A |
| 001 | BNE | rdata1 != rdata2 | N/A |
| 100 | BLT | rdata1 < rdata2 | Signed |
| 101 | BGE | rdata1 >= rdata2 | Signed |
| 110 | BLTU | rdata1 < rdata2 | Unsigned |
| 111 | BGEU | rdata1 >= rdata2 | Unsigned |
The DUT instantiates the rv32i_branch module to perform these checks. The always_comb block ensures the taken signal updates immediately when inputs change.
Practice
Open this chapter in RtlStudio and confirm files appear in the Explore tree. Edit student/rtl/ip/riscv/rv32i_branch.sv to complete the branch logic. Replace each __TODO__ placeholder with the correct comparison expression for the corresponding funct3 case. Ensure you use $signed() for signed comparisons (BLT, BGE) and raw comparison for unsigned (BLTU, BGEU). Review student/tb/tb_top.sv to understand the test stimulus. Run Simulation from the sidebar to check your work. Read the Log panel to see if the testbench passes the checks.
Summary
You implemented the branch condition compare unit to evaluate signed and unsigned comparisons based on the funct3 opcode. This practice reinforced the correct use of casting for signed arithmetic and direct comparison for unsigned logic. The successful simulation confirms that your design correctly determines branch taken signals for all supported instructions.
© 2026 RtlStudio. Licensed under CC BY-NC-ND 4.0. Commercial use and modifications by third parties are not permitted.
Run it in the Verilog IDE
- Open the RV32I lesson in the IDE. Fetch, decode, and execute are source you can simulate.
- Run Verilator and watch PC, the instruction, and the register write on the waveform.
- Treat a program printout as done only after the ISA step this lesson names matches the wave.
- Change the RTL in the editor, rerun Verilator, and read the waveform again in the same tab.
The lesson is done when the simulation matches Branch condition compare unit. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.