CPU Emulation and locking to a specific clock speed

前端 未结 7 1386
臣服心动
臣服心动 2020-12-04 14:58

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.

The CPU emulator is mostly c

7条回答
  •  没有蜡笔的小新
    2020-12-04 15:24

    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.

    EDIT: 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;
    }
    

    Hope this helps.

提交回复
热议问题