; part2.asm: copies the picture's colours, moves eight sprites on a sine table.

koala_screen = $7f40
koala_colour = $8328
sprites      = $5000
screen       = $5c00
sprite_ptrs  = $5ff8
phase        = $fb

        *= $2000
        jmp init            ; export table
        jmp tick

init    ldx #0
copy    lda koala_screen,x
        sta screen,x
        lda koala_screen+$100,x
        sta screen+$100,x
        lda koala_screen+$200,x
        sta screen+$200,x
        lda koala_screen+$300,x
        sta screen+$300,x
        lda koala_colour,x
        sta $d800,x
        lda koala_colour+$100,x
        sta $d900,x
        lda koala_colour+$200,x
        sta $da00,x
        lda koala_colour+$300,x
        sta $db00,x
        inx
        bne copy

        ldx #63
ball    lda ball_data,x
        sta sprites,x
        dex
        bpl ball

        ldx #7
setup   lda #sprites-$4000>>6
        sta sprite_ptrs,x
        lda colours,x
        sta $d027,x
        dex
        bpl setup
        lda #$ff
        sta $d015
        rts

tick    inc phase
        ldx #0
        ldy #0
move    tya
        asl
        asl
        asl
        asl
        adc phase
        stx save_x
        tax
        lda sine_x,x
        ldx save_x
        sta $d000,x
        tya
        asl
        asl
        asl
        asl
        adc phase
        adc phase
        stx save_x
        tax
        lda sine_y,x
        ldx save_x
        sta $d001,x
        inx
        inx
        iny
        cpy #8
        bne move
        rts

save_x  .byte 0

colours .byte WHITE, YELLOW, CYAN, LIGHT_GREEN, LIGHT_BLUE, ORANGE, LIGHT_RED, LIGHT_GREY

        .align 256
sine_x  .for i = 0, 256
        .byte sin(i * 2 * PI / 256) * 110 + 130
        .endfor
sine_y  .for i = 0, 256
        .byte cos(i * 2 * PI / 256) * 70 + 150
        .endfor

ball_data .binary "ball.spr"
