diff --git a/headers/drivers.h b/headers/drivers.h index 2038849..9b467a8 100644 --- a/headers/drivers.h +++ b/headers/drivers.h @@ -6,4 +6,4 @@ void load_drivers(void); void keyboard_handler(struct registers *regs); -void clock_handler(struct registers *regs); +void clock_init(struct registers *regs); diff --git a/headers/interrupts.h b/headers/interrupts.h index 324faa9..e7bb3a5 100644 --- a/headers/interrupts.h +++ b/headers/interrupts.h @@ -18,3 +18,13 @@ typedef void (*isr_t)(struct registers *); void isr_handler(struct registers *regs); void pic_send_eoi(uint8_t irq); void register_interrupt_handler(int index, isr_t handler); + +static inline void cli(void) +{ + __asm__ volatile("cli"); +} + +static inline void sti(void) +{ + __asm__ volatile("sti"); +} \ No newline at end of file diff --git a/headers/time.h b/headers/time.h new file mode 100644 index 0000000..5701514 --- /dev/null +++ b/headers/time.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +void sleep(uint64_t delay); \ No newline at end of file diff --git a/src/drivers/clock.c b/src/drivers/clock.c index 62fcf9c..8f7bb0b 100644 --- a/src/drivers/clock.c +++ b/src/drivers/clock.c @@ -1,6 +1,47 @@ -#include "interrupts.h" +#include -void clock_handler(struct registers *regs) +#include "debug.h" +#include "drivers.h" +#include "interrupts.h" +#include "kprintf.h" +#include "memory.h" +#include "sys/io.h" + +#define PIT_CHANNEL0 0x40 +#define PIT_FREQUENCY 1193182 +#define PIT_COUNT (65535 / 2) +#define DELAY (1000 / (PIT_FREQUENCY / PIT_COUNT)) + +uint32_t counter = 0; + +static void clock_handler(struct registers *regs); + +static void set_pit_count(unsigned count) +{ + cli(); + + outb(0x40, count & 0xFF); // Low byte + outb(0x40, (count & 0xFF00) >> 8); // High byte + + sti(); +} + +void clock_init(struct registers *regs) { (void)regs; + set_pit_count(PIT_COUNT); + register_interrupt_handler(0, clock_handler); } + +static void clock_handler(struct registers *regs) +{ + (void)regs; + counter--; +} + +void sleep(uint64_t delay) +{ + counter = delay / DELAY; + while (counter) + ; +} \ No newline at end of file diff --git a/src/drivers/drivers.c b/src/drivers/drivers.c index e4cd6d2..e392c01 100644 --- a/src/drivers/drivers.c +++ b/src/drivers/drivers.c @@ -3,6 +3,6 @@ void load_drivers(void) { - register_interrupt_handler(0, clock_handler); + register_interrupt_handler(0, clock_init); register_interrupt_handler(1, keyboard_handler); }