

//====================================

.var accBackup = $3c  //byte
.const colReg1 = $d021
.const colReg2 = $d020
.const irqLine1 = $04
.const irqLine2 = irqLine1+2

.const raster_colors1=$3b38
.const raster_colors2=$3a00
.const raster_colors3=$3900

.const canvas_start=43
.const bottom_raster_length=50

:BasicUpstart2(run)

* = $0810 "Main"

run:
            sei
            lda #$35
            sta $01
            lda #$7f
            sta $dc0d
            lda #$01
            sta $d01a
            lda #<irq
            ldy #>irq
            sta $fffe
            sty $ffff
            lda #irqLine1
            sta $d012
            lda #$3b
            sta $d011
            lda #$80
            sta $d018
            lda #$02
            sta $dd00
            lda #%00011000
            sta $d016
            lda $3fff
            sta $d020
            ldx #$00
!:          lda $3c00,x
            sta $d800,x
            lda $3d00,x
            sta $d900,x
            lda $3e00,x
            sta $da00,x
            lda $3f00,x
            sta $db00,x
            dex
            bne !-
            jsr setup_colors_3
            jsr setup_colors_1
            cli
            jmp *

//-----------------------------------------------------------------------
//IRQs

vblank:     lda $d011
            bpl *-3
            lda $d011
            bmi *-3
            rts

.macro enterIrq() {
            pha
            txa
            pha
            tya
            pha
            lda $01
            pha

            lda #$35
            sta $01
}

.macro setIrq(addr, raster) {
            lda #<addr
            ldy #>addr
            sta $fffe
            sty $ffff
            lda #raster
            sta $d012
}

.macro exitIrq() {
            // dec colReg1
            inc $d019
            pla
            sta $01
            pla
            tay
            pla
            tax
            pla
            rti
}


.macro wait3(amount) {
    .for (var y=0; y<amount; y++)
    {
            bit $ea
    }
}

.macro wait2(amount) {
    .for (var y=0; y<amount; y++)
    {
            nop
    }
}



*=$097d "irq"   //DONT MOVE!


irq:
    sta accBackup
    lda #$01
    sta $d019
    lda #irqLine2
    sta $d012
    lda #<irq2
    sta $fffe
    lda #>irq2
    sta $ffff
    lda #irqLine1+1
!:  cmp $d012
    bne !-
    cli
    :wait2(35)
    jmp *


irq2:
    txa
    pha
    tya
    pha
    lda $01
    pha
    lda #$35
    sta $01

    :wait2(13)
    bit $ea

    lda $d012
    cmp #irqLine2+1
    bne !+
!:

    :wait2(27)
    .for (var i=0; i<canvas_start-1; i++) {
        lda raster_colors2+i
        sta colReg2
        ldx #$0a
!:      dex
        bne !-
        nop
        nop
    }
        lda raster_colors2+canvas_start-1
        sta colReg2
        ldx #$08
!:      dex
        bne !-
        nop
        nop
        nop

    ldx #$00

.var raster_colors2_list=List()
.var raster_colors1_list=List()
.var raster_colors3_list=List()


.for (var i=0;i<25;i++)
{
    nop
    lda raster_colors2+(i*8)+canvas_start
    sta colReg2
    .eval raster_colors1_list.add(*+1)
    ldy #$00      //bad line
    .eval raster_colors3_list.add(*+1)
    lda #$00
    sty colReg1
    sta colReg2,x
    nop

    lda raster_colors2+1+(i*8)+canvas_start
    sta colReg2
    lda raster_colors1+1+(i*8)
    sta colReg1
    lda raster_colors3+1+(i*8)
    sta colReg2,x
    wait2(19)

    lda raster_colors2+2+(i*8)+canvas_start
    sta colReg2
    lda raster_colors1+2+(i*8)
    sta colReg1
    lda raster_colors3+2+(i*8)
    sta colReg2,x
    wait2(19)

    lda raster_colors2+3+(i*8)+canvas_start
    sta colReg2
    lda raster_colors1+3+(i*8)
    sta colReg1
    lda raster_colors3+3+(i*8)
    sta colReg2,x
    wait2(19)

    lda raster_colors2+4+(i*8)+canvas_start
    sta colReg2
    lda raster_colors1+4+(i*8)
    sta colReg1
    lda raster_colors3+4+(i*8)
    sta colReg2,x
    wait2(19)

    lda raster_colors2+5+(i*8)+canvas_start
    sta colReg2
    lda raster_colors1+5+(i*8)
    sta colReg1
    lda raster_colors3+5+(i*8)
    sta colReg2,x
    wait2(19)

    lda raster_colors2+6+(i*8)+canvas_start
    sta colReg2
    lda raster_colors1+6+(i*8)
    sta colReg1
    lda raster_colors3+6+(i*8)
    sta colReg2,x
    wait2(19)

    lda raster_colors2+7+(i*8)+canvas_start
    sta colReg2
    lda raster_colors1+7+(i*8)
    sta colReg1
    lda raster_colors3+7+(i*8)
    sta colReg2,x
    wait2(15)
    bit $ea
}

    nop
    .for (var i=0; i<bottom_raster_length; i++) {
        lda raster_colors2+i+43+200
        sta colReg2
        ldx #$0a
!:      dex
        bne !-
        nop
        nop
    }


    lda #irqLine1
    sta $d012
    lda #$01
    sta $d019
    lda #<irq
    sta $fffe
    lda #>irq
    sta $ffff


    lda $dc0d
    lsr $d019
    pla
    sta $01
    pla
    tay
    pla
    tax
    pla
    pla
    pla
    lda accBackup
    rti


setup_colors_3:
    .for (var i=0; i<25; i++) {
        lda raster_colors3+(i*8)
        sta raster_colors3_list.get(i)
    }
    rts

setup_colors_1:
    .for (var i=0; i<25; i++) {
        lda raster_colors1+(i*8)
        sta raster_colors1_list.get(i)
    }
    rts

//    *=$3900 "raster_colors3"
//
//    .for (var i=0; i<25; i++) {
//        .byte $05, $06, $05, $06, $05, $06, $05, $06
//    }
//
//
//    *=$3a00 "raster_colors2"
//
//    .for (var i=0; i<37; i++) {
//        .byte $02, $07, $02, $07, $02, $07, $02, $07
//    }
//
//    *=$3b38 "raster_colors1"
//
//    .for (var i=0; i<25; i++) {
//        .byte $03, $04, $03, $04, $03, $04, $03, $04
//    }
//
//    *=$4000 "Bitmap"
//    .fill 16384, 0
    