Trouble moving sprite

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
SilverSurfer
Posts: 13
Joined: Wed May 03, 2023 12:41 am

Trouble moving sprite

Post by SilverSurfer »

I modified the nerdy night tutorial to include my first custom sprite that I made. I'm trying to get it to move, but every time I push "right" Mesen says the program has crashed. I can't figure out why. shipX and shipY are each declared in zeropage as .res 1. Debugging in Mesen shows shipX and shipY are within range. What am I missing?

Code: Select all

readcontroller1:
    LDA #$01
    STA $4016
    LDA #$00
    STA $4016
    LDX #$08
readcontroller1loop:
    LDA $4016
    LSR A           
    ROL buttons1   
    DEX
    BNE readcontroller1loop
    RTS


moveShipRight:
    LDA buttons1
    CMP #%00000001          ; right in buttons1 is at bit 0
    BNE moveShipRightDone   ; is right being pressed

    LDA shipX
    CLC
    ADC #$01
    STA shipX
    
    moveShipRightDone:

updateSprites:
        lda shipX
        sta $0203

        lda shipY
        sta $0200

        lda #$00
        sta $0201

        lda #$00
        sta $0202

Fiskbit
Posts: 891
Joined: Sat Nov 18, 2017 9:15 pm

Re: Trouble moving sprite

Post by Fiskbit »

I don't think the problem is in the quoted code. What happens after the end of this code? There's no RTS, so it's not clear if you're executing off the end of the function or if there's more code you just didn't include.

It can be very helpful to post a copy of the ROM; it's often much easier for us to identify the problem by seeing it in action and using Mesen's excellent debugging tools.
SilverSurfer
Posts: 13
Joined: Wed May 03, 2023 12:41 am

Re: Trouble moving sprite

Post by SilverSurfer »

thanks Fiskbit, I always appreciate your help. I have attached the rom file below.
Attachments
game.nes
(40.02 KiB) Downloaded 22 times
Fiskbit
Posts: 891
Joined: Sat Nov 18, 2017 9:15 pm

Re: Trouble moving sprite

Post by Fiskbit »

The main problem seems to be that your NMI returns using RTS instead of RTI. Interrupts put 3 things on the stack, not 2, and so you need to use RTI for them. (The format of the return address also slightly differs between them.) Interrupt handlers should also be saving A, X, and Y at the start and restoring them at the end because interrupts can happen anytime during code, and so if you don't save and restore registers, then when you return from the interrupt, the registers the code was using are trashed and that code will misbehave.

Once you fix the RTI problem, then the game seems to just return to an infinite loop, so while it reads joypads, it never does anything with the result. I'm not exactly sure why we see the symptom that it crashes when right is pressed (right is the last button read and may change register state when mistakenly RTS'ing from the function), but it's not important to dig into that.

If it helps, I wrote up some skeleton code here showing the basics of how a game might be laid out, covering an NMI and the main loop that handles joypad reading and mode dispatch. Note that you normally want to read your joypads in your main loop, so unless all of your game logic is in your NMI, it shouldn't live in the NMI. I also don't normally recommend having everything in your NMI because it makes it hard to run your sound engine on lag frames, and laggy sound makes slowdown feel much worse.
Post Reply