CPU仿真并将其锁定为特定的时钟速度 [英] CPU Emulation and locking to a specific clock speed

查看:155
本文介绍了CPU仿真并将其锁定为特定的时钟速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您阅读了我的其他问题,您就会知道我已经花了这个周末一起使用6502 CPU仿真器作为编程练习。

If you had read my other question, you'll know I've spent this weekend putting together a 6502 CPU emulator as a programming exercise.

CPU仿真器基本上是完整的,并且从我有限的测试来看似乎相当准确,但是它的运行速度非常快,我想将其调节到机器的实际时钟速度。

The CPU emulator is mostly complete, and seems to be fairly accurate from my limited testing, however it is running incredibly fast, and I want to throttle it down to the actual clock speed of the machine.

我当前的测试循环是:

    // Just loop infinitely.
    while (1 == 1)
    {                
        CPU.ClockCyclesBeforeNext--;

        if (CPU.ClockCyclesBeforeNext <= 0)
        {
            // Find out how many clock cycles this instruction will take
            CPU.ClockCyclesBeforeNext = CPU.OpcodeMapper.Map[CPU.Memory[CPU.PC]].CpuCycles;

            // Run the instruction
            CPU.ExecuteInstruction(CPU.Memory[CPU.PC]);

            // Debugging Info
            CPU.DumpDebug();
            Console.WriteLine(CPU.OpcodeMapper.Map[CPU.Memory[CPU.PC]].ArgumentLength);

            // Move to next instruction
            CPU.PC += 1 + CPU.OpcodeMapper.Map[CPU.Memory[CPU.PC]].ArgumentLength;                                        
        }
    }

如您所知,每个操作码都需要一个特定的金额时间来完成,因此在我倒数CPU周期时钟之前,我不会运行下一条指令。

As you can tell, each opcode takes a specific amount of time to complete, so I do not run the next instruction until I count down the CPU Cycle clock. This provides proper timing between opcodes, its just that the entire thing runs way to fast.

目标CPU速度为1.79mhz,但是我想为该解决方案提供任何解决方案时钟问题可以将速度保持在1.79MHz,即使我增加了复杂度,也不必调整它。

The targeted CPU speed is 1.79mhz, however I'd like whatever solution to the clock issue to keep the speed at 1.79mhz even as I add complexity, so I don't have to adjust it up.

有什么想法吗?

推荐答案

多年前,我写了一个Z80仿真器,为了精确执行周期,我将时钟速率划分为多个小块,并具有核心执行那么多时钟周期。就我而言,我将其与我正在模拟的游戏系统的帧速率联系在一起。每个操作码都知道要执行多少个周期,内核将继续运行操作码,直到执行了指定数量的周期为止。我有一个外部运行循环,它将运行cpu核心,并运行仿真系统的其他部分,然后休眠直到下一次迭代的开始时间。

I wrote a Z80 emulator many years ago, and to do cycle accurate execution, I divided the clock rate into a number of small blocks and had the core execute that many clock cycles. In my case, I tied it to the frame rate of the game system I was emulating. Each opcode knew how many cycles it took to execute and the core would keep running opcodes until the specified number of cycles had been executed. I had an outer run loop that would run the cpu core, and run other parts of the emulated system and then sleep until the start time of the next iteration.

编辑:添加运行循环示例。

Adding example of run loop.

int execute_run_loop( int cycles )
{
    int n = 0;
    while( n < cycles )
    {
        /* Returns number of cycles executed */
        n += execute_next_opcode();
    }

    return n;
}

希望这会有所帮助。

这篇关于CPU仿真并将其锁定为特定的时钟速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