1

implement a scrollback buffer

This commit is contained in:
churl
2022-05-09 16:19:59 +02:00
parent b5a66f769e
commit 6f6301d5d0
7 changed files with 229 additions and 17 deletions

61
c_os/lib/ScrollbackBuffer.cc Executable file
View File

@ -0,0 +1,61 @@
#include "ScrollbackBuffer.h"
// NOTE: I added this file
void ScrollbackBuffer::buffer_next_line() {
this->current_linenumber = this->current_linenumber + 1;
this->current_linenumber = this->current_linenumber % this->lines;
}
CGA::cga_line_t* ScrollbackBuffer::get_current_line() const {
return (CGA::cga_line_t*)(this->buffer + this->current_linenumber);
}
void ScrollbackBuffer::line_to_buffer(CGA::cga_line_t* line) {
mymemcpy<CGA::cga_line_t>(this->get_current_line(), line);
this->buffer_next_line();
}
void ScrollbackBuffer::copy_page_from_buffer(CGA::cga_line_t* destination, unsigned char page) const {
if (page < 0 || page >= this->pages) {
return;
}
// We reverse the pagenumber so page 0 is always the current page
// (The last written line is always before curent_line
// so we need to take the last page if we want the newest lines)
unsigned char rpage = this->pages - page - 1;
// Copy linewise because page may wrap around buffer borders
// Pageheight: 4, Pages: 2 | page = 0 => rpage = 2 - 0 - 1 = 1
// LINE 0 | - line = 0 => wrapline = (5 + 1 * 4 + 0) % 8 = 1
// LINE 1 | - [...]
// LINE 2 | - line = 3 => wrapline = (5 + 1 * 4 + 3) % 8 = 4
// LINE 3 |
// LINE 4 | page = 1 => rpage = 2 - 1 - 1 = 0
// LINE 5 - current_linenumber | - line = 0 => wrapline = (5 + 0 * 4 + 0) % 8 = 5
// LINE 6 | - [...]
// LINE 7 | - line = 3 => wrapline = (5 + 0 * 4 + 3) % 8 = 0
unsigned int wrapline;
for (unsigned int line = 0; line < this->pageheight; ++line) {
wrapline = (this->current_linenumber + rpage * this->pageheight + line) % this->lines;
mymemcpy<CGA::cga_line_t>(destination + line, this->buffer + wrapline);
}
}
void ScrollbackBuffer::copy_page_from_pagebuffer(CGA::cga_page_t* destination) const {
mymemcpy<CGA::cga_page_t>(destination, this->pagebuffer);
}
void ScrollbackBuffer::copy_page_to_pagebuffer(CGA::cga_page_t* source) {
mymemcpy<CGA::cga_page_t>(this->pagebuffer, source);
}
void ScrollbackBuffer::clear() {
for (unsigned char page = 0; page < this->pages; ++page) {
myzero<CGA::cga_page_t>((CGA::cga_page_t*)this->buffer + page);
}
myzero<CGA::cga_page_t>(this->pagebuffer);
this->current_linenumber = 0;
}

39
c_os/lib/ScrollbackBuffer.h Executable file
View File

@ -0,0 +1,39 @@
#ifndef __SCROLLBACKBUFFER_INCLUDE_H_
#define __SCROLLBACKBUFFER_INCLUDE_H_
#include "devices/CGA.h"
#include "lib/MyStdLib.h"
#include <stddef.h>
// NOTE: I added this file
// The buffer dimensions must match with the destination dimensions
class ScrollbackBuffer {
private:
CGA::cga_line_t* buffer; // Circular buffer to store lines that left the screen
CGA::cga_page_t* pagebuffer; // Contains the current page separately from the scrollback
unsigned int current_linenumber;
CGA::cga_line_t* get_current_line() const; // Translate linenumber to memory location
void buffer_next_line(); // Moves the current_line one line forward
public:
const unsigned int pageheight, // Number of lines per page
pages, // Number of pages in buffer
lines; // Total number of lines in buffer
ScrollbackBuffer(unsigned char width, unsigned char height, unsigned char pages)
: current_linenumber(0), pageheight(height), pages(pages), lines(height * pages) {
this->buffer = new CGA::cga_line_t[lines];
this->pagebuffer = new CGA::cga_page_t;
this->clear(); // Null out the buffer so no crap gets displayed
}
void line_to_buffer(CGA::cga_line_t* line);
void copy_page_from_buffer(CGA::cga_line_t* destination, unsigned char page) const;
void copy_page_from_pagebuffer(CGA::cga_page_t* destination) const;
void copy_page_to_pagebuffer(CGA::cga_page_t* source);
void clear();
};
#endif