Fix 80386 protected-mode far return handling
authorRobert Pengelly <robertapengelly@hotmail.com>
Fri, 23 Oct 2026 02:45:59 +0000 (03:45 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Fri, 23 Oct 2026 02:45:59 +0000 (03:45 +0100)
cpu.c

diff --git a/cpu.c b/cpu.c
index 2a28fc8de2c8fa69366b59387870c143473d7e53..ee8b056f21e628e3ea83e2d2999682e5c88b73e6 100644 (file)
--- a/cpu.c
+++ b/cpu.c
@@ -5294,30 +5294,69 @@ fetch_instruction:
         case 0xCA: {                    /* RETF imm16 */
         
             uint16_t add_sp = fetch16 (cpu);
-            uint16_t new_ip = pop16 (cpu);
-            uint16_t new_cs = pop16 (cpu);
             
-            cpu->regs[REG_SP] = (uint16_t) (cpu->regs[REG_SP] + add_sp);
+            uint32_t new_ip;
+            uint16_t new_cs;
+            
+            if (cpu->has_operand_override) {
+            
+                new_ip = pop32 (cpu);
+                new_cs = (uint16_t) pop32 (cpu);
+            
+            } else {
+            
+                new_ip = pop16 (cpu);
+                new_cs = pop16 (cpu);
+            
+            }
+            
+            cpu_set_stack_pointer (cpu, cpu_stack_pointer (cpu) + add_sp);
             
             if (cpu_load_segment (cpu, 1, new_cs) != 0) {
                 return -1;
             }
             
-            cpu->ip = new_ip;
+            if (new_ip > cpu->segments[1].limit || new_ip > 0xFFFFUL) {
+            
+                fprintf (stderr, "32-bit protected-mode return offset %08X is not implemented\n", new_ip);
+                return -1;
+            
+            }
+            
+            cpu->ip = (uint16_t) new_ip;
             break;
         
         }
         
         case 0xCB: {
         
-            uint16_t new_ip = pop16 (cpu);
-            uint16_t new_cs = pop16 (cpu);
+            uint32_t new_ip;
+            uint16_t new_cs;
+            
+            if (cpu->has_operand_override) {
+            
+                new_ip = pop32 (cpu);
+                new_cs = (uint16_t) pop32 (cpu);
+            
+            } else {
+            
+                new_ip = pop16 (cpu);
+                new_cs = pop16 (cpu);
+            
+            }
             
             if (cpu_load_segment (cpu, 1, new_cs) != 0) {
                 return -1;
             }
             
-            cpu->ip = new_ip;
+            if (new_ip > cpu->segments[1].limit || new_ip > 0xFFFFUL) {
+            
+                fprintf (stderr, "32-bit protected-mode return offset %08X is not implemented\n", new_ip);
+                return -1;
+            
+            }
+            
+            cpu->ip = (uint16_t) new_ip;
             break;
         
         }