Add configurable 8086 CPU cycle-based throttling
authorRobert Pengelly <robertapengelly@hotmail.com>
Tue, 27 Oct 2026 10:36:11 +0000 (10:36 +0000)
committerRobert Pengelly <robertapengelly@hotmail.com>
Tue, 27 Oct 2026 10:36:11 +0000 (10:36 +0000)
main.c

diff --git a/main.c b/main.c
index 0103de4350612c8121aa2578fec6fbd6f9d77b57..5b1d241c2e52530ad12b852f4d29f0e4d3cffdb0 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,10 +1,17 @@
 /******************************************************************************
  * @file            main.c
  *****************************************************************************/
+#if     !defined (_WIN32) && !defined (_POSIX_C_SOURCE)
+# define    _POSIX_C_SOURCE             200809L
+#endif
+
 #include    <ctype.h>
+#include    <errno.h>
+#include    <stdint.h>
 #include    <stdio.h>
 #include    <stdlib.h>
 #include    <string.h>
+#include    <time.h>
 
 #if     defined (_WIN32)
 # include       <windows.h>
 #define     EXE_LOAD_SEG                0x00E0
 #define     MEM_SIZE                    (16UL << 20)
 
+static uint64_t cpu_clock_ns (void) {
+
+#if     defined (_WIN32)
+    LARGE_INTEGER counter, frequency;
+    
+    if (!QueryPerformanceCounter (&counter) || !QueryPerformanceFrequency (&frequency) || frequency.QuadPart <= 0) {
+        return (uint64_t) GetTickCount () * 1000000ULL;
+    }
+    
+    return (uint64_t) (counter.QuadPart / frequency.QuadPart) * 1000000000ULL + (uint64_t) (counter.QuadPart % frequency.QuadPart) * 1000000000ULL / frequency.QuadPart;
+#else
+    struct timespec now;
+    
+    if (clock_gettime (CLOCK_MONOTONIC, &now) != 0) {
+        return 0;
+    }
+    
+    return (uint64_t) now.tv_sec * 1000000000ULL + now.tv_nsec;
+#endif
+
+}
+
+static void cpu_sleep_ns (uint64_t duration) {
+
+#if     defined (_WIN32)
+    DWORD milliseconds = (DWORD) (duration / 1000000ULL);
+    
+    if (milliseconds) {
+        Sleep (milliseconds);
+    }
+#else
+    struct timespec delay;
+    
+    delay.tv_sec = (time_t) (duration / 1000000000ULL);
+    delay.tv_nsec = (long) (duration % 1000000000ULL);
+    
+    while (nanosleep (&delay, &delay) != 0 && errno == EINTR) {}
+#endif
+
+}
+
+/* Approximate 8086 bus/execution timing, not a cycle-exact model. */
+static unsigned cpu_8086_cycles (CPU8086 *cpu, uint8_t op, uint8_t modrm, uint32_t next_ip, uint16_t old_cx, int repeated) {
+
+    unsigned cycles = 4;
+    unsigned memory_operand = (modrm & 0xC0) != 0xC0;
+    
+    if (op >= 0x70 && op <= 0x7F) {
+        return cpu->ip != next_ip ? 16 : 4;
+    }
+    
+    if (op >= 0xB0 && op <= 0xBF) {
+        return 4;
+    }
+    
+    if (op >= 0x40 && op <= 0x5F) {
+        return op >= 0x50 ? 11 : 3;
+    }
+    
+    if (op <= 0x3B && (op & 7) <= 3) {
+        return memory_operand ? 16 : 3;
+    }
+    
+    switch (op) {
+    
+        case 0x88:  case 0x89:  case 0x8A:  case 0x8B:
+        
+            cycles = memory_operand ? 13 : 2;
+            break;
+        
+        case 0x80:  case 0x81:  case 0x83:
+        
+            cycles = memory_operand ? 17 : 4;
+            break;
+        
+        case 0x90:
+        
+            cycles = 3;
+            break;
+        
+        case 0xA4:  case 0xA5:
+        
+            cycles = 18;
+            break;
+        
+        case 0xA6:  case 0xA7:
+        
+            cycles = 22;
+            break;
+        
+        case 0xAA:  case 0xAB:
+        
+            cycles = 11;
+            break;
+        
+        case 0xAC:  case 0xAD:
+        
+            cycles = 12;
+            break;
+        
+        case 0xAE:  case 0xAF:
+        
+            cycles = 15;
+            break;
+        
+        case 0xE0:  case 0xE1:  case 0xE2:
+        
+            cycles = cpu->ip != next_ip ? 17 : 5;
+            break;
+        
+        case 0xE3:
+        
+            cycles = cpu->ip != next_ip ? 18 : 6;
+            break;
+        
+        case 0xE8:  case 0x9A:
+        
+            cycles = 23;
+            break;
+        
+        case 0xC2:  case 0xC3:  case 0xCA:  case 0xCB:
+        
+            cycles = 20;
+            break;
+        
+        case 0xCD:  case 0xCC:
+        
+            cycles = 51;
+            break;
+        
+        case 0xE9:  case 0xEB:
+        
+            cycles = 15;
+            break;
+        
+        case 0xF6:  case 0xF7:
+        
+            cycles = memory_operand ? 24 : 12;
+            break;
+        
+        default:
+        
+            cycles = 4;
+            break;
+    
+    }
+    
+    if (repeated && op >= 0xA4 && op <= 0xAF) {
+        return 2 + cycles * (unsigned) (uint16_t) (old_cx - cpu->regs[REG_CX]);
+    }
+    
+    return cycles;
+
+}
+
 #if     defined (_WIN32)
 static HANDLE terminal_output_handle;
 static DWORD terminal_output_mode;
