void vga_move_cursor(uint8_t column, uint8_t row) { cursor_row = row; cursor_column = column; /* wrap if the right-most edge of the screen is reached */ if (cursor_column > VGA_WIDTH - 1) { cursor_row++; cursor_column = 0; } /* scroll the screen if the cursor has fallen off the bottom XXX make this use memcpy once memcpy works */ if (cursor_row > VGA_HEIGHT - 1) { cursor_row = VGA_HEIGHT - 1; /* scroll up VGA memory by one line */ memcpy(VGA_MEMORY, VGA_MEMORY + VGA_WIDTH, (VGA_HEIGHT * VGA_WIDTH - VGA_WIDTH) * 2); /* blank the freshly exposed bottom line */ for (size_t index = 0; index < VGA_WIDTH; index++) { VGA_MEMORY[(VGA_HEIGHT - 1) * VGA_WIDTH + index] = vga_character(' '); } } /* poke the VGA ports to move the cursor XXX strictly the base port should be queried from the BIOS not hardcoded */ outb(0x3D4, 0x0F); outb(0x3D5, (unsigned char)(vga_cursor_memory_index() & 0xFF)); outb(0x3D4, 0x0E); outb(0x3D5, (unsigned char )((vga_cursor_memory_index() >> 8) & 0xFF)); }
/* clear the terminal and move the cursor back to the top left */ void terminal_clear() { for (uint8_t row = 0; row < VGA_HEIGHT; row++) { for (uint8_t column = 0; column < VGA_WIDTH; column++) { VGA_MEMORY[row * VGA_WIDTH + column] = vga_character(' '); } } vga_move_cursor(0, 0); }
void vga_initialize() { vga_row = 0; vga_column = 0; vga_color_palette = vga_color_palette_byte(kColorLightGray, kColorBlack); vga_buffer = (VgaCharacter *) kVgaBuffer; for (size_t entry = 0; entry < kVgaWidth * kVgaHeight; entry++) { vga_buffer[entry] = vga_character(' ', vga_color_palette); } }
/* write a null terminated string at the current cursor position. handles \n for newlines. */ void terminal_write(char *string) { for (size_t index = 0; string[index] != 0; index++) { if (string[index] == '\n') { vga_move_cursor(0, ++cursor_row); } else { VGA_MEMORY[vga_cursor_memory_index()] = vga_character(string[index]); vga_move_cursor(++cursor_column, cursor_row); } } }
void vga_place_character(char character, uint8_t color_palette, int row, int column) { const int index = row * kVgaWidth + column; vga_buffer[index] = vga_character(character, color_palette); }