Problems with GB emulator

Discussion of programming and development for the original Game Boy and Game Boy Color.
Post Reply
Alyrikki256
Posts: 4
Joined: Sat Jun 09, 2018 4:29 am

Problems with GB emulator

Post by Alyrikki256 »

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: Select all

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
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Problems with GB emulator

Post by tepples »

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.
Alyrikki256
Posts: 4
Joined: Sat Jun 09, 2018 4:29 am

Re: Problems with GB emulator

Post by Alyrikki256 »

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.
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Problems with GB emulator

Post by tepples »

$19 plus $E7 is $100, which wraps to $00. Wraparound of unsigned integer types (such as uint8_t) is well-defined in C.
Alyrikki256
Posts: 4
Joined: Sat Jun 09, 2018 4:29 am

Re: Problems with GB emulator

Post by Alyrikki256 »

I see, but it seems strange that I'd need to fill out every single byte by myself. Am I missing something?
Post Reply