Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

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

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by tokumaru » Thu Apr 16, 2020 3:49 am

What's wrong with...

Code: Select all

LDA P1_PORT
AND #%00000011
CMP #%00000001
...and using the carry as the button state? All 3 combinations of pressed buttons (%01, %10 and %11) will result in the carry being set.

User avatar
Controllerhead
Posts: 89
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by Controllerhead » Thu Apr 16, 2020 4:05 am

tokumaru wrote:
Thu Apr 16, 2020 3:49 am
What's wrong with...

Code: Select all

LDA P1_PORT
AND #%00000011
CMP #%00000001
...and using the carry as the button state? All 3 combinations of pressed buttons (%01, %10 and %11) will result in the carry being set.
The way the code is currently, I always need the value of the accumulator to be 1 with no carry, or 0 with a carry after reading the controller. I wrote some complex code to deal with different button situations, it does some pretty neat stuff.

Read / Run it if you are interested!
viewtopic.php?p=248744#p248744

EDIT- Wait a sec. Good idea. Improved by 2 cycles! Thanks =)

EDIT2- Improved again. You were right. See post. Thanks man.
Last edited by Controllerhead on Thu Apr 16, 2020 9:39 am, edited 2 times in total.

User avatar
Dwedit
Posts: 4328
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by Dwedit » Thu Apr 16, 2020 9:22 am

You usually rotate rather than ADC to put the carry flag into a register.
You'd read all 8 buttons, and ROR each bit into your value.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

User avatar
rainwarrior
Posts: 7822
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by rainwarrior » Thu Apr 16, 2020 9:33 am

Controllerhead wrote:
Thu Apr 16, 2020 2:50 am
Ok, this will work if Either bit 0 or bit 1 are activated, but not both. It will break if they are both set. Will the Famicom or anything else ever trigger both bit 0 and bit 1?
Unlikely that someone would try to use their expansion controller at the same time as the built-in one... but at the same time, why use such a fragile solution? LSR and CMP #0 take the same number of cycles:

Code: Select all

lda $4016
and #%11
cmp #1 ; result is now in carry
Edit: oops didn't see the second page, this was already suggested. :P

User avatar
Controllerhead
Posts: 89
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by Controllerhead » Thu Apr 16, 2020 9:49 am

Dwedit wrote:
Thu Apr 16, 2020 9:22 am
You'd read all 8 buttons, and ROR each bit into your value.
I wrote something a bit different for controller reading. You can read here if you're interested.
viewtopic.php?p=248744#p248744
rainwarrior wrote:
Thu Apr 16, 2020 9:33 am
why use such a fragile solution? LSR and CMP #0 take the same number of cycles
Inexperience. It's fixed now. Fix is in the above link. Thanks guys!

Pokun
Posts: 1485
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by Pokun » Fri Apr 17, 2020 4:36 am

rainwarrior wrote:
Thu Apr 16, 2020 9:33 am
Unlikely that someone would try to use their expansion controller at the same time as the built-in one...
Unless the player has siblings... :P

krzysiobal
Posts: 730
Joined: Sun Jun 12, 2011 12:06 pm
Location: Poland

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by krzysiobal » Thu Apr 23, 2020 11:18 am

Controllerhead wrote:
Wed Apr 15, 2020 11:42 pm
I am relying on the values #$40 for released and #$41 for pressed specifically for buttons, i have tested it successfully on my NTSC toaster and toploader models with a standard NES pad and Dogbone. I am loading code from an Everdrive N8.
Of course not, look at this:

Code: Select all

.SEGMENT "ZEROPAGE"
zp_ptr1: .res 2  

.SEGMENT "CODE"
	lda #<$4016
	sta zp_ptr1
	lda #>$4016 
	sta zp_ptr1 + 1
	ldy #0
	lda (zp_ptr1), y  
Depending on where zp_ptr1 is placed in memory, bits other that 0 and 2 can contain almost any value (for example, if zp_ptr1 is placed at $FE and $FE, this way of reading $4016 will return $FE or $FF depending if button is pressed or not.

lidnariq
Posts: 9491
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by lidnariq » Thu Apr 23, 2020 11:43 am

krzysiobal wrote:
Thu Apr 23, 2020 11:18 am
this way of reading $4016 will return $FE or $FF depending if button is pressed or not.
No, open bus will contain the contents of the pointer that was loaded, not the zero page address of the pointer.

Also, different NES/Famicoms have different numbers of open bus signals; nesdevwiki:Controller reading § Unconnected data lines and open bus, so even if you could get $FF on open bus, you'd still see $E0/$E1 on a NES-001, $E4/$E5 on a NES-101, and $F8/$F9 on a HVC-001. See tepples's thread.

User avatar
Controllerhead
Posts: 89
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by Controllerhead » Thu Apr 23, 2020 12:03 pm

I have already fixed the code to handle %xxxxxx01, %xxxxxx10, %xxxxxx11 as a press and ignore all other bits, as was suggested.

Code: Select all

LDA P1_PORT
AND #%00000011
CMP #%00000001
The full controller reading code is here.
viewtopic.php?p=248744#p248744

I think i'm in good shape, thanks to you fine folks, but if you have any reasons / edge cases where this would fail or suggestions for improvement i'm certainly open to them!

krzysiobal
Posts: 730
Joined: Sun Jun 12, 2011 12:06 pm
Location: Poland

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by krzysiobal » Fri Jul 03, 2020 6:03 pm

lidnariq wrote:
Thu Apr 23, 2020 11:43 am
so even if you could get $FF on open bus
What about LDA ($00, Y) when the pointer points at $3FF8 and Y=$1F?
Won't the last fetch be from broken address $3F17 (because page boundary is crossed) and then next one from $4017?
So fetch from $3F17 is ame as from $2007 which we can force to be any value we want because before we can set ppu address to VRAM, write any arbitrary value and then read it back.

lidnariq
Posts: 9491
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Is it safe to assume the values #$40 and #$41 for NTSC button presses?

Post by lidnariq » Fri Jul 03, 2020 7:47 pm

krzysiobal wrote:
Fri Jul 03, 2020 6:03 pm
What about LDA ($00, Y) when the pointer points at $3FF8 and Y=$1F?
Won't the last fetch be from broken address $3F17 (because page boundary is crossed) and then next one from $4017?
Yes. Tepples did that in the thread I'd linked above.

Post Reply