Fix 32-bit IMUL operand handling and DOOM divide exception
authorRobert Pengelly <robertapengelly@hotmail.com>
Sat, 24 Oct 2026 17:22:25 +0000 (18:22 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Sat, 24 Oct 2026 17:22:25 +0000 (18:22 +0100)
cpu.c

diff --git a/cpu.c b/cpu.c
index 49467f183bdf42ac5ee699339fa45d1551cba6cc..db2b58844d1fe0c0448d0974ca1000ba3cee05df 100644 (file)
--- a/cpu.c
+++ b/cpu.c
@@ -3987,14 +3987,34 @@ fetch_instruction:
                     OperandRef rm = decode_rm_operand (cpu, modrm);
                     
                     int reg = (modrm >> 3) & 7;
-                    int32_t result = (int32_t) (int16_t) cpu->regs[reg] * (int32_t) (int16_t) operand_read16 (cpu, &rm);
                     
-                    cpu->regs[reg] = (uint16_t) result;
+                    if (cpu->has_operand_override) {
+                    
+                        int64_t result = (int64_t) (int32_t) get_reg32 (cpu, reg) * (int32_t) operand_read32 (cpu, &rm);
+                        
+                        set_reg32 (cpu, reg, (uint32_t) result);
+                        
+                        if (result < INT32_MIN || result > INT32_MAX) {
+                            cpu->flags |= FLAG_CF | FLAG_OF;
+                        } else {
+                            cpu->flags &= (uint16_t) ~(FLAG_CF | FLAG_OF);
+                        }
+                        
+                        break;
+                    
+                    }
+                    
+                    {
+                    
+                        int32_t result = (int32_t) (int16_t) cpu->regs[reg] * (int32_t) (int16_t) operand_read16 (cpu, &rm);
+                        cpu->regs[reg] = (uint16_t) result;
+                        
+                        if (result < -32768 || result > 32767) {
+                            cpu->flags |= FLAG_CF | FLAG_OF;
+                        } else {
+                            cpu->flags &= (uint16_t) ~(FLAG_CF | FLAG_OF);
+                        }
                     
-                    if (result < -32768 || result > 32767) {
-                        cpu->flags |= FLAG_CF | FLAG_OF;
-                    } else {
-                        cpu->flags &= (uint16_t) ~(FLAG_CF | FLAG_OF);
                     }
                     
                     break;