我已经设法在我的程序中添加了对新行的支持,现在我正在尝试如何实现滚动功能,以便当终端满时,用户可以上下滚动。目前,我正在输出25条信息"Hello“,并不是所有的信息都显示在终端上,因为终端上没有空间。因此,我希望实现滚动功能,以便用户能够读取终端上显示的所有消息。我试图实现缓冲区,但我似乎无法让它发挥作用,因为我不知道自己哪里出错了。这就是代码的样子:
任何能帮助我解决这个问题的建议或建议都将不胜感激。
uint8_t make_color(enum vga_color fg, enum vga_color bg) {
return fg | bg << 4;
}
uint16_t make_vgaentry(char c, uint8_t color) {
uint16_t c16 = c;
uint16_t color16 = color;
return c16 | color16 << 8;
}
size_t strlen(const char* str) {
size_t ret = 0;
while ( str[ret] != 0 )
ret++;
return ret;
}
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
size_t term_row;
size_t term_column;
uint8_t term_color;
uint16_t* term_buffer;
void terminal_initialize() {
term_row = 0;
term_column = 0;
term_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK); /*Terminal Background*/
term_buffer = (uint16_t*) 0xB8000;
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;
term_buffer[index] = make_vgaentry(' ', term_color);
term_buffer[y * VGA_WIDTH + x] = term_buffer[(y + 1) * VGA_WIDTH + x];
}
}
}
void term_setcolor(uint8_t color) {
term_color = color;
}
void *my_memmove(void *dest, const void *src, size_t n) {
signed char operation;
size_t end;
size_t current;
if(dest != src) {
if(dest < src) {
operation = 1;
current = 0;
end = n;
} else {
operation = -1;
current = n - 1;
end = -1;
}
for( ; current != end; current += operation) {
*(((unsigned char*)dest) + current) = *(((unsigned char*)src) + current);
}
}
return dest;
}
void term_scroll()
{
/* scroll up one line*/
my_memmove(term_buffer, term_buffer + VGA_WIDTH * sizeof(uint16_t),
(VGA_WIDTH - 1) * VGA_HEIGHT * sizeof(uint16_t)) ;
/* zero out the last line*/
for (unsigned int i = (VGA_HEIGHT-1) * VGA_WIDTH; i < (VGA_HEIGHT) * VGA_WIDTH; i++)
{
term_buffer[i] = make_vgaentry(' ', term_color);
}
}
void term_putentryat(char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x;
term_buffer[index] = make_vgaentry(c, color);
}
void term_putchar(char c) {
if (c == '\n')
{
term_column = 0;
if (++term_row == VGA_HEIGHT)
{
/*perform scroll; Trying to implement scrolling functionality here*/
term_column = 0;
}
}
else
term_putentryat(c, term_color, term_column, term_row);
if (++term_column == VGA_WIDTH) {
term_column = 0;
if (++term_row == VGA_HEIGHT) {
/*perform scroll; Trying to implement scrolling functionality here*/
term_row = 0;
}
}
}
void term_writestring(const char* data) {
size_t datalen = strlen(data);
for (size_t i = 0; i < datalen; i++)
term_putchar(data[i]);
}
其主要功能如下:
void main(){
term_initialize();
term_writestring(" Kernel World1!\n");
term_writestring(" Kernel World2!\n");
term_writestring(" Kernel World3!\n");
term_writestring(" Kernel World4!\n");
.
.
.
term_writestring(" Kernel World25!\n");
}
发布于 2016-02-25 04:46:15
试试这个:
void terminal_scroll()
{
// scroll up one line
memmove(term_buffer, term_buffer + VGA_WIDTH * sizeof(uint16_t),
(VGA_WIDTH - 1) * VGA_HEIGHT * sizeof(uint16_t)) ;
// zero out the last line
for (int i = (VGA_HEIGHT-1) * VGA_WIDTH; i < (VGA_HEIGHT) * VGA_WIDTH; i++)
{
term_buffer[i] = vgaentry(' ', terminal_color);
}
}
这是未经测试的代码,在可能不会编译或无错误,但它至少应该让您知道如何做。
https://stackoverflow.com/questions/35626745
复制