nesdev.com
http://forums.nesdev.com/

Initial VBL loop in Donkey Kong
http://forums.nesdev.com/viewtopic.php?f=3&t=15561
Page 1 of 1

Author:  blkhp19 [ Tue Feb 21, 2017 12:17 am ]
Post subject:  Initial VBL loop in Donkey Kong

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.

Author:  lidnariq [ Tue Feb 21, 2017 12:43 am ]
Post subject:  Re: Initial VBL loop in Donkey Kong

In the NES, addresses from $2000-$5BFF are never random-access memory, but instead MMIO

Author:  blkhp19 [ Tue Feb 21, 2017 10:31 am ]
Post subject:  Re: Initial VBL loop in Donkey Kong

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?

Author:  tepples [ Tue Feb 21, 2017 10:47 am ]
Post subject:  Re: Initial VBL loop in Donkey Kong

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?

Author:  blkhp19 [ Thu Feb 23, 2017 10:51 pm ]
Post subject:  Re: Initial VBL loop in Donkey Kong

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?

Author:  Quietust [ Fri Feb 24, 2017 5:49 am ]
Post subject:  Re: Initial VBL loop in Donkey Kong

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.

Author:  tokumaru [ Fri Feb 24, 2017 7:56 am ]
Post subject:  Re: Initial VBL loop in Donkey Kong

Very few games rely on open bus, AFAIK, but you should eventually implement it if accuracy is one of your goals.

Author:  Zepper [ Fri Feb 24, 2017 8:06 am ]
Post subject:  Re: Initial VBL loop in Donkey Kong

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).

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/