RAM_STRINGSTART: EQU $FF0200
RAM_STRINGPOS: EQU $FF0204
RAM_SCROLLMOD: EQU $FF0208
RAM_ScrollValue: EQU $FF020C
RAM_CurrentWidth: EQU $ff0210
RAM_CurrentPos: EQU $ff0220
RAM_StringEnded:  EQU $FF0224
RAM_BigscrollMap: EQU $FF0300
ram_fademan: equ $ff0230

BMPSCROLLOFFSET: EQU 120


VRAMADDR: macro reg
    and.l   #$FFFF, \reg
    lsl.l   #2, \reg
    lsr.w   #2, \reg
    or.w    #$4000, reg
    swap    \reg
    move.l  \reg, (a6)
    endm

BMPCRAMDMA: macro src, len

    move.w #$8154,(a6)
    move.l #$7FFC0003, (a6) ;vram write to $ffff for overflowing the fifomator
    moveq #0, d0
    
       btst #3, (a3)               ; Syncs to new frame
       beq.s *-4
 
        btst #3, (a3)               ; syncs to end of vblank
        bne.s *-4
		echo_z80request
        rept 5 
           move.w d0, (a5)              ; overflows the FIFO
        endr 
        
        rept 94                         ; Timing for a slot
           nop 
        endr 
        ; Execute DMA 
        
    
    move.l  #$94009300|(((\len)<<7)&$FF0000)|(((\len)>>1)&$FF), (a6)
    move.l  #$96009500|(((\src)<<7)&$FF0000)|(((\src)>>1)&$FF), (a6)
    move.l  #$97000000|((((\src)>>17)&$FF)<<16)|$8114, (a6) ; DMA + Display off
    move.l  #$C0000080, (a6)

	echo_z80release
    endm
    
BMPCRAMDMA_V: macro src, len, start

    move.w #$8154,(a6)
    move.l #$7FFC0003, (a6) ;vram write to $ffff for overflowing the fifomator
    moveq #0, d0
    
       btst #3, (a3)               ; Syncs to new frame
       beq.s *-4
 
        btst #3, (a3)               ; syncs to end of vblank
        bne.s *-4
        
		echo_z80request
        if start=0
        cmp.b #\start, ($C00008)
        bne.s *-8
        endif
        
        rept 5 
           move.w d0, (a5)              ; overflows the FIFO
        endr 
        
        rept 94                         ; Timing for a slot
           nop 
        endr 
        ; Execute DMA 
        
    
    move.l  #$94009300|(((\len)<<7)&$FF0000)|(((\len)>>1)&$FF), (a6)
    move.l  #$96009500|(((\src)<<7)&$FF0000)|(((\src)>>1)&$FF), (a6)
    move.l  #$97000000|((((\src)>>17)&$FF)<<16)|$8114, (a6) ; DMA + Display off
    move.l  #$C0000080, (a6)
    move.l #$C0000000, (a6)
    move.w #0, (a5)
	echo_z80release
    endm

BMP_VDPINIT:
    lea $c00000, a5             ; VDP data
    lea $c00004, a6             ; VDP control
    lea $C00005, a3             ; ---- 
    
    move.w #$8004, (a6)
    move.w #$8114, (a6)
    move.w #$8230, (a6)
    move.w #$832c, (a6)
    move.w #$8407, (a6)
    move.w #$8578, (a6)
    move.w #$8700, (a6)
    move.w #$8800, (a6)
    move.w #$8900, (a6)
    move.w #$8a00, (a6)
    move.w #$8b03, (a6);2, (a6)
    move.w #$8c81, (a6)
    move.w #$8d3e, (a6)
    move.w #$8f02, (a6)
    move.w #$9011, (a6)
    move.w #$9100, (a6)
    move.w #$9200, (a6)
    rts
    
FullscreenBMP:
    jsr BMP_VDPInit
    
    move.l #$8AFF8F00, (a6) ;HINT 255, AUTOINC 0
    
    move.w #100, d1

