我正在尝试实现模拟器主循环。模拟器全速运行。大约 60 MHz。但我想让它以 4.77Mhz 运行。是怎么做到的?
for (;;)
{
emu_step(ctx) ;
uint64_t current = get_gtod_clock_time () ;
uint64_t elapsed = current - last_time ;
if (elapsed >= 1000000)
{
printf("Cycles: %d\n", get_cycles(ctx)) ;
set_cycles_zero(ctx, 0) ;
last_time = get_gtod_clock_time () ;
}
}
emu_step()
是执行一条指令的函数。 get_cycles(ctx)
是执行的机器周期的总和。 set_cycles_zero(ctx, 0)
将循环计数器设置为零。
最佳答案
我认为这是因为 emu_step
在 for 循环的每次迭代中都会执行,如果将其移到 if
内,那么它只会在 已用时间 >= 1000000
。在这种特殊情况下,您可以独立于循环执行一次,这样您就不必等待一个受限的周期
。
它可能看起来像
emu_step(ctx) ;
for (;;)
{
uint64_t current = get_gtod_clock_time () ;
uint64_t elapsed = current - last_time ;
if (elapsed >= 1000000)
{
emu_step(ctx);
printf("Cycles: %d\n", get_cycles(ctx)) ;
set_cycles_zero(ctx, 0) ;
last_time = get_gtod_clock_time () ;
}
}
关于c - 减慢模拟器速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40548409/