Look-up Tables
Look-up tables are used often in Mega Drive programmes and even outside of the Mega Drive to either accelerate some operation, or to provide an easy way to store information that may need to be accessed by an index. An example of this could be found in a game's sound driver — instead of having to give it a pointer to the song or sound effect every time you want to play it, instead you could provide an index into a table, from which it would lift the appropriate address.
Magic Jump
A "magic jump" is a kind of dynamic jump where you load an index into a register, call the subroutine, and then put a jump table immediately after the routine. The program then jumps to the chosen routine. <source lang="asm" line="GESHI_FANCY_LINE_NUMBERS"> Magic_JMP:
- ==============================================================================
- INPUT
- d7.13-0 = Jump Index
- ==============================================================================
move.l (a7)+,a6 ; Pull address from stack. lsl.w #2,d7 ; Index --> 16-bit Offset. move.l 0(a6,d7.w),a6 ; Get jump address. lsr.w #2,d7 ; Restore Index. jmp (a6)
</source>
This routine accepts a 14-bit jump index in register d7. The address to jump to ends up in register a6. Each entry in the jump table is 32-bits long. For example: <source lang="asm" line="GESHI_FANCY_LINE_NUMBERS"> jsr Magic_JMP dc.l $1000, $2000, $3000, $4000 </source>
- If d7 = 0, it jumps to $1000.
- If d7 = 1, it jumps to $2000.
- If d7 = 2, it jumps to $3000.
- If d7 = 3, it jumps to $4000.