Explorar el Código

Use VESA highres modes and render fonts

Johannes Zellner hace 8 meses
padre
commit
d0ca527d70
Se han modificado 14 ficheros con 864 adiciones y 92 borrados
  1. 9 3
      Makefile
  2. 47 0
      boot.S
  3. 88 54
      clock.c
  4. 169 0
      fbtext.c
  5. 19 0
      fbtext.h
  6. 231 0
      font_8x16.c
  7. 11 0
      font_8x16.h
  8. 96 24
      io.c
  9. 9 6
      io.h
  10. 6 0
      kernel.c
  11. 5 1
      kernel.ld
  12. 1 0
      terminal.c
  13. 147 2
      vga.c
  14. 26 2
      vga.h

+ 9 - 3
Makefile

@@ -23,7 +23,13 @@ boot.o: boot.S
 kernel.o: kernel.c io.h terminal.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-io.o: io.c io.h
+io.o: io.c io.h fbtext.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+font_8x16.o: font_8x16.c font_8x16.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+fbtext.o: fbtext.c fbtext.h font_8x16.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 terminal.o: terminal.c terminal.h io.h rtc.h timer.h disk.h
@@ -62,7 +68,7 @@ boot.elf: boot.o
 timer.o: timer.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o vga.o clock.o
+kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o vga.o font_8x16.o fbtext.o clock.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -101,4 +107,4 @@ run: image.bin root.img
 	qemu-system-x86_64 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 clean:
-	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o disk.o vga.o clock.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img
+	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o disk.o vga.o font_8x16.o fbtext.o clock.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img

+ 47 - 0
boot.S

@@ -23,6 +23,53 @@ loader_start:
 	movw  $0x0003, %ax
 	int   $0x10
 
+	/* VESA: try fixed mode list, use 0x6000 for mode info (256 bytes). */
+	xorw  %ax, %ax
+	movw  %ax, %es
+	movw  $0x6000, %di
+	/* Try modes: 0x11C, 0x11B, 0x118, 0x114 (descending res, >= 16bpp). */
+	movw  $0x11C, %cx
+1:	movw  $0x4F01, %ax
+	int   $0x10
+	cmpw  $0x004F, %ax
+	jnz   2f
+	cmpb  $16, 0x19(%di)	/* BitsPerPixel >= 16 */
+	jb    2f
+	testw $0x80, (%di)	/* ModeAttributes bit 7 = LFB */
+	jz    2f
+	movw  %cx, %bx
+	orw   $0x4000, %bx	/* Set mode with LFB */
+	movw  $0x4F02, %ax
+	int   $0x10
+	cmpw  $0x004F, %ax
+	jz    vesa_ok
+2:	cmpw  $0x114, %cx
+	jz   vesa_fail
+	subw  $1, %cx		/* 0x11C -> 0x11B -> 0x118 -> 0x114 */
+	jmp   1b
+vesa_fail:
+	xorw  %ax, %ax
+	movw  $0x600, %bx
+	movb  %al, (%bx)	/* valid = 0 */
+	jmp   load_kernel
+vesa_ok:
+	movw  $0x6000, %di	/* Reload: int 0x10/4F02 may trash DI */
+	movw  $0x600, %bx
+	movb  $1, (%bx)		/* valid = 1 */
+	movw  0x12(%di), %ax
+	movw  %ax, 1(%bx)	/* width */
+	movw  0x14(%di), %ax
+	movw  %ax, 3(%bx)	/* height */
+	movw  0x10(%di), %ax
+	movw  %ax, 5(%bx)	/* pitch */
+	movb  0x19(%di), %al
+	movb  %al, 7(%bx)	/* bpp */
+	movw  0x28(%di), %ax
+	movw  %ax, 8(%bx)	/* lfb_base lo */
+	movw  0x2a(%di), %ax
+	movw  %ax, 10(%bx)	/* lfb_base hi */
+
+load_kernel:
 	/* Load kernel: sectors 1..kernel_sectors to 0x7E00. */
 	movw  kernel_sectors, %cx
 	movw  $1, %si

+ 88 - 54
clock.c

@@ -1,30 +1,25 @@
-/* clock command: analog clock in VGA mode 13h. Press ESC to return. */
+/* clock command: analog clock in VESA LFB (or mode 13h fallback). Press ESC to return. */
 
 #include "io.h"
 #include "rtc.h"
 #include "timer.h"
 #include "vga.h"
 
-#define CX          (VGA_GFX_WIDTH / 2)
-#define CY          (VGA_GFX_HEIGHT / 2)
-#define RADIUS      80
-#define RAD_HOUR    40
-#define RAD_MIN     56
-#define RAD_SEC     72
-#define TICK_LEN    6
+/* VGA palette index to RGB (for LFB). 0=black, 7=gray, 12=light red, 15=white */
+static const unsigned int pal_r[16] = { 0, 0, 0, 0, 170, 170, 170, 170, 85, 85, 85, 85, 255, 255, 255, 255 };
+static const unsigned int pal_g[16] = { 0, 0, 170, 170, 0, 0, 85, 170, 85, 85, 255, 255, 85, 85, 255, 255 };
+static const unsigned int pal_b[16] = { 0, 170, 0, 170, 0, 170, 0, 170, 85, 255, 85, 255, 85, 255, 85, 255 };
 
 #define COL_BG      0
-#define COL_FACE    15   /* white */
-#define COL_TICK    7    /* gray */
+#define COL_FACE    15
+#define COL_TICK    7
 #define COL_HOUR    15
 #define COL_MIN     15
