;  Scaler test V2.0, now hopefully working.

; theory behind this:
; Stationary line would equal to incrementing the scroll value (Scroll upwards) every line, by 1
; 
; Scaling to n lines with an original image size and x being the curent line number, the formula
; would look as such:
;
; VSCROLL = (imgsize / n) * x - x
; 
; This is a test implementation of that formula. Let's do this!

; Equates!

; GenScroll:
; a0 = Scroll table
; a1 = Division table
; d0 = Original image start line
; d1 = Original image size
; d2 = Target image start line
; d3 = Target image size
; d4 = Control Reg 1
; d5 = Control Reg 2
; d6 = Control reg 3
; d7 = VSCROLL Value

RAM_GunstarCounter EQU $FF0300
ram_DIRECTION equ $ff0400
ram_VisiblePARTS equ $ff0408
ram_loopsmade equ $ff040A

Gunstar: ;!!!!!!!! A beh zeh deh
       
	   moveq #0, d0
	   moveq #0, d1
	   moveq #0, d2
	   moveq #0, d3
	   moveq #0, d4
	   moveq #0, d5
	   moveq #0, d6
	   moveq #0, d7
	   move.l #0, a0
	   move.l #0, a1
	   move.l #0, a2
	   move.l #0, a3
	   move.l #0, a4
	   move.l #0, a5
	   move.l #0, a6
        
@needmusic90
        waitframe
        Echo_z80request
        move.b ($A01200), d0
        move.b #0, ($A01200)
        echo_z80release
        tst.b d0
         beq.w @needmusic90        
        
        disableints

		
       move.w  #$9011, (VDPControl)

;       move.w #$8C00+%00000000, (VDPControl)


        singleDMAVRAM GunstarTiles, 0, 725*32, VDPControl
        
        
        SingleDMACRAM GunstarPal, 0, 32, VDPControl
        
        SingleDMAVRAM GunstarMap, $C000+32*64*2, 64*32*2, VDPControl
        
        


        move.l #ScaleVBL, (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   
        
        enableints
        
        move.w #256, d0
        move.w #64,  d1
        move.w #0, d2
        move.w #64, d3
        move.w #0, d4
   ;    jsr Genscroll
   move.w #256, d5
        
  ;    jmp *
        lea (GunstarSine+512), a4
        lea (GunstarSine), a5
        moveq #0, d7
@Intro1
        lea ($FF8000), a0
        move.w #224*2/4, d6
@CLRMEM2
        move.l #0, (a0)+
        dbf d6, @CLRMEM2
        
        setcramaddr 0, vdpcontrol
        move.w #0, vdpdata

        move.w (a4, d7.w), d2
        lsr.w #1, d2
        add.w #8, d2
        move.w (a5, d7.w), d3
        
        jsr Genscroll
        
        waitframe
        
        add.w d5, d7
        cmp.w #768, d7
        bne.s @NoFlippage
        not.w     d4
@NoFlippage
        cmp.w #1024,d7
        bne.w @Intro1
       moveq #0, d7
        lsr.w #1, d5
        cmp.b #4, d5
        bne.w @intro1
        
        DisableInts
        
        setvsramaddr 0, vdpcontrol
        move.l #-48, vdpdata
        
        SingleDMAVRAM GunstarMap, $E000, 64*8*2, VDPControl
        

        waitframe
        
        move.w #$8F00, VDPCONTROL
        moveq #0, d6
        moveq #1, d7
        move.w #$9001, VDPCONTROL
        SETVSRAMADDR 2, VDPCONTROL
        rept 8*3
            rept 8
                add.w d7, d6
                move.w d6, vdpdata
                waitframe
            endr
        add.w #8, d7

        endr
         
        move.w #$8f02, VDPCONTROL
        move.w #68, d0
@loopmania
        SETVSRAMADDR 2, VDPCONTROL
                add.w d7, d6
                move.w d6, vdpdata
                waitframe
        dbf d0, @Loopmania
        SETVSRAMADDR 0, VDPCONTROL
                move.w d6, vdpdata
        
        setvramaddr $F800, vdpcontrol
        move.w #30, VDPDATA
        move.w #30, VDPDATA
        
        
        SingleDMAVRAM DDRTILES, $300*32, 193*32, VDPCONTROL
