添加滚动功能

时间:2016-02-25 12:02:31

标签: c

我已经设法在我的程序中添加对换行的支持,我现在正试图弄清楚如何实现滚动功能,以便当终端已满时,用户可以向上和向下滚动。目前我正在输出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");
    }

1 个答案:

答案 0 :(得分: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);
  }
}

这是未经测试的代码,可能无法编译,也没有错误,但它至少应该让您知道该怎么做。