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
mepcregister stores the address of the trapped instruction. - Learn how the
mcauseregister identifies the reason for the trap. - Practice reading CSR values back after a trap has occurred.
- Verify that the MIE bit in
mstatusis 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.
| Register | Address | Description |
|---|---|---|
mepc | 0x341 | Stores the return address for traps. |
mcause | 0x342 | Stores the exception code (e.g., 11 for ECALL). |
mstatus | 0x300 | Controls interrupt enable (MIE) and previous state (MPIE). |
Practice
- Open RtlStudio and select this chapter.
- In the Explore panel, open
student/rtl/ip/riscv/rv32i_csr.sv. - Locate the
always_combblock that assignscsr_rdata. - Replace the
__TODO__placeholders with the correct internal register names forCSR_MSTATUS,CSR_MTVEC,CSR_MEPC, andCSR_MCAUSE. - Open
student/tb/tb_top.svand run Simulation. - 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
- 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 ECALL trap entry. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.