diff --git a/interrupt.h b/interrupt.h index 037f069..45b7bfc 100644 --- a/interrupt.h +++ b/interrupt.h @@ -8,3 +8,4 @@ void print_handler(struct interrupt_frame *frame, ulong error_code); //IRQ void keyboard_handler(struct interrupt_frame *frame); +void timer_handler(struct interrupt_frame *frame); diff --git a/main.c b/main.c index 243b5d2..8fc016e 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,7 @@ #include "interrupt.h" #include "io.h" #include "irq.h" +#include "pit.h" #include "types.h" #include "vga.h" @@ -24,7 +25,6 @@ void cpuid(int code, uint32_t *a, uint32_t *d) asm volatile("cpuid" : "=a"(*a), "=d"(*d) : "0"(code) : "ebx", "ecx"); } - void kmain() { const short color = GREEN; @@ -34,17 +34,18 @@ void kmain() idtSetup(); printString("Setting up IRQ", color, BLACK, 0, 1); irqSetup(); + initPit(100); - printString("Setting up IRQ_KEYBOARD", color, BLACK, 0, 1); + printString("Setting up IRQ handlers", color, BLACK, 0, 1); irqSetRoutine(IRQ_KEYBOARD, keyboard_handler); - printString("Enabling HW interrupts", color, BLACK, 0, 1); + irqSetRoutine(IRQ_TIMER, timer_handler); + printString("Enabling HW interrupts", color, BLACK, 0, 2); exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, print_handler); // Enabling the HW interrupts asm volatile("sti\n"); - printString("Idling", color, BLACK, 0, 2); + int count = 0; while (1) { - char c = getScancode(); - printChar(c, color, BLACK, 0, 5); + printInt(count++, color, BLACK, 0, 6); } - printString("exiting", color, BLACK, 0, 3); + printString("exiting", color, BLACK, 0, 4); } diff --git a/pit.c b/pit.c new file mode 100644 index 0000000..f6592a8 --- /dev/null +++ b/pit.c @@ -0,0 +1,14 @@ +#include "pit.h" +#include "io.h" + +int initPit(unsigned int freq) +{ + unsigned int divisor = PIT_FREQ / freq; + if (divisor > 65535) + divisor = 0; // Used to represent 35536 + outb(PIT_CMD, 0x34); // chan 0; low then high; mode 2 + outb(PIT_CHAN_0, divisor & 0xFF); + outb(PIT_CHAN_0, divisor >> 8u); + + return 0; +} diff --git a/pit.h b/pit.h new file mode 100644 index 0000000..2e5f85c --- /dev/null +++ b/pit.h @@ -0,0 +1,16 @@ +#pragma once + +// C.f https://wiki.osdev.org/PIT + +#define PIT_FREQ 1193182 +#define PIT_CHAN_0 0x40 // IRQ0 +#define PIT_CHAN_1 0x41 // Used for DRAM refresh. Not used anymore +#define PIT_CHAN_2 0x42 // PC Speaker +#define PIT_CMD 0x43 +// Cmd are +// 7-6: select channel. 0 ->chan0, 1 -> chan1, 2 -> chan 2, 3 -> read back +// 5-4: access mode. 0 -> latch count; 1 -> low value only; 2 -> high value only; +// 3 -> low then high 3-1: mode. See https://wiki.osdev.org/PIT + +int initPit(unsigned int freq); +