diff --git a/src/Console.cc b/src/Console.cc index 56ef18f..65282a4 100644 --- a/src/Console.cc +++ b/src/Console.cc @@ -32,20 +32,64 @@ makeVGAEntry(char c, Console::Console() : mBase(reinterpret_cast(0xB8000)), - mCursor{0, 0} + mCursor{0, 0}, + mColor(makeVGAColor(Console::Color::LightGray, Console::Color::Black)) { } void Console::clear(Console::Color color) { - const uint16_t vgaColor = makeVGAColor(Color::LightGray, color); + setColor(Color::LightGray, color); for (size_t y = 0; y < Console::Height; y++) { for (size_t x = 0; x < Console::Width; x++) { - const size_t index = y * Console::Width + x; - mBase[index] = makeVGAEntry(' ', vgaColor); + putEntryAt(x, y, ' ', mColor); } } } + +void +Console::writeChar(char c) +{ + putEntryAt(mCursor.col, mCursor.row, c, mColor); + if (++mCursor.col == Console::Width) { + mCursor.col = 0; + if (++mCursor.row == Console::Height) { + mCursor.row = 0; + } + } +} + +void +Console::writeString(const char *str) +{ + // XXX: THIS IS VERY UNSAFE. PUT DOWN THE POINTER ERYN. NO BAD ERYN DONT YOU DARE. + const char *cur = str; + while (*cur != '\0') { + writeChar(*cur++); + } +} + +void +Console::setColor(Console::Color fg, + Console::Color bg) +{ + mColor = makeVGAColor(fg, bg); +} + +/* + * Private + */ + +void +Console::putEntryAt(size_t x, + size_t y, + char c, + uint8_t color) +{ + const size_t index = y * Console::Width + x; + mBase[index] = makeVGAEntry(c, color); +} + } /* namespace kernel */ diff --git a/src/Console.hh b/src/Console.hh index 531e9ab..b205d5f 100644 --- a/src/Console.hh +++ b/src/Console.hh @@ -46,9 +46,20 @@ struct Console /** Clear the console to the provided color. */ void clear(Color color = Color::Black); + /** Write a character to the terminal at the current cursor position. */ + void writeChar(char c); + + /** Write a string to the terminal at the current cursor position. */ + void writeString(const char *str); + + void setColor(Color fg, Color bg); + private: uint16_t *const mBase; Cursor mCursor; + uint8_t mColor; + + void putEntryAt(size_t x, size_t y, char c, uint8_t color); }; } /* namespace kernel */ diff --git a/src/Main.cc b/src/Main.cc index 3e50991..15fa7ce 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -1,5 +1,6 @@ #include #include +#include "Console.hh" #if defined(__linux__) #error "This file should be compiled with a cross-compiler, not the Linux system compiler!" @@ -13,7 +14,11 @@ extern "C" void kearly() -{ } +{ + kernel::Console console; + console.clear(kernel::Console::Color::Blue); + console.writeString("Hello world!"); +} /** The beginning of the world... */ diff --git a/src/SConscript b/src/SConscript index b0243ff..d609834 100644 --- a/src/SConscript +++ b/src/SConscript @@ -9,7 +9,7 @@ files = [ 'boot.s', 'crti.s', 'crtn.s', - 'main.cc', + 'Main.cc', 'Console.cc' ]