OSDev:为什么我的 VGA 终端滚动不起作用?
OSDev: Why does my VGA terminal scrolling not work?
我正在开发操作系统作为个人爱好,所以我可以学习软件工程和计算机体系结构。
我试图在文本到达底部或 VGA_HEIGHT 时将 VGA 终端设置为 "scroll"。我正在使用来自 OSDev wiki 的代码和我自己的代码。
我的目标是复制每一行,然后把它写到它上面的那一行。
这是我正在使用的代码:
void terminal_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_putchar(char c) {
unsigned char uc = c;
switch(c) {
case NEWLINE:
terminal_row++;
terminal_column = 0;
terminal_putentryat(' ', terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
break;
case '\t':
/* TODO: Implement tab */
terminal_column += 4;
break;
default:
terminal_putentryat(uc, terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
terminal_row = 0;
}
}
if(terminal_row >= VGA_HEIGHT) {
terminal_print_error();
terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--)
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
}
但是这个功能只部分起作用。具体来说,这部分:
if(terminal_row >= VGA_HEIGHT) {
terminal_print_error();
terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--)
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
它只复制了部分数据。比如我用'printf()'写终端,如果字符串比滚动的数据长,就不会滚动。
/*
* This is the screen driver. It contains functions which print
* characters and colors to the screen
* using the VGA controller.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <kernel/tty.h>
#include "vga.h"
#define REG_SCREEN_CTRL 0x3D4
#define REG_SCREEN_DATA 0x3D5
#define NEWLINE 0x0A
#define TAB 0x09
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
static uint16_t *const VGA_MEMORY = (uint16_t *)0xC03FF000;
static size_t terminal_row;
static size_t terminal_column;
static uint8_t terminal_color;
static uint16_t *terminal_buffer;
int get_offset(int col, int row) {
return 2 * (row * VGA_WIDTH + col);
}
int get_offset_row(int offset) {
return offset / (2 * VGA_WIDTH);
}
int get_offset_col(int offset) {
return (offset - (get_offset_row(offset) * 2 * VGA_WIDTH)) / 2;
}
static void scroll() {
if(terminal_row >= VGA_HEIGHT) {
}
}
void terminal_print_error(void) {
if(terminal_row >= VGA_HEIGHT) {
terminal_row = 0;
/* print white/red E to bottom right corner of screen */
terminal_putentryat('E', vga_entry_color(VGA_COLOR_RED, VGA_COLOR_WHITE),
VGA_WIDTH - 1, VGA_HEIGHT - 1);
}
}
void terminal_initialize(void) {
terminal_row = 0;
terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_CYAN);
terminal_buffer = VGA_MEMORY;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(' ', terminal_color);
}
}
}
void terminal_setcolor(uint8_t color) {
terminal_color = color;
}
void terminal_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_putchar(char c) {
unsigned char uc = c;
switch(c) {
case NEWLINE:
terminal_row++;
terminal_column = 0;
terminal_putentryat(' ', terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
break;
case '\t':
/* TODO: Implement tab */
terminal_column += 4;
break;
default:
terminal_putentryat(uc, terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
terminal_row = 0;
}
}
if(terminal_row >= VGA_HEIGHT) {
terminal_print_error();
terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--)
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
}
void terminal_write(const char *data, size_t size) {
for (size_t i = 0; i < size; i++)
terminal_putchar(data[i]);
}
void terminal_writestring(const char *data) {
terminal_write(data, strlen(data));
}
/* inb */
unsigned char port_byte_in(unsigned short port) {
unsigned char result;
asm ("in %%dx, %%al" : "=a" (result) : "d" (port));
return result;
}
void port_byte_out(unsigned short port, unsigned char data) {
asm ("out %%al, %%dx" : : "a" (data), "d" (port));
}
int get_cursor_offset() {
/* Use the VGA ports to get the current cursor position
* 1. Ask for high byte of the cursor offset (data 14)
* 2. Ask for low byte (data 15)
*/
port_byte_out(REG_SCREEN_CTRL, 14);
int offset = port_byte_in(REG_SCREEN_DATA) << 8; /* High byte: << 8 */
port_byte_out(REG_SCREEN_CTRL, 15);
offset += port_byte_in(REG_SCREEN_DATA);
return offset * 2; /* Position * size of character cell */
}
void set_cursor_offset(int offset) {
/* Similar to get_cursor_offset, but instead of reading we write data */
offset /= 2;
port_byte_out(REG_SCREEN_CTRL, 14);
port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset >> 8));
port_byte_out(REG_SCREEN_CTRL, 15);
port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset & 0xff));
}
void update_cursor(int x, int y) {
uint16_t pos = y * VGA_WIDTH + x;
port_byte_out(REG_SCREEN_CTRL, 15);
port_byte_out(REG_SCREEN_DATA, (uint8_t)(pos & 0xFF));
port_byte_out(REG_SCREEN_CTRL, 14);
port_byte_out(REG_SCREEN_DATA, (uint8_t)(pos >> 8) & 0xFF);
}
/*
void enable_cursor(uint8_t cursor_start, uint8_t cursor_end) {
outb(0x3D4, 0x0A);
outb(0x3D, (inb(0x3D5) & 0xC0) | cursor_start);
outb(0x3D4, 0x0B);
outb(0x3D5, (inb(0x3D5) & 0xE0) | cursor_end);
}
*/
我相信循环滚动工作 大部分 是正确的,但是在它之前的很多代码导致了您 运行 遇到的一些错误。
switch 语句中的默认情况是当光标宽度超过屏幕右边缘时递增光标行。如果行增量超过屏幕底部边缘,if (++terminal_row == VGA_HEIGHT)
会将光标行重置为 0。这可以防止滚动代码 运行。您应该删除 if (++terminal_row == VGA_HEIGHT) terminal_row = 0;
并仅替换为 terminal_row++;
,因为紧随您的开关之后的逻辑处理终端行变量。
我建议将修改 terminal_row
和 terminal_column
的逻辑与验证、重置和滚动这些变量的逻辑分开。例如,您对 '\t' 字符的处理,如果放在一行的最后 3 个字符内,会将字符溢出到下一行,而不会将 terminal_row
和 terminal_column
变量更新到它们应该在的位置.
- 您的换行符将始终在行的开头留下一个空白字符,因为您
terminal_putentryat
在将光标修改为换行符之后而不是之前。实际上,您不必为任何换行符执行 terminal_putentryat
,因为显然没有字符发生变化,只有光标位置发生变化。
- 您可能希望修改
\t
的处理以调用 terminal_write(' ');
而不是直接修改列变量。这简化了实际更新终端的逻辑。上面的第二段详细说明了此更改解决的一些问题。
update_cursor()
只应在 terminal_putchar()
末尾调用一次,因为您放置的每个字符都应更新光标。如果您希望 terminal_putchar()
处理 0 宽度字符,这可能会改变,但这对我来说似乎违反直觉,因为此函数专门设计用于处理显示的字符。
- 修改终端缓冲区向上滚动的循环永远不会清除底行字符
处理 terminal_row >= VGA_HEIGHT
的函数底部的逻辑永远不会将 terminal_row 重置为有效值。它确实会调用 terminal_print_error()
,但是当您想将该行保留在底部时,此函数会将您的行重置为 0。
void terminal_putchar(char c) {
unsigned char uc = c;
// Handle character output and terminal_row/column modification
switch(c) {
case NEWLINE:
terminal_row++;
terminal_column = 0;
break;
case '\t':
terminal_write(' ');
break;
default:
terminal_putentryat(uc, terminal_color, terminal_column, terminal_row);
terminal_column++;
}
// Handle validation on terminal_column before terminal_row, since the logic in terminal_column can update terminal_row
if(terminal_column >= VGA_WIDTH) {
terminal_column = 0;
terminal_row++;
}
// Handle validating terminal_row, and scrolling the screen upwards if necessary.
if(terminal_row >= VGA_HEIGHT) {
// You shouldn't need terminal_print_error() since you are handling the case where terminal_row >= VGA_HEIGHT
// terminal_print_error();
// What does this line do? Appears to set the 16th character of the 16th row to the same value as the 5th character of the 1st row.
// terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--) {
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
// Also clear out the bottom row
for(i = 0; i < VGA_WIDTH-1; i++) {
terminal_putentryat(' ', terminal_color, i, VGA_HEIGHT-1);
}
terminal_row = VGA_HEIGHT-1;
}
update_cursor(terminal_column, terminal_row);
}
我正在开发操作系统作为个人爱好,所以我可以学习软件工程和计算机体系结构。
我试图在文本到达底部或 VGA_HEIGHT 时将 VGA 终端设置为 "scroll"。我正在使用来自 OSDev wiki 的代码和我自己的代码。
我的目标是复制每一行,然后把它写到它上面的那一行。
这是我正在使用的代码:
void terminal_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_putchar(char c) {
unsigned char uc = c;
switch(c) {
case NEWLINE:
terminal_row++;
terminal_column = 0;
terminal_putentryat(' ', terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
break;
case '\t':
/* TODO: Implement tab */
terminal_column += 4;
break;
default:
terminal_putentryat(uc, terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
terminal_row = 0;
}
}
if(terminal_row >= VGA_HEIGHT) {
terminal_print_error();
terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--)
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
}
但是这个功能只部分起作用。具体来说,这部分:
if(terminal_row >= VGA_HEIGHT) {
terminal_print_error();
terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--)
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
它只复制了部分数据。比如我用'printf()'写终端,如果字符串比滚动的数据长,就不会滚动。
/*
* This is the screen driver. It contains functions which print
* characters and colors to the screen
* using the VGA controller.
*/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <kernel/tty.h>
#include "vga.h"
#define REG_SCREEN_CTRL 0x3D4
#define REG_SCREEN_DATA 0x3D5
#define NEWLINE 0x0A
#define TAB 0x09
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
static uint16_t *const VGA_MEMORY = (uint16_t *)0xC03FF000;
static size_t terminal_row;
static size_t terminal_column;
static uint8_t terminal_color;
static uint16_t *terminal_buffer;
int get_offset(int col, int row) {
return 2 * (row * VGA_WIDTH + col);
}
int get_offset_row(int offset) {
return offset / (2 * VGA_WIDTH);
}
int get_offset_col(int offset) {
return (offset - (get_offset_row(offset) * 2 * VGA_WIDTH)) / 2;
}
static void scroll() {
if(terminal_row >= VGA_HEIGHT) {
}
}
void terminal_print_error(void) {
if(terminal_row >= VGA_HEIGHT) {
terminal_row = 0;
/* print white/red E to bottom right corner of screen */
terminal_putentryat('E', vga_entry_color(VGA_COLOR_RED, VGA_COLOR_WHITE),
VGA_WIDTH - 1, VGA_HEIGHT - 1);
}
}
void terminal_initialize(void) {
terminal_row = 0;
terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_CYAN);
terminal_buffer = VGA_MEMORY;
for (size_t y = 0; y < VGA_HEIGHT; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(' ', terminal_color);
}
}
}
void terminal_setcolor(uint8_t color) {
terminal_color = color;
}
void terminal_putentryat(unsigned char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_putchar(char c) {
unsigned char uc = c;
switch(c) {
case NEWLINE:
terminal_row++;
terminal_column = 0;
terminal_putentryat(' ', terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
break;
case '\t':
/* TODO: Implement tab */
terminal_column += 4;
break;
default:
terminal_putentryat(uc, terminal_color, terminal_column, terminal_row);
update_cursor(terminal_column + 1, terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
terminal_row = 0;
}
}
if(terminal_row >= VGA_HEIGHT) {
terminal_print_error();
terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4];
size_t i, j;
for(i = 0; i < VGA_WIDTH-1; i++) {
for(j = VGA_HEIGHT-2; j > 0; j--)
terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i];
}
}
}
void terminal_write(const char *data, size_t size) {
for (size_t i = 0; i < size; i++)
terminal_putchar(data[i]);
}
void terminal_writestring(const char *data) {
terminal_write(data, strlen(data));
}
/* inb */
unsigned char port_byte_in(unsigned short port) {
unsigned char result;
asm ("in %%dx, %%al" : "=a" (result) : "d" (port));
return result;
}
void port_byte_out(unsigned short port, unsigned char data) {
asm ("out %%al, %%dx" : : "a" (data), "d" (port));
}
int get_cursor_offset() {
/* Use the VGA ports to get the current cursor position
* 1. Ask for high byte of the cursor offset (data 14)
* 2. Ask for low byte (data 15)
*/
port_byte_out(REG_SCREEN_CTRL, 14);
int offset = port_byte_in(REG_SCREEN_DATA) << 8; /* High byte: << 8 */
port_byte_out(REG_SCREEN_CTRL, 15);
offset += port_byte_in(REG_SCREEN_DATA);
return offset * 2; /* Position * size of character cell */
}
void set_cursor_offset(int offset) {
/* Similar to get_cursor_offset, but instead of reading we write data */
offset /= 2;
port_byte_out(REG_SCREEN_CTRL, 14);
port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset >> 8));
port_byte_out(REG_SCREEN_CTRL, 15);
port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset & 0xff));
}
void update_cursor(int x, int y) {
uint16_t pos = y * VGA_WIDTH + x;
port_byte_out(REG_SCREEN_CTRL, 15);
port_byte_out(REG_SCREEN_DATA, (uint8_t)(pos & 0xFF));
port_byte_out(REG_SCREEN_CTRL, 14);
port_byte_out(REG_SCREEN_DATA, (uint8_t)(pos >> 8) & 0xFF);
}
/*
void enable_cursor(uint8_t cursor_start, uint8_t cursor_end) {
outb(0x3D4, 0x0A);
outb(0x3D, (inb(0x3D5) & 0xC0) | cursor_start);
outb(0x3D4, 0x0B);
outb(0x3D5, (inb(0x3D5) & 0xE0) | cursor_end);
}
*/
我相信循环滚动工作 大部分 是正确的,但是在它之前的很多代码导致了您 运行 遇到的一些错误。
switch 语句中的默认情况是当光标宽度超过屏幕右边缘时递增光标行。如果行增量超过屏幕底部边缘,if (++terminal_row == VGA_HEIGHT)
会将光标行重置为 0。这可以防止滚动代码 运行。您应该删除 if (++terminal_row == VGA_HEIGHT) terminal_row = 0;
并仅替换为 terminal_row++;
,因为紧随您的开关之后的逻辑处理终端行变量。
我建议将修改 terminal_row
和 terminal_column
的逻辑与验证、重置和滚动这些变量的逻辑分开。例如,您对 '\t' 字符的处理,如果放在一行的最后 3 个字符内,会将字符溢出到下一行,而不会将 terminal_row
和 terminal_column
变量更新到它们应该在的位置.
- 您的换行符将始终在行的开头留下一个空白字符,因为您
terminal_putentryat
在将光标修改为换行符之后而不是之前。实际上,您不必为任何换行符执行terminal_putentryat
,因为显然没有字符发生变化,只有光标位置发生变化。 - 您可能希望修改
\t
的处理以调用terminal_write(' ');
而不是直接修改列变量。这简化了实际更新终端的逻辑。上面的第二段详细说明了此更改解决的一些问题。 update_cursor()
只应在terminal_putchar()
末尾调用一次,因为您放置的每个字符都应更新光标。如果您希望terminal_putchar()
处理 0 宽度字符,这可能会改变,但这对我来说似乎违反直觉,因为此函数专门设计用于处理显示的字符。- 修改终端缓冲区向上滚动的循环永远不会清除底行字符
处理
terminal_row >= VGA_HEIGHT
的函数底部的逻辑永远不会将 terminal_row 重置为有效值。它确实会调用terminal_print_error()
,但是当您想将该行保留在底部时,此函数会将您的行重置为 0。void terminal_putchar(char c) { unsigned char uc = c; // Handle character output and terminal_row/column modification switch(c) { case NEWLINE: terminal_row++; terminal_column = 0; break; case '\t': terminal_write(' '); break; default: terminal_putentryat(uc, terminal_color, terminal_column, terminal_row); terminal_column++; } // Handle validation on terminal_column before terminal_row, since the logic in terminal_column can update terminal_row if(terminal_column >= VGA_WIDTH) { terminal_column = 0; terminal_row++; } // Handle validating terminal_row, and scrolling the screen upwards if necessary. if(terminal_row >= VGA_HEIGHT) { // You shouldn't need terminal_print_error() since you are handling the case where terminal_row >= VGA_HEIGHT // terminal_print_error(); // What does this line do? Appears to set the 16th character of the 16th row to the same value as the 5th character of the 1st row. // terminal_buffer[(15 * VGA_WIDTH) + 15] = terminal_buffer[(0 * VGA_WIDTH) + 4]; size_t i, j; for(i = 0; i < VGA_WIDTH-1; i++) { for(j = VGA_HEIGHT-2; j > 0; j--) { terminal_buffer[(j * VGA_WIDTH) + i] = terminal_buffer[((j+1) * VGA_WIDTH) + i]; } } // Also clear out the bottom row for(i = 0; i < VGA_WIDTH-1; i++) { terminal_putentryat(' ', terminal_color, i, VGA_HEIGHT-1); } terminal_row = VGA_HEIGHT-1; } update_cursor(terminal_column, terminal_row); }