Split up source and added basic support for different targets.
This commit is contained in:
26
targets/_any/src/os/panic.cpp
Normal file
26
targets/_any/src/os/panic.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
#include "os/panic.hpp"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include "os/tty.hpp"
|
||||
|
||||
void panic(const char* message) noexcept
|
||||
{
|
||||
tty::write("Kernel panic!\n");
|
||||
tty::write(message);
|
||||
tty::putChar('\n');
|
||||
std::abort();
|
||||
}
|
||||
|
||||
void panicf(const char* format, ...) noexcept
|
||||
{
|
||||
tty::write("Kernel panic!\n");
|
||||
va_list parameters;
|
||||
va_start(parameters, format);
|
||||
std::vprintf(format, parameters);
|
||||
va_end(parameters);
|
||||
tty::putChar('\n');
|
||||
std::abort();
|
||||
}
|
||||
93
targets/_any/src/os/tty.cpp
Normal file
93
targets/_any/src/os/tty.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
|
||||
#include "os/tty.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace tty
|
||||
{
|
||||
namespace
|
||||
{
|
||||
inline const size_t VGA_WIDTH = 80;
|
||||
inline const size_t VGA_HEIGHT = 25;
|
||||
|
||||
size_t gTerminalRow = 0;
|
||||
size_t gTerminalColumn = 0;
|
||||
VgaDoubleColor gTerminalColor = VgaDoubleColor();
|
||||
uint16_t* gTerminalBuffer = reinterpret_cast<uint16_t*>(0xB8000);
|
||||
|
||||
constexpr uint8_t vgaEntryColor(VgaDoubleColor color)
|
||||
{
|
||||
return static_cast<uint8_t>(color.foreground) | static_cast<uint8_t>(color.background) << 4;
|
||||
}
|
||||
|
||||
constexpr uint16_t vgaEntry(const unsigned char chr, const VgaDoubleColor color)
|
||||
{
|
||||
return static_cast<uint16_t>(chr) | static_cast<uint16_t>(vgaEntryColor(color) << 8);
|
||||
}
|
||||
|
||||
void newline()
|
||||
{
|
||||
gTerminalColumn = 0;
|
||||
if (gTerminalRow < VGA_HEIGHT - 1)
|
||||
{
|
||||
++gTerminalRow;
|
||||
}
|
||||
else
|
||||
{
|
||||
// scrolling up
|
||||
std::memmove(&gTerminalBuffer[0], &gTerminalBuffer[VGA_WIDTH], VGA_WIDTH * (VGA_HEIGHT - 1) * sizeof(uint16_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initialize()
|
||||
{
|
||||
for (size_t y = 0; y < VGA_HEIGHT; y++)
|
||||
{
|
||||
for (size_t x = 0; x < VGA_WIDTH; x++)
|
||||
{
|
||||
const size_t index = y * VGA_WIDTH + x;
|
||||
gTerminalBuffer[index] = vgaEntry(' ', gTerminalColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setColor(VgaDoubleColor color)
|
||||
{
|
||||
gTerminalColor = color;
|
||||
}
|
||||
|
||||
void putEntryAt(char chr, VgaDoubleColor color, size_t posX, size_t posY)
|
||||
{
|
||||
const size_t index = posY * VGA_WIDTH + posX;
|
||||
gTerminalBuffer[index] = vgaEntry(chr, color);
|
||||
}
|
||||
|
||||
void putChar(char chr)
|
||||
{
|
||||
if (chr == '\n')
|
||||
{
|
||||
newline();
|
||||
return;
|
||||
}
|
||||
|
||||
putEntryAt(chr, gTerminalColor, gTerminalColumn, gTerminalRow);
|
||||
if (++gTerminalColumn == VGA_WIDTH)
|
||||
{
|
||||
newline();
|
||||
}
|
||||
}
|
||||
|
||||
void write(const char* data, size_t size)
|
||||
{
|
||||
for (size_t idx = 0; idx < size; idx++)
|
||||
{
|
||||
putChar(data[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
void write(const char* data)
|
||||
{
|
||||
write(data, std::strlen(data));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user