[stella] programming teaser

Subject: [stella] programming teaser
From: "Andrew Davie" <atari2600@xxxxxxxxxxxxx>
Date: Fri, 20 May 2005 05:48:28 -0400
As promised, here is a programming teaser.  Explain how the following code 
could ever exit?

loop
        jsr Tricky            ; do we ever return from this subroutine?
        inc colour            ; yes!  but how?!!
        lda colour
        sta COLUBK
        jmp loop



Tricky

        ldx #$FF                                            ; 'character' 
line number

Continue

        inx
        stx SET_BANK_RAM                ; bankswitch to appropriate RAM bank 
for shape data

        ldy #10                        ; draw scanlines in groups of 21 (ie: 
1 character row)

anotherLine
            ; draw a scanline here (code omitted)

        sta WSYNC

        dey
        bne anotherLine
        bmi Continue


This is a conceptual version of the actual kernel from notBoulderDash.  I 
can assure you, the background colour will flash quite merrily, proving that 
the Tricky subroutine does actually return.  There is a subtle trick 
operating here which means that this is actually a completely valid kernel 
which will exit after drawing an appropriate number of scanlines (in my 
case, it's currently 168).

Think a bit laterally... can anyone explain how it might be possible for 
code like the above to do its work and actually return to the caller?  You 
will need to make an assumption that is not entirely clear.  Can anyone 
figure this out?

Cheers
A

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

Current Thread