From 7826b9b7d42d732a4eb8764e2026fbf7b190b13c Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Tue, 7 May 2024 21:01:50 +0200 Subject: [PATCH] Include file restructure! - include/ now has subdirs - standard C headers will be in 'include/c'. Any program can include these - all other header shall only be used by the kernel --- Makefile | 4 +- include/{ => c}/math.h | 0 include/{ => c}/stdbool.h | 0 include/{ => c}/stdint.h | 0 include/{ => c}/string.h | 0 include/{ => c}/time.h | 2 + include/{ => disk}/disk.h | 2 +- include/{ => interrupt}/exceptions.h | 4 +- include/{ => interrupt}/idt.h | 2 +- include/{ => interrupt}/pic.h | 2 +- include/{ => interrupt}/pit.h | 6 +-- include/{ => interrupt}/syscall.h | 2 +- include/{ => keyboard}/keyboard.h | 8 ++-- .../physical.h} | 0 include/{system.h => ports/io.h} | 0 include/{ => screen}/graphics.h | 2 +- include/{screen.h => screen/text.h} | 0 include/timer.h | 47 ------------------- src/calc.c | 9 +--- src/kernel.c | 26 +++++----- 20 files changed, 33 insertions(+), 83 deletions(-) rename include/{ => c}/math.h (100%) rename include/{ => c}/stdbool.h (100%) rename include/{ => c}/stdint.h (100%) rename include/{ => c}/string.h (100%) rename include/{ => c}/time.h (85%) rename include/{ => disk}/disk.h (99%) rename include/{ => interrupt}/exceptions.h (99%) rename include/{ => interrupt}/idt.h (98%) rename include/{ => interrupt}/pic.h (98%) rename include/{ => interrupt}/pit.h (94%) rename include/{ => interrupt}/syscall.h (98%) rename include/{ => keyboard}/keyboard.h (96%) rename include/{physicalMemManager.h => memory/physical.h} (100%) rename include/{system.h => ports/io.h} (100%) rename include/{ => screen}/graphics.h (99%) rename include/{screen.h => screen/text.h} (100%) delete mode 100644 include/timer.h diff --git a/Makefile b/Makefile index 5e26c5c..1b06e2d 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CC = i386-elf-gcc LD = i386-elf-ld AS = nasm -CFLAGS = -ffreestanding -fno-builtin -fno-stack-protector -nostdinc -mgeneral-regs-only -m32 -march=i386 -Iinclude -Wall +CFLAGS = -ffreestanding -fno-builtin -fno-stack-protector -nostdinc -mgeneral-regs-only -m32 -march=i386 -Wall -Iinclude/c SRC = ./src BUILD = ./build @@ -47,7 +47,7 @@ kernel: $(SRC)/kernel.c $(SRC)/entry.asm @echo "Assembling $(SRC)/entry.asm..." @$(AS) -felf32 -o $(BUILD)/entry.o $(SRC)/entry.asm @echo "Compiling $(SRC)/kernel.c..." - @$(CC) $(CFLAGS) -o $(BUILD)/kernel.o -c $(SRC)/kernel.c + @$(CC) $(CFLAGS) -Iinclude -o $(BUILD)/kernel.o -c $(SRC)/kernel.c @$(LD) -T$(SRC)/kernel.ld --oformat binary -o $(BUILD)/kernel.bin $(BUILD)/*.o @rm $(BUILD)/*.o diff --git a/include/math.h b/include/c/math.h similarity index 100% rename from include/math.h rename to include/c/math.h diff --git a/include/stdbool.h b/include/c/stdbool.h similarity index 100% rename from include/stdbool.h rename to include/c/stdbool.h diff --git a/include/stdint.h b/include/c/stdint.h similarity index 100% rename from include/stdint.h rename to include/c/stdint.h diff --git a/include/string.h b/include/c/string.h similarity index 100% rename from include/string.h rename to include/c/string.h diff --git a/include/time.h b/include/c/time.h similarity index 85% rename from include/time.h rename to include/c/time.h index e6a193d..d7a9d7b 100644 --- a/include/time.h +++ b/include/c/time.h @@ -1,6 +1,8 @@ #ifndef TIME_H #define TIME_H +#include + void sleep(uint32_t ms) { __asm__ volatile ("int $0x80" : : "a"(0), "b"(ms)); } diff --git a/include/disk.h b/include/disk/disk.h similarity index 99% rename from include/disk.h rename to include/disk/disk.h index adf77a3..23c0c54 100644 --- a/include/disk.h +++ b/include/disk/disk.h @@ -2,7 +2,7 @@ #define DRIVE_H #include -#include +#include #define PIO_PORT 0x1f0 #define READ_COMMAND 0x20 diff --git a/include/exceptions.h b/include/interrupt/exceptions.h similarity index 99% rename from include/exceptions.h rename to include/interrupt/exceptions.h index 52727a4..3a118b3 100644 --- a/include/exceptions.h +++ b/include/interrupt/exceptions.h @@ -1,8 +1,8 @@ #ifndef ISRS_H #define ISRS_H -#include -#include +#include +#include // Exception handlers __attribute__ ((interrupt)) diff --git a/include/idt.h b/include/interrupt/idt.h similarity index 98% rename from include/idt.h rename to include/interrupt/idt.h index 88bf031..26ec238 100644 --- a/include/idt.h +++ b/include/interrupt/idt.h @@ -2,7 +2,7 @@ #define IDT_H #include -#include +#include #define TRAP_GATE_FLAGS 0x8f // P = 1, DPL = 00, S = 0, Type = 1111 (32-bit Trap gate) #define INT_GATE_FLAGS 0x8e // P = 1, DPL = 00, S = 0, Type = 1110 (32-bit Int gate) diff --git a/include/pic.h b/include/interrupt/pic.h similarity index 98% rename from include/pic.h rename to include/interrupt/pic.h index 8445351..0f581fe 100644 --- a/include/pic.h +++ b/include/interrupt/pic.h @@ -1,7 +1,7 @@ #ifndef PIC_H #define PIC_H -#include +#include // PIC I/O Ports #define PIC_1_CMD 0x20 diff --git a/include/pit.h b/include/interrupt/pit.h similarity index 94% rename from include/pit.h rename to include/interrupt/pit.h index 474bedf..0a212f0 100644 --- a/include/pit.h +++ b/include/interrupt/pit.h @@ -2,9 +2,9 @@ #define TIMER_H #include -#include -#include -#include +#include +#include +#include #define CHANNEL_0 0x40 // Offset it by 1 or 2 for ch1 and ch2 #define COMMAND 0x43 diff --git a/include/syscall.h b/include/interrupt/syscall.h similarity index 98% rename from include/syscall.h rename to include/interrupt/syscall.h index 7fbd540..693fe77 100644 --- a/include/syscall.h +++ b/include/interrupt/syscall.h @@ -1,7 +1,7 @@ #ifndef SYSCALL_H #define SYSCALL_H -#include +#include // System call count #define MAX_SYSCALLS 2 diff --git a/include/keyboard.h b/include/keyboard/keyboard.h similarity index 96% rename from include/keyboard.h rename to include/keyboard/keyboard.h index bb84382..2ac69f0 100644 --- a/include/keyboard.h +++ b/include/keyboard/keyboard.h @@ -1,10 +1,10 @@ #ifndef KEYBOARD_H #define KEYBOARD_H -#include -#include -#include -#include +#include +#include +#include +#include #include #include diff --git a/include/physicalMemManager.h b/include/memory/physical.h similarity index 100% rename from include/physicalMemManager.h rename to include/memory/physical.h diff --git a/include/system.h b/include/ports/io.h similarity index 100% rename from include/system.h rename to include/ports/io.h diff --git a/include/graphics.h b/include/screen/graphics.h similarity index 99% rename from include/graphics.h rename to include/screen/graphics.h index df182fb..157787d 100644 --- a/include/graphics.h +++ b/include/screen/graphics.h @@ -2,7 +2,7 @@ #define GRAPHICS_H #include -#include +#include #include #define rgb(r,g,b) ( ((r) << 16 | (g) << 8 | (b)) & 0x00ffffff ) diff --git a/include/screen.h b/include/screen/text.h similarity index 100% rename from include/screen.h rename to include/screen/text.h diff --git a/include/timer.h b/include/timer.h deleted file mode 100644 index 474bedf..0000000 --- a/include/timer.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef TIMER_H -#define TIMER_H - -#include -#include -#include -#include - -#define CHANNEL_0 0x40 // Offset it by 1 or 2 for ch1 and ch2 -#define COMMAND 0x43 -#define INPUT_CLK 1193182 - -static uint32_t* timerTicks = (uint32_t*)0x1000; - -__attribute__ ((interrupt)) -void timerHandler(intFrame_t* iframe) { - *timerTicks += 1; - - // TODO: Improve the cursor rendering - if (*timerTicks % 500 == 0) putcAt(' ', cursor.x, cursor.y); - else if (*timerTicks % 250 == 0) putcAt(127, cursor.x, cursor.y); - - sendPicEOI(0); -} - -void setPitPhase(uint8_t channel, uint8_t opMode, uint16_t hz) { - if (channel > 2 || opMode > 7) { return; } - __asm__ volatile ("cli"); - - // Send command - bits 7-6 channel, 5-4 Access mode, 3-1 operating mode, 0 - Should be zero in x86 - outb(COMMAND, ((channel << 6) | (3 << 4) | (opMode << 1))); - - // Send frequency - uint16_t freq = INPUT_CLK / hz; // I solved an equation to figure this out - outb(CHANNEL_0 + channel, (uint8_t)freq); - outb(CHANNEL_0 + channel, (uint8_t)(freq >> 8)); - - __asm__ volatile ("sti"); -} - -void setupTimer() { // Sets up IRQ 0 - PIT - idtSetGate(0x20, timerHandler, INT_GATE_FLAGS); - unsetIrqMask(0); - setPitPhase(0, 2, 1000); // Default setup: 1000Hz - 1 tick per 1ms -} - -#endif diff --git a/src/calc.c b/src/calc.c index 1b29286..e2fbae0 100644 --- a/src/calc.c +++ b/src/calc.c @@ -1,13 +1,8 @@ -#include "stdint.h" -#include "screen.h" +#include __attribute__ ((section ("entry"))) void main() { - uint32_t wait = 0x1fffffff; - - print("kern.\n\n"); - - while (wait--); + sleep(4500); // Wait 4.5 seconds return; } diff --git a/src/kernel.c b/src/kernel.c index f341287..ca1280d 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,16 +1,16 @@ -#include // TODO: Name this better -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #define PROMPT "#> "