aboutsummaryrefslogtreecommitdiff
path: root/src/timer.c
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-08-28 15:16:12 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-08-28 15:30:25 +0100
commite35cff6d299a07d9b34f303717083a9299a37e82 (patch)
tree7204099ad4978dfc67e04bc11df29d0f366af851 /src/timer.c
downloaduboxlib-dos-e35cff6d299a07d9b34f303717083a9299a37e82.tar.gz
uboxlib-dos-e35cff6d299a07d9b34f303717083a9299a37e82.zip
Initial import
Diffstat (limited to 'src/timer.c')
-rw-r--r--src/timer.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/timer.c b/src/timer.c
new file mode 100644
index 0000000..a55d0c0
--- /dev/null
+++ b/src/timer.c
@@ -0,0 +1,78 @@
+#include <stdint.h>
+#include <go32.h>
+#include <dpmi.h>
+#include <stddef.h>
+
+volatile uint32_t ubox_ticks = 0;
+volatile uint32_t clock = 0;
+volatile uint8_t clock_secs = 0;
+volatile uint8_t clock_enabled = 0;
+volatile uint8_t *clock_updated = NULL;
+volatile void (*user_fn)(void) = NULL;
+
+static _go32_dpmi_seginfo old_handler, new_handler;
+
+static void timer_handler()
+{
+ ubox_ticks++;
+
+ if (clock_enabled)
+ {
+ clock += 5494;
+ if (clock > 100000)
+ {
+ clock -= 100000;
+ if (clock_secs)
+ {
+ clock_secs--;
+ if (clock_updated)
+ *clock_updated = 1;
+ }
+ }
+ }
+
+ if (user_fn)
+ user_fn();
+}
+
+void ubox_timer_init()
+{
+ _go32_dpmi_get_protected_mode_interrupt_vector(0x1c, &old_handler);
+ new_handler.pm_offset = (unsigned long)timer_handler;
+ new_handler.pm_selector = _go32_my_cs();
+ _go32_dpmi_chain_protected_mode_interrupt_vector(0x1c, &new_handler);
+}
+
+void ubox_timer_user_fn(void (*fn)(void))
+{
+ user_fn = fn;
+}
+
+void ubox_timer_free()
+{
+ _go32_dpmi_set_protected_mode_interrupt_vector(0x1c, &old_handler);
+}
+
+void ubox_timer_start(uint8_t secs, volatile uint8_t *updated)
+{
+ *updated = 0;
+ clock_updated = updated;
+ clock_secs = secs;
+ clock_enabled = 1;
+}
+
+uint8_t ubox_timer_value()
+{
+ *clock_updated = 0;
+ return clock_secs;
+}
+
+void ubox_timer_stop()
+{
+ clock_enabled = 0;
+}
+
+void ubox_timer_resume()
+{
+ clock_enabled = 1;
+}