Reputation: 1692
I'm currently building a small virtual machine in c modelling an old 16-bit CPU, which runs at a super slow clock speed (a few 100 Khz). How would I throttle the virtual machine's processing speed of opcode, etc..? or would I even want to?
Upvotes: 0
Views: 446
Reputation: 2568
As I said in the comments I suggest using some sort of timer mechanism
if you would like to match a certain speed here is how I would do it:
1 kHz 1000 Hz 1/s
----- * ------- * ----- therefore 1 kHz = 1000/s
1 1 kHz 1 Hz
which means every second 1000 operations are occurring, so take the reciprocal to find the amount of time in between operations so 1/1000 s or 1 ms
So lets say you want to match 125 kHz
125 kHz 1000 Hz 1/s
------- * ------- * ----- therefore 125 kHz = 125000/s
1 1 kHz 1 Hz
so 1/125000 s or .008 ms or 8000 ns
Hope this helps!
Upvotes: 1