;RAM_PaletteATM EQU $FF1400
;RAM_PaletteTO EQU $FF1500
;Ram_PaletteBuilt EQU $FF1600
;
;

End:

    singledmavram EndTiles, 0, 133*32, vdpcontrol
    singledmavram EndMap, $C000, 64*32*2, vdpcontrol

    lea RAM_PaletteATM, a2
    moveq #16-1, d0
@FillWhite
    move.b #$0E, (a2)+
    move.b #$0E, (a2)+
    move.b #$0E, (a2)+
    dbf d0, @FillWhite
    
    lea EndPal, a0
    
    moveq #16-1, d7
    lea RAM_PaletteTo, a1
@Loadmajig
    move.b (a0)+, (a1)+
    move.b (a0), d0
    lsr.b #4, d0
    move.b d0, (a1)+
    move.b (a0)+, d0
    and.b #$0F, d0
    move.b d0, (a1)+    
    dbf d7, @Loadmajig
    
    
    moveq #0, d3
    moveq #0, d4
    moveq #0, d5
        
@endloop
    lea RAM_PaletteATM, a0
    lea RAM_PaletteTO, a1

    lea (a0, d3.w), a0
    lea (a1, d3.w), a1
    
    
    moveq #16-1, d7
    
@checkloop
    move.b (a0), d0
    move.b (a1)+, d1
    cmp.b d0, d1
    bge.s @dontcare
    subq.b #2, (a0)
    
@dontcare

    addq.l #3,a0
    addq.l #2,a1
    
    dbf d7, @checkloop
    
    lea RAM_PaletteATM, a0
    lea RAM_PaletteBuilt, a1
    
    moveq #16-1, d6
    
@BuildPal:
    move.b (a0)+, (a1)+
    move.b (a0)+, d0
    lsl.b #4, d0
    or.b (a0)+, d0
    move.b d0, (a1)+
    dbf d6, @BuildPal    

    SingleDMACram RAM_PaletteBuilt, 0, 32, VDPControl    
    
    WaitFrame
    
    addq.w #1, d4
    
    cmp.w #25, d4
    
    blt.s @nope
    
    bra.s @MOARFXLOL
@nope
    
    addq.w #1, d3
    cmp.b #3, d3
    blt.w @Endloop
    moveq #0, d3
    bra.w @Endloop
    
    jmp *
    rts
    
@MoarFXLOL
    lea (WAitframes_end), a6
    
    move.w (a6)+, d7    
    jsr WAITD7
    
    move.w #$0040, d0
    
    setvsramaddr 0, vdpcontrol
    move.w d0, (Vdpdata)
    
    move.w (a6)+, d7
    jsr waitd7
    
    setvsramaddr 0, vdpcontrol
    move.w #0, (Vdpdata)
    
    move.w #$8b03, (VDPCONTROL)
    
    move.w (A6)+, d7
    jsr waitd7
    
    move.w (A6)+, d7

    jsr EndHorizDistort
    
    setvramaddr $f800,vdpcontrol
 
    move.w #224, d6
@clr1:
    move.l #0, vdpdata
    dbf d6, @clr1
    
    move.w (A6)+, d7
    jsr waitd7
    
    move.w #$0060, d0    
    setvsramaddr 0, vdpcontrol
    move.w d0, (Vdpdata)
    
    move.w (A6)+, d7

    jsr EndHorizDistort  
    
    setvramaddr $f800,vdpcontrol
 
    move.w #224, d6
@clr2:
    move.l #0, vdpdata
    dbf d6, @clr2
    
    setvsramaddr 0, vdpcontrol
    move.w #0, (Vdpdata)
    
    move.w #$8b00, (VDPCONTROL)
    
    
    move.w (A6)+, d7
    jsr waitd7
    
    
    setvramaddr $f800,vdpcontrol
    move.w #$20, vdpdata
    
    lea (Endpal+32), a0
    lea $FF8002, a1
    
    rept 32
    move.b -(a0), (a1)+    
    endr
    
    
    singledmacram $ff8002, 2, 32, vdpcontrol
    
    
    move.w (A6)+, d7
    jsr waitd7
    
    setvramaddr $f800,vdpcontrol
    move.w #$00, vdpdata
    
    singledmacram EndPal, 0, 32, vdpcontrol
    
    move.w #$8b03, (VDPCONTROL)
    
    
    move.w (A6)+, d7
    jsr waitd7
    
    
    disableints
@END_DISTORTER
   
	move.w #100, d7 ; wait 2 secs
	jsr WAITD7
		singledmavram unlockedtiles, $100*32, 69*32, VDPCONTROL
    singledmacram unlockedPal, 32, 32, vdpcontrol
	lea (unlockedmap), a0
	lea (vdpdata), a1
	move.w #$8B00, vdpcontrol
	setvramaddr $F802, vdpcontrol
	move.w #6, (a1)
	
	setvramaddr $E000, vdpcontrol
	move.w #28*64-1, d0
@Unlockmaps

	move.w (a0)+, d1
	add.w #$100, d1
	or.w #%0010000000000000, d1
	
	move.w d1, (a1)
	dbf d0, @Unlockmaps
	
	
	jmp *
	
    rts

WAITD7:
    waitframe
    dbf d7, WAITD7
    rts
    
EndHorizDistort:
    movem.l d0-d7/a0-a6, -(sp)
    lea (Random256), a5
    lea (Random256), a4
    moveq #0, d6
@horizdistort
    setvramaddr $F800, vdpcontrol
    move.w #224*2, d5
@distort:

    move.b (a5)+, d0
    and.w #$00FF, d0
    ;add.w #384, d0
    sub.w #$80, d0
    lsr.w d3, d0
    move.w d0, (VDPDATA)
    dbf d5, @distort
    waitframe
    move.b (a4)+, d6
    lea (random256), a5
    lea (a5, d6.w), a5
    dbf d7, @horizdistort 
    
    movem.l (sp)+,d0-d7/a0-a6
    rts
Waitframes_end:
    dc.w 33
    dc.w 1
    dc.w 10
    dc.w 10
    dc.w 10
    dc.w 1
    dc.w 16
    dc.w 3
    dc.w 50
    
random256:
    incbin ./luts/random256.bin
    incbin ./luts/random256.bin
    incbin ./luts/random256.bin
    even
randommap:

    incbin ./end/randommap.bin
    even

EndTiles:
    incbin ./end/end.bin
Endmap:
    incbin ./end/end.map
Endpal:
    incbin ./end/end.pal
	align 131072
unlockedtiles:
    incbin ./end/unlocked.bin
unlockedmap:
    incbin ./end/unlocked.map.bin ; fuck windows filenames
unlockedpal:
    incbin ./end/unlocked.pal.bin
    
    even
    
FadeWHITE:

    moveq #15, d7
    
@FADELOOP:
    lea RAM_PaletteATM, a0

    moveq #(3*16)-1, d6
@ColorParse
    move.b (a0)+, d0
    tst.b d0
    beq.s @dontcare
    subq.b #1, d0
    move.b d0, -1(a0)
@dontcare
    dbf d6, @ColorParse

    lea RAM_PaletteATM, a0
    lea RAM_PaletteBuilt, a1
    
    moveq #16-1, d6
@BuildPal:
    move.b (a0)+, (a1)+
    move.b (a0)+, d0
    lsl.b #4, d0
    or.b (a0)+, d0
    move.b d0, (a1)+
    dbf d6, @BuildPal

    SingleDMACram RAM_PaletteBuilt, 0, 32, VDPControl
    
    WaitFrame
    WaitFrame
    dbf d7, @FADELOOP

    rts