From 7065967be3380712a85a7325308ea137faaf40a0 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Tue, 4 Oct 2022 20:39:50 +0200 Subject: [PATCH] term: Reimplement context control --- common/drivers/vga_textmode.c | 19 ------- common/drivers/vga_textmode.h | 23 +++++++++ common/lib/term.c | 95 +++++++++++++++++++++++++++++++++-- 3 files changed, 114 insertions(+), 23 deletions(-) diff --git a/common/drivers/vga_textmode.c b/common/drivers/vga_textmode.c index e555254d..7a0df2b0 100644 --- a/common/drivers/vga_textmode.c +++ b/common/drivers/vga_textmode.c @@ -11,26 +11,7 @@ #include #include -struct textmode_context { - struct term_context term; - - volatile uint8_t *video_mem; - - uint8_t *back_buffer; - uint8_t *front_buffer; - - size_t cursor_offset; - size_t old_cursor_offset; - bool cursor_status; - uint8_t text_palette; - - uint8_t saved_state_text_palette; - size_t saved_state_cursor_offset; -}; - #define VIDEO_BOTTOM ((VD_ROWS * VD_COLS) - 1) -#define VD_COLS (80 * 2) -#define VD_ROWS 25 static void draw_cursor(struct textmode_context *ctx) { uint8_t pal = ctx->back_buffer[ctx->cursor_offset + 1]; diff --git a/common/drivers/vga_textmode.h b/common/drivers/vga_textmode.h index f9355d4a..577294be 100644 --- a/common/drivers/vga_textmode.h +++ b/common/drivers/vga_textmode.h @@ -3,7 +3,30 @@ #if defined (BIOS) +#include +#include #include +#include + +#define VD_COLS (80 * 2) +#define VD_ROWS 25 + +struct textmode_context { + struct term_context term; + + volatile uint8_t *video_mem; + + uint8_t *back_buffer; + uint8_t *front_buffer; + + size_t cursor_offset; + size_t old_cursor_offset; + bool cursor_status; + uint8_t text_palette; + + uint8_t saved_state_text_palette; + size_t saved_state_cursor_offset; +}; void vga_textmode_init(bool managed); diff --git a/common/lib/term.c b/common/lib/term.c index 8b0acef0..142e21ef 100644 --- a/common/lib/term.c +++ b/common/lib/term.c @@ -6,6 +6,8 @@ #include #include #include +#include +#include int current_video_mode = -1; int term_backend = _NOT_READY; @@ -263,19 +265,104 @@ extern void set_cursor_pos_helper(size_t x, size_t y); static uint8_t xfer_buf[TERM_XFER_CHUNK]; #endif +static uint64_t context_size(void) { + switch (term_backend) { + case TEXTMODE: + return sizeof(struct textmode_context) + (VD_ROWS * VD_COLS) * 2; + case GTERM: { + struct fbterm_context *ctx = (void *)term; + return sizeof(struct fbterm_context) + + ctx->font_bits_size + + ctx->font_bool_size + + ctx->canvas_size + + ctx->grid_size + + ctx->queue_size + + ctx->map_size; + } + default: + return 0; + } +} + +static void context_save(uint64_t buf) { + switch (term_backend) { + case TEXTMODE: { + struct textmode_context *ctx = (void *)term; + memcpy32to64(buf, (uintptr_t)ctx, sizeof(struct textmode_context)); + buf += sizeof(struct textmode_context); + memcpy32to64(buf, (uintptr_t)ctx->back_buffer, VD_ROWS * VD_COLS); + buf += VD_ROWS * VD_COLS; + memcpy32to64(buf, (uintptr_t)ctx->front_buffer, VD_ROWS * VD_COLS); + buf += VD_ROWS * VD_COLS; + break; + } + case GTERM: { + struct fbterm_context *ctx = (void *)term; + memcpy32to64(buf, (uintptr_t)ctx, sizeof(struct fbterm_context)); + buf += sizeof(struct fbterm_context); + memcpy32to64(buf, (uintptr_t)ctx->font_bits, ctx->font_bits_size); + buf += ctx->font_bits_size; + memcpy32to64(buf, (uintptr_t)ctx->font_bool, ctx->font_bool_size); + buf += ctx->font_bool_size; + memcpy32to64(buf, (uintptr_t)ctx->canvas, ctx->canvas_size); + buf += ctx->canvas_size; + memcpy32to64(buf, (uintptr_t)ctx->grid, ctx->grid_size); + buf += ctx->grid_size; + memcpy32to64(buf, (uintptr_t)ctx->queue, ctx->queue_size); + buf += ctx->queue_size; + memcpy32to64(buf, (uintptr_t)ctx->map, ctx->map_size); + buf += ctx->map_size; + break; + } + } +} + +static void context_restore(uint64_t buf) { + switch (term_backend) { + case TEXTMODE: { + struct textmode_context *ctx = (void *)term; + memcpy32to64((uintptr_t)ctx, buf, sizeof(struct textmode_context)); + buf += sizeof(struct textmode_context); + memcpy32to64((uintptr_t)ctx->back_buffer, buf, VD_ROWS * VD_COLS); + buf += VD_ROWS * VD_COLS; + memcpy32to64((uintptr_t)ctx->front_buffer, buf, VD_ROWS * VD_COLS); + buf += VD_ROWS * VD_COLS; + break; + } + case GTERM: { + struct fbterm_context *ctx = (void *)term; + memcpy32to64((uintptr_t)ctx, buf, sizeof(struct fbterm_context)); + buf += sizeof(struct fbterm_context); + memcpy32to64((uintptr_t)ctx->font_bits, buf, ctx->font_bits_size); + buf += ctx->font_bits_size; + memcpy32to64((uintptr_t)ctx->font_bool, buf, ctx->font_bool_size); + buf += ctx->font_bool_size; + memcpy32to64((uintptr_t)ctx->canvas, buf, ctx->canvas_size); + buf += ctx->canvas_size; + memcpy32to64((uintptr_t)ctx->grid, buf, ctx->grid_size); + buf += ctx->grid_size; + memcpy32to64((uintptr_t)ctx->queue, buf, ctx->queue_size); + buf += ctx->queue_size; + memcpy32to64((uintptr_t)ctx->map, buf, ctx->map_size); + buf += ctx->map_size; + break; + } + } +} + void _term_write(uint64_t buf, uint64_t count) { switch (count) { case TERM_CTX_SIZE: { - //uint64_t ret = context_size(); - //memcpy32to64(buf, (uint64_t)(uintptr_t)&ret, sizeof(uint64_t)); + uint64_t ret = context_size(); + memcpy32to64(buf, (uint64_t)(uintptr_t)&ret, sizeof(uint64_t)); return; } case TERM_CTX_SAVE: { - //context_save(buf); + context_save(buf); return; } case TERM_CTX_RESTORE: { - //context_restore(buf); + context_restore(buf); return; } case TERM_FULL_REFRESH: {