From 8734cd4da6cb3c8e4ae152e3190ed43476f0b521 Mon Sep 17 00:00:00 2001 From: lordtet Date: Sun, 8 Jun 2025 16:18:06 -0400 Subject: [PATCH] Added some functions, did some reorganizing. kttools.h/c is a lib for converting types (only currently contains i_to_str). Also added a printf for i/o. --- include/{kernio.h => kio.h} | 12 ++++---- include/kttools.h | 7 +++++ src/kernio.c | 57 ------------------------------------- src/kio.c | 45 +++++++++++++++++++++++++++-- src/kttools.c | 6 ++-- src/main.c | 17 ++++++++--- 6 files changed, 72 insertions(+), 72 deletions(-) rename include/{kernio.h => kio.h} (77%) create mode 100644 include/kttools.h delete mode 100644 src/kernio.c diff --git a/include/kernio.h b/include/kio.h similarity index 77% rename from include/kernio.h rename to include/kio.h index fed80d7..ff1cc13 100644 --- a/include/kernio.h +++ b/include/kio.h @@ -1,8 +1,9 @@ -#ifndef KERNIO_H -#define KERNIO_H -#define VGA_GRID_COLS 79 +#ifndef KIO_H +#define KIO_H +#define VGA_GRID_COLS 80 #define VGA_GRID_ROWS 25 #include +#include /* * CONSTANTS AND VARIABLES @@ -29,7 +30,8 @@ void vga_putc(char c); void vga_set_attributes(uint8_t attributes); void vga_print(const char* out); void vga_println(const char* out); - - +void vga_printhex(uint32_t out); +void vga_prindec(uint32_t out); +void vga_printf(const char* fmt, ...); #endif diff --git a/include/kttools.h b/include/kttools.h new file mode 100644 index 0000000..c15ce07 --- /dev/null +++ b/include/kttools.h @@ -0,0 +1,7 @@ +#ifndef KTTOOLS_H +#define KTTOOLS_H +#include + +void i_to_str(uint32_t num, char* buf, int size, int radix); + +#endif diff --git a/src/kernio.c b/src/kernio.c deleted file mode 100644 index ac32fec..0000000 --- a/src/kernio.c +++ /dev/null @@ -1,57 +0,0 @@ -#include "kernio.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; - -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/kio.c b/src/kio.c index 38f027f..a99a8ce 100644 --- a/src/kio.c +++ b/src/kio.c @@ -1,4 +1,5 @@ #include "kio.h" +#include "kttools.h" #include /* @@ -34,7 +35,8 @@ void vga_putc(char c) //Check for some freaky escape character first if(c == '\n') { cursor_col = 0; - cursor_row = (cursor_row + 1) % 25; + //mod implements wraparound + cursor_row = (cursor_row + 1) % (VGA_GRID_ROWS-1); return; } //Calculate where in the vga buffer to put the character @@ -49,8 +51,7 @@ void vga_set_attributes(uint8_t attributes) { } -void vga_print(const char* out) -{ +void vga_print(const char* out) { for (int i = 0; out[i] != '\0'; i++) vga_putc(out[i]); } @@ -59,3 +60,41 @@ void vga_println(const char* out) { vga_print(out); vga_print("\n"); } + +void vga_printhex(uint32_t out) { + char buff[9]; + i_to_str(out, buff, 9, 0x10); + vga_print(buff); +} + +void vga_printdec(uint32_t out) { + char buff[11]; + i_to_str(out, buff, 11, 10); + vga_print(buff); +} + +void vga_printf(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + + while(*fmt) { + if(*fmt == '%') { + fmt++; + switch(*fmt) { + case 'X': + vga_print("0x"); + case 'x': + vga_printhex(va_arg(args,uint32_t)); + break; + case 'd': + vga_printdec(va_arg(args,uint32_t)); + break; + + } + } else { + vga_putc(*fmt); + } + fmt++; + } + +} diff --git a/src/kttools.c b/src/kttools.c index 1471e4d..1412ee6 100644 --- a/src/kttools.c +++ b/src/kttools.c @@ -28,16 +28,16 @@ * @return No return value * */ -void i_to_str(uint32_t num, char* buf, int size) { +void i_to_str(uint32_t num, char* buf, int size, int radix) { //null terminate the string buf[--size] = '\0'; while(size > 0 && (num) != 0){ - int isolated_num = num % 0x10; + int isolated_num = num % radix; if(isolated_num > 9){ isolated_num+=7; } buf[--size] = '0' + isolated_num; - num/=0x10; + num/=radix; } //now shift the whole thing to the left diff --git a/src/main.c b/src/main.c index 1fe04b2..bad066f 100644 --- a/src/main.c +++ b/src/main.c @@ -20,10 +20,19 @@ void kern_main(uint32_t multiboot_magic, multiboot_info_t* multiboot_info) //wipe the screen vga_clear(); - //IT IS TIME. TO PRINT. - char lol[9]; - i_to_str(multiboot_info->mem_upper, lol, 9); + //We're going to use this buffer as our 8char hex representation for reading mem - vga_println(lol); + vga_printf("Entry eax:%X\n", multiboot_magic); + + if(multiboot_magic != 0x2BADB002) { + vga_println("Bootloader not multiboot1 compliant! Needed for mmap, etc. Can't work without it, kthxbye!"); + return; + } else { + vga_println("Multiboot detected! Continuing..."); + } + + vga_printf("MEM_LOWER:%X\n", multiboot_info->mem_lower); + vga_printf("MEM_UPPER:%X\n", multiboot_info->mem_upper); + }