/*
**
** $VER: timer.c 0.6 (05.11.02)
**
** Description
**		Timer utility fonctions
**
** (C) Copyright 1999 Paul Hill
** (C) Copyright 2002 Alexandre Balaban
**
** $HISTORY :
**		- 0.6, 05.11.02 : Added pack pragma directives dor PPC compilation
**		- 0.5, 04.11.02 : Improved PPC includes
**		- 0.4, 03.10.02 : Include PPC compilation improvements by Steffen Haeuser
**		- 0.3, 29.08.02 : Add WaitIO() in closetimer
**		- 0.2, 25.07.02 : Cleanning by Alex
**		- 0.1, 27.02.99 : Initial version by Paul Hill
*/

#pragma pack(2)

// [SHA, 03/10/2002 : PPC compilation improvement]
#ifdef __PPC__
#include </ADE/os-includeppc/proto/exec.h>
#include </ADE/os-includeppc/proto/timer.h>
#include <clib/alib_protos.h>
#else
#include <proto/exec.h>
#include <proto/timer.h>
#include <proto/alib.h>
#endif // __PPC__
// [END SHA, 03/10/2002]

#include <devices/timer.h>
#include <sys/time.h>

#pragma pack()

#include <stdio.h>

#include "timer.h"

struct timerequest *TimerIO=NULL;
struct MsgPort *TimerMP=NULL;

int opentimer( void )
{
	int rc=0;

	if( ( TimerMP = CreatePort( 0, 0 ) ) )
	{
		if( ( TimerIO = (struct timerequest *) CreateExtIO(TimerMP,sizeof(struct timerequest) ) ) )
		{
			/* Open the device once */
			if (!(OpenDevice( TIMERNAME, UNIT_VBLANK,(struct IORequest *) TimerIO, 0L)))
			{
				rc = 1;
			}
		}
	}
	return rc;
}


void closetimer( void )
{
	if (TimerIO)
	{
		/* Delete any pending timer requests */
		if (!(CheckIO((struct IORequest *)TimerIO))) AbortIO((struct IORequest *)TimerIO);

        WaitIO( (struct IORequest*) TimerIO );

		CloseDevice((struct IORequest *) TimerIO);
		DeleteExtIO((struct IORequest *) TimerIO);
	}

	if (TimerMP)
	{
		DeletePort(TimerMP);
	}
}
