Look-up Tables

From MegaDrive Wiki
Jump to: navigation, search

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.

  1. Magic_JMP:
  2. ;==============================================================================
  3. ; INPUT:	d7.13-0 =	Jump Index
  4. ;==============================================================================
  5.  	move.l	(a7)+,a6	; Pull address from stack.
  6.  	lsl.w	#2,d7		; Index --> 16-bit Offset.
  7.  	move.l	0(a6,d7.w),a6	; Get jump address.
  8.  	lsr.w	#2,d7		; Restore Index.
  9.  	jmp	(a6)

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:

  1. 	jsr	Magic_JMP
  2. 	dc.l	$1000, $2000, $3000, $4000
  • If d7 = 0, it jumps to $001000.
  • If d7 = 1, it jumps to $002000.
  • If d7 = 2, it jumps to $003000.
  • If d7 = 3, it jumps to $004000.

Magic Branch

A "magic branch" is similar to the magic jump, except the pointers are relative instead of absolute. The value of the pointer is added to the address of the first entry into the table. To use, load an index into a register, call the subroutine, and then put a jump table immediately after the routine, just like before. The program then jumps to the chosen routine.

  1. Magic_BRA:
  2. ;==============================================================================
  3. ; INPUT:	d7.13-0 =	Jump Index
  4. ;==============================================================================
  5. 	move.l	(a7)+,a6	; Pull address from stack.
  6. 	move.w	sr,-(a7)	; Save SR.	
  7. 	add.w	d7,d7		; Index --> 16-bit Offset.
  8. 	add.w	0(a6,d7.w),a6	; Get jump address.
  9. 	lsr.w	#1,d7		; Restore Index.
  10. 	move.w	(a7)+,sr	; Restore SR.
  11. 	jmp	(a6)

This routine accepts a 15-bit jump index in register d7. The address to jump to ends up in register a6. Each entry in the jump table is 16-bits long, and the values are signed, making the branch range from -32,768 to +32,767 bytes. For example:

  1. 	jsr	Magic_BRA
  2. 	dc.l	$1000, $F000, $7000

Assume that the jsr instruction appears at ROM address $007A24. Since a JSR is 6 bytes long, the address $007A24 + $6 = $007A2A is pushed onto the stack. Therefore, all addresses are relative to $007A2A.

  • If d7 = 0, it jumps to $007A2A + $1000 = $008A2A.
  • If d7 = 1, note that $F000 = -$1000. $007A2A - $1000 = $006A2A.
  • If d7 = 2, it jumps to $007A2A + $7000 = $00EA2A.