feat: io: serial & console

This commit is contained in:
Karina
2025-12-21 13:40:52 +04:00
parent bce8adb119
commit 626174d3d5
13 changed files with 313 additions and 3 deletions
+8
View File
@@ -1,7 +1,9 @@
#include "../../common/bootinfo.h"
#include "console.h"
#include "types.h"
#include "shitgui.h"
#include "serial.h"
void kmain(Bootinfo* info) {
u32 *fb = (u32*)info->base;
@@ -29,5 +31,11 @@ void kmain(Bootinfo* info) {
p.y += stripe_h;
sg_draw_rect(&sg_ctx, &p, info->width, stripe_h, 0x5BCEFA);
serial_init();
console_init(&sg_ctx);
console_set_color(0x000000);
kprint("Hello!!!");
for (int i = 0; i < 1000; i++) kprint("!");
while (1) { __asm__("hlt"); }
}
+44
View File
@@ -0,0 +1,44 @@
#include "console.h"
#include "serial.h"
#include "font.h"
#include "shitgui.h"
static SG_Context *ctx_ptr = nullptr;
static SG_Point s_cursor_pos = {0};
static SG_Font s_font = {0};
static u32 s_color = 0xFFFFFF;
void console_init(SG_Context *ctx) {
ctx_ptr = ctx;
s_cursor_pos.x = 0;
s_cursor_pos.y = 0;
s_font.h = 8;
s_font.w = 8;
s_font.base = (const unsigned char*)font8x8_basic;
}
static void console_putc(char c) {
serial_writec(c);
if (!ctx_ptr) return;
if (c == '\n') {
s_cursor_pos.x = 0;
s_cursor_pos.y += s_font.h;
} else {
sg_draw_char_bitmap(ctx_ptr, &s_cursor_pos, c, s_color, &s_font);
s_cursor_pos.x += s_font.w;
}
if (s_cursor_pos.x >= ctx_ptr->width) {
s_cursor_pos.x = 0;
s_cursor_pos.y += s_font.h;
}
}
void console_set_color(u32 color) {
s_color = color;
}
void kprint(const char *str) {
for (int i = 0; str[i] != '\0'; i++) console_putc(str[i]);
}
+1
View File
@@ -1,3 +1,4 @@
#include "memory.h"
#include "types.h"
void *memset(void *ptr, int value, usize num) {
+28
View File
@@ -0,0 +1,28 @@
#include "serial.h"
#include "io.h"
#define PORT 0x3F8 // com1
int serial_init() {
outb(PORT + 1, 0x00); // interrupt enable register (+1) / disable interruptions
outb(PORT + 3, 0x80); // line control register (+3)
outb(PORT + 0, 0x03); // speed : low byte
outb(PORT + 1, 0x00); // speed : high byte
outb(PORT + 3, 0x03); // line control register (+3) / reset DLAB / no parity / 1 stop bit / (8N1)
outb(PORT + 2, 0xC7); // FIFO Control register (+2) / 1100 0111 / last ones are enable FIFO buff / first ones to clean noise
outb(PORT + 4, 0x0B); // ready :3
return 0;
}
static int is_transmit_empty() {
return inb(PORT + 5) & 0x20; // checking 5th bit LSR
}
void serial_writec(char chr) {
while (is_transmit_empty() == 0); // waiting for buffer to flush (spinlock)
outb(PORT, chr);
}
void serial_write(const char *str) {
for (int i = 0; str[i] != '\0'; i++) serial_writec(str[i]);
}
+23
View File
@@ -42,4 +42,27 @@ void sg_draw_rect(SG_Context *ctx, SG_Point *p, u32 w, u32 h, u32 color) {
volatile u32 *row_ptr = &ctx->fb[(start_y + y) * ctx->pitch + start_x];
for (u32 x = 0; x < draw_w; x++) row_ptr[x] = color;
}
}
void sg_draw_char_bitmap(SG_Context *ctx, SG_Point *p, char c, u32 color, SG_Font *font) {
if (!ctx->fb) return;
u32 start_x = p->x;
u32 start_y = p->y;
u32 draw_w = font->w;
u32 draw_h = font->h;
if (start_x >= ctx->width || start_y >= ctx->height) return;
if (start_x + draw_w > ctx->width) draw_w = ctx->width - start_x;
if (start_y + draw_h > ctx->height) draw_h = ctx->height - start_y;
for (u32 y = 0; y < draw_h; y++) {
u8 bitmap_row = font->base[(unsigned char)c * font->h + y];
volatile u32 *row_ptr = &ctx->fb[(start_y + y) * ctx->pitch + start_x];
for (u32 x = 0; x < draw_w; x++) {
if (bitmap_row & (1 << (font->w - 1 - x))) {
row_ptr[x] = color;
}
}
}
}