I'm currently working in a demo of a platformer in 6502 assembly for a college project, and I'm not understanding how I could implement smooth horizontal/vertical movement and acceleration in this project. Basically, every movement consists in a slight different version of the following code:
READ_RIGHT:
LDA JOYPAD1
AND #%00000001
BNE DO_RIGHT
JMP READ_RIGHT_DONE
DO_RIGHT:
JSR HandleAcceleration
JSR UpdatePlayerPosition
READ_RIGHT_DONE:
RTI
HandleAcceleration:
LDA ACCEL + 1
CLC
ADC #$01
STA ACCEL + 1
BCC @ON_CARRY
LDA ACCEL + 0
CMP MAX_ACCEL
BCC @NOT_MAX_ACCEL
LDA MAX_ACCEL
STA ACCEL
@ON_CARRY:
INC ACCEL
INC MOV_FLAG
@NOT_MAX_ACCEL:
RTS
UpdatePlayerPosition:
LDA PLAYER_X
STA $0203
STA $020B
TAX
CLC
ADC #$08
STA $0207
STA $020F
ADC SPEED
STA PLAYER_X
RTS
I think I should use fixed-point arithmetics for this, but every single time I've tried, the program didn't work like it was intended. If someone knows what I should do or what I'm doing wrong, please help.
I've tried applying the following logic to the code:
READ_RIGHT:
LDA JOYPAD1
AND #%00000001
BNE DO_RIGHT
JMP READ_RIGHT_DONE
DO_RIGHT:
JSR HandleAcceleration
JSR UpdatePlayerPosition
READ_RIGHT_DONE:
RTI
HandleAcceleration:
LDA ACCEL + 1
CLC
ADC #$01
STA ACCEL + 1
BCC @ON_CARRY
LDA ACCEL + 0
CMP MAX_ACCEL
BCC @NOT_MAX_ACCEL
LDA MAX_ACCEL
STA ACCEL
@ON_CARRY:
INC ACCEL
INC MOV_FLAG
@NOT_MAX_ACCEL:
RTS
UpdatePlayerPosition:
LDA PLAYER_X
STA $0203
STA $020B
TAX
CLC
ADC #$08
STA $0207
STA $020F
ADC SPEED
STA PLAYER_X
RTS
Instead of smoothly applying acceleration to the sprite, it only made it go a lot faster instantly and nothing else.
Your problem is caused by not taking care of vertical retrace. When you don't catch the beam or don't set the IRQ correctly, CPU runs your code continuously. What you need is to run your code at a constant speed/FPS. I'm not an expert on NES hardware but from documentation, $2002 address seems to be the answer for a main loop structure. Try adding;
or by using IRQ, try setting your IRQ enterance parameters correctly taking into account the vertical retrace. Every 6502 based 8-bit machine has a method to catch the beam at the top of the screen. You just need to a do a research on "vertical retrace", "cathing the beam" subjects for NES.