polka/src/Console.cc

96 lines
1.8 KiB
C++
Raw Normal View History

2016-02-27 12:51:25 -05:00
/* Console.cc
* vim: set tw=80:
* Eryn Wells <eryn@erynwells.me>
*/
/**
* Implementation of the Console class.
*/
#include "Console.hh"
2016-02-27 13:02:49 -05:00
namespace kernel {
2016-02-27 12:51:25 -05:00
/** Create a VGA color pair. */
static inline uint8_t
makeVGAColor(Console::Color fg,
Console::Color bg)
{
2016-02-27 13:02:49 -05:00
return uint8_t(fg) | uint8_t(bg) << 4;
2016-02-27 12:51:25 -05:00
}
2016-02-27 13:02:49 -05:00
2016-02-27 12:51:25 -05:00
/** Create a VGA character entry. */
static inline uint16_t
makeVGAEntry(char c,
uint8_t color)
{
uint16_t c16 = c;
uint16_t color16 = color;
return c16 | color16 << 8;
}
Console::Console()
2016-02-27 13:02:49 -05:00
: mBase(reinterpret_cast<uint16_t *>(0xB8000)),
2016-02-27 13:50:51 -05:00
mCursor{0, 0},
mColor(makeVGAColor(Console::Color::LightGray, Console::Color::Black))
2016-02-27 12:51:25 -05:00
{ }
2016-02-27 13:02:49 -05:00
void
2016-02-27 12:51:25 -05:00
Console::clear(Console::Color color)
{
2016-02-27 13:50:51 -05:00
setColor(Color::LightGray, color);
2016-02-27 12:51:25 -05:00
for (size_t y = 0; y < Console::Height; y++) {
for (size_t x = 0; x < Console::Width; x++) {
2016-02-27 13:50:51 -05:00
putEntryAt(x, y, ' ', mColor);
2016-02-27 12:51:25 -05:00
}
}
}
2016-02-27 13:50:51 -05:00
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);
}
2016-02-27 12:51:25 -05:00
} /* namespace kernel */