Skip to main content

Display System

The display system provides low-level screen manipulation functions for text-mode display output. It interfaces directly with video memory at address 0xb8000 and provides basic text output functionality with color support.

Features

  • Text-mode display (25 lines × 80 columns)
  • Color text support (16 colors)
  • Direct video memory access
  • Automatic scrolling
  • Cursor management

Screen Specifications

  • Screen dimensions: 25 lines × 80 columns
  • Each screen element consists of 2 bytes:
    • First byte: ASCII character
    • Second byte: Attribute byte (text color and background color)
  • Total screen size: 4000 bytes (25 × 80 × 2)

API Reference

Basic Output

void clear_screen(void)

Clears the entire screen by filling it with spaces and setting the default color attribute (light grey on black).

void print(const char *str)

Prints a null-terminated string to the screen using the default color (light grey on black).

Parameters:

  • str: Null-terminated string to print. If NULL, the function returns without doing anything.

Features:

  • Handles newline characters (\n)
  • Automatically scrolls when reaching the bottom of the screen
  • Stops printing if screen buffer is full

Color Output

void printc(const char *str, unsigned char color)

Prints a null-terminated string to the screen using the specified color attribute.

Parameters:

  • str: Null-terminated string to print
  • color: Color attribute byte specifying text and background colors

Cursor Control

void print_newline(void)

Moves the cursor to the beginning of the next line.

Features:

  • Automatically scrolls the screen up when reaching the bottom
  • Clears the new line at the bottom after scrolling

void backspace(void)

Removes the character before the current cursor position.

Features:

  • Moves cursor back one position
  • Clears the character at that position
  • Only operates if there is space to move back

Color Constants

ConstantValueDescription
CL_BLACK0x00Black
CL_BLUE0x01Blue
CL_GREEN0x02Green
CL_CYAN0x03Cyan
CL_RED0x04Red
CL_MAGENTA0x05Magenta
CL_BROWN0x06Brown
CL_LIGHT_GREY0x07Light Grey (Default)
CL_DARK_GREY0x08Dark Grey
CL_LIGHT_BLUE0x09Light Blue
CL_LIGHT_GREEN0x0ALight Green
CL_LIGHT_CYAN0x0BLight Cyan
CL_LIGHT_RED0x0CLight Red
CL_LIGHT_MAGENTA0x0DLight Magenta
CL_YELLOW0x0EYellow
CL_WHITE0x0FWhite

Implementation Details

Memory Layout

  • Video memory begins at physical address 0xb8000
  • Each character occupies two consecutive bytes:
    • Byte 1: ASCII character code
    • Byte 2: Attribute byte (foreground and background colors)

Scrolling Mechanism

When the screen is full:

  1. Copy all lines up one position
  2. Clear the bottom line
  3. Adjust cursor position
  4. Update video memory

Color Attributes

The attribute byte is divided into:

  • Bits 0-3: Foreground color
  • Bits 4-6: Background color
  • Bit 7: Blink bit (not commonly used)

Usage Examples

// Clear the screen
display::clear_screen();

// Print text in default color
display::print("Hello, World!\n");

// Print colored text
display::printc("Error: ", CL_RED);
display::printc("File not found\n", CL_WHITE);

// Print multiple lines
display::print("Line 1\nLine 2\nLine 3");