;  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!

RAM_ScrollTable EQU $FF8000

; ---
Scalerator:
        rept 50
        waitframe
        endr
        disableints
        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
@fuck:
        moveq #0, d3
        moveq #2, d2
@Looper
        
        move.w #112, d0
        move.l #112, d1
        addq.w #1, d3
        addq.w #1, d2
        cmp.w #112, d2
        bne.s @l2
        jmp @fuck
@l2
        bsr GenScroll

        WaitFrame

	    btst #5, ($A10003)
        beq.s @Scaler_End
        bra.s @Looper
@Scaler_End
        rts

ScaleHBL:
        SetVSRAMAddr 0, VDPControl
        move.w (a2)+, VDPDATA
        move.w #0, VDPDATA
        rte
ScaleHBL_End:

ScaleVBL:
        clr.w (RAM_HCount)
        lea (RAM_ScrollTable), a2
                SetVSRAMAddr 0, VDPControl
        move.w (a2)+, VDPDATA
        move.w #0, VDPDATA
        rte
        

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 = FLIP
; d5 = Control Reg 2
; d6 = Control reg 3
; d7 = VSCROLL Value

        movem.l d0-d3/a0-a1, -(sp)
        tst.w d3
        beq.s @EndGen
        lea (RAM_ScrollTable), a0
        lea (DivTable), a1
        adda.w d2, a0
        adda.w d2, a0   
        
        sub.w d2, d0
        
        subq.w #1, d3
        move.l d3, d2   
        
        lsl.w #8, d3
        or.b d1, d3
        lsl.l #2, d3
        tst.w d4
        beq.w @NotFlip
@Flip:     
;       neg.w d0
        add.w d1, d0
        swap d0
        move.l (a1, d3.l), d1
@Loop3
        sub.l d1, d0
        sub.l #$00010000, d0
        swap d0
        move.w d0, (a0)+
        swap d0
        dbf d2, @Loop3
        
        movem.l (sp)+, d0-d3/a0-a1
        rts
@NotFlip             
        swap d0
        move.l (a1, d3.l), d1
        sub.l #$00010000, d1
                
@Loop2: 
        add.l d1, d0
        swap d0
        move.w d0, (a0)+
        swap d0
        dbf d2, @loop2   
@EndGen      
        movem.l (sp)+, d0-d3/a0-a1
        rts

DivTable:
        incbin "./luts/divtable.bin"

; 1359 HOLY SHIT IT WORKS YEEEAAAAAAH :D
