gbasys
view 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 source
1 /*
2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
4 This file is part of libgba, a library for GameBoy Advance development.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 #include <limits.h>
22 #include "intr.h"
23 #include "signal.h"
25 /* prescalar selection based on the system clock (16.78MHz) */
26 #define TIMER_CNTL_CLK 0
27 #define TIMER_CNTL_CLK64 1
28 #define TIMER_CNTL_CLK256 2
29 #define TIMER_CNTL_CLK1024 3
31 /* control bits */
32 #define TIMER_CNTL_COUNTUP 4
33 #define TIMER_CNTL_INTR 0x40
34 #define TIMER_CNTL_ENABLE 0x80
36 static void timer_intr_handler(void);
38 volatile static unsigned short *reg_timer[] = {(void*)0x4000100, (void*)0x4000104, (void*)0x4000108, (void*)0x400010c};
39 static unsigned short *reg_timer_cntl[] = {(void*)0x4000102, (void*)0x4000106, (void*)0x400010a, (void*)0x400010e};
41 static unsigned long milli_sec;
42 static unsigned long alarm_val;
44 void enable_timer(int timer) {
45 *reg_timer_cntl[timer] |= TIMER_CNTL_ENABLE;
46 }
48 void disable_timer(int timer) {
49 *reg_timer_cntl[timer] &= ~TIMER_CNTL_ENABLE;
50 }
52 void reset_msec_timer(void) {
53 *reg_timer_cntl[0] &= ~TIMER_CNTL_ENABLE;
54 interrupt(INTR_TIMER0, timer_intr_handler);
55 milli_sec = 0;
56 *reg_timer[0] = USHRT_MAX - 16779;
57 *reg_timer_cntl[0] = TIMER_CNTL_INTR | TIMER_CNTL_ENABLE;
58 unmask(INTR_TIMER0);
59 }
62 unsigned long get_millisec(void) {
63 return milli_sec;
64 }
66 unsigned int alarm(unsigned int seconds) {
67 unsigned int prev = alarm_val;
68 alarm_val = seconds * 1000;
69 }
71 static void timer_intr_handler(void) {
72 milli_sec++;
74 if(alarm_val > 0) {
75 if(!--alarm_val) raise(SIGALRM);
76 }
77 }