@FSBMP

    BMPCRAMDMA Fullscreen, 198*224*2
    
    dbf d1, @FSBMP
@FSFADE

p   = 0
    while p<224
    
    BMPCRAMDMA_V Fullscreen+(198*p*2), 198*(224-p)*2, p

    
p   = p+1
    endw
	
	move.w #$8174, VDPCONTROL
@Dataintworkin
	waitframe
    echo_z80request
    move.b ($A01200), d0
    move.b #0, ($A01200)
    echo_z80release
    tst.b d0
    beq.w @Dataintworkin
    
@wearedonehere2

		rts
    
    align 131072    

Fullscreen:
    incbin ./cramdma/fullscreen.dma
    even


VRAMADDR: macro reg
    and.l   #$FFFF, \reg
    lsl.l   #2, \reg
    lsr.w   #2, \reg
    or.w    #$4000, reg
    swap    \reg
    move.l  \reg, (a6)
    endm

BMPCRAMDMA: macro src, len
	echo_z80request
    move.w #$8154,(a6)
    move.l #$7FFC0003, (a6) ;vram write to $ffff for overflowing the fifomator
    moveq #0, d0
    
       btst #3, (a3)               ; Syncs to new frame
       beq.s *-4
 
        btst #3, (a3)               ; syncs to end of vblank
        bne.s *-4
        rept 5 
           move.w d0, (a5)              ; overflows the FIFO
        endr 
        
        rept 94                         ; Timing for a slot
           nop 
        endr 
        ; Execute DMA 
        
    
    move.l  #$94009300|(((\len)<<7)&$FF0000)|(((\len)>>1)&$FF), (a6)
    move.l  #$96009500|(((\src)<<7)&$FF0000)|(((\src)>>1)&$FF), (a6)
    move.l  #$97000000|((((\src)>>17)&$FF)<<16)|$8114, (a6) ; DMA + Display off
    move.l  #$C0000080, (a6)
	
	echo_z80release

    endm
    
CRAMDMASCROLLER:;;;;;;; Clear all variables
	disableInts
    moveq #0, d0
    moveq #0, d1
    moveq #0, d2
    moveq #0, d3
    moveq #0, d4
    moveq #0, d5
    moveq #0, d6
    moveq #0, d7
    DisableInts
    lea (Font), a0
;   lea (RLEData), a1
;   jsr RunLengthDecoding                           ;;;; load Font
;
;   singleDMAVRAM RLEDATA, 32, 32*100, VDPControl       

    SetCRAMADDR 0, VDPControl
    move.l #$00000EEE, (VDPDATA)

; PRELOAD VALUES
    lea $c00000, a5             ; VDP data
    lea $c00004, a6             ; VDP control
    lea $C00005, a3             ; ---- 
    
    
    move.w #$8004, (a6)
    move.w #$8114, (a6)
    move.w #$8230, (a6)
    move.w #$832c, (a6)
    move.w #$8407, (a6)
    move.w #$8578, (a6)
    move.w #$8700, (a6)
    move.w #$8800, (a6)
    move.w #$8900, (a6)
    move.w #$8a00, (a6)
    move.w #$8b03, (a6);2, (a6)
    move.w #$8c81, (a6)
    move.w #$8d3e, (a6)
    move.w #$8f02, (a6)
    move.w #$9011, (a6)
    move.w #$9100, (a6)
    move.w #$9200, (a6)
    
    clr.w (RAM_ScrollMod) ; VDP MODIFIERS
    
    ; BUILD STRING!
    moveq #0, d5
    
;    move.l #ScrollerString, (RAM_STRINGSTART)
;    move.l #ScrollerString+41, (RAM_STRINGPOS)
;    jsr ScrollerString40
    move.w #$8F02, (a6)
    move.w #$8154, (a6)


    SetCRAMADDR 0, VDPControl
    move.w #$0000, (a5)
    move.w #$0EEE, (a5)
