It is currently Tue Sep 25, 2018 3:54 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sat Jun 09, 2018 4:37 am 
Offline

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


Top
 Profile  
 
PostPosted: Sat Jun 09, 2018 6:18 am 
Offline

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


Top
 Profile  
 
PostPosted: Sat Jun 09, 2018 6:31 am 
Offline

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


Top
 Profile  
 
PostPosted: Sat Jun 09, 2018 6:49 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20574
Location: NE Indiana, USA (NTSC)
$19 plus $E7 is $100, which wraps to $00. Wraparound of unsigned integer types (such as uint8_t) is well-defined in C.


Top
 Profile  
 
PostPosted: Sat Jun 09, 2018 7:02 am 
Offline

Joined: Sat Jun 09, 2018 4:29 am
Posts: 3
I see, but it seems strange that I'd need to fill out every single byte by myself. Am I missing something?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group