RAM_ProgressSrc 	equ $ffa000
RAM_ProgressFrame	equ $ffa004
RAM_ProgressSAT 	equ $ffb000

LoaderMain:
    DisableInts

    ;-------------------------------------------------------------------------
    ; Initialization
    move.w  #$8004, VDPControl ; 512 colors
    move.w  #$8174, VDPControl ; VINT, DMA, Display enabled, Genesis
    move.w  #$8540, VDPControl ; Sprite Attr Table Base = $8000
	move.l	#0, 	RAM_ProgressSrc
	move.l	#0,		RAM_ProgressFrame
	
	;-------------------------------------------------------------------------  
    ; Copy tiles and palette into VRAM
    SingleDMAVRAM ROM_LoaderBin, $0000, 5824, VDPControl
    SingleDMACRAM ROM_LoaderPal, $0000, 32,   VDPControl
    
	; Copy map into Plane A
	lea 	ROM_LoaderMap, a0
	moveq	#(320/8)-1, d0
	move.l  #$40000000|($c000>>14), (VDPControl)
@CopyLine:
	rept	(320/8)
		move.w	(a0)+, VDPData
	endr
	rept	(512-320)/8
		move.w	#0, VDPData
	endr
	dbf 	d0, @CopyLine
	
	;-------------------------------------------------------------------------      
    ; Copy attr table into RAM
    lea     (ROM_ProgressSAT), a0
    lea     (RAM_ProgressSAT), a1   
    move.l  #((8*7)/4)-1, d0
@CopyAttrTable:
    move.l  (a0)+, (a1)+
    dbf d0, @CopyAttrTable

	; Copy progress bar into VRAM
    SingleDMACRAM ROM_ProgressPal,   $0020, 32,      VDPControl
	SingleDMAVRAM RAM_ProgressSAT,   $8000, 8*7,     VDPControl
	SingleDMAVRAM ROM_ProgressTiles, $2000, 32*8*32, VDPControl
	
    ;-------------------------------------------------------------------------  
    ; Start
	move.l  #LoaderVINT, (RAM_VINTLOC+2)
    moveq	#0, d3
	
sf	= 50 ; start frame
@Progress:
	cmp		#sf, d3
	bne		@Disable
	EnableInts
@Disable:	
	cmp		#(sf+202), d3
	bne		@GoOn
	jmp FadeLoader
@GoOn:
	WaitFrame
	addq	#1, d3
	
	
    jmp     @Progress
    
FadeLoader:
	disableints

	lea ROM_LoaderPAL, 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
	
    moveq #15, d7
    
@FADELOOP:
    lea RAM_PaletteATM, a0
    lea RAM_PaletteTO, a1

    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    

    lea RAM_PaletteBuilt+$A, a1
    SingleDMACram RAM_PaletteBuilt, 0, 32, VDPControl
    setcramaddr 32+2, vdpcontrol
	move.w (a1), vdpdata
	
    WaitFrame
    WaitFrame
    dbf d7, @FADELOOP
	
	
	move.w #10, d7
	jsr WAITD7
	
	rts
	;-------------------------------------------------------------------------  
    ; Vertical Interrupt
LoaderVINT:
	move.l 	RAM_ProgressSrc, d0
	moveq	#4, d2
	
	lea		ROM_ProgressTimeline, a0
	lea		RAM_ProgressSAT, a1
	
	moveq	#7-1, d1		; write 7 sprites
@WriteAttrTable:
	move.w	(a0,d0.w), (a1,d2.w)
	add.l	#2, d0
	add.l	#8, d2
	dbf		d1, @WriteAttrTable
	
	move.l	d0, RAM_ProgressSrc
	
	add.l	#1, RAM_ProgressFrame
	
	SingleDMAVRAM RAM_ProgressSAT, $8000, 8*7, VDPControl
	rte
	
	; ------------------------------------------------------------------------
    ; Sprite data for processing sprite
