RAM_NamesA equ $ff8000
RAM_NamesB equ $ff9000
RAM_Temp   equ $ffa000
RAM_PSAT   equ $ffb000

Plasma_FX:
    ; Init
    DISABLEINTS

    ; Load data
    move.l  #PlasmaVINT, (RAM_VINTLOC+2)
    move.w  #$8b03, (vdpcontrol)
    
	; Prepare plasma
    lea     (PlasmaBlocksA), a0
    move.w  #32, d1
    move.l  #$40000000, (VDPCONTROL)
@Load
    rept 16
        rept 16
            move.w (a0)+, (VDPDATA)
        endr
        sub.l   #32, a0
    endr
    add.l   #32, a0
    dbf     d1, @Load
    
    SingleDMACRAM PlasmaGradient,  $00, 32, VDPControl
    SingleDMACRAM PlasmaGradient2, $20, 32, VDPControl
	
	; Prepare sprite
	move.w  #$8540, VDPControl ; Sprite Attr Table Base = $8000
	SingleDMAVRAM SpriteTiles,   $4000, (128*96)/2, VDPControl
	SingleDMACRAM SpritePalette, $40,   32,         VDPControl
	
    ; Copy attr table into RAM
    lea     (ROM_PSAT), a0
    lea     (RAM_PSAT), a1   
    move.l  #(640/4)-1, d0
@CopyAttrTable:
    move.l  (a0)+, (a1)+
    dbf d0, @CopyAttrTable
	
    ; Go!
    ENABLEINTS
    
    move.w  #$8B0F, (VDPCONTROL)

    lea     Zeros, a0
    movem.l (a0)+, d0-d7
    
    lea     (Sine), a1
    lea     (BigSine), a0 ; Cosine
    lea     (RAM_NamesA), a2
    lea     (RAM_NamesB), a3
    lea     (RAM_Temp), a4

    move.w   #10*50+2, d1
    moveq   #28-1, d4   

@StartY:
    move.b  d4, d0          ; n = y  
    add.b   d5, d0          ; n = y + time*2  
    move.b  (a0,d0.w), d6   ; Get result  
      
    add.b   d4, d0          ; n = y*2 + time*2  
    sub.b   d7, d0          ; n = y*2 + time  
    move.b  (a1,d0.w), d2   ; Get result 

x   = 39
    while x>=0
    moveq   #0, d3

    ; Calc X
    move.b  #x, d0          ; n = x  
    add.b   d5, d0          ; n = x + time*2
    move.b  (a0,d0.w), d3   ; Get result

    add.b   #x, d0          ; n = x*2 + time*2  
    sub.b   d7, d0          ; n = x*2 + time
    add.b   (a1,d0.w), d3   ; Get result

    ; Add Y Components
    add.b   d6, d3
    add.b   d2, d3
    
    ; Plane A
    move.w  d3, (a2)+
    
    ; Plane B
    not.w   d3
    and.w   #$ff, d3
    or.w    #$2100, d3
    move.w  d3, (a3)+

x   = x-1
    endw
    
    add.w   #128-80, a2
    add.w   #128-80, a3
    dbf d4, @StartY         ; Y DIRECTION LOOP
        
    WaitFrame
    move.w  #$8000, a2
    move.w  #$9000, a3
    moveq   #27, d4
    addq.b  #1, d7
    move.b  d7, d5
    add.b   d5, d5
    
	dbf d1, @StartY
@Plasma_End:
    rts
    
PLASMAVINT:
	SingleDMAVRAM RAM_PSAT,   $8000, 96, VDPControl  
    SingleDMAVRAM RAM_NamesA, $C000, $e00, VDPControl
    SingleDMAVRAM RAM_NamesB, $E000, $e00, VDPControl
    rte
        

	even
Sine:
    incbin ./plasma/sinetable.bin
    incbin ./plasma/sinetable.bin
    incbin ./plasma/sinetable.bin
    incbin ./plasma/sinetable.bin
    incbin ./plasma/sinetable.bin
    incbin ./plasma/sinetable.bin
	
    even
BigSine:
    incbin ./plasma/sinetable2.bin
    incbin ./plasma/sinetable2.bin
    incbin ./plasma/sinetable2.bin
    incbin ./plasma/sinetable2.bin
    incbin ./plasma/sinetable2.bin
    incbin ./plasma/sinetable2.bin
    
	even
SineScroll
    incbin ./plasma/sinescroll.bin
	
    even
PlasmaGradient:
    incbin ./plasma/gradient.bin
	
    even
PlasmaGradient2:
    incbin ./plasma/gradient3.bin

    even
SpritePalette:
	incbin ./plasma/titansprite.pal
	
	even
SpriteTiles:
	incbin ./plasma/titansprite.bin

	even
ROM_PSAT:
sx	= 		96
sy	= 		64
tx  =       4
ty  =       3

y	=		0
	while y<ty
x	=		0
	while x<tx
	
	dc.w	(128+sy+(y*32)) ; y
	dc.b	%00001111       ; 4x4
	dc.b	((y*4)+x+1)     ; link
	;        pccvhnnnnnnnnnnn
	dc.w	(%0100001000000000)|(((y*4)+x)*16)
	dc.w    (128+sx+(x*32)) ; x

x	= 		x+1
	endw
y	= 		y+1
	endw

	rept 64-(tx*ty)
	dc.l	0, 0
	endr
	
	even
PlasmaBlocksA:
i   =       0
    while   i<16
v   =       i|i<<4
rgt =       i<<4
lft =       i
    dc.l    0
    dc.b    0, v, v, 0
    rept    4
    dc.b    lft, v, v, rgt
    endr
    dc.b    0, v, v, 0
    dc.l    0
i   =       i+1
    endw
    
	even
PlasmaBlocksB:
i   =       15
    while   i>=0
v   =       i|i<<4
rgt =       i
lft =       i<<4
    dc.b    v, v, v, v
    dc.b    v, 0, 0, v
    rept    4
    dc.b    lft, 0, 0, rgt
    endr
    dc.b    v, 0, 0, v
    dc.b    v, v, v, v
i   =       i-1
    endw
    
	even
Zeros:
    rept 8
    dc.l    0
    endr