pit and kernel ticks

This commit is contained in:
rami 2024-07-02 00:46:29 -04:00
parent 37019a53ea
commit 57a6d51e68
6 changed files with 24 additions and 8 deletions

View File

@ -20,7 +20,6 @@ ISO := Hazel.iso
CFLAGS := -ffreestanding -Wall -Wextra -Werror -I $(INCLUDEDIR) -I lib CFLAGS := -ffreestanding -Wall -Wextra -Werror -I $(INCLUDEDIR) -I lib
LDFLAGS := -ffreestanding -nostdlib -lgcc -T kernel/kernel.ld LDFLAGS := -ffreestanding -nostdlib -lgcc -T kernel/kernel.ld
QEMUFLAGS := -cdrom $(BUILDDIR)/$(ISO) \ QEMUFLAGS := -cdrom $(BUILDDIR)/$(ISO) \
-d int \
-serial stdio -serial stdio
$(BUILDDIR)/$(KIMG): $(KOBJ) $(LOBJ) $(BUILDDIR)/$(KIMG): $(KOBJ) $(LOBJ)

View File

@ -11,6 +11,7 @@ typedef struct {
multiboot_memory_map_t *mem_map; multiboot_memory_map_t *mem_map;
uint32_t mem_map_len; uint32_t mem_map_len;
log_method_t log_method; log_method_t log_method;
uint64_t ticks;
} kernel_ctx_t; } kernel_ctx_t;
#endif #endif

View File

@ -11,6 +11,10 @@
#define PIT_LOHIGH (3<<4) #define PIT_LOHIGH (3<<4)
#define PIT_CHAN0 0 #define PIT_CHAN0 0
#define PIT_FREQUENCY 1193182
#define DESIRED_FREQUENCY 100
#define DIVISOR (PIT_FREQUENCY/DESIRED_FREQUENCY)
void pit_init(void); void pit_init(void);
void pit_handler(void); void pit_handler(void);

View File

@ -19,6 +19,12 @@ void exception_handler(int_stack_frame_t r) {
r.eip, r.cs, r.eflags); r.eip, r.cs, r.eflags);
} }
void sleep(int delay) {
uint64_t end = ctx.ticks + delay;
while (ctx.ticks < end)
cpu_relax;
}
void kernel(multiboot_info_t *info) { void kernel(multiboot_info_t *info) {
if (!CHECK_FLAG(info->flags, 6)) goto halt; // Memory map if (!CHECK_FLAG(info->flags, 6)) goto halt; // Memory map
if (!CHECK_FLAG(info->flags, 12)) goto halt; // VBE data if (!CHECK_FLAG(info->flags, 12)) goto halt; // VBE data
@ -41,8 +47,9 @@ void kernel(multiboot_info_t *info) {
pic_remap(PIC_1_START, PIC_2_START); pic_remap(PIC_1_START, PIC_2_START);
asm volatile ("sti" ::); asm volatile ("sti" ::);
ctx.ticks = 0;
pit_init(); pit_init();
halt: halt:
asm volatile ("cli" ::);
for (;;) {} for (;;) {}
} }

View File

@ -26,6 +26,6 @@ void pic_remap(int pic1_start, int pic2_start) {
outb(PIC_2_DATA, 0x01); outb(PIC_2_DATA, 0x01);
// Unmask // Unmask
//outb(PIC_1_DATA, 0b11111101); outb(PIC_1_DATA, 0b11111110);
//outb(PIC_2_DATA, 0b11111101); outb(PIC_2_DATA, 0b11111110);
} }

View File

@ -1,14 +1,19 @@
#include <kernel/kernel.h>
#include <kernel/pit.h> #include <kernel/pit.h>
#include <kernel/io.h> #include <kernel/io.h>
#include <kernel/pic.h> #include <kernel/pic.h>
void pit_init(void) { extern kernel_ctx_t ctx;
outb(PIT_MODE_CMD, PIT_CHAN0 | PIT_LOHIGH | PIT_MODE0);
outb(PIT_CHAN0, 1); void pit_init(void) {
outb(PIT_CHAN0, 0); outb(PIT_MODE_CMD, PIT_CHAN0 | PIT_LOBYTE | PIT_MODE0);
outb(PIT_CHAN0_DATA, (uint8_t)(DIVISOR & 0xff));
} }
void pit_handler(void) { void pit_handler(void) {
ctx.ticks++;
outb(PIT_CHAN0_DATA, (uint8_t)(DIVISOR & 0xff));
pic_send_eoi(0); pic_send_eoi(0);
} }