RTL Studio Open IDE

4.2 · RISC-V CLASS

ECALL trap entry

Implement CSR read logic for mepc, mcause, and mstatus to verify ECALL trap entry behavior in the RISC-V CSR module.

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

What you practice

ECALL trap entry (4.2). Implement CSR read logic for mepc, mcause, and mstatus to verify ECALL trap entry behavior in the RISC-V CSR module. It is in ECALL & trap entry — 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

Today you will implement the hardware logic that saves the program counter and exception cause when an ECALL instruction triggers a trap. This mechanism is critical for operating systems to handle system calls and exceptions correctly. You will modify the CSR module to update mepc and mcause registers during trap entry.

Learning objectives

  • Understand how the mepc register stores the address of the trapped instruction.
  • Learn how the mcause register identifies the reason for the trap.
  • Practice reading CSR values back after a trap has occurred.
  • Verify that the MIE bit in mstatus is cleared during trap entry.

Theory

When a trap occurs, the processor must save the state so it can return to the trapped instruction later. The mepc (Machine Exception Program Counter) holds the address of the instruction that caused the trap. The mcause (Machine Cause) holds a code indicating the trap type. For an ECALL, the cause code is 11. The mstatus register tracks the machine interrupt enable bit (MIE). When a trap enters, MIE is saved to MPIE and then cleared to disable interrupts during the handler. The CSR module must support reading these registers via the csr_rdata output when the correct address is provided.

RegisterAddressDescription
mepc0x341Stores the return address for traps.
mcause0x342Stores the exception code (e.g., 11 for ECALL).
mstatus0x300Controls interrupt enable (MIE) and previous state (MPIE).

Practice

  1. Open RtlStudio and select this chapter.
  2. In the Explore panel, open student/rtl/ip/riscv/rv32i_csr.sv.
  3. Locate the always_comb block that assigns csr_rdata.
  4. Replace the __TODO__ placeholders with the correct internal register names for CSR_MSTATUS, CSR_MTVEC, CSR_MEPC, and CSR_MCAUSE.
  5. Open student/tb/tb_top.sv and run Simulation.
  6. Check the Log panel for the INFO: lines and verify the test passes.

Summary

You implemented the hardware logic to save the trapping program counter and exception cause into the mepc and mcause CSRs respectively. This practice reinforced the atomic update of machine-mode status registers during trap entry to ensure correct exception handling. The successful simulation confirms that the CPU correctly captures the trap state for subsequent processing.

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