Initial VBL loop in Donkey Kong

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
blkhp19
Posts: 3
Joined: Mon Feb 20, 2017 1:39 pm

Initial VBL loop in Donkey Kong

Post by blkhp19 »

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

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

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

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.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Initial VBL loop in Donkey Kong

Post by lidnariq »

In the NES, addresses from $2000-$5BFF are never random-access memory, but instead MMIO
blkhp19
Posts: 3
Joined: Mon Feb 20, 2017 1:39 pm

Re: Initial VBL loop in Donkey Kong

Post by blkhp19 »

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

Re: Initial VBL loop in Donkey Kong

Post by tepples »

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?
blkhp19
Posts: 3
Joined: Mon Feb 20, 2017 1:39 pm

Re: Initial VBL loop in Donkey Kong

Post by blkhp19 »

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?
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Initial VBL loop in Donkey Kong

Post by Quietust »

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.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Initial VBL loop in Donkey Kong

Post by tokumaru »

Very few games rely on open bus, AFAIK, but you should eventually implement it if accuracy is one of your goals.
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: Initial VBL loop in Donkey Kong

Post by Zepper »

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