Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

#1 2019-12-09 15:25:16

popeyeotaku
New member
Registered: 2019-12-09
Posts: 1

Basic Question - Indexing

i understand this is something really simple that i probably should be able to figure out how to do on my own, but what's the best/fastest way to index into a table on the Gameboy CPU? I'm so used to 6502 development, i can't quite figure out a good way to do it

Offline

 

#2 2019-12-09 19:12:14

Tauwasser
Member
Registered: 2010-10-23
Posts: 160

Re: Basic Question - Indexing

It honestly depends on the table. If you have a table of 1-byte stuff, you can just

Code:

; a indexes into table at tableAddress, whose entries are one byte each
ld hl, tableAddress
add a, l
ld l, a
ld a, h
adc a, 0
ld h, a

and be done with it.

If you need larger stuff, you might want to

Code:

; a indexes into table at tableAddress, whose entries are twelve byte each
ld l, a
ld h, $00
add hl, hl ; * 2
add hl, hl ; * 4
push hl
add hl, hl ; * 8
pop bc
add hl, bc ; * 12
ld de, tableAddress
add hl, de

instead.

Of course there can be faster way, for instance if you have a small table like in the first case above and know the initial addition won't overflow, you simply don't add the carry to h. Or if you have a bigger table, powers of two help to reduce additions to shifts and if the table starts aligned, you can trade additions of the (some multiple of) the index with shifts of the base address to save a multiplication.

Last edited by Tauwasser (2019-12-09 19:16:00)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson