RAM_Frames EQU $FF0280
RAM_TempDMA EQU $FF0300
RAM_FadeFlag EQU $FF027E
RAM_FadeCnt  EQU $FF027C
RAM_VSCROLLCNT EQU $FF027A
RAM_FrameCNT2 EQU $FF0278
RAM_PatternCue EQU $FFA000
    
Wait macro asd
    move.w #\asd, d7
    jsr WAITD7
    endm
WAITBTN:
	btst 	#5, ($A10003)
    bne.s WAITBTN
    WAIT 20
    rts
    
LiquifyVBL:
        clr.w (RAM_HCount)
        lea (RAM_ScrollTable), a2
                SetVSRAMAddr 0, VDPControl
        move.w (a2)+, VDPDATA
        move.w #0, VDPDATA
                  move.w (RAM_Frames), d2
        lea (Frames), a5
        lea (a5,d2.w), a5
        move.l (a5), a5
        jsr (a5)  
        rte
            
LiquifyVBL2:
        lea (Frames), a5
        lea (a5,d2.w), a5
        move.l (a5), a5
        jsr (a5)  
        rte
        
Liquify:
        waitframe
        Echo_z80request
        move.b ($A01200), d0
        move.b #0, ($A01200)
        echo_z80release
        tst.b d0
        beq.s Liquify
        clr.w (RAM_FadeCNT)
        disableints
        move.l #LiquifyVBL, (RAM_VINTLOC+2)
        move.w #$8B0B, (VDPControl)
        lea (ScaleHBL), a0
        lea (RAM_HInt), a1
        move.l #((ScaleHBL_End-ScaleHBL)/2)-1,d0   
@CopyHINT:
        move.w (a0)+, (a1)+
        dbf d0, @CopyHINT 

        SingleDMAVRAM cityTiles, 520*32, 115*32, VDPControl
        SingleDMAVRAM Frame1, 0*32, 518*32, VDPControl
        SingleDMACRAM cityPal, 32, 32, VDPControl 
        SingleDMACRAM frame1Pal, 0, 32, VDPControl 
    
	
        SingleDMAVRAM die, $360*32, 64*32, VDPControl
        
        setvramaddr $E000, vdpcontrol
        lea (citymap), a0
        move.w #28*64, d0
@copymap:
        move.w (a0)+, d1
        add.w #520, d1
        or.w #%0010000000000000, d1
        move.w d1, (vdpdata)
        dbf d0, @copymap
        
        
    jsr LoadFrame1
    
    
    
       ENABLEINTS
	moveq #0, d0
	moveq #0, d1
	moveq #0, d2
	moveq #0, d3
	moveq #0, d4
	moveq #0, d5
	moveq #0, d6
	move.w #$F0, d6 
	moveq #0, d7
	lea (UberSine), a1
	lea (UberCoSine), a0 ; Cosine
        lea (RAM_Scrolltable), a3

    move.w #-50, (RAM_Framecnt)
    move.w #0, (RAM_GreetzID)
    
    
    move.w #$8712, (vdpcontrol)
    
    
	move.w #224-1, d1
	moveq #0, d4   
    
@DatLoop:
    move.w #$8F04, (vdpcontrol)
    setvramaddr $F802, vdpcontrol
    move.w #56, d2
    neg.w d7
@doscroll:    
    move.w d7, (vdpdata)    
    dbf d2, @doscroll
    neg.w d7
    move.w #$8F02, (vdpcontrol)    
@Start:
    

        move.w  d1, d0              ; n = x  
        add.w   d5, d0              ; n = x + time*2  
        move.b  (a0,d0.w), d3       ; Get result  
      
        add.w   d1, d0              ; n = x*2 + time*2  
        sub.w   d7, d0              ; n = x*2 + time  
        add.b   (a1,d0.w), d3       ; Get result        
        move.w  d4, d0              ; n = y  
        add.w   d5, d0              ; n = y + time*2  
        add.b   (a0,d0.w), d3       ; Get result        
        add.w   d4, d0              ; n = y*2 + time*2  
        sub.w   d7, d0              ; n = y*2 + time  
        add.b   (a1,d0.w), d3       ; Get result  
        move.b (a0,d3.w), d3        
        move.w d3, (a3)+
        dbf d1, @Start          ; X DIRECTION  LOOP

	move.w #224-1, d1
	
;        add.w #128-80, a3
;        dbf d4, @Start	        ; Y DIRECTION LOOP

        addq.w #1, RAM_FRAMECNT
        
        
        tst.w (RAM_FADEFLAG)
        bne.w @NeedMusic
        
        
        Echo_z80request
        move.b ($A01200), d0
        move.b #0, ($A01200)
        echo_z80release
        tst.b d0
        beq.s @NeedMusic
        
        move.w #$FF, (RAM_FadeFlag)
        
