# RISC-V: building a datapath for conditional Branch instructions

I am simulating a multi-cycle 32bit RISC-V CPU in Logisim-Evolution and so far so good, i had implemented almost every instruction from the basic RV32I ISA. But im having trouble to understand how the condition branch instructions works.

I want to implement:

1. Branch if equal (BEQ)
2. Branch if not equal (BNE)
3. Branch if less than (BLE)
4. Branch if greater than equal (BGE)
5. Branch if less then unsigned (BLTU)
6. Branch if greater then equal unsigned (BGEU)

I have read that the ALU uses 4 bits as input:

3bits: Funct3 or ADD or SUB with with check for zero

1bit: 6th bit from funct7

So than how (or where) is the branch generated? The ALU unit has a ZERO output, and its anded with a control line to activate the branch. But the Branch instructions have all different Funct3 codes, so that means it must be somewhere decoded.

And even than, where is the calculation performed, for example for BGE/BGEU? We can calculate the BEQ just by subtracting the two numbers in ALU, but what for the others?

My simulation of the CPU for ilustartion

Thanks for any help.

• For BGE etc, think what happens if you subtract. Does the sign bit of the result tell you anything useful? – user_1818839 Jul 28 '19 at 12:43

The abstract behind the branch ISA's are that if the conditions are met, the program counter will JUMP to the given address. To implement a branch, the Branch generator must be correct and the PC source must be set to branch. To insure the PC source is correct, a control unit decoder must be able to differentiate between the commands. That's where the opcode and the func3, func7 come into play. So the rundown pretty much goes like this.

1. Compiler breaks down the ISA into binary where the decoder uses that to determine the given instruction.
2. The control unit sets the muxes to the correct one.
3. The PC then moves to the correct address

Photo was taken from Cal Poly EE Department CPE 233 Assembly and Computer Design taught by Professor Joseph Callenes-Sloan.