From 760884de78377935896272237b1300d721da3654 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 27 Feb 2016 15:07:52 -0500 Subject: [PATCH] Console can scroll now! --- src/Console.cc | 49 +++++++++++++++++++++++++++++++++++++++++-------- src/Console.hh | 4 +++- src/Main.cc | 19 +++++++++++++++++-- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/Console.cc b/src/Console.cc index 9966386..c24292f 100644 --- a/src/Console.cc +++ b/src/Console.cc @@ -38,9 +38,8 @@ Console::Console() void -Console::clear(Console::Color color) +Console::clear() { - setColor(Color::LightGray, color); for (size_t y = 0; y < Console::Height; y++) { for (size_t x = 0; x < Console::Width; x++) { putEntryAt(x, y, ' ', mColor); @@ -49,21 +48,31 @@ Console::clear(Console::Color color) } +void +Console::clear(Console::Color color) +{ + setColor(Color::LightGray, color); + clear(); +} + + void Console::writeChar(char c) { switch (c) { case '\n': mCursor.col = 0; - if (++mCursor.row == Console::Height) { - mCursor.row = 0; + if (++mCursor.row >= Console::Height) { + scroll(); + mCursor.row = Console::Height - 1; } break; case '\t': mCursor.col += 8; if (mCursor.col >= Console::Width) { - if (++mCursor.row == Console::Height) { - mCursor.row = 0; + if (++mCursor.row >= Console::Height) { + scroll(); + mCursor.row = Console::Height - 1; } mCursor.col %= Console::Width; } @@ -72,8 +81,9 @@ 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; + if (++mCursor.row >= Console::Height) { + scroll(); + mCursor.row = Console::Height - 1; } } break; @@ -111,4 +121,27 @@ Console::putEntryAt(size_t x, mBase[index] = makeVGAEntry(c, color); } +void +Console::scroll(size_t lines) +{ + if (lines == 0) { + return; + } + + if (lines > Console::Height) { + clear(); + return; + } + + uint16_t *dst = mBase; + uint16_t *src = mBase + lines * Console::Width; + uint16_t *const end = mBase + Console::Width * Console::Height; + while (src < end) { + *dst++ = *src++; + } + while (dst < end) { + *dst++ = makeVGAEntry(' ', mColor); + } +} + } /* namespace kernel */ diff --git a/src/Console.hh b/src/Console.hh index b205d5f..dcc0537 100644 --- a/src/Console.hh +++ b/src/Console.hh @@ -44,7 +44,8 @@ struct Console Console(); /** Clear the console to the provided color. */ - void clear(Color color = Color::Black); + void clear(); + void clear(Color color); /** Write a character to the terminal at the current cursor position. */ void writeChar(char c); @@ -60,6 +61,7 @@ private: uint8_t mColor; void putEntryAt(size_t x, size_t y, char c, uint8_t color); + void scroll(size_t lines = 1); }; } /* namespace kernel */ diff --git a/src/Main.cc b/src/Main.cc index 77d0270..ebdab01 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -18,8 +18,23 @@ kearly() kernel::Console console; console.clear(kernel::Console::Color::Blue); console.writeString("Hello world!\n"); - console.writeString("a\nb\nc\n"); - console.writeString("abc\tdef\tghi\tjkl\tmno\n"); + + volatile int foo = 0; + int j = 0; + int i = 0; + for (;;) { + if (j == 0) { + console.writeString("--- MARK ---\n"); + } + console.writeChar('a' + i); + console.writeChar('\n'); + i = (i + 1) % 26; + j = (j + 1) % 500; + + for (uint32_t k = 0; k < (2u << 27) - 1; k++) { + foo /= 2; + } + } }