Fancy fancy numeric module.
This commit is contained in:
parent
99704be202
commit
e8666e90ae
11 changed files with 223 additions and 8 deletions
20
prototypes/base/include/bsod.hpp
Normal file
20
prototypes/base/include/bsod.hpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
|
||||
#include "errors.hpp"
|
||||
|
||||
/**
|
||||
* This class provides the blue screen of death.
|
||||
* Discaimer: Color may vary!
|
||||
*/
|
||||
class BSOD
|
||||
{
|
||||
private:
|
||||
BSOD() = delete;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Dies with a simple message and error code display.
|
||||
*/
|
||||
static void die(Error code, const char *msg);
|
||||
|
||||
};
|
|
@ -33,6 +33,11 @@ private:
|
|||
void updateCaret();
|
||||
public:
|
||||
Console(Screen *screen);
|
||||
|
||||
/**
|
||||
* Clears the console.
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* Puts a character on the screen.
|
||||
|
@ -106,4 +111,10 @@ public:
|
|||
this->bg = color.color;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Console & operator << (uint32_t value);
|
||||
|
||||
Console & operator << (int32_t value);
|
||||
|
||||
Console & operator << (void *value);
|
||||
};
|
10
prototypes/base/include/errors.hpp
Normal file
10
prototypes/base/include/errors.hpp
Normal file
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
enum class Error
|
||||
{
|
||||
Success = 0,
|
||||
OutOfMemory = 1,
|
||||
|
||||
|
||||
|
||||
};
|
39
prototypes/base/include/numeric.hpp
Normal file
39
prototypes/base/include/numeric.hpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
|
||||
class Numeric
|
||||
{
|
||||
private:
|
||||
Numeric() = delete;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Converts an unsigned number to a string.
|
||||
* @param buffer The target buffer where the string should be stored.
|
||||
* @param length The length of the buffer.
|
||||
* @param number The number that should be converted.
|
||||
* @param radix The numeric base for the printed number.
|
||||
* @return The length of the converted strings.
|
||||
*/
|
||||
static size_t toString(
|
||||
char *buffer,
|
||||
size_t length,
|
||||
uint32_t number,
|
||||
uint32_t radix = 10);
|
||||
|
||||
/**
|
||||
* Converts a signed number to a string.
|
||||
* @param buffer The target buffer where the string should be stored.
|
||||
* @param length The length of the buffer.
|
||||
* @param number The number that should be converted.
|
||||
* @param radix The numeric base for the printed number.
|
||||
* @return The length of the converted strings.
|
||||
*/
|
||||
static size_t toString(
|
||||
char *buffer,
|
||||
size_t length,
|
||||
int32_t number,
|
||||
uint32_t radix = 10);
|
||||
};
|
|
@ -1,7 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* Physical memory management tool.
|
||||
*/
|
||||
|
|
|
@ -51,7 +51,7 @@ public:
|
|||
|
||||
ScreenChar & operator ()(int x, int y) {
|
||||
if(x < 0 || y < 0 || x >= this->width || y >= this->height) {
|
||||
// return Screen::outOfScreen;
|
||||
return Screen::outOfScreen;
|
||||
}
|
||||
return this->buffer[this->width * y + x];
|
||||
}
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
#include <inttypes.h>
|
||||
|
||||
#include "console.hpp"
|
||||
#include "pmm.hpp"
|
||||
#include "numeric.hpp"
|
||||
#include "compat.h"
|
||||
|
||||
const char * numbers[] = {
|
||||
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
|
||||
};
|
||||
|
||||
extern "C" void init(void)
|
||||
{
|
||||
Console::main
|
||||
|
@ -14,4 +12,10 @@ extern "C" void init(void)
|
|||
<< FColor(Color::Yellow) << "Hello color!" << FColor() << "\n"
|
||||
<< BColor(Color::Blue) << "Hello blue!" << BColor() << "\n"
|
||||
<< "Hello default color.\n";
|
||||
for(int i = 0; i < 25; i++) {
|
||||
Console::main << (-i) << "\n";
|
||||
}
|
||||
Console::main << "addrof(init) = 0x" << reinterpret_cast<void*>(0xFF00FF) << "\n";
|
||||
}
|
||||
|
||||
static_assert(sizeof(void*) == 4, "Target platform is not 32 bit.");
|
6
prototypes/base/src/bsod.cpp
Normal file
6
prototypes/base/src/bsod.cpp
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include "bsod.hpp"
|
||||
|
||||
void BSOD::die(Error code, const char *msg)
|
||||
{
|
||||
__asm__ volatile ("cli");
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
#include "console.hpp"
|
||||
#include "numeric.hpp"
|
||||
|
||||
Console Console::main(&Screen::main);
|
||||
|
||||
|
@ -11,6 +12,21 @@ Console::Console(Screen *screen) :
|
|||
this->updateCaret();
|
||||
}
|
||||
|
||||
void Console::clear()
|
||||
{
|
||||
for(int y = 0; y < this->screen->height; y++) {
|
||||
for(int x = 0; x < this->screen->width; x++) {
|
||||
auto &c = (*this->screen)(x, y);
|
||||
c.c = ' ';
|
||||
c.fg = (int)this->fg;
|
||||
c.bg = (int)this->bg;
|
||||
}
|
||||
}
|
||||
this->x = 0;
|
||||
this->y = 0;
|
||||
this->updateCaret();
|
||||
}
|
||||
|
||||
void Console::put(char c)
|
||||
{
|
||||
switch(c) {
|
||||
|
@ -83,4 +99,36 @@ void Console::setCaretVisible(bool visible)
|
|||
{
|
||||
this->caretEnabled = visible;
|
||||
this->updateCaret();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Console & Console::operator << (uint32_t value)
|
||||
{
|
||||
char buffer[12];
|
||||
size_t len = Numeric::toString(buffer, sizeof(buffer), value, 10);
|
||||
for(size_t i = 0; i < len; i++) {
|
||||
this->put(buffer[i]);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Console & Console::operator << (int32_t value)
|
||||
{
|
||||
char buffer[13];
|
||||
size_t len = Numeric::toString(buffer, sizeof(buffer), value, 10);
|
||||
for(size_t i = 0; i < len; i++) {
|
||||
this->put(buffer[i]);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Console & Console::operator << (void *value)
|
||||
{
|
||||
char buffer[13];
|
||||
size_t len = Numeric::toString(buffer, sizeof(buffer), reinterpret_cast<uint32_t>(value), 16);
|
||||
for(size_t i = 0; i < len; i++) {
|
||||
this->put(buffer[i]);
|
||||
}
|
||||
return *this;
|
||||
}
|
69
prototypes/base/src/numeric.cpp
Normal file
69
prototypes/base/src/numeric.cpp
Normal file
|
@ -0,0 +1,69 @@
|
|||
#include "numeric.hpp"
|
||||
|
||||
static char getDigit(uint32_t i)
|
||||
{
|
||||
if(i >= 0 && i <= 9) {
|
||||
return '0' + i;
|
||||
}
|
||||
return 'A' + (i-10);
|
||||
}
|
||||
|
||||
size_t Numeric::toString(
|
||||
char *buffer,
|
||||
size_t length,
|
||||
uint32_t number,
|
||||
uint32_t radix)
|
||||
{
|
||||
if(length == 0) {
|
||||
return 0;
|
||||
}
|
||||
if(number == 0) {
|
||||
buffer[0] = '0';
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t len = 0;
|
||||
while(number > 0)
|
||||
{
|
||||
buffer[len++] = getDigit(number % radix);
|
||||
if(len >= length)
|
||||
break;
|
||||
number /= radix;
|
||||
}
|
||||
|
||||
int half = len / 2;
|
||||
for(int i = 0; i < half; i++)
|
||||
{
|
||||
char c = buffer[i];
|
||||
buffer[i] = buffer[len - i - 1];
|
||||
buffer[len - i - 1] = c;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t Numeric::toString(
|
||||
char *buffer,
|
||||
size_t length,
|
||||
int32_t number,
|
||||
uint32_t radix)
|
||||
{
|
||||
if(length == 0) {
|
||||
return 0;
|
||||
}
|
||||
if(number == 0) {
|
||||
buffer[0] = '0';
|
||||
return 1;
|
||||
}
|
||||
if(number < 0) {
|
||||
buffer[0] = '-';
|
||||
length -= 1;
|
||||
if(length == 0) {
|
||||
return 0;
|
||||
}
|
||||
return Numeric::toString(&buffer[1], length, static_cast<uint32_t>(-number), radix) + 1;
|
||||
}
|
||||
else {
|
||||
return Numeric::toString(buffer, length, static_cast<uint32_t>(number), radix);
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
#include "pmm.hpp"
|
||||
|
||||
|
||||
/**
|
||||
* Number stored of pages in the bitmap
|
||||
*/
|
||||
|
@ -19,16 +19,25 @@ static const uint32_t BitmapLength = BitmapSize / 32;
|
|||
*/
|
||||
static uint32_t bitmap[BitmapLength];
|
||||
|
||||
/**
|
||||
* Casts a pointer to an integer.
|
||||
*/
|
||||
static uint32_t ptrcast(void *ptr)
|
||||
{
|
||||
return reinterpret_cast<uint32_t>(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts an integer to a pointer
|
||||
*/
|
||||
static void *ptrcast(uint32_t ptr)
|
||||
{
|
||||
return reinterpret_cast<void*>(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an interger is 4096-aligned
|
||||
*/
|
||||
static bool isAligned(uint32_t ptr)
|
||||
{
|
||||
return (ptr % 4096) == 0;
|
||||
|
@ -84,5 +93,6 @@ void PMM::free(void *page)
|
|||
uint32_t idx = pageId / 32;
|
||||
uint32_t bit = pageId % 32;
|
||||
|
||||
// Mark the selected bit as free.
|
||||
bitmap[idx] &= ~(1<<bit);
|
||||
}
|
Loading…
Reference in a new issue