From fc57b7950b9797badb1acb0296a353f980931860 Mon Sep 17 00:00:00 2001 From: Andrea Odetti Date: Fri, 22 Dec 2023 15:00:58 +0000 Subject: [PATCH] applen: reuse new CommonFrame execution. Signed-off-by: Andrea Odetti --- source/frontends/ncurses/main.cpp | 80 ++++++++++-------------------- source/frontends/ncurses/world.cpp | 3 +- source/frontends/ncurses/world.h | 2 +- 3 files changed, 27 insertions(+), 58 deletions(-) diff --git a/source/frontends/ncurses/main.cpp b/source/frontends/ncurses/main.cpp index b73f6d1f3..283d934c8 100644 --- a/source/frontends/ncurses/main.cpp +++ b/source/frontends/ncurses/main.cpp @@ -7,8 +7,6 @@ #include "CardManager.h" #include "Core.h" -#include "CPU.h" -#include "NTSC.h" #include "SaveState.h" #include "Utilities.h" @@ -23,33 +21,10 @@ namespace { - bool ContinueExecution(const common2::EmulatorOptions & options, const std::shared_ptr & frame) - { - const auto start = std::chrono::steady_clock::now(); - - const double fUsecPerSec = 1.e6; -#if 1 - const UINT nExecutionPeriodUsec = 1000000 / 60; // 60 FPS - // const UINT nExecutionPeriodUsec = 100; // 0.1ms - const double fExecutionPeriodClks = g_fCurrentCLK6502 * ((double)nExecutionPeriodUsec / fUsecPerSec); -#else - const double fExecutionPeriodClks = 1800.0; - const UINT nExecutionPeriodUsec = (UINT) (fUsecPerSec * (fExecutionPeriodClks / g_fCurrentCLK6502)); -#endif - - const DWORD uCyclesToExecute = fExecutionPeriodClks; - - const bool bVideoUpdate = options.ntsc; - g_bFullSpeed = !bVideoUpdate; - - const DWORD uActualCyclesExecuted = CpuExecute(uCyclesToExecute, bVideoUpdate); - g_dwCyclesThisFrame += uActualCyclesExecuted; - - CardManager & cardManager = GetCardMgr(); - cardManager.Update(uActualCyclesExecuted); - - const int key = ProcessKeyboard(frame); + void ProcessKeys(const std::shared_ptr & frame, bool &quit) + { + const int key = GetKeyPressed(frame); switch (key) { @@ -65,7 +40,8 @@ namespace } case KEY_F(3): { - return false; + quit = true; + break; } case KEY_F(5): { @@ -87,56 +63,50 @@ namespace break; } } + } - frame->ProcessEvDev(); + void ContinueExecution(const common2::EmulatorOptions & options, const std::shared_ptr & frame, bool &quit) + { + const auto start = std::chrono::steady_clock::now(); - const UINT dwClksPerFrame = NTSC_GetCyclesPerFrame(); - if (g_dwCyclesThisFrame >= dwClksPerFrame) - { - g_dwCyclesThisFrame = g_dwCyclesThisFrame % dwClksPerFrame; - if (!options.headless) - { - frame->VideoPresentScreen(); - } - } + constexpr const int64_t nExecutionPeriodUsec = 1000000 / 60; // 60 FPS + frame->ExecuteOneFrame(nExecutionPeriodUsec); + + ProcessKeys(frame, quit); + frame->ProcessEvDev(); if (!options.headless) { - const auto end = std::chrono::steady_clock::now(); - const auto diff = end - start; - const long us = std::chrono::duration_cast(diff).count(); - - const double coeff = exp(-0.000001 * nExecutionPeriodUsec); // 0.36 after 1 second - - na2::g_relativeSpeed = na2::g_relativeSpeed * coeff + double(us) / double(nExecutionPeriodUsec) * (1.0 - coeff); - - if (!cardManager.GetDisk2CardMgr().IsConditionForFullSpeed()) + frame->VideoPresentScreen(); + if (!g_bFullSpeed) { + const auto end = std::chrono::steady_clock::now(); + const auto diff = end - start; + const int64_t us = std::chrono::duration_cast(diff).count(); if (us < nExecutionPeriodUsec) { const auto duration = std::chrono::microseconds(nExecutionPeriodUsec - us); std::this_thread::sleep_for(duration); } } - return true; - } - else - { - return !na2::g_stop; } } void EnterMessageLoop(const common2::EmulatorOptions & options, const std::shared_ptr & frame) { - while (ContinueExecution(options, frame)) + bool quit = false; + + do { - } + ContinueExecution(options, frame, quit); + } while (!quit && !na2::g_stop); } int run_ncurses(int argc, const char * argv []) { common2::EmulatorOptions options; const bool run = getEmulatorOptions(argc, argv, "ncurses", options); + options.fixedSpeed = true; if (!run) return 1; diff --git a/source/frontends/ncurses/world.cpp b/source/frontends/ncurses/world.cpp index 440539219..29c1931c1 100644 --- a/source/frontends/ncurses/world.cpp +++ b/source/frontends/ncurses/world.cpp @@ -32,7 +32,6 @@ namespace namespace na2 { - double g_relativeSpeed = 1.0; bool g_stop = false; void SetCtrlCHandler(const bool headless) @@ -48,7 +47,7 @@ namespace na2 } } - int ProcessKeyboard(const std::shared_ptr & frame) + int GetKeyPressed(const std::shared_ptr & frame) { WINDOW * window = frame->GetWindow(); if (!window) diff --git a/source/frontends/ncurses/world.h b/source/frontends/ncurses/world.h index a767ec91b..356a9eabf 100644 --- a/source/frontends/ncurses/world.h +++ b/source/frontends/ncurses/world.h @@ -7,7 +7,7 @@ namespace na2 class NFrame; - int ProcessKeyboard(const std::shared_ptr & frame); + int GetKeyPressed(const std::shared_ptr & frame); void SetCtrlCHandler(const bool headless); extern double g_relativeSpeed;