The Trap Unit manages exceptions and interrupts. Reset conditions are also handles by the trap unit. This implementation uses purely combinatorial logic, except for trap CSRs.
This document uses the RISC-V Specification definitions of trap, exception, and interrupt.
- trap: the synchronous transfer of control to a trap handler caused by an exception or interrupt
- exception: an unusual condition occurring at run time associated with an instruction in the current hart.
- interrupt: an external event that occurs asynchronously to the current hart.
XLEN=32
data width
clk
core clockrst_n
active-low resetpc[WIDTH-1:0]
current program counter valuedecoder_next_pc[WIDTH-1:0]
next PC calculated by decoderglobal_mie
global machine-mode interrupt enable from mstatus CSRcsr_rd_en
CSR read enablecsr_wr_en
CSR write enablecsr_addr[CSR_ADDR_WIDTH-1:0]
CSR read/write addresscsr_wr_data[WIDTH-1:0]
CSR write datamret
machine-mode return flagdbus_wait
flag indicating dbus transaction requires extra cycle
exception flags
inst_access_fault
instruction access fault flag, from fetchinst_misaligned
instruction address misaligned flag, from Decodeillegal_inst
illegal instruction flag, from decoderillegal_csr
illegal CSR instruction flag, from CSRinst[WIDTH-1:0]
instruction bits, from decoderecall
environment call flag, from decoderebreak
breakpoint flag, from decoderdata_misaligned
load/store address misaligned flag, from LSUdata_access_fault
load/store access fault flag, from LSUload_store_n
indicates source of load/store exception, from LSU (0=store,1=load)data_addr[WIDTH-1:0]
data address from LSU
interrupt flags
mtime_int
machine timer interruptgpioa_int_0
GPIOA interrupt 0gpioa_int_1
GPIOA interrupt 1gpiob_int_0
GPIOA interrupt 0gpiob_int_1
GPIOA interrupt 1gpioc_int_0
GPIOA interrupt 0gpioc_int_1
GPIOA interrupt 1uart0_rx_int
UART0 RX interruptuart0_tx_int
UART0 TX interrupttim0_int
timer0 interrupttim1_int
timer1 interrupt
next_pc[WIDTH-1:0]
next PCcsr_rd_data[WIDTH-1:0]
CSR read dataexception
exception flagtrap
trap taken flag
At reset, the Trap Unit sets the PC to BOOT_ADDR
.
During operation, if no exception or interrupt flags are asserted, next_pc
gets decoder_next_pc
and the trap
flag is low
If the dbus_wait
flag is asserted, the Trap Unit must always wait for the instruction to complete by assigning next_pc
to pc
.
When exception flag is asserted, a trap immediately occurs. Exception priority is in Table 1.
Table 1. Exception priority
Priority | Trap Code | Description |
---|---|---|
Highest | 3 | Instruction address breakpoint |
1 | Instruction access fault | |
2 0 8, 9, 11 3 3 |
Illegal instruction Instruction address misaligned Environment call Environment break Load/store/AMO address breakpoint |
|
4, 6 | Load/store/AMO address misaligned | |
Lowest | 5, 7 | Load/store/AMO access fault |
Exceptions always have priority over interrupts.
Note: instruction address misaligned exceptions are raised by control-flow instructions with misaligned targets, rather than by the act of fetching an instruction
If an interrupt flag is asserted, the appropriate bit in mip
is set.
Interrupt conditions are continually evaluated, and an interrupt will trap if:
- global interrupts are enabled, indicated by
mstatus_mie
being asserted, - an interrupt is both pending and enabled, indicated by the same bit being asserted in
mip
andmie
respectively. - an exception is not occurring
All interrupts, standard and non-standard, are registered and are thus delayed by 1 cycle.
Interrupts use a static priority scheme where lower trap codes have higher interrupt priority (i.e. highest priority is trap code 0).
When a trap occurs:
next_pc
<= base ofmtvec
mepc
<= address of instruction causing the exception or interrupted instructionmcause
<= exception code (see CSR documentation)mtval
<= see Table 2trap
<= 1
Note: see the CSR documentation for details of how a trap affects the global interrupt enable bits in mstatus
.
Table 2. mtval
encoding
Exception | Value |
---|---|
breakpoint | faulting address |
misaligned | faulting address |
access-fault | faulting address |
illegal inst | faulting instruction |
other | 0 |
The trap unit contains several CSRs. See the CSR documentation for more details.
Table 3. Trap Unit CSRs
CSR | Hardware | Software |
---|---|---|
mtvec |
read | r/w |
mip |
write | r/w |
mie |
read | r/w |
mepc |
write | r/w |
mcause |
write | r/w legal |
mtval |
write | r/w |