AS3 Intel8080 “Space Invaders” Emulation

Emulation programming has always been something really interesting to me. Joa, Claus and Darron and Jon Pollard already did some really cool stuff in AS3 concerning emulation programming. Lately I wanted to do my first steps with it and decided to start with a simple 8-bit CPU. Quickly, I noticed that the Intel8080 was really popular because of its usage in the first arcade games and especially the very famous Space Invaders (Special tribute to you Mike!).

This CPU has been designed to run at 2Mhz and is a good start for getting started with CPU cause each operation code is stored in a single byte. By reading a few documents related to emulation programming I noticed that the "Hello World" seems to be the Space Invaders emulation in the emulation programming scene. I found this great tutorial in VB about emulating Space Invaders and decided to try it in AS3. It would have been cool to have a byte and short type in addition to int to avoid byte overflow, but well, I read that Darron and Claus had the same problem with FC64 and had to do the checking manually too.

So here is my first try, the original Space Invaders ROM (8Kb) is loaded and executed :

A Flash animation should have appeared here, but it seems that your browser has an older version of the Flash Player or it is not installed at all. Please, install the last release of the Flash Player now, then reload this page.

I just can't stop playing with it, it reminds me when I was a kid playing to Space Invaders on my Atari 2600. One thing I would like to do is plug this emulator to a database to store player scores, or maybe multiplayer support through RTMFP, that would be cool :)

The original game in 1978 was not using that orange color but instead white and green, but I wanted to tune it a bit and give it a disco look'n feel:). The Intel8080 emulation is a simple interpreted emulation, I optimized a little bit the fetch code approach by avoiding as much as possible switch cases and initialised just once a mapping table where each index (opcode) in a Vector references each operation to execute, something like :

for (var i:int = 0; i< lng; i++)
{
code = memory[i];

switch ( code )
{
case 0xC3:
mappingTable[code] = Instruction_JMP;
break;

Instead of multiple swich cases during the fetch code loop, I execute the appropriate instruction thanks to the mapping table :

instruction = getRomByte();
mappingTable[instruction]();

A much better way to emulate a CPU is the dynamic recompilation approach, where each opcode from the ROM is dynamically recompiled at runtime to AVM2 instructions, this way you bypass compiler optimizations problems and get a more granular way to optimize and get better performance. I guess there are many different ways to emulate a CPU, well these are my first steps !

Sources available here.

Comments (17)

  1. felix wrote:

    fantastic! next up – mame in your browser.

    Monday, April 13, 2009 at 7:46 pm #
  2. Thibault Imbert wrote:

    Hi felix,

    Someone already started it :)

    http://yvern.com/fMAME/fMAME.html

    Thibault

    Monday, April 13, 2009 at 8:02 pm #
  3. You’re a king! Great work :)

    Monday, April 13, 2009 at 10:10 pm #
  4. Love it!

    Tuesday, April 14, 2009 at 1:07 am #
  5. Tek wrote:

    Excellent! Do you load a ROM or the emulator is specially customized for “Space Invaders”?

    Notice, I’m sure it’s because Flash Player is soooooo fast …. but you probably better need to slow it down a little. Even, the first level is really hard. ;)

    Tuesday, April 14, 2009 at 1:24 am #
  6. Thibault Imbert wrote:

    Hi Tek,

    I load the original ROM game, the only thing customized is the color. You are right, I will slow it down a little bit :)

    Thibault

    Tuesday, April 14, 2009 at 1:47 am #
  7. Hi Thibault: great! I love the solution you chose: mapping bytecode. :-)

    Tuesday, April 14, 2009 at 2:18 am #
  8. Hi Thibault,
    great work! Could you elaborate on what you did with dynamic compilation? It sounds like a great way to go meta-geek on a rainy day…
    Ariel

    Tuesday, April 14, 2009 at 1:38 pm #
  9. Thibault Imbert wrote:

    Hi Ariel,

    In this version, I didn’t do any Dynamic Recompilation, the Intel8080 CPU emulation is only interpreted. I am currently looking at running a Z80 with some binary translation. You can read more about it in this PDF :

    http://www.scribd.com/doc/94546/Study-of-the-techniques-for-emulation-programming-by-Victor-Moya-del-Barrio

    Thibault

    Tuesday, April 14, 2009 at 1:51 pm #
  10. nicoptere wrote:

    amazing piece of work!
    as always :)
    I find it much more difficult then some years ago though…

    Wednesday, April 15, 2009 at 7:08 pm #
  11. Tony wrote:

    Hi,

    I love this disco look :)

    Tony

    Friday, April 17, 2009 at 8:59 pm #
  12. ryan wrote:

    LEGEND! keep it up… i’m dreaming of the day we can all play our old classics right within our browsers…

    Saturday, April 18, 2009 at 3:31 am #
  13. pascal wrote:

    Nice job!
    I’ve included it in onyx ;)
    http://www.flashapi.org/onyx/previews.html
    (sample #2)
    pascal

    Wednesday, August 5, 2009 at 5:04 pm #
  14. chris wrote:

    good job
    continue this job
    more than this

    Thursday, January 14, 2010 at 6:47 pm #
  15. Awesome work M. Imbert! I’m the one who wrote the BlitzMax tutorial on how to write an emulator in Basic. Congratulations, keep up the good work!

    Wednesday, February 24, 2010 at 2:04 am #
  16. Thibault Imbert wrote:

    Hi Remi,

    Thanks for the kind words, It’s a pleasure having you here, thanks for your great tutorial!

    Thibault

    Wednesday, February 24, 2010 at 9:03 am #
  17. i guess this i name wrote:

    You say “VB”, but it’s Blitzmax.

    I love the Blitzmax concept, but I can’t dip my toes in the water without paying 80 bucks. Is there an old VB way of experimenting with this or a non-commercial Blitzmax?

    Friday, November 19, 2010 at 6:45 am #

Trackbacks/Pingbacks (8)

  1. nada » Blog Archive » intel8080 on as3 on Wednesday, April 15, 2009 at 10:46 am

    [...] AS3 Arcade “Space Invaders” Emulation done by Thibault Imbert [...]

     
  2. [...] Thibault Imbert: “This CPU has been designed to run at 2Mhz and is a good start for getting started with CPU cause each operation code is stored in a single byte. By reading a few documents related to emulation programming I noticed that the "Hello World" seems to be the Space Invaders emulation in the emulation programming scene. I found this great tutorial in VB about emulating Space Invaders and decided to try it in AS3. It would have been cool to have a byte and short type in addition to int to avoid byte overflow, but well, I read that Darron and Claus had the same problem with FC64 and had to do the checking manually too.” [...]

     
  3. [...] Tibo AS3 Space Invaders Arcade emulator ( http://www.bytearray.org/?p=622 ) Blitz Max tutorial by Remi Veilleux ( http://sites.google.com/site/rveilleux/arcadechapter1 ) [...]

     
  4. emulation - StartTags.com on Monday, January 25, 2010 at 10:32 pm

    [...] as affordable, to achieve a functioning emulation, i.e., one you could talk to and so on. …AS3 Intel8080 Space Invaders Emulation [ by …[ Emulation programming has always been something really interesting to me. … did some really cool [...]

     
  5. Innovation and Flash | RIAgora on Wednesday, June 2, 2010 at 4:04 pm

    [...] the source code Voice gesture recognition: http://www.bytearray.org/?p=1151 Intel8080 CPU emulation: http://www.bytearray.org/?p=622 Street Fighter CPU emulation: http://www.dotemu.com/en/sf2 Alchemy C++ 2D Engine: [...]

     
  6. Innovación y Flash | RIA212 on Wednesday, June 2, 2010 at 6:49 pm

    [...] Emulación del Intel8080 CPU. [...]

     
  7. Intel 8080 emulation in JavaScript | TypedArray.org on Monday, March 18, 2013 at 7:46 am

    [...] few years ago, I wrote an Intel 8080 CPU emulator in ActionScript 3 and thought this would be a great fit for a TypeScript exercise. For the context, the Intel 8080 is [...]

     
  8. Intel 8080 CPU emulation in JavaScript | TypedArray.org on Monday, June 24, 2013 at 6:52 pm

    [...] few years ago, I wrote an Intel 8080 CPU emulator in ActionScript 3 and thought this would be a great fit for a TypeScript exercise. For the context, the Intel 8080 is [...]