add: sleep

This commit is contained in:
Starnakin 2025-01-08 16:38:02 +01:00
parent bab3069152
commit 916e8b6f19
5 changed files with 60 additions and 4 deletions

View File

@ -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);

View File

@ -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");
}

5
headers/time.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <stdint.h>
void sleep(uint64_t delay);

View File

@ -1,6 +1,47 @@
#include "interrupts.h"
#include <stdint.h>
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)
;
}

View File

@ -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);
}