Lode Runner: Apple II reverse engineering

2. Programming techniques#

2.1 Zero page temporaries#

Zero-page consists essentially of global variables. Sometimes we need local temporaries, and Lode Runner mostly doesn't use the stack for those. Rather, some "global" variables are reserved for temporaries. You might see multiple symbols equated to a single zero-page location. The names of such symbols are used to make sense within their context.

2.2 Tail calls#

Rather than a JSR immediately followed by an RTS, instead a JMP can be used to save stack space, code space, and time. This is known as a tail call, because it is a call that happens at the tail of a function.

2.3 Unconditional branches#

The 6502 doesn't have an unconditional short jump. However, if you can find a condition that is always true, this can serve as an unconditional short jump, which saves space and time.

2.4 Stretchy branches#

6502 branches have a limit to how far they can jump. If they really need to jump farther than that, you have to put a JMP or an unconditional branch within reach.

2.5 Shared code#

To save space, sometimes code at the end of one function is also useful to the next function, as long as it is within reach. This can save space, at the expense of functions being completely independent.

2.6 DOS#

Since programs generally come on disk, and such disks are genrally bootable, the only thing the disk card does is load the data of the disk on track 0 sector 0 to location 0800 and then jump to it. Thus, any additional services need to be supplied by the disk as a disk operating system. The most popular ones were DOS 3.3 and ProDOS.

Lode Runner contains just the parts of DOS 3.3 it needs. See the section on Disk routines for more information.

2.7 Temporaries and scratch space#

⟨defines [2]⟩=
TMP_PTR             EQU     $0A     ; 2 bytes
TMP                 EQU     $1A
SCRATCH_5C          EQU     $5C
MATH_TMPL           EQU     $6F
MATH_TMPH           EQU     $70
TMP_LOOP_CTR        EQU     $88
SCRATCH_A1          EQU     $A1
Used in ⟨*⟩
Defines MATH_TMPH, MATH_TMPL, SCRATCH_5C, SCRATCH_A1, TMP, TMP_LOOP_CTR, TMP_PTR