VGA Standard in Real Mode

VGA

VGA stands for Video Graphics Array. VGA standard was first developed by IBM.

VGA is a video display standard introduced by IBM that provides specific resolutions and color depths. It is capable of both text and graphics modes. The VGA standard provides a way to interact with video hardware in a standard way, but it is limited to low resolution display modes.

When a PC first boots up, it is set to a standard VGA text mode. This initial mode provides a basic interface for displaying text on the screen, which is essential for the BIOS and operating system to communicate with the user before more complex graphics drivers are loaded.

While the initial VGA text mode is useful for displaying text during the early stages of booting, you will eventually want to switch to a graphics mode to draw pixels and create more complex visual outputs. In real mode, this can be done using BIOS interrupts. However, when transitioning to protected mode, you lose direct access to BIOS services, which complicates the process.

Each video card you find in a PC comes with a piece of code that allows it to be initialized, the video BIOS. This bios runs in the Real Mode, and as long as you are, you can access all the functions directly by calling int 0x10 with the right values in the registers.

Many video cards have two interfaces, one VGA interface for low resolutions, and the VESA VBE interface for higher resolutions.

Key Characteristics of Initial VGA Text Mode

1 Resolution and Layout:

  • The default text mode is typically 80 columns by 25 rows, meaning the screen can display 80 characters horizontally and 25 characters vertically.
  • Each character cell is typically 9x16 pixels.

2 Memory Addressing:

  • The video memory for the default text mode starts at the address 0xB8000.
  • Each character on the screen is represented by two bytes: the ASCII code and an attribute byte that defines the foreground and background colors.

3 BIOS Role:

  • The BIOS (Basic Input/Output System) sets up the initial video mode during the Power-On Self Test (POST).
  • BIOS interrupt INT 10h is used for video services, including setting and querying the video mode, writing characters, and controlling the cursor.

Example of Default VGA Text Mode

Memory Layout:

  • Each character cell is represented by two bytes:
    • The first byte is the ASCII code of the character.
    • The second byte is the attribute byte, which defines the foreground and background colors.

Attribute Byte Structure:

  • The attribute byte is typically organized as follows:
    • Bits 0-3: Foreground color.
    • Bits 4-6: Background color.
    • Bit 7: Blink attribute (if supported).

Interface:

The VGA (Video Graphics Array) interface and the VBE (VESA BIOS Extensions) interface are both methods used to interact with graphics hardware, but they differ in their capabilities, standards, and level of abstraction.

1 VGA Interface

VGA is a widely supported interface providing basic video functionality, including several text and graphics modes.

  • Definition: VGA is a graphics standard introduced by IBM in 1987, providing a basic set of graphics modes and control registers for graphics display on computer monitors.
  • Capabilities:
    • VGA provides standard text and graphics modes, such as 80x25 text mode and 320x200 graphics mode.
    • It supports basic operations like setting pixel colors, drawing lines and shapes, and scrolling the screen.
  • Access Methods:
    • Direct Memory Access (DMA) and memory-mapped I/O (MMIO) are commonly used to interact with VGA hardware.
    • Developers directly access VGA memory addresses and control registers to perform graphics operations.
  • Standardization:
    • VGA is a widely supported standard for graphics display on x86-based computers.
    • It is relatively low-level, providing direct access to hardware registers and memory.

2 VESA VBE Interface

VESA (Video Electronics Standards Association) VBE (VESA BIOS Extensions): VESA VBE is a standard interface for accessing higher resolutions and color depths than what VGA offers.

  • Definition: VBE is a set of extensions to the standard VGA BIOS, developed by the Video Electronics Standards Association (VESA) to provide standardized graphics capabilities beyond VGA.
  • Capabilities:
    • VBE extends VGA by providing additional graphics modes, color depths, and resolution options.
    • It introduces higher-level functions for tasks such as setting display modes, manipulating graphics buffers, and querying hardware capabilities.
  • Access Methods:
    • VBE provides a standardized interface through BIOS interrupts, allowing software to access its features in a uniform way.
    • Instead of directly accessing hardware registers and memory, developers call BIOS functions to perform graphics operations.
  • Standardization:
    • VBE is standardized by the VESA organization, ensuring compatibility across different hardware platforms and BIOS implementations.
    • It provides a higher level of abstraction compared to VGA, making it easier for developers to write graphics software that works across different systems.

Common VGA Modes in Real Mode:

1 Text Mode:

VGA (Video Graphics Array) text modes provide a simple way to display text-based user interfaces on computer monitors. These text modes define the number of columns and rows of characters that can be displayed on the screen, as well as the character size and color attributes. Here are some common VGA text modes:

  • Video Memory Organization:
    • In VGA text modes, video memory is typically organized as a linear array of memory locations.
    • Each character cell on the screen occupies two consecutive memory locations: one for the character code and one for the attribute byte.
    • The starting address of video memory for text modes is usually 0xB8000 (in hexadecimal) while for graphic modes it is 0xA0000.
  • Character Representation:
    • The character byte in each cell contains the ASCII code of the character to be displayed.
    • For example, the ASCII code for the letter 'A' is 65 (or 0x41 in hexadecimal).
  • Attribute Byte:
    • The attribute byte in each cell determines the color and other display attributes of the character.
    • Typically, the attribute byte is divided into two parts: the foreground color (lower nibble) and the background color (upper nibble).
  • Memory Access:
    • To display text on the screen, programs directly write to video memory using memory access instructions like MOV (move) or we can use interrupt function provided by the BIOS. (INT 0x0E for this case).
    • For example, to write the character 'A' with white foreground and black background to the top-left corner of the screen, programs write the ASCII code of 'A' to 0xB8000 and the attribute byte for white-on-black to 0xB8001.
  • Updating Display:
    • Whenever the content of video memory is modified, the display hardware automatically updates the screen to reflect the changes.
    • This allows programs to dynamically update the text displayed on the screen by simply modifying the corresponding memory locations in video memory.

2 Graphics Mode:

In addition to text modes, VGA (Video Graphics Array) also supports various graphics modes, which allow for the display of graphics on the screen.

The most common graphic mode is Mode 13h:

  • Resolution: 320 pixels x 200 pixels.
  • Color Depth: Supports 256 colors (8 bits per pixel).
  • Memory Layout: Each pixel is represented by a single byte, with each byte corresponding to a color index in the palette.
  • Memory Addressing: The starting address of video memory for Mode 13h is typically 0xA0000.
  • Palette: A fixed palette of 256 colors is typically used, allowing for a wide range of colors to be displayed simultaneously.

References: Get to Know more about these modes: https://thejat.in/learn/changing-video-mode-using-bios

VGA Controller:

The VGA controller has both a memory mapped frame buffer and an I/O mapped CRTC.

Key Components of the VGA Controller:

  • Video Memory:
    • Memory dedicated to storing the display data (pixels or characters).
    • Different starting addresses for text and graphics modes (0xB8000 for text mode, 0xA0000 for graphics mode).
  • Control Registers: Accesses through I/O ports.
    • Attribute Controller: Manages color attributes. Used to set color attributes and control the display of text and graphics modes.
      • Index Register: 0x3C0
      • Data Register: 0x3C1
    • CRT Controller: Controls the timing, resolution, and display format.
      • Address Register: 0x3D4
      • Data Register: 0x3D5
    • Sequencer: Controls the operation of the VGA sequencer.
      • Address Register: 0x3C4
      • Data Register: 0x3C5
    • Graphics Controller: Manages the mapping of video memory to the display.
      • Address Register: 0x3CE
      • Data Register: 0x3CF
    • Digital-to-Analog Converter (DAC): Converts digital signals to analog signals for the monitor.
      • Address Write Mode: 0x3C8
      • Data Register: 0x3C9

VGA in Real Mode:

When the x86 system boots up, then because of the compatibility then the system is set to VGA standard by the BIOS. BIOS initializes the VGA hardware setting up default text mode.

Text Mode 3 (80x25 Text Mode): The Default Mode

  • Resolution: 80 columns x 25 rows of text.
  • Character Size: Each character occupies an 8x16 pixel grid.
  • Color Depth: Typically supports 16 foreground colors and 8 background colors.
  • Total Displayable Characters: 80 * 25 = 2000 characters.
  • Video Memory Address: Typically starts at 0xB8000 (in hexadecimal).
  • Printing Characters:
    • Printing characters in VGA text mode 3 can be achieved using both direct memory access as we know the mapped address and by BIOS interrupt. We already did all this like many times.

You can visit and explore more about this in below posts:

https://thejat.in/learn/printing-char-to-the-display

Switch to a Different Mode (Graphic Mode):

As we all know there are different flavors of text modes available in the real mode for the VGA standard. The most loved and lucrative one is “Mode 13”

Mode 13h, also known as VGA mode 13, is a graphics mode commonly used in DOS-era programming for simple 320x200 pixel resolution graphics with 256 colors.

I already explained about mode 13 in detail. We can switch to mode 13 with the help of BIOS Interrupt. We can do so in real mode since bios functions are only available in real mode environment. Once we are switch to either protected or long mode, it becomes complicated like switch back to real mode, switch mode and returning back to respective mode. Once we are in graphic mode 13h we cant print character to the screen like we are doing in the default text mode. Because now we are up with the fundamental unit of display which is pixel we have to make our own way to print characters using bitmaps which we will do later on.

Below is the code snippet to switch to mode 13h:

; Set video mode to Mode 13h (320x200, 256 colors)
mov     ax, 0x0013   ; AH = 0x00 (video function), AL = 0x13 (Mode 13h)
int     0x10         ; BIOS interrupt call to set video mode

In this code snippet:

  • mov ax, 0x0013 sets AX register to 0x0013, where AH = 0x00 represents the video function, and AL = 0x13 represents Mode 13h.
  • int 0x10 triggers the BIOS interrupt 0x10, which will set the video mode to Mode 13h.
  • After setting the video mode, you can proceed with your graphics rendering code.
  • After switching modes screen gets cleared.

In the next chapter we will give example of changing the video mode in our boot code and try to draw some graphics as well

Important:

  • In Mode 13h, which is a graphics mode with a resolution of 320x200 pixels and 256 colors, printing characters in the traditional sense (like in text modes) is not directly supported. Unlike text modes where characters are predefined and can be displayed by simply writing ASCII codes to specific memory locations, Mode 13h operates at the pixel level, and each pixel is represented by a single byte in video memory.
  • However, it's still possible to achieve a similar effect by creating bitmap fonts and rendering characters pixel by pixel. This process involves defining a bitmap representation for each character and then rendering those bitmaps onto the screen by setting the appropriate pixel colors in video memory.

References

Operating Systems Development Series (brokenthorn.com)

brokenthorn.com/Resources/OSDevVid2.html

Realmode Assembly - Writing bootable stuff - Part 5 - Programming - 0x00sec - The Home of the Hacker

Why is it that in x86 real mode some devices (e.g. VGA) have memory regions mapped while others (e.g. keyboard) don't? Should I use in and OUT instructions for those that are not mapped? - Quora

Programming (archive.org)