KraKern

C++ Kernel
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/KraKern

commit e30114e9b0f515a33600eb25cbff858f767ae656
parent 119ae69bc425acd9b77fe7bee7d5d30bdaf5bb6e
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 22 Feb 2016 14:28:37 +0100

Add(printk): Add a printk like function in Io class

Diffstat:
Mincludes/core/Io.hpp | 4+++-
Mkernel/Io.cpp | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Mkernel/Kernel.cpp | 7++-----
3 files changed, 72 insertions(+), 25 deletions(-)

diff --git a/includes/core/Io.hpp b/includes/core/Io.hpp @@ -4,6 +4,7 @@ # include <os.h> # include <kern_types.h> # include <base_lib.h> +# include <stdarg.h> // X86 Defines # define RAM_SCREEN 0xB8000 @@ -47,10 +48,10 @@ class Io { u32 read(char* buf,u32 count); void putc(char c); void puts(const char *s); + void printk(const char *s, ...); void set_color(char color); void setXY(char xc,char yc); void clear(void); - void print(const char *s, ...); u32 getX(void); u32 getY(void); void switch_tty(void); @@ -78,6 +79,7 @@ class Io { int _inlock; int _keystate; int _in_loading; + void _move_cursor(void); char _x; char _y; diff --git a/kernel/Io.cpp b/kernel/Io.cpp @@ -85,6 +85,58 @@ void Io::putc(char c) { } if (this->_y > 24) scrollup(this->_y - 24); + this->_move_cursor(); +} + +void Io::printk(const char *s, ...) { + va_list arg; + char buf[16]; + u32 size; + + va_start(arg, s); + for (; *s; s++) { + size = 0; + if (*s == '%' && *(s + 1)) { + s++; + if (*s >= '0' && *s <= '9') { + size = *s - '0'; + s++; + } if (*s == 's') { + this->puts(va_arg(arg, char *)); + } else if (*s == 'd' || *s == 'u') { + int tmp = va_arg(arg, int); + if (tmp < 0) { + this->putc('-'); + tmp = ~tmp + 1; + } + itoa(buf, tmp, 10); + if (strlen(buf) < size) + for (int j = strlen(buf), i = size; i >= 0; i--, j--) + buf[i] = (j >= 0 ? buf[j] : '0'); + this->puts(buf); + } else if (*s == 'x') { + itoa(buf, va_arg(arg, int), 16); + if (strlen(buf) < size) + for (int j = strlen(buf), i = size; i >= 0; i--, j--) + buf[i] = (j >= 0 ? buf[j] : '0'); + this->puts(buf); + } else if (*s == 'p') { + itoa(buf, va_arg(arg, int), 16); + if (strlen(buf) < 4) + for (int j = strlen(buf), i = 4; i >= 0; i--, j--) + buf[i] = (j >= 0 ? buf[j] : '0'); + this->puts("0x"); + this->puts(buf); + } else if (*s == 'c') { + this->putc(va_arg(arg, int)); + } else if (*s == 'e') { + this->set_color(va_arg(arg, int)); + } + } else { + this->putc(*s); + } + } + this->set_color(COLOR_WHITE); } void Io::clear(void) { @@ -131,34 +183,30 @@ void Io::loading(const char *s) { } void Io::done(void) { - this->puts("["); - this->set_color(COLOR_GREEN); - this->puts(" DONE "); - this->set_color(COLOR_WHITE); - this->puts("]\n"); + this->printk("[%e DONE %e]\n", COLOR_GREEN, COLOR_WHITE); this->_in_loading = 0; } void Io::panic(const char *s) { - if (this->_in_loading) { - this->puts("["); - this->set_color(COLOR_RED); - this->puts(" FAIL "); - this->set_color(COLOR_WHITE); - this->puts("]"); - this->_in_loading = 0; - } - this->puts("\n[ "); - this->set_color(COLOR_RED); - this->puts("PANIC"); - this->set_color(COLOR_WHITE); - this->puts(" ]: "); - this->puts(s); + if (this->_in_loading) + this->printk("[%e FAIL %e]", COLOR_RED, COLOR_WHITE); + this->printk("\n[%e PANIC %e]: %s", COLOR_RED, COLOR_WHITE, s); asm("hlt"); while (1); // Reboot } +void Io::_move_cursor(void) { + u16 c_pos; + + c_pos = this->_y * 80 + this->_x; + + this->obyte(0x3d4, 0x0f); + this->obyte(0x3d5, (u8) c_pos); + this->obyte(0x3d4, 0x0e); + this->obyte(0x3d5, (u8) (c_pos >> 8)); +} + // Output void Io::obyte(u32 a, u8 v) { asmv("outb %%al, %%dx" :: "d" (a), "a" (v));; }; void Io::oword(u32 a, u16 v) { asmv("outw %%ax, %%dx" :: "d" (a), "a" (v));; }; diff --git a/kernel/Kernel.cpp b/kernel/Kernel.cpp @@ -43,7 +43,7 @@ void Kernel::init(multiboot_info *mbi) { this->pt.init(); this->ready(); __kern.io.puts("\n"); - list_files(this->fs.get_root(), 0); + //list_files(this->fs.get_root(), 0); while (1); } @@ -74,8 +74,5 @@ void Kernel::ready(void) { __kern.io.set_color(COLOR_LIGHT_GREY); for (int i = 0; i < 26; i++, this->io.puts("=")); __kern.io.set_color(COLOR_WHITE); - __kern.io.puts("Date: "); - __kern.io.puts(getTextDate(*h)); - __kern.io.puts(" "); - __kern.io.puts(getTextTime(*h)); + __kern.io.printk("Date: %s %s\n", getTextDate(*h), getTextTime(*h)); }