What is VGA?
VGA stands for Video Graphics Array. It's a display standard introduced by IBM in 1987 as part of their PS/2 line of computers. VGA quickly became the de facto standard for displaying graphics on personal computers and remained prevalent for many years.
VGA (Video Graphics Array) text mode is a display mode supported by VGA-compatible video adapters, commonly used in PCs and similar systems. In text mode, the screen is divided into a grid of character cells, each capable of displaying a single character. Here's a breakdown of the key aspects of VGA text mode:
- Character Cells: In VGA text mode, the screen is divided into a grid of character cells. Each cell typically represents a single character and occupies a fixed amount of space on the screen. The most common dimensions for a character cell are 8 pixels wide and 16 pixels high.
- Text Buffer: VGA text mode uses a memory buffer, often referred to as the "text buffer" or "video memory", to store the characters to be displayed on the screen. This buffer is usually mapped to a specific region of memory, such as the address
0xB8000
in real mode or0xB8000
in protected mode on x86 systems. - Character Encoding: Characters to be displayed on the screen are encoded using ASCII or another character encoding scheme. Each character is represented by a numeric value, which corresponds to its position in the character set. For example, the ASCII value for the letter 'A' is 65.
- Color Attributes: VGA text mode allows for the specification of color attributes for both the foreground (text color) and background of each character cell. These attributes are typically encoded using a single byte, where the higher bits represent the background color and the lower bits represent the foreground color.
- Cursor Control: VGA text mode supports cursor control, allowing the position of the cursor on the screen to be manipulated. This enables text-based user interfaces to interactively display and edit text.
- Resolution: VGA text mode typically supports a resolution of 80 columns by 25 rows, resulting in a total of 2000 character cells on the screen. However, some VGA-compatible adapters may support different resolutions and aspect ratios.
- Performance: VGA text mode is relatively simple and efficient, making it well-suited for displaying textual information, such as console output in operating systems or command-line interfaces.
#include "stdint.h" // Include your custom stdint.h header
#define VGA_BUFFER_ADDR 0xB8000
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
// Define text mode attributes
#define TEXT_COLOR 0x0F // White text on black background
// Function to write a single character to the VGA text mode buffer
static inline void write_char(char c, size_t row, size_t col, uint8_t color) {
uint16_t* buffer = (uint16_t*)VGA_BUFFER_ADDR;
size_t index = (row * VGA_WIDTH + col);
buffer[index] = (uint16_t)c | ((uint16_t)color << 8);
}
// Function to clear the screen
void clear_screen() {
for (size_t row = 0; row < VGA_HEIGHT; row++) {
for (size_t col = 0; col < VGA_WIDTH; col++) {
write_char(' ', row, col, TEXT_COLOR);
}
}
}
// Function to initialize the console
void console_init() {
clear_screen();
// Other initialization steps such as setting cursor position can go here
}
// Example usage
void kernel_main(void) {
// Initialize the console
console_init();
// Other kernel initialization and main logic
}
// Function to calculate the VGA text mode buffer index from row and column
static inline size_t vga_buffer_index(size_t row, size_t col) {
return (row * VGA_WIDTH + col) * 2;
}
// Function to clear the screen with a specified background color
void clear_screen(uint8_t background_color) {
uint16_t* buffer = (uint16_t*)VGA_BUFFER_ADDR;
uint8_t color = (background_color << 4) | 0x0F; // Combine background color with default text color
// Fill the screen with spaces with the specified color attribute
for (size_t row = 0; row < VGA_HEIGHT; row++) {
for (size_t col = 0; col < VGA_WIDTH; col++) {
size_t index = vga_buffer_index(row, col);
buffer[index + 1] = color; // Set color attribute
}
}
}