;ML ball routine

.org $801
.obj "@0:ball"

    .byte $1c,$08,$0a,$00,$9e,$20,$32,$30,$38,$30,$20,$3a,$20,$8f,$20
    .byte $36,$35,$30,$32,$20,$50,$4f,$57,$45,$52,$21,$00,$00,$00,$00,$00

    xp = $fa
    yp = $fb
    dx = $fc
    dy = $fd
    pos = $fe   ;this address probably won't cause problems...
;BASIC: 10 PRINT"{CLR/HOME}"
    lda #147
    jsr $ffd2
    ldx #0
    lda #1
-   sta $d800,x  ;fill color with white
    sta $d900,x
    sta $da00,x
    sta $db00,x
    inx
    bne -
;BASIC: 20 POKE 53280,7:POKE 53281,13
    ;never mind--ugly colors
;BASIC: 30 X=1:Y=1
    lda #1
    sta xp
    sta yp
;BASIC: 40 DX=1:DY=1
    lda #1
    sta dx
    sta dy
;BASIC: 50 POKE 1024+X+40*Y,81
    mainLoop = *
    jsr getPos
    ldy #0
    lda #81
    sta (pos),y
;BASIC: 60 FOR T=1 TO 10:NEXT
    ;you'll have to delay a lot longer than that in ML!!!  (counts to 2,560)
    ldy #10  ;this value is just a guess at a reasonable time
    ldx #0
-   inx
    bne -
    dey
    bne -
;BASIC: 70 POKE 1024+X+40*Y,32
    jsr getPos
    ldy #0
    lda #32
    sta (pos),y
;BASIC: 80 X=X+DX
    clc
    lda xp
    adc dx
    sta xp
;BASIC: 90 IF X<=0 OR X>=39 THEN DX=-DX
    lda xp
    bmi +
    cmp #1
    bcc +
    cmp #39
    bcc ++
+   sec
    lda #0
    sbc dx
    sta dx
;BASIC: 100 Y=Y+DY
+   clc
    lda yp
    adc dy
    sta yp
;BASIC: 110 IF Y<=0 OR Y>=24 THEN DY=-DY
    lda yp
    bmi +
    cmp #1
    bcc +
    cmp #24
    bcc ++
+   sec
    lda #0
    sbc dy
    sta dy
;BASIC: 120 GOTO 50
+   jmp mainLoop

;this subroutine does the job of evaluating 1024+X+40*Y
    getPos = *
    lda yp       ;get Y
    ldy #0
    sty pos+1
    asl          ;times 2
    asl          ;times 4
    adc yp       ;times 5
    asl          ;times 10
    rol pos+1
    asl          ;times 20
    rol pos+1
    asl          ;times 40
    rol pos+1
    adc xp       ;add X
    bcc +
    inc pos+1
+   sta pos+0
    clc          ;add 1024
    lda pos+1
    adc #4
    sta pos+1
    rts
