I'm trying to understand why the following code doesn't work the way it is described to (this is slightly modified code from one of GBAGuy's tutorials). I'm trying to use variables to set the x and y position of my sprite, instead of immediate numbers (#50 and such). Here's what I'm doing:
.bank 0 ; bank 0 - our place for code.
.org $0000
X_Pos: .db 50
Y_Pos: .db 50
.org $8000 ; code starts at $8000
Start: (more code here)
I'm setting X_Pos and Y_Pos to 50. Later in the code I'm setting the X and Y values with these labels:
lda Y_Pos ; load Y value
sta $2004 ; store Y value
lda #$01 ; tile number 1
sta $2004 ; store tile number
lda #$00 ; no special junk
sta $2004 ; store special junk
lda X_Pos ; load X value
sta $2004 ; store X value
When I run this in FCEUltra or Nestopia, the sprite doesn't display at 50, 50 but 0,0 instead.
Can someone tell me why this is happening? I used the debugger in FCEUltra, and it looks like the memory at $0000 and $0001 (x and y pos) doesn't get set to 50 like it's supposed to.
Thanks in advance!
Nathaniel
Full code below:
;;--- CODE START ---;;
; INES header stuff
.inesprg 1 ; 1 bank of code
.ineschr 1 ; 1 bank of spr/bkg data
.inesmir 1 ; something always 1
.inesmap 0 ; we use mapper 0
.bank 1 ; following goes in bank 1
.org $FFFA ; start at $FFFA
.dw 0 ; dw stands for Define Word and we give 0 as address for NMI routine
.dw Start ; give address of start of our code for execution on reset of NES.
.dw 0 ; give 0 for address of VBlank interrupt handler, we tell PPU not to
; make an interrupt for VBlank.
.bank 0 ; bank 0 - our place for code.
.org $0000
X_Pos: .db 50
Y_Pos: .db 50
.org $8000 ; code starts at $8000
Start:
lda #%00001000 ; do the setup of PPU
sta $2000 ; that we
lda #%00011110 ; talked about
sta $2001 ; on a previous day
ldx #$00 ; clear X
lda #$3F ; have $2006 tell
sta $2006 ; $2007 to start
lda #$00 ; at $3F00 (pallete).
sta $2006
loadpal: ; this is a freaky loop
lda tilepal, x ; that gives 32 numbers
sta $2007 ; to $2007, ending when
inx ; X is 32, meaning we
cpx #32 ; are done.
bne loadpal ; if X isn't =32, goto "loadpal:" line.
waitblank: ; this is the wait for VBlank code from above
lda $2002 ; load A with value at location $2002
bpl waitblank ; if bit 7 is not set (not VBlank) keep checking
lda #$00 ; these lines tell $2003
sta $2003 ; to tell
lda #$00 ; $2004 to start
sta $2003 ; at $0000.
lda Y_Pos ; load Y value
sta $2004 ; store Y value
lda #$01 ; tile number 1
sta $2004 ; store tile number
lda #$00 ; no special junk
sta $2004 ; store special junk
lda X_Pos ; load X value
sta $2004 ; store X value
; and yes, it MUST go in that order.
infin:
jmp infin ; JuMP to infin. note that this loop never ends.
tilepal: .incbin "our.pal" ; include and label our pallete
.bank 2 ; switch to bank 2
.org $0000 ; start at $0000
.incbin "our.bkg" ; empty background first
.incbin "our.spr" ; our sprite pic data
; note these MUST be in that order.
;;--- WERE DONE / CODE END ---;;
.bank 0 ; bank 0 - our place for code.
.org $0000
X_Pos: .db 50
Y_Pos: .db 50
.org $8000 ; code starts at $8000
Start: (more code here)
I'm setting X_Pos and Y_Pos to 50. Later in the code I'm setting the X and Y values with these labels:
lda Y_Pos ; load Y value
sta $2004 ; store Y value
lda #$01 ; tile number 1
sta $2004 ; store tile number
lda #$00 ; no special junk
sta $2004 ; store special junk
lda X_Pos ; load X value
sta $2004 ; store X value
When I run this in FCEUltra or Nestopia, the sprite doesn't display at 50, 50 but 0,0 instead.
Can someone tell me why this is happening? I used the debugger in FCEUltra, and it looks like the memory at $0000 and $0001 (x and y pos) doesn't get set to 50 like it's supposed to.
Thanks in advance!
Nathaniel
Full code below:
;;--- CODE START ---;;
; INES header stuff
.inesprg 1 ; 1 bank of code
.ineschr 1 ; 1 bank of spr/bkg data
.inesmir 1 ; something always 1
.inesmap 0 ; we use mapper 0
.bank 1 ; following goes in bank 1
.org $FFFA ; start at $FFFA
.dw 0 ; dw stands for Define Word and we give 0 as address for NMI routine
.dw Start ; give address of start of our code for execution on reset of NES.
.dw 0 ; give 0 for address of VBlank interrupt handler, we tell PPU not to
; make an interrupt for VBlank.
.bank 0 ; bank 0 - our place for code.
.org $0000
X_Pos: .db 50
Y_Pos: .db 50
.org $8000 ; code starts at $8000
Start:
lda #%00001000 ; do the setup of PPU
sta $2000 ; that we
lda #%00011110 ; talked about
sta $2001 ; on a previous day
ldx #$00 ; clear X
lda #$3F ; have $2006 tell
sta $2006 ; $2007 to start
lda #$00 ; at $3F00 (pallete).
sta $2006
loadpal: ; this is a freaky loop
lda tilepal, x ; that gives 32 numbers
sta $2007 ; to $2007, ending when
inx ; X is 32, meaning we
cpx #32 ; are done.
bne loadpal ; if X isn't =32, goto "loadpal:" line.
waitblank: ; this is the wait for VBlank code from above
lda $2002 ; load A with value at location $2002
bpl waitblank ; if bit 7 is not set (not VBlank) keep checking
lda #$00 ; these lines tell $2003
sta $2003 ; to tell
lda #$00 ; $2004 to start
sta $2003 ; at $0000.
lda Y_Pos ; load Y value
sta $2004 ; store Y value
lda #$01 ; tile number 1
sta $2004 ; store tile number
lda #$00 ; no special junk
sta $2004 ; store special junk
lda X_Pos ; load X value
sta $2004 ; store X value
; and yes, it MUST go in that order.
infin:
jmp infin ; JuMP to infin. note that this loop never ends.
tilepal: .incbin "our.pal" ; include and label our pallete
.bank 2 ; switch to bank 2
.org $0000 ; start at $0000
.incbin "our.bkg" ; empty background first
.incbin "our.spr" ; our sprite pic data
; note these MUST be in that order.
;;--- WERE DONE / CODE END ---;;