Add native rendering for high-resolution VGA text graphics
authorRobert Pengelly <robertapengelly@hotmail.com>
Sun, 25 Oct 2026 07:30:55 +0000 (07:30 +0000)
committerRobert Pengelly <robertapengelly@hotmail.com>
Sun, 25 Oct 2026 07:30:55 +0000 (07:30 +0000)
interrupts.c
video_native.h
video_native_linux.c
video_native_macos.c
video_native_other.c
video_native_text.c [new file with mode: 0644]
video_native_win32.c

index c5f4c3f3e3873f6c0bf0babc027e070b072d11fb..8b6b53ef818ad9874595969700aed9f9bb183204 100644 (file)
@@ -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;
index ce29af347b8243730bbec3506e7490c49a2826a0..c8bbe81391109170b47407421e3407006b5d7833 100644 (file)
@@ -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);
 
index f6024522441c754931bc1c2ee1515e6325abf3cb..0f48f952804a59d239179fc67dd334c49f18132f 100644 (file)
@@ -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;
 
 }
 
index 4412da10ca9b3b5eea2c33d55ca16d770b2f8042..7860360dfb3973cb3f6f884969db426edf216882 100644 (file)
@@ -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;
 
 }
 
index 92852005b9f8021b7ca6e04b4f98af62012549c9..fbcdd5d65e4e965944d73a9f36cdddc9d79ff85d 100644 (file)
@@ -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 (file)
index 0000000..b06e6a0
--- /dev/null
@@ -0,0 +1,68 @@
+/******************************************************************************
+ * @file            video_native_text.c
+ *****************************************************************************/
+#include    <stdint.h>
+
+#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;
+                
+                }
+            
+            }
+        
+        }
+    
+    }
+
+}
index 30d821c317a06a2af1e3909a7cab4361129b4c1e..883aa31dfc425816e1fc9d21cee9d3af0bcc71fc 100644 (file)
@@ -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;
 
 }