Cursor movement problems.
Posted: Fri Sep 26, 2014 2:24 am
I have an issue that's been infuriating. This is code I have for determining whether the user has pressed up or down on the gamepad (this subroutine is being called from the main loop), and doing the appropriate maths to move the cursor on screen vertically up or down. The problem is, no matter how I rewrite this code, I can only get one direction working. I think there's something wrong with the branching here and I suspect that, if user has pressed up, it will branch to the @subtracty and go to rts, but I think that rts is taking us back up to the point where it branched to subtracty, and going to the addition, cancelling out my subtraction math. I'm not sure how to approach this! I thought using jmp to get out of this and return to the main loop would be kind of 'hacky', so I'm trying to avoid doing it. This is my last question here for a while, I've been going a good pace at figuring stuff out and I've come this far. Just this snag is really annoying.
The goal is to be able to either add, or subtract 8 pixels of y position to or from the old y value to create the new y value, dpad up or down being the deciding factor on whether or not we add or subtract. I've isolated parts of the code and tested them and the addition makes both cursor sprites move down 8 pixels at a time, and subtraction makes both sprites move up 8 pixels at a time, but no matter how I write this branching code... I can't get it to work with both!!!
EDIT: Yeah using jsr to jump back into the main loop is bad news here. Eventually, my ram is filled with junk stack data from the jsr being used too much and this results in the rom crashing. I was hoping to keep my y positioning all in one subroutine without having to separate it out. Is there any way this can be achieved, am I missing something important?
EDIT:This is logical to me, but still does not work! Does this have to do with the stack?
Code: Select all
MOVECURSORY:
;d0 how many pixels to move (8 pixels for one row) = 0x08
;d1 sprite ID, = 0x00
;d2 sprite 2 ID, = 0x01
;d3 0x00 for up, 0x01 for down
mulu.w #0x8, d1
swap d1 ;d1 0x00000000
mulu.w #0x8, d2
swap d2 ;d2 0x00080000
add.l #VDPWRITESPRITE, d1 ;0x60000003
add.l #VDPWRITESPRITE, d2 ;0x60080003
move.l d1, CURSORATTRIBUTERAM ;This is for the VDP control port during vblank
move.l d2, CURSORATTRIBUTERAM2 ;This is for the VDP control port during vblank
move.l #0x00FFFFB2, a2 ;Ready ram address for y position
move.w (a2), d4 ; move old y value to d4
cmp.b #0x01, d3
bne @SUBTRACTY
add.w d0, d4 ;add old y value and 8 pixels to get new y value
move.w d4, 0x00FFFFB2
move.w d4, CURSORPOSITIONRAM ;This is for VDP Data port
jmp @END
@SUBTRACTY:
sub.w d0, d4 ;sub old y value and 8 pixels to get new y value
move.w d4, 0x00FFFFB2
move.w d4, CURSORPOSITIONRAM ;This is for VDP Data port
@END:
rts
EDIT: Yeah using jsr to jump back into the main loop is bad news here. Eventually, my ram is filled with junk stack data from the jsr being used too much and this results in the rom crashing. I was hoping to keep my y positioning all in one subroutine without having to separate it out. Is there any way this can be achieved, am I missing something important?
EDIT:
Code: Select all
cmp.b #0x01, d3
bne SUBTRACTY
cmp.b #0x01, d3
beq ADDY
rts
ADDY:
add.w d0, d4 ;add old y value and 8 pixels to get new y value
move.w d4, 0x00FFFFB2
move.w d4, CURSORPOSITIONRAM ;This is for VDP Data port
rts
SUBTRACTY:
sub.w d0, d4 ;add old y value and 8 pixels to get new y value
move.w d4, 0x00FFFFB2
move.w d4, CURSORPOSITIONRAM ;This is for VDP Data port
rts