Defender Conversion for the CoCo 3 – Part 2 – Short and Sweet

Just a little blog entry to share a neat little bit of 6809 code.
I’m really impressed with how well the 6809 is programmed in Defender.  I was decoding some of the Defender source code today and I came across some neat code to handle the button presses.  Someone other then myself might like to use this code in their own projects.
Defender get’s many of the buttons into the A accumulator bits 7 to 0 (7=Down,6=Reverse,5=1 Player Start,4=2 Player Start,3=Hyperspace,2=Smart Bomb,1=Thrust,0=Fire)
Then it uses this cool bit of code to handle the bits that are set (active).
       LDU   #MF882 ;D5E2: CE F8 82 Load U with table start position (add 4 to get real start address)
ZD5E5: CLRB         ;D5E5: 5F       B=0 & Clears the Carry flag on the CC register
ZD5E6: ADDB  #$04   ;D5E6: CB 04    B=B+4
       LSRA         ;D5E8: 44       Shift A which has the button values into the Carry bit
       BCC   ZD5E6  ;D5E9: 24 FB    If the bit position was a zero (button not pressed) then go check the next
       LEAU  B,U    ;D5EB: 33 C5    U=U+B U now points to the location in RAM to get the four bytes associated with the button press
       PULU  X,D    ;D5ED: 37 16    LOAD D & X with the button value

I like the use of the carry bit to find the active bit and then using U as the pointer and instead of loading X & D it pulls the data into the registers.  This saves cycles and space!  If you had to access the data from a table that had 6 entries you can do the same thing except you would use ADDB   #$06 and PULU  Y,X,D.  This becomes even better since a load to the Y instruction is slower and takes more space.  Neat stuff!

I’ve already used this technique to speed up my audio playback code since each sample needs it’s own Bank # in CoCo 3 memory, the # of banks the sample uses, start address and what MMU bank location to use.

Here is my setup code used whenever a sample is changed in Defender for the CoCo 3.  This uses the same PULU technique I just learned from the Defender code and even pulls the value of the Y register and uses it as the pointer to update the MMU page selections.

See you in the next post…

* Playback sound sample # in B
* Input = B
        PSHS    A,X,Y,U
        LDU     #SoundBankLocation
        LSLB                        * B=B*2
        STB     PlySndAddB+1        * Save the value for the add, self modify
        LSLB                        * B=B*4
        ADDB    #$00                * B=B+(old B*2 value)
                                    * So B=B*6 - This value gets self modified, for the amount to add to B from above
        LEAU    B,U                 * Move U pointer to the correct position
        PULU    A,B,X,Y             * Load A,B,X & Y from the table below
                                    * A = Bank in RAM where the sample data is located
                                    * B = # of Banks this sample uses
                                    * X = Address where the sample data starts
                                    * Y = MMU Register Page Location the sample data will use to start playback from
        BMI     PlayRepeatSound     * If B - bit 7 is high then it indicates this loop is one the repeats go setup and play this sample looping
        BEQ     PlayNoSound         * If A = 0 then this turns off the audio output
!       STA     ,Y+                 * setup the correct MMU page selection to match where the samples are in RAM
        BNE     <
        STX     LoadAudio1+1        * Save the Audio playback start point in the playback routine memory directly
        LDX     #FIRQ_PlaySoundOnce * Playback sample once (no loop)
        STX     FIRQ_Jump_position+1    * Update the FIRQ Jump address
        PULS    A,X,Y,U,PC          * Done
        LDX     #FIRQ_NoSound       *  Audio off
        STX     FIRQ_Jump_position+1    * Update the FIRQ Jump address
        PULS    A,X,Y,U,PC          * Done
        STA     MMU_Reg_Bank1_3 * Block $6000-$7FFF  Block #3
        STX     LoadAudio2+1        * Save the Audio playback start point in the playback routine memory directly
        STX     SampleStart         * Save it so when the sample player gets to the end of the sample it will load this value and start again
        LDX     #FIRQ_PlaySoundLoop     * Playback sample looping
        STX     FIRQ_Jump_position+1    * Update the FIRQ Jump address
        PULS    A,X,Y,U,PC          * Done
