What you practice
Machine CSR file (4.1). Implement the Machine CSR read mux for mstatus, mtvec, mepc, and mcause to support read, set, and clear operations in the RV32I core. It is in CSR file (CSRRW/S/C) — 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 Machine Status Register (mstatus) and related Machine CSRs (mtvec, mepc, mcause) in the RV32I CSR module. This module handles read, write, set, and clear operations for these control registers. Mastering CSRs is essential for building a functional RISC-V core that can handle traps and interrupts.
Learning objectives
- Implement the CSR read mux to return the correct register value based on
csr_addr. - Understand how
csr_cmd(RW, RS, RC) modifies CSR values in the write logic. - Verify the MIE bit behavior in
mstatususing the testbench stimulus. - Complete the
rv32i_csrmodule to pass the simulation checks.
Theory
Machine CSRs control the processor's state during exception handling and interrupt management. The mstatus register contains the MIE (Machine Interrupt Enable) bit at position 3. The testbench uses specific commands to modify these bits. The csr_cmd input determines the operation: 01 for read-write, 10 for set, and 11 for clear. The read mux must select the correct internal register (mstatus, mtvec, mepc, or mcause) based on the csr_addr input. The write logic updates the internal registers only when csr_cmd is not NOP. The trap_enter and mret signals handle context switching by saving/restoring mepc and updating mstatus bits automatically.
Practice
- Open RtlStudio and select this chapter from the Examples list.
- Navigate to the Explore tab and open
student/rtl/ip/riscv/rv32i_csr.sv. - Locate the
always_combblock that assignscsr_rdata. - Replace the
__TODO__[0]placeholders with the correct internal register names (mstatus,mtvec,mepc,mcause) for each case label. - Save the file and open
student/tb/tb_top.svto review the testbench stimulus. - Click Simulation in the sidebar and run the test.
- Check the Log panel for
INFO:lines andINFO: PASS.
Summary
You implemented the Machine CSR file by completing the read multiplexer and write command logic for mstatus, mtvec, mepc, and mcause. This practice reinforced how RW, RS, and RC operations manipulate specific trap and control registers in the RISC-V architecture. Successful verification confirmed that the core correctly handles register updates and interrupt enable bits. This foundational work ensures accurate exception handling and system state management for the RV32I core.
© 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 Machine CSR file. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.