1

Buffered CGA refactor finish

This commit is contained in:
churl
2022-05-20 16:15:43 +02:00
parent 80cbe0900d
commit 5e2196c898
5 changed files with 29 additions and 36 deletions

View File

@ -4,8 +4,14 @@
// NOTE: This has to be called when memorymanagement is active // NOTE: This has to be called when memorymanagement is active
void BufferedCGA::init(unsigned int pages) { void BufferedCGA::init(unsigned int pages) {
this->scrollback_buffer = new ScrollbackBuffer(ROWS, pages); this->scrollback_buffer = new ScrollbackBuffer(ROWS, pages);
this->initialized = true; this->screen_buffer = new CGA::cga_page_t;
if (this->scrollback_buffer == NULL || this->screen_buffer == NULL) {
this->print("\nError initializing scrollback buffer\n\n", 39);
return;
}
this->initialized = true;
this->print("\nInitialized scrollback buffer\n\n", 32); this->print("\nInitialized scrollback buffer\n\n", 32);
} }
@ -13,7 +19,7 @@ void BufferedCGA::display_scrollback() {
if (this->initialized) { if (this->initialized) {
if (this->scrollback == 0) { if (this->scrollback == 0) {
// Use pagebuffer // Use pagebuffer
this->scrollback_buffer->save_screen((cga_page_t*)CGA_START); mmem::memcpy<CGA::cga_page_t>((CGA::cga_page_t*)CGA_START, this->screen_buffer);
} else { } else {
// Use scrollback // Use scrollback
this->scrollback_buffer->get((cga_line_t*)CGA_START, this->scrollback - 1); this->scrollback_buffer->get((cga_line_t*)CGA_START, this->scrollback - 1);
@ -50,6 +56,7 @@ void BufferedCGA::clear() {
if (this->initialized) { if (this->initialized) {
this->scrollback_buffer->clear(); this->scrollback_buffer->clear();
mmem::zero<CGA::cga_page_t>(this->screen_buffer);
} else { } else {
this->print("ScrollbackBuffer not initialized\n\n", 34); this->print("ScrollbackBuffer not initialized\n\n", 34);
} }
@ -60,7 +67,7 @@ void BufferedCGA::scroll_page_backward() {
// If this is the first scrollback we have to save the current screen content // If this is the first scrollback we have to save the current screen content
if (this->scrollback == 0) { if (this->scrollback == 0) {
this->scrollback_buffer->restore_screen((cga_page_t*)CGA_START); mmem::memcpy<CGA::cga_page_t>(this->screen_buffer, (CGA::cga_page_t*)CGA_START);
} }
// current_page can be equal to scrollback_buffer->pages // current_page can be equal to scrollback_buffer->pages

View File

@ -5,26 +5,32 @@
#include "devices/Keyboard.h" #include "devices/Keyboard.h"
#include "lib/ScrollbackBuffer.h" #include "lib/ScrollbackBuffer.h"
// NOTE: I added this file // NOTE: I added this file, I don't know if it will be used till the end but right know it's nice to have
// NOTE: I wanted to write to screen_buffer always and copy this to video mem to allow easier composition
// of different screen elements, but this would disable printing before initialization or I would
// have to replace print with unbuffered_print or something, which I don't like.
class BufferedCGA : public CGA { class BufferedCGA : public CGA {
private: private:
ScrollbackBuffer* scrollback_buffer; ScrollbackBuffer* scrollback_buffer; // Contains previous pages
bool initialized; // Don't do ScrollbackBuffer actions if not initialized CGA::cga_page_t* screen_buffer; // Contains the current page separately from the scrollback.
bool initialized; // Don't do ScrollbackBuffer actions if not initialized
BufferedCGA(const CGA& copy); BufferedCGA(const BufferedCGA&) = delete;
void display_scrollback(); // Write the current_page to CGA memory
public: public:
BufferedCGA() : CGA(), initialized(false), scrollback(0) {}; BufferedCGA() : CGA(), initialized(false), scrollback(0) {};
~BufferedCGA() { ~BufferedCGA() {
if (this->initialized) { delete this->scrollback_buffer; } if (this->initialized) {
delete this->scrollback_buffer;
delete this->screen_buffer;
}
} }
unsigned char scrollback; // The page that is displayed, public to enable page display 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 void init(unsigned int pages); // Scrollback needs to be initialized after memorymanagement
void display_scrollback(); // Write the current_page to CGA memory
void scroll_page_backward(); // Scroll up the page history void scroll_page_backward(); // Scroll up the page history
void scroll_page_forward(); // Scroll down the page history (to the current page) void scroll_page_forward(); // Scroll down the page history (to the current page)

View File

@ -345,8 +345,6 @@ void Keyboard::trigger() {
} }
// TODO: Keyboard insert mode // TODO: Keyboard insert mode
// kout.show(0, 0, (char)key);
} }
// TODO: Where to place this? // TODO: Where to place this?
@ -355,11 +353,9 @@ void scroll_mode(Key key) {
switch ((char)key) { switch ((char)key) {
case 'k': case 'k':
kout.scroll_page_backward(); kout.scroll_page_backward();
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.scrollback));
break; break;
case 'j': case 'j':
kout.scroll_page_forward(); kout.scroll_page_forward();
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.scrollback));
break; break;
} }
} }

View File

@ -25,18 +25,9 @@ void ScrollbackBuffer::get(CGA::cga_line_t* destination, unsigned char page) con
} }
} }
void ScrollbackBuffer::save_screen(CGA::cga_page_t* destination) const {
mmem::memcpy<CGA::cga_page_t>(destination, this->pagebuffer);
}
void ScrollbackBuffer::restore_screen(CGA::cga_page_t* source) {
mmem::memcpy<CGA::cga_page_t>(this->pagebuffer, source);
}
void ScrollbackBuffer::clear() { void ScrollbackBuffer::clear() {
for (unsigned char page = 0; page < this->pages; ++page) { for (unsigned char page = 0; page < this->pages; ++page) {
mmem::zero<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->pos = 0; this->pos = 0;
} }

View File

@ -7,15 +7,12 @@
// NOTE: I added this file // NOTE: I added this file
// The buffer dimensions must match with the destination dimensions
class ScrollbackBuffer { class ScrollbackBuffer {
private: private:
CGA::cga_page_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. unsigned int pos; // Buffer write position
// I thought this was easier since it also captures the little output
// generated before the scrollback buffer is initialized by accident ScrollbackBuffer(const ScrollbackBuffer&) = delete;
// (but only if it's less than a page)
unsigned int pos;
public: public:
const unsigned int pages; // Number of pages in buffer const unsigned int pages; // Number of pages in buffer
@ -23,19 +20,15 @@ public:
ScrollbackBuffer(unsigned char rows, unsigned char pages) ScrollbackBuffer(unsigned char rows, unsigned char pages)
: pos(0), pages(pages), rows(rows * pages) { : pos(0), pages(pages), rows(rows * pages) {
this->buffer = new CGA::cga_page_t[pages]; // Allocate with new because it's quite large, this->buffer = new CGA::cga_page_t[pages]; // Allocate with new because it's quite large
this->pagebuffer = new CGA::cga_page_t; // also I wanted to use the new memory manager. this->clear(); // Null out the buffer so no crap gets displayed
this->clear(); // Null out the buffer so no crap gets displayed.
} }
~ScrollbackBuffer() { ~ScrollbackBuffer() {
delete[] this->buffer; delete[] this->buffer;
delete this->pagebuffer;
} }
void put(CGA::cga_line_t* line); void put(CGA::cga_line_t* line);
void get(CGA::cga_line_t* destination, unsigned char page) const; void get(CGA::cga_line_t* destination, unsigned char page) const;
void save_screen(CGA::cga_page_t* destination) const; // Do not lose video memory when scrolling
void restore_screen(CGA::cga_page_t* source);
void clear(); void clear();
}; };