;    jmp *
    singledmavram BigFont, 0, 318*32, a6 
    singledmacram BigFontPal, 0, 32, a6
    singledmavram Parascroll, $200*32, 513*32, a6
    singledmacram Parascroll_Pal, 32, 32, a6

;;;; LOAD MAPPINGS FOR PARALLAX SCROLLER

    setvramaddr $E000+BMPSCROLLOFFSET/8*64*2+128, a6
    lea (Parascroll_Map), a0
;            pccvhnnnnnnnnnnn
    move.w #%0010000000000000, d0
    move.l #14*64-1, d1

@loadparamap
    move.w (a0)+, d2
    add.w #$200, d2
    or.w d0, d2
    move.w d2, (a5)
    dbf d1, @loadparamap

    

;;;;


    lea (ScrollerString), a0

;    jsr MakeBigString
    move.w #$C000+BMPSCROLLOFFSET/8*64*2+40*2, (RAM_CurrentPos)

;    jmp *
    moveq #0, d0    
    moveq #0, d1
    move.l #$8AFF8F00, (a6) ;HINT 255, AUTOINC 0
    move.w #$0, (RAM_CurrentWidth)

    moveq #0, d2
    moveq #0, d3
    moveq #0, d4
    moveq #0, d5
    moveq #0, d6
    moveq #0, d7

@Bitmaploop:


    BMPCRAMDMA ScrollIMG, 198*116*2

    move.w #$8154,(a6)
    move.w #$8F02,(a6)

    setcramaddr 0, a6
    move.w #$0, (a5)

@Scrollermania
    move.w (RAM_Currentwidth), d1
    cmp.w (RAM_Scrollvalue), d1
    bne.s @Dontreload
    move.b (a0)+, (a0); Increment a0 by 1
    tst.b (a0)
    bne.s @notended
    lea (ScrollerString), a0
@notended:
    jsr MakeBigString
    clr.w (RAM_Scrollvalue)
    
@DontReload:
    SETVRAMADDR $F800+BMPSCROLLOFFSET*2*2-2, a6
    subq.w #4, d5
    sub.l #$0000A000, d6
    subq.w #1, d3
    subq.w #3, d2
    sub.l #$00004000, d4
    swap d4
    swap d6
    subq.w #6, d7

@DelayScroll
    cmp.b #216, ($C00008)
    bne.s @DelayScroll
    
    move.w #10*2-1, d1    
@First  
    move.w d5, (a5)
    move.w d4, (a5)    
    dbf d1, @First  
    move.w #7*2-1, d1   
@second 
    move.w d5, (a5)
    move.w d6, (a5)    
    dbf d1, @second 
    move.w #16-1, d1
@third
    move.w d5, (a5)
    move.w d3, (a5)    
    dbf d1, @third     
    move.w #25-1, d1
@fourth
    move.w d5, (a5)
    move.w d2, (a5)    
    dbf d1, @fourth   
    move.w #33-1, d1
@fifth
    move.w d5, (a5)
    move.w d7, (a5)    
    dbf d1, @fifth    
    
    addq.w #4, (RAM_Scrollvalue)
;   waitframe
;    setcramaddr 0, a6
;    move.w #$0,(a5)
    swap d4
    swap d6
    
	tst.b RAM_Fademan
	bne.s @Fadomania
    Echo_z80request
    move.b ($A01200), d0
    move.b #0, ($A01200)
    echo_z80release
    tst.b d0
    beq.w @NotDone   
	move.b #$FF, (RAM_FADEMAN)
	lea parascroll_pal, a0
    lea RAM_PaletteATM, a1
    moveq #16-1, d0
@FillWhite
    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 d0, @FillWhite
    
    lea EndPal, a0
    
    moveq #16-1, d7
    lea RAM_PaletteTo, a1
@Loadmajig
    move.b #0, (a1)+
    move.b #0, (a1)+
    move.b #0, (a1)+    
    dbf d7, @Loadmajig
@Fadomania
	disableints
	jsr FADEPROCESS
	enableints
	Echo_z80request
    move.b ($A01200), d0
    move.b #0, ($A01200)
    echo_z80release
    tst.b d0
    bne.w @WeAreDoneHere
	
	
	
	
@NotDone
	move.w #$8F00,(a6)  ; AUTOINC 0 
    move.w #$8114,(a6)  ; DISABLE DISPLAY
	
	bra.w @BitMaPLOOP
    
   
    rts
@WeareDoneHere:
	rts
FADEPROCESS
	movem.l d0-d7/a0-a1, -(sp)
	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, 32, 32, VDPControl    
	
	movem.l (sp)+, d0-d7/a0-a1
	rts

ScrollerString:
    rept 11
    dc.b " "
    endr
    dc.b " WTF U TALKIN 'BOUT??     WHAT IS THIS I DON'T EVEN?     WELL, IF YOU INSIST...                                  "

    rept 11
    dc.b " "
    endr
    
    dc.b 0 
    
    
    even

    align 131072    
Scrollimg:
    incbin ./cramdma/test.dma
    even

ScrollerString40:
    movem.l d0-d2/a0, -(sp)

    SETVRAMADDR $c000+BMPScrolloffset/8*64*2, a6
    move.l (RAM_StringPos), a0
    sub.l #41, a0
    move.w (RAM_ScrollMod), d2
    moveq #41-1, d0
    moveq #0, d1
@writestring
    move.b (a0)+, d1
    sub.b #32-1, d1
    or.w d2, d1
    move.w d1, (a5)
    dbf d0, @writestring

   

    movem.l (sp)+, d0-d2/a0
    rts

MakeBigString:

    movem.l d0-d4/a0-a2, -(sp)

    moveq #0, d0
    moveq #0, d1
    moveq #0, d2
    moveq #0, d3
    moveq #0, d4

    clr.w (RAM_CurrentWidth)


@ConstructString

@writebigstring
    ; GET CURRENT CHAR
    moveq #0, d1
    move.b (a0)+, d1
    
    ; NORMALIZE BY ASCII TABLE

; ;;;;;;;;;             LETTER !
    sub.b #32, d1   ;; 0 = space !11
    ; MULTIPLY BY 4
    lsl.w #2, d1
    lea fontinfo(pc), a1
    lea (a1,d1.w), a1
    move.w (a1)+, d1 ; vertical offset within map
    move.w (a1), d2 ; d2 = WIDTH IN TILES (.w!)
    move.w d2, d3
    lsl.w #3, d3
    move.w d3, (RAM_CurrentWidth)

@WidthIsAlreadyPut

    lea fontmap+229*2(pc), a1       ; LOAD FONT MAP 
    lea (a1,d1.w), a1       ; LOAD CORRECT POSITION WITHIN MAP FOR THIS LETTER

    moveq #0, d4
    move.w (RAM_CurrentPos),d4
  
    move.w #229*2, d1
    subq.b #1, d2
    
    move.w #$8F80,(a6)
@DoLetter
    lea (a1), a2            ; A1 = Big map of... letter nametable definitiosn

    moveq #10-1, d0
    move.w d4, d3

    and.l   #$FFFF, d3
    lsl.l   #2, d3
    lsr.w   #2, d3
    or.w    #$4000, d3
    swap    d3
    move.l  d3, (a6)

@Writecol:
    move.w (a2), (a5)
    lea (a2, d1.w), a2      ; Go further 229*2 bytes in big table for next ROW
    dbf d0, @Writecol
    move.w (a1), (a1)+
    addq.w #2, d4
    cmp.w #$C000+BMPSCROLLOFFSET/8*64*2+64*2, d4
    bne.s @NotTooBigYet
    move.w #$c000+BMPSCROLLOFFSET/8*64*2, d4
