#include "QPF.H"

#define TOCR	0xFFD80000
#define TSTR	0xFFD80004
#define TCOR0	0xFFD80008
#define TCNT0	0xFFD8000C
#define TCR0	0xFFD80010
#define TCOR1	0xFFD80014
#define TCNT1	0xFFD80018
#define TCR1	0xFFD8001C
#define IPRA	0xFFD00004

void InitTimerSH4(void)
{
	static int InitDone = 0;

	if (!InitDone) {
		InitDone = 1;
		*(unsigned char *)TSTR = 0x00;			/* Timer stop */
		*(unsigned char *)TOCR = 0x01;
		*(unsigned short *)TCR0 = 0x0000;		/* UNIE */
		*(unsigned long *)TCOR0 = 12500000/2;
		*(unsigned long *)TCNT0 = 12500000/2;
		*(unsigned short *)TCR1 = 0x0000;
		*(unsigned long *)TCOR1 = 0xffffffff;
		*(unsigned long *)TCNT1 = 0xffffffff;
		*(unsigned char *)TSTR = 0x03;			/* Timer start */
	}
}


int QueryPerformanceFrequency(LARGE_INTEGER *lpli)
{
	InitTimerSH4();
	lpli->LowPart = SEC_DEF;
/*	lpli->HighPart = 0;*/
	return 1;
}

int QueryPerformanceCounter(LARGE_INTEGER *lpli)
{
	lpli->LowPart = 0xFFFFFFFFUL - (*(unsigned long*)(TCNT1));
/*	lpli->HighPart = 0;*/
	return 1;
}

unsigned long timeGetTime(void)
{
	static unsigned long	t = 0;

	return t++;
}
