From d81cbb56073061cc5b3fb9dfe578e846e34c059a Mon Sep 17 00:00:00 2001 From: Jorj Bauer Date: Mon, 28 Dec 2020 16:04:57 -0500 Subject: [PATCH] avoid fast-forwarding the CPU after exiting the BIOS --- sdl/aiie.cpp | 8 +++++--- teensy/teensy.ino | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/sdl/aiie.cpp b/sdl/aiie.cpp index a2d3a22..04ba5c4 100644 --- a/sdl/aiie.cpp +++ b/sdl/aiie.cpp @@ -36,6 +36,8 @@ volatile bool wantResume = false; volatile bool cpuDebuggerRunning = false; +volatile bool cpuClockInitialized = false; + void doDebugging(); void readPrefs(); void writePrefs(); @@ -118,14 +120,13 @@ static struct timespec runBIOS(struct timespec now) static struct timespec runCPU(struct timespec now) { - static bool initialized = false; static struct timespec startTime; static struct timespec nextInstructionTime; - if (!initialized) { + if (!cpuClockInitialized) { do_gettime(&startTime); do_gettime(&nextInstructionTime); - initialized = true; + cpuClockInitialized = true; } // Check for interrupt-like actions before running the CPU @@ -337,6 +338,7 @@ void loop() g_display->redraw(); // Redraw the UI ((AppleDisplay*)(g_vm->vmdisplay))->modeChange(); // force a full re-draw and blit + cpuClockInitialized = false; // force it to reset so it doesn't fast-forward wasBios = false; } } diff --git a/teensy/teensy.ino b/teensy/teensy.ino index 53dd688..18e3ab8 100644 --- a/teensy/teensy.ino +++ b/teensy/teensy.ino @@ -41,6 +41,8 @@ TeensyUSB usb; Bounce resetButtonDebouncer = Bounce(); +volatile bool cpuClockInitialized = false; + void onKeypress(int unicode) { /* @@ -309,6 +311,16 @@ void runCPU(uint32_t now) static uint32_t countSinceLast = 0; static uint32_t microsAtStart = micros(); static uint32_t microsForNext = microsAtStart + (countSinceLast * SPEEDCTL); + + // Allow the BIOS to reset our timing + if (!cpuClockInitialized) { + nextResetMicros = 0; + countSinceLast = 0; + microsAtStart = micros(); + microsForNext = microsAtStart + (countSinceLast * SPEEDCTL); + + cpuClockInitialized = true; + } if (now >= microsForNext) { countSinceLast += g_cpu->Run(24); // The CPU runs in bursts of cycles. This '24' is the max burst we perform. @@ -361,6 +373,8 @@ void loop() // Poll the keyboard before we start, so we can do selftest on startup g_keyboard->maintainKeyboard(); + // Reset the CPU clock so it doesn't fast-forward + cpuClockInitialized = false; wasBios = false; } }