Controller poll ($4016/7 related)

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
Zepper
Formerly Fx3
Posts: 3190
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Controller poll ($4016/7 related)

Post by Zepper » Sat Jan 11, 2014 1:32 pm

When should the controllers be polled? Once per PPU frame, at some specific line?
The test ROM "test_cpu_exec_space_apu.nes" gives an error on $4016/7.

EDIT: the test requires ORing $40 on reads. Now it's ok.

zzo38
Posts: 1050
Joined: Mon Feb 07, 2011 12:46 pm

Re: Controller poll ($4016/7 related)

Post by zzo38 » Sat Jan 11, 2014 5:09 pm

The high three bits of $4016 and $4017 are open bus.

The controller data will be accessed when the strobe is high and then low, so the emulator should read them while the strobe is high, making the data available to the emulated program when it is low.
[url=gopher://zzo38computer.org/].[/url]

User avatar
tokumaru
Posts: 11465
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Controller poll ($4016/7 related)

Post by tokumaru » Sat Jan 11, 2014 5:21 pm

As for WHEN to poll the controllers, that's up to you. On a real NES, the controllers can be read several times per frame and the data returned will always reflect their current state. In an emulator you will most likely read the actual control(er)s once per frame and return that to the game as many times as it requests.

User avatar
Zepper
Formerly Fx3
Posts: 3190
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: Controller poll ($4016/7 related)

Post by Zepper » Sat Jan 11, 2014 6:14 pm

I read about "open bus", but I couldn't take any conclusions on it. I read about "returning the last value written there" or "returning 0xFF", I'm not sure. Usually, the value is (address >> 8).

User avatar
tokumaru
Posts: 11465
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Controller poll ($4016/7 related)

Post by tokumaru » Sat Jan 11, 2014 7:11 pm

The 6502 has a data "bus", which is a set of paths for the data bits to travel. When the CPU reads instructions from ROM, the bytes that make up the instruction travel through this bus, one at a time. If the instruction is "LDA $4016", the CPU will read $AD, $16 and finally $40. So $40 is the last value to travel through the bus before the command is executed. When answering back, the controller uses this same bus to send the data, but it doesn't use all of the lines (there's no need to control all 8 lines if the answer fits in less bits). The lines that are not used simply retain their old values, so the result is a mix of the controller data with the previous value that traveled through he bus. In most cases the old value is the high byte of the address of the register you read from.

User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Re: Controller poll ($4016/7 related)

Post by blargg » Sat Jan 11, 2014 7:31 pm

Code: Select all

ldx #$16 + 1
lda $3fff, x
Clarification: just posting an example of how to have the open-bus value be different than $40, in case it helps someone think more concretely about it.
Last edited by blargg on Sat Jan 11, 2014 7:58 pm, edited 1 time in total.

User avatar
tokumaru
Posts: 11465
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Controller poll ($4016/7 related)

Post by tokumaru » Sat Jan 11, 2014 7:49 pm

blargg wrote:

Code: Select all

ldx #$16 + 1
lda $3fff, x
Yeah, this is why I said "in most cases". If different addressing modes are used to access the registers you might get something other than the high byte of the register's address from open bus, so you shouldn't assume it's always that.

zzo38
Posts: 1050
Joined: Mon Feb 07, 2011 12:46 pm

Re: Controller poll ($4016/7 related)

Post by zzo38 » Sun Jan 12, 2014 9:16 pm

blargg wrote:

Code: Select all

ldx #$16 + 1
lda $3fff, x
I think according to how the 6502 instruction set works, in such a case it will first read without carrying, so from $3F16 (a mirror of the PPU address register; I don't know what happens when you read that register), and then it will try again, this time using the correct address.

Also, on RF Famicom (and others that use the same internal logic; I don't know if Famicom Titler does), the high five bits of $4016 are open bus; on all other systems, it is the high three bits.
[url=gopher://zzo38computer.org/].[/url]

Zelex
Posts: 267
Joined: Fri Apr 29, 2011 9:44 pm

Re: Controller poll ($4016/7 related)

Post by Zelex » Sun Jan 12, 2014 11:50 pm

Do any games rely on the true open bus behavior? If so, which ones?

User avatar
freem
Posts: 162
Joined: Mon Oct 01, 2012 3:47 pm
Location: freemland (NTSC-U)
Contact:

Re: Controller poll ($4016/7 related)

Post by freem » Mon Jan 13, 2014 12:07 am

According to the wiki, Paperboy does. Other games might; I'm not sure.

zzo38
Posts: 1050
Joined: Mon Feb 07, 2011 12:46 pm

Re: Controller poll ($4016/7 related)

Post by zzo38 » Mon Jan 13, 2014 1:51 am

freem wrote:According to the wiki, Paperboy does. Other games might; I'm not sure.
It says: "Paperboy relies on this behavior and requires that reads from the controller ports return exactly $40 or $41 as appropriate." It seems to me, this means it only cares when actually reading like LDA $4016, so it isn't the complete use of the open bus.

However, if writing an emulator you should emulate the full behavior of the open bus, even if nothing known currently uses it. Doing so would improve the accuracy, and may also be useful in future.
[url=gopher://zzo38computer.org/].[/url]

User avatar
Zepper
Formerly Fx3
Posts: 3190
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: Controller poll ($4016/7 related)

Post by Zepper » Mon Jan 13, 2014 6:33 am

A test ROM would be nice. ;)

Post Reply