
; joystick driver for sprite movement
; This is assuming that there is a sprite enabled and onscreen.
; This illustrates the technique of moving a sprite all over the screen
; by using a joystick.
; By Todd S. Elliott

; Equates
ciapra = $DC00; Joystick Port Two
sp0x = $D000; Sprite X position
sp0y = $D001; Sprite Y position
msigx = $D010; Most Signficant Byte (MSB) of Sprite X position

; Begin routine

joystick lda ciapra; check for joystick action
  and #%00011111; check affected bits
  cmp #%00011111; to determine if there was any action at all
  bne joy'up
  rts; return back to calling routine - no joystick action

joy'up lsr
  bcs joy'down; check to see if the user went up on the joystick or not.
  tay; save the .A temporarily
  lda sp0y
  cmp #50; check for the top of the screen
  beq +
  dec sp0y; Move the sprite up.
+ tya; restore joystick info

joy'down lsr
  bcs joy'left; check to see if the user went down on the joystick
  tay; save the .A temporarily
  lda sp0y
  cmp #248; check for the bottom of the screen
  beq +
  inc sp0y; Move the sprite down.
+ tya; restore joystick info

joy'left lsr
  bcs joy'right; check to see if the user went left on the joystick
  tay; save the .A temporarily
  lda msigx; check the MSB of the sprite pointer
  beq +
  lda sp0x
  bne ++; check to see if we have crossed the boundary or not
  sta msigx; turn off MSB bit in sprite 0 and 1
  lda #$ff
  sta sp0x; Store new value in sprite x location.
  bpl +++
+ lda sp0x
  cmp #24; check for the left edge of the screen
  beq ++
+ dec sp0x; Move the sprite to the left.
+ tya; restore joystick info

joy'right lsr
  bcs joy'fire; check to see if the user went right on the joystick
  tay; save the .A temporarily
  lda msigx
  beq +; check to see if we have the MSB on or off
  lda sp0x
  cmp #157; check for the right edge of the screen
  beq ++
- inc sp0x; Move the sprite to the right.
  bne ++
+ lda sp0x
  cmp #$ff; check to see if we crossed the page boundary
  bne -
  lda #$03
  sta msigx; turn on the MSB for sprites 0 and 1
  lda #$00
  sta sp0x; Store in new value for sprite x location
+ tya; restore joystick info

joy'fire lsr
  bcs +
; whatever action you want the fire button to do.
+ rts; return to calling routine

; end joystick and sprite movement routine


