How to implement terminal scrolling?

Viewed 510

I am currently trying to develop an OS for drones. I am following this guide: https://wiki.osdev.org/Bare_Bones

Please find my code here, I intentionally removed the header files.

void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) 
{
    if(y > VGA_HEIGHT) {
        for(size_t y = 0; y < VGA_HEIGHT; y++) {
            const size_t index = y * VGA_WIDTH + x;
            terminal_buffer[index] = terminal_buffer[index+1];
            terminal_buffer[index] = vga_entry(c, color);
        }
    }
    const size_t index = y * VGA_WIDTH + x;
    terminal_buffer[index] = vga_entry(c, color);
}
 
void terminal_putchar(char c) 
{
    if(c == '\n') {
                terminal_row = terminal_row+1;
                terminal_column = 0;
                return;
        }

    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;
    }
}
 
void terminal_write(const char* data, size_t size) 
{
    for (size_t i = 0; i < size; i++)
        terminal_putchar(data[i]);
}
 
void terminal_writestring(const char* data) 
{
    terminal_write(data, strlen(data));
}
 
1 Answers

First copy the characters in the terminal buffer one line up:

memmove(terminal_buffer, terminal_buffer + VGA_WIDTH, VGA_WIDTH * (VGA_HEIGHT - 1) * sizeof(uint16_t));

Then clear the line at the bottom

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