From 97598b96dbf756bc29215fd7e31671cd1acfb08f Mon Sep 17 00:00:00 2001 From: Ruslan Isaev Date: Wed, 19 Mar 2025 13:10:41 +0300 Subject: [PATCH] add PIC code from osdev --- Makefile | 5 +++- include/pic.h | 8 ++++++ src/kernel.c | 2 ++ src/pic.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 include/pic.h create mode 100644 src/pic.c diff --git a/Makefile b/Makefile index 8d6ad9d..a429b21 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Define the object files -OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o bin/disk.o +OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o bin/disk.o bin/pic.o # Define the compiler and assembler CC = gcc -D__is_katauos @@ -58,6 +58,9 @@ bin/stdio.o: src/stdio.c bin/disk.o: src/disk.c $(CC) $(CFLAGS) -c $< -o $@ +bin/pic.o: src/pic.c + $(CC) $(CFLAGS) -c $< -o $@ + clean: rm -rf bin/ rm -f $(OUTPUT) diff --git a/include/pic.h b/include/pic.h new file mode 100644 index 0000000..aa06fcd --- /dev/null +++ b/include/pic.h @@ -0,0 +1,8 @@ +#ifndef PIC_H +#define PIC_H + +#include "../include/utils.h" + +void pic_remap(int offset1, int offset2); + +#endif diff --git a/src/kernel.c b/src/kernel.c index 5da1a85..7f4c34a 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -4,6 +4,7 @@ #include "../include/utils.h" #include "../include/disk.h" #include "../include/string.h" +#include "../include/pic.h" #include @@ -11,6 +12,7 @@ void kmain() { terminal_initialize(); printf("KatauOS booting up\n"); + pic_remap(0x20, 0x28); isr_install(); printf("Kernel init sequence completed\n"); diff --git a/src/pic.c b/src/pic.c new file mode 100644 index 0000000..5c7495b --- /dev/null +++ b/src/pic.c @@ -0,0 +1,72 @@ +#include "../include/pic.h" + +//took this straight outta https://wiki.osdev.org/8259_PIC#Code_Examples + +#define PIC1 0x20 /* IO base address for master PIC */ +#define PIC2 0xA0 /* IO base address for slave PIC */ +#define PIC1_COMMAND PIC1 +#define PIC1_DATA (PIC1+1) +#define PIC2_COMMAND PIC2 +#define PIC2_DATA (PIC2+1) + +#define PIC_EOI 0x20 /* End-of-interrupt command code */ + +#define PIC_READ_IRR 0x0a /* OCW3 irq ready next CMD read */ +#define PIC_READ_ISR 0x0b /* OCW3 irq service next CMD read */ + +/* reinitialize the PIC controllers, giving them specified vector offsets + rather than 8h and 70h, as configured by default */ + +#define ICW1_ICW4 0x01 /* Indicates that ICW4 will be present */ +#define ICW1_SINGLE 0x02 /* Single (cascade) mode */ +#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */ +#define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */ +#define ICW1_INIT 0x10 /* Initialization - required! */ + +#define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */ +#define ICW4_AUTO 0x02 /* Auto (normal) EOI */ +#define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */ +#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */ +#define ICW4_SFNM 0x10 /* Special fully nested (not) */ + +static inline void io_wait() +{ + outb(0x80, 0x00); +} + + +/* +arguments: + offset1 - vector offset for master PIC + vectors on the master become offset1..offset1+7 + offset2 - same for slave PIC: offset2..offset2+7 +*/ +void pic_remap(int offset1, int offset2) +{ + outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); // starts the initialization sequence (in cascade mode) + io_wait(); + outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4); + io_wait(); + outb(PIC1_DATA, offset1); // ICW2: Master PIC vector offset + io_wait(); + outb(PIC2_DATA, offset2); // ICW2: Slave PIC vector offset + io_wait(); + outb(PIC1_DATA, 4); // ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100) + io_wait(); + outb(PIC2_DATA, 2); // ICW3: tell Slave PIC its cascade identity (0000 0010) + io_wait(); + + outb(PIC1_DATA, ICW4_8086); // ICW4: have the PICs use 8086 mode (and not 8080 mode) + io_wait(); + outb(PIC2_DATA, ICW4_8086); + io_wait(); + + // Unmask both PICs. + outb(PIC1_DATA, 0); + outb(PIC2_DATA, 0); +} + +void pic_disable(void) { + outb(PIC1_DATA, 0xff); + outb(PIC2_DATA, 0xff); +}