Blog post: Writing a Game Boy emulator in OCaml

Is this a real-time program?
I guess you need to refrain the emulator from going to fast, or the games
won’t be playable.

Not 100% sure what you mean by “real-time program”, but to answer the latter half of the question: yes, as you pointed out, I did need to throttle the emulator so the emulator runs at a fixed speed, namely 60 FPS/sec.

The way I introduced throttling was different between the SDL2 native frontend and the js_of_ocaml web frontend:

  • For the SDL2 native frontend: I triggered an appropriate amount of Unix.sleep depending on how long it took to render the frame (code)
  • For the js_of_ocaml web frontend: I used the requestAnimationFrame and scheduled the main loop to be called on every repaint, which is 60 times/sec (code)

Also, in case garbage collection is triggered at an unsavory time point,
the game might lag.
Does it happen sometimes?

Actually, the emulator generates very little garbage so GC has never been an issue. It also doesn’t do much IO, so it is a purely CPU-intensive program.

3 Likes