From: Robert Pengelly Date: Sun, 25 Oct 2026 02:12:44 +0000 (+0000) Subject: Implement VGA planar memory X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=7833e50ea8346f33327a9c7ca8b1a719475c5180;p=dosemu.git Implement VGA planar memory --- diff --git a/cpu.c b/cpu.c index f455c0c..268737d 100644 --- 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 2386c9f..6d1f78e 100644 --- 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]; diff --git a/interrupts.c b/interrupts.c index a80c715..9544817 100644 --- a/interrupts.c +++ b/interrupts.c @@ -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; diff --git a/memory.c b/memory.c index 278ccd4..a8d962c 100644 --- a/memory.c +++ b/memory.c @@ -1,15 +1,144 @@ /****************************************************************************** * @file memory.c *****************************************************************************/ +#include + #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; diff --git a/memory.h b/memory.h index 1e812d6..46c3f64 100644 --- a/memory.h +++ b/memory.h @@ -12,6 +12,11 @@ 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);