From a99951a7a0e9af934fe3b5f307a8243674346a81 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Thu, 29 Oct 2026 19:54:33 +0000 Subject: [PATCH] Implement CGA graphics video modes --- cpu.c | 13 ++++++-- cpu.h | 1 + interrupts.c | 65 +++++++++++++++++++++++++++++++------ video_native.h | 6 +++- video_native_linux.c | 13 +++++--- video_native_macos.c | 13 +++++--- video_native_text.c | 77 ++++++++++++++++++++++++++++++++++++++++++++ video_native_win32.c | 13 +++++--- 8 files changed, 177 insertions(+), 24 deletions(-) diff --git a/cpu.c b/cpu.c index ee23151..f0e09ec 100644 --- a/cpu.c +++ b/cpu.c @@ -2613,9 +2613,18 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) { break; - case 0x3D8: case 0x3D9: + case 0x3D8: /* CGA mode control */ - /* CGA mode control / colour select - not yet modelled */ + cpu->cga_mode_control = value; + + mem_video_dirty |= 2; + break; + + case 0x3D9: /* CGA colour select */ + + cpu->cga_color_select = value; + + mem_video_dirty |= 2; break; default: diff --git a/cpu.h b/cpu.h index f855b11..24ef41f 100644 --- a/cpu.h +++ b/cpu.h @@ -128,6 +128,7 @@ typedef struct __attribute__ ((packed)) CPU8086 { uint8_t crtc_index, crtc_registers[25]; uint16_t crtc_base; + uint8_t cga_mode_control, cga_color_select; uint8_t vga_misc_output, vga_enable, vga_graphics_mode; uint8_t vga_seq_index, vga_seq_registers[5]; uint8_t vga_gc_index, vga_gc_registers[9]; diff --git a/interrupts.c b/interrupts.c index f8afafd..240a0df 100644 --- a/interrupts.c +++ b/interrupts.c @@ -9779,13 +9779,25 @@ static uint32_t bios_graphics_sample (CPU8086 *cpu, unsigned col, unsigned line, unsigned weight = width * height; uint8_t index; + const uint8_t *rgb; + uint8_t cga_rgb[3]; if (bios_video_mode == 0x13) { index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; rgb = cpu->vga_dac_palette[index]; + } else if (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) { + + uint32_t color = video_native_cga_pixel (cpu, bios_video_mode == 0x06 ? x * 2 : x, y, bios_video_mode); + + cga_rgb[0] = (uint8_t) ((color >> 16) / 4); + cga_rgb[1] = (uint8_t) (((color >> 8) & 0xFF) / 4); + cga_rgb[2] = (uint8_t) ((color & 0xFF) / 4); + + rgb = cga_rgb; + } else { uint32_t address = 0xB8000 + ((y / 2) * 80 + (x / 4)) * 2; @@ -9837,7 +9849,7 @@ static void bios_graphics_refresh (CPU8086 *cpu) { struct timeval now; #endif - if (video_native_present (cpu, bios_video_mode != 0x13, bios_video_blink_enabled)) { + if (video_native_present (cpu, bios_video_mode, bios_video_blink_enabled)) { return; } @@ -9857,7 +9869,7 @@ static void bios_graphics_refresh (CPU8086 *cpu) { return; } - full_redraw = !bios_graphics_visible || columns != bios_graphics_columns || rows != bios_graphics_rows || bios_graphics_text_mode != (bios_video_mode != 0x13); + full_redraw = !bios_graphics_visible || columns != bios_graphics_columns || rows != bios_graphics_rows || bios_graphics_text_mode != bios_video_mode; 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; @@ -9886,7 +9898,7 @@ static void bios_graphics_refresh (CPU8086 *cpu) { bios_graphics_columns = columns; bios_graphics_rows = rows; - bios_graphics_text_mode = bios_video_mode != 0x13; + bios_graphics_text_mode = bios_video_mode; memcpy (bios_graphics_palette, cpu->vga_dac_palette, sizeof (bios_graphics_palette)); bios_graphics_dac_mask = cpu->vga_dac_mask; @@ -10386,11 +10398,17 @@ static void bios_windows_vt_render_region (unsigned top, unsigned left, unsigned #endif static unsigned bios_video_page_size (void) { + + if (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) { + return 0x4000; + } + return bios_video_columns == 40 ? 0x0800 : 0x1000; + } static unsigned bios_video_page_count (void) { - return bios_video_mode == 0x07 ? 1 : (bios_video_columns == 40 ? 8 : 4); + return bios_video_mode == 0x07 || (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) ? 1 : (bios_video_columns == 40 ? 8 : 4); } static uint32_t bios_video_page_base (unsigned page) { @@ -10522,7 +10540,7 @@ static void bios_video_update_bda (CPU8086 *cpu) { mem_write8 (cpu->mem, cpu->mem_size, 0x466, 0); mem_write8 (cpu->mem, cpu->mem_size, 0x484, (uint8_t) (bios_video_rows - 1)); - mem_write16 (cpu->mem, cpu->mem_size, 0x485, bios_video_mode == 0x13 ? 8 : 16); + mem_write16 (cpu->mem, cpu->mem_size, 0x485, (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) || bios_video_mode == 0x13 ? 8 : 16); cpu->vga_attr_registers[0x10] = (cpu->vga_attr_registers[0x10] & ~0x08) | (bios_video_blink_enabled ? 0x08 : 0); cpu->crtc_base = bios_video_mode == 7 ? 0x3B4 : 0x3D4; @@ -10581,14 +10599,14 @@ static void int_10 (CPU8086 *cpu) { unsigned row, col; - if (mode != 0x00 && mode != 0x01 && mode != 0x02 && mode != 0x03 && mode != 0x07 && mode != 0x13) { + if (mode != 0x00 && mode != 0x01 && mode != 0x02 && mode != 0x03 && mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x07 && mode != 0x13) { fprintf (stderr, "Unsupported Video Mode 0x%02X\n", mode); break; } - bios_video_columns = mode == 0x13 ? 40 : (mode <= 1 ? 40 : 80); + bios_video_columns = mode == 0x13 || mode == 0x04 || mode == 0x05 ? 40 : (mode <= 1 ? 40 : 80); bios_video_mode = mode; bios_video_page = 0; @@ -10635,6 +10653,24 @@ static void int_10 (CPU8086 *cpu) { } + if (mode >= 0x04 && mode <= 0x06) { + + cpu->cga_mode_control = mode == 0x06 ? 0x1A : 0x0A; + cpu->cga_color_select = mode == 0x06 ? 0x0F : 0x30; + + if (!(requested & 0x80)) { + memset (cpu->mem + 0xB8000, 0, 0x4000); + } + + bios_video_update_bda (cpu); + + mem_video_dirty |= 2; + bios_video_force_redraw = 1; + + break; + + } + if (!(requested & 0x80)) { for (row = 0; row < bios_video_rows; row++) { @@ -11107,7 +11143,18 @@ static void int_10 (CPU8086 *cpu) { } - /* BH=01h selects a CGA graphics palette; graphics modes are not supported. */ + if (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) { + + if (bh == 0) { + cpu->cga_color_select = (cpu->cga_color_select & 0xF0) | (get_reg8 (cpu, REG_BX) & 0x0F); + } else if (bh == 1) { + cpu->cga_color_select = (cpu->cga_color_select & ~0x20) | ((get_reg8 (cpu, REG_BX) & 1) << 5); + } + + mem_video_dirty |= 2; + + } + break; } @@ -11218,7 +11265,7 @@ void bios_video_refresh (CPU8086 *cpu) { int full_redraw = bios_video_force_redraw; uint32_t base; - if (bios_video_mode == 0x13 || (bios_video_mode == 0x03 && bios_video_columns == 80 && (cpu->crtc_registers[9] & 0x1F) == 3)) { + if (bios_video_mode == 0x13 || (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) || (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 c8bbe81..8e631fd 100644 --- a/video_native.h +++ b/video_native.h @@ -6,7 +6,11 @@ #include "cpu.h" -int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled); +int video_native_present (CPU8086 *cpu, int mode, int blink_enabled); + +uint32_t video_native_cga_pixel (CPU8086 *cpu, unsigned x, unsigned y, unsigned mode); +void video_native_cga_render (CPU8086 *cpu, uint32_t *pixels, unsigned mode); + void video_native_text_render (CPU8086 *cpu, uint32_t *pixels, int blink_enabled); int video_native_active (void); diff --git a/video_native_linux.c b/video_native_linux.c index 0f48f95..e7b21c3 100644 --- a/video_native_linux.c +++ b/video_native_linux.c @@ -1245,8 +1245,11 @@ static int video_initialize (void) { } -int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { +int video_native_present (CPU8086 *cpu, int mode, int blink_enabled) { + int text_mode = mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x13; + unsigned dirty_mask = mode == 0x13 ? 4u : 2u; + int palette_changed; uint64_t now; @@ -1279,7 +1282,7 @@ int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { palette_changed = memcmp (previous_palette, cpu->vga_dac_palette, sizeof (previous_palette)) || previous_mask != cpu->vga_dac_mask; - 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)) { + if (!(mem_video_dirty & dirty_mask) && (text_mode || !palette_changed) && video_last_text_mode == mode && (!text_mode || video_last_blink_enabled == blink_enabled) && (backend != 1 || !x11.redraw)) { return 1; } @@ -1293,6 +1296,8 @@ int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { if (text_mode) { video_native_text_render (cpu, video_framebuffer, blink_enabled); + } else if (mode != 0x13) { + video_native_cga_render (cpu, video_framebuffer, (unsigned) mode); } else { for (y = 0; y < 200; y++) { @@ -1324,9 +1329,9 @@ int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { memcpy (previous_palette, cpu->vga_dac_palette, sizeof (previous_palette)); previous_mask = cpu->vga_dac_mask; - mem_video_dirty &= ~(text_mode ? 2u : 4u); + mem_video_dirty &= ~dirty_mask; - video_last_text_mode = text_mode; + video_last_text_mode = mode; video_last_blink_enabled = blink_enabled; return 1; diff --git a/video_native_macos.c b/video_native_macos.c index 7860360..fc1e3cc 100644 --- a/video_native_macos.c +++ b/video_native_macos.c @@ -441,8 +441,11 @@ int video_native_poll (CPU8086 *cpu) { } -int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { +int video_native_present (CPU8086 *cpu, int mode, int blink_enabled) { + int text_mode = mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x13; + unsigned dirty_mask = mode == 0x13 ? 4u : 2u; + unsigned x, y; NSTimeInterval now; @@ -466,7 +469,7 @@ int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { 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 & (text_mode ? 2u : 4u)) && (text_mode || !palette_changed) && video_last_text_mode == text_mode && (!text_mode || video_last_blink_enabled == blink_enabled)) { + if (!(mem_video_dirty & dirty_mask) && (text_mode || !palette_changed) && video_last_text_mode == mode && (!text_mode || video_last_blink_enabled == blink_enabled)) { return 1; } @@ -480,6 +483,8 @@ int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { if (text_mode) { video_native_text_render (cpu, video_pixels, blink_enabled); + } else if (mode != 0x13) { + video_native_cga_render (cpu, video_pixels, (unsigned) mode); } else { for (y = 0; y < 200; y++) { @@ -506,9 +511,9 @@ int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { memcpy (video_palette, cpu->vga_dac_palette, sizeof (video_palette)); video_mask = cpu->vga_dac_mask; - mem_video_dirty &= ~(text_mode ? 2u : 4u); + mem_video_dirty &= ~dirty_mask; - video_last_text_mode = text_mode; + video_last_text_mode = mode; video_last_blink_enabled = blink_enabled; [video_view setNeedsDisplay: YES]; diff --git a/video_native_text.c b/video_native_text.c index b06e6a0..640055d 100644 --- a/video_native_text.c +++ b/video_native_text.c @@ -66,3 +66,80 @@ void video_native_text_render (CPU8086 *cpu, uint32_t *pixels, int blink_enabled } } + +uint32_t video_native_cga_pixel (CPU8086 *cpu, unsigned x, unsigned y, unsigned mode) { + + static const uint32_t colors[16] = { + + 0x000000, 0x0000AA, 0x00AA00, 0x00AAAA, + 0xAA0000, 0xAA00AA, 0xAA5500, 0xAAAAAA, + 0x555555, 0x5555FF, 0x55FF55, 0x55FFFF, + 0xFF5555, 0xFF55FF, 0xFFFF55, 0xFFFFFF + + }; + + uint32_t offset = 0xB8000 + (y & 1) * 0x2000 + (y >> 1) * 80; + + uint8_t color; + uint8_t value; + uint8_t pixel; + + if (mode == 0x06) { + + value = cpu->mem[offset + (x >> 3)]; + color = (value & (0x80 >> (x & 7))) ? (cpu->cga_color_select & 0x0F) : 0; + + } else { + + value = cpu->mem[offset + (x >> 2)]; + + pixel = (value >> (6 - (x & 3) * 2)) & 3; + color = pixel; + + if (pixel == 0) { + color = cpu->cga_color_select & 0x0F; + } else if (mode == 0x05) { + color = (uint8_t) ((const uint8_t[]) { 0, 3, 4, 7 })[color]; + } else if (cpu->cga_color_select & 0x20) { + color = (uint8_t) ((const uint8_t[]) { 0, 3, 5, 7 })[color]; + } else { + color = (uint8_t) (color * 2); + } + + if ((cpu->cga_color_select & 0x10) && pixel != 0) { + color |= 8; + } + + } + + return colors[color]; + +} + +void video_native_cga_render (CPU8086 *cpu, uint32_t *pixels, unsigned mode) { + + unsigned x, y; + + for (y = 0; y < 200; y++) { + + for (x = 0; x < (mode == 0x06 ? 640u : 320u); x++) { + + uint32_t color = video_native_cga_pixel (cpu, x, y, mode); + + unsigned output_x = mode == 0x06 ? x : x * 2; + unsigned width = mode == 0x06 ? 1 : 2; + + unsigned i; + + for (i = 0; i < width; i++) { + + pixels[y * 2 * 640 + output_x + i] = color; + pixels[(y * 2 + 1) * 640 + output_x + i] = color; + + } + + } + + } + +} diff --git a/video_native_win32.c b/video_native_win32.c index 883aa31..8be5706 100644 --- a/video_native_win32.c +++ b/video_native_win32.c @@ -179,7 +179,10 @@ int video_native_poll (CPU8086 *cpu) { } -int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { +int video_native_present (CPU8086 *cpu, int mode, int blink_enabled) { + + int text_mode = mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x13; + unsigned dirty_mask = mode == 0x13 ? 4u : 2u; unsigned x, y; DWORD now; @@ -204,7 +207,7 @@ int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { 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 & (text_mode ? 2u : 4u)) && (text_mode || !palette_changed) && video_last_text_mode == text_mode && (!text_mode || video_last_blink_enabled == blink_enabled)) { + if (!(mem_video_dirty & dirty_mask) && (text_mode || !palette_changed) && video_last_text_mode == mode && (!text_mode || video_last_blink_enabled == blink_enabled)) { return 1; } @@ -218,6 +221,8 @@ int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { if (text_mode) { video_native_text_render (cpu, video_pixels, blink_enabled); + } else if (mode != 0x13) { + video_native_cga_render (cpu, video_pixels, (unsigned) mode); } else { for (y = 0; y < 200; y++) { @@ -244,9 +249,9 @@ int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) { memcpy (video_palette, cpu->vga_dac_palette, sizeof (video_palette)); video_mask = cpu->vga_dac_mask; - mem_video_dirty &= ~(text_mode ? 2u : 4u); + mem_video_dirty &= ~dirty_mask; - video_last_text_mode = text_mode; + video_last_text_mode = mode; video_last_blink_enabled = blink_enabled; InvalidateRect (video_window, NULL, FALSE); -- 2.34.1