;        SingleDMAVRAM DDRMAP, $C000, 64*2*10, VDPControl
        move.w #$9011, VDPCONTROL
        move.w #$8b00, VDPCONTROL
        
        SETVRAMADDR $C000, vdpcontrol        
        move.w #$2000/4-1,d0
@CLRVS2
        move.l #0, VDPDATA
        dbf d0, @CLRVS2   
        SETVSRAMADDR $02, VDPCONTROL
        move.w #256, VDPDATA  
        SingleDMACRAM DDRPAL, 32, 32, VDPCONTROL
        setvramaddr $c000, vdpcontrol
        move.w #64*11, d0
        lea ddrmap, a0
@DDRMAPC:  
        move.w (a0)+, d2
        add.w #$300, d2
        or.w #%0010000000000000, d2
        move.w d2, VDPDATA
        
        dbf d0, @DDRMAPC
        
        SingleDMACRAM DDRPAL, 0, 32, VDPCONTROL
        SETVSRAMADDR 2, VDPCONTROL
        move.w #-255, VDPDATA 
        SETVRAMADDR $E000, vdpcontrol   
        lea DDRMAP+64*2*11, a0
        move.w #64*4-1, d0
@DDRMAPLOADER
        move.w (a0)+, d1
        add.w #$300, d1
        or.w #%1000000000000000, d1
        move.w d1, VDPDATA
        dbf d0, @DDRMAPLOADER
        
        move.w #64*5-1, d0
@DDRMAPLOADER2
        move.w #0, VDPDATA
        dbf d0, @DDRMAPLOADER2
        
        move.w #$8D20, VDPCONTROL
        setvramaddr $8000, vdpcontrol
        move.w #32, vdpdata
        move.w #32, vdpdata
        move.w #$8034, VDPCONTROL
        move.w #-255, d5
        move.w #16, d4
        move.w #32, d7
        move.w #13, d0 
        SETVSRAMADDR $2, VDPCONTROL
        move.w #-232, VDPDATA
        
        move.w #-232,  d6
        
@loopmania2
        SETVSRAMADDR 0, VDPCONTROL
                sub.w d4, d5
                move.w d5, vdpdata
                move.w d6, vdpdata
                waitframe
        dbf d0, @Loopmania2
        
        move.w #3, d0 
        move.w #-232,  d6
@loopmania3
        SETVSRAMADDR 0, VDPCONTROL
                add.w d7, d6
                sub.w d4, d5
                move.w d5, vdpdata
                move.w d6, vdpdata
                waitframe
        dbf d0, @Loopmania3
        

    
@needmusic8
        waitframe 
        Echo_z80request
        move.b ($A01200), d0
        move.b #0, ($A01200)
        echo_z80release
        tst.b d0
		beq.w @needmusic8
        
    lea DDRPAL, a0
    lea RAM_PaletteATM, a1
    moveq #16-1, d0
@FillWhite
    move.b (a0)+, (a1)+
    move.b (a0)+, (a1)+
    move.b (a0)+, (a1)+
    dbf d0, @FillWhite   
    
    lea RAM_PaletteTO, a1
    moveq #16-1, d0
@FillWhite2
    move.b #$00, (a1)+
    move.b #$00, (a1)+
    move.b #$00, (a1)+
    dbf d0, @FillWhite2
    
    lea DDRPAL, a0
    moveq #16-1, d7
    lea RAM_PaletteATM, 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
    
@needmusic9   
        waitframe 
        Echo_z80request
        move.b ($A01200), d0
        move.b #0, ($A01200)
        echo_z80release
        tst.b d0
		beq.w @needmusic9
@fxdone0
    jsr fadeout2
        
