NKLegacy’s VGA driver targets the classic 80×25 text mode backed by the VGA frame buffer at physical addressDocumentation 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.
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
| Constant | Value | Description |
|---|---|---|
VGA_WIDTH | 80 | Number of character columns |
VGA_HEIGHT | 25 | Number of character rows |
VGA_MEMORY | 0xB8000 | Physical base address of the VGA text frame buffer |
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.
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:
| Character | Code | Behavior |
|---|---|---|
| Newline | \n | Moves cursor to column 0 of the next row |
| Carriage return | \r | Moves cursor to column 0 of the current row |
| Backspace | \b | If cursor_x > 0, moves cursor one column left and overwrites that cell with a space |
| Tab | \t | Advances 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 |
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.
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.
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:
vga_clear().
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:
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.
Color Attribute Reference
The table below lists every color attribute used inkmain.c. All entries have a black background (nibble 0).
| Value | Foreground | Background | Usage in kmain.c |
|---|---|---|---|
0x0F | White | Black | Default text, kernel version string |
0x0B | Light cyan | Black | Kernel name in boot banner |
0x08 | Dark gray | Black | Copyright notice |
0x0A | Light green | Black | [OK] status labels |
0x07 | Light gray | Black | Subsystem status descriptions |
0x0E | Yellow | Black | Shell prompt (nklegacy>) |
Scrolling
Whencursor_y reaches VGA_HEIGHT (25) after a vga_putchar call, the internal vga_scroll() function is invoked automatically:
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.
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.
Usage Example
The following snippet shows the VGA color-sequencing pattern used bykmain.c to produce the NT-style boot banner: