CPU Emulation and locking to a specific clock speed?

1.79 mhz means 1790000 Instructions per second, or ~0,55 microseconds per instruction. Sounds like a job for a high-precision timer, although i'm not sure how good they are for such small intervals. Maybe a queue that executed 20 instructions at a time every 11 microseconds?

Good advice, but its actually 179,000 cycles per second. Since each instruction takes between 1 and 7 cycles to complete, the actual instructions per second time will vary. – FlySwat Sep 21 '08 at 23:18 True on the Instruction parts, I actually meant clock cycles.

Just: Are you sure about the 179k cycles? Megahertz is a million hertz, and therefore I think it's 1.79 million cycles, unless there is something on the 6502 that causes 1 cycle to take 10 Hertz? – Michael Stum Sep 21 '08 at 23:21 it's been a long time since i've tried to implement a CPU emulator (i still want to write a GameBoy Emulator in Silverlight one day :D), but generally I think that a timer is the way to go, only the exact implementation I am no expert on.

– Michael Stum Sep 21 '08 at 23:22.

Take a look at the original quicktime documentation for inspiration. It was written a long time ago, when displaying video meant just swapping still frames at high enough speed, but the Apple guys decided they needed a full time-management framework. The design at first looks overengineered, but it let them deal with widely different speed requirements and keep them tightly synchronized.

You're fortunate that 6502 has deterministic time behaviour, the exact time each instruction takes is well documented; but it's not constant. Some instructions take 2 cycles, other 3. Just like frames in QuickTime, a video doesn't have a 'frames per second' parameter, each frame tells how long it wants to be in screen.

Since modern CPU's are so non-deterministic, and multitasking OS's can even freeze for a few miliseconds (virtual memory! ), you should keep a tab if you're behind schedule, or if you can take a few microseconds nap.

I would use the clock cycles to calculate time and them sleep the difference in time. Of course, to do this, you need a high-resolution clock. They way you are doing it is going to spike the CPU in spinning loops.

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.

As jfk says, the most common way to do this is tie the cpu speed to the vertical refresh of the (emulated) video output. Pick a number of cycles to run per video frame. This will often be machine-specific but you can calculate it by something like : cycles = clock speed in Hz / required frames-per-second Then you also get to do a sleep until the video update is hit, at which point you start the next n cycles of CPU emulation.

If you're emulating something in particular then you just need to look up the fps rate and processor speed to get this approximately right. EDIT: If you don't have any external timing requirements then it is normal for an emulator to just run as fast as it possibly can. Sometimes this is a desired effect and sometimes not :).

As a starting point, just insert a delay of X milliseconds after you "execute" every single instruction of your guest. It isn't precise, but on average it works. If you need to emulate not only the cpu, but also its signals and the communication with other hardware, you have to account the timings of every instructions.It is not difficult, but requires your time and good cpu reference docs.

To start, read about the MAME project. It is an emulator of different cpus and pals, its docs give you insights about software emulation of hardware and timings.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions