What you practice
ALU for R-type and I-type operations (2.2). Implement the combinational ALU for ADD, SUB, AND, OR, XOR, and SLT operations in the RV32I instruction set. It is in ALU R-type & I-type — 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 core Arithmetic Logic Unit (ALU) for the RV32I instruction set. This component handles addition, subtraction, bitwise logic, and comparison operations required by both R-type and I-type instructions.
Learning objectives
- Implement the
rv32i_alumodule to support ADD, SUB, AND, OR, and XOR operations. - Understand how the ALU generates a zero flag based on the result.
- Verify ALU functionality using a structured testbench with sequential stimulus.
Theory
The ALU is the computational heart of the CPU core. It takes two 32-bit operands and an operation code to produce a result. The operation code alu_op selects between arithmetic, logical, and shift functions. For this lesson, you will focus on the basic arithmetic and logical operations. The ALU also outputs a zero signal which is asserted when the result is exactly zero. This signal is critical for branch instructions that depend on comparison results.
alu_op | Operation | Description | |
|---|---|---|---|
| 4'd0 | ADD | a + b | |
| 4'd1 | SUB | a - b | |
| 4'd2 | AND | a & b | |
| 4'd3 | OR | `a | b` |
| 4'd4 | XOR | a ^ b |
Practice
- Open this chapter in RtlStudio and confirm files appear in the Explore tree.
- Open
student/rtl/ip/riscv/rv32i_alu.svto implement the ALU logic. - Open
student/tb/tb_top.svto review the testbench stimulus. - Edit
student/rtl/ip/riscv/rv32i_alu.svto complete the missing case arms for ADD, SUB, AND, OR, and XOR. - Run Simulation from the sidebar and check the Log panel for
INFO:lines. - If the simulation fails, correct the logic in
student/rtl/ip/riscv/rv32i_alu.svand re-run.
Summary
You implemented the combinational ALU logic for RV32I arithmetic, bitwise, and comparison operations. By replacing the placeholders in the IP module, you connected the control signals to the correct functional units. The successful simulation confirms that your datapath correctly computes results and sets the zero flag. This foundational component is now ready for integration into the broader processor execution path.
© 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 ALU for R-type and I-type operations. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.