What you practice
MRET and mepc restore (4.3). Implement MRET logic to restore MIE from MPIE and expose mepc in the CSR module. It is in MRET & mepc restore — 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
You will implement the MRET instruction logic to restore the machine interrupt enable bit from the previous interrupt enable state. You will also ensure the machine exception program counter is correctly exposed for the return address. This mechanism is critical for returning from traps to the interrupted context.
Learning objectives
- Implement the read logic for CSR registers in the
always_combblock. - Understand how MRET restores MIE from the MPIE bit in mstatus.
- Verify the mepc value is correctly driven to the output port.
- Run the simulation to confirm the trap return sequence passes.
Theory
The Machine Return (MRET) instruction is used to return from a trap. It restores the machine state to the point where the trap occurred. The key state changes involve the mstatus register. Specifically, the MIE (Machine Interrupt Enable) bit is restored from the MPIE (Machine Previous Interrupt Enable) bit. The MPIE bit holds the value of MIE before the trap was taken. MRET also sets MPIE to 1 to indicate that the next interrupt will be masked until the next trap. The mepc (Machine Exception Program Counter) register holds the address of the instruction that was interrupted. This value must be readable and available to the pipeline for the return. The CSR read logic must simply return the current value of the internal register for the corresponding address.
| CSR Address | Name | Description |
|---|---|---|
| 0x300 | mstatus | Holds MIE and MPIE bits. |
| 0x341 | mepc | Holds the return address. |
| 0x342 | mcause | Holds the exception cause. |
| 0x305 | mtvec | Holds the trap vector base address. |
Practice
- Open RtlStudio and select this chapter in the Examples list.
- Open
student/rtl/ip/riscv/rv32i_csr.svin the Explore panel. - Locate the
always_combblock that assignscsr_rdata. - Replace the
__TODO__placeholders with the correct internal register names forCSR_MSTATUS,CSR_MTVEC, andCSR_MEPC. - Open
student/tb/tb_top.svto review the testbench stimulus. - Click Simulation in the sidebar and run the simulation.
- Check the Log panel for the
INFO:messages andPASSstatus.
Summary
In this lesson, you implemented the read logic for mstatus, mtvec, and mepc within the CSR module to support trap handling. You verified that the MRET instruction correctly restores the MIE bit from MPIE and exposes the mepc value for the return path. The simulation confirmed that the hardware state transitions align with the RISC-V specification for returning from traps.
© 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 MRET and mepc restore. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.