aboutsummaryrefslogtreecommitdiff
path: root/arm9/source/devices/clock.c
diff options
context:
space:
mode:
authorBen Bridle <ben@derelict.engineering>2025-09-19 13:17:14 +1200
committerBen Bridle <ben@derelict.engineering>2025-09-19 13:32:32 +1200
commitbb1aa5958d1b67707dcf0f6b08bfaf0b408bd46e (patch)
treeb26d07ed58aaf7a5230fc3e28c103d616abfa9b8 /arm9/source/devices/clock.c
parent9612c307f00c4313d73fe0c3a86c05c8d8cd514e (diff)
downloadbedrock-nds-bb1aa5958d1b67707dcf0f6b08bfaf0b408bd46e.zip
Massive rewrite
This commit rewrites the emulator halfway from scratch to make it easier to change and maintain in the future. The emulator core was rewritten to adhere to the released Bedrock specification (earlier versions implemented an older prototype specification, which is no longer relevant). This commit also adds proper support for running multiple concurrent Bedrock instances. This was previously supported in a limited manner for the on-screen keyboard, but now works for any regular program as well, with switching being performed by pressing the L or R bumper buttons. This is disabled by default, as programs will still need to be baked into the emulator and hand-loaded.
Diffstat (limited to 'arm9/source/devices/clock.c')
-rw-r--r--arm9/source/devices/clock.c101
1 files changed, 65 insertions, 36 deletions
diff --git a/arm9/source/devices/clock.c b/arm9/source/devices/clock.c
index 6ecee1d..c05ba2b 100644
--- a/arm9/source/devices/clock.c
+++ b/arm9/source/devices/clock.c
@@ -1,55 +1,84 @@
-#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.
+
+/*
+TODO: Add functions to set the system time and date.
+*/
+
+
+// ------ UPTIME AND DATETIME --------------------------------------------------
+
+// Uptime is the number of 1/256th second ticks elapsed since the emulator began.
static u32 uptime;
-u32 get_uptime(void) { return uptime; }
+
+// Increment the local uptime value.
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;
+// Configure timer 0 to increment the local uptime value every tick.
+void init_nds_clock(void) {
+ // Start a 256Hz timer to increment the uptime value.
+ timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(256), uptime_handler);
}
-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);
+// Return the current time and date.
+struct tm* get_datetime(void) {
+ time_t timestamp = time(NULL);
+ struct tm* datetime = localtime(&timestamp);
+ return datetime;
}
-u8 get_timer_low(ClockTimer *t) {
- return LOW(t->read);
+
+// ------ TIMERS ---------------------------------------------------------------
+
+// Reset a clock timer.
+void timer_reset(ClockTimer *timer) {
+ timer->end = 0;
+ timer->read = 0;
+ timer->write = 0;
}
-void set_timer_high(ClockTimer *t, u8 high) {
- SET_HIGH(t->write, high);
+// Update the cached read value of a timer.
+void timer_read(ClockTimer *timer) {
+ if (timer->end > uptime) {
+ timer->read = timer->end - uptime;
+ } else {
+ timer->read = 0;
+ timer->end = 0;
+ }
}
-void set_timer_low(ClockTimer *t, u8 low) {
- SET_LOW(t->write, low);
- if (t->write) {
- t->end = uptime + t->write;
+
+// Update the value of a timer using the cached write value.
+void timer_write(ClockTimer *timer) {
+ if (timer->write) {
+ timer->end = uptime + timer->write;
} else {
- t->end = 0;
+ timer->end = 0;
}
}
-void init_clock(void) {
- // Start a 256Hz timer to increment the uptime value.
- timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(256), uptime_handler);
+
+// ------ CLOCK ----------------------------------------------------------------
+
+// Reset the clock device.
+void clock_reset(ClockDevice *clock) {
+ clock->start = uptime;
+ timer_reset(&clock->t1);
+ timer_reset(&clock->t2);
+ timer_reset(&clock->t3);
+ timer_reset(&clock->t4);
}
-struct tm* get_datetime(void) {
- time_t timestamp = time(NULL);
- struct tm* datetime = localtime(&timestamp);
- return datetime;
+// Update the cached uptime value.
+void clock_uptime_read(ClockDevice *clock) {
+ clock->uptime = uptime - clock->start;
+}
+
+// Return true if any timer has expired.
+bool clock_check_timers(ClockDevice *clock) {
+ bool output = false;
+ if (clock->t1.end && clock->t1.end <= uptime) { clock->t1.end = 0; output = true; }
+ if (clock->t2.end && clock->t2.end <= uptime) { clock->t2.end = 0; output = true; }
+ if (clock->t3.end && clock->t3.end <= uptime) { clock->t3.end = 0; output = true; }
+ if (clock->t4.end && clock->t4.end <= uptime) { clock->t4.end = 0; output = true; }
+ return output;
}