From 8c558d9442fbbc31a0241c2f29abcd05778f0b20 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Sat, 24 Oct 2026 18:08:54 +0100 Subject: [PATCH] Add VGA register handling, mode 13h and 32-bit ROR support --- cpu.c | 184 +++++++++++++++++++++++++++++++++++++++++++++++++-- cpu.h | 10 ++- interrupts.c | 35 ++++++++-- 3 files changed, 219 insertions(+), 10 deletions(-) diff --git a/cpu.c b/cpu.c index 73f9b1d..49467f1 100644 --- a/cpu.c +++ b/cpu.c @@ -1940,6 +1940,10 @@ static uint8_t io_read8 (CPU8086 *cpu, uint16_t port) { return 0xFF; + case 0x3B4: case 0x3D4: /* VGA CRT controller index */ + + return port == cpu->crtc_base ? cpu->crtc_index : 0xFF; + case 0x3B5: case 0x3D5: { /* 6845 CRT controller data */ uint8_t index = cpu->crtc_index; @@ -1948,19 +1952,84 @@ static uint8_t io_read8 (CPU8086 *cpu, uint16_t port) { return 0xFF; /* adapter not installed */ } - if (index >= 0x0E && index <= 0x11) { + if (index < 25) { return cpu->crtc_registers[index]; } - return 0x00; /* write-only / undefined register */ + return 0xFF; /* undefined register */ } - - case 0x3DA: /* CGA/EGA/VGA status */ + case 0x3BA: case 0x3DA: /* VGA input status 1 */ + + cpu->vga_attr_flipflop = 0; cpu->vga_status_flip++; + return (uint8_t) ((cpu->vga_status_flip & 0x01) | ((cpu->vga_status_flip & 0xE0) == 0xE0 ? 0x08 : 0x00)); + case 0x3C0: /* VGA attribute controller index */ + + return cpu->vga_attr_index; + + case 0x3C1: /* VGA attribute controller data */ + + return (cpu->vga_attr_index & 0x1F) < 21 ? cpu->vga_attr_registers[cpu->vga_attr_index & 0x1F] : 0xFF; + + case 0x3C2: /* VGA input status 0 */ + + return 0; + + case 0x3C3: /* VGA video subsystem enable */ + + return cpu->vga_enable; + + case 0x3C4: /* VGA sequencer index */ + + return cpu->vga_seq_index; + + case 0x3C5: /* VGA sequencer data */ + + return cpu->vga_seq_index < 5 ? cpu->vga_seq_registers[cpu->vga_seq_index] : 0xFF; + + case 0x3C6: /* VGA DAC pixel mask */ + + return cpu->vga_dac_mask; + + case 0x3C7: /* VGA DAC state */ + + return cpu->vga_dac_state; + + case 0x3C8: /* VGA DAC write index */ + + return cpu->vga_dac_write_index; + + case 0x3C9: { /* VGA DAC palette data */ + + uint8_t value = cpu->vga_dac_palette[cpu->vga_dac_read_index][cpu->vga_dac_read_component]; + + if (++cpu->vga_dac_read_component == 3) { + + cpu->vga_dac_read_component = 0; + cpu->vga_dac_read_index++; + + } + + return value; + + } + + case 0x3CC: /* VGA miscellaneous output */ + + return cpu->vga_misc_output; + + case 0x3CE: /* VGA graphics controller index */ + + return cpu->vga_gc_index; + + case 0x3CF: /* VGA graphics controller data */ + + return cpu->vga_gc_index < 9 ? cpu->vga_gc_registers[cpu->vga_gc_index] : 0xFF; + default: if (io_absent_sound_port (port)) { @@ -2226,7 +2295,7 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) { case 0x3B5: case 0x3D5: /* 6845 register data */ - if (port == (uint16_t) (cpu->crtc_base + 1) && cpu->crtc_index < 0x10) { + if (port == (uint16_t) (cpu->crtc_base + 1) && cpu->crtc_index < 25) { if (cpu->crtc_index == 0x0E) { value &= 0x3F; @@ -2238,6 +2307,91 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) { break; + case 0x3C0: /* VGA attribute controller */ + + if (cpu->vga_attr_flipflop) { + + if ((cpu->vga_attr_index & 0x1F) < 21) { + cpu->vga_attr_registers[cpu->vga_attr_index & 0x1F] = value; + } + + } else { + cpu->vga_attr_index = value; + } + + cpu->vga_attr_flipflop ^= 1; + break; + + case 0x3C2: /* VGA miscellaneous output */ + + cpu->vga_misc_output = value; + break; + + case 0x3C3: /* VGA video subsystem enable */ + + cpu->vga_enable = value & 1; + break; + + case 0x3C4: /* VGA sequencer index */ + + cpu->vga_seq_index = value & 7; + break; + + case 0x3C5: /* VGA sequencer data */ + + if (cpu->vga_seq_index < 5) { + cpu->vga_seq_registers[cpu->vga_seq_index] = value; + } + + break; + + case 0x3C6: /* VGA DAC pixel mask */ + + cpu->vga_dac_mask = value; + break; + + case 0x3C7: /* VGA DAC read index */ + + cpu->vga_dac_read_index = value; + cpu->vga_dac_read_component = 0; + cpu->vga_dac_state = 0; + + break; + + case 0x3C8: /* VGA DAC write index */ + + cpu->vga_dac_write_index = value; + cpu->vga_dac_write_component = 0; + cpu->vga_dac_state = 3; + + break; + + case 0x3C9: /* VGA DAC palette data */ + + cpu->vga_dac_palette[cpu->vga_dac_write_index][cpu->vga_dac_write_component] = value & 0x3F; + + if (++cpu->vga_dac_write_component == 3) { + + cpu->vga_dac_write_component = 0; + cpu->vga_dac_write_index++; + + } + + break; + + case 0x3CE: /* VGA graphics controller index */ + + cpu->vga_gc_index = value & 0x0F; + break; + + case 0x3CF: /* VGA graphics controller data */ + + if (cpu->vga_gc_index < 9) { + cpu->vga_gc_registers[cpu->vga_gc_index] = value; + } + + break; + case 0x3D8: case 0x3D9: /* CGA mode control / colour select - not yet modelled */ @@ -6060,6 +6214,26 @@ fetch_instruction: } } + + } else if (subop == 1) { /* ROR r/m32, imm8 */ + + result = (value >> count) | (value << (32 - count)); + + if (result & 0x80000000UL) { + cpu->flags |= FLAG_CF; + } else { + cpu->flags &= (uint16_t) ~FLAG_CF; + } + + if (count == 1) { + + if (((result >> 31) ^ (result >> 30)) & 1) { + cpu->flags |= FLAG_OF; + } else { + cpu->flags &= (uint16_t) ~FLAG_OF; + } + + } } else if (subop == 4) { /* SHL r/m32, imm8 */ diff --git a/cpu.h b/cpu.h index 60f1793..2386c9f 100644 --- a/cpu.h +++ b/cpu.h @@ -112,8 +112,16 @@ typedef struct __attribute__ ((packed)) CPU8086 { uint64_t pit_irq_last; uint8_t irq_poll_count; - uint8_t crtc_index, crtc_registers[18]; + uint8_t crtc_index, crtc_registers[25]; uint16_t crtc_base; + + uint8_t vga_misc_output, vga_enable; + 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]; + uint8_t vga_dac_mask, vga_dac_state, vga_dac_read_index, vga_dac_write_index; + uint8_t vga_dac_read_component, vga_dac_write_component; + uint8_t vga_dac_palette[256][3]; } CPU8086; diff --git a/interrupts.c b/interrupts.c index d561225..96aa01b 100644 --- a/interrupts.c +++ b/interrupts.c @@ -9911,7 +9911,7 @@ static void bios_video_update_bda (CPU8086 *cpu) { mem_write8 (cpu->mem, cpu->mem_size, 0x449, bios_video_mode); mem_write16 (cpu->mem, cpu->mem_size, 0x44A, bios_video_columns); - mem_write16 (cpu->mem, cpu->mem_size, 0x44C, bios_video_page_size ()); + mem_write16 (cpu->mem, cpu->mem_size, 0x44C, bios_video_mode == 0x13 ? 0xFA00 : bios_video_page_size ()); mem_write16 (cpu->mem, cpu->mem_size, 0x44E, bios_video_page * bios_video_page_size ()); for (page = 0; page < 8; page++) { @@ -9980,14 +9980,15 @@ static void int_10 (CPU8086 *cpu) { unsigned row, col; - if (mode != 0x00 && mode != 0x01 && mode != 0x02 && mode != 0x03 && mode != 0x07) { + if (mode != 0x00 && mode != 0x01 && mode != 0x02 && mode != 0x03 && mode != 0x07 && mode != 0x13) { fprintf (stderr, "Unsupported Video Mode 0x%02X\n", mode); break; } - bios_video_columns = mode <= 1 ? 40 : 80; + bios_video_columns = mode == 0x13 ? 40 : (mode <= 1 ? 40 : 80); + bios_video_mode = mode; bios_video_page = 0; @@ -9999,6 +10000,32 @@ static void int_10 (CPU8086 *cpu) { bios_video_initialize (); + if (mode == 0x13) { + + if (!(requested & 0x80)) { + memset (cpu->mem + 0xA0000, 0, 0x10000); + } + + 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_dac_mask = 0xFF; + cpu->vga_dac_state = 3; + cpu->vga_attr_flipflop = 0; + cpu->vga_enable = 1; + + bios_video_update_bda (cpu); + + fflush (stdout); + break; + + } + if (!(requested & 0x80)) { for (row = 0; row < bios_video_rows; row++) { @@ -10542,7 +10569,7 @@ void bios_video_refresh (CPU8086 *cpu) { mem_video_dirty = 0; bios_video_force_redraw = 0; - if (!(dirty & (bios_video_mode == 0x07 ? 1 : 2)) && !full_redraw) { + if (bios_video_mode == 0x13 || (!(dirty & (bios_video_mode == 0x07 ? 1 : 2)) && !full_redraw)) { return; } -- 2.34.1