Implement VGA planar memory
authorRobert Pengelly <robertapengelly@hotmail.com>
Sun, 25 Oct 2026 02:12:44 +0000 (02:12 +0000)
committerRobert Pengelly <robertapengelly@hotmail.com>
Sun, 25 Oct 2026 02:12:44 +0000 (02:12 +0000)
cpu.c
cpu.h
interrupts.c
memory.c
memory.h

diff --git a/cpu.c b/cpu.c
index f455c0ca0b9f523257377ee21052ccf4a0f4e1d9..268737d933b07dcbc70cf8216b07bd1f9de17dcd 100644 (file)
--- a/cpu.c
+++ b/cpu.c
@@ -2867,8 +2867,9 @@ void cpu_reset (CPU8086 *cpu, uint8_t *mem, size_t mem_size) {
     cpu->gdtr_limit = 0xFFFF;
     cpu->idtr_limit = 0x03FF;
     
-    cpu->mem = mem;
     cpu->mem_size = mem_size;
+    cpu->mem = mem;
+    mem_vga_attach (cpu);
     
     cpu->pic[0].mask = 0xF8;
     cpu->pic[0].offset = 0x08;
diff --git a/cpu.h b/cpu.h
index 2386c9f9c827f90aacf52c684eb5176aad842933..6d1f78e6ed7b13ceb86d8f015cd1ee22e19318f3 100644 (file)
--- a/cpu.h
+++ b/cpu.h
@@ -115,7 +115,7 @@ typedef struct __attribute__ ((packed)) CPU8086 {
     uint8_t crtc_index, crtc_registers[25];
     uint16_t crtc_base;
     
-    uint8_t vga_misc_output, vga_enable;
+    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];
     uint8_t vga_attr_index, vga_attr_flipflop, vga_attr_registers[21];
index a80c71568e15784fd2f7ad3433ab428a60286576..9544817caa1be4bf87ecd135e0af541af53b1370 100644 (file)
@@ -9594,7 +9594,7 @@ static void bios_graphics_refresh (CPU8086 *cpu) {
                 unsigned x = (col * 320 + 160) / columns;
                 unsigned y = (row * 200 + 100) / rows;
                 
-                uint8_t index = cpu->mem[0xA0000 + y * 320 + x] & cpu->vga_dac_mask;
+                uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask;
                 const uint8_t *rgb = cpu->vga_dac_palette[index];
                 
                 unsigned best = 0, best_distance = ~0u;
@@ -9677,8 +9677,8 @@ static void bios_graphics_refresh (CPU8086 *cpu) {
                 y_bottom = 199;
             }
             
-            top = cpu->mem[0xA0000 + y_top * 320 + x] & cpu->vga_dac_mask;
-            bottom = cpu->mem[0xA0000 + y_bottom * 320 + x] & cpu->vga_dac_mask;
+            top = mem_vga_pixel (cpu, x, y_top) & cpu->vga_dac_mask;
+            bottom = mem_vga_pixel (cpu, x, y_bottom) & cpu->vga_dac_mask;
             
             foreground = cpu->vga_dac_palette[top];
             background = cpu->vga_dac_palette[bottom];
@@ -10241,6 +10241,8 @@ static void int_10 (CPU8086 *cpu) {
             bios_video_mode = mode;
             bios_video_page = 0;
             
+            cpu->vga_graphics_mode = mode == 0x13;
+            
             bios_cursor_start = mode == 7 ? 0x0B : 0x06;
             bios_cursor_end = mode == 7 ? 0x0C : 0x07;
             
@@ -10252,17 +10254,21 @@ static void int_10 (CPU8086 *cpu) {
             if (mode == 0x13) {
             
                 if (!(requested & 0x80)) {
-                    memset (cpu->mem + 0xA0000, 0, 0x10000);
+                    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] = 0x00;
                 cpu->vga_seq_registers[4] = 0x0E;
+                
                 cpu->vga_gc_registers[5] = 0x40;
                 cpu->vga_gc_registers[6] = 0x05;
+                cpu->vga_gc_registers[8] = 0xFF;
+                
                 cpu->vga_dac_mask = 0xFF;
                 cpu->vga_dac_state = 3;
                 cpu->vga_attr_flipflop = 0;
index 278ccd44b7c78eb634b4cc44a08cac3a12034f10..a8d962c66ee2ed6a653dfdfa4e9bdde6bbabf8d6 100644 (file)
--- a/memory.c
+++ b/memory.c
 /******************************************************************************
  * @file            memory.c
  *****************************************************************************/
+#include    <string.h>
+
 #include    "memory.h"
 #include    "cpu.h"
 
+static CPU8086 *vga_cpu;
+
+static uint8_t vga_planes[4][65536];
+static uint8_t vga_latches[4];
+
+void mem_vga_attach (CPU8086 *cpu) {
+
+    vga_cpu = cpu;
+    
+    memset (vga_planes, 0, sizeof (vga_planes));
+    memset (vga_latches, 0, sizeof (vga_latches));
+
+}
+
+void mem_vga_clear (CPU8086 *cpu) {
+
+    memset (vga_planes, 0, sizeof (vga_planes));
+    memset (vga_latches, 0, sizeof (vga_latches));
+    
+    memset (cpu->mem + 0xA0000, 0, 0x10000);
+    mem_video_dirty |= 4;
+
+}
+
+uint8_t mem_vga_pixel (CPU8086 *cpu, unsigned x, unsigned y) {
+
+    if (cpu != vga_cpu || x >= 320 || y >= 200) {
+        return 0;
+    }
+
+    return vga_planes[x & 3][y * 80 + (x >> 2)];
+
+}
+
+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;
+}
+
+static uint32_t mem_vga_offset (uint32_t addr) {
+
+    uint32_t offset = addr - 0xA0000;
+    
+    if (vga_cpu->vga_seq_registers[4] & 0x08) {
+        return offset >> 2;
+    }
+    
+    return offset;
+
+}
+
+static unsigned mem_vga_read_plane (uint32_t addr) {
+
+    if (vga_cpu->vga_seq_registers[4] & 0x08) {
+        return (addr - 0xA0000) & 3;
+    }
+    
+    return vga_cpu->vga_gc_registers[4] & 3;
+
+}
+
+static void mem_vga_write (uint32_t addr, uint8_t value) {
+
+    uint32_t offset = mem_vga_offset (addr);
+    
+    uint8_t map_mask = vga_cpu->vga_seq_registers[2];
+    uint8_t bit_mask = vga_cpu->vga_gc_registers[8];
+    uint8_t rotate = vga_cpu->vga_gc_registers[3] & 7;
+    
+    unsigned plane;
+    value = (uint8_t) ((value >> rotate) | (value << ((8 - rotate) & 7)));
+    
+    if (vga_cpu->vga_seq_registers[4] & 0x08) {
+        map_mask &= (uint8_t) (1 << ((addr - 0xA0000) & 3));
+    }
+    
+    for (plane = 0; plane < 4; plane++) {
+    
+        uint8_t data = value;
+        
+        if (!(map_mask & (1 << plane))) {
+            continue;
+        }
+        
+        if (vga_cpu->vga_gc_registers[1] & (1 << plane)) {
+            data = (vga_cpu->vga_gc_registers[0] & (1 << plane)) ? 0xFF : 0;
+        }
+        
+        switch (vga_cpu->vga_gc_registers[3] & 0x18) {
+        
+            case 0x08:
+            
+                data &= vga_latches[plane];
+                break;
+            
+            case 0x10:
+            
+                data |= vga_latches[plane];
+                break;
+            
+            case 0x18:
+            
+                data ^= vga_latches[plane];
+                break;
+        
+        }
+        
+        vga_planes[plane][offset] = (uint8_t) ((data & bit_mask) | (vga_latches[plane] & ~bit_mask));
+    
+    }
+    
+    mem_video_dirty |= 4;
+
+}
+
 uint8_t mem_read8 (const uint8_t *mem, size_t mem_size, uint32_t addr) {
 
     if (addr >= mem_size) {
         return 0;
     }
     
+    if (mem_vga_address (mem, addr)) {
+    
+        uint32_t offset = mem_vga_offset (addr);
+        unsigned plane;
+        
+        for (plane = 0; plane < 4; plane++) {
+            vga_latches[plane] = vga_planes[plane][offset];
+        }
+        
+        return vga_latches[mem_vga_read_plane (addr)];
+    
+    }
+    
     return mem[addr];
 
 }
@@ -30,7 +159,11 @@ void mem_write8 (uint8_t *mem, size_t mem_size, uint32_t addr, uint8_t value) {
     
     if ((size_t) addr < mem_size) {
     
-        mem[addr] = value;
+        if (mem_vga_address (mem, addr)) {
+            mem_vga_write (addr, value);
+        } else {
+            mem[addr] = value;
+        }
         
         if (addr >= 0xA0000 && addr < 0xB0000) {
             mem_video_dirty |= 4;
index 1e812d69f0d2e94a97532b2efe72ebe095c59906..46c3f64420ab1ad5b1d66d853fbad2fd39ca8b71 100644 (file)
--- a/memory.h
+++ b/memory.h
 extern unsigned mem_video_dirty;
 extern int watch_suppress;
 
+void mem_vga_attach (CPU8086 *cpu);
+void mem_vga_clear (CPU8086 *cpu);
+
+uint8_t mem_vga_pixel (CPU8086 *cpu, unsigned x, unsigned y);
+
 uint8_t mem_read8 (const uint8_t *mem, size_t mem_size, uint32_t addr);
 uint16_t mem_read16 (uint8_t *mem, size_t mem_size, uint32_t addr);