Implemented a kinda broken tty

This commit is contained in:
Alex
2022-10-09 04:01:49 +03:00
parent e332fc57e0
commit 7c4d43fec3
11 changed files with 496 additions and 133 deletions

View File

@ -28,7 +28,7 @@ void tracepagetable(PageTable *pt)
for (int i = 0; i < 512; i++)
{
if (pt->Entries[i].Value.Present)
debug("Entry %d: %x %x %x %x %x %x %x %x %x %x %x %p-%#lx", i,
debug("Entry %03d: %x %x %x %x %x %x %x %x %x %x %x %p-%#lx", i,
pt->Entries[i].Value.Present, pt->Entries[i].Value.ReadWrite,
pt->Entries[i].Value.UserSupervisor, pt->Entries[i].Value.WriteThrough,
pt->Entries[i].Value.CacheDisable, pt->Entries[i].Value.Accessed,

View File

@ -118,6 +118,13 @@ namespace Memory
this->Unmap((void *)((uint64_t)VirtualAddress + (i * PAGE_SIZE)));
}
Virtual::Virtual(PageTable *Table) { this->Table = Table; }
Virtual::Virtual(PageTable *Table)
{
if (Table)
this->Table = Table;
else
this->Table = (PageTable *)CPU::PageTable();
}
Virtual::~Virtual() {}
}

111
core/Video/Display.cpp Normal file
View File

@ -0,0 +1,111 @@
#include <display.hpp>
#include <lock.hpp>
#include <debug.h>
extern uint64_t _binary_files_ter_powerline_v12n_psf_start;
extern uint64_t _binary_files_ter_powerline_v12n_psf_end;
extern uint64_t _binary_files_ter_powerline_v12n_psf_size;
NEWLOCK(PrintLock);
namespace Video
{
char Display::Print(char Char, int Index)
{
SMARTLOCK(PrintLock);
switch (Char)
{
case '\b':
{
if (Buffers[Index]->CursorX > 0)
Buffers[Index]->CursorX -= this->GetCurrentFont()->GetInfo().Width;
return Char;
}
case '\t':
{
Buffers[Index]->CursorX = (Buffers[Index]->CursorX + 8) & ~(8 - 1);
return Char;
}
case '\r':
{
Buffers[Index]->CursorX = 0;
return Char;
}
case '\n':
{
Buffers[Index]->CursorX = 0;
Buffers[Index]->CursorY += this->GetCurrentFont()->GetInfo().Height;
return Char;
}
}
if (Buffers[Index]->CursorX + this->GetCurrentFont()->GetInfo().Width >= Buffers[Index]->Width)
{
Buffers[Index]->CursorX = 0;
Buffers[Index]->CursorY += this->GetCurrentFont()->GetInfo().Height;
}
if (Buffers[Index]->CursorY + this->GetCurrentFont()->GetInfo().Height >= Buffers[Index]->Height)
{
Buffers[Index]->CursorY = Buffers[Index]->Height - this->GetCurrentFont()->GetInfo().Height;
this->SetBuffer(Index);
Scroll(Index, 1);
}
if (CurrentFont->GetInfo().Type == FontType::PCScreenFont2)
{
// if (CurrentFont->PSF2Font->GlyphBuffer == (uint16_t *)0x01) // HAS UNICODE TABLE
// Char = CurrentFont->PSF2Font->GlyphBuffer[Char];
int bytesperline = (CurrentFont->GetInfo().PSF2Font->Header->width + 7) / 8;
char *FontPtr = (char *)CurrentFont->GetInfo().StartAddress +
CurrentFont->GetInfo().PSF2Font->Header->headersize +
(Char > 0 && (unsigned char)Char < CurrentFont->GetInfo().PSF2Font->Header->length ? Char : 0) *
CurrentFont->GetInfo().PSF2Font->Header->charsize;
for (unsigned long Y = Buffers[Index]->CursorY; Y < Buffers[Index]->CursorY + CurrentFont->GetInfo().PSF2Font->Header->height; Y++)
{
for (unsigned long X = Buffers[Index]->CursorX; X < Buffers[Index]->CursorX + CurrentFont->GetInfo().PSF2Font->Header->width; X++)
if ((*FontPtr & (0b10000000 >> (X - Buffers[Index]->CursorX))) > 0)
*(uint32_t *)((uint64_t)Buffers[Index]->Buffer + (Y * Buffers[Index]->Width + X) * (framebuffer.BitsPerPixel / 8)) = 0xFFFFFF;
FontPtr += bytesperline;
}
Buffers[Index]->CursorX += CurrentFont->GetInfo().PSF2Font->Header->width;
return Char;
}
else if (CurrentFont->GetInfo().Type == FontType::PCScreenFont1)
{
uint32_t *PixelPtr = (uint32_t *)Buffers[Index]->Buffer;
char *FontPtr = (char *)CurrentFont->GetInfo().PSF1Font->GlyphBuffer + (Char * CurrentFont->GetInfo().PSF1Font->Header->charsize);
for (unsigned long Y = Buffers[Index]->CursorY; Y < Buffers[Index]->CursorY + 16; Y++)
{
for (unsigned long X = Buffers[Index]->CursorX; X < Buffers[Index]->CursorX + 8; X++)
if ((*FontPtr & (0b10000000 >> (X - Buffers[Index]->CursorX))) > 0)
*(unsigned int *)(PixelPtr + X + (Y * Buffers[Index]->Width)) = Buffers[Index]->Color;
FontPtr++;
}
Buffers[Index]->CursorX += 8;
return Char;
}
return Char;
}
Display::Display(BootInfo::FramebufferInfo Info, bool LoadDefaultFont)
{
framebuffer = Info;
if (LoadDefaultFont)
{
CurrentFont = new Font(&_binary_files_ter_powerline_v12n_psf_start, &_binary_files_ter_powerline_v12n_psf_end, FontType::PCScreenFont2);
FontInfo Info = CurrentFont->GetInfo();
debug("Font loaded: %dx%d %s",
Info.Width, Info.Height, Info.Type == FontType::PCScreenFont1 ? "PSF1" : "PSF2");
}
this->CreateBuffer(Info.Width, Info.Height, 0);
}
Display::~Display()
{
for (int i = 0; i < 16; i++)
DeleteBuffer(i);
}
}

54
core/Video/Font.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <display.hpp>
#include <debug.h>
#include <cstring>
namespace Video
{
Font::Font(uint64_t *Start, uint64_t *End, FontType Type)
{
trace("Initializing font with start %#llx and end %#llx Type: %d", Start, End, Type);
this->Info.StartAddress = Start;
this->Info.EndAddress = End;
this->Info.Type = Type;
if (Type == FontType::PCScreenFont2)
{
this->Info.PSF2Font = new PSF2_FONT;
uint64_t FontDataLength = End - Start;
PSF2_HEADER *font2 = (PSF2_HEADER *)KernelAllocator.RequestPages(FontDataLength / PAGE_SIZE + 1);
for (uint64_t i = 0; i < FontDataLength / PAGE_SIZE + 1; i++)
Memory::Virtual().Map((void *)(font2 + (i * PAGE_SIZE)), (void *)(font2 + (i * PAGE_SIZE)), Memory::PTFlag::RW);
memcpy((void *)font2, Start, FontDataLength);
this->Info.Width = font2->width;
this->Info.Height = font2->height;
if (font2->magic[0] != PSF2_MAGIC0 || font2->magic[1] != PSF2_MAGIC1 || font2->magic[2] != PSF2_MAGIC2 || font2->magic[3] != PSF2_MAGIC3)
error("Font2 magic mismatch.");
this->Info.PSF2Font->Header = font2;
this->Info.PSF2Font->GlyphBuffer = reinterpret_cast<void *>(reinterpret_cast<uint64_t>(Start) + sizeof(PSF2_HEADER));
}
else if (Type == FontType::PCScreenFont1)
{
this->Info.PSF1Font = new PSF1_FONT;
PSF1_HEADER *font1 = (PSF1_HEADER *)Start;
if (font1->magic[0] != PSF1_MAGIC0 || font1->magic[1] != PSF1_MAGIC1)
error("Font1 magic mismatch.");
uint32_t glyphBufferSize = font1->charsize * 256;
if (font1->mode == 1) // 512 glyph mode
glyphBufferSize = font1->charsize * 512;
void *glyphBuffer = reinterpret_cast<void *>(reinterpret_cast<uint64_t>(Start) + sizeof(PSF1_HEADER));
this->Info.PSF1Font->Header = font1;
this->Info.PSF1Font->GlyphBuffer = glyphBuffer;
UNUSED(glyphBufferSize); // TODO: Use this in the future?
// TODO: Get font size.
this->Info.Width = 16;
this->Info.Height = 8;
}
}
Font::~Font()
{
}
}