Displaying a screen while holding a button (working code)

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

Post Reply
lazerbeat
Posts: 64
Joined: Tue Jul 09, 2013 7:13 am

Displaying a screen while holding a button (working code)

Post by lazerbeat »

I have a little rom I am making where I would like to load a screen while holding a button on the controller, if not holding a button, display a different screen. ie

nothing held = load screen 0
While holding A = display screen 1

My code works! But I am fairly sure it is a bit (more likely a lot) hacky, and probably far from best practice. Is there a better way to do this?

Code: Select all

; controller reading subroutines start
readcontroller:

        LDA newbuttons
	STA oldbuttons

        LDX #$00
	LDA #$01		; strobe joypad
	STA $4016
	LDA #$00
	STA $4016
ConLoop:
	LDA $4016		; check the state of each button
	LSR
	ROR newbuttons
        INX
        CPX #$08
        bne ConLoop

	LDA oldbuttons          ; invert bits
	EOR #$FF
	AND newbuttons
	STA justpressed

	LDA screennumber        ; save old screen number for later compare
	STA oldscreen

        ;button ( 0 0 0 0 0  0  0 0 )
        ;layout ( R L D U St Sl B A )






checkselect:
	lda #0
sta screennumber
	LDA #%00000100
	AND oldbuttons
	BEQ checkstart
lda #5
sta screennumber

checkstart:	
	LDA #%00001000
	AND oldbuttons
	BEQ checka
lda #6
sta screennumber

checka:	
	LDA #%00000010
	AND oldbuttons
	BEQ checkb
lda #7
sta screennumber

checkb:	
	LDA #%00000001
	AND oldbuttons
	BEQ checkup
lda #8
sta screennumber

checkup:	
	LDA #%00010000
	AND oldbuttons
	BEQ checkdown
lda #2
sta screennumber

checkdown:	
	LDA #%00100000
	AND oldbuttons
	BEQ checkleft
lda #4
sta screennumber

checkleft:	
	LDA #%01000000
	AND oldbuttons
	BEQ checkright
lda #1
sta screennumber

checkright:	
	LDA #%10000000
	AND oldbuttons
	BEQ checksdone
lda #3
sta screennumber

checksdone:
Attachments
gallerynes.nes
(36.02 KiB) Downloaded 98 times
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Displaying a screen while holding a button (working code

Post by lidnariq »

In this specific case, where you check for each button and don't care about multiple presses, you could instead consider something like

Code: Select all

 LDX #7
 LDA oldbuttons
@more:
 ROL
 BCS @done
 DEX
 BPL @more
@done:
 STX screennumber
screennumber will contain -1,0,1,2,3,4,5,6,7 for each of none, A, B, Sel, Start, Up, Down, Left, Right pressed; rightmost here has precedence.
lazerbeat
Posts: 64
Joined: Tue Jul 09, 2013 7:13 am

Re: Displaying a screen while holding a button (working code

Post by lazerbeat »

Thanks a lot!
Post Reply