Repository navigation
6.3 Display VGA, Framebuffer, Console and Serial
Relevant source files
- linker.ld
- src/kernel/.gitignore
- src/kernel/console.zig
- src/kernel/convert.zig
- src/kernel/drivers/serial.zig
- src/kernel/font.zig
- src/kernel/font8x16.bin
- src/kernel/framebuffer.zig
- src/kernel/tss.zig
- src/kernel/vga.zig
- src/stage2/stage2.asm
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
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.
-
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_modeboolean 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_rowandcursor_col(src/kernel/vga.zig45-49), implementing line wrapping, scrolling viascroll()(src/kernel/vga.zig196-220), and hardware cursor updates.

Sources: src/kernel/vga.zig1-186src/kernel/console.zig1-23
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).
-
Font Metrics:
font.zigembeds the classic IBM VGA 8x16 bitmap font (src/kernel/font.zig3-40). Each glyph isGLYPH_WIDTH(8 pixels) wide andGLYPH_HEIGHT(16 pixels) high, consuming 16 bytes per character (src/kernel/font.zig17-26). The raw bitmap data is embedded fromfont8x16.bin(src/kernel/font.zig40). -
Framebuffer State:
init()configuresfb_ptr,fb_stride,fb_width,fb_height, and bits-per-pixel (fb_bpp) alongside RGB channel bit offsets (src/kernel/framebuffer.zig97-126). These parameters are obtained from the bootloader's framebuffer information structure. -
Pixel Plotting:
plotPixel()handles both 24-bit (3 bytes) and 32-bit (4 bytes) packed pixel formats (src/kernel/framebuffer.zig133-144). The color value must already be packed into the format expected by the active video mode. -
Glyph Rasterization:
drawGlyph()reads individual row bytes fromfont.glyphsand expands set bits into foreground colors and cleared bits into background colors (src/kernel/font.zig40src/kernel/framebuffer.zig160-197). It uses a VGA-compatible colorpalette(src/kernel/framebuffer.zig65-82) andpackColor()(src/kernel/framebuffer.zig148-153) to convert VGA color indices to the framebuffer's native pixel format.

Sources: src/kernel/framebuffer.zig1-197src/kernel/font.zig1-40src/kernel/font8x16.bin1
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).
-
writeString(s, fg, bg): Forwards text tovga.writeString()andserial.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 globalIrqSpinLockis used to serialize multi-context console access, preventing race conditions during output (src/kernel/console.zig16).
Sources: src/kernel/console.zig1-54
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.
-
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).

Sources: src/kernel/drivers/serial.zig1-56src/stage2/stage2.asm81-104src/stage2/stage2.asm126-128
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).
-
toHex(T, value, buf): Converts integer types into uppercase fixed-width hexadecimal strings without a0xprefix (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 au32, returning anerror.InvalidDigitif invalid digits are encountered (src/kernel/convert.zig103-115).
Sources: src/kernel/convert.zig1-137
- Overview
- Getting Started and Build System
- Kernel Configuration and Global State
- Project Governance and Contributing
- Boot and Early Kernel Initialization
- Bootloader Stages and Linking
- kmain Boot Sequence
- Splash Screen and Branding
- Interrupts, Scheduling, and Syscalls
- IDT, PIC, APIC and IRQ Stubs
- Scheduler and Task Lifecycle
- System Call Interface
- Demonstration Tasks
- CodaFS Filesystem and Storage
- On-Disk Format and Operations
- Space Manager and Block Devices
- Binary Format and Program Loading
- Shell, Terminal, and Display
- Shell Command Pipeline
- Terminal, Input Devices and Prompts
- Display: VGA, Framebuffer, Console and Serial