Question

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?

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top