Nios® V Processor Software Developer Handbook

ID 743810
Date 10/06/2025
Public
Document Table of Contents

9.3.5.4.2. Example of Hardware Exception Handler

Once the polling mode is enabled in the JTAG UART following the instruction from the previous section, the following steps can help identify the cause of non-hardware exceptions like load, store misaligned, or others.

In the main.c file, add the following lines to include the library and the following instr_exception_handler() function.
#include <inttypes.h>
#include <sys/alt_exceptions.h> 
// Provides a way to register a custom instruction exception handler
alt_exception_result instr_exception_handler(alt_exception_cause cause,
    alt_u32 epc, alt_u32 tval) 
{
    printf("Instruction exception!\n");
    printf(" * cause: %d\n", cause);
    printf(" * epc:   0x%" PRIx32 "\n", epc);
    printf(" * tval:  0x%" PRIx32 "\n", tval);
 while (1) {};
    return NIOSV_EXCEPTION_RETURN_REISSUE_INST; // Should not be reached.
}