@NeedMusic
        tst.w (RAM_FADEFLAG)
        beq.w @NoFade
        
        Echo_z80request
        move.b ($A01200), d0
        move.b #0, ($A01200)
        echo_z80release
        tst.b d0
        bne.w @wefinished    
;       jmp *
        addq.w #5, (RAM_FADECNT)
        move.w (RAM_FADECNT), d0
        subq.w #1, d0
        lea (RAM_Scrolltable), a3 ;;;
@OverwriteMem        
        move.w #0, (a3)+
        dbf d0, @OverwriteMem
        
@NoFade 
        WaitFrame
        
        addq.w #4, (RAM_Frames)
        cmp.w #12*3, (RAM_Frames)
        
        bne.s @not1
        clr.w (RAM_Frames)
@not1
        lea (RAM_ScrollTable), a3
;        moveq #27, d4
        add.l #$00005000, d6
        swap d6
        move.b (a1, d6.w), d4
        swap d6
        addq.w #2, d7
        cmp.w #$400, d7
        bne.s @asda
        moveq #0, d7
@asda:
       move.w d7, d5
       add.w d5, d5
        
        bra @DatLoop
@Plasma_End:
        rts
        
@WeFinished:    
    disableints
    move.w #$8712, VDPCONTROL
    move.w #$8b07, VDPCONTROL
    setvramaddr $C000, vdpcontrol
    move.w #32*64, d0
@CLRSCROLL
    move.w #0, (VDPDATA)
    dbf d0, @CLRSCROLL

    SingleDMAVRAM GreetzTiles, $300*32, 64*32, VDPCONTROL
    SingleDMAVRAM GreetsTiles, $400*32, 293*32, VDPCONTROL
    SingleDMACRAM GreetzPal, 64, 32, VDPCONTROL
	
        SingleDMAcRAM diepal, 96, 32, VDPControl
    
	
    jsr LoadTheGreetz
    
        clr.w (RAM_Frames)
 
	lea (PatternCue), a0
	lea (RAM_PatternCue), a1
	rept 8
	move.l (a0)+, (a1)+
	move.l (a0)+, (a1)+
	endr
	
        WaitFrame
    move.w #$C000+224/8*64*2, (RAM_VSCROLLCNT)
    move.w  #0, (RAM_FRAMECNT)
    move.w  #0, (RAM_FRAMECNT2)
@DatLoop2

    move.w #$8F04, (vdpcontrol)
    setvramaddr $F802, vdpcontrol
    move.w #56, d4
    neg.w d7 
@UpdScroll
    move.w d7, (vdpdata)    
    dbf d4, @UpdScroll
    neg.w d7
    move.w #$8F02, (vdpcontrol)   
    
                  move.w (RAM_Frames), d2
        lea (Frames2), a5
        lea (a5,d2.w), a5
        move.l (a5), a5
        jsr (a5)  
        jsr LoadTheGreetz
        
        jsr CREDITSSCROLL
        move.w #$8F02, vdpcontrol
		singledmavram RAM_PatternCue, $F000, 8*8, VDPCONTROL

        WaitFrame
        
        
        Echo_z80request
        move.b ($A01200), d0
        move.b #0, ($A01200)
        echo_z80release
        tst.b d0
        bne.w @NextPart  
        
        addq.w #1, (RAM_FRAMECNT)
        addq.w #4, (RAM_Frames)
        cmp.w #12*3, (RAM_Frames)
        
        bne.s @DontClrFrames
        clr.w (RAM_Frames)
@DontClrFrames
        addq.w #2, d7
        cmp.w #$400, d7
        bne.s @DontReset
        moveq #0, d7
@DontReset
       move.w d7, d5
       add.w d5, d5
        
        bra @DatLoop2
        
@NextPart
        rts
CreditsScroll
    movem.l d0-d2/a0-a2, -(sp)
    move.w (RAM_FRAMECNT), d0
  ; neg.w d0
    move.w #$8F04, VDPCONTROL
    setvsramaddr 0, VDPCONTROL
    rept 10
    move.w d0, (VDPDATA)
    endr
	
	cmp.w #136, d0
	blt.s @Less128
	lea (Random256), a2
	lea (a2, d0.w), a2
	lea (RAM_PatternCue+4), a1
	move.w #8-1, d1
@WriteSprites: 
	move.b (a2)+, d2
	and.w #%00000011, d2
	lsl.w #4, d2
	add.w #$360, d2
	or.w #%1110000000000000, d2
	move.w d2, (a1)
	lea 8(a1), a1
	dbf d1, @WriteSprites
@Less128
    move.w #$8F02, VDPCONTROL
  ; neg.w d0
    and.b #%00000111, d0
    tst.b d0
    bne.w @NoUpdate
    
    move.w (RAM_FRAMECNT2), d0
    lea (GreetsMap), a0
    lea (a0,d0.w), a0
    
    
    add.w  #64*2, (RAM_FRAMECNT2)
    move.w (RAM_VscrollCNT), d0
    add.w #64*2, d0
    and.w #$CFFF, d0 
