Search code examples
if-statementmachine-codegameboy

gbz80 - IF statements


I'm doing some coding in Z80 on VisualboyAdvance and I have no idea how to do an IF statement of any kind.

I made a script to increment the value of each tile on screen starting at 0xC3A0. The screen data ends at 0xC507.

My code is as follows:

    d322|21A0C3|LD HL,C3A0h
    d325|34    |INC (HL)
    d326|23    |INC HL
    d327|00    |NOP
    .
    .
    .
    d340|00    |NOP
    d341|C325D3|JP D325h

As you can see, it loops constantly and eventually crashes the game by incrementing core functions. I'd like a way to have it so it uses 0xC9 (Ret) upon HL reaching 0xC507.

Any help would be much appreciated.

And if you could explain to me how the statements you provide work, that'd be great. Machine code is foreign to me.


Solution

  • Do NOT use Visual Boy Advance.

    Its Game Boy emulation accuracy is very bad, especially the memory access timing which is a big problem for many commercial ROMs: http://gbdev.gg8.se/wiki/articles/Test_ROMs#Emulators_running_on_desktop_computers

    It has also many security vulnerabilities. Stack buffer overflows that let the attacker take control of your machine. Here's one of these: https://youtu.be/L-L8qWpd_74

    Use BGB for your debugging needs, it features an excellent debugger and its accuracy is very good.

    The condition to exit the loop must be done on the jump instruction, like this:

    01 67 01         ld   bc,0167            ;0xC507 - 0xC3A0
    21 A0 C3         ld   hl,C3A0            ;start of the area we wish to increment
    :loop_start
    34               inc (hl)                ;(hl) ++
    23               inc hl                  ;hl ++
    0B               dec  bc                 ;bc --
    78               ld   a,b
    B1               or   c
    20 F9            jr   nz,[loop_start]    ;if(bc ≠ 0), loop to :loop_start
    ...