-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
72 lines (66 loc) · 1.36 KB
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "common.h"
#include "cpu.h"
#include "mem.h"
#include "interrupt.h"
unsigned char div_reg;
unsigned char timer_enabled;
unsigned char timer_input_clock_select;
unsigned short timer_counter;
unsigned char timer_modulo;
unsigned char timer_enabled;
unsigned char timer_input_clock_select;
unsigned int timer_cycles;
unsigned int div_cycles;
void increment_timer ( void )
{
if ( timer_counter > 0xff )
{
INTERRUPT(TIMER);
timer_counter = timer_modulo & 0xff;
}
else
{
timer_counter++;
}
}
void update_TIMA ( void )
{
if ( (timer_input_clock_select == 0) && (timer_cycles >= 1024) )
{
timer_cycles -= 1024;
increment_timer();
}
else if ( (timer_input_clock_select == 1) && (timer_cycles >= 16) )
{
timer_cycles -= 16;
increment_timer();
}
else if ( (timer_input_clock_select == 2) && (timer_cycles >= 64) )
{
timer_cycles -= 64;
increment_timer();
}
else if ( (timer_input_clock_select == 3) && (timer_cycles >= 256) )
{
timer_cycles -= 256;
increment_timer();
}
}
void update_DIV ( void )
{
div_cycles += cpu_cycles;
if ( div_cycles >= 256 )
{
div_cycles -= 256;
div_reg++;
}
}
void cycle_timer ( void )
{
update_TIMA();
update_DIV();
}
void init_timer ( void )
{
div_reg = 0;
}