
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;					VDP Register settings (needed to turn on screen)							
	
VdpInitData:
	;Turn off left hand side
	;db %00100110,128+0 ; reg. 0, display and interrupt mode.

	db %00000110,128+0 ; reg. 0, display and interrupt mode.
	db %11100000,128+1 ; reg. 1, display and interrupt mode.
	db &ff		,128+2 ; reg. 2, name table address. &FF = TileMap at &3800 &FD = TileMap at &3000
	db &ff		,128+3 ; reg. 3, Name Table Base Address  (no function) &0000
	db &ff 		,128+4 ; reg. 4, Color Table Base Address (no function) &0000
	db &ff		,128+5 ; reg. 5, sprite attribute table. -DCBA98- = bits of address $3f00
	db &00		,128+6 ; reg. 6, sprite tile address. -----D-- = bit 13 of address $2000
	db &00		,128+7 ; reg. 7, border color. 			----CCCC = Color
	db &00 		,128+8 ; reg. 8, horizontal scroll value = 0.
	db &00		,128+9 ; reg. 9, vertical scroll value = 0.
	db &ff 		,128+10; reg. 10, raster line interrupt. Turn off line int. requests.
VdpInitDataEnd:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;								Basic palette in native format									
	
PaletteData:
	ifdef BuildSGG					;SGG
		dw &0000; ;0  -BGR
		dw &00F1; ;1  -BGR
		dw &0555; ;2  -BGR
		dw &0AAA; ;3  -BGR
		dw &0FFF; ;4  -BGR
		dw &0682; ;5  -BGR
		dw &03D3; ;6  -BGR
		dw &033E; ;7  -BGR
		dw &067E; ;8  -BGR
		dw &05AE; ;9  -BGR
		dw &04FF; ;10  -BGR
		dw &0A2A; ;11  -BGR
		dw &0F0F; ;12  -BGR
		dw &0D30; ;13  -BGR
		dw &0B63; ;14  -BGR
		dw &0FD0; ;15  -BGR
		
		dw &0000; ;0  -BGR
		dw &00F1; ;1  -BGR
		dw &0555; ;2  -BGR
		dw &0AAA; ;3  -BGR
		dw &0FFF; ;4  -BGR
		dw &0682; ;5  -BGR
		dw &03D3; ;6  -BGR
		dw &033E; ;7  -BGR
		dw &067E; ;8  -BGR
		dw &05AE; ;9  -BGR
		dw &04FF; ;10  -BGR
		dw &0A2A; ;11  -BGR
		dw &0F0F; ;12  -BGR
		dw &0D30; ;13  -BGR
		dw &0B63; ;14  -BGR
		dw &0FD0; ;15  -BGR
	else 							;SMS
		db %00000000; ;0  --BBGGRR
		db %00001100; ;1  --BBGGRR
		db %00010101; ;2  --BBGGRR
		db %00101010; ;3  --BBGGRR
		db %00111111; ;4  --BBGGRR
		db %00011000; ;5  --BBGGRR
		db %00001100; ;6  --BBGGRR
		db %00000011; ;7  --BBGGRR
		db %00010111; ;8  --BBGGRR
		db %00011011; ;9  --BBGGRR
		db %00011111; ;10  --BBGGRR
		db %00100010; ;11  --BBGGRR
		db %00110011; ;12  --BBGGRR
		db %00110000; ;13  --BBGGRR
		db %00100100; ;14  --BBGGRR
		db %00111100; ;15  --BBGGRR
		
		
		db %00000000; ;0  --BBGGRR
		db %00001100; ;1  --BBGGRR
		db %00010101; ;2  --BBGGRR
		db %00101010; ;3  --BBGGRR
		db %00111111; ;4  --BBGGRR
		db %00011000; ;5  --BBGGRR
		db %00001100; ;6  --BBGGRR
		db %00000011; ;7  --BBGGRR
		db %00010111; ;8  --BBGGRR
		db %00011011; ;9  --BBGGRR
		db %00011111; ;10  --BBGGRR
		db %00100010; ;11  --BBGGRR
		db %00110011; ;12  --BBGGRR
		db %00110000; ;13  --BBGGRR
		db %00100100; ;14  --BBGGRR
		db %00111100; ;15  --BBGGRR
	endif

	
ScreenInit:
	ld a,&30
	ld (ActiveBank),a
	ld a,&FF
	ld (VisibleBankReg),a



	;Init the screen 												
	ld hl,VdpInitData	;Source of data
    ld b,VdpInitDataEnd-VdpInitData		;Byte count
    ld c,vdpControl		;Destination port
    otir				;Out (c),(hl).. inc HL... dec B, djnz 

	;Define Palette 
    ld hl, &c000	    ; set VRAM write address to CRAM (palette) address 0
		; note &C0-- is a set palette command... it's not a literal memory address 
    call prepareVram

    ld hl,PaletteData	;Source of data
	ifdef BuildSGG
		ld b,16*2 *2		;Byte count (32 on SGG)
	else
		ld b,16	*2		;Byte count (16 on SMS)
	endif
	ld c,vdpData		;Destination port
	otir				;Out (c),(hl).. inc HL... dec B, djnz  
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;					Start of the Test Code														

	;Copy bitmap to tilemap
	ld de, 0*8*4			;8 lines of 4 bytes per tile
	ld hl, TileData		;Source Address of sprite data
	ld bc, TileDataEnd-TileData ; Length of sprite data
;	call DefineTiles		;Send data to VRAM
;	ret
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DefineTiles:	;DE=VDP address, HL=Source,BC=Bytecount
	ex de,hl
	call prepareVram	;Set VRAM address we want to write to
	ex de,hl
DefineTiles2:
	ld a,(hl)
	out (vdpData),a		;Send Byte to VRAM
	inc hl
	dec bc				;Decrease counter and see if we're done
	ld a,b
	or c
	jr nz,DefineTiles2
	ret
	
prepareVram:				;Set vdpData to write to memory address HL in vram
	    ld a,l
	    out (vdpControl),a
	    ld a,h
	    or &40				;we set bit 6 to define that we want to Write data...
	    out (vdpControl),a	;As the VDP ram only goes from &0000-&3FFF 
    ret			
	
	
	
FlipScreen:	

	ld a,(HSpriteNum)
	cp HSpriteLimit
	jr nc,AllHspritesUsed
	
	out (vdpControl),a		;LByte
	neg
	add HSpriteLimit
	ld b,a
	
	ld a,&3F+&40			;HByte
	out (vdpControl),a
	ld a,200
ClearSprite:	
	out (vdpData),a
	djnz ClearSprite
AllHspritesUsed:	
		

	ld a,0
	ld (HSpriteNum),a
	
	ld a,(YShift)
	out (vdpControl),a
	ld a,9+128
	out (vdpControl),a


	ld a,(XShift)
	out (vdpControl),a
	ld a,8+128
	out (vdpControl),a



	ld a,(ActiveBank)
	xor &08
	ld (ActiveBank),a
	
	ld a,(VisibleBankReg)
	xor &02
	ld (VisibleBankReg),a
	
	out (vdpControl),a
	ld a,2+128
	out (vdpControl),a
	
	ld bc,&2000
FlipDelay:	
	dec bc
	ld a,b
	or c
	jr nz,FlipDelay
ClearBorder:
	ret

	