/*
    Timer Module

    Time measuring routines

*/
//* "Includes"
//#include <global.h>
//*
//* "Globals"
#include <clib/exec_protos.h>
#include <pragmas/exec_pragmas.h>
#include <exec/memory.h>
#include <devices/timer.h>
#include <clib/timer_protos.h>
#include <pragmas/timer_pragmas.h>

struct timerequest *TimeIO;
struct Library *TimerBase;
struct EClockVal *t1,*t2;
ULONG E_Freq;
//*
//* "InitTimer"
BOOL InitTimer(void) {

    TimeIO = (struct timerequest *)AllocMem(sizeof(struct timerequest), MEMF_CLEAR|MEMF_PUBLIC);
    if (!TimeIO) return FALSE;
    if (OpenDevice((STRPTR)"timer.device", UNIT_ECLOCK, (struct IORequest *)TimeIO, 0L))
        return FALSE;
    t1=AllocMem(sizeof(struct EClockVal), MEMF_CLEAR|MEMF_PUBLIC);
    if (!t1) return FALSE;
    t2=AllocMem(sizeof(struct EClockVal), MEMF_CLEAR|MEMF_PUBLIC);
    if (!t2) return FALSE;
    TimerBase = (struct Library *)(TimeIO->tr_node.io_Device);
    return TRUE;
}
//*
//* "CloseTimer"
void CloseTimer(void) {
    if (TimeIO) CloseDevice((struct IORequest *)TimeIO);
    if (t1) FreeMem(t1,sizeof(struct EClockVal));
    if (t2) FreeMem(t2,sizeof(struct EClockVal));
    if (TimeIO) FreeMem(TimeIO, sizeof(struct timerequest));
}
//*
//* "StartClock"
void StartClock(void) {
    E_Freq = ReadEClock(t1);
}
//*
//* "StopClock"
ULONG StopClock(void) {
    register ULONG frames;
    E_Freq = ReadEClock(t2);
    frames = t2->ev_lo - t1->ev_lo;
    if (frames==0) return 0;
    return E_Freq/frames;
}
//*

