diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/drivers/timer.c | 39 | ||||
-rw-r--r-- | src/inc/timer.h | 10 |
2 files changed, 49 insertions, 0 deletions
diff --git a/src/drivers/timer.c b/src/drivers/timer.c new file mode 100644 index 0000000..922b385 --- /dev/null +++ b/src/drivers/timer.c @@ -0,0 +1,39 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#include <cpu.h> +#include <def.h> +#include <interrupts.h> + +static u32 timer_ticks = 0; + +void timer_phase(int hz) +{ + int divisor = 3579545 / 3 / hz; + outb(0x43, 0x36); // 01 10 11 0b // CTR, RW, MODE, BCD + outb(0x40, divisor & 0xFF); + outb(0x40, divisor >> 8); +} + +// Executed 1000 times per second +void timer_handler() +{ + timer_ticks++; +} + +// "Delay" function with CPU sleep +void timer_wait(u32 ticks) +{ + u32 eticks; + + eticks = timer_ticks + ticks; + while (timer_ticks < eticks) { + __asm__("sti//hlt//cli"); + } +} + +// Install timer handler into IRQ0 +void timer_install() +{ + /* timer_phase(1000); */ + irq_install_handler(0, timer_handler); +} diff --git a/src/inc/timer.h b/src/inc/timer.h new file mode 100644 index 0000000..4b2bb8c --- /dev/null +++ b/src/inc/timer.h @@ -0,0 +1,10 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef TIMER_H +#define TIMER_H + +#include <def.h> + +void timer_install(); + +#endif |