/*
	COSTATE1.C

	Run on any Z-World board

   If using Dynamic C for DOS, make sure CPLC.LIB is
   commented out with an '*' and uncomment out VDRIVER.LIB
   in LIB.DIR.

	Demonstrates the use of Costatements for multiple execution
	threads.
*/

#use vdriver.lib     					// Required for Costatements

main(){

	int HCOUNT, LCOUNT, DONE, LOOP;
	HCOUNT = LCOUNT = DONE = LOOP = 0;

	VdInit();								// The virtual driver is needed for costatements

	printf("HCOUNT LCOUNT LOOP\n");

	while(!DONE){

		LOOP++;

		// Each costatement represents a separate execution
		// thread. The waitfors are like busy waits WITHIN 
		// each costatement, but the other costatements
		// DO execute while one costatement is waiting 

		costate{
			waitfor(DelayMs(500));
			goxy(0,1);  
			printf("%3d    %3d    %5d", HCOUNT, LCOUNT, LOOP);
		}

		costate{
			waitfor(  HCOUNT >= 999 );
			DONE = 1;
		}

		costate{
			waitfor(DelayMs(10));
			HCOUNT++;
		}

		costate{
			waitfor(DelayMs(100));
			LCOUNT++;
		}
	}
}

// position cursor at x,y
goxy(int x, int y)
{ 
    printf("\x1b=%c%c",x+32,y+32);
}
