RTL Studio Open IDE

2.2 · RISC-V CLASS

ALU for R-type and I-type operations

Implement the combinational ALU for ADD, SUB, AND, OR, XOR, and SLT operations in the RV32I instruction set.

Video coming soon. This lesson page stays at the same URL when the lecture publishes.

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_alu module 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_opOperationDescription
4'd0ADDa + b
4'd1SUBa - b
4'd2ANDa & b
4'd3OR`ab`
4'd4XORa ^ b

Practice

  1. Open this chapter in RtlStudio and confirm files appear in the Explore tree.
  2. Open student/rtl/ip/riscv/rv32i_alu.sv to implement the ALU logic.
  3. Open student/tb/tb_top.sv to review the testbench stimulus.
  4. Edit student/rtl/ip/riscv/rv32i_alu.sv to complete the missing case arms for ADD, SUB, AND, OR, and XOR.
  5. Run Simulation from the sidebar and check the Log panel for INFO: lines.
  6. If the simulation fails, correct the logic in student/rtl/ip/riscv/rv32i_alu.sv and 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

  1. Open the RV32I lesson in the IDE. Fetch, decode, and execute are source you can simulate.
  2. Run Verilator and watch PC, the instruction, and the register write on the waveform.
  3. Treat a program printout as done only after the ISA step this lesson names matches the wave.
  4. 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.