ZX Spectrum in Flash Player 9/AS3

One weekend in November last year, I decided to port the wonderful Sinclair ZX Spectrum of olde to Flash using the Flex SDK, Flash Develop + an eclipse plugin from OSFlash which does the donkey work of porting Java to Actionscript.

I never blogged about it at the time because let’s face it, blogging is hard work and takes up valuable time. But now someone else has released one I am being urged to bring mine into the light of day.

The ZX Spectrum was my first computer and was given to me by my father at the age of around 11. It therefore has a very special place in my heart.

I started with an open source implementation of the ZX Spectrum written in 1997, Jasper 1.1 by Adam Davidson & Andrew Pollard.

screenshot

Jasper was a Java Applet (the flavour of the day) and it is interesting to note that it is ten years since this was being done in Java… It makes me realise how little has really changed :)

Anyway, getting the damn thing to compile after this brutal treatment was quite a long haul, the automatic porting tool not really being that clever, and just based on search and replace rather than a code DOM.

The fun part came when it finally compiled and all the timing, graphics and IO stuff had been rewritten for the Flash player…

I was staring at a completely black screen… and the thing was humming away but no graphics…nothing, not a bean.

It turned out to be a tiny problem with my paint function… everything was there, just being painted in black on black… with this fixed I pressed Build again. Suddenly the thing sprung into life… (C)1982 Sinclair Research Ltd. there in glorious 8 bit monochrome.
I almost fell off my seat it was so good.

I must confess I thought I had done it at that point. I was pretty pleased, things had gone well and this had taken just a couple of nights - so I immediately started loading up games into my virtual machine… my favourite, Starquake worked like a dream.

But when I tried Elite (a real classic of the time) the Spectrum would crash about 30 seconds into the splash screen.

I had to get Elite working, that was for sure. If I did not manage this, the whole thing would be a failure.

There was something wrong deep down in the code, some tiny discrepancy which did not become apparent until 30 seconds into a game, something so tiny that a whole game could work perfectly but not Elite… wow. How infuriating!

And so began a journey into an abstract and extraordinary debugging process that was simply a matter of homing in on the divergence in behaviour between the (working) applet and the crashing version I had produced…

This involved tracing the machine code instructions from both VMs and basically looking for the point where they diverged, and then
stepping through the java code, and tracing all suspect results, and again comparing the logs… and repeating the process.. ad nauseam..

Eventually of course I cornered the bug. It was to do with Java’s byte type, which in fact represents a *signed* byte (-127 to 127) which I had foolishly thought of as being an unsigned byte (0-255) and had thus treated it as an int in actionscript 3, which is why the spectrum wigged out after 30 seconds of Elite…

The fix:

function byte(n:int):int
{
return n>127?(256-n)*-1:n;
}

With this tiny mod Elite was working and so were all the other games I tried, with the exception of Penetrator. If anyone wants to get Penetrator working properly then I challenge you to do so. You’ll see what I went through, and if you get it working then we can all have a party (Penetrator was a real beaut of a game, I’m sure the programmer was using some highly undocumented shit)

There is also the small matter of the sound which was not implemented in the version I ported from… if any one is feeling up to it, the libraries are out there to port the sound to flash… but I dont have time to do that right now.

Anyway, once the machine was basically working, I spent a while worrying over performance concerns, since it seemed to run at around 100% on my very fast machine. I tried several different optimisation stategies, but strangely nothing I tried seemed to make any difference.

I finally traced the problem to… no problem at all. It turned out that running the spectrum in a non-debug version of the flash player, that it would actually run at a very reasonable <5%

Therefore I urge people to be aware of the enormous difference that this can make (I had no idea the effect could be so marked) when looking at performance.

Finally I searched high and low for the most classic games that I remember from my childhood. The images that this spec loads are .sna files, snapshots of the machine state. I put a few up there with the emulator for a bit of fun and to really bring back that old school feeling I added the ZX rainbow to the page.

All in all I had spent about 7 nights hacking against this thing and I now finally stood proudly on Mount Spectrum.

Without further ado let me present for your delight and delectation my Flash ZX Spectrum emulator (Flasper) based on Pollard and Davidson’s (1997) Jasper

2 Responses to “ZX Spectrum in Flash Player 9/AS3”

  1. Bob Thedino Says:

    Merry Christmas! What key is Symbol Shift mapped to on a Mac? It’s important to be able to slow down in Elite!

  2. Rita Says:

    Hi
    I’m working on a website that could use some of the games presented here. Would you be interested in helping us? Could you please send me an email if you are?
    Many Thanks
    Rita

Leave a Reply