Space Invaders – CPU Translated from Intel 8080 to Motorola 6809 for the TRS-80 Color Computer 3

Hello,

I have recently gotten back into old computer emulation like my Commodore Amiga and even more so with my TRS-80 Color Computer.  I was having a lot of fun getting back into assembly programming on the CoCo.  In the 80’s I enjoyed making utilities for the CoCo but I never did write any games for the machine.  One thing I never understood was Interrupts, and I was listening to the CoCo Crew podcast and John Linville has a Tech Talk section where he talks in detail about CoCo hardware.  One of the first episodes he gave a great explanation on the 6809 and Interrupts and it got me thinking that I should write some code to wrap my head around how these work.

In the past I was aware that an exceptionally talented CoCo programmer managed to Transcode Donkey Kong to the CoCo 3.  The programmer is known as Sock Master and his website has lots of cool CoCo info including information on his Transcoded version of Donkey Kong for the CoCo 3.  He also listed other old Arcade games that might be able to be transcoded in the same way.  I looked through his list and thought I’ve always enjoyed Pac-Man and thought maybe I should give it a try.  This would allow me to learn more about the CoCo hardware and in the end we all could enjoy another cool game for the CoCo.

I started looking into the hardware of Pac Man and I found pretty quickly that it has some hardware the CoCo doesn’t namely sprites for graphics.  Since I don’t know much about computer sprites I decided to start my transcoding to an older more simpler game that would be a good starting point to get my 6809 programming skills back in order.  So I decided Space Invaders would be a great game to start with.

While I was digging through all the info I could for Space Invaders hardware I came across an amazing website called Computer Archeology which has all the details worked out how the hardware works and a fully commented disassembly of the Space Invaders ROMs!!!

With this information I wrote a quick and dirty ‘C’ program on my Mac to transcode the Intel 8080 instructions with all the commented code from the Computer Archeologies disassembly to the equivalent Motorola 6809 instructions.  The program isn’t perfect but it does a reasonably good job.  I could then work through the code fixing up parts of the code that aren’t directly compatible.  As the 8080 has more registers than the 6809 I had to work around these problems manually.  But for the most part the instructions at the machine code level are very similar.

Once I had the code converted to 6809 code I had to figure out the Interrupt parts that Space Invaders uses and how Interrupts work on the CoCo.  I got some great help from the people on the CoCo mailing list especially from Robert Gault.

I had the demo mode partially working but the shots from the player going up the screen were showing up and disappearing then reappearing higher up.  This caused me so much trouble, I scanned through the Space Invaders code trying to figure out what the problem was for days and days.  I almost gave up…  Then I found on the internet that someone had already Transcoded Space Invaders for the CoCo 3.  A website called Retro Ports has a bunch of blog posts describing some of the steps the author tcdev (contact page is currently broken) has gone through to get his version working on the CoCo 3.  tcdev has gotten as far as getting the game fully working on a CoCo 3 but it’s like the original Space Invaders and requires you to rotate the monitor manually or use the -rol command with the MAME emulator.  Out of desperation I downloaded his version of Space Invaders for the CoCo 3 to see if looking through his code I could see why mine is acting so weird with the players shot.  After going through his code for many more days I finally realized I had a setting for the Timer Interrupt wrong, I was using the slow timer instead of the fast timer.  One bit (literally a 1 that should have been a 0) in my initializing code was wrong and it caused about 30 hours of frustrating, code scouring, grief!

I had to work through many more bugs but they were more logical and easier to fix, maybe a day here and a day there…  Running MAME in -debug mode on both my CoCo code and real Space Invaders side by side with breakpoints to compare code was invaluable.  Other things that needed to be worked out for the CoCo vs the Space Invaders hardware is the bitmapped graphics data is backwards.  Space Invaders uses bits 01234567 while the CoCo uses 76543210 for screen data.  So I wrote some code to handle this.  Also the Intel 8080 stores 16 bit values as LSB, MSB where the Motorola stores 16 bit values as MSB,LSB so these values need to be swapped when loaded.  Space Invaders also has a hardware shift circuit that was used to shift the bits of two bytes of data a certain number of bits.  I also wrote some small code to handle this.

So far the game is pretty much at the same point as tcdev’s from Retro Ports.  I don’t know if I’m going to continue any further since I have learned what I wanted to from it and I think I’m going to see how far I can get with original inspiration of Pac Man.

