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
79
80
81
82
83
84
85
86
87
|
#include <nds.h>
#include <time.h>
#include "clock.h"
#include "../bang.h"
// Uptime is the number of 1/256th second ticks since the emulator began.
static u32 uptime;
u32 get_uptime(void) { return uptime; }
void uptime_handler(void) { uptime++; }
// Check if any timer has expired.
bool check_timers(ClockDevice *clk) {
bool output = FALSE;
if (clk->t1.end && clk->t1.end <= uptime) {
clk->t1.end = 0;
output = TRUE;
}
if (clk->t2.end && clk->t2.end <= uptime) {
clk->t2.end = 0;
output = TRUE;
}
if (clk->t3.end && clk->t3.end <= uptime) {
clk->t3.end = 0;
output = TRUE;
}
if (clk->t4.end && clk->t4.end <= uptime) {
clk->t4.end = 0;
output = TRUE;
}
return output;
}
u8 get_timer_high(ClockTimer *t) {
if (t->end > uptime) {
t->read = t->end - uptime;
} else {
t->end = 0;
t->read = 0;
}
return HIGH(t->read);
}
u8 get_timer_low(ClockTimer *t) {
return LOW(t->read);
}
void set_timer_high(ClockTimer *t, u8 high) {
SET_HIGH(t->write, high);
}
void set_timer_low(ClockTimer *t, u8 low) {
SET_LOW(t->write, low);
if (t->write) {
t->end = uptime + t->write;
} else {
t->end = 0;
}
}
u8 get_timer1_high(ClockDevice *clock) { return get_timer_high(&clock->t1); }
u8 get_timer1_low( ClockDevice *clock) { return get_timer_low( &clock->t1); }
u8 get_timer2_high(ClockDevice *clock) { return get_timer_high(&clock->t2); }
u8 get_timer2_low( ClockDevice *clock) { return get_timer_low( &clock->t2); }
u8 get_timer3_high(ClockDevice *clock) { return get_timer_high(&clock->t3); }
u8 get_timer3_low( ClockDevice *clock) { return get_timer_low( &clock->t3); }
u8 get_timer4_high(ClockDevice *clock) { return get_timer_high(&clock->t4); }
u8 get_timer4_low( ClockDevice *clock) { return get_timer_low( &clock->t4); }
void set_timer1_high(ClockDevice *clock, u8 high) { set_timer_high(&clock->t1, high); }
void set_timer1_low( ClockDevice *clock, u8 low) { set_timer_low( &clock->t1, low); }
void set_timer2_high(ClockDevice *clock, u8 high) { set_timer_high(&clock->t2, high); }
void set_timer2_low( ClockDevice *clock, u8 low) { set_timer_low( &clock->t2, low); }
void set_timer3_high(ClockDevice *clock, u8 high) { set_timer_high(&clock->t3, high); }
void set_timer3_low( ClockDevice *clock, u8 low) { set_timer_low( &clock->t3, low); }
void set_timer4_high(ClockDevice *clock, u8 high) { set_timer_high(&clock->t4, high); }
void set_timer4_low( ClockDevice *clock, u8 low) { set_timer_low( &clock->t4, low); }
void init_clock(void) {
// Start a 256Hz timer to increment the uptime value.
timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(256), uptime_handler);
}
struct tm* get_datetime(void) {
time_t timestamp = time(NULL);
struct tm* datetime = localtime(×tamp);
return datetime;
}
|