From e8a6c514ad8b6aac7d2c8579cb55a74729fd9644 Mon Sep 17 00:00:00 2001 From: lordtet Date: Sun, 8 Jun 2025 01:27:41 -0400 Subject: [PATCH] kernel tools split into kio (for i/o) and kttools (kernel type tools). in general, kernel libraries will be marked with "k" in the beginning. --- src/kio.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/kttools.c | 52 +++++++++++++++++++++++++++++++++++++++++++ src/main.c | 44 +++++++++++++------------------------ src/start.s | 3 ++- 4 files changed, 130 insertions(+), 30 deletions(-) create mode 100644 src/kio.c create mode 100644 src/kttools.c diff --git a/src/kio.c b/src/kio.c new file mode 100644 index 0000000..38f027f --- /dev/null +++ b/src/kio.c @@ -0,0 +1,61 @@ +#include "kio.h" +#include + +/* + * VARS + */ + +int cursor_col = 0; +int cursor_row = 0; +volatile uint16_t* const vga_buffer = (uint16_t*)0xB8000; +uint16_t vga_attributes = 0x0F00; + +/* + * FUNCTIONS + */ + +void vga_clear() { + for (int col = 0; col < VGA_GRID_COLS; col ++) { + for (int row = 0; row < VGA_GRID_ROWS; row ++) { + //works out to iterating every cell + const size_t index = (VGA_GRID_COLS * row) + col; + //vga buffer looks something like xxxxyyyyzzzzzzzz + //x=bg color + //y=fg color + //c=character to use + //Therefore, to write, we just take our color data and tack on the character to the end. + vga_buffer[index] = vga_attributes | ' '; //blank out + } + } +} + +void vga_putc(char c) +{ + //Check for some freaky escape character first + if(c == '\n') { + cursor_col = 0; + cursor_row = (cursor_row + 1) % 25; + return; + } + //Calculate where in the vga buffer to put the character + const size_t index = (VGA_GRID_COLS * cursor_row) + cursor_col; + //VGA buffer cell consists of the first half attributes, second half character + vga_buffer[index] = vga_attributes | c; + cursor_col++; +} + +void vga_set_attributes(uint8_t attributes) { + vga_attributes = ((uint16_t)attributes) << 8; +} + + +void vga_print(const char* out) +{ + for (int i = 0; out[i] != '\0'; i++) + vga_putc(out[i]); +} + +void vga_println(const char* out) { + vga_print(out); + vga_print("\n"); +} diff --git a/src/kttools.c b/src/kttools.c new file mode 100644 index 0000000..1471e4d --- /dev/null +++ b/src/kttools.c @@ -0,0 +1,52 @@ +#include "kttools.h" +#include +/* + * VARS + */ + +//Placeholder. + +/* + * FUNCTIONS + */ + + +//convert digits from int num to buffer buf. in hex. This is 32 bit only for now! +//WARN: integer is written from right to left into the buffer, and will halt if the buffer is too small. + +/** + * @brief Convert a hex int to string, up to 32 bits. + * + * Converts int "num" into a null-terminated string, placed into buffer "buf". + * Evaluates from right to left, so left hand digits will be cut off if the buffer is too small + * Should work for non 32-bit integers actually, but is uint32_t for now. + * + * @param num: Value to convert to string + * @param buf: Memory to place the string into + * @param size: Size of the memory buffer + * + * @return No return value + * + */ +void i_to_str(uint32_t num, char* buf, int size) { + //null terminate the string + buf[--size] = '\0'; + while(size > 0 && (num) != 0){ + int isolated_num = num % 0x10; + if(isolated_num > 9){ + isolated_num+=7; + } + buf[--size] = '0' + isolated_num; + num/=0x10; + } + + //now shift the whole thing to the left + if(size != 0) { + while(buf[size]!='\0') { + *buf = buf[size]; + buf++; + } + *buf = '\0'; + } +} + diff --git a/src/main.c b/src/main.c index ec75c19..1fe04b2 100644 --- a/src/main.c +++ b/src/main.c @@ -1,43 +1,29 @@ //Our own code, at this point... //#include #include -#include "kernio.h" - -//convert digits from int num to buffer buf. in hex. This is 32 bit only for now! -//WARN: integer is written from right to left into the buffer, and will halt if the buffer is too small. -void i_to_str(uint32_t num, char* buf, int size) { - //null terminate the string - buf[--size] = '\0'; - while(size > 0 && (num) != 0){ - int isolated_num = num % 0x10; - if(isolated_num > 9){ - isolated_num+=7; - } - buf[--size] = '0' + isolated_num; - num/=0x10; - } - - //now shift the whole thing to the left - if(size != 0) { - while(buf[size]!='\0') { - *buf = buf[size]; - buf++; - } - *buf = '\0'; - } -} +#include "kio.h" +#include "kttools.h" +typedef struct { + uint32_t flags; + uint32_t mem_lower; + uint32_t mem_upper; + uint32_t boot_device; + uint32_t cmdline; + uint32_t mods_count; + uint32_t mods_addr; +} multiboot_info_t; //finally, main. -void kern_main() +void kern_main(uint32_t multiboot_magic, multiboot_info_t* multiboot_info) { - //wipe the screen vga_clear(); //IT IS TIME. TO PRINT. - char lol[5]; - i_to_str(0xCAFEBABE, lol, 5); + char lol[9]; + i_to_str(multiboot_info->mem_upper, lol, 9); + vga_println(lol); } diff --git a/src/start.s b/src/start.s index 8643d4e..5f2dbaa 100644 --- a/src/start.s +++ b/src/start.s @@ -36,7 +36,8 @@ start: //Lets set up the stack. Stack grows downward on x86. We did the work earlier of defining where the top of the stack is, so just tell esp. mov $stack_top, %esp //set the stack pointer - + pushl %ebx + pushl %eax //To C-land! call kern_main