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 :
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 (16)
fantastic! next up – mame in your browser.
Hi felix,
Someone already started it
http://yvern.com/fMAME/fMAME.html
Thibault
You’re a king! Great work
Love it!
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.
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
Hi Thibault: great! I love the solution you chose: mapping bytecode.
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
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
amazing piece of work!
as always
I find it much more difficult then some years ago though…
Hi,
I love this disco look
Tony
LEGEND! keep it up… i’m dreaming of the day we can all play our old classics right within our browsers…
Nice job!
I’ve included it in onyx
http://www.flashapi.org/onyx/previews.html
(sample #2)
pascal
good job
continue this job
more than this
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!
Hi Remi,
Thanks for the kind words, It’s a pleasure having you here, thanks for your great tutorial!
Thibault
Trackbacks/Pingbacks (6)
[...] AS3 Arcade “Space Invaders” Emulation done by Thibault Imbert [...]
[...] 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.” [...]
[...] 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 ) [...]
[...] 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 [...]
[...] 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: [...]
[...] Emulación del Intel8080 CPU. [...]