Nios® V Processor Software Developer Handbook

ID 743810
Date 7/08/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

9.8.4.3. Adding Custom Exception Handler

Once the polling mode is enabled in the JTAG UART following the instruction from the previous section, the following steps can help with seeing the cause of non-interrupt related exceptions like load or store misaligned, etc. In the main.c file, add the following lines to include the library and 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.
}
Add the following section to register this new exception handling function in the main() function, right above the code which causes the exception.
int main(void) {
    alt_instruction_exception_register (instr_exception_handler);  // Register custom instruction exception handler.
 
    printf("Hello world!\n");
    function_which_causes_the_exception();
   return 0;
}