/* Simple timer code V1.01 28/8/04
   See timer.h for docs
   Copyright 2008 Jeffrey Lee
   This file is part of WOUM.
   WOUM is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   WOUM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with WOUM.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _TIMER_C
#define _TIMER_C

#include "kernel.h"
#include "swis.h"

#include "timer.h"

static timer timers[NUM_TIMERS];

static _kernel_swi_regs regs;

void timer_start(int n)
{
	if ((n < 0) || (n >= NUM_TIMERS))
		return;
	_kernel_swi(OS_ReadMonotonicTime,&regs,&regs);
	timers[n].stime = regs.r[0];
}

void timer_stop(int n)
{
	if ((n < 0) || (n >= NUM_TIMERS))
		return;
	_kernel_swi(OS_ReadMonotonicTime,&regs,&regs);
	timers[n].total += regs.r[0] - timers[n].stime;
}

int timer_report(int n)
{
	if ((n < 0) || (n >= NUM_TIMERS))
		return 0;
	return timers[n].total;
}

void timer_reset(int n)
{
	if ((n < 0) || (n >= NUM_TIMERS))
		return;
	timers[n].total = 0;
}

#endif
