Fix BIOS keyboard input in native graphics mode
authorRobert Pengelly <robertapengelly@hotmail.com>
Thu, 29 Oct 2026 20:08:32 +0000 (20:08 +0000)
committerRobert Pengelly <robertapengelly@hotmail.com>
Thu, 29 Oct 2026 20:08:32 +0000 (20:08 +0000)
interrupts.c
main.c

index 240a0dfd5d681b5edaa02fd3fa360f0a5ead2ae4..5ff82c933cf39d4d63cd2b0b630ee186d4139306 100644 (file)
@@ -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 5b1d241c2e52530ad12b852f4d29f0e4d3cffdb0..ac0c28170c8004ba8d7d0421ce420df02435285f 100644 (file)
--- 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);