aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/timer/timer.c
diff options
context:
space:
mode:
authorMarvin Borner2019-09-19 19:56:59 +0200
committerMarvin Borner2019-09-19 20:05:38 +0200
commit05e1fedcc9cd30d1a34a65e640da45e980b4f859 (patch)
tree9cfb7620907ac126f26cdfe9363cb73ed74ea179 /src/kernel/timer/timer.c
parentffd82e18b5259fab477ad375a7af8550fac526d8 (diff)
Moved source to kernel directory
Diffstat (limited to 'src/kernel/timer/timer.c')
-rw-r--r--src/kernel/timer/timer.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/kernel/timer/timer.c b/src/kernel/timer/timer.c
new file mode 100644
index 0000000..38f5be6
--- /dev/null
+++ b/src/kernel/timer/timer.c
@@ -0,0 +1,32 @@
+#include "../interrupts/interrupts.h"
+#include "../io/io.h"
+
+volatile unsigned int timer_ticks = 0;
+
+void timer_phase(int hz) {
+ int divisor = 1193180 / hz;
+ send(0x43, 0x36); // 01 10 11 0b // CTR, RW, MODE, BCD
+ send(0x40, divisor & 0xFF);
+ send(0x40, divisor >> 8);
+}
+
+// Executed 100 times per second
+void timer_handler(struct regs *r) {
+ timer_ticks++;
+}
+
+// "Delay" function with CPU sleep
+void timer_wait(int ticks) {
+ unsigned int eticks;
+
+ eticks = timer_ticks + ticks;
+ while (timer_ticks < eticks) {
+ __asm__ __volatile__ ("sti//hlt//cli");
+ }
+}
+
+// Install timer handler into IRQ0
+void timer_install() {
+ timer_phase(100);
+ irq_install_handler(0, timer_handler);
+} \ No newline at end of file