aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-06-01 23:51:05 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-06-01 23:51:05 +0100
commit74daa7b891486fdd92cce0c67d076e29b93aeb1c (patch)
tree97ce20fb058a798c0d6c48250301fd56f6d9eba5
parentafc7ea1e4f667c16100677a3fe6d9b27278a8635 (diff)
downloadgold-mine-run-74daa7b891486fdd92cce0c67d076e29b93aeb1c.tar.gz
gold-mine-run-74daa7b891486fdd92cce0c67d076e29b93aeb1c.zip
Add timer to count ticks
-rw-r--r--src/main.c12
-rw-r--r--src/timer.c28
-rw-r--r--src/timer.h9
3 files changed, 48 insertions, 1 deletions
diff --git a/src/main.c b/src/main.c
index 2b01d3f..d83c18b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,14 +2,22 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <crt0.h>
#include <conio.h>
-#include "data.h"
+#include "timer.h"
#include "vga.h"
+#include "data.h"
+
+/* disable paging because our int handlers are written in C */
+int _crt0_startup_flags = _CRT0_FLAG_LOCK_MEMORY;
int main(int argc, char *argv[])
{
+ timer_init();
+ atexit(timer_free);
+
/* set VGA 320x200, 256 col */
set_mode(0x13);
@@ -36,5 +44,7 @@ int main(int argc, char *argv[])
set_mode(3);
close_framebuffer();
+ printf("tick is %li\n", ticks);
+
return 0;
}
diff --git a/src/timer.c b/src/timer.c
new file mode 100644
index 0000000..73675dc
--- /dev/null
+++ b/src/timer.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <go32.h>
+#include <dpmi.h>
+
+/* updates 18.2 times per second by default */
+volatile uint32_t ticks = 0;
+
+static _go32_dpmi_seginfo old_handler, new_handler;
+
+static void timer_handler()
+{
+ ticks++;
+}
+
+void 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 timer_free()
+{
+ if (_go32_dpmi_set_protected_mode_interrupt_vector(0x1c, &old_handler) == -1)
+ fprintf(stderr, "Failed to free the timer :(\n");
+}
diff --git a/src/timer.h b/src/timer.h
new file mode 100644
index 0000000..22ef6d2
--- /dev/null
+++ b/src/timer.h
@@ -0,0 +1,9 @@
+#ifndef _TIMER_H
+#define _TIMER_H
+
+extern volatile uint32_t ticks;
+
+void timer_init();
+void timer_free();
+
+#endif /* _TIMER_H */