User guide

Sometimes during development work it is necessary to install exception handlers into the vectors directly from the
main application. As a result, the required instruction encoding must be written to the appropriate vector address.
This can be done for both the branch and the load pc method of reaching the handler.
Branch method
The required instruction can be constructed as follows:
1. Take the address of the exception handler.
2. Subtract the address of the corresponding vector.
3. Subtract 0x8 to allow for prefetching.
4. Shift the result to the right by two to give a word offset, rather than a byte offset.
5. Test that the top eight bits of this are clear, to ensure that the result is only 24 bits long (because the offset for
the branch is limited to this).
6. Logically OR this with 0xEA000000 (the opcode for the Branch instruction) to produce the value to be placed in
the vector.
Example 5-3 shows a C function that implements this algorithm. It takes the following arguments:
the address of the handler
the address of the vector in which the handler is to be to installed.
The function can install the handler and return the original contents of the vector. This result can be used to create a
chain of handlers for a particular exception. See Chaining exception handlers for further details.
Example 5-3
unsigned Install_Handler (unsigned *handlerloc, unsigned *vector)
/* Updates contents of 'vector' to contain LDR pc,,[pc,#offset] */
/* instruction to cause long branch to address in handlerloc */
/* Function return value is original contents of 'vector'.*/
{ unsigned vec, oldvec;
vec = *handlerloc - (unsigned)vector - 0x8;
if ((vec & 0xFFFFF000) != 0)
{
/* diagnose the fault */
exit (1);
}
vec = 0xE59FF000 | vec;
oldvec = *vector;
*vector = vec;
return (oldvec);
}
The following code calls this to install an IRQ handler:
unsigned *irqvec = (unsigned *)0x18;
Install_Handler ((unsigned)IRQHandler, irqvec);
In this case, the returned, original contents of the IRQ vector are discarded.
Load pc method
The required instruction can be constructed as follows:
1. Take the address of the word containing the address of the exception handler.
2. Subtract the address of the corresponding vector.
3. Subtract 0x8 to allow for prefetching.
4. Check that the result can be represented in 12 bits.
5. Logically OR this with 0xe59FF000 (the opcode for LDR pc, [pc,#offset]) to produce the value to be
placed in the vector.
6. Put the address of the handler into the storage location.
Example 5-4 shows a C routine that implements this method.
Example 5-4
unsigned Install_Handler (unsigned location, unsigned *vector)
/* Updates contents of 'vector' to contain LDR pc, [pc, #offset] */
Handling Processor Exceptions
Copyright ?1999 2001 ARM Limited 5-6