[stella] Lord of the Rings optimizations

Subject: [stella] Lord of the Rings optimizations
From: Thomas Jentzsch <tjentzsch@xxxxxx>
Date: Mon, 17 Dec 2001 19:11:11 +0100
Hi,
now, this is something for the hard core coders! (like me :-)

While disassembling LOTR I found an interesting way of coding.

Whenever the programmer wanted to check, if a game has run through a
special code point, he placed a BRK there followed by an id-byte.

Example: (checks, if the code has to set bit 2 of $e3)
    lda    $e3
    and    #$04
    bne    Lb568
    brk
    .byte $0e        ; id-byte
    lda    $e3       ; <- here we will continue
    ora    #$04
    sta    $e3
Lb568:

Now something very interesting is happening. The break-vector
(indirectly) points to this routine:
    plp             ; remove flags from stack (not needed)
    tsx             ; load x with stackpointer
    inx             ; x++
    dec    $00,x    ; adjust return address
    lda    ($00,x)  ; read break-id...
    tay             ; ...and store in y
Subroutine:
    ldx    #$00     ; find variable with lower value
    lda    $ef
    cmp    $f0
    bcc    Lbed3
    inx
Lbed3:
    tya
    cmp    $ef,x    ; compare break-id with the lower value of $ef/$f0
    bcc    .exit    ; if smaller
    beq    .exit    ;  or equal then exit
    
    sty    $ef,x    ; overwrite old id with larger new id
    lda    Lb36e,y  ; load some tables values into some registers
    sta    $f1,x
    lda    Lb37e,y
    sta    $f3,x
.exit:
    rts             ; the code returns two bytes behind the breakpoint

First I thought, this was debugging code, but then I found the registers
filled in that routine are used for... SOUND!
(Each sound has it's own priority and only the highest two are played.)

So this is another (rather complicated) way for something like:
   ldy     #value
   jsr     Subroutine

But it saves 3 bytes with each call! And the overhead is only 8 bytes,
so after only 3 subroutine calls (LOTR has about 20) you are winning
ROM space! Cool, isn't it?

Have fun!
Thomas                            
_______________________________________________________
Thomas Jentzsch         | *** Every bit is sacred ! ***
tjentzsch at web dot de |

----------------------------------------------------------------------------------------------
Archives (includes files) at http://www.biglist.com/lists/stella/archives/
Unsub & more at http://www.biglist.com/lists/stella/


Current Thread