sx	=		58
sy	=		183
ROM_ProgressSAT:
	dc.w	128+sy
	dc.b	%00001101
	dc.b	1
	;             1
	;             0521
	;             2152631
	;             42684268421
    ;        pccvhnnnnnnnnnnn
    dc.w    %0010001000000000
    dc.w    128+sx
	
	dc.w	128+sy
	dc.b	%00001101
	dc.b	2
	;        pccvhnnnnnnnnnnn
    dc.w    %0010001000000000
    dc.w    128+sx+32
	
	dc.w	128+sy
	dc.b	%00001101
	dc.b	3
	;        pccvhnnnnnnnnnnn
    dc.w    %0010001000000000
    dc.w    128+sx+64
	
	dc.w	128+sy
	dc.b	%00001101
	dc.b	4
	;        pccvhnnnnnnnnnnn
    dc.w    %0010001000000000
    dc.w    128+sx+96
	
	dc.w	128+sy
	dc.b	%00001101
	dc.b	5
	;        pccvhnnnnnnnnnnn
    dc.w    %0010001000000000
    dc.w    128+sx+128
	
	dc.w	128+sy
	dc.b	%00001101
	dc.b	6
	;        pccvhnnnnnnnnnnn
    dc.w    %0010001000000000
    dc.w    128+sx+160

	dc.w	128+sy
	dc.b	%00001101
	dc.b	0
	;        pccvhnnnnnnnnnnn
    dc.w    %0010001000000000
    dc.w    128+sx+192
	
    ; ------------------------------------------------------------------------
    ; Loader timeline
LoaderCreateStep: macro c, v
	rept	c
		dc.w	%0010000100000000|$f8
	endr
	dc.w	%0010000100000000|(v)
	rept	6-c
		dc.w	%0010001000000000
	endr
	endm

	even
ROM_ProgressTimeline:
i	=	0
	while i<6
j	=	0
		while j<32
			LoaderCreateStep i, j*8
j	=	j+1
		endw
i	=	i+1
	endw
	
	even
	LoaderCreateStep 6, $00
	LoaderCreateStep 6, $08
	LoaderCreateStep 6, $10
	LoaderCreateStep 6, $18
	LoaderCreateStep 6, $20
	LoaderCreateStep 6, $28
	LoaderCreateStep 6, $30
	LoaderCreateStep 6, $38
	LoaderCreateStep 6, $40
	LoaderCreateStep 6, $48
	LoaderCreateStep 6, $50
	
    ; ------------------------------------------------------------------------
    ; Loader data
    even
ROM_LoaderBin:
	incbin './loader/loader.bin'
    
	even
ROM_LoaderMap:
	incbin './loader/loader.map'
    
	even
ROM_LoaderPal:
	dc.w	$0000,$0222,$0888,$0AAA,$0EEE,$080A,$0C0E,$0C8E
	dc.w	$0606,$0404,$0066,$00AA,$00EE,$08EE,$04EE,$0444
	
	even
ROM_ProgressPal:
	dc.w	$0000,$0C0E,$0000,$0000,$0000,$0000,$0000,$0000
	dc.w	$0000,$0000,$0000,$0000,$0000,$0000,$0000,$0000

	; ------------------------------------------------------------------------
    ; Progress tiles rows
c0	=		$10000000
c1	=		$11000000
c2	=		$11100000
c3	=		$11110000
c4	=		$11111000
c5	=		$11111100
c6	=		$11111110
c7	=		$11111111

ProgressCreateTile: macro c, v
		rept c
			rept	11
				dc.l	c7
			endr
			rept	5
				dc.l	0
			endr
		endr
		rept	11
			dc.l	v
		endr
		rept	5
			dc.l	0
		endr
		rept 3-c
			rept	11
				dc.l	0
			endr
			rept	5
				dc.l	0
			endr
		endr
	endm
	
	even
ROM_ProgressTiles:
i	= 	0
	while i<4
	ProgressCreateTile i, c0
	ProgressCreateTile i, c1
	ProgressCreateTile i, c2
	ProgressCreateTile i, c3
	ProgressCreateTile i, c4
	ProgressCreateTile i, c5
	ProgressCreateTile i, c6
	ProgressCreateTile i, c7
i	= 	i+1
	endw
	