Specifications

Determining the Cause of Interrupt and Instruction-Related Exceptions
The general exception handler must determine the cause of each exception and then transfer control to an
appropriate exception routine.
With Extra Exception Information
When you have included the extra exception information in your Nios II system, the CAUSE field of the
exception register contains a code for the highest-priority exception occurring at the time and the BADDR
field of the badaddr register contains the byte instruction address or data address for certain exceptions.
Refer to the Nios II Exceptions table for more information in the Exception Overview section.
Note: External interrupts do not set exception.CAUSE.
To determine the cause of an exception, simply read the cause of the exception from exception.CAUSE
and then transfer control to the appropriate exception routine.
Note:
Extra exception information is always enabled in Nios II systems containing an MMU or MPU.
Related Information
The exception Register on page 3-17
The badaddr Register on page 3-22
Exception Overview on page 3-36
Without Extra Exception Information
When you have not included the extra exception information in your Nios II system, your exception
handler must determine the cause of exception itself. For this reason, Altera recommends always enabling
the extra exception information.
When the extra exception information is not available, use the sequence in the example below to
determine the cause of an exception.
Example 3-3: Determining Exception Cause Without Extra Exception Information
/* With an internal interrupt controller, check for interrupt
exceptions. With an external interrupt controller, ipending is
always 0, and this check can be omitted. */
if (estatus.PIE == 1 and ipending != 0) {
handle interrupt
/* Decode exception from instruction */
/* Note: Because the exception register is included with the MMU and */
/* MPU, you never need to determine MMU or MPU exceptions by decoding */
} else {
decode instruction at $ea-4
if (instruction is trap)
handle trap exception
else if (instruction is load or store)
handle misaligned data address exception
else if (instruction is branch, bret, callr, eret, jmp, or ret)
handle misaligned destination address exception
else if (instruction is unimplemented)
handle unimplemented instruction exception
else if (instruction is illegal)
handle illegal instruction exception
else if (instruction is divide) {
if (denominator == 0)
NII51003
2015.04.02
Determining the Cause of Interrupt and Instruction-Related Exceptions
3-53
Programming Model
Altera Corporation
Send Feedback