diff --git a/src/kernel/arch/i686/idt.asm b/src/kernel/arch/i686/idt.asm new file mode 100644 index 0000000..81f945f --- /dev/null +++ b/src/kernel/arch/i686/idt.asm @@ -0,0 +1,24 @@ +;/////////////////////; +;Nanite OS ; +;COPYRIGHT (C) 2024 ; +;Tyler McGurrin ; +;/////////////////////; +[bits 32] + +;void __attribute__((cdecl)) i686_IDT_Load(IDTDescriptor* idtDescriptor); +global i686_IDT_Load +i686_IDT_Load: + ; make new call frame + push ebp ; save old call frame + mov ebp, esp ; initialize new call frame + + ; load IDT + mov eax, [ebp + 8] + lgdt [eax] + + + + ; restore old call frame + mov esp, ebp + pop ebp + ret \ No newline at end of file diff --git a/src/kernel/arch/i686/idt.c b/src/kernel/arch/i686/idt.c new file mode 100644 index 0000000..15acb65 --- /dev/null +++ b/src/kernel/arch/i686/idt.c @@ -0,0 +1,50 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2024| +|Tyler McGurrin | +\*----------------*/ +#include "idt.h" +#include +#include + +typedef struct { + uint16_t BaseLow; + uint16_t SegmentSelector; + uint8_t Reserved; + uint8_t Flags; + uint16_t BaseHigh; +} __attribute__((packed)) IDTEntry; + +typedef struct { + uint16_t Limit; + IDTEntry* Ptr; + +} __attribute__((packed)) IDTDescriptor; + + +IDTEntry g_IDT[256]; + +IDTDescriptor g_IDTDescriptor = {sizeof(g_IDT) -1, g_IDT}; + +void __attribute__((cdecl)) i686_IDT_Load(IDTDescriptor* idtDescriptor); + +void i686_IDT_SetGate(int interupt, void* base, uint16_t segmentDescriptor, uint8_t flags) { + g_IDT[interupt].BaseLow = ((uint32_t)base) & 0xFFFF; + g_IDT[interupt].SegmentSelector = segmentDescriptor; + g_IDT[interupt].Reserved = 0; + g_IDT[interupt].Flags = flags; + g_IDT[interupt].BaseHigh = ((uint32_t)base >> 16) & 0xFFFF; +} + +void i686_IDT_EnableGate(int interupt) { + FLAG_SET(g_IDT[interupt].Flags, IDT_FLAG_PRESENT); +} + +void i686_IDT_DisableGate(int interupt) { + FLAG_UNSET(g_IDT[interupt].Flags, IDT_FLAG_PRESENT); +} + +void i686_IDT_Initialize() { + i686_IDT_Load(&g_IDTDescriptor); +} + diff --git a/src/kernel/arch/i686/idt.h b/src/kernel/arch/i686/idt.h index ec047f2..29ad673 100644 --- a/src/kernel/arch/i686/idt.h +++ b/src/kernel/arch/i686/idt.h @@ -2,4 +2,28 @@ |Nanite OS | |Copyright (C) 2024| |Tyler McGurrin | -\*----------------*/ \ No newline at end of file +\*----------------*/ +#pragma once + +#include + +void i686_IDT_Initialize(); +void i686_IDT_DisableGate(int interupt); +void i686_IDT_EnableGate(int interupt); +void i686_IDT_SetGate(int interupt, void* base, uint16_t segmentDescriptor, uint8_t flags); + +typedef enum { + IDT_FLAG_GATE_TASK = 0x5, + IDT_FLAG_GATE_16BIT_INT = 0x6, + IDT_FLAG_GATE_16BIT_TRAP = 0x7, + IDT_FLAG_GATE_32BIT_INT = 0xE, + IDT_FLAG_GATE_32BIT_TRAP = 0xF, + + IDT_FLAG_RING0 = (0 << 5), + IDT_FLAG_RING1 = (1 << 5), + IDT_FLAG_RING2 = (2 << 5), + IDT_FLAG_RING3 = (3 << 5), + + IDT_FLAG_PRESENT = 0x80, + +} IDT_FLAGS; \ No newline at end of file diff --git a/src/kernel/hal/hal.c b/src/kernel/hal/hal.c index 8dc2318..4c26eed 100644 --- a/src/kernel/hal/hal.c +++ b/src/kernel/hal/hal.c @@ -1,6 +1,18 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2024| +|Tyler McGurrin | +\*----------------*/ #include "hal.h" #include +#include +#include -void HAL_Initialize() { +void HAL_Initialize() { + printf("> Initializing GDT..."); i686_GDT_Initialize(); + printf("Done!\n"); + printf("> Initializing IDT..."); + i686_IDT_Initialize(); + printf("Done!\n"); } \ No newline at end of file diff --git a/src/kernel/hal/hal.h b/src/kernel/hal/hal.h index 4ca63a4..d022bb4 100644 --- a/src/kernel/hal/hal.h +++ b/src/kernel/hal/hal.h @@ -1,3 +1,8 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2024| +|Tyler McGurrin | +\*----------------*/ #pragma once void HAL_Initialize(); \ No newline at end of file diff --git a/src/kernel/main.c b/src/kernel/main.c index c7aa4cc..977282b 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -21,9 +21,13 @@ extern uint8_t __end; printf("Loaded Kernel!\n"); // init HAL - printf("Initializing HAL..."); + printf("Initializing HAL...\n"); + int halx, haly = 0; HAL_Initialize(); - printf("Done!\n"); + movecursorpos(19, 8); + printf("Done!\n\n\n"); + + end: for (;;); diff --git a/src/kernel/stdio.c b/src/kernel/stdio.c index 3dba0f0..3e01664 100644 --- a/src/kernel/stdio.c +++ b/src/kernel/stdio.c @@ -8,13 +8,25 @@ #include #include + +const uint8_t DEFAULT_COLOUR = 0x7; + const unsigned SCREEN_WIDTH = 80; const unsigned SCREEN_HEIGHT = 25; -const uint8_t DEFAULT_COLOUR = 0x7; uint8_t* g_ScreenBuffer = (uint8_t*)0xB8000; int g_ScreenX = 0, g_ScreenY = 0; +void movecursorpos(int x, int y) { + g_ScreenX = x; + g_ScreenY = y; +} + +void getcursorpos(int x, int y) { + x = g_ScreenX; + y = g_ScreenY; +} + void putchr(int x, int y, char c) { g_ScreenBuffer[2 * (y * SCREEN_WIDTH + x)] = c; @@ -37,12 +49,12 @@ uint8_t getcolour(int x, int y) void setcursor(int x, int y) { - int pos = y * SCREEN_WIDTH + x; + int pos = y * SCREEN_WIDTH + x; - x86_outb(0x3D4, 0x0F); - x86_outb(0x3D5, (uint8_t)(pos & 0xFF)); - x86_outb(0x3D4, 0x0E); - x86_outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); + x86_outb(0x3D4, 0x0F); + x86_outb(0x3D5, (uint8_t)(pos & 0xFF)); + x86_outb(0x3D4, 0x0E); + x86_outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF)); } void clrscr() diff --git a/src/kernel/stdio.h b/src/kernel/stdio.h index e455425..8ed8aa4 100644 --- a/src/kernel/stdio.h +++ b/src/kernel/stdio.h @@ -6,9 +6,11 @@ #pragma once #include + void clrscr(); void putc(char c); void puts(const char* str); void printf(const char* fmt, ...); void print_buffer(const char* msg, const void* buffer, uint32_t count); -void setcursor(int x, int y); \ No newline at end of file +void movecursorpos(int x, int y); +void getcursorpos(int x, int y); \ No newline at end of file diff --git a/src/kernel/util/binary.h b/src/kernel/util/binary.h new file mode 100644 index 0000000..2825c5b --- /dev/null +++ b/src/kernel/util/binary.h @@ -0,0 +1,9 @@ +/*----------------*\ +|Nanite OS | +|Copyright (C) 2024| +|Tyler McGurrin | +\*----------------*/ +#pragma once + +#define FLAG_SET(x, flag) x |= (flag) +#define FLAG_UNSET(x, flag) x &= ~(flag) \ No newline at end of file