Page 1 of 1

Reading Joypad 1

Posted: Sun Nov 05, 2006 5:14 pm
by CartCollector
I can't seem to figure out what's wrong with my input routine. I'm trying to move a single sprite, to no avail. The sprite is displayed, though, so it must be something with how my program handles input. Here's the relevant part of the program:

Code: Select all


SetSpr:

	lda #$00		
	ldx X_Pos	
	ldy Y_Pos

	sta $2003	;Set SPR-RAM to $0000
	sta $2003

	sty $2004	;Set y-value for Sprite 0
	sta $2004	;Use Tile #0
	sta $2004	;Use Color #0
	stx $2004	;Set x-value for Sprite 0

ReadPad1:

	lda #$01
	ldx #$00	;Strobe joypad
	sta $4016
	stx $4016	
	sta $4016
	stx $4016

	lda $4016	;A		
	lda $4016	;B
	lda $4016	;Select
	lda $4016	;Start
	lda $4016	;Up
	sta Up
	lda $4016	;Down
	sta Down
	lda $4016	;Left
	sta Left
	lda $4016	;Right
	sta Right

UpTest:
	
	lda Y_Pos	;Test upper boundary
	cmp #$07
	beq DownTest
	
	lda Up		;Test if Up pressed
	cmp #$01
	bne DownTest
	inc Y_Pos	;If so, increase Y position

DownTest:
	
	lda Y_Pos
	cmp #223
	beq LeftTest

	lda Down		
	cmp #$01
	bne LeftTest
	dec Y_Pos
	
LeftTest:

	lda X_Pos	
	cmp #$00
	beq RightTest

	lda Left		
	cmp #$01
	bne RightTest
	dec X_Pos

RightTest:

	lda X_Pos
	cmp #248
	beq InfiniteLoop

	lda RightTest		
	cmp #$01
	bne InfiniteLoop
	inc X_Pos

InfiniteLoop

	jmp InfiniteLoop

Posted: Sun Nov 05, 2006 5:19 pm
by Quietust
I see you're using the GbaGuy NESASM tutorial. For your own good, I would strongly suggest that you get rid of that tutorial (which is absolutely riddled with errors and lacks any proper understanding of the NES) and switch to a better one (my recommendation is NES 101).

That being said, the most likely problem is that you're running your program in a decent emulator where reading $4016 returns either $41 (button pressed) or $40 (not pressed), while your program is expecting $01 and $00. The simplest fix would be to drop an "AND #$01" between each "LDA $4016" and "STA [direction]". Oh, and that last "lda RightTest" should be "lda Right".

Posted: Fri Nov 10, 2006 7:57 pm
by CartCollector
Thanks Quietust, that worked. Also wrong in the code is that to go right, the x value is decremented, instead of incremented as in the code. Vice versa goes for the left.

Posted: Sat Nov 11, 2006 9:17 am
by Disch
No... incrementing X will move the object more to the right. Decrementing moves left.

It's more likely you have your left and right joypad buttons backwards.

Posted: Sat Nov 11, 2006 12:37 pm
by Celius
Did you know you strobed it twice?

lda #$01
ldx #$00
sta $4016
stx $4016
sta $4016
stx $4016

Posted: Sat Nov 11, 2006 1:23 pm
by tepples
Strobing twice shouldn't have an effect with standard controllers, should it?

Posted: Sat Nov 11, 2006 1:37 pm
by Disch
nah, it's just unnecessary

Posted: Sat Nov 11, 2006 7:51 pm
by CartCollector
@Disch:

I tried it on FCE Ultra, Nestopia, and Nintendulator, with all of them set to left = left arrow, right = right arrow, and "dec X_Pos" to move right worked. So what else could it be?

@Celius:

I strobed twice because I had a bug and I didn't know what to do. So I started guessing.

Posted: Sat Nov 11, 2006 8:21 pm
by Quietust
Are you sure your screen isn't rotated or mirrored or something? Because I checked and noted that your Left/Right are normal, but your Up/Down are backwards...

Posted: Sun Nov 12, 2006 12:42 pm
by Celius
Your program is always going to that infinite loop, and thus, getting stuck in the infinite loop. Forever.

Posted: Sun Nov 12, 2006 1:47 pm
by Quietust
Celius wrote:Your program is always going to that infinite loop, and thus, getting stuck in the infinite loop. Forever.
Except he's probably landing at "SetSpr" after hitting his NMI code, so this makes it perfectly normal. Even SMB1 ends its per-frame code by ending in an infinite loop.

Posted: Sun Nov 12, 2006 5:01 pm
by Celius
Quietust wrote: Except he's probably....
Probably means it's probable, but not for certain. If he isn't doing something like that, then that is a huge problem.

Posted: Mon Nov 13, 2006 4:13 pm
by CartCollector
Don't worry, my program runs. It's been running since my second reply.

Oh and Quietust is right, I do NMI to SetSpr.

Posted: Sat Nov 18, 2006 2:11 pm
by CartCollector
Though there is a small bug: Whenever I go to the left or bottom border and hold the respective direction down, then release it, the sprite will move in one pixel, then out one. What's going on?

Posted: Sat Nov 18, 2006 4:27 pm
by blargg
Probably doing bounds checking before moving it, rather than after.