;=================================================================
; [VRAMPAL.INC] -- It's pretty self-explaining, isn't it?
;                  Shi!t !! Almost forgot that I also put
;                  some fade-routines in here too!
;=================================================================


; Fades in display with de-pixelization effect.

.MACRO FADE_IN_MOSAIC
    pha

    lda #$00
    sta $2100
    lda #$ff
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$01
    sta $2100
    lda #$ef
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$02
    sta $2100
    lda #$df
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$03
    sta $2100
    lda #$cf
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$04
    sta $2100
    lda #$bf
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$05
    sta $2100
    lda #$af
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$06
    sta $2100
    lda #$9f
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$07
    sta $2100
    lda #$8f
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$08
    sta $2100
    lda #$7f
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$09
    sta $2100
    lda #$6f
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$0a
    sta $2100
    lda #$5f
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$0b
    sta $2100
    lda #$4f
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$0c
    sta $2100
    lda #$3f
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$0d
    sta $2100
    lda #$2f
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$0e
    sta $2100
    lda #$1f
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@
    lda #$0f
    sta $2100
    lda #$0f
    sta $2106
    jsr _M_FINM_WAI_KTHX_\@

    jmp _M_FINM_ENDE_\@

_M_FINM_WAI_KTHX_\@:
    wai
    wai
    wai
    wai
    rts

_M_FINM_ENDE_\@:
    pla
.ENDM


; Fades out display with de-pixelization effect.

.MACRO FADE_OUT_MOSAIC
    pha

    lda #$0f
    sta $2100
    lda #$0f
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$0e
    sta $2100
    lda #$1f
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$0d
    sta $2100
    lda #$2f
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$0c
    sta $2100
    lda #$3f
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$0b
    sta $2100
    lda #$4f
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$0a
    sta $2100
    lda #$5f
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$09
    sta $2100
    lda #$6f
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$08
    sta $2100
    lda #$7f
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$07
    sta $2100
    lda #$8f
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$06
    sta $2100
    lda #$9f
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$05
    sta $2100
    lda #$af
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$04
    sta $2100
    lda #$bf
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$03
    sta $2100
    lda #$cf
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$02
    sta $2100
    lda #$df
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$01
    sta $2100
    lda #$ef
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@
    lda #$00
    sta $2100
    lda #$ff
    sta $2106
    jsr _M_FOTM_WAI_KTHX_\@

    jmp _M_FOTM_ENDE_\@

_M_FOTM_WAI_KTHX_\@:
    wai
    wai
    wai
    wai
    rts

_M_FOTM_ENDE_\@:
    lda #$0f
    sta $2106
    pla
.ENDM


.MACRO FADE_IN
    pha
    phx
    phy
    ldy   $100E
    stz   $100E
_M_FIN_R_\@:
    ldx   #$04
_M_FIN_L_\@:
    wai
    dex
    cpx   #$00
    bne   _M_FIN_L_\@
    inc   $100E
    lda   $100E
    sta   $2100
    cmp   #$0F
    bne   _M_FIN_R_\@
_M_FIN_E_\@:
    sty   $100E
    ply
    plx
    pla
.ENDM


.MACRO FADE_OUT
    pha
    phx
    phy
    ldy   $100E
    lda   #$0F
    sta   $100E
_M_FOT_R_\@:
    ldx   #$04
_M_FOT_L_\@:
    wai
    dex
    cpx   #$00
    bne   _M_FOT_L_\@
    dec   $100E
    lda   $100E
    sta   $2100
    cmp   #$00
    bne   _M_FOT_R_\@
_M_FOT_E_\@:
    sty   $100E
    ply
    plx
    pla
.ENDM


;================================


.MACRO INTROTXT_LOAD_PAL
    lda #:\1
    ldx #\1
    jsl DMA_INTROTXT_PAL
.ENDM

.MACRO LOAD_PAL
    lda   #:\1
    ldx   #\1
    jsl   DMA_PAL
.ENDM


.MACRO LOAD_BLOCK2VRAM
    ldx   #\2                   ; Destination
    stx   $2116
    lda   #:\1                  ; Source bank
    ldx   #\1                   ; Source offset
    ldy   #\3                   ; Size
    jsl   LOAD_VRAM
.ENDM


;=================================================================


.BANK 0 SLOT 0
.ORG HEADER_OFF
.SECTION "loadvramcode" SEMIFREE

; NOTE: Make sure that you have set the VRAM address *before*
; calling this routine!

LOAD_VRAM:
    pha
    phx
    phy
    phb
    php
    sep   #$20
    stz   $420B
    stx   $4302
    sty   $4305
    sta   $4304
    lda   #$80
    sta   $2115
    lda   #$01
    sta   $4300
    lda   #$18
    sta   $4301
    lda   #$01
    sta   $420B
    plp
    plb
    ply
    plx
    pla
    rtl

.ENDS


;=================================================================


.BANK 0 SLOT 0
.ORG HEADER_OFF
.SECTION "dmapalcode" SEMIFREE

DMA_PAL:
    pha
    phx
    phb
    php
    sep   #$20
    stz   $2121
    stz   $420B
    stx   $4302
    ldx   #$0200
    stx   $4305
    sta   $4304
    lda   #$00
    sta   $4300
    lda   #$22
    sta   $4301
    lda   #$01
    sta   $420B
    plp
    plb
    plx
    pla
    rtl


.ENDS


;=================================================================


.BANK 0 SLOT 0
.ORG HEADER_OFF
.SECTION "dmaintrotxtpal" SEMIFREE

DMA_INTROTXT_PAL:
    pha
    phx
    phb
    php
    sep #$20
    pha
    lda #$80
    sta $2121
    pla
    stz $420B
    stx $4302
    ldx #$0020
    stx $4305
    sta $4304
    lda #$00
    sta $4300
    lda #$22
    sta $4301
    lda #$01
    sta $420B
    plp
    plb
    plx
    pla
    rtl


.ENDS


;=================================================================


.BANK 0 SLOT 0
.ORG HEADER_OFF
.SECTION "vramclear"

CLEAR_VRAM

    lda #$80                   ; increase VRAM address on write $2119
    sta $2115
    ldx #$0000
clrVRM_loop
    stz $2118
    stz $2119
    inx
    cpx #$8000
    bne clrVRM_loop
    rtl

.ENDS
