This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

RTS strange bug?

RTS strange bug?
by on (#59432)
Hi,so...I'm running this third or fourth time,but i don't know how to fix it(I mean,it's HAVE to be subroutine...)
I just using this code:
Code:
 jsr Music_Init
**Some code here**

Music_Init: 

   ; ignore IRQs   
   sei

   ; This must be done BEFORE calling any routines, since
   ; it sets the stack pointer
   ldx #$FF
   txs

   ; Now init the music. First, select the song index
   ldx #$00

   ; Then store the address of the intented music data, if any.
   lda #LOW(songData1)
   sta ft_music_addr
   lda #HIGH(songData1)
   sta ft_music_addr + 1
   
   ; Finally select the song inside the music data
   lda #$0            ; track 0
   ldx #$0                     ; NTSC
   jsr ft_music_init

   ; init default value for ppu_crtl flags + activate NMI
   lda #PPU_CTRL_NMI
   sta zpPpuCtrlFlags
   sta PPU_CTRL
        rts

But game is crashing!
If i replace rts with jump to a label just after "jsr Music_Init"then it's working fine,but RTS is crashing game...
I noticed that RAM $100-$200 is randomly changed,but...debugger can't show what code is changing that RAM!

by on (#59434)
Possibly because you change the stack after the first JSR on the first line. If I'm correct, a JSR pushes its current address onto the stack in order for it to know where to go when an RTS is made. When you 'set-up' the stack in Music_init, you screw up where the computer needs to return to. Notice how your comments say to set-up the stack before any sub-routine is called, yet you already called one right on the first line.

by on (#59435)
I think it has to do with you loading a new stack position, plus you JSR somewhere else in the program, I didn't look over it too good, but thats what I see.

Jsr Music_Init (Pushes PC to stack)

(Change stack location)

jsr ft_music_init (Push PC to stackN No code here has that tag)

So lets assume it RTS's from that one, it will then RTS again and god knows what the stack is pointing at. What if the stack was up to lets say $107, And you change it to $FF, That just ruins the whole stack as it is. I'd maybe not change the stack. If your program if just JSR and RTS, It hurts nothing, it won't hurt any values there.


Hope my attempt was helpful.

Aaron


Edit:

What he said above me haha ^_^ You know you only have to load the stack once? Everytime you use it it doesn't just blow up, it's still there :P

by on (#59456)
Many thanks,you two.
Thread can be closed now.