/*
	SC1NMI.C

	CM71xx/Z1 and CM72xx/Z2

	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
loop:
			call	hitwd					; make sure no watchdog reset is generated
											; at low power
timeout:
			ld		bc, NMI				; 
			in		a, (c)				; read NMI bit
			bit	0, a					; test NMI bit
			jr		nz, test				; waitout the brown out condition
											; NMI bit is high when PFO is active (low).
			jp		timeout				; use the watchdog timeout to debounce
											; the power fail state

test:		
			ld		hl, (tracknmi)		; increment this variable for testing
			inc	hl
			ld		(tracknmi), hl		; save
			jp		loop
#endasm		

/**/