@@ -445,6 +607,50 @@ int main (int argc, char **argv) {
     int offset = 0, len, i;
     unsigned long steps = 0;
     
+    uint8_t timing_op = 0, timing_modrm = 0;
+    
+    uint32_t timing_next_ip = 0;
+    uint16_t timing_old_cx;
+    
+    int timing_rep = 0;
+    
+    double cpu_hz = 0;
+    uint64_t cpu_cycles = 0;
+    
+    unsigned cpu_batch = 0;
+    uint64_t cpu_deadline = 0;
+    
+    const char *cpu_name = getenv ("DOSEMU_CPU");
+    const char *cpu_mhz_env = getenv ("DOSEMU_MHZ");
+    
+    if (cpu_name != NULL && strcmp (cpu_name, "8086") != 0) {
+    
+        fprintf (stderr, "Unsupported DOSEMU_CPU: %s\n", cpu_name);
+        return 1;
+    
+    }
+    
+    if (cpu_mhz_env != NULL && *cpu_mhz_env != '\0') {
+    
+        char *end;
+        double mhz;
+        
+        errno = 0;
+        mhz = strtod (cpu_mhz_env, &end);
+        
+        if (errno != 0 || *end != '\0' || !(mhz > 0.0 && mhz <= 1000.0)) {
+        
+            fprintf (stderr, "Invalid DOSEMU_MHZ value: %s\n", cpu_mhz_env);
+            return 1;
+        
+        }
+        
+        cpu_hz = mhz * 1000000.0;
+    
+    } else if (cpu_name != NULL) {
+        cpu_hz = 4770000.0;
+    }
+    
 #if     defined (_WIN32)
     DWORD video_refresh_time = GetTickCount ();
 #endif
@@ -548,6 +754,10 @@ int main (int argc, char **argv) {
     /* init_dos_heap(&cpu, 0x2000); */
     init_ivt (&cpu);
     
+    if (cpu_hz > 0) {
+        cpu_deadline = cpu_clock_ns ();
+    }
+    
     while (!cpu.halted) {
     
         if (trace) {
@@ -574,6 +784,42 @@ int main (int argc, char **argv) {
         
         }
         
+        timing_op = 0, timing_modrm = 0;
+        timing_next_ip = 0;
+        
+        timing_old_cx = cpu.regs[REG_CX];
+        timing_rep = 0;
+        
+        if (cpu_hz > 0) {
+        
+            uint32_t timing_ip = cpu.ip;
+            uint32_t timing_address;
+            
+            unsigned prefix_count = 0;
+            
+            do {
+            
+                timing_address = ((uint32_t) cpu.cs << 4) + (uint16_t) timing_ip;
+                timing_op = timing_address < cpu.mem_size ? cpu.mem[timing_address] : 0x90;
+                
+                timing_ip++;
+                
+                if (timing_op == 0xF2 || timing_op == 0xF3) {
+                    timing_rep = 1;
+                }
+            
+            } while (++prefix_count < 15 && (timing_op == 0xF2 || timing_op == 0xF3 || timing_op == 0x26 || timing_op == 0x2E || timing_op == 0x36 || timing_op == 0x3E || timing_op == 0xF0));
+            
+            timing_address = ((uint32_t) cpu.cs << 4) + (uint16_t) timing_ip;
+            timing_modrm = timing_address < cpu.mem_size ? cpu.mem[timing_address] : 0xC0;
+            timing_next_ip = (uint16_t) (timing_ip + 1);
+            
+            if ((timing_op >= 0x70 && timing_op <= 0x7F) || (timing_op >= 0xE0 && timing_op <= 0xE3)) {
+                timing_next_ip = (uint16_t) (timing_ip + 1);
+            }
+        
+        }
+        
         rc = cpu_step (&cpu);
         
 #if     defined (_WIN32)
@@ -607,6 +853,31 @@ int main (int argc, char **argv) {
         }
         
         steps++;
+        
+        if (cpu_hz > 0) {
+        
+            cpu_cycles += cpu_8086_cycles (&cpu, timing_op, timing_modrm, timing_next_ip, timing_old_cx, timing_rep);
+            
+            if (++cpu_batch == 256) {
+            
+                uint64_t duration = (uint64_t) ((double) cpu_cycles * 1000000000.0 / cpu_hz);
+                uint64_t now;
+                
+                cpu_batch = 0;
+                cpu_cycles = 0;
+                
+                cpu_deadline += duration;
+                now = cpu_clock_ns ();
+                
+                if (cpu_deadline > now) {
+                    cpu_sleep_ns (cpu_deadline - now);
+                } else if (now - cpu_deadline > 100000000ULL) {
+                    cpu_deadline = now;
+                }
+            
+            }
+        
+        }
     
     }