Refactor screen ouput from main console code

This commit is contained in:
Justin C. Miller
2018-04-24 09:32:57 -07:00
parent eb13f1f4fb
commit 94de87ef86
5 changed files with 224 additions and 196 deletions

View File

@@ -51,7 +51,6 @@ WARNFLAGS += -Wno-attributes -Wno-sign-compare -Wno-multichar
WARNFLAGS += -Wno-div-by-zero -Wno-endif-labels -Wno-pragmas
WARNFLAGS += -Wno-format-extra-args -Wno-unused-result
WARNFLAGS += -Wno-deprecated-declarations -Wno-unused-function
WARNFLAGS += -Wno-unused-but-set-parameter
ASFLAGS ?=
ASFLAGS += -p $(BUILD_D)/versions.s
@@ -62,7 +61,7 @@ CFLAGS += -std=c11 -mcmodel=large
CXXFLAGS := $(INCLUDES) $(DEPENDFLAGS) $(BASEFLAGS) $(WARNFLAGS)
CXXFLAGS += -DGIT_VERSION="\"$(VERSION)\""
CXXFLAGS += -std=c++14 -mcmodel=large
CXXFLAGS += -std=c++14 -mcmodel=large -fno-rtti
BOOT_CFLAGS := $(INCLUDES) $(DEPENDFLAGS) $(BASEFLAGS) $(WARNFLAGS)
BOOT_CFLAGS += -std=c11 -I src/boot -fPIC -fshort-wchar

View File

