;**************************************************************************
;*
;* boot loader main test 2 (load a picture later)
;*
;* is loaded from bootLoader_2000_4_16.bin (will load 16 sectors (16*128 bytes) from sector 4 into address $2000 
;*
;**************************************************************************
	
	opt	h-
	opt	f+

	org	$2000			   ;prg start
	
	icl "custom.i"
	icl "bootLoader.i"

;-------- CODEADR is start segment

	cld
	
	;set display list (use shadow regs)
	
	lda	#<displayList
	sta	DLISTL	
	lda	#>displayList
	sta	DLISTH

	lda	#$00
	sta	NMIEN			  ;NMIEN all off 
	sta	DMACTL			  ;DMACTL all off
	
	;vertical blank interrupt 
	
	lda 	#>vbi_irq
	sta	VVBLKIH	
	lda 	#<vbi_irq
	sta	VVBLKIL	
	
	;chbase
	
	lda	#$E0
	sta	CHBASE
	
	;color setup
	
	lda	#$0	
	sta	COLBK
	lda	#$a	
	sta	COLPF0
	lda	#$e	
	sta	COLPF1
	lda	#$8	
	sta	COLPF2
	lda	#$88				
	sta	COLPF3
				
	;disable player/missile dma
	
	lda	#$00
	sta	GRACTL
	
	;
	
	lda	#$0		;normal
	sta	PRIOR
	
	;setup displays
	   
	lda	#DMACTL_DL|DMACTL_PF_NORMAL		
		
		;enable display list dma 			
		;playfield normal display (40)
		
	sta	DMACTL	;DMACTL
	
	;enable ints
	
	lda	#NMIEN_VBI
	sta	NMIEN	;VBI
	
	;load picture
	
	cli
	jsr	loadPicture
	sei
	
	;os key remove
	
	lda	#<noKeyRead
        sta	VSERINL        
        lda	#>noKeyRead
        sta	VSERINH  
	
	
loop	

?WaitVC0
        lda	VCOUNT		
        beq	?WaitVC0   	
?WaitVCnot0
        lda	VCOUNT		
        bne	?WaitVCnot0   
		
	;TBD	
		
	jmp	loop
	
	
;************************************************
;*
;* loadPicture
;*
;************************************************

loadPicture

	lda	#<gfx
	sta	za0
	lda	#>gfx
	sta	za0+1		;memory target
	
	lda	#29		;after code and bootloader
	sta	za1
	lda	#0
	sta	za1+1		;memory target
	
	lda	#60		;sector count
	sta	zd0
	
	lda	#<LOADER_ADDRESS	;loader in low memory
	sta	za2
	lda	#>LOADER_ADDRESS	
	sta	za2+1
	
	ldy	#0			;get address
	lda	(za2),y
	sta	?jsrTarget
	ldy	#1			;get address
	lda	(za2),y
	sta	?jsrTarget+1
	
	.byte	$20			;jsr
?jsrTarget	
	.byte	0,0
	rts
	
;************************************************
;*
;* dummy
;*
;************************************************
	
noKeyRead	
	rts	
	
	lda	#$0
	sta	HSCROL
	
bootEnd	
	
;************************************************
;*
;* vertical blank interrupt (immidiate, x,y regs save bei os rom)
;*
;************************************************

vbi_irq
					
	;prolog (like XITVBL)
	
	pla					  
	tay
	pla					  
	tax
	pla					  
	rti
	
	; display list

	;7	6	5	4	3	2	1	0
	;DLI	LMS	VSCROLL	HSCOLL	Mode
	;
	;Mode 0 Blank lines 7..4 is count+1 of blank lines
	;Mode 1 JMP (use with LMS to set destination)
	;Mode $2-$f is ANTIC mode

	.align $400
	

displayList	 
	.byte 0			;Mode 0
	.byte $70		;8 blank lines	
	.byte $4f,<gfx,>gfx   	;Mode f + LMS	 
	.rept 101		;(101+1) * 40bytes = 4080
	.byte $f   
	.endr
	.byte $4f,<(gfx+4096+24),>(gfx+4096+24) ;Mode f + LMS skip one lone because 4096 border (4080 next is 4096+24 to met 40 byte per line)
	.rept 88		;89+1-1(skip) is remaining
	.byte $f   
	.endr
	
	.byte $41,<displayList,>displayList ; wait vblank, restart same display list on next frame
	
.define	PADDING (16*128)-(bootEnd - $2000)

        .rept PADDING
        .byte 0
        .endr	
	
	org	$4000
gfx	;60 sectors (7680 bytes) hast to load here, sector start is 28	
