2016-02-25 165 views
0

我已經成功地在我的程序中添加了對換行符的支持,現在我正試圖弄清楚如何實現滾動功能,以便終端滿時用戶可以上下滾動。目前,我輸出25條消息「Hello World」,並且由於終端上沒有空間,因此並非所有消息都顯示出來。因此,我希望實現滾動功能,以便用戶可以讀取終端上顯示的所有消息。我試圖實現緩衝區,但我似乎無法使其工作,因爲我不知道我哪裏出錯。這是代碼的樣子:添加滾動功能

任何提示/建議,這將幫助我解決這個問題將不勝感激。

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"); 
    } 
+0

請不要在獲得答案後嘗試摧毀您的問題。這種行爲可能會導致暫時或永久禁止堆棧溢出。 – usr2564301

回答

1

試試這個:

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); 
    } 
} 

這是未經測試的代碼中,可能無法編譯,也不是免費的錯誤,但它至少應該給你知道該怎麼做。

+0

['memmove'](http://www.cplusplus.com/reference/cstring/memmove/)是一個標準功能。如果你不能在你的上下文中使用這個標準函數,那麼你必須寫它。谷歌「memmove源代碼」,你會發現噸的實現 –

+0

簡單的memmove:看[這裏](http://www.geeksforgeeks.org/write-memcpy/)。 –

+0

@ user2977378 _「我假設我需要在terminal_putchar中調用terminal_scroll函數?」_ ..好的...是的。 –