mplement EGA video mode 0Dh and VGA write mode 2
authorRobert Pengelly <robertapengelly@hotmail.com>
Thu, 29 Oct 2026 22:17:51 +0000 (22:17 +0000)
committerRobert Pengelly <robertapengelly@hotmail.com>
Thu, 29 Oct 2026 22:17:51 +0000 (22:17 +0000)
interrupts.c
memory.c
memory.h
video_native.h
video_native_linux.c
video_native_macos.c
video_native_text.c
video_native_win32.c

index 1bc7cbc7c9c193d668fbfa0adb46e9b68f204533..072e2b7c2adf1b6b8fab1b140eb11c9a664c8645 100644 (file)
@@ -9889,7 +9889,17 @@ static uint32_t bios_graphics_sample (CPU8086 *cpu, unsigned col, unsigned line,
             const uint8_t *rgb;
             uint8_t cga_rgb[3];
             
-            if (bios_video_mode == 0x13) {
+            if (bios_video_mode == 0x0D) {
+            
+                uint8_t value = cpu->vga_attr_registers[mem_ega_pixel (cpu, x, y)] & 0x3F;
+                
+                cga_rgb[0] = (uint8_t) ((((value & 0x20) ? 2 : 0) + ((value & 4) ? 1 : 0)) * 21);
+                cga_rgb[1] = (uint8_t) ((((value & 0x10) ? 2 : 0) + ((value & 2) ? 1 : 0)) * 21);
+                cga_rgb[2] = (uint8_t) ((((value & 0x08) ? 2 : 0) + ((value & 1) ? 1 : 0)) * 21);
+                
+                rgb = cga_rgb;
+            
+            } else if (bios_video_mode == 0x13) {
             
                 index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask;
                 rgb = cpu->vga_dac_palette[index];
@@ -9981,7 +9991,7 @@ static void bios_graphics_refresh (CPU8086 *cpu) {
         full_redraw = 1;
     }
     
-    if (!(mem_video_dirty & (bios_video_mode == 0x13 ? 4u : 2u)) && !full_redraw && !bios_video_force_redraw) {
+    if (!(mem_video_dirty & (bios_video_mode == 0x13 || bios_video_mode == 0x0D ? 4u : 2u)) && !full_redraw && !bios_video_force_redraw) {
         return;
     }
     
@@ -10009,7 +10019,7 @@ static void bios_graphics_refresh (CPU8086 *cpu) {
     memcpy (bios_graphics_palette, cpu->vga_dac_palette, sizeof (bios_graphics_palette));
     bios_graphics_dac_mask = cpu->vga_dac_mask;
     
-    mem_video_dirty &= ~(bios_video_mode == 0x13 ? 4u : 2u);
+    mem_video_dirty &= ~(bios_video_mode == 0x13 || bios_video_mode == 0x0D ? 4u : 2u);
     bios_video_force_redraw = 0;
     
 #if     defined (_WIN32)
@@ -10509,16 +10519,20 @@ static unsigned bios_video_page_size (void) {
         return 0x4000;
     }
     
+    if (bios_video_mode == 0x0D) {
+        return 0x2000;
+    }
+    
     return bios_video_columns == 40 ? 0x0800 : 0x1000;
 
 }
 
 static unsigned bios_video_page_count (void) {
-    return bios_video_mode == 0x07 || (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) ? 1 : (bios_video_columns == 40 ? 8 : 4);
+    return bios_video_mode == 0x07 || (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) || bios_video_mode == 0x0D ? 1 : (bios_video_columns == 40 ? 8 : 4);
 }
 
 static uint32_t bios_video_page_base (unsigned page) {
-    return (bios_video_mode == 0x07 ? 0xB0000 : 0xB8000) + page * bios_video_page_size ();
+    return (bios_video_mode == 0x07 ? 0xB0000 : bios_video_mode == 0x0D ? 0xA0000 : 0xB8000) + page * bios_video_page_size ();
 }
 
 static uint32_t bios_video_memory_base (void) {
@@ -10646,7 +10660,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 >= 0x04 && bios_video_mode <= 0x06) || 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 == 0x0D || 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;
@@ -10730,6 +10744,13 @@ static void bios_cga_character (CPU8086 *cpu, unsigned row, unsigned col, uint8_
 
 }
 
+static void bios_ega_character (CPU8086 *cpu, unsigned row, unsigned col, uint8_t ch, uint8_t attr) {
+
+    const uint8_t *glyph = ch < 128 ? bios_cga_font[ch] : bios_cga_font[0];
+    mem_ega_character (cpu, row, col, glyph, attr);
+
+}
+
 static void bios_cga_scroll (CPU8086 *cpu, unsigned top, unsigned left, unsigned bottom, unsigned right, unsigned lines, uint8_t color, int down) {
 
     unsigned row, scan;
@@ -10773,7 +10794,7 @@ static int bios_graphics_console_char (CPU8086 *cpu, uint8_t al, uint8_t color)
 
     unsigned row, col;
     
-    if (bios_video_mode < 0x04 || bios_video_mode > 0x06) {
+    if ((bios_video_mode < 0x04 || bios_video_mode > 0x06) && bios_video_mode != 0x0D) {
         return 0;
     }
     
@@ -10792,7 +10813,11 @@ static int bios_graphics_console_char (CPU8086 *cpu, uint8_t al, uint8_t color)
     
     } else if (al >= 0x20) {
     
-        bios_cga_character (cpu, row, col, al, color);
+        if (bios_video_mode == 0x0D) {
+            bios_ega_character (cpu, row, col, al, color);
+        } else {
+            bios_cga_character (cpu, row, col, al, color);
+        }
         
         if (++col >= bios_video_columns) {
         
@@ -10805,7 +10830,12 @@ static int bios_graphics_console_char (CPU8086 *cpu, uint8_t al, uint8_t color)
     
     if (row >= 25) {
     
-        bios_cga_scroll (cpu, 0, 0, 24, bios_video_columns - 1, 1, 0, 0);
+        if (bios_video_mode == 0x0D) {
+            mem_ega_scroll (cpu, 0, 0, 24, bios_video_columns - 1, 1, 0, 0);
+        } else {
+            bios_cga_scroll (cpu, 0, 0, 24, bios_video_columns - 1, 1, 0, 0);
+        }
+        
         row = 24;
     
     }
@@ -10838,20 +10868,20 @@ static void int_10 (CPU8086 *cpu) {
             
             unsigned row, col;
             
-            if (mode != 0x00 && mode != 0x01 && mode != 0x02 && mode != 0x03 && mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x07 && mode != 0x13) {
+            if (mode != 0x00 && mode != 0x01 && mode != 0x02 && mode != 0x03 && mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x07 && mode != 0x0D && mode != 0x13) {
             
                 fprintf (stderr, "Unsupported Video Mode 0x%02X\n", mode);
                 break;
             
             }
             
-            bios_video_columns = mode == 0x13 || mode == 0x04 || mode == 0x05 ? 40 : (mode <= 1 ? 40 : 80);
+            bios_video_columns = mode == 0x13 || mode == 0x0D || mode == 0x04 || mode == 0x05 ? 40 : (mode <= 1 ? 40 : 80);
             
             bios_video_mode = mode;
             bios_video_page = 0;
             bios_video_blink_enabled = 1;
             
-            cpu->vga_graphics_mode = mode == 0x13;
+            cpu->vga_graphics_mode = mode == 0x13 || mode == 0x0D;
             
             bios_cursor_start = mode == 7 ? 0x0B : 0x06;
             bios_cursor_end = mode == 7 ? 0x0C : 0x07;
@@ -10861,6 +10891,43 @@ static void int_10 (CPU8086 *cpu) {
             
             bios_video_initialize ();
             
+            if (mode == 0x0D) {
+            
+                static const uint8_t palette[16] = { 0, 1, 2, 3, 4, 5, 0x14, 7, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F };
+                unsigned i;
+                
+                if (!(requested & 0x80)) {
+                    mem_vga_clear (cpu);
+                }
+                
+                cpu->vga_misc_output = 0x63;
+                cpu->vga_seq_registers[0] = 0x03;
+                cpu->vga_seq_registers[1] = 0x01;
+                cpu->vga_seq_registers[2] = 0x0F;
+                cpu->vga_seq_registers[3] = 0;
+                cpu->vga_seq_registers[4] = 0x06;
+                
+                memset (cpu->vga_gc_registers, 0, sizeof (cpu->vga_gc_registers));
+                
+                cpu->vga_gc_registers[6] = 0x05;
+                cpu->vga_gc_registers[8] = 0xFF;
+                
+                for (i = 0; i < 16; i++) {
+                    cpu->vga_attr_registers[i] = palette[i];
+                }
+                
+                cpu->vga_attr_flipflop = 0;
+                cpu->vga_enable = 1;
+                
+                bios_video_update_bda (cpu);
+                
+                mem_video_dirty |= 4;
+                bios_video_force_redraw = 1;
+                
+                break;
+            
+            }
+            
             if (mode == 0x13) {
             
                 if (!(requested & 0x80)) {
@@ -11124,6 +11191,13 @@ static void int_10 (CPU8086 *cpu) {
             
             }
             
+            if (bios_video_mode == 0x0D) {
+            
+                mem_ega_scroll (cpu, top, left, bottom, right, lines, attr, ah == 0x07);
+                break;
+            
+            }
+            
             bios_video_initialize ();
             
             height = (unsigned) bottom - top + 1;
@@ -11271,12 +11345,18 @@ static void int_10 (CPU8086 *cpu) {
             
             unsigned viewport_width, viewport_height;
             
-            if (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) {
+            if ((bios_video_mode >= 0x04 && bios_video_mode <= 0x06) || bios_video_mode == 0x0D) {
             
                 if (page == bios_video_page) {
                 
                     for (i = 0; i < count && (unsigned) bios_cursor_col[page] + i < bios_video_columns; i++) {
-                        bios_cga_character (cpu, bios_cursor_row[page], bios_cursor_col[page] + i, al, (uint8_t) cpu->regs[REG_BX]);
+                    
+                        if (bios_video_mode == 0x0D) {
+                            bios_ega_character (cpu, bios_cursor_row[page], bios_cursor_col[page] + i, al, (uint8_t) cpu->regs[REG_BX]);
+                        } else {
+                            bios_cga_character (cpu, bios_cursor_row[page], bios_cursor_col[page] + i, al, (uint8_t) cpu->regs[REG_BX]);
+                        }
+                    
                     }
                 
                 }
@@ -11597,7 +11677,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 >= 0x04 && bios_video_mode <= 0x06) || (bios_video_mode == 0x03 && bios_video_columns == 80 && (cpu->crtc_registers[9] & 0x1F) == 3)) {
+    if (bios_video_mode == 0x13 || bios_video_mode == 0x0D || (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;
index f6895f5d39ae96ef5fb9298b878594c505c79542..014552c7f22ee613e6e578f86de81777dcc326e7 100644 (file)
--- a/memory.c
+++ b/memory.c
@@ -40,6 +40,103 @@ uint8_t mem_vga_pixel (CPU8086 *cpu, unsigned x, unsigned y) {
 
 }
 
+uint8_t mem_ega_pixel (CPU8086 *cpu, unsigned x, unsigned y) {
+
+    uint32_t offset;
+    unsigned plane;
+    uint8_t mask, color = 0;
+    
+    if (cpu != vga_cpu || x >= 320 || y >= 200) {
+        return 0;
+    }
+    
+    offset = y * 40 + (x >> 3);
+    mask = (uint8_t) (0x80 >> (x & 7));
+    
+    for (plane = 0; plane < 4; plane++) {
+    
+        if (vga_planes[plane][offset] & mask) {
+            color |= (uint8_t) (1 << plane);
+        }
+    
+    }
+    
+    return color;
+
+}
+
+void mem_ega_character (CPU8086 *cpu, unsigned row, unsigned col, const uint8_t glyph[8], uint8_t color) {
+
+    unsigned y, plane;
+    
+    if (cpu != vga_cpu || row >= 25 || col >= 40) {
+        return;
+    }
+    
+    for (y = 0; y < 8; y++) {
+    
+        uint32_t offset = (row * 8 + y) * 40 + col;
+        
+        for (plane = 0; plane < 4; plane++) {
+        
+            uint8_t data = (color & (1 << plane)) ? glyph[y] : 0;
+            
+            if (color & 0x80) {
+                vga_planes[plane][offset] ^= data;
+            } else {
+                vga_planes[plane][offset] = data;
+            }
+        
+        }
+    
+    }
+    
+    mem_video_dirty |= 4;
+
+}
+
+void mem_ega_scroll (CPU8086 *cpu, unsigned top, unsigned left, unsigned bottom, unsigned right, unsigned lines, uint8_t color, int down) {
+
+    unsigned plane, row, scan;
+    unsigned height = bottom - top + 1;
+    unsigned width = right - left + 1;
+    
+    if (cpu != vga_cpu || bottom >= 25 || right >= 40) {
+        return;
+    }
+    
+    for (plane = 0; plane < 4; plane++) {
+    
+        uint8_t fill = color & (1 << plane) ? 0xFF : 0;
+        
+        for (row = 0; row < height; row++) {
+        
+            unsigned dest_row = down ? bottom - row : top + row;
+            int source_row = lines && row + lines < height ? (int) dest_row + (down ? -(int) lines : (int) lines) : -1;
+            
+            for (scan = 0; scan < 8; scan++) {
+            
+                uint32_t dest = (dest_row * 8 + scan) * 40 + left;
+                
+                if (source_row >= 0) {
+                
+                    uint32_t src = ((unsigned) source_row * 8 + scan) * 40 + left;
+                    memmove (vga_planes[plane] + dest, vga_planes[plane] + src, width);
+                
+                } else {
+                    memset (vga_planes[plane] + dest, fill, width);
+                }
+            
+            }
+        
+        }
+    
+    }
+    
+    mem_video_dirty |= 4;
+
+}
+
 static int mem_vga_address (const uint8_t *mem, uint32_t addr) {
     return vga_cpu && vga_cpu->vga_graphics_mode && mem == vga_cpu->mem && addr >= 0xA0000 && addr < 0xB0000;
 }
@@ -74,7 +171,9 @@ static void mem_vga_write (uint32_t addr, uint8_t value) {
     uint8_t bit_mask = vga_cpu->vga_gc_registers[8];
     uint8_t rotate = vga_cpu->vga_gc_registers[3] & 7;
     
+    uint8_t unrotated = value;
     unsigned plane;
+    
     value = (uint8_t) ((value >> rotate) | (value << ((8 - rotate) & 7)));
     
     if (vga_cpu->vga_seq_registers[4] & 0x08) {
@@ -96,7 +195,9 @@ static void mem_vga_write (uint32_t addr, uint8_t value) {
         
         }
         
-        if (vga_cpu->vga_gc_registers[1] & (1 << plane)) {
+        if ((vga_cpu->vga_gc_registers[5] & 3) == 2) {
+            data = (unrotated & (1 << plane)) ? 0xFF : 0;
+        } else if (vga_cpu->vga_gc_registers[1] & (1 << plane)) {
             data = (vga_cpu->vga_gc_registers[0] & (1 << plane)) ? 0xFF : 0;
         }
         
index 46c3f64420ab1ad5b1d66d853fbad2fd39ca8b71..d7257b07a40569eaf4fc96469be4429c5c3981b4 100644 (file)
--- a/memory.h
+++ b/memory.h
 extern unsigned mem_video_dirty;
 extern int watch_suppress;
 
+uint8_t mem_ega_pixel (CPU8086 *cpu, unsigned x, unsigned y);
+
+void mem_ega_character (CPU8086 *cpu, unsigned row, unsigned col, const uint8_t glyph[8], uint8_t color);
+void mem_ega_scroll (CPU8086 *cpu, unsigned top, unsigned left, unsigned bottom, unsigned right, unsigned lines, uint8_t color, int down);
+
 void mem_vga_attach (CPU8086 *cpu);
 void mem_vga_clear (CPU8086 *cpu);
 
index 8e631fd18b59fe95cc018afdd48eefe35fd3d1d0..3366453e3c5864ba8f2c52ac44ec970e3babe0bb 100644 (file)
@@ -8,9 +8,10 @@
 
 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_ega_render (CPU8086 *cpu, uint32_t *pixels);
 
+uint32_t video_native_cga_pixel (CPU8086 *cpu, unsigned x, unsigned y, unsigned mode);
 void video_native_text_render (CPU8086 *cpu, uint32_t *pixels, int blink_enabled);
 
 int video_native_active (void);
index e7b21c30d848dceacdecdcb589b8602aac5b7545..9a040ef38aa61d276a89a2c2e1dc6b8323876b60 100644 (file)
@@ -1247,8 +1247,8 @@ static int video_initialize (void) {
 
 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 text_mode = mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x0D && mode != 0x13;
+    unsigned dirty_mask = mode == 0x13 || mode == 0x0D ? 4u : 2u;
     
     int palette_changed;
     
@@ -1296,6 +1296,8 @@ int video_native_present (CPU8086 *cpu, int mode, int blink_enabled) {
     
     if (text_mode) {
         video_native_text_render (cpu, video_framebuffer, blink_enabled);
+    } else if (mode == 0x0D) {
+        video_native_ega_render (cpu, video_framebuffer);
     } else if (mode != 0x13) {
         video_native_cga_render (cpu, video_framebuffer, (unsigned) mode);
     } else {
index fc1e3cc81d7c3e210ba680ecd5be7ddc1154bf0a..6dc6cb67d545966a1ed8fb0a1cc0aa07a5e8cddd 100644 (file)
@@ -443,8 +443,8 @@ int video_native_poll (CPU8086 *cpu) {
 
 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 text_mode = mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x0D && mode != 0x13;
+    unsigned dirty_mask = mode == 0x13 || mode == 0x0D ? 4u : 2u;
     
     unsigned x, y;
     NSTimeInterval now;
@@ -483,6 +483,8 @@ int video_native_present (CPU8086 *cpu, int mode, int blink_enabled) {
     
     if (text_mode) {
         video_native_text_render (cpu, video_pixels, blink_enabled);
+    } else if (mode == 0x0D) {
+        video_native_ega_render (cpu, video_pixels);
     } else if (mode != 0x13) {
         video_native_cga_render (cpu, video_pixels, (unsigned) mode);
     } else {
index 640055d7e7e983cf73d9ef9588a9542de5aaa9d9..b5dd384ea9d86e92eaf3aad58c5a8f3fca26a202 100644 (file)
@@ -4,6 +4,7 @@
 #include    <stdint.h>
 
 #include    "video_native.h"
+#include    "memory.h"
 
 void video_native_text_render (CPU8086 *cpu, uint32_t *pixels, int blink_enabled) {
 
@@ -143,3 +144,31 @@ void video_native_cga_render (CPU8086 *cpu, uint32_t *pixels, unsigned mode) {
     }
 
 }
+
+void video_native_ega_render (CPU8086 *cpu, uint32_t *pixels) {
+
+    unsigned x, y;
+    
+    for (y = 0; y < 200; y++) {
+    
+        for (x = 0; x < 320; x++) {
+        
+            uint8_t index = mem_ega_pixel (cpu, x, y);
+            uint8_t value = cpu->vga_attr_registers[index] & 0x3F;
+            
+            unsigned red = ((value & 0x20) ? 2 : 0) + ((value & 0x04) ? 1 : 0);
+            unsigned green = ((value & 0x10) ? 2 : 0) + ((value & 0x02) ? 1 : 0);
+            unsigned blue = ((value & 0x08) ? 2 : 0) + ((value & 0x01) ? 1 : 0);
+            
+            uint32_t color = ((uint32_t) (red * 85) << 16) | (green * 85 << 8) | blue * 85;
+            
+            pixels[y * 2 * 640 + x * 2] = color;
+            pixels[y * 2 * 640 + x * 2 + 1] = color;
+            pixels[(y * 2 + 1) * 640 + x * 2] = color;
+            pixels[(y * 2 + 1) * 640 + x * 2 + 1] = color;
+        
+        }
+    
+    }
+
+}
index 8be5706285e6d19a7168f5f1d71ef6bfe72be294..67964fb1ff3ee526ea62725ba2dedfce57706b24 100644 (file)
@@ -181,8 +181,8 @@ int video_native_poll (CPU8086 *cpu) {
 
 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 text_mode = mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x0D && mode != 0x13;
+    unsigned dirty_mask = mode == 0x13 || mode == 0x0D ? 4u : 2u;
 
     unsigned x, y;
     DWORD now;
@@ -221,6 +221,8 @@ int video_native_present (CPU8086 *cpu, int mode, int blink_enabled) {
     
     if (text_mode) {
         video_native_text_render (cpu, video_pixels, blink_enabled);
+    } else if (mode == 0x0D) {
+        video_native_ega_render (cpu, video_pixels);
     } else if (mode != 0x13) {
         video_native_cga_render (cpu, video_pixels, (unsigned) mode);
     } else {