Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions Kernel/Core/Kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@
#include "../System/Syscall.h"
#include "../System/Gdt.h"
#include "Panic.h"
#include "stdbool.h"
#include "UserMode.h"
#include "../Drivers/Io.h"
#include "../Memory/VMem.h"

#define NULL ((void*)0)
#define true 1
#define false 0
typedef int bool;

// VGA Constants
#define VGA_BUFFER_ADDR 0xB8000
#define VGA_WIDTH 80
Expand Down Expand Up @@ -136,14 +132,14 @@ static void ConsolePutchar(char c) {
// Modern string output with length checking
void PrintKernel(const char* str) {
if (!str) return;

// Cache the original color
const uint8_t original_color = console.color;

for (const char* p = str; *p; p++) {
ConsolePutchar(*p);
}

console.color = original_color;
}

Expand All @@ -170,52 +166,52 @@ void PrintKernelWarning(const char* str) {
void PrintKernelHex(uint64_t num) {
static const char hex_chars[] = "0123456789ABCDEF";
char buffer[19]; // "0x" + 16 hex digits + null terminator

buffer[0] = '0';
buffer[1] = 'x';

if (num == 0) {
buffer[2] = '0';
buffer[3] = '\0';
PrintKernel(buffer);
return;
}

int pos = 18;
buffer[pos--] = '\0';

while (num > 0 && pos >= 2) {
buffer[pos--] = hex_chars[num & 0xF];
num >>= 4;
}

PrintKernel(&buffer[pos + 1]);
}

// Optimized integer printing with proper sign handling
void PrintKernelInt(int64_t num) {
char buffer[21]; // Max digits for 64-bit signed integer + sign + null

if (num == 0) {
PrintKernel("0");
return;
}

bool negative = num < 0;
if (negative) num = -num;

int pos = 20;
buffer[pos--] = '\0';

while (num > 0 && pos >= 0) {
buffer[pos--] = '0' + (num % 10);
num /= 10;
}

if (negative && pos >= 0) {
buffer[pos--] = '-';
}

PrintKernel(&buffer[pos + 1]);
}

Expand Down
9 changes: 9 additions & 0 deletions Kernel/Core/Panic.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ int ForceReboot() {
asm volatile("lidt %0" :: "m"(*(short*)0));
PrintKernelWarning("[SYSTEM] Rebooting now...");
asm volatile("int $0x3");
while (1) {
__asm__ __volatile__("hlt");
}
}


Expand All @@ -31,6 +34,9 @@ void __attribute__((noreturn)) Panic(const char* message) {
PrintKernelError(message);
PrintKernelError("\n[SYSTEM] Calling KernelPanicHandler()...\n");
KernelPanicHandler();
while (1) {
__asm__ __volatile__("hlt");
}
}

void __attribute__((noreturn)) PanicWithCode(const char* message, uint64_t error_code) {
Expand All @@ -41,4 +47,7 @@ void __attribute__((noreturn)) PanicWithCode(const char* message, uint64_t error
PrintKernelHex(error_code);
PrintKernelError(" -- Not handled");
KernelPanicHandler();
while (1) {
__asm__ __volatile__("hlt");
}
}
6 changes: 6 additions & 0 deletions Kernel/Core/stdbool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef VOIDFRAME_STDBOOL_H
#define VOIDFRAME_STDBOOL_H
typedef int bool;
#define true 1
#define false 0
#endif
2 changes: 1 addition & 1 deletion Kernel/Drivers/Pic.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef PIC_H
#define PIC_H

#define PIT_FREQUENCY_HZ 1000
#define PIT_FREQUENCY_HZ 250

int PicInstall();
void PitInstall();
Expand Down
Loading