Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/pastaboy12345/NKLegacy/llms.txt

Use this file to discover all available pages before exploring further.

NKLegacy’s VGA driver targets the classic 80×25 text mode backed by the VGA frame buffer at physical address 0xB8000. It supports colored text, hardware cursor positioning via the CRTC register pair, and automatic scrolling when output reaches the bottom of the screen.

Constants

ConstantValueDescription
VGA_WIDTH80Number of character columns
VGA_HEIGHT25Number of character rows
VGA_MEMORY0xB8000Physical base address of the VGA text frame buffer
Each cell in the frame buffer is a 16-bit value: the low byte holds the ASCII character code and the high byte holds the color attribute.

API Reference

void vga_clear(void)

Fills the entire 80×25 screen with space characters using the current color attribute, then resets the hardware cursor to column 0, row 0. Call this at startup (after vga_set_color) to produce a clean slate before printing the boot banner.
vga_set_color(0x0F);   /* white on black */
vga_clear();           /* blank the screen, cursor -> (0,0) */

void vga_putchar(char c)

Writes a single character to the screen at the current cursor position, then advances the cursor. The following control characters are handled specially:
CharacterCodeBehavior
Newline\nMoves cursor to column 0 of the next row
Carriage return\rMoves cursor to column 0 of the current row
Backspace\bIf cursor_x > 0, moves cursor one column left and overwrites that cell with a space
Tab\tAdvances cursor to the next 8-column boundary (cursor_x = (cursor_x + 8) & ~7); if the result is ≥ VGA_WIDTH, wraps to column 0 of the next row
For printable characters, if cursor_x reaches VGA_WIDTH (80) after the write, the cursor wraps to column 0 of the next row. After every character (printable or control), if cursor_y reaches VGA_HEIGHT (25), the driver calls the internal vga_scroll() function to shift all rows up one line before updating the hardware cursor.
vga_putchar('H');
vga_putchar('i');
vga_putchar('\n');
vga_putchar('\t');   /* advance to next tab stop */

void vga_print(const char *str)

Iterates over the NUL-terminated string str, calling vga_putchar for each byte. Returns when the NUL terminator is reached.
vga_print("NKLegacy kernel initialized.\n");

void vga_set_color(uint8_t c)

Sets the current color attribute used by all subsequent write operations. The attribute byte uses the standard CGA encoding:
Bits 7–4  Background color (high nibble)
Bits 3–0  Foreground color (low nibble)
The color takes effect for every cell written from this point forward, including cells cleared by vga_clear().
vga_set_color(0x0A);     /* light green on black */
vga_print("  [OK] ");
vga_set_color(0x07);     /* light gray on black */
vga_print("Keyboard driver loaded\n");

void vga_update_cursor(void)

Writes the current cursor_x / cursor_y position to the VGA CRTC (CRT Controller) so that the blinking hardware cursor tracks the software cursor. The position is encoded as a linear cell index:
pos = cursor_y * VGA_WIDTH + cursor_x
It is sent to the CRTC over I/O ports 0x3D4 / 0x3D5:
  • Register 0x0F (low byte): outb(0x3D4, 0x0F); outb(0x3D5, pos & 0xFF)
  • Register 0x0E (high byte): outb(0x3D4, 0x0E); outb(0x3D5, (pos >> 8) & 0xFF)
vga_update_cursor is called automatically at the end of every vga_putchar invocation, so drivers do not normally need to call it directly.

void vga_set_cursor(size_t x, size_t y)

Sets the internal cursor_x and cursor_y state variables to the supplied column and row, then calls vga_update_cursor to push the new position to hardware. Use this to jump the cursor to an arbitrary screen location.
vga_set_cursor(0, 0);    /* home the cursor after vga_clear */

Color Attribute Reference

The table below lists every color attribute used in kmain.c. All entries have a black background (nibble 0).
ValueForegroundBackgroundUsage in kmain.c
0x0FWhiteBlackDefault text, kernel version string
0x0BLight cyanBlackKernel name in boot banner
0x08Dark grayBlackCopyright notice
0x0ALight greenBlack[OK] status labels
0x07Light grayBlackSubsystem status descriptions
0x0EYellowBlackShell prompt (nklegacy>)

Scrolling

When cursor_y reaches VGA_HEIGHT (25) after a vga_putchar call, the internal vga_scroll() function is invoked automatically:
1

Copy rows upward

Every cell in rows 1–24 is copied to the row above (rows 0–23), shifting the entire visible content up by one line.
2

Clear the last row

Row 24 (the bottom row) is filled with space characters using the current color attribute, providing a blank line for new output.
3

Adjust cursor

cursor_y is set to VGA_HEIGHT - 1 (24) so the next character is written on the freshly cleared bottom row.

Usage Example

The following snippet shows the VGA color-sequencing pattern used by kmain.c to produce the NT-style boot banner:
#include "driver/vga/vga.h"

/* Phase 1: clear screen with white-on-black default */
vga_set_color(0x0F);
vga_clear();

/* Phase 4: print boot header */
vga_set_color(0x0B);   /* light cyan */
vga_print("NKLegacy Kernel ");
vga_set_color(0x0F);   /* white */
vga_print("v0.1.0 [Build: " __DATE__ " " __TIME__ "]\n");

vga_set_color(0x08);   /* dark gray */
vga_print("Copyright (c) 2026 William Beauregard. MIT License.\n");
vga_print("Windows NT-inspired kernel operating system.\n\n");

/* [OK] lines: light green label, light gray description */
vga_set_color(0x0A);
vga_print("  [OK] ");
vga_set_color(0x07);
vga_print("Global Descriptor Table\n");

/* ... repeated for IDT, PIC, PIT, PS/2 Keyboard, Serial ... */

/* Shell prompt in yellow */
vga_set_color(0x0E);
vga_print("nklegacy> ");
vga_set_color(0x0F);

Build docs developers (and LLMs) love