@NotTooBigYet:
    dbf d2, @DoLetter
    
    move.w d4, (RAM_CurrentPos)
    move.w #$8F02, (a6)
    
    
    movem.l (sp)+, d0-d4/a0-a2
    rts
    

FontInfo:
    dc.w 0*2, 4 ; " "
    dc.w 4*2, 3 ; "!"
    dc.w 7*2, 4 ; """`
    dc.w 11*2, 4 ; " "
    dc.w 15*2, 4 ; " "
    dc.w 19*2, 4 ; " "
    dc.w 23*2, 4 ; " "
    dc.w 27*2, 3 ; "'"
    dc.w 30*2, 3 ; "("
    dc.w 33*2, 3 ; ")" 
    dc.w 36*2, 4 ; " "
    dc.w 40*2, 4 ; "+"
    dc.w 44*2, 2 ; ","
    dc.w 46*2, 4 ; "-"
    dc.w 50*2, 3 ; "."
    dc.w 53*2, 4 ; "/"
    dc.w 57*2, 4 ; "0"
    dc.w 61*2, 3 ; "1"
    dc.w 64*2, 4 ; "2"
    dc.w 68*2, 4 ; "3"
    dc.w 72*2, 4 ; "4"
    dc.w 76*2, 4 ; "5"
    dc.w 80*2, 4 ; "6"
    dc.w 84*2, 4 ; "7"
    dc.w 88*2, 4 ; "8"
    dc.w 92*2, 4 ; "9"
    dc.w 96*2, 3 ; ":"
    dc.w 99*2, 3 ; ";"
    dc.w 102*2, 4 ; " "
    dc.w 106*2, 4 ; "="
    dc.w 110*2, 4 ; " "
    dc.w 114*2, 4 ; "?"
    dc.w 118*2, 4 ; " "
    dc.w 122*2, 4 ; "A"
    dc.w 126*2, 4 ; "B"
    dc.w 130*2, 4 ; "C"
    dc.w 134*2, 4 ; "D"
    dc.w 138*2, 4 ; "E"
    dc.w 142*2, 4 ; "F"
    dc.w 146*2, 4 ; "G"
    dc.w 150*2, 4 ; "H"
    dc.w 154*2, 3 ; "I"
    dc.w 157*2, 4 ; "J"
    dc.w 161*2, 4 ; "K"
    dc.w 165*2, 4 ; "L"
    dc.w 169*2, 6 ; "M"
    dc.w 175*2, 5 ; "N"
    dc.w 180*2, 4 ; "O"
    dc.w 184*2, 4 ; "P"
    dc.w 188*2, 4 ; "Q"
    dc.w 192*2, 4 ; "R"
    dc.w 196*2, 4 ; "S"
    dc.w 200*2, 4 ; "T"
    dc.w 204*2, 4 ; "U"
    dc.w 208*2, 4 ; "V"
    dc.w 212*2, 5 ; "W"
    dc.w 217*2, 4 ; "X"
    dc.w 221*2, 4 ; "Y"
    dc.w 225*2, 4 ; "Z"
    ; DAS ENDE 
    align 65536
FontMap:
    incbin ./cramdma/lettermap.bin
    even ;JIC

parascroll:
    incbin ./cramdma/parascroll.bin ; Tiles
    even
    
parascroll_pal:
    incbin ./cramdma/parascroll.pal ; Tiles
    even

parascroll_map:
    incbin ./Cramdma/parascroll.map
    even

    align 131072 
    
BigFont:
    incbin ./cramdma/bigfont.bin
    even

BigFontPal:
	dc.w	$0000,$0EEE,$0644,$0866,$0888,$0AAA,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000
    
     ;   Echo_z80request
     ;   move.b ($A01200), d0
     ;   move.b #0, ($A01200)
     ;   echo_z80release
     ;   tst.b d0
     ;   bne.w @NextPart 
	 
	 
