RTL Studio Open IDE

4.4 · RISC-V CLASS

mcause codes and the mtvec base

Program the trap vector base and read back interrupt versus exception causes in the RISC-V CSR module.

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

What you practice

mcause codes and the mtvec base (4.4). Program the trap vector base and read back interrupt versus exception causes in the RISC-V CSR module. It is in mcause & mtvec modes — Part 4 · CSR, Trap & IRQ Interface 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

This lesson covers the Machine Trap Vector Base (mtvec) and Machine Exception Cause (mcause) registers in the RISC-V CSR interface. You will learn to program the trap vector base address and verify that the CSR module correctly reads back interrupt and exception causes. Understanding these registers is critical for implementing robust trap handling in RISC-V cores.

Learning objectives

  • Program the mtvec register to set the trap vector base address.
  • Verify the mcause register correctly captures the cause of a trap.
  • Understand the relationship between trap_enter signals and CSR state updates.
  • Read back CSR values using the csr_rdata output port.

Theory

The RISC-V privileged architecture defines several Control and Status Registers (CSRs) for handling traps. The mtvec (Machine Trap Vector Base Address) register holds the base address of the trap handler. When a trap occurs, the processor jumps to an address derived from mtvec and the trap cause. The mcause register indicates whether the trap was an exception or an interrupt, and specifies the specific cause. In this lesson, we model a simplified CSR interface where writing to mtvec updates the trap base, and reading mcause returns the value latched during a trap entry. The mstatus register controls global interrupt enablement via the MIE bit. When trap_enter is asserted, the core saves the current program counter (mepc) and the trap cause (mcause) into their respective registers, and disables interrupts by clearing MIE.

Practice

  1. Open RtlStudio and select this chapter from the Examples or Lecture list.
  2. Confirm files appear in the Explore tree under student/rtl/ and student/tb/.
  3. Open student/rtl/ip/riscv/rv32i_csr.sv in the editor.
  4. Locate the always_comb block that assigns csr_rdata based on csr_addr.
  5. Replace the __TODO__ placeholders with the correct internal register names (mstatus, mtvec, mepc, mcause) for each case item.
  6. Open student/tb/tb_top.sv to review the testbench stimulus.
  7. Click Simulation in the left sidebar and run the simulation.
  8. Check the Log panel for INFO: lines and verify the result.

Summary

You implemented the read logic for the mtvec and mcause CSRs to handle trap vector bases and interrupt causes. This practice reinforced how the processor distinguishes between exceptions and interrupts using the mcause register bits. The verification confirmed that the design correctly captures and returns these critical state values during simulation.

© 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 mcause codes and the mtvec base. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.