    .code
    .bank 0
    .org $e000
reset:
    sei
    csh
    cld

    ldx    #$ff
    txs
    
    lda    #$ff
    tam0
    
    lda    #$f8
    tam1
    
    stz    <$00
    tii    $2000, $2001, $1fff

    st0    #$05
    st1    #$00             ; disable display
    st2    #$00

    stz    $0801
    stz    $0809

    lda    #$05
psg_reset:
    sta    $0800
    stz    $0804
    stz    $0805
    dec    A
    bpl    psg_reset

    lda    #4
    sta    $0800
    stz    $0807
    inc    A
    sta    $0800
    stz    $0807
    
    lda    #$01
    sta    $0c01
    
    stz    $1403

    st0    #$09         ; set BAT to 64x32
    st1    #%00010000
    st2    #$00

    st0    #$0a
    st1    #$02
    st2    #$0b
    
    st0    #$0b
    st1    #$3f
    st2    #$04
    
    st0    #$0c
    st1    #$02   
    st2    #$0f 

    st0    #$0d
    st1    #$ef
    st2    #$00

    st0    #$0e
    st1    #$0C
    st2    #$00

    lda    #%0000_0110
    sta    <$02
    sta    $0400

    stz    $0402
    stz    $0403
    tia    palette, $0404, 32

    tia    $2200, $0404, 32

    st0    #$00
    st1    #$00
    st2    #$08

    st0    #$02

    lda    #$01
    ldx    #$07
upload:
    tam2
    tia    $4000, $0002, $2000
    inc    A
    dex
    bne    upload
  
    tam2
    tia    $4000, $0002, $1000

    st0    #$00
    st1    #$00
    st2    #$00

    st0    #$02

    lda    #$80
    sta    <$04
    lda    #$00
    sta    <$05

    ldy    #30
bat_y:
        ldx    #64
bat_x:
        lda    <$04
        sta    $0002
        
        lda    <$05
        sta    $0003

        inc    <$04
        bne    skip
            inc    <$05
    skip:
        dex
        bne    bat_x
    dey
    bne    bat_y

    ldx    #$80
bat_footer:
    st1    #$80
    st2    #$10
    dex
    bne    bat_footer

    lda    #120
    sta    <$06

    st0    #$05
    st1    #$8c        ; bg enable, vblank & hblank enable
    st2    #$00

    lda    #%1111_1101  ; enable irq 1
    sta    $1402

    cli

loop:
    stz    <$01
wait_vsync:
    lda    <$01
    beq    wait_vsync

    bra    loop

irq1:
    php
    pha
    phx
    phy

    lda    $0000
    sta    <$00

    bbr2   <$00, check_vsync
hsync:
TOP=64+120/2
BOTTOM=TOP+120-1
RCR_VCE_CHANGE=$126
BORDER_SCR=239

top:
        ldx    <$09
        cpx    #high(TOP)
        bcc    border
        bne    bottom
        lda    <$08
        cmp    #low(TOP)
        bcc    border
bottom:
        cpx    #high(BOTTOM)
        bcc    center
        bne    border
        cmp    #low(BOTTOM)
        bcs    border
center:
        st0    #$08
        lda    <$0a
        sta    $0002
        lda    <$0b
        sta    $0003

        inc    <$0a
        bne    l2
            inc <$0b
l2:
        bra    hsync_end

border:
        st0    #$08
        st1    #low(BORDER_SCR)
        st2    #high(BORDER_SCR)

hsync_end:
    lda    <$09
    cmp    #high(RCR_VCE_CHANGE)
    bne    l3
    lda    <$08
    cmp    #low(RCR_VCE_CHANGE)
    bne    l3
        lda    <$02
        eor    #%0000_0100
        sta    $0400
        sta    <$02
l3:

    inc    <$08
    bne    l1
        inc    <$09
l1:

    st0    #$06
    lda    <$08
    sta    $0002
    lda    <$09
    sta    $0003

    stz    $1403

    ply
    plx
    pla
    plp

    rti
check_vsync:
    bbr5   <$00, irq1_end
vsync:
        inc    <$01

        lda    <$06
        sta    <$0a
        eor    #120
        sta    <$06
        stz    <$0b

        st0    #$08
        st1    #low(BORDER_SCR)
        st2    #high(BORDER_SCR)
        
        st0    #$06
        lda    #low(64)
        sta    <$08
        sta    $0002
        lda    #high(64)
        sta    <$09
        sta    $0003

irq1_end:

    stz    $1403

    ply
    plx
    pla
    plp
    rti

irq2:
timer:
nmi:
    rti

palette:
    .incbin "data/image.pal"

    .org $fff6

    .word irq2 
    .word irq1
    .word timer
    .word nmi
    .word reset

    .data
    .bank $01
    .org  $4000
image_000:
    .incbin "data/image.000"

    .data
    .bank $02
    .org  $4000
image_001:
    .incbin "data/image.001"

    .data
    .bank $03
    .org  $4000
image_002:
    .incbin "data/image.002"

    .data
    .bank $04
    .org  $4000
image_003:
    .incbin "data/image.003"

    .data
    .bank $05
    .org  $4000
image_004:
    .incbin "data/image.004"

    .data
    .bank $06
    .org  $4000
image_005:
    .incbin "data/image.005"

    .data
    .bank $07
    .org  $4000
image_006:
    .incbin "data/image.006"

    .data
    .bank $08
    .org  $4000
image_007:
    .incbin "data/image.007"