-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtimer.c
More file actions
80 lines (60 loc) · 1.31 KB
/
timer.c
File metadata and controls
80 lines (60 loc) · 1.31 KB
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
73
74
75
76
77
78
#include "timer.h"
#include "interrupt.h"
#include "cpu.h"
struct timer timer;
unsigned int time = 0;
unsigned int change = 0;
void setDiv(unsigned char address) {
address = 0;
timer.div = address; // setting div to anything makes it 0
}
unsigned int getDiv(void) {
return timer.div;
}
void setTima(unsigned char address) {
timer.tima = address;
}
unsigned int getTima(void) {
return timer.tima;
}
void setTma(unsigned char address) {
timer.tma = address;
}
unsigned int getTma(void) {
return timer.tma;
}
void setTac(unsigned char address) { // revisit this
int speeds[] = {1, 64, 16, 4};
timer.tac = address;
timer.started = address & 4;
timer.speed = speeds[address & 3];
}
unsigned int getTac(void) {
return timer.tac;
}
void tick(void) {
timer.tick += 1;
if (timer.tick == 16) {
timer.div += 1;
timer.tick = 0;
}
if (!timer.started)
return;
if (timer.tick == timer.speed) {
timer.tima += 1;
timer.tick = 0;
}
if (timer.tima == 0x100) {
interrupt.flags |= TIMER;
timer.tima = timer.tma;
}
}
void timerCycle(void) {
unsigned int delta = getCycles() - time;
time = getCycles();
change += delta * 4;
if (change >= 16) {
tick();
change -= 16;
}
}