FetherOS/kernel/kernel.c
2026-06-10 17:24:01 +03:00

70 lines
1.5 KiB
C

// fether os kernel.
#include <stddef.h>
#include <stdint.h>
// protecting compilation.
#if defined(__linux__)
#error "You should compile all with a cross-compiler. See project wiki."
#elif !defined(__amd64__)
#error "This is an 32bit\64bit kernel. Please use i386 or amd64 target."
#elif !defined(__i386__)
#error "This is an 32bit\64bit kernel. Please use i386 or amd64 target."
#endif
volatile uint16_t* vga_buffer = (uint16_t*)0xB8000; // defining VGA text mode.
// vga(2) settings
const int VGA_COLS = 80;
const int VGA_ROWS = 25;
// terminal settings
int term_col = 0;
int term_row = 0;
uint8_t term_color = 0x0F;
// clearing the terminal
void term_init() {
for(int col = 0; col < VGA_COLS; col++) {
for(int row = 0; row < VGA_ROWS; row++) {
const size_t index = (VGA_COLS * row) + col;
vga_buffer[index] = ((uint16_t)term_color << 8) | ' ';
}
}
}
void term_put(char c) {
switch(c) {
case '\n': // newline implemintation
{
term_col = 0;
term_row++;
break;
}
default: // if char doesn't match any special symbol
{
const size_t index = (VGA_COLS * term_row) + term_col;
vga_buffer[index] = ((uint16_t)term_color << 8) | c;
break;
}
}
if(term_col >= VGA_COLS) {
term_col = 0;
term_row++;
}
if(term_row >= VGA_ROWS) {
term_col = 0;
term_row = 0;
}
}
void term_print(const char* str) { for(size_t i = 0; str[i] != '\0'; i++) { term_put(str[i]); } }
void kmain() {
term_init();
term_print("Kernel loaded.\nFether.");
}