我正在我的 github 上实现 DMG-01(AKA gameboy 1989)。我已经实现了 APU 和 PPU,在我的电脑(和我朋友的电脑)上具有(几乎)完美的时机。然而,当我在我朋友的一台电脑上运行模拟器时,它的运行速度是我或其他朋友的两倍。
同步时钟的代码(在游戏机和运行它的电脑之间)如下:
Clock.h 头文件:
class Clock
{
// ...
public:
void SyncClock();
private:
/* API::LR35902_HZ_CLOCK is 4'194'304 */
using lr35902_clock_period = std::chrono::duration<int64_t, std::ratio<1, API::LR35902_HZ_CLOCK>>;
static constexpr lr35902_clock_period one_clock_period{1};
using clock = std::chrono::high_resolution_clock;
private:
decltype(clock::now()) _last_tick{std::chrono::time_point_cast<clock::duration>(clock::now() + one_clock_period)};
};
时钟.cpp 文件
void Clock::SyncClock()
{
// Sleep until one tick has passed.
std::this_thread::sleep_until(this->_last_tick);
// Use time_point_cast to convert (via truncation towards zero) back to
// the "native" duration of high_resolution_clock
this->_last_tick = std::chrono::time_point_cast<clock::duration>(this->_last_tick + one_clock_period);
}
像这样在 main.cpp 中调用它:
int main()
{
// ...
while (true)
{
// processor.Clock() returns the number of clocks it took for the processor to run the
// current instruction. We need to sleep this thread for each clock passed.
for (std::size_t current_clock = processor.Clock(); current_clock > 0; --current_clock)
{
clock.SyncClock();
}
}
// ...
}
在这种情况下,chrono 是否会在其他计算机中以不同的方式受到影响?时间是绝对的,我明白为什么在一台电脑上运行模拟器会更慢,但为什么更快呢?我检查了我的时钟类型(high_resolution_clock),但我不明白为什么会这样。谢谢!