Porting the PCE emulator to the browser
The Internet Archive recently added the original Macintosh to the list of classic computers of which they provide emulation, so you can run their archive of software titles in your browser, without installing anything. This is great because it provides the same level of accessibility and convenience to emulation as you'd expect of playing a media file or viewing a document.
When you start up the emulated computer on these pages of the Internet Archive, you're running the PCE emulator, originally a piece of software intended to run natively on desktop operating systems, which has been adapted and recompiled to run in your web browser. As I did the initial work of porting this emulator to the browser (back in 2013), I thought it would be worthwhile to provide a run-down of the tools and hacks which made this possible.
emcc compiler executable instead of
gcc. Emscripten's wrapper for Autotools' configure, called
emconfigure, does most of the work here. Emscripten also handles the mapping of native APIs to browser equivalents, so SDL rendering calls become Canvas API calls, browser input events become SDL events, etc.
requestAnimationFrame, which means that to achieve reasonable performance of the emulator we need to run many emulated CPU cycles for each yield. How many cycles to run could be dynamically adjusted, much like a game timestep, but to keep things simple I just hard-coded and hand-tuned this, and found that ~10000 CPU cycles per yield gives a decent balance of speed and I/O responsiveness for the emulator.
Finally, there was the issue of 'mouse pointer integration', a feature provided by OS virtualization apps like VirtualBox. At this point, moving your mouse around the browser window resulted in the relative movement of the mouse being provided to Mac OS as emulated hardware mouse inputs. Mac OS then moves the mouse cursor by the same amount on the emulator's screen, but the resulting mouse position was not necessarily the same as your OS' real mouse pointer. I felt I could do better, so I added a neat/evil hack to actually update the emulated Mac OS mouse position to match your real mouse cursor's position on the screen. I realised that in classic Mac OS, the mouse position is stored in a few fixed absolute locations in the computer's memory, called 'low memory globals'. Basically, I directly write the mouse position value into the emulated computer's memory. Gross, right? But it works great, as you can see by drawing some stuff in Kid Pix. The mouse responds perfectly. You can read more about low memory globals in this folklore.org story.
I'm really glad Classic Mac emulation made its way onto archive.org, because I think everybody should have the opportunity to experience computing history, and the original Macintosh is an essential piece of that history.
If you're wondering about my rationale for porting emulators to the browser, have a read of my previous post on the subject.