CAMLBOY is a Game Boy emulator written in OCaml that runs in the browser at 60 FPS even on mobile devices.
The emulator uses OCaml’s module system, functors, GADTs, and first-class modules for clean structure, testability, and runtime cartridge selection.
The architecture comprises CPU, timer, and GPU modules synchronized via a bus that routes 8-bit and 16-bit reads/writes through shared interfaces.
Synchronization is achieved with a main loop that advances timer and GPU in a “catch up” method based on CPU cycle counts.
Game Boy instructions are represented using OCaml GADTs for type-safe argument handling and simplified execution logic.
Integration tests use test ROMs and ppx_expect to enable regression testing and exploratory development.
The emulator compiles to JavaScript with js_of_ocaml and Brr for a browser UI implementation.
Profiling identified GPU and other modules as bottlenecks, and sequential optimizations improved FPS from 20 to 60+ in browsers and 100+ on PC.
Benchmarks in headless mode compare performance across OCaml compiler backends.
The author praises OCaml’s strengths (static typing, variants, modules, ecosystem) and notes challenges in dependency management and abstraction costs.
Get notified when new stories are published for "🇺🇸 Hacker News English"