I’m releasing my ‘C’ program that I used to take the Space Invaders commented disassembly into 6809 code.  It should be easy enough for someone to tweak to use with other 8080 or Z80 disassembled code.  I’m also including my source code of the Space Invaders transcode to 6809 for the CoCo 3 that I hope others can learn from.  There are two versions, both have sections of code I’ve re-written while I was debugging my player shoot bug (Timer problem).  One is the first version I got working the other is where I took that version and then I used the optimizations below which might be useful for someone else in the future.

My ‘C’ program makes all Branch statements Long Branches, so I don’t have to think about them while debugging the code.  But once the code is working I changed all Long Branches and made them regular short Branches like LBEQ and LBNE became BEQ and BNE.  Then I would let the assembler figure out which had to be Long Branches and I would change them to Long versions.  For BRA and BSR commands I explain below:

Optimizing ideas:
BRA - 3 cycles
LBRA - 5 cycles
JMP - 4 cycles
BSR - 7 cycles
LBSR - 9 cycles
JSR - 8 cycles
Change LBSR to BSR and if BSR is out of range change it
to a JSR
Change LBRA to BRA and if BRA is out of range change it to a JMP

-Multiple LEAX 1,X in a row could be changed to one instruction
        LEAX   1,X
        LEAX   1,X
        LEAX   1,X
Could be changed to:
        LEAX   3,X

-Usage of +X or -X or other pointer registers (LDA ,X+ loads A
 then increments X, LDA ,-X decrements X then loads A) so:
        LDA   ,X
        LEAX  1,X
Could be changed to:
        LDA   ,X+
Also
        LEAX   -1,X
        LEAX   -1,X
        LDA    ,X
Could be changed to:
        LDA    ,--X

-Don’t need to TSTA after a LDA
        LDA    otherShot1 
        TSTA
        BEQ  L0589
Change to:
        LDA    otherShot1
        BEQ  L0589

-Two lines to Clear a memory location change to one line
        CLRA
        STA     playerAlive
Change to:
        CLR     playerAlive

-Sometimes A is loaded and transferred to B, we could load B directly
 make sure in the code that A is not being used...
L0878   LDA     refAlienDXr      
        TFR     A,B   
To:
L0878   LDB     refAlienDXr

-Sometimes X or HL is loaded and exchanged to Y  or DE, we could
 load Y directly make sure X isn’t used though…. Since Exchange is
 different then a transfer.
        LDX     refAlienYr
        EXG     Y,X
To:
        LDY     refAlienYr

-8080 cannot store the B register to memory so sometimes there is a:
        TFR     B,A     
        STA     refAlienDXr
To:
        STB     refAlienDXr 

-Where the code is translated to (make sure A isn’t used though
 in other code)
        LDA     Address
        TSTA
Could be changed to:
        TST    Address

-Where the code is translated to (make sure A isn’t used though
 in other code)
        LDA      ,X
        TSTA
Could be changed to:
        TST      ,X

With all the original commented code someone could take this code and get the code to work on a regular CoCo 3 properly without the rotation problem.  Maybe they could add sound samples too to make it perfect!

FYI – I used LWTOOLS to assemble the 6809 code with the following command

$ lwasm -9bl -p cd -oSI.BIN Space_Invaders_001.asm > listing.txt

Then Test using MAME with the following two lines:

$ imgtool del coco_jvc_rsdos Disk1.dsk SI.BIN
$ imgtool put coco_jvc_rsdos Disk1.dsk SI.BIN SI.BIN
$ mame -window -natural -nojoy -rol coco3 -flop1 Disk1.dsk

For debugging with MAME you can use this line:

$ mame -window -natural -nojoy -debug -rol coco3 -flop1 Disk1.dsk

The files have been uploaded to “TRS-80 COLOR COMPUTER ARCHIVE” the .Zip file is called “Space_Invaders_8080_to_6809_for_CoCo_3.zip” . I’m not sure what section it will be moved to once it has been verified.

Cheers, Glen

This entry was posted in CoCo Programming. Bookmark the permalink.

6 Responses to Space Invaders – CPU Translated from Intel 8080 to Motorola 6809 for the TRS-80 Color Computer 3

  1. Pingback: Space Invaders – TRS-80 CoCo 3 port directly from Intel 8080 Arcade sources! – Vintage is the New Old

  2. Pingback: Port do dia: Space Invaders de arcade para CoCo 3 | Retrocomputaria

  3. landkraken says:

    Thanks for the FYI regarding setup/debugging with Mame. Have a workflow for developing 6809 Assembly for the Vectrex, and was hoping to ultimately start developing for Coco. Appreciate your time in documenting your efforts here. Best wishes on any future projects you may tackle!

  4. Pingback: Arcade Machine Conversion to the CoCo Overview | Glen's Weblog

Leave a comment