Fix DOS/4G startup hang and implement 8042/CMOS support
authorRobert Pengelly <robertapengelly@hotmail.com>
Sat, 24 Oct 2026 03:08:56 +0000 (04:08 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Sat, 24 Oct 2026 03:08:56 +0000 (04:08 +0100)
cpu.c
cpu.h
interrupts.c

diff --git a/cpu.c b/cpu.c
index 97647b17d731a6e0b3bd46498a31779b076d92ca..5462d191d8030085773c9905f7a53fbebd4d7fe9 100644 (file)
--- a/cpu.c
+++ b/cpu.c
@@ -1457,6 +1457,13 @@ static uint8_t io_read8 (CPU8086 *cpu, uint16_t port) {
         
         case 0x60:                      /* keyboard data port */
         
+            if (cpu->kbd_output_port_read_pending) {
+            
+                cpu->kbd_output_port_read_pending = 0;
+                return cpu->kbd_output_port;
+            
+            }
+            
             if (cpu->kbd_ack_pending) {
             
                 cpu->kbd_ack_pending--;
@@ -1472,7 +1479,11 @@ static uint8_t io_read8 (CPU8086 *cpu, uint16_t port) {
 
         case 0x64:                      /* keyboard status port */
         
-            return cpu->kbd_ack_pending ? 0x01 : 0x00;
+            return cpu->kbd_ack_pending || cpu->kbd_output_port_read_pending ? 0x01 : 0x00;
+        
+        case 0x71:                      /* CMOS/RTC data register */
+        
+            return cpu->cmos_regs[cpu->cmos_index];
         
         case 0x92:                      /* system control port A */
         
@@ -1551,6 +1562,22 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) {
         
         case 0x60:                      /* AT keyboard data port */
         
+            if (cpu->kbd_output_port_pending) {
+            
+                cpu->kbd_output_port_pending = 0;
+                
+                if (!(value & 1)) {
+                    fprintf (stderr, "8042 keyboard controller reset is not implemented\n");
+                    exit (1);
+                }
+                
+                cpu->kbd_output_port = value;
+                cpu->a20_enabled = ((cpu->kbd_output_port | cpu->port92) & 2) != 0;
+                
+                break;
+            
+            }
+            
             if (cpu->kbd_typematic_pending) {
             
                 cpu->kbd_typematic_rate = value & 0x7F;
@@ -1588,6 +1615,33 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) {
         
         }
         
+        case 0x64:                      /* 8042 controller command port */
+        
+            if (value == 0xD0) {
+                cpu->kbd_output_port_read_pending = 1;
+            } else if (value == 0xD1) {
+                cpu->kbd_output_port_pending = 1;
+            } else if (value >= 0xF0 && (value & 1)) {
+                /* Pulse output bits 0-3; no reset pulse requested. */
+            } else {
+            
+                fprintf (stderr, "unhandled keyboard controller command %02X at %04X:%04X\n", value, cpu->cs, cpu->ip);
+                exit (1);
+            
+            }
+            
+            break;
+        
+        case 0x70:                      /* CMOS/RTC register select */
+        
+            cpu->cmos_index = value & 0x7F;
+            break;
+        
+        case 0x71:                      /* CMOS/RTC data register */
+        
+            cpu->cmos_regs[cpu->cmos_index] = value;
+            break;
+        
         case 0x92:                      /* system control port A */
         
             if (value & 1) {
@@ -1598,8 +1652,8 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) {
             }
             
             cpu->port92 = value;
-            cpu->a20_enabled = (value & 2) != 0;
             
+            cpu->a20_enabled = ((cpu->kbd_output_port | value) & 2) != 0;
             break;
         
         case 0x201:                     /* game port: reset joystick timing */
@@ -2059,6 +2113,20 @@ void cpu_reset (CPU8086 *cpu, uint8_t *mem, size_t mem_size) {
     memset (cpu, 0, sizeof (*cpu));
     
     cpu->crtc_base = 0x3D4;
+    cpu->kbd_output_port = 0x01;
+    
+    cpu->cmos_regs[0x0A] = 0x26;
+    cpu->cmos_regs[0x0B] = 0x02;
+    cpu->cmos_regs[0x0D] = 0x80;
+    cpu->cmos_regs[0x15] = 0x80;
+    cpu->cmos_regs[0x16] = 0x02;
+    
+    cpu->cmos_regs[0x18] = (uint8_t) (((mem_size - 0x100000) / 1024) >> 8);
+    cpu->cmos_regs[0x17] = (uint8_t) (((mem_size - 0x100000) / 1024) & 0xff);
+    
+    cpu->cmos_regs[0x30] = cpu->cmos_regs[0x17];
+    cpu->cmos_regs[0x31] = cpu->cmos_regs[0x18];
+    
     cpu->cr0 = 0x00000010UL;            /* 80386 ET bit is set after reset */
     
     cpu->gdtr_limit = 0xFFFF;
diff --git a/cpu.h b/cpu.h
index 3e656a7fe84317517c92b382910f9456323005f7..28e38969c6cb35ae353bede3af4597695828d721 100644 (file)
--- a/cpu.h
+++ b/cpu.h
@@ -80,6 +80,9 @@ typedef struct __attribute__ ((packed)) CPU8086 {
     int rep_type;
     
     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 cmos_index, cmos_regs[128];
     uint8_t port61, port92, a20_enabled, vga_status_flip;
     
     PITCounter pit[3];
index 3e5fe7425d8315d9bcb54488d4a3e45b31e3c440..40e72f75e10c8d82f8b8df8846015dbb5c7b73b2 100644 (file)
@@ -10937,8 +10937,26 @@ void init_ivt (CPU8086 *cpu) {
     for (i = 0; i < 256; i++) {
     
         uint32_t entry = stub_base + (i * 4);
+        uint16_t entry_offset = (uint16_t) (i * 4);
         
-        mem_write16 (cpu->mem, cpu->mem_size, i * 4, i * 4);
+        switch (i) {
+        
+            case 0x00:  case 0x01:  case 0x02:  case 0x03:
+            case 0x04:  case 0x05:  case 0x06:  case 0x07:
+            case 0x10:  case 0x11:  case 0x13:  case 0x15:
+            case 0x16:  case 0x1A:  case 0x20:  case 0x21:
+            case 0x23:  case 0x28:  case 0x2F:  case 0x67:
+            
+                break;
+            
+            default:
+            
+                entry_offset = 0x0400;
+                break;
+        
+        }
+        
+        mem_write16 (cpu->mem, cpu->mem_size, i * 4, entry_offset);
         mem_write16 (cpu->mem, cpu->mem_size, i * 4 + 2, DOS_IVT_STUB_SEG);
         
         mem_write8 (cpu->mem, cpu->mem_size, entry + 0, 0xCD);
@@ -10948,8 +10966,8 @@ void init_ivt (CPU8086 *cpu) {
     
     }
     
-    /* No mouse driver is installed, so INT 33h must point to IRET. */
-    mem_write16 (cpu->mem, cpu->mem_size, 0x33 * 4, 0x33 * 4 + 2);
+    /* Unassigned interrupt vectors share one BIOS-style IRET handler. */
+    mem_write8 (cpu->mem, cpu->mem_size, stub_base + 0x0400, 0xCF);
     
     dos_long_paths = env_long_paths && strcmp (env_long_paths, "1") == 0;