Problems with GB emulator

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Problems with GB emulator
by on (#219560)
Hi! I've been boring on a Game Boy emulator to learn more about how CPUs work on a low level. I'm using the DMG_ROM.bin for testing my emulator, and for the most part, it's working.

I'm gonna post the final lines of code I've been having issues with below.

Code:
Addr_00F4:
   ADD (HL)      ; $00f4
   INC HL      ; $00f5
   DEC B         ; $00f6
   JR NZ, Addr_00F4   ; $00f7
   ADD (HL)      ; $00f9
   JR NZ,$fe      ; $00fa   ; if $19 + bytes from $0134-$014D  don't add to $00
                  ;  ... lock up


The main issue is the last two lines. It checks if $19 + the values at memory address $0134-$14D == $00.
This really confuses me, as I'm no sure how two positive values added together can result in a 0.
Even if I assigned some values to those memory addresses, the ZERO flag would still not be set, hence it would get stuck in an infinite loop at the last line.

I've debugged and saw that the B register was indeed decrementing fine, and was set to 0 at the last loop, hence it was able to go on to the last `ADD (HL)`

If any of you have any ideas, please let me know! I'm at a total loss.
Here's the link for where I found the disassembled code: http://gbdev.gg8.se/wiki/articles/Gameboy_Bootstrap_ROM
Re: Problems with GB emulator
by on (#219568)
Addition on an 8-bit CPU is modulo 256. This means, for example, that $FF + $01 = $00. So is $C4 + $3C. An addition whose result is 256 will set both the Z flag and the C flag.
Re: Problems with GB emulator
by on (#219569)
Seems weird, as I have looked at other emulators' source code, and the integer is only 8 bits (unsigned). If I made the bytes $FF, it would overflow, since A is already $19.
Re: Problems with GB emulator
by on (#219571)
$19 plus $E7 is $100, which wraps to $00. Wraparound of unsigned integer types (such as uint8_t) is well-defined in C.
Re: Problems with GB emulator
by on (#219573)
I see, but it seems strange that I'd need to fill out every single byte by myself. Am I missing something?