Johannes Zellner 8 сар өмнө
parent
commit
34d8cdfaa2
7 өөрчлөгдсөн 345 нэмэгдсэн , 257 устгасан
  1. 9 3
      Makefile
  2. 178 0
      io.c
  3. 27 0
      io.h
  4. 19 254
      kernel.c
  5. 95 0
      terminal.c
  6. 10 0
      terminal.h
  7. 7 0
      timer.h

+ 9 - 3
Makefile

@@ -18,7 +18,13 @@ all: image.bin
 boot.o: boot.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.o: kernel.c
+kernel.o: kernel.c io.h terminal.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+io.o: io.c io.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+terminal.o: terminal.c terminal.h io.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 boot.elf: boot.o
@@ -27,7 +33,7 @@ boot.elf: boot.o
 timer.o: timer.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.o timer.o
+kernel.elf: kernel.o timer.o io.o terminal.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -48,4 +54,4 @@ run: image.bin
 	qemu-system-i386 -M pc -drive file=image.bin,format=raw 2>/dev/null || qemu-system-x86_64 -M pc -drive file=image.bin,format=raw
 
 clean:
-	rm -f boot.o kernel.o timer.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin
+	rm -f boot.o kernel.o timer.o io.o terminal.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin

+ 178 - 0
io.c

