终端顶行未按预期清除

时间:2014-12-31 03:30:47

标签: c linux x86 kernel elf

我跟随this barebones kernel tutorial。我目前正在尝试get terminal scrolling working,为此,我已将给定kernel.c文件中的代码修改为以下内容:

void terminal_putchar (char c) {
  if (c == '\n') {
    terminal_row++;
    terminal_column = 0;
  } else {
    terminal_putentryat(c, terminal_colour, terminal_column, terminal_row);
    if (++terminal_column == VGA_WIDTH) {
      terminal_column = 0;
      if (terminal_row < VGA_HEIGHT) {
         terminal_row++;
       } else {
         //clear top row
         for (size_t i = 0; i < VGA_WIDTH; i++) {
           terminal_buffer[i] = make_vgaentry(' ', terminal_colour);
         }
          //move everything up one row
         for (size_t y = 1; y < VGA_HEIGHT; y++) {
          for (size_t x = 0; x < VGA_WIDTH; x++) {
            size_t const old_index = y * VGA_WIDTH + x;
            size_t const new_index = (y-1) * VGA_WIDTH + x;
            terminal_buffer[new_index] = terminal_buffer[old_index];
            terminal_buffer[old_index] = make_vgaentry(' ', terminal_colour);
          }
        }
        //put us on the last (empty) row
        terminal_row = 24;
      }
    }
  }
}

然而,这并没有给我所期望的行为 - 当我告诉终端输出超过25行文本时,终端不会滚动,而只是简单地跳过最后一行。我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:1)

one of the logic errors is:

when the cursor is on row 24 (the last row as rows start with 0)

then a '\n' is passed to the function,

the row number is incremented
Now the cursor is on row 25 (past the end of the buffer)

Under the above scenario, a vertical scroll should have been
performed, leaving the cursor on row 24
with the whole row 24 being blanks

so the logic needs some more work