/*
	FASTCALL.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 the virtual driver's
	"fastcall". The fastcall execution thread is called
	by the virtual driver 5 - 1280 times per second. The user
	must define the macro VD_FASTCAll to 1 and initialize
   the virtual	driver.

	vd_initquickloop(N) sets the calling interval in units of
	virtual driver clock ticks. The user defined function
	vd_quick_loop() is called every Nth tick. The fastest
   speed is N = 1, the slowest speed is N = 255.

	The fastcall can also be used with the RTK or SRTK.
*/

#define VD_FASTCALL 1					//  for fastcall
#use vdriver.lib     					// Required 

unsigned counter,value,DONE;

main(){									
							
	counter  = 0;
	value =   50; 
	DONE =   0; 

	VdInit();							  // need virtual driver
	vd_initquickloop(255);			  // begin fastcall at 5Hz
	
	while(!DONE){

		// printf is relatively slow, but the quick loop
		// will preempt it
			goxy(0,1);
			printf("c = %05u \n",counter);

		// automatic termination after 24 s
		costate{
			waitfor(DelaySec(22));	
			vd_initquickloop(0);
			goxy(0,2);
			printf("fastcall shut off");

			waitfor(DelaySec(2));	
			DONE = 1;
		}
	}
}

void vd_quick_loop(){

	counter++;

	 // change speed on the fly
	 // toggle between 5 and 1280 Hz
	if(counter == value){
		if(value==50){
			value = 10000;
			vd_initquickloop(1);		// switch to 1280Hz      
		}
		else{
			value = 50;
			vd_initquickloop(255);	// slow down
		}
		counter  = 0;
	}
}

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