我正在嘗試編輯這個內核。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]);}
我需要像終端中的那樣逐行滾動。
uj5u.com熱心網友回復:
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 ;
}
}
}
uj5u.com熱心網友回復:
螢屏上顯示的文字直接取自記憶體。如果您移動該記憶體的內容,它將看起來像移動的文本。
因此,如果您從第二個“行”移動到最后一個“行”(到顯示記憶體的開頭),同時將最后一個“行”設定為全零,那么它看起來就像滾動一樣。
就像是:
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);
在此之后您要做的就是將游標放在最后一行的開頭。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367264.html
上一篇:Makefile錯誤如何處理:make:***[Makefile:11:test]錯誤3
下一篇:除了“intmain(intargc,char**argv)”之外,我可以安全地使用名稱中包含“main”的函式嗎?
