Initial VBL loop in Donkey Kong

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Initial VBL loop in Donkey Kong
by on (#189558)
I'm trying to figure out why my emulator and every other emulator I've tried behave differently during the first few instructions of Donkey Kong (Japan) being executed.

Code:
;Disable interrupts
C79E : 78         sei         ;
;Clear the CPU's decimal flag
C79F : D8         cld         ;
;Reset PPU control (including the disabling of VBL interrupts)
C7A0 : A9 10      lda   #$10      ;
C7A2 : 8D 00 20      sta   $2000      ;
;Init stack pointer
C7A5 : A2 FF      ldx   #$FF      ;
C7A7 : 9A         txs            ;
;Wait for VBL, looping until it happens
C7A8 : AD 02 20      lda   $2002      ;
C7AB : 29 80      and   #$80      ;
C7AD : F0 F9      beq   $C7A8      ;


Nintendulator initializes the PPU registers with 0xFF. When it does
Code:
C7A0 : A9 10      lda   #$10      ;
C7A2 : 8D 00 20      sta   $2000      ;

One would expect $2000 to have the value 0x10 in it. This isn't reflected in the CPU memory viewer.

Next, when it does
Code:
C7A8 : AD 02 20      lda   $2002      ;

I would expect 0xFF to be loaded into the accumulator (since $2002 is initialized with 0xFF in this emulator. That's not the case. It actually loads the value 0x10 into the accumulator. Where does this value come from?

In my emulator, my memory is initialized to 0x00, so when it does the load from $2002, I load the value 0x00 into the accumulator.
Re: Initial VBL loop in Donkey Kong
by on (#189559)
In the NES, addresses from $2000-$5BFF are never random-access memory, but instead MMIO
Re: Initial VBL loop in Donkey Kong
by on (#189614)
lidnariq wrote:
In the NES, addresses from $2000-$5BFF are never random-access memory, but instead MMIO


That does make sense, although even FCEUX seems to show the last value written just to make it easier to see what's going on (as does my emulator).

I'm still unsure about how $2002 = 0x10. It never gets set to that, so how does it get that value?
Re: Initial VBL loop in Donkey Kong
by on (#189621)
Data bus capacitance inside the PPU has the effect of briefly storing the last value written to or read from any PPU port ($2000-$3FFF) in a dynamic latch. This dynamic latch, called _io_db in Visual 2C02 and PPUGenLatch in FCEUX, can be read through any of the nominally read-only ports ($2000, $2001, $2003, $2005, $2006) as well as the low 5 bits of $2002. For example, writing $69 to $2007 sets the value of this latch to $69 (%01101001), and then reading it back through $2002 may return $09, or $89 if vblank just started, or $49 after a sprite 0 hit, etc.

See also other posts mentioning PPUGenLatch: Riding the open bus; Can I use PPUGenLatch's value again?
Re: Initial VBL loop in Donkey Kong
by on (#189865)
tepples wrote:
Data bus capacitance inside the PPU has the effect of briefly storing the last value written to or read from any PPU port ($2000-$3FFF) in a dynamic latch. This dynamic latch, called _io_db in Visual 2C02 and PPUGenLatch in FCEUX, can be read through any of the nominally read-only ports ($2000, $2001, $2003, $2005, $2006) as well as the low 5 bits of $2002. For example, writing $69 to $2007 sets the value of this latch to $69 (%01101001), and then reading it back through $2002 may return $09, or $89 if vblank just started, or $49 after a sprite 0 hit, etc.

See also other posts mentioning PPUGenLatch: Riding the open bus; Can I use PPUGenLatch's value again?



Thanks for this, I had no idea about this phenomena. Is this something I need to implement in my emulator to get something basic like Donkey Kong working?
Re: Initial VBL loop in Donkey Kong
by on (#189876)
blkhp19 wrote:
Thanks for this, I had no idea about this phenomena. Is this something I need to implement in my emulator to get something basic like Donkey Kong working?

For basic games? No - you just need to properly implement the PPU and its I/O interface to the CPU.
Re: Initial VBL loop in Donkey Kong
by on (#189882)
Very few games rely on open bus, AFAIK, but you should eventually implement it if accuracy is one of your goals.
Re: Initial VBL loop in Donkey Kong
by on (#189884)
You need a decent CPU code in order to get the things working right, and a basic PPU rendering engine to see the outputs. PPU registers are VERY important - I'd say $2007 + loopy's logic is the second heart (first is the CPU).