5.10.2.4 Status Register Preservation

The -mresetbits option (see Resetbits Option) preserves some of the bits in the STATUS register before they are clobbered by the remainder of the program. The state of the bits within this saved register can then be examined at any subsequent time to determine the cause of Reset. This option is not available when compiling for PIC18 devices.

The STATUS register is saved to an 8-bit wide assembly variable ___resetbits, although any bank selection bits in the register (if present for the target device) might not be accurately preserved. The compiler also defines the assembly symbols ___powerdown and ___timeout to represent the Power-down (PD) and Time-out (TO) bits within the STATUS register and which can be used in assembly code, if required. Check the runtime startup file startup.s to see these definitions and the compiler-generated code which stores the register.

The above symbols can also be accessed from C code once <xc.h> has been included. Note that the equivalent C identifiers will use just two leading underscore characters, e.g. __resetbits. See Equivalent Assembly Symbols for more details of symbol mapping. The following Mid-range code example checks the state of the saved bits at the beginning of main() before proceeding. To determine the exact cause of Reset, you might also need to check the state of bits in other registers (such as PCON or PCON0). Check your device data sheet for all the recorded causes of Reset.
#include <xc.h>

int
main(void)
{
  // how did we get here?
  if(__timeout == 0 && __powerdown == 0)
    handleWDT_timeout();	// WDT wake-up from sleep
  // proceed with remaining code
}

The compiler will detect the usage of the above symbols in your code and automatically enable the -mresetbits option, if they are present. You may choose to enable this feature manually, if desired.