gbasys

diff src/timer.c @ 0:875ef6085efc

gbasys mercurial repository
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 04 Mar 2012 04:04:25 +0200
parents
children 72c6429ae953
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/timer.c	Sun Mar 04 04:04:25 2012 +0200
     1.3 @@ -0,0 +1,77 @@
     1.4 +/*
     1.5 +Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
     1.6 +
     1.7 +This file is part of libgba, a library for GameBoy Advance development.
     1.8 +
     1.9 +This program is free software; you can redistribute it and/or modify
    1.10 +it under the terms of the GNU General Public License as published by
    1.11 +the Free Software Foundation; either version 2 of the License, or
    1.12 +(at your option) any later version.
    1.13 +
    1.14 +This program is distributed in the hope that it will be useful,
    1.15 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 +GNU General Public License for more details.
    1.18 +
    1.19 +You should have received a copy of the GNU General Public License
    1.20 +along with this program; if not, write to the Free Software
    1.21 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.22 +*/
    1.23 +
    1.24 +#include <limits.h>
    1.25 +#include "intr.h"
    1.26 +#include "signal.h"
    1.27 +
    1.28 +/* prescalar selection based on the system clock (16.78MHz) */
    1.29 +#define TIMER_CNTL_CLK		0
    1.30 +#define TIMER_CNTL_CLK64	1
    1.31 +#define TIMER_CNTL_CLK256	2
    1.32 +#define TIMER_CNTL_CLK1024	3
    1.33 +
    1.34 +/* control bits */
    1.35 +#define TIMER_CNTL_COUNTUP	4
    1.36 +#define TIMER_CNTL_INTR		0x40
    1.37 +#define TIMER_CNTL_ENABLE	0x80
    1.38 +
    1.39 +static void timer_intr_handler(void);
    1.40 +
    1.41 +volatile static unsigned short *reg_timer[] = {(void*)0x4000100, (void*)0x4000104, (void*)0x4000108, (void*)0x400010c};
    1.42 +static unsigned short *reg_timer_cntl[] = {(void*)0x4000102, (void*)0x4000106, (void*)0x400010a, (void*)0x400010e};
    1.43 +
    1.44 +static unsigned long milli_sec;
    1.45 +static unsigned long alarm_val;
    1.46 +
    1.47 +void enable_timer(int timer) {
    1.48 +	*reg_timer_cntl[timer] |= TIMER_CNTL_ENABLE;
    1.49 +}
    1.50 +
    1.51 +void disable_timer(int timer) {
    1.52 +	*reg_timer_cntl[timer] &= ~TIMER_CNTL_ENABLE;
    1.53 +}
    1.54 +
    1.55 +void reset_msec_timer(void) {
    1.56 +	*reg_timer_cntl[0] &= ~TIMER_CNTL_ENABLE;
    1.57 +	interrupt(INTR_TIMER0, timer_intr_handler);
    1.58 +	milli_sec = 0;
    1.59 +	*reg_timer[0] = USHRT_MAX - 16779;
    1.60 +	*reg_timer_cntl[0] = TIMER_CNTL_INTR | TIMER_CNTL_ENABLE;
    1.61 +	unmask(INTR_TIMER0);
    1.62 +}
    1.63 +
    1.64 +
    1.65 +unsigned long get_millisec(void) {
    1.66 +	return milli_sec;
    1.67 +}
    1.68 +
    1.69 +unsigned int alarm(unsigned int seconds) {
    1.70 +	unsigned int prev = alarm_val;
    1.71 +	alarm_val = seconds * 1000;
    1.72 +}
    1.73 +
    1.74 +static void timer_intr_handler(void) {
    1.75 +	milli_sec++;
    1.76 +
    1.77 +	if(alarm_val > 0) {
    1.78 +		if(!--alarm_val) raise(SIGALRM);
    1.79 +	}
    1.80 +}