Console can scroll now!

This commit is contained in:
Eryn Wells 2016-02-27 15:07:52 -05:00
parent 42ac9ebe86
commit 760884de78
3 changed files with 61 additions and 11 deletions

View file

@ -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 */

View file

@ -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 */

View file

@ -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;
}
}
}