@needmusic7   
        waitframe 
        Echo_z80request
        move.b ($A01200), d0
        move.b #0, ($A01200)
        echo_z80release
        tst.b d0
		beq.w @needmusic7
		
        jsr SetVDPDefaults
        jsr ClearEverything
        
        
        move.l #ScaleVBL, (RAM_VINTLOC+2)
        move.w #$8B0B, (VDPControl)
        lea (ScaleHBL), a0
        lea (RAM_HInt), a1
        move.l #((ScaleHBL_End-ScaleHBL)/2)-1,d0   
@CopyHINT2
        move.w (a0)+, (a1)+
        dbf d0, @CopyHINT2 
        
        
        move.w #$9011, VDPCONTROL

        
        ; GENERATORATOR ITRO
        
            singleDMAVRAM GunstarTiles, 0, 725*32, VDPControl
        
        
        SingleDMACRAM GunstarPal+2, 2, 32, VDPControl
        move.w #$9011, vdpcontrol
        
        SingleDMAVRAM GunstarMap, $C000+32*64*2, 64*32*2, VDPControl    
        
        enableints
        moveq #0, d0
        moveq #0, d1
        moveq #0, d0
        moveq #0, d3
        moveq #0, d4
        moveq #0, d5
        moveq #0, d6
        moveq #0, d7
        moveq #0, d7

        
        
        setvsramaddr 0, VDPCONTROL
        move.w #72, VDPDATA
         
        
        moveq #0, d0
        moveq #0, d1
        moveq #0, d0
        moveq #0, d3
        moveq #0, d4
        moveq #0, d5
        moveq #0, d6
        moveq #0, d7
        moveq #0, d7
        
        clr.w RAM_FRAMECNT
        
        lea (Gunstars2), a1
        lea (GunstarLUT), a3
        move.w #64, d1 ; THIS WONT EVER CHANGE
@GunstarLoop

        move.w (a1, d7.w), d6        
        ; D6 = SINE TABLE VALUE
        lsl.w #2, d6 ; d6 = d6 * 8
        and.w #%1111111111111000, d6
        lea (RAM_ScrollTable), a4
        bsr CLRRAM
        move.w 0(a3, d6.w), d0
        move.b 2(a3, d6.w), d2
        move.b 3(a3, d6.w), d3
        and.w #$00ff, d2
        and.w #$00ff, d3
        jsr GenScroll


        move.w 4+0(a3, d6.w), d0
        move.b 4+2(a3, d6.w), d2
        move.b 4+3(a3, d6.w), d3
        and.w #$00ff, d2
        and.w #$00ff, d3
        jsr GenScroll

        add.w d3, d2
        add.w d2, d2
        subq.w #2, d2
        
        lea (RAM_ScrollTable), a4
        lea (a4, d2.w), a4
        bsr CLRRAM
    
        Echo_z80request
        move.b ($A01200), d0
        move.b #0, ($A01200)
        echo_z80release
        tst.b d0
        bne.w @FXdone
        
        waitframe
        addq.w #4, d7
        cmp.w #$1024, d7
        bne.w @gunstarloop
        moveq #0, d7
        
        bra.w @gunstarloop

@FXDone:    
            rts
      
CLRRAM:
        move.b 2(a3, d7.w), d2
        and.w #$00ff, d2
        add.w #32, d2 
@CLRAREA
        move.w #0, (a4)+
        dbf d2, @CLRAREA
        rts


FadeOUT2:
    moveq #0, d0
    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
    SingleDMACram RAM_PaletteBuilt, 32, 32, VDPControl
    
    WaitFrame
    WaitFrame
    WaitFrame
    WaitFrame
    dbf d7, @FADELOOP        
    rts
GunstarTiles:
    incbin ./gunstar/test.bin
GunstarPal:
    incbin ./gunstar/test.pal
GunstarMap:
    incbin ./gunstar/test.map

GunstarSIN:
    incbin ./gunstar/sine.bin
GunstarSINE:
    incbin ./gunstar/sinetable.bin
Gunstars2:
    incbin ./gunstar/sinetable2.bin
    
GunstarLUT:
    incbin ./gunstar/table.lut
        even
ddrtiles:
    incbin ./gunstar/ddr866.bin
ddrmap:
    incbin ./gunstar/ddr866.map
    
ddrpal:
    incbin ./gunstar/ddr866.pal