* Table will need these values
* Sample start address and the Bank or Page the sample starts from
*                        Start  MMU Reg
*       Bank,# of Banks,Address,Bank Location
        FCB     $0F,$02,$42,$17,$FF,$AA        ;00 Startup Sound
        FCB     $11,$04,$03,$55,$FF,$A8        ;01 Free Man
        FCB     $00,$00,$00,$00,$FF,$AC        ;02 ?? Sounds like Robotron Tank ??
        FCB     $00,$00,$00,$00,$FF,$AC        ;03 No Sound
        FCB     $00,$00,$00,$00,$FF,$AC        ;04 No Sound
        FCB     $15,$04,$04,$E9,$FF,$A8        ;05 Human falling
        FCB     $19,$02,$4A,$DB,$FF,$AA        ;06 Coin Inserted
        FCB     $00,$00,$00,$00,$FF,$AC        ;07 No Sound
        FCB     $1b,$02,$43,$10,$FF,$AA        ;08 Player or Enemy Death part2 Explosion
        FCB     $1D,$81,$60,$02,$FF,$AB        ;09 Player thrust
        FCB     $1E,$01,$66,$D3,$FF,$AB        ;10 Enemy/player teleports on screen
        FCB     $1F,$04,$00,$05,$FF,$A8        ;11 Player Fire
        FCB     $00,$00,$00,$00,$FF,$AC        ;12 No Sound - After death
        FCB     $00,$00,$00,$00,$FF,$AC        ;13 No Sound
        FCB     $23,$01,$6F,$AC,$FF,$AB        ;14 Player or Enemy Death/Smart Bomb
        FCB     $00,$00,$00,$00,$FF,$AC        ;15 No Sound
        FCB     $24,$81,$60,$0E,$FF,$AB        ;16 Play Thrust slow down sound
        FCB     $25,$01,$61,$5E,$FF,$AB        ;17 Lander & Humanoid turn into Mutant
        FCB     $00,$00,$00,$00,$FF,$AC        ;18 ??? Sounds like Joust ???
        FCB     $26,$01,$71,$FA,$FF,$AB        ;19 Swarmer shooting
        FCB     $27,$01,$6C,$95,$FF,$AB        ;20 Human picked up
        FCB     $28,$03,$36,$A3,$FF,$A9        ;21 Player 1 or 2 Button Pushed
        FCB     $2B,$04,$00,$01,$FF,$A8        ;22 Mutant attacking
        FCB     $2F,$01,$7A,$A7,$FF,$AB        ;23 Picked up human in the air/Got 500
        FCB     $30,$01,$66,$B9,$FF,$AB        ;24 Shot the Baiter or swarmer
        FCB     $31,$01,$72,$1D,$FF,$AB        ;25 Play hit Alien Ship
        FCB     $32,$04,$04,$C6,$FF,$A8        ;26 Shot the Pod
        FCB     $00,$00,$00,$00,$FF,$AC        ;27 No Sound
        FCB     $36,$01,$7C,$0B,$FF,$AB        ;28 Enemy fire
        FCB     $00,$00,$00,$00,$FF,$AC        ;29 No Sound
        FCB     $37,$01,$63,$85,$FF,$AB        ;30 shot the bomber
        FCB     $00,$00,$00,$00,$FF,$AC        ;31 No Sound
This entry was posted in CoCo Programming. Bookmark the permalink.

7 Responses to Defender Conversion for the CoCo 3 – Part 2 – Short and Sweet

  1. thanks glen for another awesome conversion!!!

    if theres any energy left in you, is there a chance you may tackle ?
    a) super pacman (dual 6809)
    b) robotron 2084 (i know there is a blitter there)

    seeing at least one of those should be like a dream come true… may the force be with you!

    • nowhereman999 says:

      Hi Perezernestoj,

      I appreciate you want the games on the CoCo 3, I would love to see them all too… It’s such a huge undertaking to get these old classic games to be run/emulated on the CoCo 3. You must fully understand the hardware of the original game and then figure out where in the code you can make the CoCo look and react the way the original game code expects. That means you must disassemble the original game and break down most of how the code operates. It takes so much time and effort that someone is only going to go through all that trouble if the video game really meant a lot to them. Unfortunately Super Pacman never did much for me. As for Robotron… Well that’s interesting, when I was a puppy I used to always play Defender, my best friend would always play Robotron. I would sometimes play Robotron, but my friend could always kick my butt on the game. I’ve always really liked Robotron but Defender was my favourite. During my research on Defender I’ve learned that Williams used the same hardware for Defender, Robotron and Joust (and others). Those three games are arcade classics for sure. Robotron and the other Williams games also used a blitter as you were aware. I don’t know personally yet but I have heard that when the blitter is used in those games that the CPU is being halted. If that is true then it might be possible to get those games working on a CoCo 3! I did find someone’s disassembly of Robotron and some of the code is re-used in Defender and it is helping me to figure out parts of the Defender game. With all that said, after I finish Defender and I’m able to get it to run as fast as the original (if I’m not too burnt out) I will have a lot of the hardware emulated and I’ll be in a good position to work on Joust and Robotron. I was on a recent CoCo Crew video with Bill Noble and he mentioned that he wanted to convert Robotron to the CoCo 3. So I would definitely not want to step on Bill’s toes. If he wanted any help doing his Robotron conversion I would definitely help anyway I could. Like I said it’s a ton of work to convert the games to work on the CoCo 3 so if someone else wants to do it then I could spend time on other games that I want to see on the CoCo 3. In that case I would probably work on Joust after Defender, which is another big favourite of mine. Of course all this is just speculation at this time. Pac Man burnt me out at the time and I needed time away from doing heavy 6809 coding. I’m a little worried that will happen again with Defender. Then I might not want to work on converting/transcoding anymore games. I’m sorry for the long comment, I just wanted to let you know where I’m coming from and what’s involved in bringing these old arcade games to the CoCo 3.

  2. Ted KC9LKE says:

    I was a component level tech back in the day and a huge fan of Williams hardware, the 6809 and Defender.
    I am looking forward to this very interesting conversion!
    I too enjoy the way you take time to discuss the code and its nuances, great job!

    • nowhereman999 says:

      Hi Ted,
      It’s nice to hear someone is enjoying the blog, thanks for taking the time to let me know. Sounds like you had a cool job back in the day. DId you get to play Defender and other video games all day for free? So you could “Test” that it’s working 100%. 🙂
      Sometimes I think I’m just talking to myself when I write these entries. I like to document the neat things I’m coming across. I think it will be useful for myself to refer to and even re-read. If anyone else can learn or enjoy the info then that is fantastic!

  3. glen

    after i found your website, following the pacman transcode, you became a programming hero!!! 🙂

    i sincerely appreciatte the time and effort put into programming, but also the dedication into documenting your journey into conversion/transcoding.

    maybe i should move my lazy ass and start doing something with super pacman. dual 6809… not easy…

    • nowhereman999 says:

      Hi Perezernestoj,

      Thanks for the kind words, I’m glad you enjoy the blog posts. Super Pacman is going to be difficult with Dual 6809s. I just checked the WIKI and it says both CPUs run at 1.536 MHz, which is slower then the CoCo 3 which is great! One 6809 is for sound. So I would suggest getting the game running without sound and adding sound later using samples. It does sound interesting… I hope you give converting it a try. It really helps improve your assembly “Kung Fu”. If you think I can help for some weird parts let me know… I’ve worked out some weird parts of Defender that I’ll post about once I’ve tested it. Maybe we could use the website to work through it together if you need help on certain parts of the game. Others could help and learn at the same time…

  4. I love that you delve into everything you do, I understand it, I know what you’re talking about, and I enjoy your blogs a lot, keep going that you’re doing well, I love all the coconut tech, I know it in depth, coco3 a little less,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s