From 05e1fedcc9cd30d1a34a65e640da45e980b4f859 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 19 Sep 2019 19:56:59 +0200 Subject: Moved source to kernel directory --- src/kernel/timer/timer.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/kernel/timer/timer.c (limited to 'src/kernel/timer/timer.c') 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 -- cgit v1.2.3