/*-=-=-=-=-=-=-=-=-=-=-=- 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.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#ifdef SGI_CFG

#include <sys/time.h>

#include "../sys/rciext/rciioctl.h"
#include "../h/cdefs.h"
#include "../h/rciConfig.h"
#include "../h/rciTask.h"

extern int rciIsOpen;
extern int rciClockFd;

extern int rciCpu;
extern int rciSimOnly;

static rciTimeInitDone = -1;
static double clockRes;
static struct timeval starttime;

rciTimeInit (addr, res)
int *addr;
double res;
{
	/* Called by monitor clock control code */

	clockRes = res;
	rciTimeInitDone = rciCpu;
	if (rciSimOnly)
	 { getSystemTime (&starttime);
	 }
}

rciTime ()
{
	if (!rciSimOnly)
	 { if (rciTimeInitDone != rciCpu || !rciIsOpen)
	    { return (0);
	    }
	   else
	    { int tick;
	      ioctl (rciClockFd, RCIIOC_TIMER_GET, &tick);
	      return (tick*clockRes);
	    }
	 }
	else
	 { struct timeval tv;

	   getSystemTime (&tv);
	   tv.tv_sec -= starttime.tv_sec;
	   tv.tv_usec -= starttime.tv_usec;
	   return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
	 }
}

float rciTimeInterval()
{
	return (clockRes);
}

rciGetClock(num)
{
	int tick;

	if (rciSimOnly || !rciIsOpen)
	 { return (0);
	 }
	else
	 { ioctl (rciClockFd, RCIIOC_TIMER_GET, &tick);
	   return (tick);
	 }
}

rciSetClock(num, count)
int num, count;
{
	if (rciSimOnly || !rciIsOpen || num != 0)
	 { return (0);
	 }
	else
	 { ioctl (rciClockFd, RCIIOC_TIMER_SET, count);
	   return (1);
	 }
}

rciStartClock (incumbent, newRes, oldRes)
int incumbent;
float newRes, oldRes;
{
	int setcount;

	if (rciSimOnly || !rciIsOpen || incumbent != UNDEF)
	 { return (0);
	 }
	setcount = 1;
	ioctl (rciClockFd, RCIIOC_TIMER_SET, setcount);
	rciTimeInit (0, newRes);
	return (setcount);
}

rciExportClock (targetCpu, code)
int targetCpu, code;
{
}

rciImportClock (cpu, res, reInitialize)
int cpu;
double res;
int reInitialize;
{
}

#else /* !defined(SGI_CFG) */

static int keepRanlibHappy;

#endif
