BUTTONS

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

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

Re: BUTTONS

Post by tokumaru »

Yes, Famicom controllers that connect to the expansion port. Another common technique to read them is to AND the byte with %00000011 to keeo only the bits that matter and CMP the result to 1, setting the carry for values 1 (button pressed on regular controller only), 2 (pressed on expansion port controller only) and 3 (pressed on both controllers). The carry can then be shifted into the final byte.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: BUTTONS

Post by thefox »

tokumaru wrote:
Pokun wrote:Having learned from Nerdy Nights myself I both agree and disagree with you. It's not ideal but it's the best newbie tutorials I found for making NES games.
Yeah, it definitely has its merits, I just can't help noticing that a lot of people come here having trouble with the far from ideal program structure used in that tutorial.
Indeed, it's not ideal (even bunnyboy would agree with that, I believe), but as long as nobody steps up to write a better one, we're going to be pointing newcomers to it.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: BUTTONS

Post by tepples »

tokumaru wrote:Yes, Famicom controllers that connect to the expansion port. Another common technique to read them is to AND the byte with %00000011 to keeo only the bits that matter and CMP the result to 1, setting the carry for values 1 (button pressed on regular controller only), 2 (pressed on expansion port controller only) and 3 (pressed on both controllers). The carry can then be shifted into the final byte.
The last snippet at Gamepad code is what I use in my own games. It reads bit 1 and 0 of both controller ports using the AND/CMP/ROL technique that tokumaru described. It also uses a trick called a "ring counter", where the result variable doubles as a loop counter. If I initialize the result variable to $01 and shift it left once each loop, the 1 comes out into the carry once the loop has run eight times, and then the subroutine can loop based on whether the carry has been set yet.

If there's anything in this code that you want me to explain further, I'm ready to do so.

Code: Select all

JOYPAD1 = $4016
JOYPAD2 = $4017

.zeropage
buttons1: .res 1
buttons2: .res 1

.code
readjoy:
  lda #$01
  sta JOYPAD1
  sta buttons2  ; player 2's buttons double as a ring counter
  lsr a         ; now A is 0
  sta JOYPAD1
@loop:
  lda JOYPAD1
  and #$03      ; ignore bits other than controller
  cmp #$01      ; Set carry if and only if nonzero
  rol buttons1  ; Carry -> bit0; bit 7 -> Carry
  lda JOYPAD2   ; Repeat 
  and #$03
  cmp #$01
  rol buttons2  ; Carry -> bit0; bit 7 -> Carry
  bcc @loop
  rts
mikaelmoizt
Posts: 120
Joined: Sat Apr 12, 2014 12:11 pm
Location: Gothenburg, Sweden

Re: BUTTONS

Post by mikaelmoizt »

tokumaru wrote:I'm starting to think that the way these beginner tutorials approach such basic tasks does more harm
I agree. Like I wrote in my previous post, a code snippet fixing this would require a bit more knowledge which in turn means to redo the whole structure if it is going to work as intended. Not that I am assembler/NES guru or anything but my experience tells me what NOT to do. If you want to do anything else with this code and still maintain some form of understandability, you pretty soon realize there needs to be de-spaghettification. Like you wrote, mixing all kinds of tasks that does not belong together in your NMI is not a good way of building a game. This ofcourse is a flaw of the NN tutorials and not the newbie trying to learn.
I´ve got %01100011 problems but the BITs aint one.
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: BUTTONS

Post by rainwarrior »

Pokun wrote:Another flaw of Nerdy Nights is that he doesn't say a word about the init code he puts in each program you write. This makes you wonder if you missed something.
Nerdy Nights neglects to read $2002 before it begins its first vblank wait, so it's not a totally robust init example either.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: BUTTONS

Post by tokumaru »

Before anyone gets offended, I want to make it clear that my intention is not to bash the Nerdy Nights or any other tutorial. In this regard, the expression "doing more harm than good" was probably exaggerated, but english is not my first language and sometimes things don't come off as I intended.

It's good to know we have something that newbies can use to get started, and we have a lot to thank to the people who dedicate their time to creating tutorials to help others. I know Nerdy Nights is indeed the best option for now. It may use some questionable program structures, but at least it's not full of program breaking mistakes and bad understanding of the platform in general.

Still, even though I don't have the time or competence (I see myself as a good coder, but a terrible teacher) to create something better, I think it's important for us to discuss and point out the shortcomings of the existing materials, so that newbies can know what to expect and how to proceed after the tutorials, and so that anyone that might decide to write a tutorial in the future keeps the issues in mind.
Post Reply