summaryrefslogtreecommitdiff
path: root/src/pic.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pic.c')
-rw-r--r--src/pic.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/pic.c b/src/pic.c
new file mode 100644
index 0000000..ad2c15d
--- /dev/null
+++ b/src/pic.c
@@ -0,0 +1,107 @@
+#include <nrvn.h>
+#include <pic.h>
+#include <idt.h>
+#include <vga.h>
+
+extern void toggle_interrupts(bool enable);
+
+#define pic1 0x20
+#define pic2 0xA0
+#define pic1_d (pic1 + 1)
+#define pic2_d (pic2 + 2)
+
+void pic_remap(uint32_t offset1, uint32_t offset2) {
+ uint8_t a1, a2;
+
+ a1 = inb(pic1_d);
+ a2 = inb(pic2_d);
+
+ outb(pic1, 0x10 | 0x01);
+ io_wait();
+ outb(pic2, 0x10 | 0x01);
+ io_wait();
+ outb(pic1_d, offset1);
+ io_wait();
+ outb(pic2_d, offset2);
+ io_wait();
+ outb(pic1_d, 4);
+ io_wait();
+ outb(pic2_d, 2);
+ io_wait();
+
+ outb(pic1_d, 0x01);
+ io_wait();
+ outb(pic2_d, 0x01);
+ io_wait();
+
+ outb(pic1_d, a1);
+ outb(pic2_d, a2);
+}
+
+void pic_set_mask(uint8_t irq_line) {
+ uint32_t port;
+ uint8_t value;
+
+ if (irq_line < 8)
+ port = pic1_d;
+ else {
+ port = pic2_d;
+ irq_line -= 8;
+ }
+ value = inb(port) | (1 << irq_line);
+ outb(port, value);
+}
+
+void pic_clear_mask(uint8_t irq_line) {
+ uint32_t port;
+ uint8_t value;
+
+ if (irq_line < 8)
+ port = pic1_d;
+ else {
+ port = pic2_d;
+ irq_line -= 8;
+ }
+ value = inb(port) & ~(1 << irq_line);
+ outb(port, value);
+}
+
+void pic_set_all_masks(void) {
+ outb(pic1_d, 0xFF);
+ outb(pic2_d, 0xFF);
+}
+
+void pic_clear_all_masks(void) {
+ outb(pic1_d, 0x00);
+ outb(pic2_d, 0x00);
+}
+
+void pic_eoi(uint8_t irq) {
+ if (irq >= 8)
+ outb(pic2, 0x20);
+ outb(pic1, 0x20);
+}
+
+void keyboard_handler(uint64_t num, uint64_t _unused) {
+ (void)num;
+ (void)_unused;
+ uint8_t key = inb(0x60);
+ vga_puts("key pressed: ");
+ vga_putx(key);
+ vga_putc('\n');
+ pic_eoi(0x1);
+}
+
+void pic_init(void) {
+ pic_set_all_masks();
+ pic_clear_mask(1);
+ //pic_clear_mask(12);
+ pic_remap(32, 40);
+
+ for (size_t i = 32; i < 48; i++)
+ idt_set_isr(i, isr_table[i], 0x08, 0x8E);
+
+ idt_register_isr(33, keyboard_handler);
+
+ toggle_interrupts(true);
+}