Wednesday, 3 December 2014

Space Invaders!

When I was a teenager Space Invaders arrived and changed the world. I must have put pounds into that machine in the local chipper, in the days when chips cost shillings. It never occurred to me that you could actually buy one of your own until one of my brother's friends' fathers did. (Come to think of it though, if I'd kept all of the cash I put into various machines, I probably could have had one of my own. I believe this is known as delayed gratification.)

Same Hardware, Different Processor!

Fast-forward 30-odd years and I'm sitting there blogging about an emulation of an 8-bit microcomputer of similar vintage. Hmm...

Space Invaders hardware comprises an Intel 8080 clocked at 2MHz, 8k ROM, 1k RAM and 7k memory-mapped video RAM. (An Emutalk thread has all the details and the ROMs.) The most difficult part is definitely the CPU but that only has to be done once to open up a whole world of crap games.

There are dozens of 8080 emulations out there, of varying quality. (Most of them written solely for this game!) I picked one and remixed it in the spirit of the 6502 emulator I made for the UK101; it plugs into that framework quite easily.

When a moderately-complex project like this links for the first time, the natural thing to do is upload it, more in hope than expectation, and when it fails, start debugging. The best way to do this is with a test-suite, preferably one which someone else has written in advance. (This worked out really well for my 6502 core thanks to Klaus Dormann's excellent 6502 tester.)

For the 8080, all searches led to a 'CPU exerciser'. Rather than testing a single instruction in isolation, this program runs a group of operations with different arguments and merging the resulting CPU state into a checksum as it goes. If the final checksum obtained by 'your' CPU is the same as one from a real chip, you know (to a high probability anyway) that your implementation is good for the instructions tested. (Luckily, that page documents results from real CPUs so there's no need to dig up an old 8080 board.)

If that sounds daunting, it is! Fortunately a preliminary test is also provided which tests the instructions required to run the exerciser itself. This helped fix a few bugs on its own and, since it used the CP/M "bdos conout" operation to report success, raised the interesting possibility of implementing a virtual 8080-based CP/M machine. I subsequently found a slightly more complex set of tests in another emulator's repository which helped find enough bugs to get all-but-two of the exerciser's tests passing.

(Timings of the exerciser indicate that my 8 year-old Lenovo laptop runs an emulated 8080 about 80x faster than the 2MHz 8080 for which it was intended. I haven't timed it on a Stellaris Launchpad however.)

Adventures in Dremel

So, despite the fact that some bugs remain in the emulation, enough of it is working to run Space Invaders properly. It runs at correct speed because it is driven by a timer interrupt (emulated with Energia's millisecond timer) and therefore we didn't need to bother keeping track of individual instruction timings, which all of the CPU emulators I looked at did. (Most of them were similar in other ways too, indicating a common influence, perhaps in this document.)

Just like the real thing, if you're 6" tall.

You can find the whole thing at Github, as usual.

No comments:

Post a Comment