@@ -0,0 +1,178 @@
+/* I/O library: VGA text output and PS/2 keyboard. */
+
+#include "io.h"
+
+#define VGA_BASE ((volatile unsigned short *)0xB8000)
+#define VGA_ATTR 0x0A
+#define KB_DATA  0x60
+#define KB_STATUS 0x64
+
+static inline unsigned char inb(unsigned short port)
+{
+	unsigned char v;
+	__asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
+	return v;
+}
+
+static inline void outb(unsigned short port, unsigned char v)
+{
+	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+}
+
+static void disable_hw_cursor(void)
+{
+	outb(0x3D4, 0x0A);
+	outb(0x3D5, 0x20);
+}
+
+/* Scancode set 1 (XT) -> ASCII. 0 = ignore. */
+static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
+	[0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
+	[0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
+	[0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
+	[0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
+	[0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
+	[0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
+	[0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
+	[0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
+	[0x28] = '\'', [0x29] = '`',
+	[0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
+	[0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
+	[0x35] = '/', [0x39] = ' ',
+};
+
+static int shift_pressed;
+static unsigned int cursor;
+static unsigned short cursor_cell;
+static unsigned char last_scancode;
+
+static char apply_shift(char c)
+{
+	if (!shift_pressed) return c;
+	if (c >= 'a' && c <= 'z') return (char)(c - 32);
+	switch ((unsigned char)c) {
+		case '1': return '!';  case '2': return '@';  case '3': return '#';
+		case '4': return '$';  case '5': return '%';  case '6': return '^';
+		case '7': return '&';  case '8': return '*';  case '9': return '(';
+		case '0': return ')';  case '-': return '_';  case '=': return '+';
+		case '[': return '{';  case ']': return '}';  case ';': return ':';
+		case '\'': return '"'; case '`': return '~';  case '\\': return '|';
+		case ',': return '<';  case '.': return '>';  case '/': return '?';
+		default: return c;
+	}
+}
+
+static void hide_cursor_at(unsigned int pos)
+{
+	VGA_BASE[pos] = cursor_cell;
+}
+
+void io_init(void)
+{
+	disable_hw_cursor();
+	io_clear_screen();
+	cursor = 0;
+	cursor_cell = (unsigned short)(VGA_ATTR << 8);
+	io_drain_keyboard();
+}
+
+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);
+}
+
+void io_putchar(char c)
+{
+	if (c == '\n') {
+		hide_cursor_at(cursor);
+		cursor = (cursor / IO_VGA_COLS + 1) * IO_VGA_COLS;
+		if (cursor >= IO_CONTENT_SIZE) {
+			unsigned int i;
+			for (i = 0; i < (IO_CONTENT_ROWS - 1) * IO_VGA_COLS; i++)
+				VGA_BASE[i] = VGA_BASE[i + IO_VGA_COLS];
+			for (i = (IO_CONTENT_ROWS - 1) * IO_VGA_COLS; i < IO_CONTENT_SIZE; i++)
+				VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
+			cursor = (IO_CONTENT_ROWS - 1) * IO_VGA_COLS;
+		}
+		cursor_cell = (unsigned short)(VGA_ATTR << 8);
+		return;
+	}
+	if (c == '\b') {
+		if (cursor > 0) {
+			hide_cursor_at(cursor);
+			cursor--;
+			cursor_cell = VGA_BASE[cursor];
+		}
+		return;
+	}
+	if (cursor < IO_CONTENT_SIZE) {
+		VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
+		cursor++;
+		cursor_cell = (unsigned short)(VGA_ATTR << 8);
+	}
+}
+
+void io_putstr(const char *s)
+{
+	while (*s) io_putchar(*s++);
+}
+
+unsigned int io_get_cursor(void)
+{
+	return cursor;
+}
+
+#define CURSOR_BLOCK ((unsigned short)((VGA_ATTR << 8) | 0xDB))
+
+void io_cursor_show_block(void)
+{
+	VGA_BASE[cursor] = CURSOR_BLOCK;
+}
+
+void io_cursor_hide_block(void)
+{
+	VGA_BASE[cursor] = cursor_cell;
+}
+
+void io_vga_write(unsigned int pos, unsigned short value)
+{
+	VGA_BASE[pos] = value;
+}
+
+void io_drain_keyboard(void)
+{
+	while (inb(KB_STATUS) & 1)
+		(void)inb(KB_DATA);
+}
+
+char io_poll_key(void)
+{
+	if (!(inb(KB_STATUS) & 1))
+		return 0;
+	{
+		unsigned char sc = inb(KB_DATA);
+		if (sc & 0x80) {
+			if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
+			return 0;
+		}
+		if (sc == 0x2A || sc == 0x36) {
+			shift_pressed = 1;
+			return 0;
+		}
+		{
+			char c = (char)scancode_to_ascii[sc];
+			if (c) {
+				last_scancode = sc;
+				return apply_shift(c);
+			}
+		}
+	}
+	return 0;
+}
+
+unsigned char io_last_scancode(void)
+{
+	return last_scancode;
+}

+ 27 - 0
io.h

@@ -0,0 +1,27 @@
+/* I/O library: VGA text output and PS/2 keyboard. Usable by terminal and other programs. */
+#ifndef IO_H
+#define IO_H
+
+/* Content area: rows 0..(IO_DEBUG_ROW-1). Debug row and busy position. */
+#define IO_VGA_COLS      80
+#define IO_VGA_ROWS      25
+#define IO_DEBUG_ROW     24
+#define IO_CONTENT_ROWS  IO_DEBUG_ROW
+#define IO_CONTENT_SIZE  (IO_CONTENT_ROWS * IO_VGA_COLS)
+#define IO_BUSY_POS      (IO_DEBUG_ROW * IO_VGA_COLS + 79)
+
+void io_init(void);
+void io_clear_screen(void);
+void io_putchar(char c);
+void io_putstr(const char *s);
+unsigned int io_get_cursor(void);
+void io_cursor_show_block(void);
+void io_cursor_hide_block(void);
+void io_vga_write(unsigned int pos, unsigned short value);
+void io_drain_keyboard(void);
+
+/* Poll keyboard; returns 0 or ASCII character. Shift applied. */
+char io_poll_key(void);
+unsigned char io_last_scancode(void);
+
+#endif

+ 19 - 254
kernel.c

@@ -1,48 +1,16 @@
-/* Kernel: entered from bootloader in 32-bit protected mode at 0x7E00.
- * Must be linked so this function is at 0x7E00 (see kernel.ld).
- * Simple terminal: keyboard echo. */
+/* Kernel: entry point at 0x7E00, timer (PIT 1 kHz), then run terminal. */
 
-#define VGA_BASE ((volatile unsigned short *)0xB8000)
-#define VGA_ATTR 0x0A  /* green on black */
-#define VGA_COLS 80
-#define VGA_ROWS 25
-#define VGA_SIZE (VGA_COLS * VGA_ROWS)
-#define DEBUG_ROW 24                    /* last row: debug line (input vs echo) */
-#define CONTENT_ROWS (DEBUG_ROW)        /* rows 0..23 for terminal */
-#define CONTENT_SIZE (CONTENT_ROWS * VGA_COLS)
-
-/* Port I/O */
-static inline unsigned char inb(unsigned short port)
-{
-	unsigned char v;
-	__asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
-	return v;
-}
+#include "io.h"
+#include "terminal.h"
+#include "timer.h"
 
 static inline void outb(unsigned short port, unsigned char v)
 {
 	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
 }
 
-/* Disable the VGA hardware cursor (BIOS blinking underline at top-left). */
-static void disable_hw_cursor(void)
-{
-	outb(0x3D4, 0x0A);  /* Cursor Start register */
-	outb(0x3D5, 0x20);  /* Bit 5 = cursor disable */
-}
-
-/* PS/2 keyboard: data port 0x60, status 0x64 (bit 0 = output buffer full) */
-#define KB_DATA  0x60
-#define KB_STATUS 0x64
+volatile unsigned int ticks;
 
-/* Timer: PIT at 1 kHz (1 tick = 1 ms). IRQ0 -> INT 0x20 after PIC remap. */
-#define PIT_HZ      1000
-#define CURSOR_MS   500
-#define BUSY_MS     33
-
-volatile unsigned int ticks;  /* milliseconds since timer_init */
-
-/* IDT entry (8 bytes) and table for vector 0x20 (IRQ0). */
 struct idt_entry {
 	unsigned short offset_lo;
 	unsigned short selector;
@@ -61,19 +29,23 @@ static struct idtr_desc idtr;
 
 extern void pit_irq_stub(void);
 
-void pit_irq(void);  /* called from timer.S stub */
+void pit_irq(void);
 
 void pit_irq(void)
 {
 	ticks++;
-	outb(0x20, 0x20);  /* EOI to master PIC */
+	outb(0x20, 0x20);
+}
+
+unsigned int get_ticks_ms(void)
+{
+	return ticks;
 }
 
 static void timer_init(void)
 {
 	unsigned int stub = (unsigned int)pit_irq_stub;
 
-	/* Remap PIC: IRQ0..7 -> INT 0x20..0x27 */
 	outb(0x20, 0x11);
 	outb(0xA0, 0x11);
 	outb(0x21, 0x20);
@@ -82,21 +54,19 @@ static void timer_init(void)
 	outb(0xA1, 0x02);
 	outb(0x21, 0x01);
 	outb(0xA1, 0x01);
-	outb(0x21, 0xFE);  /* mask all except IRQ0 on master */
-	outb(0xA1, 0xFF);  /* mask all on slave */
+	outb(0x21, 0xFE);
+	outb(0xA1, 0xFF);
 
-	/* IDT entry for 0x20 (IRQ0) */
 	idt[0x20].offset_lo = (unsigned short)(stub & 0xFFFF);
 	idt[0x20].selector  = 0x08;
-	idt[0x20].zero     = 0;
-	idt[0x20].type     = 0x8E;  /* 32-bit interrupt gate */
+	idt[0x20].zero      = 0;
+	idt[0x20].type      = 0x8E;
 	idt[0x20].offset_hi = (unsigned short)((stub >> 16) & 0xFFFF);
 
 	idtr.limit = (unsigned short)(sizeof(idt) - 1);
 	idtr.base  = (unsigned int)idt;
 	__asm__ volatile ("lidt %0" : : "m"(idtr));
 
-	/* PIT channel 0: 1 kHz (divisor 1193) */
 	outb(0x43, 0x36);
 	outb(0x40, (unsigned char)(1193 & 0xFF));
 	outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
@@ -104,215 +74,10 @@ static void timer_init(void)
 	__asm__ volatile ("sti");
 }
 
-/* Scancode set 1 (XT) -> ASCII. 0 = ignore. Set 1 break = make | 0x80, we ignore those. */
-static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
-	[0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
-	[0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
-	[0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
-	[0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
-	[0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
-	[0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
-	[0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
-	[0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
-	[0x28] = '\'', [0x29] = '`',
-	[0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
-	[0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
-	[0x35] = '/', [0x39] = ' ',
-};
-
-/* Set 1: left shift make 0x2A break 0xAA, right shift make 0x36 break 0xB6 */
-static int shift_pressed;
-
-static unsigned int cursor;  /* linear position 0 .. CONTENT_SIZE-1 */
-static unsigned char last_scancode;     /* set by getchar for debug line */
-#define BUSY_POS (DEBUG_ROW * VGA_COLS + 79)  /* bottom-right: never overwritten by echo */
-static unsigned int busy_idx;   /* 0..3 for busy_chars */
-static unsigned int last_busy_tick;
-/* Explicit ASCII so no encoding/escape issues: - \ | / */
-static const unsigned char busy_chars[4] = { 0x2D, 0x5C, 0x7C, 0x2F };
-
-/* Blinking cursor: cell content when cursor is hidden; 0xDB = full block (CP437) */
-static unsigned short cursor_cell;
-static int cursor_visible;
-static unsigned int last_cursor_tick;
-#define CURSOR_BLOCK ((unsigned short)((VGA_ATTR << 8) | 0xDB))
-
-/* Restore cell at current cursor, then update cursor position and cursor_cell. */
-static void hide_cursor_at(unsigned int pos)
-{
-	VGA_BASE[pos] = cursor_cell;
-}
-
-static void putchar(char c)
-{
-	if (c == '\n') {
-		hide_cursor_at(cursor);
-		cursor = (cursor / VGA_COLS + 1) * VGA_COLS;
-		if (cursor >= CONTENT_SIZE) {
-			unsigned int i;
-			for (i = 0; i < (CONTENT_ROWS - 1) * VGA_COLS; i++)
-				VGA_BASE[i] = VGA_BASE[i + VGA_COLS];
-			for (i = (CONTENT_ROWS - 1) * VGA_COLS; i < CONTENT_SIZE; i++)
-				VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
-			cursor = (CONTENT_ROWS - 1) * VGA_COLS;
-		}
-		cursor_cell = (unsigned short)(VGA_ATTR << 8);
-		return;
-	}
-	if (c == '\b') {
-		if (cursor > 0) {
-			hide_cursor_at(cursor);
-			cursor--;
-			cursor_cell = VGA_BASE[cursor];
-		}
-		return;
-	}
-	if (cursor < CONTENT_SIZE) {
-		VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
-		cursor++;
-		cursor_cell = (unsigned short)(VGA_ATTR << 8);
-	}
-}
-
-/* Draw blinking cursor at current cursor position (every CURSOR_MS ms). */
-static void update_cursor_blink(void)
-{
-	unsigned int t = ticks;
-	if (t - last_cursor_tick >= CURSOR_MS) {
-		last_cursor_tick = t;
-		cursor_visible = !cursor_visible;
-	}
-	if (cursor_visible)
-		VGA_BASE[cursor] = CURSOR_BLOCK;
-	else
-		VGA_BASE[cursor] = cursor_cell;
-}
-
-/* Write one hex byte at VGA position; returns next position. */
-static unsigned int puthex(unsigned int pos, unsigned char byte)
-{
-	static const char hex[] = "0123456789ABCDEF";
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | hex[byte >> 4]);
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | hex[byte & 0x0F]);
-	return pos;
-}
-
-/* Update bottom line with last scancode and decoded char. Leaves col 79 for busy. */
-static void debug_line(unsigned char sc, char c)
-{
-	unsigned int i, pos = DEBUG_ROW * VGA_COLS;
-	for (i = 0; i < 79; i++)
-		VGA_BASE[pos + i] = (unsigned short)(VGA_ATTR << 8);
-	/* "SC:XX CH:YY 'c'" */
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'S');
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'C');
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ':');
-	pos = puthex(pos, sc);
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ' ');
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'C');
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | 'H');
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ':');
-	pos = puthex(pos, (unsigned char)c);
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | ' ');
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | '\'');
-	VGA_BASE[pos++] = (unsigned short)((VGA_ATTR << 8) | (c >= 0x20 && c < 0x7F ? (unsigned char)c : '.'));
-	VGA_BASE[pos]   = (unsigned short)((VGA_ATTR << 8) | '\'');
-}
-
-static void putstr(const char *s)
-{
-	while (*s) putchar(*s++);
-}
-
-/* Apply shift to get US layout shifted character. Returns c unchanged if no shift variant. */
-static char apply_shift(char c)
-{
-	if (!shift_pressed) return c;
-	if (c >= 'a' && c <= 'z') return (char)(c - 32);
-	switch ((unsigned char)c) {
-		case '1': return '!';  case '2': return '@';  case '3': return '#';
-		case '4': return '$';  case '5': return '%';  case '6': return '^';
-		case '7': return '&';  case '8': return '*';  case '9': return '(';
-		case '0': return ')';  case '-': return '_';  case '=': return '+';
-		case '[': return '{';  case ']': return '}';  case ';': return ':';
-		case '\'': return '"'; case '`': return '~';  case '\\': return '|';
-		case ',': return '<';  case '.': return '>';  case '/': return '?';
-		default: return c;
-	}
-}
-
-/* Draw busy indicator at bottom-right (every BUSY_MS ms). */
-static void update_busy(void)
-{
-	unsigned int t = ticks;
-	if (t - last_busy_tick >= BUSY_MS) {
-		last_busy_tick = t;
-		busy_idx = (busy_idx + 1) % 4;
-	}
-	VGA_BASE[BUSY_POS] = (unsigned short)((VGA_ATTR << 8) | busy_chars[busy_idx]);
-}
-
-/* String in .text so it loads with the kernel */
-static const char msg[] __attribute__((section(".text"))) = "Nebs OS |o|";
-static const char prompt[] __attribute__((section(".text"))) = "\n> ";
-
-/* Entry point: must be first in .text so bootloader jump to 0x7E00 hits it */
 void kernel_main(void) __attribute__((section(".text.entry")));
 void kernel_main(void)
 {
-	unsigned i;
-
-	disable_hw_cursor();  /* remove BIOS blinking underline at top-left */
-
-	/* Clear screen */
-	for (i = 0; i < VGA_SIZE; i++)
-		VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
-	cursor = 0;
-	cursor_cell = (unsigned short)(VGA_ATTR << 8);
-	cursor_visible = 1;
-	last_cursor_tick = 0;
-	last_busy_tick = 0;
-	busy_idx = 0;
-
-	putstr(msg);
-	putstr(prompt);
-
-	timer_init();  /* PIT 1 kHz, IDT, sti; ticks counts ms */
-	last_cursor_tick = ticks;
-	last_busy_tick   = ticks;
-
-	/* Drain keyboard buffer so we don't echo leftover/garbage bytes */
-	while (inb(KB_STATUS) & 1)
-		(void)inb(KB_DATA);
-
-	/* Terminal echo loop: busy indicator, blinking cursor at cursor, then poll keyboard */
-	for (;;) {
-		update_busy();
-		update_cursor_blink();
-		if (inb(KB_STATUS) & 1) {
-			unsigned char sc = inb(KB_DATA);
-			if (sc & 0x80) {
-				/* Set 1 break: release shift */
-				if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
-				continue;
-			}
-			if (sc == 0x2A || sc == 0x36) {
-				shift_pressed = 1;
-				continue;
-			}
-			{
-				char c = (char)scancode_to_ascii[sc];
-				if (c) {
-					c = apply_shift(c);
-					last_scancode = sc;
-					debug_line(last_scancode, c);
-					putchar(c);
-					if (c == '\n')
-						putstr(prompt);
-				}
-			}
-		}
-		else
-			__asm__ volatile ("hlt");  /* wait for next interrupt (PIT or other) */
-	}
+	io_init();
+	timer_init();
+	terminal_run();
 }

+ 95 - 0
terminal.c

@@ -0,0 +1,95 @@
+/* Terminal: prompt, busy indicator, blinking cursor, echo loop. */
+
+#include "io.h"
+#include "terminal.h"
+#include "timer.h"
+
+static unsigned int busy_idx;
+static unsigned int last_busy_tick;
+static const unsigned char busy_chars[4] = { 0x2D, 0x5C, 0x7C, 0x2F };
+
+static int cursor_visible;
+static unsigned int last_cursor_tick;
+
+static unsigned int puthex(unsigned int pos, unsigned char byte)
+{
+	static const char hex[] = "0123456789ABCDEF";
+	unsigned short cell = (unsigned short)(0x0A << 8);
+	io_vga_write(pos++, (unsigned short)(cell | hex[byte >> 4]));
+	io_vga_write(pos++, (unsigned short)(cell | hex[byte & 0x0F]));
+	return pos;
+}
+
+static void debug_line(unsigned char sc, char c)
+{
+	unsigned int i, pos = IO_DEBUG_ROW * IO_VGA_COLS;
+	for (i = 0; i < 79; i++)
+		io_vga_write(pos + i, (unsigned short)(0x0A << 8));
+	pos = IO_DEBUG_ROW * IO_VGA_COLS;
+	io_vga_write(pos++, (unsigned short)((0x0A << 8) | 'S'));
+	io_vga_write(pos++, (unsigned short)((0x0A << 8) | 'C'));
+	io_vga_write(pos++, (unsigned short)((0x0A << 8) | ':'));
+	pos = puthex(pos, sc);
+	io_vga_write(pos++, (unsigned short)((0x0A << 8) | ' '));
+	io_vga_write(pos++, (unsigned short)((0x0A << 8) | 'C'));
+	io_vga_write(pos++, (unsigned short)((0x0A << 8) | 'H'));
+	io_vga_write(pos++, (unsigned short)((0x0A << 8) | ':'));
+	pos = puthex(pos, (unsigned char)c);
+	io_vga_write(pos++, (unsigned short)((0x0A << 8) | ' '));
+	io_vga_write(pos++, (unsigned short)((0x0A << 8) | '\''));
+	io_vga_write(pos++, (unsigned short)((0x0A << 8) | (c >= 0x20 && c < 0x7F ? (unsigned char)c : '.')));
+	io_vga_write(pos,   (unsigned short)((0x0A << 8) | '\''));
+}
+
+static void update_busy(void)
+{
+	unsigned int t = get_ticks_ms();
+	if (t - last_busy_tick >= BUSY_MS) {
+		last_busy_tick = t;
+		busy_idx = (busy_idx + 1) % 4;
+	}
+	io_vga_write(IO_BUSY_POS, (unsigned short)((0x0A << 8) | busy_chars[busy_idx]));
+}
+
+static void update_cursor_blink(void)
+{
+	unsigned int t = get_ticks_ms();
+	if (t - last_cursor_tick >= CURSOR_MS) {
+		last_cursor_tick = t;
+		cursor_visible = !cursor_visible;
+	}
+	if (cursor_visible)
+		io_cursor_show_block();
+	else
+		io_cursor_hide_block();
+}
+
+static const char msg[] __attribute__((section(".text"))) = "Nebs OS |o|";
+static const char prompt[] __attribute__((section(".text"))) = "\n> ";
+
+void terminal_run(void)
+{
+	last_cursor_tick = get_ticks_ms();
+	last_busy_tick   = get_ticks_ms();
+	cursor_visible   = 1;
+	busy_idx         = 0;
+
+	io_putstr(msg);
+	io_putstr(prompt);
+
+	for (;;) {
+		update_busy();
+		update_cursor_blink();
+		{
+			char c = io_poll_key();
+			if (c) {
+				debug_line(io_last_scancode(), c);
+				io_putchar(c);
+				if (c == '\n')
+					io_putstr(prompt);
+			} else {
+				__asm__ volatile ("hlt");
+			}
+		}
+	}
+}

+ 10 - 0
terminal.h

@@ -0,0 +1,10 @@
+/* Terminal: prompt and echo loop. Uses io and timer. */
+#ifndef TERMINAL_H
+#define TERMINAL_H
+
+#define CURSOR_MS  500
+#define BUSY_MS    33
+
+void terminal_run(void);
+
+#endif

+ 7 - 0
timer.h

@@ -0,0 +1,7 @@
+/* Timer API: milliseconds since timer_init(). */
+#ifndef TIMER_H
+#define TIMER_H
+
+unsigned int get_ticks_ms(void);
+
+#endif