c-内核终端中的光标



我正在尝试编辑这个内核。c、 它不支持游标。我写了25行,它显示的不超过25行,我无法向上或向下查看其余部分。这是除主函数外的全部代码。

uint8_t make_color(enum vga_color fg, enum vga_color bg) {
return fg | bg << 4;}
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
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 terminal_row;
size_t terminal_column;
uint8_t terminal_color;
uint16_t* terminal_buffer;
void terminal_initialize() {
terminal_row = 0;
terminal_column = 0;
terminal_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK);
terminal_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;
terminal_buffer[index] = make_vgaentry(' ',
terminal_color);
}}}
void terminal_setcolor(uint8_t color) {
terminal_color = color;
}
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) 
{
const size_t index = y * VGA_WIDTH + x;
if(y>VGA_HEIGHT){
size_t index = (VGA_HEIGHT - 1) * VGA_WIDTH;
for(size_t x = 0; x < VGA_WIDTH; ++x)
{
terminal_buffer[index + x] = vga_entry(' ', terminal_color);
}
}
else
terminal_buffer[index] = make_vgaentry(c, color);}
void terminal_putchar(char c) {
terminal_putentryat(c, terminal_color, terminal_column, 
terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT)
terminal_row = 0;}
else if (c == 'n')
{
terminal_row = terminal_row + 1;
terminal_column = 0;
}
}
void terminal_writestring(const char* data) {
size_t datalen = strlen(data);
for (size_t i = 0; i < datalen; i++)
terminal_putchar(data[i]);}

我需要像终端中那样逐行滚动。

void terminal_scrollUp(void)
{
for (size_t y = 1; 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_WIDTH] = terminal_buffer[index];
}
}
for(size_t col = 0; col < VGA_WIDTH; col++)
{
terminal_putentryat(' ', terminal_color, col, VGA_HEIGHT - 1);
}
}
void terminal_putchar(char c) 
{
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH || c == 'n') 
{
terminal_column = 0;
if(terminal_row == VGA_HEIGHT - 1)
{
terminal_scrollUp();
}
else
{
terminal_row++;
}
}
}

屏幕上显示的文本直接取自内存。如果你移动内存中的内容,它看起来就像移动的文本。

因此,如果你从第二个移动到最后一个";行";向上一行(到显示存储器的开头(;行";如果全部为零,则它看起来像是滚动的。

类似于:

char *ptr_to_first_line = terminal_buffer;
char *ptr_to_second_line = terminal_buffer + VGA_WIDTH * 2;  // *2 because of the attribute byte
char *ptr_to_last_line = terminal_buffer + (VGA_WIDTH * 2) * (VGA_HEIGHT - 1); // -1 to get the beginning of the last line
char *ptr_to_end = terminal_buffer + VGA_WIDTH * 2 * VGA_HEIGHT;
// Move (copy, overlapping memory okay) the memory, making appear as it's scrolling
// This copies from the second line to the first line, the third line to the second line, etc.
memmove(ptr_to_first_line, ptr_to_second_line, ptr_to_end - ptr_to_second_line);
// Clear the last line so it appears empty
memset(ptr_to_last_line, 0, VGA_WIDTH * 2);

在这之后,您所要做的就是将光标放在最后一行的开头。

相关内容

  • 没有找到相关文章

最新更新