First time poster here, hopefully I'm following this forums formatting rules so feel free to let me know otherwise.
I'm following the Nerdy Nights tutorials and just finished putting my first sprite on the screen. I gave myself the challenge to see if I can
move the sprite back and forth across the screen, and was surprised to see how difficult it was. I have something that now works
as I intended (shown below) but had a few questions on it.
1. I initially did not use CLC and SEC instructions before add and subtract and the movement of the sprite across the screen was rather broken. The sprite would only move right and start back at 00 sometimes... rather than bouncing off the right side of the screen. If I shrunk the boundaries of the x direction from the screens max of 0x00-0xF9, to say 01 and F8 I found that the code would work. I'm confused to why Adding CLC and SEC worked in the first place, as since I'm only adding and subtracting between 00 and F9, I thought there shouldn't be an overflow causing the carry bit to screw up any additions. Why does adding these instructions fix this behaviour?
2. Adding CLC and SEC slowed down the movement of the sprite considerably. Why is this? Is it because the VBLANK is already ready again and it's just waiting for NMI label to return?
3. For what I'm trying to do, is this proper style? Would it be possible to do this outside the NMI loop?
Code: Select all
; removed boiler plate above ;attempt to make the sprite move left and right across the screens width LDA #$01 ; initial movment direction (to the right) STA $0066 ; random address to store direction vector Forever: JMP Forever ;jump back to Forever, infinite loop NMI: LDA #$00 STA $2003 ; set the low byte (00) of the RAM address LDA #$02 STA $4014 ; set the high byte (02) of the RAM address, start the transfer LDA $0066 ; Direction vector for movement CMP #$01 BEQ MoveRight JMP MoveLeft MoveRight: LDA $0203 CLC ADC #$01 ;calculate the movement STA $0203 CMP #$F9 BEQ RtoL RTI MoveLeft: LDA $0203 SEC SBC #$01; STA $0203 CMP #$00 BEQ LtoR RTI ;following two labels are to switch the direction ;variable that the sprite moves in RtoL: LDA #$00 STA $0066 RTI ; return from NMI interrupt LtoR: LDA #$01 STA $0066 RTI ; return from NMi interrupt ; removed boiler plate below