From b43278ab064caf73a31a306b852fe3d296e518cd Mon Sep 17 00:00:00 2001 From: Aaro Koivukangas Date: Wed, 15 Apr 2026 10:39:28 +0300 Subject: [PATCH] moved things around, still ongoing --- .gitignore | 2 + c/kernel.c | 169 ------------------------------- c/kernel/Makefile | 44 ++++++++ c/kernel/arch/i386/include/tty.h | 24 +++++ c/kernel/arch/i386/include/vga.h | 38 +++++++ c/kernel/arch/i386/tty.c | 37 +++++++ c/{ => kernel}/boot.s | 0 c/kernel/compile_commands.json | 22 ++++ c/{ => kernel}/grub.cfg | 0 c/kernel/include/kernel.h | 12 +++ c/kernel/kernel/kernel.c | 71 +++++++++++++ c/{ => kernel}/linker.ld | 0 rust/kernel/src/main.rs | 4 +- rust/kernel/src/vga_buffer.rs | 67 ++++++++++++ 14 files changed, 320 insertions(+), 170 deletions(-) delete mode 100644 c/kernel.c create mode 100644 c/kernel/Makefile create mode 100644 c/kernel/arch/i386/include/tty.h create mode 100644 c/kernel/arch/i386/include/vga.h create mode 100644 c/kernel/arch/i386/tty.c rename c/{ => kernel}/boot.s (100%) create mode 100644 c/kernel/compile_commands.json rename c/{ => kernel}/grub.cfg (100%) create mode 100644 c/kernel/include/kernel.h create mode 100644 c/kernel/kernel/kernel.c rename c/{ => kernel}/linker.ld (100%) create mode 100644 rust/kernel/src/vga_buffer.rs diff --git a/.gitignore b/.gitignore index a13b5fc..e19c771 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ sysroot isodir + +c-meaty-skeleton diff --git a/c/kernel.c b/c/kernel.c deleted file mode 100644 index 88ef99e..0000000 --- a/c/kernel.c +++ /dev/null @@ -1,169 +0,0 @@ -#include -#include -#include - -/* Check if the compiler thinks you are targeting the wrong operating system. */ -#if defined(__linux__) -#error "You are not using a cross-compiler, you will most certainly run into trouble" -#endif - -/* This tutorial will only work for the 32-bit ix86 targets. */ -#if !defined(__i386__) -#error "This tutorial needs to be compiled with a ix86-elf compiler" -#endif - -/* Hardware text mode color constants. */ -enum vga_color { - VGA_COLOR_BLACK = 0, - VGA_COLOR_BLUE = 1, - VGA_COLOR_GREEN = 2, - VGA_COLOR_CYAN = 3, - VGA_COLOR_RED = 4, - VGA_COLOR_MAGENTA = 5, - VGA_COLOR_BROWN = 6, - VGA_COLOR_LIGHT_GREY = 7, - VGA_COLOR_DARK_GREY = 8, - VGA_COLOR_LIGHT_BLUE = 9, - VGA_COLOR_LIGHT_GREEN = 10, - VGA_COLOR_LIGHT_CYAN = 11, - VGA_COLOR_LIGHT_RED = 12, - VGA_COLOR_LIGHT_MAGENTA = 13, - VGA_COLOR_LIGHT_BROWN = 14, - VGA_COLOR_WHITE = 15, -}; - -static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) -{ - return fg | bg << 4; -} - -static inline uint16_t vga_entry(unsigned char uc, uint8_t color) -{ - return (uint16_t) uc | (uint16_t) color << 8; -} - -size_t strlen(const char* str) -{ - size_t len = 0; - while (str[len]) - len++; - return len; -} - -static const size_t VGA_WIDTH = 80; -static const size_t VGA_HEIGHT = 25; - -size_t terminal_row; -size_t terminal_column; -uint8_t terminal_color; -uint16_t* terminal_buffer; - -void terminal_initialize(void) -{ - terminal_row = 0; - terminal_column = 0; - terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); - terminal_buffer = (uint16_t*) 0xB8000; - for (size_t y = 0; y < VGA_HEIGHT; y++) { - for (size_t x = 0; x < VGA_WIDTH; x++) { - const size_t index = y * VGA_WIDTH + x; - terminal_buffer[index] = vga_entry(' ', terminal_color); - } - } -} - -void terminal_setcolor(uint8_t color) -{ - terminal_color = color; -} - -void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) -{ - const size_t index = y * VGA_WIDTH + x; - terminal_buffer[index] = vga_entry(c, color); -} - -void terminal_clear_row(size_t row) -{ - for (size_t i=0; i Hello 1 - // row 1 => Hello 2 - // row 2 => Hello 3 * - // - // scroll 1 - // - // row 0 => Hello 2 - // row 1 => Hello 3 - // row 2 => * - - - for (size_t i=0; i +#include + +#include "vga.h" + +static size_t TERMINAL_WIDTH = VGA_WIDTH; +static size_t TERMINAL_HEIGHT = VGA_HEIGHT; + +static size_t terminal_row; +static size_t terminal_column; +static uint8_t terminal_color; +static uint16_t *terminal_buffer; + +void terminal_setcolor(uint8_t color); +void terminal_putentryat(char c, uint8_t color, size_t x, size_t y); +void terminal_clear_row(size_t row); +void terminal_scroll(void); +void terminal_initialize(void); +void terminal_initialize2(void); + +#endif diff --git a/c/kernel/arch/i386/include/vga.h b/c/kernel/arch/i386/include/vga.h new file mode 100644 index 0000000..792f710 --- /dev/null +++ b/c/kernel/arch/i386/include/vga.h @@ -0,0 +1,38 @@ +#ifndef VGA_H +#define VGA_H + +#include +#include + +/* Hardware text mode color constants. */ +enum vga_color { + VGA_COLOR_BLACK = 0, + VGA_COLOR_BLUE = 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_CYAN = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_MAGENTA = 5, + VGA_COLOR_BROWN = 6, + VGA_COLOR_LIGHT_GREY = 7, + VGA_COLOR_DARK_GREY = 8, + VGA_COLOR_LIGHT_BLUE = 9, + VGA_COLOR_LIGHT_GREEN = 10, + VGA_COLOR_LIGHT_CYAN = 11, + VGA_COLOR_LIGHT_RED = 12, + VGA_COLOR_LIGHT_MAGENTA = 13, + VGA_COLOR_LIGHT_BROWN = 14, + VGA_COLOR_WHITE = 15, +}; + +static const size_t VGA_WIDTH = 80; +static const size_t VGA_HEIGHT = 25; + +static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg) { + return fg | bg << 4; +} + +static inline uint16_t vga_entry(unsigned char uc, uint8_t color) { + return (uint16_t)uc | (uint16_t)color << 8; +} + +#endif diff --git a/c/kernel/arch/i386/tty.c b/c/kernel/arch/i386/tty.c new file mode 100644 index 0000000..f815e83 --- /dev/null +++ b/c/kernel/arch/i386/tty.c @@ -0,0 +1,37 @@ +#include "tty.h" +#include "vga.h" + +void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) { + const size_t index = y * TERMINAL_WIDTH + x; + terminal_buffer[index] = vga_entry(c, color); +} + +void terminal_setcolor(uint8_t color) { terminal_color = color; } + +void terminal_clear_row(size_t row) { + for (size_t i = 0; i < TERMINAL_WIDTH; i++) + terminal_putentryat(' ', terminal_color, i, row); +} + +void terminal_scroll(void) { + for (size_t i = 0; i < TERMINAL_HEIGHT; i++) { + for (size_t j = 0; j < TERMINAL_WIDTH; j++) + terminal_buffer[i * TERMINAL_WIDTH + j] = + terminal_buffer[(i + 1) * TERMINAL_WIDTH + j]; + } + + terminal_clear_row(TERMINAL_HEIGHT - 1); +} + +void terminal_initialize(void) { + terminal_row = 0; + terminal_column = 0; + terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); + terminal_buffer = (uint16_t *)0xB8000; + for (size_t y = 0; y < TERMINAL_HEIGHT; y++) { + for (size_t x = 0; x < TERMINAL_WIDTH; x++) { + const size_t index = y * TERMINAL_WIDTH + x; + terminal_buffer[index] = vga_entry(' ', terminal_color); + } + } +} diff --git a/c/boot.s b/c/kernel/boot.s similarity index 100% rename from c/boot.s rename to c/kernel/boot.s diff --git a/c/kernel/compile_commands.json b/c/kernel/compile_commands.json new file mode 100644 index 0000000..4b4924b --- /dev/null +++ b/c/kernel/compile_commands.json @@ -0,0 +1,22 @@ +[ + { + "file": "arch/i386/tty.c", + "arguments": [ + "i686-elf-gcc", + "-MD", + "-c", + "arch/i386/tty.c", + "-std=gnu99", + "-o", + "arch/i386/tty.o", + "-ffreestanding", + "-O2", + "-Wall", + "-Wextra", + "-Iinclude", + "-Iarch/i386/include" + ], + "directory": "/home/martti/git/deros/c/kernel", + "output": "arch/i386/tty.o" + } +] \ No newline at end of file diff --git a/c/grub.cfg b/c/kernel/grub.cfg similarity index 100% rename from c/grub.cfg rename to c/kernel/grub.cfg diff --git a/c/kernel/include/kernel.h b/c/kernel/include/kernel.h new file mode 100644 index 0000000..c7e64d9 --- /dev/null +++ b/c/kernel/include/kernel.h @@ -0,0 +1,12 @@ +#ifndef KERNEL_H +#define KERNEL_H + +#include +#include + +void terminal_initialize(void); +void terminal_write(const char *data, size_t size); +void terminal_writestring(const char *data); +void kernel_main(void); + +#endif diff --git a/c/kernel/kernel/kernel.c b/c/kernel/kernel/kernel.c new file mode 100644 index 0000000..4d5463a --- /dev/null +++ b/c/kernel/kernel/kernel.c @@ -0,0 +1,71 @@ +#include +#include +#include + +#include "tty.h" + +/* Check if the compiler thinks you are targeting the wrong operating system. */ +#if defined(__linux__) +#error \ + "You are not using a cross-compiler, you will most certainly run into trouble" +#endif + +/* This tutorial will only work for the 32-bit ix86 targets. */ +#if !defined(__i386__) +#error "This tutorial needs to be compiled with a ix86-elf compiler" +#endif + +size_t strlen(const char *str) { + size_t len = 0; + while (str[len]) + len++; + return len; +} + + +void terminal_putchar(char c) { + if (c == '\n') { + terminal_column = 0; + if (++terminal_row == TERMINAL_HEIGHT) { + terminal_scroll(); + terminal_row--; + } + return; + } + + terminal_putentryat(c, terminal_color, terminal_column, terminal_row); + + if (++terminal_column == TERMINAL_WIDTH) { + terminal_column = 0; + if (++terminal_row == TERMINAL_HEIGHT) { + terminal_scroll(); + terminal_row--; + } + } +} + +void terminal_write(const char *data, size_t size) { + for (size_t i = 0; i < size; i++) + terminal_putchar(data[i]); +} + +void terminal_writestring(const char *data) { + terminal_write(data, strlen(data)); +} + +void terminal_test_scroll() { + for (size_t i = 0; i < 126 - 65; i++) { + terminal_putchar(i + 65); + terminal_writestring(" more chars...\n"); + } +} + +void kernel_main(void) { + /* Initialize terminal interface */ + terminal_initialize(); + + /* Newline support is left as an exercise. */ + + terminal_test_scroll(); + terminal_writestring("Hello from the kernel :)\n"); +} diff --git a/c/linker.ld b/c/kernel/linker.ld similarity index 100% rename from c/linker.ld rename to c/kernel/linker.ld diff --git a/rust/kernel/src/main.rs b/rust/kernel/src/main.rs index eac2b3c..f5a1576 100644 --- a/rust/kernel/src/main.rs +++ b/rust/kernel/src/main.rs @@ -1,6 +1,8 @@ #![no_std] #![no_main] +mod vga_buffer; + static HELLO: &[u8] = b"Hello World!"; #[unsafe(no_mangle)] @@ -12,9 +14,9 @@ pub extern "C" fn _start() -> ! { *vga_buffer.offset(i as isize * 2 + 1) = 0xb; } } - loop {} } + #[panic_handler] fn panic(_info: &core::panic::PanicInfo) -> ! { loop {} diff --git a/rust/kernel/src/vga_buffer.rs b/rust/kernel/src/vga_buffer.rs new file mode 100644 index 0000000..e428bbc --- /dev/null +++ b/rust/kernel/src/vga_buffer.rs @@ -0,0 +1,67 @@ +#[allow(dead_code)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u8)] +pub enum Color { + Black = 0, + Blue = 1, + Green = 2, + Cyan = 3, + Red = 4, + Magenta = 5, + Brown = 6, + LightGray = 7, + DarkGray = 8, + LightBlue = 9, + LightGreen = 10, + LightCyan = 11, + LightRed = 12, + Pink = 13, + Yellow = 14, + White = 15, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(transparent)] +struct ColorCode(u8); + +impl ColorCode { + fn new(foreground: Color, background: Color) -> ColorCode { + ColorCode((background as u8) << 4 | (foreground as u8)) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(C)] +struct TerminalChar { + ascii_char: u8, + color_code: ColorCode, +} + +const BUFFER_HEIGHT: usize = 25; +const BUFFER_WIDTH: usize = 80; + +#[repr(transparent)] +struct Buffer { + chars: [[TerminalChar; BUFFER_WIDTH]; BUFFER_HEIGHT], +} + +pub struct Writer { + column_position: usize, + color_code: ColorCode, + buffer: &'static mut Buffer, +} + +impl Writer { + fn write_byte(&mut self, data: u8) { + if self.column_position + 1 < BUFFER_WIDTH { + let terminal_char = TerminalChar { + ascii_char: data, + color_code: self.color_code, + }; + self.buffer.chars[BUFFER_HEIGHT - 1][self.column_position + 1] = terminal_char; + self.column_position += 1; + } + } +} + +pub fn write_string(data: &str) {}