-#define COL_SEC     12   /* light red */
+#define COL_SEC     12
 #define COL_CENTER  15
 
 /*
  * Precomputed unit-vector table at radius 64 for 60 clock positions.
- * Index 0 = 12 o'clock (up), 15 = 3 o'clock, 30 = 6 o'clock, etc.
- * x = 64*sin(2*pi*i/60), y = -64*cos(2*pi*i/60)
  */
 static const signed char tab_x[60] = {
 	  0,  7, 13, 20, 26, 32, 37, 42, 47, 51,
@@ -43,7 +38,25 @@ static const signed char tab_y[60] = {
 	-47,-51,-55,-59,-62,-63
 };
 
-/* ---- drawing primitives ---- */
+static int clock_use_vesa;
+static unsigned int clock_cx, clock_cy, clock_radius, clock_rad_hour, clock_rad_min, clock_rad_sec, clock_tick_len;
+
+static void put_pixel(int x, int y, unsigned char color)
+{
+	if (clock_use_vesa)
+		vga_put_pixel_rgb((unsigned int)x, (unsigned int)y,
+			pal_r[color], pal_g[color], pal_b[color]);
+	else
+		vga_put_pixel(x, y, color);
+}
+
+static void clear_screen(unsigned char color)
+{
+	if (clock_use_vesa)
+		vga_clear_rgb(pal_r[color], pal_g[color], pal_b[color]);
+	else
+		vga_clear(color);
+}
 
 static void draw_line(int x0, int y0, int x1, int y1, unsigned char color)
 {
@@ -55,13 +68,13 @@ static void draw_line(int x0, int y0, int x1, int y1, unsigned char color)
 	int i;
 
 	if (steps == 0) {
-		vga_put_pixel(x0, y0, color);
+		put_pixel(x0, y0, color);
 		return;
 	}
 	for (i = 0; i <= steps; i++) {
 		int x = x0 + dx * i / steps;
 		int y = y0 + dy * i / steps;
-		vga_put_pixel(x, y, color);
+		put_pixel(x, y, color);
 	}
 }
 
@@ -71,14 +84,14 @@ static void draw_circle(int cx, int cy, int r, unsigned char color)
 	int d = 1 - r;
 
 	while (y <= x) {
-		vga_put_pixel(cx + x, cy + y, color);
-		vga_put_pixel(cx - x, cy + y, color);
-		vga_put_pixel(cx + x, cy - y, color);
-		vga_put_pixel(cx - x, cy - y, color);
-		vga_put_pixel(cx + y, cy + x, color);
-		vga_put_pixel(cx - y, cy + x, color);
-		vga_put_pixel(cx + y, cy - x, color);
-		vga_put_pixel(cx - y, cy - x, color);
+		put_pixel(cx + x, cy + y, color);
+		put_pixel(cx - x, cy + y, color);
+		put_pixel(cx + x, cy - y, color);
+		put_pixel(cx - x, cy - y, color);
+		put_pixel(cx + y, cy + x, color);
+		put_pixel(cx - y, cy + x, color);
+		put_pixel(cx + y, cy - x, color);
+		put_pixel(cx - y, cy - x, color);
 		if (d <= 0) {
 			d += 2 * y + 3;
 		} else {
@@ -96,67 +109,86 @@ static void draw_thick_line(int x0, int y0, int x1, int y1, unsigned char color)
 	draw_line(x0, y0 + 1, x1, y1 + 1, color);
 }
 
-/* ---- clock rendering ---- */
-
 static void draw_face(void)
 {
 	int i;
+	int inner = clock_radius - clock_tick_len;
 
-	draw_circle(CX, CY, RADIUS, COL_FACE);
-	draw_circle(CX, CY, RADIUS - 1, COL_FACE);
+	draw_circle(clock_cx, clock_cy, clock_radius, COL_FACE);
+	draw_circle(clock_cx, clock_cy, clock_radius - 1, COL_FACE);
 
-	/* Hour tick marks */
 	for (i = 0; i < 60; i += 5) {
-		int inner = RADIUS - TICK_LEN;
-		int ox = CX + tab_x[i] * RADIUS / 64;
-		int oy = CY + tab_y[i] * RADIUS / 64;
-		int ix = CX + tab_x[i] * inner / 64;
-		int iy = CY + tab_y[i] * inner / 64;
+		int ox = clock_cx + tab_x[i] * (int)clock_radius / 64;
+		int oy = clock_cy + tab_y[i] * (int)clock_radius / 64;
+		int ix = clock_cx + tab_x[i] * (int)inner / 64;
+		int iy = clock_cy + tab_y[i] * (int)inner / 64;
 		draw_line(ix, iy, ox, oy, COL_TICK);
 	}
 }
 
 static void draw_hand(int idx60, int length, unsigned char color, int thick)
 {
-	int ex = CX + tab_x[idx60] * length / 64;
-	int ey = CY + tab_y[idx60] * length / 64;
+	int ex = clock_cx + tab_x[idx60] * (int)length / 64;
+	int ey = clock_cy + tab_y[idx60] * (int)length / 64;
 
 	if (thick)
-		draw_thick_line(CX, CY, ex, ey, color);
+		draw_thick_line(clock_cx, clock_cy, ex, ey, color);
 	else
-		draw_line(CX, CY, ex, ey, color);
+		draw_line(clock_cx, clock_cy, ex, ey, color);
 }
 
 static void draw_hands(unsigned int h, unsigned int m, unsigned int s)
 {
 	int hour_idx;
 
-	/* Hour hand: 5 positions per hour + minute offset */
 	hour_idx = (int)((h % 12) * 5 + m / 12);
 	if (hour_idx >= 60)
 		hour_idx = 59;
 
-	draw_hand(hour_idx, RAD_HOUR, COL_HOUR, 1);
-	draw_hand((int)m, RAD_MIN, COL_MIN, 1);
-	draw_hand((int)s, RAD_SEC, COL_SEC, 0);
+	draw_hand(hour_idx, (int)clock_rad_hour, COL_HOUR, 1);
+	draw_hand((int)m, (int)clock_rad_min, COL_MIN, 1);
+	draw_hand((int)s, (int)clock_rad_sec, COL_SEC, 0);
 
-	/* Center dot */
-	vga_put_pixel(CX, CY, COL_CENTER);
-	vga_put_pixel(CX + 1, CY, COL_CENTER);
-	vga_put_pixel(CX, CY + 1, COL_CENTER);
-	vga_put_pixel(CX + 1, CY + 1, COL_CENTER);
+	put_pixel(clock_cx, clock_cy, COL_CENTER);
+	put_pixel(clock_cx + 1, clock_cy, COL_CENTER);
+	put_pixel(clock_cx, clock_cy + 1, COL_CENTER);
+	put_pixel(clock_cx + 1, clock_cy + 1, COL_CENTER);
 }
 
-/* ---- main entry point ---- */
-
 void program_clock_main(void)
 {
 	unsigned int last_sec = 99;
 	unsigned int h, m, s;
 	unsigned int tick;
+	unsigned int w, h_;
+
+	clock_use_vesa = vga_has_vesa();
+	if (clock_use_vesa) {
+		w = vga_fb_width();
+		h_ = vga_fb_height();
+		clock_cx = w / 2;
+		clock_cy = h_ / 2;
+		clock_radius = (w < h_) ? (w / 2) : (h_ / 2);
+		if (clock_radius > 200)
+			clock_radius = 200;
+		clock_rad_hour = clock_radius / 2;
+		clock_rad_min = clock_radius * 7 / 10;
+		clock_rad_sec = clock_radius * 9 / 10;
+		clock_tick_len = clock_radius / 13;
+		if (clock_tick_len < 2)
+			clock_tick_len = 2;
+	} else {
+		vga_set_mode_13();
+		clock_cx = 160;
+		clock_cy = 100;
+		clock_radius = 80;
+		clock_rad_hour = 40;
+		clock_rad_min = 56;
+		clock_rad_sec = 72;
+		clock_tick_len = 6;
+	}
 
-	vga_set_mode_13();
-	vga_clear(COL_BG);
+	clear_screen(COL_BG);
 	io_drain_keyboard();
 
 	for (;;) {
@@ -166,7 +198,7 @@ void program_clock_main(void)
 
 		rtc_read_hms(&h, &m, &s);
 		if (s != last_sec) {
-			vga_clear(COL_BG);
+			clear_screen(COL_BG);
 			draw_face();
 			draw_hands(h, m, s);
 			last_sec = s;
@@ -177,6 +209,8 @@ void program_clock_main(void)
 			__asm__ volatile ("hlt");
 	}
 
-	vga_set_mode_text();
-	io_clear_screen();
+	if (!clock_use_vesa) {
+		vga_set_mode_text();
+		io_clear_screen();
+	}
 }

+ 169 - 0
fbtext.c

@@ -0,0 +1,169 @@
+/* Framebuffer text: draw 8x16 characters into VESA LFB. */
+
+#include "fbtext.h"
+#include "font_8x16.h"
+#include "vga.h"
+
+static int fbtext_use_lfb;
+static unsigned int fbtext_cols, fbtext_rows;
+static unsigned int fbtext_cur_col, fbtext_cur_row;
+static int fbtext_cursor_visible;
+
+/* VGA attribute (0-15) to RGB. Standard VGA palette. */
+static void attr_to_rgb(unsigned char attr, int fg, unsigned int *r, unsigned int *g, unsigned int *b)
+{
+	static const unsigned int vga_rgb[16][3] = {
+		{ 0x00, 0x00, 0x00 }, /* 0 black */
+		{ 0x00, 0x00, 0xaa }, /* 1 blue */
+		{ 0x00, 0xaa, 0x00 }, /* 2 green */
+		{ 0x00, 0xaa, 0xaa }, /* 3 cyan */
+		{ 0xaa, 0x00, 0x00 }, /* 4 red */
+		{ 0xaa, 0x00, 0xaa }, /* 5 magenta */
+		{ 0xaa, 0x55, 0x00 }, /* 6 brown */
+		{ 0xaa, 0xaa, 0xaa }, /* 7 light gray */
+		{ 0x55, 0x55, 0x55 }, /* 8 dark gray */
+		{ 0x55, 0x55, 0xff }, /* 9 light blue */
+		{ 0x55, 0xff, 0x55 }, /* 10 light green */
+		{ 0x55, 0xff, 0xff }, /* 11 light cyan */
+		{ 0xff, 0x55, 0x55 }, /* 12 light red */
+		{ 0xff, 0x55, 0xff }, /* 13 light magenta */
+		{ 0xff, 0xff, 0x55 }, /* 14 yellow */
+		{ 0xff, 0xff, 0xff }, /* 15 white */
+	};
+	unsigned int idx = (unsigned int)(fg ? (attr & 0x0F) : ((attr >> 4) & 0x0F));
+	*r = vga_rgb[idx][0];
+	*g = vga_rgb[idx][1];
+	*b = vga_rgb[idx][2];
+}
+
+void fbtext_init(void)
+{
+	if (!vga_has_vesa()) {
+		fbtext_use_lfb = 0;
+		fbtext_cols = 80;
+		fbtext_rows = 25;
+		return;
+	}
+	fbtext_use_lfb = 1;
+	fbtext_cols = vga_fb_width() / 8;
+	fbtext_rows = vga_fb_height() / 16;
+	if (fbtext_cols == 0) fbtext_cols = 80;
+	if (fbtext_rows == 0) fbtext_rows = 25;
+	fbtext_cur_col = 0;
+	fbtext_cur_row = 0;
+	fbtext_cursor_visible = 0;
+}
+
+int fbtext_active(void)
+{
+	return fbtext_use_lfb;
+}
+
+unsigned int fbtext_get_cols(void)
+{
+	return fbtext_cols;
+}
+
+unsigned int fbtext_get_rows(void)
+{
+	return fbtext_rows;
+}
+
+void fbtext_clear(void)
+{
+	if (!fbtext_use_lfb)
+		return;
+	vga_clear_rgb(0, 0, 0);
+}
+
+static void draw_glyph(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
+{
+	unsigned int x0 = col * 8;
+	unsigned int y0 = row * 16;
+	unsigned int r_fg, g_fg, b_fg, r_bg, g_bg, b_bg;
+	unsigned int x, y;
+	const unsigned char *glyph;
+
+	attr_to_rgb(attr, 1, &r_fg, &g_fg, &b_fg);
+	attr_to_rgb(attr, 0, &r_bg, &g_bg, &b_bg);
+
+	glyph = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
+	for (y = 0; y < 16; y++) {
+		unsigned char row_bits = glyph[y];
+		for (x = 0; x < 8; x++) {
+			unsigned int px = x0 + x;
+			unsigned int py = y0 + y;
+			if (row_bits & (0x80u >> x))
+				vga_put_pixel_rgb(px, py, r_fg, g_fg, b_fg);
+			else
+				vga_put_pixel_rgb(px, py, r_bg, g_bg, b_bg);
+		}
+	}
+}
+
+void fbtext_put_char_at(unsigned int col, unsigned int row, unsigned char c, unsigned char attr)
+{
+	if (!fbtext_use_lfb)
+		return;
+	if (col >= fbtext_cols || row >= fbtext_rows)
+		return;
+	draw_glyph(col, row, c, attr);
+}
+
+void fbtext_scroll_up_one_line(void)
+{
+	unsigned int y, i;
+	unsigned int pitch = vga_fb_pitch();
+	volatile unsigned char *fb = (volatile unsigned char *)vga_fb_ptr();
+	unsigned int height = vga_fb_height();
+
+	if (!fbtext_use_lfb || !fb)
+		return;
+	if (height <= 16)
+		return;
+	for (y = 16; y < height - 16; y++) {
+		unsigned int src_off = (y + 16) * pitch;
+		unsigned int dst_off = y * pitch;
+		for (i = 0; i < pitch; i++)
+			fb[dst_off + i] = fb[src_off + i];
+	}
+	for (y = height - 16; y < height; y++) {
+		unsigned int off = y * pitch;
+		for (i = 0; i < pitch; i++)
+			fb[off + i] = 0;
+	}
+}
+
+void fbtext_set_cursor(unsigned int col, unsigned int row)
+{
+	if (col >= fbtext_cols || row >= fbtext_rows)
+		return;
+	fbtext_cur_col = col;
+	fbtext_cur_row = row;
+}
+
+void fbtext_show_cursor(void)
+{
+	unsigned int x0 = fbtext_cur_col * 8;
+	unsigned int y0 = fbtext_cur_row * 16;
+	unsigned int x, y;
+	unsigned int r, g, b;
+
+	if (!fbtext_use_lfb)
+		return;
+	fbtext_cursor_visible = 1;
+	attr_to_rgb(0x0F, 1, &r, &g, &b); /* white block */
+	for (y = 0; y < 16; y++)
+		for (x = 0; x < 8; x++)
+			vga_put_pixel_rgb(x0 + x, y0 + y, r, g, b);
+}
+
+void fbtext_hide_cursor(unsigned char c, unsigned char attr)
+{
+	if (!fbtext_use_lfb)
+		return;
+	if (fbtext_cursor_visible) {
+		fbtext_cursor_visible = 0;
+		draw_glyph(fbtext_cur_col, fbtext_cur_row, c, attr);
+	}
+}

+ 19 - 0
fbtext.h

@@ -0,0 +1,19 @@
+/* Framebuffer text: draw characters into VESA LFB. 8x16 font, VGA attribute colors. */
+#ifndef FBTEXT_H
+#define FBTEXT_H
+
+void fbtext_init(void);
+int fbtext_active(void);	/* 1 if using LFB, 0 if VGA text path */
+
+unsigned int fbtext_get_cols(void);
+unsigned int fbtext_get_rows(void);
+
+void fbtext_clear(void);
+void fbtext_put_char_at(unsigned int col, unsigned int row, unsigned char c, unsigned char attr);
+void fbtext_scroll_up_one_line(void);
+void fbtext_set_cursor(unsigned int col, unsigned int row);
+void fbtext_show_cursor(void);
+/* Hide cursor and redraw cell with (c, attr) so the character is restored. */
+void fbtext_hide_cursor(unsigned char c, unsigned char attr);
+
+#endif

+ 231 - 0
font_8x16.c

@@ -0,0 +1,231 @@
+/* 8x16 bitmap font: 256 chars, 16 bytes each. Minimal VGA-style glyphs. */
+
+#include "font_8x16.h"
+
+/* One byte per row, MSB = left pixel. 256 * 16 = 4096 bytes. */
+const unsigned char font_8x16[FONT_8X16_CHARS * FONT_8X16_ROWS] = {
+	/* 0-31: control chars, blank */
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 32 space */
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 33 ! */
+	0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 34 " */
+	0x6c,0x6c,0x6c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 35 # */
+	0x36,0x36,0x7f,0x36,0x36,0x36,0x7f,0x36,0x36,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 36 $ */
+	0x0c,0x3f,0x68,0x68,0x3e,0x0b,0x0b,0x7e,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 37 % */
+	0x60,0x66,0x6c,0x18,0x30,0x66,0xc6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 38 & */
+	0x38,0x6c,0x38,0x76,0xdc,0xcc,0x76,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 39 ' */
+	0x18,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 40 ( */
+	0x0c,0x18,0x30,0x30,0x30,0x18,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 41 ) */
+	0x30,0x18,0x0c,0x0c,0x0c,0x18,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 42 * */
+	0x00,0x18,0x7e,0x3c,0x3c,0x7e,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 43 + */
+	0x00,0x18,0x18,0x7e,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 44 , */
+	0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 45 - */
+	0x00,0x00,0x00,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 46 . */
+	0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 47 / */
+	0x03,0x06,0x0c,0x18,0x30,0x60,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 48 0 */
+	0x3c,0x66,0x6e,0x76,0x66,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 49 1 */
+	0x18,0x38,0x18,0x18,0x18,0x18,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 50 2 */
+	0x3c,0x66,0x06,0x0c,0x30,0x60,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 51 3 */
+	0x3c,0x66,0x06,0x1c,0x06,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 52 4 */
+	0x0c,0x1c,0x3c,0x6c,0x7e,0x0c,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 53 5 */
+	0x7e,0x60,0x7c,0x06,0x06,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 54 6 */
+	0x3c,0x60,0x7c,0x66,0x66,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 55 7 */
+	0x7e,0x06,0x0c,0x18,0x30,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 56 8 */
+	0x3c,0x66,0x66,0x3c,0x66,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 57 9 */
+	0x3c,0x66,0x66,0x66,0x3e,0x06,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 58 : */
+	0x00,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 59 ; */
+	0x00,0x18,0x18,0x00,0x18,0x18,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 60 < */
+	0x0c,0x18,0x30,0x60,0x30,0x18,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 61 = */
+	0x00,0x00,0x7e,0x00,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 62 > */
+	0x30,0x18,0x0c,0x06,0x0c,0x18,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 63 ? */
+	0x3c,0x66,0x06,0x0c,0x18,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 64 @ */
+	0x3c,0x66,0x6e,0x6a,0x6e,0x60,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 65 A */
+	0x18,0x3c,0x66,0x66,0x7e,0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 66 B */
+	0x7c,0x66,0x66,0x7c,0x66,0x66,0x7c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 67 C */
+	0x3c,0x66,0x60,0x60,0x60,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 68 D */
+	0x7c,0x66,0x66,0x66,0x66,0x66,0x7c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 69 E */
+	0x7e,0x60,0x60,0x7c,0x60,0x60,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 70 F */
+	0x7e,0x60,0x60,0x7c,0x60,0x60,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 71 G */
+	0x3c,0x66,0x60,0x6e,0x66,0x66,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 72 H */
+	0x66,0x66,0x66,0x7e,0x66,0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 73 I */
+	0x3c,0x18,0x18,0x18,0x18,0x18,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 74 J */
+	0x1e,0x0c,0x0c,0x0c,0x0c,0x6c,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 75 K */
+	0x66,0x6c,0x78,0x70,0x78,0x6c,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 76 L */
+	0x60,0x60,0x60,0x60,0x60,0x60,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 77 M */
+	0x63,0x77,0x7f,0x6b,0x63,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 78 N */
+	0x66,0x76,0x7e,0x6e,0x66,0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 79 O */
+	0x3c,0x66,0x66,0x66,0x66,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 80 P */
+	0x7c,0x66,0x66,0x7c,0x60,0x60,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 81 Q */
+	0x3c,0x66,0x66,0x66,0x6a,0x6c,0x36,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 82 R */
+	0x7c,0x66,0x66,0x7c,0x6c,0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 83 S */
+	0x3c,0x66,0x60,0x3c,0x06,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 84 T */
+	0x7e,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 85 U */
+	0x66,0x66,0x66,0x66,0x66,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 86 V */
+	0x66,0x66,0x66,0x66,0x66,0x3c,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 87 W */
+	0x63,0x63,0x63,0x6b,0x7f,0x77,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 88 X */
+	0x66,0x66,0x3c,0x18,0x3c,0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 89 Y */
+	0x66,0x66,0x66,0x3c,0x18,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 90 Z */
+	0x7e,0x06,0x0c,0x18,0x30,0x60,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 91 [ */
+	0x3c,0x30,0x30,0x30,0x30,0x30,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 92 \ */
+	0xc0,0x60,0x30,0x18,0x0c,0x06,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 93 ] */
+	0x3c,0x0c,0x0c,0x0c,0x0c,0x0c,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 94 ^ */
+	0x18,0x3c,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 95 _ */
+	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 96 ` */
+	0x30,0x18,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 97 a */
+	0x00,0x00,0x38,0x0c,0x3c,0x6c,0x36,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 98 b */
+	0x60,0x60,0x7c,0x66,0x66,0x66,0x7c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 99 c */
+	0x00,0x00,0x3c,0x60,0x60,0x60,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 100 d */
+	0x06,0x06,0x3e,0x66,0x66,0x66,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 101 e */
+	0x00,0x00,0x3c,0x66,0x7e,0x60,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 102 f */
+	0x1c,0x30,0x30,0x7c,0x30,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 103 g */
+	0x00,0x00,0x36,0x66,0x66,0x3e,0x06,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 104 h */
+	0x60,0x60,0x7c,0x66,0x66,0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 105 i */
+	0x18,0x00,0x18,0x18,0x18,0x18,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 106 j */
+	0x0c,0x00,0x0c,0x0c,0x0c,0x0c,0x6c,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 107 k */
+	0x60,0x60,0x66,0x6c,0x78,0x6c,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 108 l */
+	0x18,0x18,0x18,0x18,0x18,0x18,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 109 m */
+	0x00,0x00,0x76,0x7f,0x6b,0x63,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 110 n */
+	0x00,0x00,0x7c,0x66,0x66,0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 111 o */
+	0x00,0x00,0x3c,0x66,0x66,0x66,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 112 p */
+	0x00,0x00,0x7c,0x66,0x66,0x7c,0x60,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 113 q */
+	0x00,0x00,0x3e,0x66,0x66,0x3e,0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 114 r */
+	0x00,0x00,0x7c,0x66,0x60,0x60,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 115 s */
+	0x00,0x00,0x3e,0x60,0x3c,0x06,0x7c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 116 t */
+	0x30,0x30,0x7c,0x30,0x30,0x30,0x1c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 117 u */
+	0x00,0x00,0x66,0x66,0x66,0x66,0x3e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 118 v */
+	0x00,0x00,0x66,0x66,0x66,0x3c,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 119 w */
+	0x00,0x00,0x63,0x6b,0x7f,0x77,0x63,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 120 x */
+	0x00,0x00,0x66,0x3c,0x18,0x3c,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 121 y */
+	0x00,0x00,0x66,0x66,0x66,0x3e,0x06,0x3c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 122 z */
+	0x00,0x00,0x7e,0x0c,0x18,0x30,0x7e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 123 { */
+	0x0c,0x18,0x18,0x70,0x18,0x18,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 124 | */
+	0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 125 } */
+	0x30,0x18,0x18,0x0e,0x18,0x18,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 126 ~ */
+	0x31,0x6b,0x46,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+	/* 127-255: zero (C zero-initializes remaining elements) */
+};

+ 11 - 0
font_8x16.h

@@ -0,0 +1,11 @@
+/* 8x16 bitmap font: 256 chars, 16 bytes each (one byte per row, MSB left).
+   Minimal VGA-style glyphs for ASCII 32-126. */
+#ifndef FONT_8X16_H
+#define FONT_8X16_H
+
+#define FONT_8X16_CHARS  256
+#define FONT_8X16_ROWS   16
+
+extern const unsigned char font_8x16[FONT_8X16_CHARS * FONT_8X16_ROWS];
+
+#endif

+ 96 - 24
io.c

@@ -1,9 +1,13 @@
 /* I/O library: VGA text output and PS/2 keyboard. */
 
 #include "io.h"
+#include "fbtext.h"
 
 #define VGA_BASE ((volatile unsigned short *)0xB8000)
 #define VGA_ATTR 0x0A
+
+static unsigned int io_cols = 80;
+static unsigned int io_rows = 25;
 #define KB_DATA  0x60
 #define KB_STATUS 0x64
 
@@ -91,12 +95,37 @@ static char apply_shift(char c)
 
 static void hide_cursor_at(unsigned int pos)
 {
-	VGA_BASE[pos] = cursor_cell;
+	if (fbtext_active()) {
+		unsigned int col = pos % io_cols;
+		unsigned int row = pos / io_cols;
+		fbtext_set_cursor(col, row);
+		fbtext_hide_cursor((unsigned char)(cursor_cell & 0xFF), (unsigned char)(cursor_cell >> 8));
+	} else {
+		VGA_BASE[pos] = cursor_cell;
+	}
+}
+
+unsigned int io_get_cols(void)
+{
+	return io_cols;
+}
+
+unsigned int io_get_rows(void)
+{
+	return io_rows;
 }
 
 void io_init(void)
 {
-	disable_hw_cursor();
+	fbtext_init();
+	if (fbtext_active()) {
+		io_cols = fbtext_get_cols();
+		io_rows = fbtext_get_rows();
+	} else {
+		io_cols = 80;
+		io_rows = 25;
+		disable_hw_cursor();
+	}
 	io_clear_screen();
 	cursor = IO_CONTENT_OFFSET;
 	cursor_cell = (unsigned short)(VGA_ATTR << 8);
@@ -105,9 +134,15 @@ void io_init(void)
 
 void io_clear_screen(void)
 {
-	unsigned int i;
-	for (i = 0; i < IO_VGA_ROWS * IO_VGA_COLS; i++)
-		VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
+	if (fbtext_active()) {
+		fbtext_clear();
+		return;
+	}
+	{
+		unsigned int i;
+		for (i = 0; i < io_rows * io_cols; i++)
+			VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
+	}
 }
 
 int io_did_scroll(void)
@@ -120,32 +155,52 @@ int io_did_scroll(void)
 
 void io_putchar(char c)
 {
+	unsigned int ncols = io_cols;
+	unsigned int content_off = ncols;
+	unsigned int content_size = (io_rows - 1) * ncols;
+
 	if (c == '\n') {
 		hide_cursor_at(cursor);
-		cursor = (cursor / IO_VGA_COLS + 1) * IO_VGA_COLS;
-		if (cursor >= IO_CONTENT_OFFSET + IO_CONTENT_SIZE) {
-			unsigned int i;
-			for (i = 0; i < (IO_CONTENT_ROWS - 1) * IO_VGA_COLS; i++)
-				VGA_BASE[IO_CONTENT_OFFSET + i] = VGA_BASE[IO_CONTENT_OFFSET + i + IO_VGA_COLS];
-			for (i = (IO_CONTENT_ROWS - 1) * IO_VGA_COLS; i < IO_CONTENT_SIZE; i++)
-				VGA_BASE[IO_CONTENT_OFFSET + i] = (unsigned short)(VGA_ATTR << 8);
-			cursor = IO_CONTENT_OFFSET + (IO_CONTENT_ROWS - 1) * IO_VGA_COLS;
+		cursor = (cursor / ncols + 1) * ncols;
+		if (cursor >= content_off + content_size) {
+			if (fbtext_active()) {
+				fbtext_scroll_up_one_line();
+			} else {
+				unsigned int i;
+				for (i = 0; i < (io_rows - 2) * ncols; i++)
+					VGA_BASE[content_off + i] = VGA_BASE[content_off + i + ncols];
+				for (i = (io_rows - 2) * ncols; i < content_size; i++)
+					VGA_BASE[content_off + i] = (unsigned short)(VGA_ATTR << 8);
+			}
+			cursor = content_off + (io_rows - 2) * ncols; /* first cell of last content row */
 			scroll_occurred = 1;
 		}
 		cursor_cell = (unsigned short)(VGA_ATTR << 8);
 		return;
 	}
 	if (c == '\b') {
-		if (cursor > IO_CONTENT_OFFSET) {
+		if (cursor > content_off) {
 			hide_cursor_at(cursor);
 			cursor--;
-			VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | ' ');  /* erase char on screen */
+			if (fbtext_active()) {
+				unsigned int col = cursor % ncols;
+				unsigned int row = cursor / ncols;
+				fbtext_put_char_at(col, row, ' ', VGA_ATTR);
+			} else {
+				VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | ' ');
+			}
 			cursor_cell = (unsigned short)((VGA_ATTR << 8) | ' ');
 		}
 		return;
 	}
-	if (cursor < IO_CONTENT_OFFSET + IO_CONTENT_SIZE) {
-		VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
+	if (cursor < content_off + content_size) {
+		if (fbtext_active()) {
+			unsigned int col = cursor % ncols;
+			unsigned int row = cursor / ncols;
+			fbtext_put_char_at(col, row, (unsigned char)c, VGA_ATTR);
+		} else {
+			VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
+		}
 		cursor++;
 		cursor_cell = (unsigned short)(VGA_ATTR << 8);
 	}
@@ -163,28 +218,45 @@ unsigned int io_get_cursor(void)
 
 void io_set_cursor(unsigned int pos)
 {
-	if (pos >= IO_VGA_ROWS * IO_VGA_COLS)
+	if (pos >= io_rows * io_cols)
 		return;
 	hide_cursor_at(cursor);
 	cursor = pos;
-	cursor_cell = VGA_BASE[cursor];
+	if (fbtext_active())
+		cursor_cell = (unsigned short)(VGA_ATTR << 8);
+	else
+		cursor_cell = VGA_BASE[cursor];
+	if (fbtext_active())
+		fbtext_set_cursor(cursor % io_cols, cursor / io_cols);
 }
 
-#define CURSOR_BLOCK ((unsigned short)((VGA_ATTR << 8) | 0xDB))
-
 void io_cursor_show_block(void)
 {
-	VGA_BASE[cursor] = CURSOR_BLOCK;
+	if (fbtext_active()) {
+		fbtext_set_cursor(cursor % io_cols, cursor / io_cols);
+		fbtext_show_cursor();
+	} else {
+		VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | 0xDB);
+	}
 }
 
 void io_cursor_hide_block(void)
 {
-	VGA_BASE[cursor] = cursor_cell;
+	if (fbtext_active())
+		fbtext_hide_cursor((unsigned char)(cursor_cell & 0xFF), (unsigned char)(cursor_cell >> 8));
+	else
+		VGA_BASE[cursor] = cursor_cell;
 }
 
 void io_vga_write(unsigned int pos, unsigned short value)
 {
-	VGA_BASE[pos] = value;
+	if (fbtext_active()) {
+		unsigned int col = pos % io_cols;
+		unsigned int row = pos / io_cols;
+		fbtext_put_char_at(col, row, (unsigned char)(value & 0xFF), (unsigned char)(value >> 8));
+	} else {
+		VGA_BASE[pos] = value;
+	}
 }
 
 void io_drain_keyboard(void)

+ 9 - 6
io.h

@@ -2,15 +2,18 @@
 #ifndef IO_H
 #define IO_H
 
-#define IO_VGA_COLS       80
-#define IO_VGA_ROWS       25
 #define IO_HEADER_ROWS    1
-#define IO_CONTENT_OFFSET (IO_HEADER_ROWS * IO_VGA_COLS)   /* row 1 start */
-#define IO_CONTENT_ROWS   (IO_VGA_ROWS - IO_HEADER_ROWS)   /* 24 rows */
-#define IO_CONTENT_SIZE   (IO_CONTENT_ROWS * IO_VGA_COLS)
 /* Row 0: banner left, datetime right (19 chars "YYYY-MM-DD HH:MM:SS") */
 #define IO_DATETIME_LEN   19
-#define IO_DATETIME_POS   (IO_VGA_COLS - IO_DATETIME_LEN)
+
+unsigned int io_get_cols(void);
+unsigned int io_get_rows(void);
+#define IO_VGA_COLS       (io_get_cols())
+#define IO_VGA_ROWS       (io_get_rows())
+#define IO_CONTENT_OFFSET (IO_HEADER_ROWS * io_get_cols())
+#define IO_CONTENT_ROWS   (io_get_rows() - IO_HEADER_ROWS)
+#define IO_CONTENT_SIZE   (IO_CONTENT_ROWS * io_get_cols())
+#define IO_DATETIME_POS   (io_get_cols() - IO_DATETIME_LEN)
 
 void io_init(void);
 void io_clear_screen(void);

+ 6 - 0
kernel.c

@@ -122,9 +122,15 @@ static void timer_init(void)
 	__asm__ volatile ("sti");
 }
 
+extern unsigned char __bss_start[], __bss_end[];
+
 void kernel_main(void) __attribute__((section(".text.entry")));
 void kernel_main(void)
 {
+	unsigned char *p = __bss_start;
+	while (p < __bss_end)
+		*p++ = 0;
+
 	io_init();
 	timer_init();
 	terminal_run();

+ 5 - 1
kernel.ld

@@ -10,7 +10,11 @@ SECTIONS
 	}
 	.rodata : { *(.rodata .rodata.*) }
 	.data : { *(.data .data.*) }
-	.bss : { *(.bss .bss.*) *(COMMON) }
+	.bss : {
+		__bss_start = .;
+		*(.bss .bss.*) *(COMMON)
+		__bss_end = .;
+	}
 	_end = .;
 }
 ASSERT(_end <= 0x20000, "Kernel too large would overwrite stack/DMA at 0x20000");

+ 1 - 0
terminal.c

@@ -248,6 +248,7 @@ void terminal_run(void)
 					} else if (is_cmd_clock(line, line_len)) {
 						program_clock_main();
 						io_drain_keyboard();
+						io_clear_screen();
 						draw_header_row();
 					} else if (is_cmd_cat(line, line_len, cat_fname, CAT_FNAME_MAX)) {
 						program_cat_main(cat_fname);

+ 147 - 2
vga.c

@@ -1,10 +1,41 @@
-/* VGA mode 13h / mode 03h via direct register programming.
-   Saves and restores the 8x16 font from plane 2 so text mode survives. */
+/* VGA: VESA LFB (from boot) or legacy mode 13h / mode 03h. */
 
 #include "vga.h"
 
 #define VGA_FB ((volatile unsigned char *)0xA0000)
 
+/* Cached boot framebuffer info (set once when vga_fb_* first used). */
+static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
+static volatile unsigned char *vga_fb_lfb;
+static int vga_fb_cached;
+
+static volatile struct vga_boot_fb_info *vga_get_boot_fb(void)
+{
+	volatile struct vga_boot_fb_info *p = VGA_BOOT_FB;
+	__asm__ volatile ("" : "+r"(p));
+	return p;
+}
+
+static void vga_cache_boot_fb(void)
+{
+	volatile struct vga_boot_fb_info *info;
+
+	if (vga_fb_cached)
+		return;
+	vga_fb_cached = 1;
+	info = vga_get_boot_fb();
+	if (!info->valid) {
+		vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
+		vga_fb_lfb = 0;
+		return;
+	}
+	vga_fb_w = info->width;
+	vga_fb_h = info->height;
+	vga_fb_pitch_val = info->pitch;
+	vga_fb_bpp_val = info->bpp;
+	vga_fb_lfb = (volatile unsigned char *)(unsigned long)info->lfb_base;
+}
+
 static inline void outb(unsigned short port, unsigned char v)
 {
 	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
@@ -191,3 +222,117 @@ void vga_clear(unsigned char color)
 	for (i = 0; i < n; i++)
 		VGA_FB[i] = color;
 }
+
+/* ---- LFB API ---- */
+
+unsigned int vga_fb_width(void)
+{
+	vga_cache_boot_fb();
+	return vga_fb_w;
+}
+
+unsigned int vga_fb_height(void)
+{
+	vga_cache_boot_fb();
+	return vga_fb_h;
+}
+
+unsigned int vga_fb_pitch(void)
+{
+	vga_cache_boot_fb();
+	return vga_fb_pitch_val;
+}
+
+unsigned int vga_fb_bpp(void)
+{
+	vga_cache_boot_fb();
+	return vga_fb_bpp_val;
+}
+
+void *vga_fb_ptr(void)
+{
+	vga_cache_boot_fb();
+	return (void *)vga_fb_lfb;
+}
+
+int vga_has_vesa(void)
+{
+	vga_cache_boot_fb();
+	return (vga_fb_w != 0 && vga_fb_lfb != 0);
+}
+
+/* 16bpp: RGB 5:6:5.  32bpp: BGRX byte order (standard VBE / QEMU). */
+static void vga_lfb_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
+{
+	unsigned int pitch = vga_fb_pitch_val;
+	unsigned int bpp = vga_fb_bpp_val;
+	unsigned char *p;
+
+	if (x >= vga_fb_w || y >= vga_fb_h || !vga_fb_lfb)
+		return;
+	p = (unsigned char *)vga_fb_lfb + (unsigned long)y * pitch;
+	if (bpp == 16) {
+		unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
+		unsigned int off = x * 2;
+		p[off] = (unsigned char)(rgb16 & 0xFF);
+		p[off + 1] = (unsigned char)(rgb16 >> 8);
+	} else if (bpp == 24) {
+		unsigned int off = x * 3;
+		p[off]     = (unsigned char)b;
+		p[off + 1] = (unsigned char)g;
+		p[off + 2] = (unsigned char)r;
+	} else if (bpp == 32) {
+		unsigned int off = x * 4;
+		p[off]     = (unsigned char)b;
+		p[off + 1] = (unsigned char)g;
+		p[off + 2] = (unsigned char)r;
+		p[off + 3] = 0;
+	}
+}
+
+void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
+{
+	vga_cache_boot_fb();
+	if (!vga_fb_lfb)
+		return;
+	vga_lfb_put_pixel(x, y, r, g, b);
+}
+
+void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
+{
+	unsigned int pitch = vga_fb_pitch_val;
+	unsigned int bpp = vga_fb_bpp_val;
+
+	vga_cache_boot_fb();
+	if (!vga_fb_lfb)
+		return;
+	if (bpp == 16) {
+		unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
+		unsigned short val = (unsigned short)((rgb16 & 0xFF) | ((rgb16 >> 8) << 8));
+		volatile unsigned short *fb = (volatile unsigned short *)vga_fb_lfb;
+		unsigned int n = (pitch / 2) * vga_fb_h;
+		unsigned int i;
+		for (i = 0; i < n; i++)
+			fb[i] = val;
+	} else if (bpp == 24) {
+		volatile unsigned char *p = vga_fb_lfb;
+		unsigned int row, x;
+		for (row = 0; row < vga_fb_h; row++) {
+			unsigned int row_off = row * pitch;
+			for (x = 0; x < vga_fb_w; x++) {
+				unsigned int off = row_off + x * 3;
+				p[off]     = (unsigned char)b;
+				p[off + 1] = (unsigned char)g;
+				p[off + 2] = (unsigned char)r;
+			}
+		}
+	} else if (bpp == 32) {
+		volatile unsigned int *fb = (volatile unsigned int *)vga_fb_lfb;
+		unsigned int pixel = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
+		unsigned int stride = pitch / 4;
+		unsigned int row, x;
+		for (row = 0; row < vga_fb_h; row++)
+			for (x = 0; x < vga_fb_w; x++)
+				fb[row * stride + x] = pixel;
+	}
+}

+ 26 - 2
vga.h

@@ -1,14 +1,38 @@
-/* VGA graphics: mode 13h (320x200 256-color) and text mode 03h.
-   Works on real VGA hardware and QEMU alike. */
+/* VGA: VESA LFB (from boot) or legacy mode 13h / text mode 03h. */
 #ifndef VGA_H
 #define VGA_H
 
+/* Legacy mode 13h (320x200 256-color); used only when no VESA. */
 #define VGA_GFX_WIDTH   320
 #define VGA_GFX_HEIGHT  200
 
+/* Boot video info at 0x600 (written by boot.S in safe conventional memory).
+   Layout: valid(1) width(2) height(2) pitch(2) bpp(1) lfb_base(4) = 12 bytes. */
+struct vga_boot_fb_info {
+	unsigned char  valid;
+	unsigned short width;
+	unsigned short height;
+	unsigned short pitch;
+	unsigned char  bpp;
+	unsigned int   lfb_base;
+} __attribute__((packed));
+
+#define VGA_BOOT_FB ((volatile struct vga_boot_fb_info *)0x600)
+
 void vga_set_mode_13(void);
 void vga_set_mode_text(void);
 void vga_put_pixel(int x, int y, unsigned char color);
 void vga_clear(unsigned char color);
 
+/* LFB API (use when VESA valid). RGB 0..255. */
+unsigned int vga_fb_width(void);
+unsigned int vga_fb_height(void);
+unsigned int vga_fb_pitch(void);
+unsigned int vga_fb_bpp(void);
+void *vga_fb_ptr(void);
+int vga_has_vesa(void);
+
+void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b);
+void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b);
+
 #endif