From e47c37699380d5653e1e3aa83412dc346e690029 Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Thu, 29 Oct 2026 20:08:32 +0000 Subject: [PATCH] Fix BIOS keyboard input in native graphics mode --- interrupts.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++--- main.c | 3 ++ 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/interrupts.c b/interrupts.c index 240a0df..5ff82c9 100644 --- a/interrupts.c +++ b/interrupts.c @@ -2029,6 +2029,68 @@ static int host_read_key (CPU8086 *cpu) { } +static void int_09 (CPU8086 *cpu); + +static int bios_read_native_key (CPU8086 *cpu, int wait) { + + for (;;) { + + uint16_t start = mem_read16 (cpu->mem, cpu->mem_size, 0x0480); + uint16_t end = mem_read16 (cpu->mem, cpu->mem_size, 0x0482); + uint16_t head = mem_read16 (cpu->mem, cpu->mem_size, 0x041A); + uint16_t tail = mem_read16 (cpu->mem, cpu->mem_size, 0x041C); + + if (start >= 0x001E && end <= 0x0100 && end > start && !((start | end | head | tail) & 1) && head >= start && head < end && tail >= start && tail < end && head != tail) { + + uint16_t key = mem_read16 (cpu->mem, cpu->mem_size, 0x0400 + head); + + if (wait) { + + head += 2; + + if (head >= end) { + head = start; + } + + mem_write16 (cpu->mem, cpu->mem_size, 0x041A, head); + + } + + return key; + + } + + if (!wait) { + return -1; + } + + if (video_native_poll (cpu) < 0) { + return -1; + } + + /* The BIOS wait loop must service its default keyboard handler. */ + if (cpu->kbd_scancode_pending && mem_read16 (cpu->mem, cpu->mem_size, 0x0024) == 0x0024 && mem_read16 (cpu->mem, cpu->mem_size, 0x0026) == DOS_IVT_STUB_SEG) { + + cpu->pic[0].irr &= (uint8_t) ~2; + int_09 (cpu); + + } + +#if defined (_WIN32) + Sleep (1); +#else + { + + struct timeval delay = { 0, 1000 }; + select (0, NULL, NULL, NULL, &delay); + + } +#endif + + } + +} + /* BIOS keyboard input returns ASCII in AL and the scan code in AH. */ static int bios_key_pending = -1; @@ -2036,6 +2098,14 @@ static int bios_read_key (CPU8086 *cpu, int wait) { int ch; + if (wait) { + bios_video_refresh (cpu); + } + + if (video_native_active ()) { + return bios_read_native_key (cpu, wait); + } + if (bios_key_pending >= 0) { ch = bios_key_pending; @@ -2045,10 +2115,6 @@ static int bios_read_key (CPU8086 *cpu, int wait) { } - if (wait) { - bios_video_refresh (cpu); - } - #if defined (_WIN32) ch = host_windows_console_key (stdout, wait); mem_write8 (cpu->mem, cpu->mem_size, get_phys (0x0040, 0x0017), host_windows_shift_flags); @@ -11676,6 +11742,23 @@ static void int_16 (CPU8086 *cpu) { case 0x01: /* check key */ case 0x11: /* enhanced check key */ + if (video_native_active ()) { + + ch = bios_read_native_key (cpu, 0); + + if (ch < 0) { + cpu->flags |= FLAG_ZF; + } else { + + cpu->regs[REG_AX] = (uint16_t) ch; + cpu->flags &= ~FLAG_ZF; + + } + + break; + + } + if (bios_key_pending < 0) { bios_key_pending = bios_read_key (cpu, 0); } diff --git a/main.c b/main.c index 5b1d241..ac0c281 100644 --- a/main.c +++ b/main.c @@ -716,6 +716,9 @@ int main (int argc, char **argv) { } + /* DOS programs begin with maskable interrupts enabled. */ + cpu.flags |= 0x0200; + if (atexit (video_native_shutdown) != 0) { free (mem); -- 2.34.1