Methods for limiting emulated processor speed

I am writing a MOS 6502 processor emulator as part of a larger project that I undertook in my free time. The emulator is written in Java, and before you say it, I know that it will not be as efficient and optimized as if it were written in c or assembly, but the goal is to run it on different platforms and pull it out 2.5MHZ on a 1GHZ processor, which is pretty good for an interpreted emulator. My problem is quite the opposite, I need to limit the number of cycles to 1 MHz. Ive looked around, but did not see many strategies for this. Ive tried a few things, including checking the time after several cycles and sleeping for the difference between the expected time and the actual time elapsed, but checking the time slows down the emulation by 8 times, so someone has some better suggestions or maybeways to optimize time polling in java to reduce slowdown?

+5
source share
4 answers

The problem with using sleep () is that you usually get only 1 ms granularity, and the actual dream you get may not even be accurate to within 1 ms, as it depends on what the rest of the system does . A few tips to try (from my head - I actually did not write a processor emulator in Java):

  • stick to your idea, but check the time between a large number of emulated commands (the execution will be a little “lumpy" in any case, especially on a single-processor machine, since the OS can potentially take the CPU out of your thread for several milliseconds at a time);

  • 1000 , CPU "": , "" , 1 . / ( , ).

+3
+3

System.nanoTime() , @pst.

. , , , . , .

To make it truly healthy, you can generate build code 6502 as text with the corresponding line numbers in byte code. This will allow you to use the debugger to go through the code, breakpoint and see what the application does .;)

An easy way to emulate memory is to use direct ByteBuffer or your own memory with the Unsafe class to access it. This will give you a block of memory that you can access like any type of data in any order.

+1
source

All Articles