joystick clc lda $dc00 lsr bcc up lsr bcc down lsr bcc left lsr bcc right jmp joystick up ldx $D005 dex stx $D005 jmp pauseit down ldx $D005 inx stx $D005 jmp pauseit left ldx $D004 dex stx $D004 jmp pauseit right ldx $D004 inx stx $D004 jmp pauseit pauseit ldx #1 ; 1 millisecond pause lda #128 loop1 cmp $d012 ;check if the raster has reached line 128 bne loop1 ;no, so keep checking loop2 cmp $d012 ;if it has you want to make sure you dont catch it more than once per frame beq loop2 ;so wait till it isn't 0 any more dex bne loop1 ;loop round 60*4 times jmp joystick
xpos_lo: .byte 0,0,0,0,0,0,0,0 xpos_hi: .byte 0,0,0,0,0,0,0,0 ypos: .byte 0,0,0,0,0,0,0,0 ; Call placeSprite to position sprite index x. placeSprite: ; y = x*2 txa asl tay ; Place y position and lo-part of x position is trivial. lda ypos,x sta $d001,y lda xpos_lo,x sta $d000,y ; Handle $d010 by first clearing the bit for the sprite lda $d010 and bittab_inv,x ; The set the bit IF xpos_hi != 0 ldy xpos_hi,x beq :+ ora bittab,x : sta $d010 rts bittab: .byte $01,$02,$04,$08,$10,$20,$40,$80 bittab_inv: .byte !$01, !$02, !$04, !$08, !$10, !$20, !$40, !$80
ldx #0; sprite to control lda $dc00 sta TMP lsr TMP bcs :+ inc ypos,x : lsr TMP bcs :+ dec ypos,x : lsr TMP bcs :+ clc lda xpos_lo,x adc #1 sta xpos_lo,x lda xpos_hi,x adc #0 sta xpos_hi,x : lsr TMP bcs :+ sec lda xpos_lo,x sbc #1 sta xpos_lo,x lda xpos_hi,x sbc #0 sta xpos_hi,x : jsr placeSprite
left lda $D004 cmp #$00 beq left2 jmp left3 left2 lda $D010 cmp #$00 beq left3 ldx #$00 stx $D010 ldx #$FF stx $D004 jmp left3 left3 lda $D010 cmp #$00 bne left9 lda $D004 cmp #$1A beq pauseit jmp left9 left9 ldx $D004 dex stx $D004 jmp pauseit right lda $D004 cmp #$FF beq right2 jmp right3 right2 lda $D010 cmp #$04 beq right3 ldx #$04 stx $D010 ldx #$00 stx $D004 jmp right3 right3 lda $D010 cmp #$04 bne right9 lda $D004 cmp #$40 beq pauseit jmp right9 right9 ldx $D004 inx stx $D004 jmp pauseit
left: lda $d010 and #$00000100 beq NotAbove //Check if the sprite x position is over the 256 limit? lda $d004 //Read current position (Which we know is larger then 256 border) sec sbc #speed //How fast do you want it to move sta $d004 //Store new position (Which is correct even if you cross the 256 boundry!) bcs NoBoundryCross lda $d010 eor #%00000100 sta $d010 //Flipp $d010 if the boundry was crossed NoBoundryCross: rts NotAbove: //The sprite position is less then 256 limit lda $d004 sec sbc #speed cmp #LeftBoundry (#$18 if my memory serves me right) bcs NoCrossBoundry lda #LeftBoundry NoCrossBoundry: sta $d004 rts // No need to do anything with $d010 here since we ain't going to cross it (Unless you experience problems like mentioned earlier with expanded sprites)!
X1 equ 480 X2 equ 344 Y1 equ 8 Y2 equ 250 UpdateSprite Lda $d010 Sta $fd Tax Eor #(X1>>8) Sta $fb Txa Eor #(X2>>8) Sta $fc Lda $dc00 ; Load Joy Port 2 Ldx $d000 ; Load X Ldy $d001 ; Load Y Lsr ; Test UP bit Bcs Down Cpy #Y1 Beq Down Dey Down Lsr ; Test DOWN bit Bcs Left Cpy #Y2 Beq Left Iny Left Lsr ; Test LEFT bit Bcs Right Cpx #(X1&$FF) ; Check left boundary Bne DoLeft Lda $FB ; Check highest bit Beq exit DoLeft Dex Cpx #$ff ; Test underflow Bne Right Lda $FD ; Fix left border Bne toggle Ldx #$F7 Bne toggle Right Lsr ; Test RIGHT bit Bcs exit Cpx #(X2&$FF) ; Check right boundary Bne DoRight Lda $FC ; Check highest bit Beq exit DoRight Inx Beq toggle ; Test overflow Cpx #$F8 Bne exit Lda $FD ; Fix left border Beq exit Ldx #$00 toggle Lda $d010 ; Toggle highest bit Eor #$01 Sta $d010 exit Sty $d001 ; Save Y Stx $d000 ; Save X Rts