CPCWiki forum

General Category => Programming => Topic started by: trabitboy on 16:04, 15 September 16

Title: trying not to use IX
Post by: trabitboy on 16:04, 15 September 16
Hi,
I am now reasonably "fluent" in z80 and I thought I would have a quick look at the gameboy spec ( bouuuuuuuuuuuu ).
I just realized there is no IX !

I use it quite a lot to curse data structures such as

; baddie address is in HL
push HL
pop IX
ld A, (IX+2) ; say ... baddie health
dec A
ld (IX+2),A

what would be the optimal way to avoid use of IX ?

I can imagine "cursing forward" HL using :
; baddie address is in HL
push HL
ld B,0
ld C,2; say ... baddie health
add HL,BC
ld A, (HL)
dec A
ld (HL),A
pop HL

Any other optimised/readable recipes?
Title: Re: trying not to use IX
Post by: Bryce on 16:19, 15 September 16
Maybe you should take a look at some disassembled GB code to see how the commercial GB coders did it?

Bryce.
Title: Re: trying not to use IX
Post by: andycadley on 16:34, 15 September 16
Cursing forward with HL (using INC H or INC L where possible) is a pretty standard way of doing things even in "real" Z80 as it's often faster than the indexed instructions.
Title: Re: trying not to use IX
Post by: Docent on 16:39, 15 September 16
Hi,
I am now reasonably "fluent" in z80 and I thought I would have a quick look at the gameboy spec ( bouuuuuuuuuuuu ).
I just realized there is no IX !

I use it quite a lot to curse data structures such as

; baddie address is in HL
push HL
pop IX
ld A, (IX+2) ; say ... baddie health
dec A
ld (IX+2),A

what would be the optimal way to avoid use of IX ?

I can imagine "cursing forward" HL using :
; baddie address is in HL
push HL
ld B,0
ld C,2; say ... baddie health
add HL,BC
ld A, (HL)
dec A
ld (HL),A
pop HL

Any other optimised/readable recipes?

You can use Gameboy cpu specific instructions:
ldi a,(hl) - loads a from (hl) and increments hl
ldi (hl),a
ldd a,(hl) - loads a from (hl) and decrements hl

bear in mind that Sharp LR35902 (Gameboy cpu) has many other differences:
- no alternate register set
- no exchange instructions
- no instructions with DD,ED,FD prefix (ie - no ix,iy related instructions, no 16bit operations like adc hl,bc or  ld bc,(nn))
- no conditionals on parity/sign/overflow
- no in/out
- djnz replaced with stop
Some instructions have different opcodes, there are also a few new instructions.

Here's the list of mnemonics, you can see the difference:)
Gameboy (LR35902) OPCODES (http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html)
Title: Re: trying not to use IX
Post by: trabitboy on 16:55, 15 September 16
well lots of good answers thant you very much !   8)
Title: Re: trying not to use IX
Post by: Prodatron on 17:30, 15 September 16
IMHO the Gameboy CPU is much closer to the 8080 than to the Z80, but it's still using the Zilog-way for writing the mnemonics.
But maybe it still may help to study some 8080 source codes.

[...]
ld A, (HL)
dec A
ld (HL),A
[...]
Any other optimised/readable recipes?

You can replace this with DEC (HL)
Title: Re: trying not to use IX
Post by: Axelay on 17:44, 15 September 16
Page aligned tables can help with using hl, then as andycadley says, an 8 bit inc or dec on l can be used to traverse it.


Also grouping the enemy data in blocks of related information, if it suits your structures.  So if you have a table of 16 enemies, you might start a table with just the x & y co-ordinates at &8000, then at &8020 you could have the enemy hits and some other related data byte taking the next 32 bytes.  Now checking the collisions need only take 2 incs or decs per baddie traversing through the first table starting at &8000, and if a collision is detected you only need use a set 5,l to get to the hits for the relevant enemy in the table starting at &8020, and a res 5,l to get hl pointing back to the co-ordinates.
Title: Re: trying not to use IX
Post by: Docent on 19:21, 15 September 16
IMHO the Gameboy CPU is much closer to the 8080 than to the Z80, but it's still using the Zilog-way for writing the mnemonics.
But maybe it still may help to study some 8080 source codes.
It will be waste of time - by default 8080 mnemonics use completely different naming convention, eg.
mov a,b -> ld a,b
ldax b -> ld a, (bc)
while almost every source for Gameboy is based on z80 mnemonics
Title: Re: trying not to use IX
Post by: trabitboy on 10:24, 16 September 16
I am looking at this relatively recent homebrew :
GitHub - furrtek/Airaki: Tile-matching RPG puzzle game for the Nintendo GameBoy (https://github.com/furrtek/Airaki)

I am humbled by the level of the answers in this thread ( particularly astonished by the solution of axelay ), and I thank you again !!!!
Title: Re: trying not to use IX
Post by: AMSDOS on 11:47, 16 September 16
A game boy coder would be coding it all in Assembly. IndeX Registers become more useful when handling code between Assembly and another Language, it can be used In-house with Assembly, though it's possible to use your other registers. I think when I've seen it being used in Assembly, it's usually to hold some value from another Register Pair you need to use while avoiding the use of PUSH & POP (which are also slow).