Skip to content

6.3 Display VGA, Framebuffer, Console and Serial

Glen edited this page Nov 14, 2026 · 2 revisions

Display: VGA, Framebuffer, Console and Serial

Relevant source files

CadenzaOS provides a multi-tiered display and output architecture that supports both legacy VGA text mode and modern VESA linear framebuffers, alongside unified console multiplexing and UART serial mirroring. This subsystem is designed to operate in freestanding environments (without standard library formatting dependencies) while offering graceful fallback capabilities between graphics and text modes.

Sources: src/kernel/vga.zig1-37src/kernel/console.zig1-13


1. Legacy VGA Text Mode (vga.zig)

The vga.zig module serves as the primary text output abstraction for early boot and text-mode operations. It manages an 80x25 character grid mapped directly to physical address 0xB8000 when running in legacy text mode.

Key Data Structures and Mechanics

  • VGA Buffer Pointer: Defined as a volatile 16-bit pointer array referencing physical address 0xB8000 (src/kernel/vga.zig31). Each character cell consists of two bytes: a low-byte ASCII character and a high-byte attribute (foreground/background color).
  • Graphics Mode Switch: The graphics_mode boolean variable (src/kernel/vga.zig37) determines whether output is directed to the hardware VGA buffer or forwarded to the VESA linear framebuffer subsystem (src/kernel/framebuffer.zig1-197).
  • Cursor and Scroll Management: Tracks cursor_row and cursor_col (src/kernel/vga.zig45-49), implementing line wrapping, scrolling via scroll() (src/kernel/vga.zig196-220), and hardware cursor updates.

Diagram: VGA and Framebuffer Output Routing

diagram

Sources: src/kernel/vga.zig1-186src/kernel/console.zig1-23


2. VESA Framebuffer & Font Rendering (framebuffer.zig, font.zig)

When CadenzaOS initializes a VESA graphics mode, framebuffer.zig takes over rendering by writing directly to a linear framebuffer pointer supplied by the bootloader (src/kernel/framebuffer.zig3-19).

Rendering Pipeline and Font Integration

Diagram: Framebuffer Rendering Architecture

diagram

Sources: src/kernel/framebuffer.zig1-197src/kernel/font.zig1-40src/kernel/font8x16.bin1


3. Unified Console Multiplexing (console.zig)

To ensure that interactive display users and external debugging tools observe identical output streams, console.zig provides a multiplexing layer that broadcasts writes to both the display backend and the serial port (src/kernel/console.zig3-10).

Core Functions

  • writeString(s, fg, bg): Forwards text to vga.writeString() and serial.writeString() (src/kernel/console.zig19-23).
  • writeRaw(s, fg, bg): Bypasses higher-level terminal filters and mirrors raw output (src/kernel/console.zig25-31).
  • putChar(c, fg, bg): Sends a single character to both targets (src/kernel/console.zig33-37).
  • writeStringAt(row, col, s, fg, bg): Writes fixed-position text to the VGA buffer while intentionally suppressing serial output, as fixed coordinates do not map to linear serial streams (src/kernel/console.zig39-46).
  • clearScreen(fg, bg): Clears the display and emits a synchronisation marker over serial (src/kernel/console.zig48-54).
  • console_lock: A global IrqSpinLock is used to serialize multi-context console access, preventing race conditions during output (src/kernel/console.zig16).

Sources: src/kernel/console.zig1-54


4. UART Serial Port Driver (serial.zig)

The UART serial driver configures and communicates with COM1 at I/O port base 0x3F8 (src/kernel/drivers/serial.zig3-11). It provides synchronous, polled I/O for debugging and remote logging.

Register Configuration and Operation

  • Initialization:init() disables interrupts, sets the DLAB divisor for 38400 baud (8N1), enables the FIFO with a 14-byte threshold, and activates RTS/DTR (src/kernel/drivers/serial.zig23-33). This initialization is crucial for early kernel debugging.
  • Polling Transmission:putChar() busy-waits on the Line Status Register (LSR) until the Transmit Holding Register Empty (LSR_THRE) bit is set (src/kernel/drivers/serial.zig37-40).
  • String Expansion:writeString() automatically expands newline characters (\n) into carriage return and line feed (\r\n) pairs for terminal compatibility (src/kernel/drivers/serial.zig42-49).
  • Byte Reception:readByte() checks the Data Ready (LSR_DR) flag to read incoming serial data without blocking (src/kernel/drivers/serial.zig51-56).

Diagram: Serial Port Initialization (Stage 2 Bootloader vs. Kernel)

diagram

Sources: src/kernel/drivers/serial.zig1-56src/stage2/stage2.asm81-104src/stage2/stage2.asm126-128


5. Formatting and Conversion Helpers (convert.zig)

Because freestanding kernel code cannot rely on standard library formatting features like std.fmt, convert.zig provides lightweight, zero-allocation integer formatting and parsing routines (src/kernel/convert.zig3-6).

Provided Utilities

  • toHex(T, value, buf): Converts integer types into uppercase fixed-width hexadecimal strings without a 0x prefix (src/kernel/convert.zig27-67). It supports various integer sizes by dynamically determining the number of bits and shift amounts.
  • u32ToStr(buf, value): Formats a 32-bit unsigned integer into decimal ASCII from right to left (src/kernel/convert.zig77-93).
  • u64ToStr(buf, value): Formats a 64-bit unsigned integer into decimal ASCII (src/kernel/convert.zig117-137).
  • strToU32(s): Parses an ASCII decimal string into a u32, returning an error.InvalidDigit if invalid digits are encountered (src/kernel/convert.zig103-115).

Sources: src/kernel/convert.zig1-137

Clone this wiki locally