The Altera HAL is already setup to use this custom instruction if present (assuming you are using Nios II 5.0 or later).
Look for# ifdef ALT_CI_EXCEPTION_VECTOR in components/altera_hal/HAL/src/alt_irq_handler.c and# ifdef ALT_CI_EXCEPTION_VECTOR_N in components/altera_nios2/HAL/src/alt_irq_entry.S. These component directories are under your Nios II kit (e.g.
c:\altera\kits\nios2\component). You might want to look at your objdump and make sure your ELF contains the custom instructions in the HAL exception handler code (should show up twice in your ELF). To generate an objdump that only shows the assembly instructions present in your ELF (and doesn't mix in the C source), use:
nios2-elf-objdump -sdtx *.elf >*.txt
BTW, your system.h should look like this when the custom instruction is present:
/*
* custom instruction macros
*
*/
# define ALT_CI_EXCEPTION_VECTOR_N 0x00000000# define ALT_CI_EXCEPTION_VECTOR __builtin_custom_in(ALT_CI_EXCEPTION_VECTOR_N)
The custom instruction returns a negative value when there are no interrupts pending. Otherwise, it returns the value of the highest priority interrupt (0 is highest priority) multipled by 8 (to properly index into the HAL ISR table).