gbasys

view src/timer.c @ 7:72c6429ae953

changed all the copyright headers and added a README
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 18 Apr 2014 02:04:46 +0300
parents 875ef6085efc
children
line source
1 /*
2 gbasys - a gameboy advance hardware abstraction library
3 Copyright (C) 2004-2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <limits.h>
19 #include "intr.h"
20 #include "signal.h"
22 /* prescalar selection based on the system clock (16.78MHz) */
23 #define TIMER_CNTL_CLK 0
24 #define TIMER_CNTL_CLK64 1
25 #define TIMER_CNTL_CLK256 2
26 #define TIMER_CNTL_CLK1024 3
28 /* control bits */
29 #define TIMER_CNTL_COUNTUP 4
30 #define TIMER_CNTL_INTR 0x40
31 #define TIMER_CNTL_ENABLE 0x80
33 static void timer_intr_handler(void);
35 volatile static unsigned short *reg_timer[] = {(void*)0x4000100, (void*)0x4000104, (void*)0x4000108, (void*)0x400010c};
36 static unsigned short *reg_timer_cntl[] = {(void*)0x4000102, (void*)0x4000106, (void*)0x400010a, (void*)0x400010e};
38 static unsigned long milli_sec;
39 static unsigned long alarm_val;
41 void enable_timer(int timer) {
42 *reg_timer_cntl[timer] |= TIMER_CNTL_ENABLE;
43 }
45 void disable_timer(int timer) {
46 *reg_timer_cntl[timer] &= ~TIMER_CNTL_ENABLE;
47 }
49 void reset_msec_timer(void) {
50 *reg_timer_cntl[0] &= ~TIMER_CNTL_ENABLE;
51 interrupt(INTR_TIMER0, timer_intr_handler);
52 milli_sec = 0;
53 *reg_timer[0] = USHRT_MAX - 16779;
54 *reg_timer_cntl[0] = TIMER_CNTL_INTR | TIMER_CNTL_ENABLE;
55 unmask(INTR_TIMER0);
56 }
59 unsigned long get_millisec(void) {
60 return milli_sec;
61 }
63 unsigned int alarm(unsigned int seconds) {
64 unsigned int prev = alarm_val;
65 alarm_val = seconds * 1000;
66 }
68 static void timer_intr_handler(void) {
69 milli_sec++;
71 if(alarm_val > 0) {
72 if(!--alarm_val) raise(SIGALRM);
73 }
74 }