Bluish Coder

Programming Languages, Martials Arts and Computers. The Weblog of Chris Double.


JavaScript Space Invaders Emulator

Ajaxian recently posted about a fun JavaScript implementation of PacMan. After spending way too much time on it I wondered how well an emulation of the old arcade game hardware would go in JavaScript.

I've written a few 8080 arcade game emulations before in different languages so I had a go at implementing it in JavaScript. You can try the work in progress at my JavaScript 8080 Emulation page. It runs surprisingly well on modern JavaScript engines.

The page first loads with the arcade game Space Invaders loaded. You can run a set number of instructions, or step through one at a time. It displays the disassembled output. Pressing 'Animate' will run the game in a timer and it can be played. It is a general 8080 arcade game emulator, for the games that use similar hardware to Space Invaders. The buttons at the top load the code for Space Invaders, Lunar Rescue or Balloon Bomber.

If you have a bleeding edge version of Firefox 3.1 with Ogg Vorbis <audio> support, pressing the 'Enable Audio' button will enable sound. The sound support uses <audio> to play the samples when requested by the emulator. This turns out to make a good test case for my audio support and it may need the fixes from bug 449159 and 454364 to work.

If you're interested in the other emulators I've done:

The implementation does not get traced with the TraceMonkey tracing JIT yet. I'll look into the reasons why and as TraceMonkey and my implementation improves it'll get faster I'm sure. Even so, it runs very close to full speed.

This implementation uses Canvas, audio for sound and should work on browsers with a fast JS engine and these technologies.

For the emulator loop I run a set number of instructions during a timer that is run via 'setInterval' to prevent the 'script is running too long' message. One thought that Robert O'Callahan suggested was to run the emulator in a worker thread and have communication for input/output via messages to the browser. I'll play with this idea and see how it goes - it'll give me a chance to try out Firefox 3.1 worker threads implementation.

The emulator can be run (without the GUI) from a JavaScript shell for testing purposes. I used the shell to test the implementation by running my Factor version and logging all the state of the emulated CPU, doing the same with the JavaScript version, and making sure the output was the same.

Although it's not quite perfect, it's currently playable, and shows that the types of games that are written as Java or Flash applets can be done in standard HTML and JavaScript in the latest browsers.


This site is accessable over tor as hidden service 6vp5u25g4izec5c37wv52skvecikld6kysvsivnl6sdg6q7wy25lixad.onion, or Freenet using key: