Page 1 of 1

Neat little optimize trick

PostPosted: Sat Nov 04, 2017 12:25 pm
by nowhereman999
I was playing with Ed Snider's CoCo PSG. The CoCo PSG has a built in audio out jack which is great but I wanted to output the sound from his cartridge to the regular outputs on the CoCo (RF and Audio out on the CoCo3). According to the CoCo PSG User's Manual it does support outputting sound through the regualr CoCo audio output ports so I was looking through some documents to set the source to the cartridge port by doing the following:
Code: Select all
* Setup Cartridge as the sound source
* $FF03 bit 3 $FF01 Bit 3 Sound Source
*     0           0  DAC
*     0           1  Cassette
*     1           0  Cartridge
*     1           1  No Sound
* 1 bit audio output is not included here and can be used independantly of the above sources

        LDA     $FF03           * PIA 0 side B control reg - PIA0BC
        ORA     #%00001000      * Bit 3 is the Select Line MSB of MUX set it to 1 for Cartridge source select
        STA     $FF03           * Save settings
        LDA     $FF01           * PIA 0 side A control reg - PIA0AC
        ANDA    #%11110111      * Bit 3 is the Select Line LSB of MUX set it to 0 for Cartridge source select
        STA     $FF01           * Save settings

But it still wasn't working, so I was looking though the Basic unravelled series to see how basic's AUDIO ON works and I see that I also had to enable sound output with this little bit of code:
Code: Select all
* Also must enable sound output
        LDA     $FF23           * PIA 1 side B control reg - PIA1BC
        ORA     #%00001000      * Set bit 3 to enable sound output
        STA     $FF23           * Save settings

Which is now working fine, but as I was reading the unravelled series I noticed a nice little trick in this bit of code:
Code: Select all
LA974     CLRA
          FCB $8C
LA976     LDA  #8
          STA ,-S

This is a cool little trick, if you want to set A to 0 then jump to LA974 if you want A to equal 8 jump to LA976. You don't need to have a BRA after the CLRA instruction if you use FCB $8C, before today I would have written the same bit of code in a longer method as:
Code: Select all
LA974     CLRA
          BRA  Skip
LA977     LDA  #8
Skip      STA ,-S

You don't have to only use CLRA it will work if you do a LDA #xx then FCB $8C to skip over the next LDA instruction. The BASIC unravelled version saves a byte of space and probably a couple of CPU cycles too. I thought it was neat and I added this to my Assembly Optimization Blog found here:


Re: Neat little optimize trick

PostPosted: Sat Nov 04, 2017 8:04 pm
by Bosco
Hi Glen.

So in this example you've inserted opcode $8C (CMPX immediate addressing mode) meaning the following two bytes, LDA #08, are interpreted as the operand.

That's an interesting memory optimisation I hadn't considered before although I should say CMPX #$xxxx takes 4 cycles whereas BRA Skip is only 3, so unfortunately not a performance saving. :(

That said, when clearing the 16-bit accumulator I generally use CLRA+CLRB to save a memory byte and LDD #$0000 only when cycle count is critical eg. during render routines.

Saving a byte doesn't sound a lot but it soon mounts up. A friend of mine recently reminded me of the importance of using DP addressing for things like game variables etc. Not only is it faster but he calculated he had saved approx. 600 bytes just by using DP instead of Extended addressing, which on a 32k total machine like the Dragon is quite significant.


Re: Neat little optimize trick

PostPosted: Sun Nov 05, 2017 12:43 am
by nowhereman999
Hi Bosco,

Yeah you're correct it doesn't actually make it faster but slower but if you need to squeeze your code into a certain size it is a neat little trick. Yeah using the DP can really help speed wise and space too.