From e945f2c4066690151bd4994de95dd00265cdda84 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Sun, 25 Oct 2026 07:30:55 +0000 Subject: [PATCH] Add native rendering for high-resolution VGA text graphics --- interrupts.c | 50 ++++++++++++++++++++++++++------ video_native.h | 4 ++- video_native_linux.c | 38 +++++++++++++++++-------- video_native_macos.c | 41 +++++++++++++++++--------- video_native_other.c | 5 +++- video_native_text.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ video_native_win32.c | 41 +++++++++++++++++--------- 7 files changed, 199 insertions(+), 48 deletions(-) create mode 100644 video_native_text.c diff --git a/interrupts.c b/interrupts.c index c5f4c3f..8b6b53e 100644 --- a/interrupts.c +++ b/interrupts.c @@ -9552,6 +9552,7 @@ static uint8_t bios_video_attrs[25][80]; static int bios_video_initialized; static int bios_graphics_visible; +static int bios_graphics_text_mode; static unsigned bios_graphics_columns, bios_graphics_rows; static uint64_t bios_graphics_last_refresh; @@ -9608,6 +9609,15 @@ static void bios_graphics_viewport (unsigned *columns, unsigned *rows) { static uint32_t bios_graphics_sample (CPU8086 *cpu, unsigned col, unsigned line, unsigned columns, unsigned lines) { + static const uint8_t vga16[16][3] = { + + { 0, 0, 0 }, { 0, 0, 42 }, { 0, 42, 0 }, { 0, 42, 42 }, + { 42, 0, 0 }, { 42, 0, 42 }, { 42, 21, 0 }, { 42, 42, 42 }, + { 21, 21, 21 }, { 21, 21, 63 }, { 21, 63, 21 }, { 21, 63, 63 }, + { 63, 21, 21 }, { 63, 21, 63 }, { 63, 63, 21 }, { 63, 63, 63 } + + }; + unsigned x0 = col * 320, x1 = (col + 1) * 320; unsigned y0 = line * 200, y1 = (line + 1) * 200; unsigned red = 0, green = 0, blue = 0; @@ -9624,8 +9634,30 @@ static uint32_t bios_graphics_sample (CPU8086 *cpu, unsigned col, unsigned line, unsigned width = (sx1 < x1 ? sx1 : x1) - (sx0 > x0 ? sx0 : x0); unsigned weight = width * height; - uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; - const uint8_t *rgb = cpu->vga_dac_palette[index]; + uint8_t index; + const uint8_t *rgb; + + if (bios_video_mode == 0x13) { + + index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; + rgb = cpu->vga_dac_palette[index]; + + } else { + + uint32_t address = 0xB8000 + ((y / 2) * 80 + (x / 4)) * 2; + + uint8_t ch = cpu->mem[address]; + uint8_t attr = cpu->mem[address + 1]; + uint8_t foreground = attr & 0x0F; + uint8_t background = (attr >> 4) & (bios_video_blink_enabled ? 0x07 : 0x0F); + + index = ch == 0xDE ? ((x & 2) ? foreground : background) : + ch == 0xDD ? ((x & 2) ? background : foreground) : + ch == 0xDB ? foreground : background; + + rgb = vga16[index]; + + } red += weight * rgb[0]; green += weight * rgb[1]; @@ -9661,7 +9693,7 @@ static void bios_graphics_refresh (CPU8086 *cpu) { struct timeval now; #endif - if (video_native_present (cpu)) { + if (video_native_present (cpu, bios_video_mode != 0x13, bios_video_blink_enabled)) { return; } @@ -9681,13 +9713,13 @@ static void bios_graphics_refresh (CPU8086 *cpu) { return; } - full_redraw = !bios_graphics_visible || columns != bios_graphics_columns || rows != bios_graphics_rows; + full_redraw = !bios_graphics_visible || columns != bios_graphics_columns || rows != bios_graphics_rows || bios_graphics_text_mode != (bios_video_mode != 0x13); - if (memcmp (bios_graphics_palette, cpu->vga_dac_palette, sizeof (bios_graphics_palette)) != 0 || bios_graphics_dac_mask != cpu->vga_dac_mask) { + if (bios_video_mode == 0x13 && (memcmp (bios_graphics_palette, cpu->vga_dac_palette, sizeof (bios_graphics_palette)) != 0 || bios_graphics_dac_mask != cpu->vga_dac_mask)) { full_redraw = 1; } - if (!(mem_video_dirty & 4) && !full_redraw) { + if (!(mem_video_dirty & (bios_video_mode == 0x13 ? 4u : 2u)) && !full_redraw && !bios_video_force_redraw) { return; } @@ -9710,11 +9742,13 @@ static void bios_graphics_refresh (CPU8086 *cpu) { bios_graphics_columns = columns; bios_graphics_rows = rows; + bios_graphics_text_mode = bios_video_mode != 0x13; memcpy (bios_graphics_palette, cpu->vga_dac_palette, sizeof (bios_graphics_palette)); bios_graphics_dac_mask = cpu->vga_dac_mask; - mem_video_dirty &= ~4u; + mem_video_dirty &= ~(bios_video_mode == 0x13 ? 4u : 2u); + bios_video_force_redraw = 0; #if defined (_WIN32) if (!vt_console) { @@ -11032,7 +11066,7 @@ void bios_video_refresh (CPU8086 *cpu) { int full_redraw = bios_video_force_redraw; uint32_t base; - if (bios_video_mode == 0x13) { + if (bios_video_mode == 0x13 || (bios_video_mode == 0x03 && bios_video_columns == 80 && (cpu->crtc_registers[9] & 0x1F) == 3)) { bios_graphics_refresh (cpu); return; diff --git a/video_native.h b/video_native.h index ce29af3..c8bbe81 100644 --- a/video_native.h +++ b/video_native.h @@ -6,7 +6,9 @@ #include "cpu.h" -int video_native_present (CPU8086 *cpu); +int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled); +void video_native_text_render (CPU8086 *cpu, uint32_t *pixels, int blink_enabled); + int video_native_active (void); int video_native_poll (CPU8086 *cpu); diff --git a/video_native_linux.c b/video_native_linux.c index f602452..0f48f95 100644 --- a/video_native_linux.c +++ b/video_native_linux.c @@ -180,6 +180,8 @@ static int backend; static int attempted; static int closed; +static int video_last_text_mode = -1; +static int video_last_blink_enabled = -1; static uint64_t last_refresh; static uint8_t previous_palette[256][3]; @@ -1243,7 +1245,7 @@ static int video_initialize (void) { } -int video_native_present (CPU8086 *cpu) { +int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { int palette_changed; @@ -1277,7 +1279,7 @@ int video_native_present (CPU8086 *cpu) { palette_changed = memcmp (previous_palette, cpu->vga_dac_palette, sizeof (previous_palette)) || previous_mask != cpu->vga_dac_mask; - if (!(mem_video_dirty & 4) && !palette_changed && (backend != 1 || !x11.redraw)) { + if (!(mem_video_dirty & (text_mode ? 2u : 4u)) && (text_mode || !palette_changed) && video_last_text_mode == text_mode && (!text_mode || video_last_blink_enabled == blink_enabled) && (backend != 1 || !x11.redraw)) { return 1; } @@ -1289,19 +1291,25 @@ int video_native_present (CPU8086 *cpu) { last_refresh = now; - for (y = 0; y < 200; y++) { + if (text_mode) { + video_native_text_render (cpu, video_framebuffer, blink_enabled); + } else { - for (x = 0; x < 320; x++) { + for (y = 0; y < 200; y++) { - uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; - const uint8_t *rgb = cpu->vga_dac_palette[index]; + for (x = 0; x < 320; x++) { - uint32_t color = ((uint32_t) (rgb[0] * 255 + 31) / 63 << 16) | ((uint32_t) (rgb[1] * 255 + 31) / 63 << 8) | (uint32_t) (rgb[2] * 255 + 31) / 63; + uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; + const uint8_t *rgb = cpu->vga_dac_palette[index]; + + uint32_t color = ((uint32_t) (rgb[0] * 255 + 31) / 63 << 16) | ((uint32_t) (rgb[1] * 255 + 31) / 63 << 8) | (uint32_t) (rgb[2] * 255 + 31) / 63; + + video_framebuffer[y * 2 * VIDEO_WIDTH + x * 2] = color; + video_framebuffer[y * 2 * VIDEO_WIDTH + x * 2 + 1] = color; + video_framebuffer[(y * 2 + 1) * VIDEO_WIDTH + x * 2] = color; + video_framebuffer[(y * 2 + 1) * VIDEO_WIDTH + x * 2 + 1] = color; - video_framebuffer[y * 2 * VIDEO_WIDTH + x * 2] = color; - video_framebuffer[y * 2 * VIDEO_WIDTH + x * 2 + 1] = color; - video_framebuffer[(y * 2 + 1) * VIDEO_WIDTH + x * 2] = color; - video_framebuffer[(y * 2 + 1) * VIDEO_WIDTH + x * 2 + 1] = color; + } } @@ -1316,7 +1324,10 @@ int video_native_present (CPU8086 *cpu) { memcpy (previous_palette, cpu->vga_dac_palette, sizeof (previous_palette)); previous_mask = cpu->vga_dac_mask; - mem_video_dirty &= ~4u; + mem_video_dirty &= ~(text_mode ? 2u : 4u); + + video_last_text_mode = text_mode; + video_last_blink_enabled = blink_enabled; return 1; @@ -1392,6 +1403,9 @@ void video_native_shutdown (void) { memset (previous_palette, 0, sizeof (previous_palette)); previous_mask = 0; + + video_last_text_mode = -1; + video_last_blink_enabled = -1; } diff --git a/video_native_macos.c b/video_native_macos.c index 4412da1..7860360 100644 --- a/video_native_macos.c +++ b/video_native_macos.c @@ -14,6 +14,9 @@ static NSWindow *video_window; static NSView *video_view; +static int video_last_text_mode = -1; +static int video_last_blink_enabled = -1; + static uint32_t video_pixels[640 * 400]; static uint8_t video_palette[256][3]; @@ -438,7 +441,7 @@ int video_native_poll (CPU8086 *cpu) { } -int video_native_present (CPU8086 *cpu) { +int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { unsigned x, y; NSTimeInterval now; @@ -463,7 +466,7 @@ int video_native_present (CPU8086 *cpu) { video_native_poll (cpu); palette_changed = memcmp (video_palette, cpu->vga_dac_palette, sizeof (video_palette)) || video_mask != cpu->vga_dac_mask; - if (!(mem_video_dirty & 4) && !palette_changed) { + if (!(mem_video_dirty & (text_mode ? 2u : 4u)) && (text_mode || !palette_changed) && video_last_text_mode == text_mode && (!text_mode || video_last_blink_enabled == blink_enabled)) { return 1; } @@ -475,20 +478,26 @@ int video_native_present (CPU8086 *cpu) { video_last_refresh = now; - for (y = 0; y < 200; y++) { + if (text_mode) { + video_native_text_render (cpu, video_pixels, blink_enabled); + } else { - for (x = 0; x < 320; x++) { + for (y = 0; y < 200; y++) { - uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; - const uint8_t *rgb = cpu->vga_dac_palette[index]; + for (x = 0; x < 320; x++) { - uint32_t color = ((uint32_t) (rgb[0] * 255 + 31) / 63 << 16) | - ((uint32_t) (rgb[1] * 255 + 31) / 63 << 8) | (uint32_t) (rgb[2] * 255 + 31) / 63; + uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; + const uint8_t *rgb = cpu->vga_dac_palette[index]; + + uint32_t color = ((uint32_t) (rgb[0] * 255 + 31) / 63 << 16) | + ((uint32_t) (rgb[1] * 255 + 31) / 63 << 8) | (uint32_t) (rgb[2] * 255 + 31) / 63; + + video_pixels[y * 2 * 640 + x * 2] = color; + video_pixels[y * 2 * 640 + x * 2 + 1] = color; + video_pixels[(y * 2 + 1) * 640 + x * 2] = color; + video_pixels[(y * 2 + 1) * 640 + x * 2 + 1] = color; - video_pixels[y * 2 * 640 + x * 2] = color; - video_pixels[y * 2 * 640 + x * 2 + 1] = color; - video_pixels[(y * 2 + 1) * 640 + x * 2] = color; - video_pixels[(y * 2 + 1) * 640 + x * 2 + 1] = color; + } } @@ -497,7 +506,10 @@ int video_native_present (CPU8086 *cpu) { memcpy (video_palette, cpu->vga_dac_palette, sizeof (video_palette)); video_mask = cpu->vga_dac_mask; - mem_video_dirty &= ~4u; + mem_video_dirty &= ~(text_mode ? 2u : 4u); + + video_last_text_mode = text_mode; + video_last_blink_enabled = blink_enabled; [video_view setNeedsDisplay: YES]; [video_view displayIfNeeded]; @@ -531,6 +543,9 @@ void video_native_shutdown (void) { memset (video_palette, 0, sizeof (video_palette)); video_mask = 0; + + video_last_text_mode = -1; + video_last_blink_enabled = -1; } diff --git a/video_native_other.c b/video_native_other.c index 9285200..fbcdd5d 100644 --- a/video_native_other.c +++ b/video_native_other.c @@ -4,9 +4,12 @@ #if !defined (__linux__) && !defined (_WIN32) && !defined (__APPLE__) || defined (__ANDROID__) #include "video_native.h" -int video_native_present (CPU8086 *cpu) { +int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { + (void) blink_enabled; + (void) text_mode; (void) cpu; + return 0; } diff --git a/video_native_text.c b/video_native_text.c new file mode 100644 index 0000000..b06e6a0 --- /dev/null +++ b/video_native_text.c @@ -0,0 +1,68 @@ +/****************************************************************************** + * @file video_native_text.c + *****************************************************************************/ +#include + +#include "video_native.h" + +void video_native_text_render (CPU8086 *cpu, uint32_t *pixels, int blink_enabled) { + + static const uint8_t vga16[16][3] = { + + { 0, 0, 0 }, { 0, 0, 42 }, { 0, 42, 0 }, { 0, 42, 42 }, + { 42, 0, 0 }, { 42, 0, 42 }, { 42, 21, 0 }, { 42, 42, 42 }, + { 21, 21, 21 }, { 21, 21, 63 }, { 21, 63, 21 }, { 21, 63, 63 }, + { 63, 21, 21 }, { 63, 21, 63 }, { 63, 63, 21 }, { 63, 63, 63 } + + }; + + uint32_t colors[16]; + unsigned row, col, x, y, i; + + for (i = 0; i < 16; i++) { + + colors[i] = ((uint32_t) (vga16[i][0] * 255 + 31) / 63 << 16) | + ((uint32_t) (vga16[i][1] * 255 + 31) / 63 << 8) | (uint32_t) (vga16[i][2] * 255 + 31) / 63; + + } + + for (row = 0; row < 100; row++) { + + for (col = 0; col < 80; col++) { + + unsigned address = 0xB8000 + (row * 80 + col) * 2; + + uint8_t ch = cpu->mem[address]; + uint8_t attr = cpu->mem[address + 1]; + uint8_t foreground = attr & 0x0F; + uint8_t background = (attr >> 4) & (blink_enabled ? 0x07 : 0x0F); + + uint32_t left = colors[background]; + uint32_t right = colors[background]; + + if (ch == 0xDE) { + right = colors[foreground]; + } else if (ch == 0xDD) { + left = colors[foreground]; + } else if (ch == 0xDB) { + left = right = colors[foreground]; + } + + for (y = 0; y < 4; y++) { + + uint32_t *line = pixels + (row * 4 + y) * 640 + col * 8; + + for (x = 0; x < 4; x++) { + + line[x] = left; + line[x + 4] = right; + + } + + } + + } + + } + +} diff --git a/video_native_win32.c b/video_native_win32.c index 30d821c..883aa31 100644 --- a/video_native_win32.c +++ b/video_native_win32.c @@ -23,6 +23,9 @@ static uint8_t video_palette[256][3]; static uint8_t video_mask; static uint8_t video_key_queue[128]; +static int video_last_text_mode = -1; +static int video_last_blink_enabled = -1; + static unsigned video_key_head, video_key_tail; static void video_key (UINT key, int pressed) { @@ -176,7 +179,7 @@ int video_native_poll (CPU8086 *cpu) { } -int video_native_present (CPU8086 *cpu) { +int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { unsigned x, y; DWORD now; @@ -201,7 +204,7 @@ int video_native_present (CPU8086 *cpu) { video_native_poll (cpu); palette_changed = memcmp (video_palette, cpu->vga_dac_palette, sizeof (video_palette)) || video_mask != cpu->vga_dac_mask; - if (!(mem_video_dirty & 4) && !palette_changed) { + if (!(mem_video_dirty & (text_mode ? 2u : 4u)) && (text_mode || !palette_changed) && video_last_text_mode == text_mode && (!text_mode || video_last_blink_enabled == blink_enabled)) { return 1; } @@ -213,20 +216,26 @@ int video_native_present (CPU8086 *cpu) { video_last_refresh = now; - for (y = 0; y < 200; y++) { + if (text_mode) { + video_native_text_render (cpu, video_pixels, blink_enabled); + } else { - for (x = 0; x < 320; x++) { + for (y = 0; y < 200; y++) { - uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; - const uint8_t *rgb = cpu->vga_dac_palette[index]; + for (x = 0; x < 320; x++) { - uint32_t color = ((uint32_t) (rgb[0] * 255 + 31) / 63 << 16) | - ((uint32_t) (rgb[1] * 255 + 31) / 63 << 8) | (uint32_t) (rgb[2] * 255 + 31) / 63; + uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; + const uint8_t *rgb = cpu->vga_dac_palette[index]; + + uint32_t color = ((uint32_t) (rgb[0] * 255 + 31) / 63 << 16) | + ((uint32_t) (rgb[1] * 255 + 31) / 63 << 8) | (uint32_t) (rgb[2] * 255 + 31) / 63; + + video_pixels[y * 2 * 640 + x * 2] = color; + video_pixels[y * 2 * 640 + x * 2 + 1] = color; + video_pixels[(y * 2 + 1) * 640 + x * 2] = color; + video_pixels[(y * 2 + 1) * 640 + x * 2 + 1] = color; - video_pixels[y * 2 * 640 + x * 2] = color; - video_pixels[y * 2 * 640 + x * 2 + 1] = color; - video_pixels[(y * 2 + 1) * 640 + x * 2] = color; - video_pixels[(y * 2 + 1) * 640 + x * 2 + 1] = color; + } } @@ -235,7 +244,10 @@ int video_native_present (CPU8086 *cpu) { memcpy (video_palette, cpu->vga_dac_palette, sizeof (video_palette)); video_mask = cpu->vga_dac_mask; - mem_video_dirty &= ~4u; + mem_video_dirty &= ~(text_mode ? 2u : 4u); + + video_last_text_mode = text_mode; + video_last_blink_enabled = blink_enabled; InvalidateRect (video_window, NULL, FALSE); UpdateWindow (video_window); @@ -261,6 +273,9 @@ void video_native_shutdown (void) { memset (video_palette, 0, sizeof (video_palette)); video_mask = 0; + + video_last_text_mode = -1; + video_last_blink_enabled = -1; } -- 2.34.1