It is currently Mon Jul 23, 2018 9:02 am

 All times are UTC - 7 hours

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Posted: Sun May 17, 2015 2:39 pm

Joined: Sun May 17, 2015 2:14 pm
Posts: 6
Location: seattle
Hi, I have a question about the logic in one of the blargg's tests in 01 - special. Test #5: POP AF has some logic that I don't quite understand.

First let's look at the source for that test:
Code:
-   PUSH BC           ; save bc on the stack
POP AF            ; pop the stack into af (effectively AF = BC)
PUSH AF           ; push AF onto the stack
POP DE            ; pop the stack into DE (effectively DE = AF (= BC))
LD A,C            ; load C into A
AND \$FO           ; only keep the high 4 bits of A
CP E              ; compare E to A
JP NZ,test_failed ; if they're not equal, the test is failed
INC B             ; otherwise, increment B
INC C             ; and C
JR NZ,-           ; and do it again until C = \$00

Now, I think I understand what the operations of the test are, but I would imagine this to always fail for the second iteration (BC = \$1301) because of this:

Code:
LD A,C   ; load \$01 to A
AND \$F0  ; keep only high bits (\$01 -> \$00)
CP E     ; compare A (\$00) with E (\$01) -> not same, test failed

I feel like there's something I'm missing, but I'm not sure what it is. I've been pouring over any documents I can find, but haven't found anything that would suggest different behavior from what I expect. Can anyone shed some light on this?

_________________
My first gameboy emulator

Top

 Posted: Sun May 17, 2015 3:26 pm

Joined: Sun Jan 26, 2014 9:31 am
Posts: 289
Let me try to remember... I believe that you can only write to the top 4 bits of the F register. It only holds 4 flags, the rest of the bits are supposed to go unused. When performing a POP AF instruction, bits 0 - 3 are ignored/masked out. Therefore:

Code:
PUSH BC       ;BC being 1301, B hold \$13, C holds \$1
POP AF        ;Basically stores B into A, and C into F
;F ignores \$1, however, so now you have AF = \$1300
PUSH AF       ;Puts \$1300 on the stack
POP DE        ;Pulls \$1300 from the stack, with E being \$0, D being \$13
LD A, C       ;Load \$1 into A
AND \$F0       ;Masks lower nibble of A, \$A is now \$0
CP E          ;Both A and E are \$0, so the zero flag is set

I recall that being a bit of a head-scratching moment myself when I was writing my first emulator. That should be what the test is looking for. It's been a while, but that's what I can pull of the top of my head.

Top

 Posted: Sun May 17, 2015 9:32 pm

Joined: Sun May 17, 2015 2:14 pm
Posts: 6
Location: seattle
Ahh!! That's it! Thank you. That makes a lot more sense now.

_________________
My first gameboy emulator

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC - 7 hours

Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki