From ede0b79a19161f93f220b51edfa8e05b052c412b Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Sun, 25 Oct 2026 03:05:05 +0000 Subject: [PATCH] Implement IRQ1 scan-code delivery for DOS games --- cpu.c | 57 +++++++++++++++++++++++++----- cpu.h | 3 ++ interrupts.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ interrupts.h | 1 + main.c | 4 +++ 5 files changed, 154 insertions(+), 8 deletions(-) diff --git a/cpu.c b/cpu.c index 268737d..0bbf31b 100644 --- a/cpu.c +++ b/cpu.c @@ -1510,19 +1510,48 @@ static int cpu_service_timer_irq (CPU8086 *cpu) { } - if (!(cpu->flags & FLAG_IF) || (cpu->pic[0].mask & 1) || (cpu->pic[0].isr & 1) || !(cpu->pic[0].irr & 1)) { + if (cpu->kbd_release_scancode && !cpu->kbd_scancode_pending && pit_clock_ns () >= cpu->kbd_release_time) { + + cpu->kbd_scancode = cpu->kbd_release_scancode; + cpu->kbd_scancode_pending = 1; + cpu->kbd_release_scancode = 0; + cpu->pic[0].irr |= 2; + + } + + if (!(cpu->flags & FLAG_IF)) { return 0; } - cpu->pic[0].irr &= (uint8_t) ~1; - cpu->pic[0].isr |= 1; + if (!(cpu->pic[0].mask & 1) && !(cpu->pic[0].isr & 1) && (cpu->pic[0].irr & 1)) { + + cpu->pic[0].irr &= (uint8_t) ~1; + cpu->pic[0].isr |= 1; + + if (cpu->cr0 & 1) { + return cpu_protected_interrupt (cpu, cpu->pic[0].offset) == 0 ? 1 : -1; + } + + handle_interrupt (cpu, cpu->pic[0].offset); + return 1; - if (cpu->cr0 & 1) { - return cpu_protected_interrupt (cpu, cpu->pic[0].offset) == 0 ? 1 : -1; } - handle_interrupt (cpu, cpu->pic[0].offset); - return 1; + if (!(cpu->pic[0].mask & 2) && !(cpu->pic[0].isr & 3) && (cpu->pic[0].irr & 2)) { + + cpu->pic[0].irr &= (uint8_t) ~2; + cpu->pic[0].isr |= 2; + + if (cpu->cr0 & 1) { + return cpu_protected_interrupt (cpu, (uint8_t) (cpu->pic[0].offset + 1)) == 0 ? 1 : -1; + } + + handle_interrupt (cpu, (uint8_t) (cpu->pic[0].offset + 1)); + return 1; + + } + + return 0; } @@ -1868,6 +1897,18 @@ static uint8_t io_read8 (CPU8086 *cpu, uint16_t port) { } + if (cpu->kbd_scancode_pending) { + + cpu->kbd_scancode_pending = 0; + + if (cpu->kbd_release_scancode) { + cpu->kbd_release_time = pit_clock_ns () + 150000000ULL; + } + + return cpu->kbd_scancode; + + } + return 0x00; /* no scan code pending */ case 0x61: /* keyboard controller / speaker control */ @@ -1876,7 +1917,7 @@ static uint8_t io_read8 (CPU8086 *cpu, uint16_t port) { case 0x64: /* keyboard status port */ - return cpu->kbd_ack_pending || cpu->kbd_output_port_read_pending ? 0x01 : 0x00; + return cpu->kbd_ack_pending || cpu->kbd_output_port_read_pending || cpu->kbd_scancode_pending ? 0x01 : 0x00; case 0x71: /* CMOS/RTC data register */ diff --git a/cpu.h b/cpu.h index 6d1f78e..fee5689 100644 --- a/cpu.h +++ b/cpu.h @@ -94,6 +94,9 @@ typedef struct __attribute__ ((packed)) CPU8086 { uint8_t kbd_typematic_pending, kbd_typematic_rate, kbd_ack_pending; uint8_t kbd_output_port, kbd_output_port_pending, kbd_output_port_read_pending; + uint8_t kbd_scancode, kbd_scancode_pending, kbd_release_scancode; + uint64_t kbd_release_time; + uint8_t cmos_index, cmos_regs[128]; uint8_t port61, port92, a20_enabled, vga_status_flip; diff --git a/interrupts.c b/interrupts.c index 676bc29..d7345ab 100644 --- a/interrupts.c +++ b/interrupts.c @@ -2267,6 +2267,103 @@ static char dos_current_dir[260] = ""; /* Extended BPB information for the emulated C: volume. */ static uint32_t dos_volume_serial = 0x80860886; +static uint8_t bios_keyboard_scan (int key) { + + static const uint8_t letters[26] = { + + 0x1E, 0x30, 0x2E, 0x20, 0x12, 0x21, 0x22, 0x23, 0x17, + 0x24, 0x25, 0x26, 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, + 0x1F, 0x14, 0x16, 0x2F, 0x11, 0x2D, 0x15, 0x2C + + }; + + static const uint8_t digits[10] = { + + 0x0B, 0x02, 0x03, 0x04, 0x05, + 0x06, 0x07, 0x08, 0x09, 0x0A + + }; + + unsigned ch = (unsigned) key & 0xFF; + unsigned scan = ((unsigned) key >> 8) & 0xFF; + + if (scan) { + return (uint8_t) scan; + } + + if (ch >= 'A' && ch <= 'Z') { + ch += 'a' - 'A'; + } + + if (ch >= 'a' && ch <= 'z') { + return letters[ch - 'a']; + } + + if (ch >= '0' && ch <= '9') { + return digits[ch - '0']; + } + + switch (ch) { + + case 0x1B: return 0x01; /* Escape */ + case 0x08: return 0x0E; /* Backspace */ + case 0x09: return 0x0F; /* Tab */ + case 0x0A: case 0x0D: return 0x1C; /* Enter */ + case ' ': return 0x39; + case '-': case '_': return 0x0C; + case '=': case '+': return 0x0D; + case '[': case '{': return 0x1A; + case ']': case '}': return 0x1B; + case ';': case ':': return 0x27; + case '\'': case '"': return 0x28; + case '`': case '~': return 0x29; + case '\\': case '|': return 0x2B; + case ',': case '<': return 0x33; + case '.': case '>': return 0x34; + case '/': case '?': return 0x35; + + } + + return 0; + +} + +void bios_keyboard_poll (CPU8086 *cpu) { + + int key; + uint8_t scan; + + /* Ordinary DOS and BIOS keyboard reads still use the existing input path. */ + if (!cpu->vga_graphics_mode || cpu->kbd_scancode_pending || cpu->kbd_release_scancode) { + return; + } + + /* Leave real-mode BIOS keyboard input alone unless IRQ1 is hooked. */ + if (!(cpu->cr0 & 1) && mem_read16 (cpu->mem, cpu->mem_size, 0x26) == DOS_IVT_STUB_SEG && + mem_read16 (cpu->mem, cpu->mem_size, 0x24) == 0x03FC) { + return; + } + + key = bios_read_key (cpu, 0); + + if (key < 0) { + return; + } + + scan = bios_keyboard_scan (key); + + if (!scan) { + return; + } + + cpu->kbd_scancode = scan; + cpu->kbd_scancode_pending = 1; + cpu->kbd_release_scancode = (uint8_t) (scan | 0x80); + cpu->kbd_release_time = 0; + cpu->pic[0].irr |= 2; + +} + static uint8_t dos_volume_label[11] = { 'E', 'M', 'U', '8', '0', '8', '6', ' ', ' ', ' ', ' ' }; static uint8_t dos_volume_fs[8] = { 'F', 'A', 'T', '1', '6', ' ', ' ', ' ' }; diff --git a/interrupts.h b/interrupts.h index 929d661..5b47301 100644 --- a/interrupts.h +++ b/interrupts.h @@ -15,6 +15,7 @@ void native_interrupt (CPU8086 *cpu, uint8_t interrupt_number); const char *dos_register_initial_program (const char *path); void bios_timer_refresh (CPU8086 *cpu); +void bios_keyboard_poll (CPU8086 *cpu); void bios_video_refresh (CPU8086 *cpu); #if defined (_WIN32) diff --git a/main.c b/main.c index 25a6524..ea43dbe 100644 --- a/main.c +++ b/main.c @@ -553,6 +553,10 @@ int main (int argc, char **argv) { bios_timer_refresh (&cpu); } + if ((steps & 0x3FFF) == 0) { + bios_keyboard_poll (&cpu); + } + rc = cpu_step (&cpu); #if defined (_WIN32) -- 2.34.1