Skip to content

Commit

Permalink
applen: reuse new CommonFrame execution.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Odetti <mariofutire@gmail.com>
  • Loading branch information
audetto committed Dec 22, 2023
1 parent 2f12127 commit fc57b79
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 58 deletions.
80 changes: 25 additions & 55 deletions source/frontends/ncurses/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include "CardManager.h"
#include "Core.h"
#include "CPU.h"
#include "NTSC.h"
#include "SaveState.h"
#include "Utilities.h"

Expand All @@ -23,33 +21,10 @@

namespace
{
bool ContinueExecution(const common2::EmulatorOptions & options, const std::shared_ptr<na2::NFrame> & 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<na2::NFrame> & frame, bool &quit)
{
const int key = GetKeyPressed(frame);

switch (key)
{
Expand All @@ -65,7 +40,8 @@ namespace
}
case KEY_F(3):
{
return false;
quit = true;
break;
}
case KEY_F(5):
{
Expand All @@ -87,56 +63,50 @@ namespace
break;
}
}
}

frame->ProcessEvDev();
void ContinueExecution(const common2::EmulatorOptions & options, const std::shared_ptr<na2::NFrame> & 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<std::chrono::microseconds>(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<std::chrono::microseconds>(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<na2::NFrame> & 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;
Expand Down
3 changes: 1 addition & 2 deletions source/frontends/ncurses/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace
namespace na2
{

double g_relativeSpeed = 1.0;
bool g_stop = false;

void SetCtrlCHandler(const bool headless)
Expand All @@ -48,7 +47,7 @@ namespace na2
}
}

int ProcessKeyboard(const std::shared_ptr<NFrame> & frame)
int GetKeyPressed(const std::shared_ptr<NFrame> & frame)
{
WINDOW * window = frame->GetWindow();
if (!window)
Expand Down
2 changes: 1 addition & 1 deletion source/frontends/ncurses/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace na2

class NFrame;

int ProcessKeyboard(const std::shared_ptr<NFrame> & frame);
int GetKeyPressed(const std::shared_ptr<NFrame> & frame);
void SetCtrlCHandler(const bool headless);

extern double g_relativeSpeed;
Expand Down

0 comments on commit fc57b79

Please sign in to comment.