/*
	This program demonstrates how to set up PI-06 and PI-07 to generate
	level-based interrupts for interrupt level 0 and interrupt level 1
	respectively. The program also demonstrates how the interrupts are
	captured by interrupt routines.

	Run this program and ground PI-06 and PI-07 momentarily. The program
	should indicate that interrupt 0 and interrupt 1 were intercepted.

	NOTE: interrupt 0 and interrupt 1 are level sensitive, so do not hold
	these interrupt lines low for long periods of time. Otherwise, the
	respective interrupt handlers will consume all the processing resources,
	causing latency problems for other interrupts and wasting CPU processing
	power.
*/

unsigned Int0Cnt;		//	counter for interrupt 0
unsigned Int1Cnt;		//	counter for interrupt 1

//	The following defines the ISR (interrupt service routine) for interrupt 0.
//	The ISR increments the counter.
//	Note the use of "interrupt" and "reti" keywords.

interrupt reti int Int0ISR() {
	++Int0Cnt;
}

//	The following line directs the interrupt vector of interrupt 0 to the ISR
//	just defined.

#JUMP_VEC RST38_VEC Int0ISR

//	The following defines the ISR for interrupt 1.
//	The ISR increments the counter for interrupt 1.
//	Note the use of "interrupt" but not "reti" keywords.

interrupt int Int1ISR() {
	++Int1Cnt;
}

//	The following line directs the interrupt vector of interrupt 1 to the ISR
//	just defined.

#INT_VEC INT1_VEC Int1ISR

main() {
	auto unsigned dupInt1Cnt, dupInt0Cnt;

	_GLOBAL_INIT();	//	initializes whatever has to be initialized
	dupInt1Cnt = dupInt0Cnt = Int0Cnt = Int1Cnt = 0;	//	initialize counters
#asm xmemok
	im	1	;	change interrupt 0 mode to mode 1 (same as reset 38h)
#endasm
	outport(ITC,inport(ITC) | 3);		//	enable int0 and int1
	printf("Ground IN6 and IN7 momentarily\n");
	while (1) {								//	do this indefinitely
		costate {
			waitfor(dupInt0Cnt != Int0Cnt);	//	wait for counter to change value
			printf("Another Int0 happened.\n");	//	then print message
			dupInt0Cnt = Int0Cnt;				//	reset duplicate counter
		}
		costate {
			waitfor(dupInt1Cnt != Int1Cnt);	//	wait for counter to change value
			printf("Another int1 happened.\n");	//	then print message
			dupInt1Cnt = Int1Cnt;				//	reset duplicate counter
		}
		hitwd();
	}
}