2020-11-20 15:32:35 +00:00
|
|
|
#include "frontends/common2/speed.h"
|
|
|
|
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "CPU.h"
|
2020-11-27 13:48:53 +00:00
|
|
|
#include "Core.h"
|
2020-11-20 15:32:35 +00:00
|
|
|
|
2021-02-25 16:04:50 +00:00
|
|
|
namespace common2
|
2020-11-20 15:32:35 +00:00
|
|
|
{
|
|
|
|
|
2021-02-25 16:04:50 +00:00
|
|
|
Speed::Speed(const bool fixedSpeed)
|
|
|
|
: myFixedSpeed(fixedSpeed)
|
2020-11-20 15:32:35 +00:00
|
|
|
{
|
2021-02-25 16:04:50 +00:00
|
|
|
reset();
|
2020-11-20 15:32:35 +00:00
|
|
|
}
|
2020-11-21 15:07:31 +00:00
|
|
|
|
2021-02-25 16:04:50 +00:00
|
|
|
void Speed::reset()
|
|
|
|
{
|
|
|
|
myStartTime = std::chrono::steady_clock::now();
|
|
|
|
myStartCycles = g_nCumulativeCycles;
|
|
|
|
}
|
2020-11-21 15:07:31 +00:00
|
|
|
|
2021-05-28 19:07:58 +01:00
|
|
|
uint64_t Speed::getCyclesAtFixedSpeed(const size_t microseconds) const
|
|
|
|
{
|
|
|
|
const uint64_t cycles = static_cast<uint64_t>(microseconds * g_fCurrentCLK6502 * 1.0e-6);
|
|
|
|
return cycles;
|
|
|
|
}
|
|
|
|
|
2021-05-16 18:44:16 +01:00
|
|
|
uint64_t Speed::getCyclesTillNext(const size_t microseconds) const
|
2021-02-25 16:04:50 +00:00
|
|
|
{
|
2021-06-07 18:34:17 +01:00
|
|
|
if (myFixedSpeed || g_bFullSpeed)
|
2020-11-21 15:07:31 +00:00
|
|
|
{
|
2021-05-28 19:07:58 +01:00
|
|
|
return getCyclesAtFixedSpeed(microseconds);
|
2020-11-21 15:07:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-25 16:04:50 +00:00
|
|
|
const uint64_t currentCycles = g_nCumulativeCycles;
|
|
|
|
const auto currentTime = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
const auto currentDelta = std::chrono::duration_cast<std::chrono::microseconds>(currentTime - myStartTime).count();
|
|
|
|
// target the next time we will be called
|
|
|
|
const auto targetDeltaInMicros = currentDelta + microseconds;
|
|
|
|
|
|
|
|
const uint64_t targetCycles = static_cast<uint64_t>(targetDeltaInMicros * g_fCurrentCLK6502 * 1.0e-6) + myStartCycles;
|
|
|
|
if (targetCycles > currentCycles)
|
|
|
|
{
|
|
|
|
// number of cycles to fill this period
|
|
|
|
const uint64_t cyclesToExecute = targetCycles - currentCycles;
|
|
|
|
return cyclesToExecute;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we got ahead, nothing to do this time
|
|
|
|
// CpuExecute will still execute 1 instruction, which does not cause any issues
|
|
|
|
return 0;
|
|
|
|
}
|
2020-11-21 15:07:31 +00:00
|
|
|
}
|
2020-11-20 15:32:35 +00:00
|
|
|
}
|
2021-02-25 16:04:50 +00:00
|
|
|
|
2020-11-20 15:32:35 +00:00
|
|
|
}
|