Nios® V Processor Software Developer Handbook

ID 743810
Date 7/20/2023
Public

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

Document Table of Contents

8.7.4.2. Example of Instruction-Related 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-interrupt related exceptions like load, store misaligned, or others.
  1. 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.
    }
    
  2. Add the following section to register this new exception handling function in the main() function, right above the code which causes the exception.
    nt 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;
    }