/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1991 by John E. Lloyd.				    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/* Control level code for the test program 'wctest' */

#include "../h/rci.h"
#include "../h/timeval.h"
#include "wctest.h"

#define VEM_TRACE	1

struct timeval rciWallClock();

controlFxn(td, arg)
RCI_DESC *td;
int arg;
{
	WC_BLK *wbk;
	struct timeval tv1, tv2, tv3;
	unsigned char oldset;
	static int lasttime = 0;
	int k;

	if ((wbk = (WC_BLK*)getMemByAddr(arg)) == NULL)
	 { rciAbort ("controlFxn() -- can't find object\n");
	   return;
	 }

#if VEM_TRACE
	*(unsigned char *)(rciBus+0x1d08f) = 0x10;
#endif
	tv1 = rciWallClock();
	for (k=0; k<wbk->delay; k++)
	 {
	 }
#if VEM_TRACE
	*(unsigned char *)(rciBus+0x1d08f) = 0x0;
#endif
	tv2 = rciWallClock();
	tv3 = diffTimeval (tv2, tv1);

	if (wbk->count == 0)
	 { wbk->tvmin = wbk->tvmax = tv3;
	 }
	if (timercmp (&tv3, &wbk->tvmax, >))
	 { wbk->tvmax = tv3;
	 }
	if (timercmp (&tv3, &wbk->tvmin, <))
	 { wbk->tvmin = tv3;
	 }
	wbk->tvsum = addTimeval(wbk->tvsum, tv3);
	if (rciTime() - lasttime >= 1000)
	 { lasttime = rciTime();
	   rciPrintf ("%d.%06d\n", tv3.tv_sec, tv3.tv_usec);
	 }	
	wbk->count++;
}
