0

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?

Seki
  • 11,135
  • 7
  • 46
  • 70
Daniel
  • 1,692
  • 2
  • 13
  • 19
  • if youre virtualizing couldnt you just run each opcode after a certain amount of time? or do you mean literally slow down the rate that the vm youre writing has opcodes interpreted by your physical processor? – DanZimm Dec 20 '12 at 09:35
  • @DanZimm The later one, though the first could be a somewhat hacky solution. Each opcode is supposed to take so many "cpu cycles", though not true cycles, I'd have to emulate my own. – Daniel Dec 20 '12 at 09:52
  • 1
    well what about the fact that as you parse the opcodes, multiple cpu cycles will occur per virtual opcode? in other words would it not be better to just use a timer or something if you desire it to be slower down? you could even use sleep() – DanZimm Dec 20 '12 at 10:00
  • @DanZimm That could work, but how would I match a specific Khz? Not exactly how seconds, or milliseconds would transfer to Khz? – Daniel Dec 20 '12 at 23:19
  • ... Not exactly sure* .... – Daniel Dec 20 '12 at 23:20

1 Answers1

1

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!

DanZimm
  • 2,528
  • 2
  • 19
  • 27