mirror of
https://github.com/Tom-on64/kern.git
synced 2026-11-03 18:22:22 +00:00
Implemented PIT
This commit is contained in:
+7
-9
@@ -75,28 +75,26 @@ int isr_init(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void isr_register_irq(uint8_t i, isr_handler_ptr handler) {
|
||||
void isr_register(uint8_t i, isr_handler_ptr handler) {
|
||||
if (i > 16) return; // Security
|
||||
irq_handlers[i] = handler;
|
||||
}
|
||||
|
||||
void isr_send_eoi(uint8_t irq) {
|
||||
if (irq >= 8) {
|
||||
outb(0xa0, 0x20);
|
||||
}
|
||||
if (irq >= 8) outb(0xA0, 0x20);
|
||||
outb(0x20, 0x20);
|
||||
}
|
||||
|
||||
// TODO: Write fault handlers and make this more robust
|
||||
int isr_handle_interrupt(struct isr_int_frame iframe) {
|
||||
if (iframe.interrupt < 32) {
|
||||
// ISRs 0-31 - Exceptions
|
||||
panic(exceptions[iframe.error]);
|
||||
panic(exceptions[iframe.interrupt]);
|
||||
} else if (iframe.interrupt >= 32 && iframe.interrupt < 48) {
|
||||
// ISRs 32-47 - Hardware interrupts
|
||||
uint8_t irq = iframe.interrupt - 32;
|
||||
if (irq_handlers[irq]) {
|
||||
irq_handlers[irq](&iframe);
|
||||
isr_send_eoi(irq);
|
||||
}
|
||||
if (irq_handlers[irq]) irq_handlers[irq](&iframe);
|
||||
isr_send_eoi(irq);
|
||||
} else if (iframe.interrupt == 128) {
|
||||
// ISR 128 - System call
|
||||
return syscall_handler(&iframe);
|
||||
|
||||
+7
-2
@@ -2,6 +2,7 @@
|
||||
#include <kernel.h>
|
||||
#include <paging.h>
|
||||
#include <serial.h>
|
||||
#include <timer.h>
|
||||
#include <gdt.h>
|
||||
#include <isr.h>
|
||||
#include <pmm.h>
|
||||
@@ -29,13 +30,17 @@ void kmain(void* ptr, uint32_t magic) {
|
||||
if (tty_init() != 0) panic("Failed to initalize TTY driver.");
|
||||
tty_puts("kern.\n\n");
|
||||
|
||||
// TODO: Timer
|
||||
// TODO: Scheduler
|
||||
// Timer
|
||||
if (timer_init() != 0) panic("Failed to initialize timer.");
|
||||
|
||||
// TODO: Syscalls
|
||||
// TODO: Scheduler
|
||||
// TODO: Interprocess communication
|
||||
// TODO: Tasking
|
||||
// TODO: Load & run /sbin/init
|
||||
|
||||
while (1);
|
||||
|
||||
panic("kmain() reached end.");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <kernel.h>
|
||||
#include <system.h>
|
||||
#include <isr.h>
|
||||
#include <tty.h>
|
||||
|
||||
#include <timer.h>
|
||||
|
||||
size_t timer_ticks = 0;
|
||||
|
||||
int timer_init(void) {
|
||||
// TODO: Check what timer we can use and select the best one
|
||||
return pit_init();
|
||||
}
|
||||
|
||||
void timer_wait(size_t t) {
|
||||
size_t req = timer_ticks + t;
|
||||
while (req != timer_ticks);
|
||||
}
|
||||
|
||||
int pit_init(void) {
|
||||
isr_register(0, pit_handler);
|
||||
pit_phase(0, 2, 1000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pit_phase(uint8_t ch, uint8_t mode, uint32_t hz) {
|
||||
if (ch > 2 || mode > 7) return;
|
||||
|
||||
__asm__ volatile ("cli");
|
||||
|
||||
outb(PIT_CMD, ((ch << 6) | (3 << 4) | (mode << 1)));
|
||||
|
||||
uint32_t div = PIT_DIV / hz;
|
||||
debugf("[PIT] Setting PIT divisor to %d...\n", div);
|
||||
outb(PIT_CH0 + ch, div & 0xFF);
|
||||
outb(PIT_CH0 + ch, div >> 8);
|
||||
|
||||
__asm__ volatile ("sti");
|
||||
}
|
||||
|
||||
void pit_handler(struct isr_int_frame* iframe) {
|
||||
timer_ticks++;
|
||||
|
||||
if (timer_ticks % 1000 == 0) tty_puts(".");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user