/*
	MG2NMI.C

	BL15xx/MicroG2

	Show how to monitor NMI interrupt for power fail or brown out conditions.
*/

unsigned int tracknmi;
unsigned int trackold;

main(){
	printf( "Old tracknmi >> %u, Current tracknmi >> %u\n", trackold, tracknmi);
 
	if( trackold != tracknmi ){
		 trackold = tracknmi;   // if not the same, copy 
	}

	printf("Power Down and see the value of tracknmi changed!!!\n");

	while(1) {
		hitwd();
		runwatch();
	}
}

char dummy[24];            // create a dummy stack

#JUMP_VEC  NMI_VEC  myint
#asm
myint::
			ld    sp, dummy+24      ; reset stack pointer to top of dummy array
											; to prevent overwrite of user code/data
											
			; do whatever service within the allowable execution time

			ld    a, 0xcf
			out0  (PIOCB2),a
			ld    a, (PIOCB2Shadow)
			or    00001000b
			out0  (PIOCB2), a       ; make sure bit 3 is input
loop:
			call  hitwd             ; make sure no watchdog reset is generated
											; at low power
timeout:
			ld    hl, (tracknmi)    ; increment this variable for testing
			inc   hl
			ld    (tracknmi), hl    ; save
			in0   a, (PIODB2)       ; read NMI bit
			bit   3, a              ;
			jr    z, loop           ; waitout the brown out condition
			jp    timeout           ; use the watchdog timeout to debounce
											; the power fail state
#endasm        
/**/

