1

BufferedCGA refactoring

This commit is contained in:
churl
2022-05-17 17:50:19 +02:00
parent c1b7c4cb5b
commit 13c7d898ab
7 changed files with 78 additions and 67 deletions

View File

@ -3,20 +3,20 @@
// Can't initialize in constructor as memory management already needs working CGA for output
// NOTE: This has to be called when memorymanagement is active
void BufferedCGA::init(unsigned int pages) {
this->scrollback_buffer = new ScrollbackBuffer(COLUMNS, ROWS, pages);
this->scrollback_buffer = new ScrollbackBuffer(ROWS, pages);
this->initialized = true;
this->print("\nInitialized scrollback buffer\n\n", 32);
}
void BufferedCGA::displaypage() {
void BufferedCGA::display_scrollback() {
if (this->initialized) {
if (this->current_page == 0) {
if (this->scrollback == 0) {
// Use pagebuffer
this->scrollback_buffer->copy_page_from_pagebuffer((cga_page_t*)CGA_START);
this->scrollback_buffer->copy_from_pagebuffer((cga_page_t*)CGA_START);
} else {
// Use scrollback
this->scrollback_buffer->copy_page_from_buffer((cga_line_t*)CGA_START, this->current_page - 1);
this->scrollback_buffer->get((cga_line_t*)CGA_START, this->scrollback - 1);
}
} else {
this->print("ScrollbackBuffer not initialized\n\n", 34);
@ -24,11 +24,11 @@ void BufferedCGA::displaypage() {
}
void BufferedCGA::print(char* string, int n, unsigned char attrib) {
if (this->current_page != 0) {
if (this->scrollback != 0) {
// Display newest content from buffer when new prints happen
this->current_page = 0;
this->displaypage();
this->scrollback = 0;
this->display_scrollback();
}
CGA::print(string, n, attrib);
@ -36,7 +36,7 @@ void BufferedCGA::print(char* string, int n, unsigned char attrib) {
void BufferedCGA::scrollup() {
if (this->initialized) {
this->scrollback_buffer->line_to_buffer((cga_line_t*)CGA_START);
this->scrollback_buffer->put((cga_line_t*)CGA_START);
} else {
this->print("ScrollbackBuffer not initialized\n\n", 34);
}
@ -46,7 +46,7 @@ void BufferedCGA::scrollup() {
void BufferedCGA::clear() {
CGA::clear();
this->current_page = 0;
this->scrollback = 0;
if (this->initialized) {
this->scrollback_buffer->clear();
@ -59,16 +59,16 @@ void BufferedCGA::scroll_page_backward() {
if (this->initialized) {
// If this is the first scrollback we have to save the current screen content
if (this->current_page == 0) {
this->scrollback_buffer->copy_page_to_pagebuffer((cga_page_t*)CGA_START);
if (this->scrollback == 0) {
this->scrollback_buffer->copy_to_pagebuffer((cga_page_t*)CGA_START);
}
// current_page can be equal to scrollback_buffer->pages
// as we have a separate pagebuffer for the current screen content
if (this->current_page < this->scrollback_buffer->pages) {
this->current_page = this->current_page + 1;
if (this->scrollback < this->scrollback_buffer->pages) {
this->scrollback = this->scrollback + 1;
}
this->displaypage();
this->display_scrollback();
} else {
this->print("ScrollbackBuffer not initialized\n\n", 34);
}
@ -77,10 +77,10 @@ void BufferedCGA::scroll_page_backward() {
void BufferedCGA::scroll_page_forward() {
if (this->initialized) {
if (this->current_page > 0) {
this->current_page = this->current_page - 1;
if (this->scrollback > 0) {
this->scrollback = this->scrollback - 1;
}
this->displaypage();
this->display_scrollback();
} else {
this->print("ScrollbackBuffer not initialized\n\n", 34);
}

View File

@ -14,13 +14,17 @@ private:
BufferedCGA(const CGA& copy);
void displaypage(); // Write the current_page to CGA memory
void display_scrollback(); // Write the current_page to CGA memory
public:
BufferedCGA() : CGA(), initialized(false), current_page(0) {};
BufferedCGA() : CGA(), initialized(false), scrollback(0) {};
~BufferedCGA() {
if (this->initialized) { delete this->scrollback_buffer; }
}
unsigned char scrollback; // The page that is displayed, public to enable page display
void init(unsigned int pages); // Scrollback needs to be initialized after memorymanagement
unsigned char current_page; // The page that is displayed
void scroll_page_backward(); // Scroll up the page history
void scroll_page_forward(); // Scroll down the page history (to the current page)

View File

@ -355,11 +355,11 @@ void scroll_mode(Key key) {
switch ((char)key) {
case 'k':
kout.scroll_page_backward();
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.current_page));
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.scrollback));
break;
case 'j':
kout.scroll_page_forward();
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.current_page));
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.scrollback));
break;
}
}

View File

@ -23,6 +23,7 @@
#define __OutStream_include__
#include "lib/StringBuffer.h"
#include <concepts>
// NOTE: I added this
class fillw {
@ -71,11 +72,14 @@ public:
// OPERATOR << : Umwandlung des angegebenen Datentypes in eine
// Zeichenkette.
// NOTE: I changed the stream manipulators to templates to be usable with different streams
// Needed because I added manipulators to the CGA_Stream class
// NOTE: I changed the stream manipulators to templates to be usable with different streams.
// If a Stream derived from OutStream gets passed to a operator<< the type won't be "lowered".
// This allows chaining of operator<< of different streams.
// Needed because I added operator<< overloads to the CGA_Stream class to change color with manipulators.
// Darstellung eines Zeichens (trivial)
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, char c) {
os.put(c);
if (c != '\n') {
@ -86,12 +90,14 @@ public:
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, unsigned char c) {
return os << (char)c;
}
// Darstellung einer nullterminierten Zeichenkette
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, char* string) {
char* pos = string;
@ -105,26 +111,31 @@ public:
// Darstellung ganzer Zahlen im Zahlensystem zur Basis base
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, short ival) {
return os << (long)ival;
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, unsigned short ival) {
return os << (unsigned long)ival;
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, int ival) {
return os << (long)ival;
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, unsigned int ival) {
return os << (unsigned long)ival;
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, long ival) {
// Bei negativen Werten wird ein Minuszeichen ausgegeben.
if (ival < 0) {
@ -136,6 +147,7 @@ public:
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, unsigned long ival) {
unsigned long div;
char digit;
@ -167,6 +179,7 @@ public:
// Darstellung eines Zeigers als hexadezimale ganze Zahl
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, void* ptr) {
int oldbase = os.base;
os.base = 16;
@ -178,12 +191,14 @@ public:
// Aufruf einer Manipulatorfunktion
// NOTE: Changed the function pointer type including the manipulator functions
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, T& (*f)(T&)) {
return f(os);
}
// NOTE: I added this
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, const fillw& w) {
os.flush(); // Flush the buffer to not modify previous output
os.fill_width = w.w;
@ -191,6 +206,7 @@ public:
}
template<typename T>
requires std::derived_from<T, OutStream>
friend T& operator<<(T& os, const fillc& c) {
os.flush();
os.fill_char = c.c;

View File

@ -2,21 +2,14 @@
// 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;
void ScrollbackBuffer::put(CGA::cga_line_t* line) {
CGA::cga_line_t* destination = (CGA::cga_line_t*)this->buffer + this->pos;
mmem::memcpy<CGA::cga_line_t>(destination, line);
this->pos = (this->pos + 1) % this->rows;
}
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) {
mmem::memcpy<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 {
void ScrollbackBuffer::get(CGA::cga_line_t* destination, unsigned char page) const {
if (page < 0 || page >= this->pages) {
return;
}
@ -38,24 +31,24 @@ void ScrollbackBuffer::copy_page_from_buffer(CGA::cga_line_t* destination, unsig
// 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;
mmem::memcpy<CGA::cga_line_t>(destination + line, this->buffer + wrapline);
for (unsigned int line = 0; line < (this->rows / this->pages); ++line) {
wrapline = (this->pos + rpage * (this->rows / this->pages) + line) % this->rows;
mmem::memcpy<CGA::cga_line_t>(destination + line, (CGA::cga_line_t*)this->buffer + wrapline);
}
}
void ScrollbackBuffer::copy_page_from_pagebuffer(CGA::cga_page_t* destination) const {
void ScrollbackBuffer::copy_from_pagebuffer(CGA::cga_page_t* destination) const {
mmem::memcpy<CGA::cga_page_t>(destination, this->pagebuffer);
}
void ScrollbackBuffer::copy_page_to_pagebuffer(CGA::cga_page_t* source) {
void ScrollbackBuffer::copy_to_pagebuffer(CGA::cga_page_t* source) {
mmem::memcpy<CGA::cga_page_t>(this->pagebuffer, source);
}
void ScrollbackBuffer::clear() {
for (unsigned char page = 0; page < this->pages; ++page) {
mmem::zero<CGA::cga_page_t>((CGA::cga_page_t*)this->buffer + page);
mmem::zero<CGA::cga_page_t>(this->buffer + page);
}
mmem::zero<CGA::cga_page_t>(this->pagebuffer);
this->current_linenumber = 0;
this->pos = 0;
}

View File

@ -10,32 +10,32 @@
// 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* buffer; // Circular buffer to store lines that left the screen
CGA::cga_page_t* pagebuffer; // Contains the current page separately from the scrollback.
// I thought this was easier since it also captures the little output
// generated before the scrollback buffer is initialized
// generated before the scrollback buffer is initialized by accident
// (but only if it's less than a page)
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
unsigned int pos;
public:
const unsigned int pageheight, // Number of lines per page
pages, // Number of pages in buffer
lines; // Total number of lines in buffer
const unsigned int pages; // Number of pages in buffer
const unsigned int rows; // 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];
ScrollbackBuffer(unsigned char rows, unsigned char pages)
: pos(0), pages(pages), rows(rows * pages) {
this->buffer = new CGA::cga_page_t[pages];
this->pagebuffer = new CGA::cga_page_t;
this->clear(); // Null out the buffer so no crap gets displayed
}
~ScrollbackBuffer() {
delete[] this->buffer;
delete this->pagebuffer;
}
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 put(CGA::cga_line_t* line);
void get(CGA::cga_line_t* destination, unsigned char page) const;
void copy_from_pagebuffer(CGA::cga_page_t* destination) const;
void copy_to_pagebuffer(CGA::cga_page_t* source);
void clear();
};

View File

@ -23,12 +23,6 @@ int main() {
// Initialize scrollback buffer after allocator.init()
kout.init(5);
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.current_page)); // Show current page
// text_demo();
// sound_demo();
// keyboard_demo();
// heap_demo();
// Tastatur-Unterbrechungsroutine 'einstoepseln'
/* hier muss Code eingefuegt werden */
@ -38,6 +32,10 @@ int main() {
/* hier muss Code eingefuegt werden */
cpu.enable_int();
// text_demo();
// sound_demo();
// keyboard_demo();
heap_demo();
// key_irq_demo();
while (1) {};