@NoReset
    move.w d0,  (RAM_VSCROLLCNT)
    jsr CalcOffset
    add.w #112/8*64*2, d0
    and.w #$CFFF, d0
    move.w d0, d2
    
    
    rept 16
        move.w (a0)+, d0
        add.w #$400, d0
        or.w #%0110000000000000, d1
        move.w d0, VDPDATA 
    endr
    moveq #0, d0
    
    move.w d2, d0
    jsr CalcOffset
    rept 16
        move.w #0, VDPDATA 
    endr
    
    
    
@NoUpdate
    movem.l (sp)+, d0-d2/a0-a2
    rts
;13792
BigassSine:
        incbin ./liquify/sine.wave

LoadTheGreetz:
    
    lea (GreetzMAP), a0
    lea (vdpdata), a1
    setvramaddr $E000+7*64*2+0, VDPCONTROL
    move.w #16*2-1, d0

p = 0
    while p<3
    lea (GreetzMAP+p*64*2), a0
    setvramaddr $E000+7*64*2+p*64*2, VDPCONTROL
    rept 16
        move.w (a0)+, d1
        or.w #%0100000000000000, d1
        add.w #$300, d1
        move.w d1, (a1)
    endr
p = p+1
    endw
    rts
UberSine:   equ BigassSine

UberCosine: equ UberSine+$100

Frames:
    dc.l LoadFRAME1, LoadFRAME1, LoadFRAME1
    dc.l LoadFRAME2, LoadFRAME2, LoadFRAME2
    dc.l LoadFRAME3, LoadFRAME3, LoadFRAME3
Frames2:
    dc.l LoadFRAME1s, LoadFRAME1s, LoadFRAME1s
    dc.l LoadFRAME2s, LoadFRAME2s, LoadFRAME2s
    dc.l LoadFRAME3s, LoadFRAME3s, LoadFRAME3s

LoadFrame1s:
p = 0
    while p<=8
    singledmavram Frame1map+(p*64*2+16*2), $C000+(p*64*2+16*2), 40*2-16*2, VDPCONTROL    
    
p = p+1
    endw
    while p<=27
    singledmavram Frame1map+(p*64*2), $E000+(p*64*2), 40*2, VDPCONTROL    
    
p = p+1
    endw
    rts
LoadFrame2s:
p = 0
    while p<=8
    singledmavram Frame1map+(p*64*2+16*2)+28*64*2, $C000+(p*64*2+16*2), 40*2-16*2, VDPCONTROL    
    
p = p+1
    endw
    while p<=27
    singledmavram Frame1map+(p*64*2)+28*64*2, $E000+(p*64*2), 40*2, VDPCONTROL    
    
p = p+1
    endw
    rts
LoadFrame3s:
p = 0
    while p<=8
    singledmavram Frame1map+(p*64*2+16*2)+56*64*2, $C000+(p*64*2+16*2), 40*2-16*2, VDPCONTROL    
    
p = p+1
    endw
    while p<=27
    singledmavram Frame1map+(p*64*2)+56*64*2, $E000+(p*64*2), 40*2, VDPCONTROL    
    
p = p+1
    endw
    rts
    
LoadFrame1:
p = 0
    while p<=27
    singledmavram Frame1map+(p*64*2), $C000+(p*64*2), 40*2, VDPCONTROL    
    
p = p+1
    endw
    rts
LoadFrame2:
p = 0
    while p<=27
    singledmavram Frame1map+(p*64*2)+28*64*2, $C000+(p*64*2), 40*2, VDPCONTROL    
    
p = p+1
    endw
    rts
LoadFrame3:
p = 0
    while p<=27
    singledmavram Frame1map+(p*64*2)+56*64*2, $C000+(p*64*2), 40*2, VDPCONTROL    
    
p = p+1
    endw
    rts
    
    align 131072
frame1:
    incbin ./gfx/frame1.bin
    
frame1pal:
    incbin ./gfx/frame1.pal
    even
    align 131072
frame1map:
    incbin ./gfx/frame1.map
    even
    align 131072
CityTiles: incbin ./gfx/city.bin
CityPAL: incbin ./gfx/city.pal
CityMAP: incbin ./gfx/city.map
    even
GreetzTiles:
    incbin ./liquify/greetz.bin
GreetzMAP:
    incbin ./liquify/greetz.map
GreetzPAL:
    incbin ./liquify/greetz.pal
    
PatternCue:
x = 0
	while x<8
	dc.w 128+8*8+2*8
	dc.b $f, x+1
	dc.w %1110000000000000|$350
	dc.w 120+x*16
x = x+1
	endw
    
Greetsmap:
    incbin ./liquify/greets.map

	rept 64*2*64
	dc.w $350
	endr
    
Greetspal:
    incbin ./liquify/greets.pal
    
Greetstiles:
    incbin ./liquify/greets.bin
    
die:
    incbin ./liquify/die.bin
    
diepal:
    incbin ./liquify/diep.bin