2022-12-27 18:29:02 +02:00
|
|
|
#include <kernel/IO.h>
|
2022-12-20 14:01:04 +02:00
|
|
|
#include <kernel/kmalloc.h>
|
2022-12-15 19:05:07 +02:00
|
|
|
#include <kernel/multiboot.h>
|
2022-12-20 14:01:04 +02:00
|
|
|
#include <kernel/panic.h>
|
2022-12-15 19:05:07 +02:00
|
|
|
#include <kernel/Serial.h>
|
|
|
|
#include <kernel/VESA.h>
|
|
|
|
|
2022-12-16 00:46:10 +02:00
|
|
|
#include "font.h"
|
|
|
|
|
2022-12-20 14:01:04 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2022-12-15 19:05:07 +02:00
|
|
|
#define MULTIBOOT_FLAGS_FRAMEBUFFER (1 << 12)
|
|
|
|
#define MULTIBOOT_FRAMEBUFFER_TYPE_GRAPHICS 1
|
|
|
|
#define MULTIBOOT_FRAMEBUFFER_TYPE_TEXT 2
|
|
|
|
|
2022-12-16 00:46:10 +02:00
|
|
|
|
2022-12-15 19:05:07 +02:00
|
|
|
extern multiboot_info_t* s_multiboot_info;
|
2022-12-16 00:46:10 +02:00
|
|
|
extern const struct bitmap_font font;
|
2022-12-15 19:05:07 +02:00
|
|
|
|
|
|
|
namespace VESA
|
|
|
|
{
|
2022-12-27 17:06:33 +02:00
|
|
|
|
2022-12-28 04:16:21 +02:00
|
|
|
static uintptr_t s_addr = 0;
|
|
|
|
static uint8_t s_bpp = 0;
|
|
|
|
static uint32_t s_pitch = 0;
|
|
|
|
static uint32_t s_width = 0;
|
|
|
|
static uint32_t s_height = 0;
|
|
|
|
static uint8_t s_mode = 0;
|
2022-12-15 19:05:07 +02:00
|
|
|
|
2022-12-27 17:22:45 +02:00
|
|
|
static uint32_t s_terminal_width = 0;
|
|
|
|
static uint32_t s_terminal_height = 0;
|
|
|
|
|
|
|
|
static void (*PutCharAtImpl)(uint16_t, uint32_t, uint32_t, Color, Color) = nullptr;
|
|
|
|
static void (*ClearImpl)(Color) = nullptr;
|
2022-12-27 18:29:02 +02:00
|
|
|
static void (*SetCursorPositionImpl)(uint32_t, uint32_t, Color) = nullptr;
|
2022-12-27 17:22:45 +02:00
|
|
|
|
2022-12-16 03:53:55 +02:00
|
|
|
static void GraphicsPutCharAt(uint16_t ch, uint32_t x, uint32_t y, Color fg, Color bg);
|
2022-12-15 19:05:07 +02:00
|
|
|
static void GraphicsClear(Color color);
|
2022-12-27 18:29:02 +02:00
|
|
|
static void GraphicsSetCursorPosition(uint32_t x, uint32_t y, Color fg);
|
2022-12-15 19:05:07 +02:00
|
|
|
|
2022-12-16 03:53:55 +02:00
|
|
|
static void TextPutCharAt(uint16_t ch, uint32_t x, uint32_t y, Color fg, Color bg);
|
2022-12-15 19:05:07 +02:00
|
|
|
static void TextClear(Color color);
|
2022-12-27 18:29:02 +02:00
|
|
|
static void TextSetCursorPosition(uint32_t x, uint32_t y, Color fg);
|
2022-12-15 19:05:07 +02:00
|
|
|
|
2022-12-27 17:22:45 +02:00
|
|
|
void PutCharAt(uint16_t ch, uint32_t x, uint32_t y, Color fg, Color bg)
|
2022-12-15 19:05:07 +02:00
|
|
|
{
|
2022-12-27 17:22:45 +02:00
|
|
|
if (x >= s_width || y >= s_height)
|
2022-12-15 19:05:07 +02:00
|
|
|
return;
|
2022-12-27 17:22:45 +02:00
|
|
|
PutCharAtImpl(ch, x, y, fg, bg);
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Clear(Color color)
|
|
|
|
{
|
2022-12-27 17:22:45 +02:00
|
|
|
ClearImpl(color);
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
2022-12-27 18:29:02 +02:00
|
|
|
void SetCursorPosition(uint32_t x, uint32_t y, Color fg)
|
|
|
|
{
|
|
|
|
SetCursorPositionImpl(x, y, fg);
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:46:10 +02:00
|
|
|
uint32_t GetTerminalWidth()
|
2022-12-15 19:05:07 +02:00
|
|
|
{
|
2022-12-27 17:22:45 +02:00
|
|
|
return s_terminal_width;
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
2022-12-16 00:46:10 +02:00
|
|
|
uint32_t GetTerminalHeight()
|
2022-12-15 19:05:07 +02:00
|
|
|
{
|
2022-12-27 17:22:45 +02:00
|
|
|
return s_terminal_height;
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:55:45 +02:00
|
|
|
bool Initialize()
|
2022-12-15 19:05:07 +02:00
|
|
|
{
|
|
|
|
if (!(s_multiboot_info->flags & MULTIBOOT_FLAGS_FRAMEBUFFER))
|
|
|
|
return false;
|
2022-12-28 04:16:21 +02:00
|
|
|
|
2022-12-15 19:05:07 +02:00
|
|
|
auto& framebuffer = s_multiboot_info->framebuffer;
|
2022-12-28 04:16:21 +02:00
|
|
|
s_addr = framebuffer.addr;
|
2022-12-15 19:05:07 +02:00
|
|
|
s_bpp = framebuffer.bpp;
|
2022-12-16 00:46:10 +02:00
|
|
|
s_pitch = framebuffer.pitch;
|
2022-12-15 19:05:07 +02:00
|
|
|
s_width = framebuffer.width;
|
|
|
|
s_height = framebuffer.height;
|
|
|
|
s_mode = framebuffer.type;
|
|
|
|
|
|
|
|
if (s_mode == MULTIBOOT_FRAMEBUFFER_TYPE_GRAPHICS)
|
|
|
|
{
|
2022-12-16 00:46:10 +02:00
|
|
|
if (s_bpp != 24 && s_bpp != 32)
|
|
|
|
{
|
|
|
|
dprintln("Unsupported bpp {}", s_bpp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-28 04:16:21 +02:00
|
|
|
dprintln("Graphics Mode {}x{} ({} bpp)", s_width, s_height, s_bpp);
|
2022-12-27 17:22:45 +02:00
|
|
|
PutCharAtImpl = GraphicsPutCharAt;
|
|
|
|
ClearImpl = GraphicsClear;
|
2022-12-27 18:29:02 +02:00
|
|
|
SetCursorPositionImpl = GraphicsSetCursorPosition;
|
2022-12-27 17:22:45 +02:00
|
|
|
s_terminal_width = s_width / font.Width;
|
|
|
|
s_terminal_height = s_height / font.Height;
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
2022-12-27 17:22:45 +02:00
|
|
|
else if (s_mode == MULTIBOOT_FRAMEBUFFER_TYPE_TEXT)
|
2022-12-15 19:05:07 +02:00
|
|
|
{
|
2022-12-28 04:16:21 +02:00
|
|
|
dprintln("Text Mode {}x{}", s_width, s_height);
|
2022-12-27 17:22:45 +02:00
|
|
|
PutCharAtImpl = TextPutCharAt;
|
|
|
|
ClearImpl = TextClear;
|
2022-12-27 18:29:02 +02:00
|
|
|
SetCursorPositionImpl = TextSetCursorPosition;
|
2022-12-27 17:22:45 +02:00
|
|
|
s_terminal_width = s_width;
|
|
|
|
s_terminal_height = s_height;
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
2022-12-27 17:22:45 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
dprintln("Unsupported type for VESA framebuffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-27 18:29:02 +02:00
|
|
|
SetCursorPositionImpl(0, 0, Color::BRIGHT_WHITE);
|
2022-12-27 17:22:45 +02:00
|
|
|
ClearImpl(Color::BLACK);
|
|
|
|
return true;
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
2022-12-16 00:46:10 +02:00
|
|
|
static uint32_t s_graphics_colors[]
|
2022-12-15 19:05:07 +02:00
|
|
|
{
|
2022-12-16 00:46:10 +02:00
|
|
|
0x00'00'00'00,
|
|
|
|
0x00'00'00'AA,
|
|
|
|
0x00'00'AA'00,
|
|
|
|
0x00'00'AA'AA,
|
|
|
|
0x00'AA'00'00,
|
|
|
|
0x00'AA'00'AA,
|
|
|
|
0x00'AA'55'00,
|
|
|
|
0x00'AA'AA'AA,
|
|
|
|
0x00'55'55'55,
|
|
|
|
0x00'55'55'FF,
|
|
|
|
0x00'55'FF'55,
|
|
|
|
0x00'55'FF'FF,
|
|
|
|
0x00'FF'55'55,
|
|
|
|
0x00'FF'55'FF,
|
|
|
|
0x00'FF'FF'55,
|
|
|
|
0x00'FF'FF'FF,
|
|
|
|
};
|
2022-12-15 19:05:07 +02:00
|
|
|
|
2022-12-20 14:01:04 +02:00
|
|
|
static void GraphicsSetPixel(uint32_t offset, uint32_t color)
|
2022-12-16 00:46:10 +02:00
|
|
|
{
|
2022-12-28 04:16:21 +02:00
|
|
|
uint32_t* address = (uint32_t*)(s_addr + offset);
|
2022-12-27 17:06:33 +02:00
|
|
|
switch (s_bpp)
|
2022-12-16 03:53:55 +02:00
|
|
|
{
|
2022-12-27 17:06:33 +02:00
|
|
|
case 24:
|
|
|
|
*address = (*address & 0xFF000000) | (color & 0x00FFFFFF);
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
*address = color;
|
|
|
|
break;
|
2022-12-16 03:53:55 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-15 19:05:07 +02:00
|
|
|
|
2022-12-16 03:53:55 +02:00
|
|
|
static void GraphicsPutCharAt(uint16_t ch, uint32_t x, uint32_t y, Color fg, Color bg)
|
2022-12-15 19:05:07 +02:00
|
|
|
{
|
2022-12-16 00:46:10 +02:00
|
|
|
// find correct bitmap
|
|
|
|
uint32_t index = 0;
|
|
|
|
for (uint32_t i = 0; i < font.Chars; i++)
|
|
|
|
{
|
|
|
|
if (font.Index[i] == ch)
|
|
|
|
{
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned char* glyph = font.Bitmap + index * font.Height;
|
2022-12-15 19:05:07 +02:00
|
|
|
|
2022-12-16 00:46:10 +02:00
|
|
|
uint32_t u32_fg = s_graphics_colors[(uint8_t)fg];
|
|
|
|
uint32_t u32_bg = s_graphics_colors[(uint8_t)bg];
|
|
|
|
|
2022-12-16 03:53:55 +02:00
|
|
|
uint32_t fx = x * font.Width;
|
|
|
|
uint32_t fy = y * font.Height;
|
|
|
|
|
2022-12-20 14:01:04 +02:00
|
|
|
uint32_t row_offset = (fy * s_pitch) + (fx * (s_bpp / 8));
|
2022-12-16 03:53:55 +02:00
|
|
|
for (uint32_t gy = 0; gy < font.Height; gy++)
|
|
|
|
{
|
2022-12-20 14:01:04 +02:00
|
|
|
uint32_t pixel_offset = row_offset;
|
2022-12-16 03:53:55 +02:00
|
|
|
for (uint32_t gx = 0; gx < font.Width; gx++)
|
|
|
|
{
|
2022-12-20 14:01:04 +02:00
|
|
|
GraphicsSetPixel(pixel_offset, (glyph[gy] & (1 << (font.Width - gx - 1))) ? u32_fg : u32_bg);
|
|
|
|
pixel_offset += s_bpp / 8;
|
2022-12-16 03:53:55 +02:00
|
|
|
}
|
2022-12-20 14:01:04 +02:00
|
|
|
row_offset += s_pitch;
|
2022-12-16 03:53:55 +02:00
|
|
|
}
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
2022-12-16 00:46:10 +02:00
|
|
|
static void GraphicsClear(Color color)
|
2022-12-15 19:05:07 +02:00
|
|
|
{
|
2022-12-16 00:46:10 +02:00
|
|
|
uint32_t u32_color = s_graphics_colors[(uint8_t)color];
|
2022-12-16 03:53:55 +02:00
|
|
|
|
2022-12-20 14:01:04 +02:00
|
|
|
if (s_bpp == 32)
|
|
|
|
{
|
2022-12-23 15:55:45 +02:00
|
|
|
uint32_t bytes_per_row = s_pitch / 4;
|
2022-12-20 14:01:04 +02:00
|
|
|
for (uint32_t y = 0; y < s_height; y++)
|
|
|
|
for (uint32_t x = 0; x < s_width; x++)
|
|
|
|
((uint32_t*)s_addr)[y * bytes_per_row + x] = u32_color;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t row_offset = 0;
|
2022-12-16 00:46:10 +02:00
|
|
|
for (uint32_t y = 0; y < s_height; y++)
|
2022-12-16 03:53:55 +02:00
|
|
|
{
|
2022-12-20 14:01:04 +02:00
|
|
|
uint32_t pixel_offset = row_offset;
|
2022-12-16 00:46:10 +02:00
|
|
|
for (uint32_t x = 0; x < s_width; x++)
|
2022-12-16 03:53:55 +02:00
|
|
|
{
|
2022-12-20 14:01:04 +02:00
|
|
|
GraphicsSetPixel(pixel_offset, u32_color);
|
|
|
|
pixel_offset += s_bpp / 8;
|
2022-12-16 03:53:55 +02:00
|
|
|
}
|
2022-12-20 14:01:04 +02:00
|
|
|
row_offset += s_pitch;
|
2022-12-16 03:53:55 +02:00
|
|
|
}
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
2022-12-27 18:29:02 +02:00
|
|
|
static void GraphicsSetCursorPosition(uint32_t x, uint32_t y, Color fg)
|
|
|
|
{
|
|
|
|
if (font.Width == 8 && font.Height == 16)
|
|
|
|
{
|
|
|
|
uint8_t cursor[] = {
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
________,
|
|
|
|
XXXXXXXX,
|
|
|
|
XXXXXXXX,
|
|
|
|
________,
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t u32_fg = s_graphics_colors[(uint8_t)fg];
|
|
|
|
|
|
|
|
uint32_t fx = x * font.Width;
|
|
|
|
uint32_t fy = y * font.Height;
|
|
|
|
|
|
|
|
uint32_t row_offset = (fy * s_pitch) + (fx * (s_bpp / 8));
|
|
|
|
for (uint32_t gy = 0; gy < font.Height; gy++)
|
|
|
|
{
|
|
|
|
uint32_t pixel_offset = row_offset;
|
|
|
|
for (uint32_t gx = 0; gx < font.Width; gx++)
|
|
|
|
{
|
|
|
|
if (cursor[gy] & (1 << (font.Width - gx - 1)))
|
|
|
|
GraphicsSetPixel(pixel_offset, u32_fg);
|
|
|
|
pixel_offset += s_bpp / 8;
|
|
|
|
}
|
|
|
|
row_offset += s_pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 19:05:07 +02:00
|
|
|
static inline uint8_t TextColor(Color fg, Color bg)
|
|
|
|
{
|
2022-12-16 00:46:10 +02:00
|
|
|
return (((uint8_t)bg & 0x0F) << 4) | ((uint8_t)fg & 0x0F);
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint16_t TextEntry(uint8_t ch, uint8_t color)
|
|
|
|
{
|
|
|
|
return ((uint16_t)color << 8) | ch;
|
|
|
|
}
|
|
|
|
|
2022-12-16 03:53:55 +02:00
|
|
|
static void TextPutCharAt(uint16_t ch, uint32_t x, uint32_t y, Color fg, Color bg)
|
2022-12-15 19:05:07 +02:00
|
|
|
{
|
2022-12-28 04:16:21 +02:00
|
|
|
uint32_t index = y * s_pitch + x;
|
2022-12-16 00:46:10 +02:00
|
|
|
((uint16_t*)s_addr)[index] = TextEntry(ch, TextColor(fg, bg));
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TextClear(Color color)
|
|
|
|
{
|
|
|
|
for (uint32_t y = 0; y < s_height; y++)
|
|
|
|
for (uint32_t x = 0; x < s_width; x++)
|
2022-12-16 00:46:10 +02:00
|
|
|
TextPutCharAt(' ', x, y, Color::BRIGHT_WHITE, color);
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
2022-12-27 18:29:02 +02:00
|
|
|
static void TextSetCursorPosition(uint32_t x, uint32_t y, Color)
|
|
|
|
{
|
|
|
|
uint16_t position = y * s_width + x;
|
|
|
|
IO::outb(0x3D4, 0x0F);
|
|
|
|
IO::outb(0x3D5, (uint8_t) (position & 0xFF));
|
|
|
|
IO::outb(0x3D4, 0x0E);
|
|
|
|
IO::outb(0x3D5, (uint8_t) ((position >> 8) & 0xFF));
|
|
|
|
}
|
|
|
|
|
2022-12-15 19:05:07 +02:00
|
|
|
}
|