@@ -2,10 +2,13 @@
#include "io.h"
const char digits[] = "0123456789abcdef";
console g_console;
console *console::default_console = nullptr;
console::console(const font &f, const screen &s, void *scratch, size_t len) :
class console_out_screen
{
public:
console_out_screen(const font &f, const screen &s, void *scratch, size_t len) :
m_font(f),
m_screen(s),
m_size(s.width() / f.width(), s.height() / f.height()),
@@ -16,7 +19,7 @@ console::console(const font &f, const screen &s, void *scratch, size_t len) :
m_data(nullptr),
m_attrs(nullptr),
m_palette(nullptr)
{
{
const unsigned count = m_size.size();
const size_t palette_size = sizeof(screen::pixel_t) * 256;
const size_t attrs_size = 2 * count;
@@ -76,40 +79,10 @@ console::console(const font &f, const screen &s, void *scratch, size_t len) :
}
repaint();
}
if (default_console == nullptr)
default_console = this;
}
static bool
serial_ready()
{
return (inb(COM1 + 5) & 0x20) != 0;
}
static void
serial_write(char c) {
while (!serial_ready());
outb(COM1, c);
}
char *
console::line_pointer(unsigned line)
{
if (!m_data) return nullptr;
return m_data + ((m_first + line) % m_size.y) * m_size.x;
}
uint16_t *
console::attr_pointer(unsigned line)
{
if (!m_attrs) return nullptr;
return m_attrs + ((m_first + line) % m_size.y) * m_size.x;
}
void
console::repaint()
{
void repaint()
{
m_screen.fill(m_bg);
if (!m_data) return;
@@ -131,21 +104,10 @@ console::repaint()
y * m_font.height());
}
}
}
}
void
console::set_color(uint8_t fg, uint8_t bg)
{
if (!m_palette) return;
m_bg = m_palette[bg];
m_fg = m_palette[fg];
m_attr = (bg << 8) | fg;
}
void
console::scroll(unsigned lines)
{
void scroll(unsigned lines)
{
if (!m_data) {
m_pos.x = 0;
m_pos.y = 0;
@@ -159,20 +121,22 @@ console::scroll(unsigned lines)
m_pos.y -= lines;
}
repaint();
}
}
size_t
console::puts(const char *message)
{
void set_color(uint8_t fg, uint8_t bg)
{
if (!m_palette) return;
m_bg = m_palette[bg];
m_fg = m_palette[fg];
m_attr = (bg << 8) | fg;
}
void putc(char c)
{
char *line = line_pointer(m_pos.y);
uint16_t *attrs = attr_pointer(m_pos.y);
size_t count = 0;
while (message && *message) {
const unsigned x = m_pos.x * m_font.width();
const unsigned y = m_pos.y * m_font.height();
const char c = *message++;
++count;
switch (c) {
case '\t':
@@ -184,17 +148,21 @@ console::puts(const char *message)
break;
case '\n':
serial_write('\r');
m_pos.x = 0;
m_pos.y++;
break;
default:
default: {
if (line) line[m_pos.x] = c;
if (attrs) attrs[m_pos.x] = m_attr;
const unsigned x = m_pos.x * m_font.width();
const unsigned y = m_pos.y * m_font.height();
m_font.draw_glyph(m_screen, c, m_fg, m_bg, x, y);
m_pos.x++;
}
}
if (m_pos.x >= m_size.x) {
m_pos.x = m_pos.x % m_size.x;
@@ -205,10 +173,77 @@ console::puts(const char *message)
scroll(1);
line = line_pointer(m_pos.y);
}
serial_write(c);
}
return count;
private:
char * line_pointer(unsigned line)
{
if (!m_data) return nullptr;
return m_data + ((m_first + line) % m_size.y) * m_size.x;
}
uint16_t * attr_pointer(unsigned line)
{
if (!m_attrs) return nullptr;
return m_attrs + ((m_first + line) % m_size.y) * m_size.x;
}
font m_font;
screen m_screen;
kutil::coord<unsigned> m_size;
kutil::coord<unsigned> m_pos;
screen::pixel_t m_fg, m_bg;
uint16_t m_attr;
size_t m_first;
size_t m_length;
char *m_data;
uint16_t *m_attrs;
screen::pixel_t *m_palette;
};
console_out_screen *
console_get_screen_out(const font &f, const screen &s, void *scratch, size_t len)
{
return nullptr;
//return new console_out_screen(f, s, scratch, len);
}
static bool
serial_ready()
{
return (inb(COM1 + 5) & 0x20) != 0;
}
static void
serial_write(char c) {
while (!serial_ready());
outb(COM1, c);
}
console::console() :
m_screen(nullptr)
{
}
void
console::set_color(uint8_t fg, uint8_t bg)
{
if (m_screen)
m_screen->set_color(fg, bg);
}
void
console::puts(const char *message)
{
while (message && *message) {
char c = *message++;
if (m_screen) m_screen->putc(c);
serial_write(c);
if (c == '\n') serial_write('\r');
}
}

View File

@@ -4,20 +4,17 @@
#include "font.h"
#include "screen.h"
struct console_data;
class console_out_screen;
class console
{
public:
console(const font &f, const screen &s, void *scratch, size_t len);
void repaint();
void scroll(unsigned lines);
console();
void set_color(uint8_t fg, uint8_t bg);
size_t puts(const char *message);
size_t printf(const char *fmt, ...);
void puts(const char *message);
void printf(const char *fmt, ...);
template <typename T>
void put_hex(T x);
@@ -25,30 +22,20 @@ public:
template <typename T>
void put_dec(T x);
static console * get() { return default_console; }
static console * get();
private:
char * line_pointer(unsigned line);
uint16_t * attr_pointer(unsigned line);
font m_font;
screen m_screen;
kutil::coord<unsigned> m_size;
kutil::coord<unsigned> m_pos;
screen::pixel_t m_fg, m_bg;
uint16_t m_attr;
size_t m_first;
size_t m_length;
char *m_data;
uint16_t *m_attrs;
screen::pixel_t *m_palette;
static console *default_console;
console_out_screen *m_screen;
};
extern console g_console;
inline console * console::get() { return &g_console; }
console_out_screen * console_get_screen_out(
const font &f, const screen &s, void *scratch, size_t len);
extern const char digits[];
template <typename T>

View File

@@ -14,6 +14,9 @@ extern "C" {
void kernel_main(popcorn_data *header);
}
inline void * operator new (size_t, void *p) throw() { return p; }
/*
console
load_console(const popcorn_data *header)
{
@@ -36,11 +39,12 @@ load_console(const popcorn_data *header)
return cons;
}
*/
void
kernel_main(popcorn_data *header)
{
console cons = load_console(header);
console *cons = new (&g_console) console();
memory_initialize_managers(
header->memory_map,
@@ -50,13 +54,13 @@ kernel_main(popcorn_data *header)
interrupts_init();
interrupts_enable();
cons.puts("Interrupts initialized.\n");
g_console.puts("Interrupts initialized.\n");
device_manager devices(header->acpi_table);
// int x = 1 / 0;
// __asm__ __volatile__("int $15");
cons.puts("boogity!");
g_console.puts("boogity!");
do_the_set_registers(header);
}

View File

@@ -0,0 +1,3 @@
extern "C" {
void __cxa_pure_virtual() { while(1); }
}