/*
	z104nmi.c

   Sample program showing the use of the NMI bit.
   
   Monitor value of tracknmi with the runwatch window. When the power is
   low, the NMI becomes active and tracknmi is incremented. Turn off the
   power to trigger the NMI.
*/

#use "default.h"

#if (BOARD_TYPE!=Z104ISA)
#fatal "This program only runs on the Z104 or ZISA boards."
#endif

unsigned int tracknmi;

main()
{
    printf("NMI testing count >> %u\n", tracknmi);
    printf("Turn off power and tracknmi value will change when run again.\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	6, a					; test NMI bit
			jr		z, test				; waitout the brown out condition
											; NMI bit is low 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		

