fix: cursor position says fixed in the console (#3)

This commit is contained in:
Pierre-Emmanuel Gob. 2026-09-19 23:14:18 +02:00
parent 591c57c8f8
commit 75c2b200d8
2 changed files with 27 additions and 1 deletions

View file

@ -3,12 +3,20 @@
#include <stdint.h>
#include "console.h"
#include "io.h"
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
#define VGA_ADDR 0xB8000
#define COLOR_DEFAULT 0x0FU /* white on black */
/* VGA CRT controller ports: index selects the register, data reads/writes
* it. Registers 0x0E/0x0F hold the high/low byte of the cursor offset. */
#define VGA_CRT_INDEX 0x3D4U
#define VGA_CRT_DATA 0x3D5U
#define VGA_CRT_CURSOR_HIGH 0x0EU
#define VGA_CRT_CURSOR_LOW 0x0FU
/* Cursor position. Lives in BSS. */
static uint8_t cursor_row;
static uint8_t cursor_col;
@ -18,6 +26,22 @@ static uint8_t cursor_col;
* store in order. */
static volatile uint16_t *const vga = (uint16_t *)VGA_ADDR;
/* Sync the blinking hardware cursor to cursor_row/cursor_col.
* WHY it exists: writing VGA cells alone never moves the CRT cursor, so
* without this the hardware cursor stays where SeaBIOS left it (issue #3).
* Runs at Ring 0 (CPL 0): `out` traps with #GP(0) if CPL > IOPL, so this
* must never run in userspace. A wrong port value cannot fault (open bus
* ignores it); it would only misplace the visible cursor. */
static void update_hw_cursor(void)
{
uint16_t pos = (uint16_t)(cursor_row * VGA_WIDTH + cursor_col);
outb(VGA_CRT_INDEX, VGA_CRT_CURSOR_LOW);
outb(VGA_CRT_DATA, (uint8_t)(pos & 0xFFU));
outb(VGA_CRT_INDEX, VGA_CRT_CURSOR_HIGH);
outb(VGA_CRT_DATA, (uint8_t)((pos >> 8) & 0xFFU));
}
/* Move every line one row up and blank the last line */
static void scroll(void)
{
@ -39,6 +63,7 @@ void console_clear(void)
}
cursor_row = 0;
cursor_col = 0;
update_hw_cursor();
}
void console_putchar(char c)
@ -76,6 +101,7 @@ void console_putchar(char c)
scroll();
cursor_row = VGA_HEIGHT - 1;
}
update_hw_cursor();
}
void console_puts(const char *s)