; Learn Multi platform Z80 Assembly Programming... With Vampires!

;Please see my website at	www.chibiakumas.com/z80/
;for the 'textbook', useful resources and video tutorials

;File		SMS Basic Functions
;Version	V1.0b
;Date		2018/7/28
;Content	Provides basic text functions using Firware calls

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PrintChar:
	ifdef useMaybeEI
		MaybeDI
	endif
	call PrintCharNoInc
	push af
		ld a,(NextCharX)
		inc a
		ld (NextCharX),a
		ifdef BuildSMS
			cp 32			;SMS	32x24
		else
			cp 20+6			;SGG 	20x18
		endif
		call nc,NewLine

	pop af
	ifdef useMaybeEI
		MaybeEI
	endif
	ret



PrintCharNoInc:
	push bc
	push hl
		push af
			ld a,(NextCharY)
			ld b,a				;Ypos (32 tiles per line)
			ld a,(NextCharX)
			ld c,a				;Xpos (2 bytes per tile)
			
			xor a				;YYYYYYYY --------
			rr b		
			rra					;-YYYYYYY Y-------
			rr b
			rra					;--YYYYYY YY------
			
			rlc c				;			XXXXX-
			or c				;--YYYYYY YYXXXXX-
			ld c,a
			
			ld hl,&3800			;Base of Tilemap
			add hl,bc
			call prepareVram
		
		pop af
		push af
			sub 32				;No characters below 32 in our font
			out (vdpData),a
			xor a				;Top byte is palette/top bit of tile num
			out (vdpData),a
		pop af
			
	pop hl
	pop bc
	ret

ReadJoy:
	in a,(&DC)
	cpl
	ret

WaitChar:
	
	ld a,(SelKeyChar)
	call PrintCharNoInc

	push bc
		call ReadJoy
		ld b,a
		ld a,(SelKeyChar)
		
		bit 4,b
		jr nz,WaitCharDone
		
		bit 1,b
		jr z,WaitCharNotDown
		inc a
WaitCharNotDown:
		bit 0,b
		jr z,WaitCharNotUp
		dec a
WaitCharNotUp:
		ld bc,&3000
		push af
WaitCharDelay:
			dec bc
			ld a,b
			or c
			jr nz,WaitCharDelay
		pop af
		cp 32
		jr nc,WaitCharNotTooLow
		ld a,96+32-1
WaitCharNotTooLow:
		cp 32+96
		jr c,WaitCharNotTooHigh
		ld a,32
WaitCharNotTooHigh:
		ld (SelKeyChar),a
		
		
	pop bc
	jr WaitChar
	
WaitCharDone:
	pop bc
	ret

ScreenINIT:	
CLS:
	call CLSB
	ifdef BuildSMS
		ld bc,32*24
	else
		ld bc,20*18
	endif
ClsAgain:
	ld a,32
	call PrintChar
	dec bc
	ld a,b
	or c
	jr nz,ClsAgain
CLSB:
	ld hl,&0000
Locate:
	push af
		ld a,h
	ifdef BuildSGG
		add 6
	endif
		ld (NextCharX),a
		ld a,l
	ifdef BuildSGG
		add 3
	endif
		ld (NextCharY),a
	pop af
	ret

GetCursorPos:
	push af
		ld a,(NextCharX)
		ifdef BuildSGG
			sub 6
		endif
		ld h,a
		ld a,(NextCharY)
		ifdef BuildSGG
			sub 3
		endif
		ld l,a
	pop af
	ret

NewLine:
	push af
	ifdef BuildSMS
		xor a
	else
		ld a,6
	endif
		ld (NextCharX),a
		ld a,(NextCharY)
		inc a
		ld (NextCharY),a
	pop af
	ret

PrintString:
	ld a,(hl)	;Print a '255' terminated string 
	cp 255
	ret z
	inc hl
	call PrintChar
	jr PrintString

DOINIT:
    	call setUpVdpRegisters
    	call clearVram
	di
    	call loadColorPalette
    	call loadFontTiles
    	call turnOnScreen
	call loadFontTiles
	call loadColorPalette
	ret


SHUTDOWN:
	di
	halt



loadFontTiles:
    ld hl, &4000		;Any value above &4000 wraps round because there IS only 16k
    call prepareVram
    ld hl,BitmapFont             ; Location of tile data
    ld bc,BitmapFontEnd-BitmapFont ;96*8  ; Counter for number of bytes to write
	;jp writeToVramx4

writeToVramx4:			;This is used for the 2 color bitmaps of the Font
    ld a,(hl)
    out (vdpData),a		;1		Bitplanes
    out (vdpData),a		;2
    out (vdpData),a		;4
    out (vdpData),a		;8
    inc hl
    dec bc
    ld a,c
    or b
    jr nz, writeToVramx4
    ret

loadColorPalette:

    ld hl, &c000	    ; set VRAM write address to CRAM (palette) address 0 (for palette index 0)
						; note &C0 is a set palette command... it's not a literal memory address 
    call prepareVram
    call writeToPalb	;Fill Sprite palette with same data
writeToPalb:
    ld hl,PaletteData ; source of data
	ifdef BuildSGG
		ld b,16*2 
	else
		ld b,16
	endif
	ld c,vdpData
	otir 
	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
turnOnScreen:
    ld a,(VdpInitData1)
	or %01000000
    out (vdpControl),a
    ld a,128+1
    out (vdpControl),a
SetFontColor:				;Can't set font color on SMS
    ret



PaletteData:
		ifdef BuildSMS
		;   --BBGGRR
		db %00110000	;0
		db %00001111	;1
		db %00111100	;2
		db %00000011	;3
		db %00001111	;4
		db %00001111	;5
		db %00001111	;6
		db %00001111	;7
		db %00001111	;8
		db %00001111	;9
		db %00001111	;A
		db %00001111	;B
		db %00001111	;C
		db %00001111	;D
		db %00001111	;E
		db %00001111	;F
	else								;SGG
		   ;GGGGRRRR, ----BBBB
		db %00000000,%00001111;0
		db %11111111,%00000000;1
		db %11110000,%00001111;2
		db %00001111,%00000000;3
		db %00001111,%00001111;4
		db %00001111,%00001111;5
		db %00001111,%00001111;6
		db %00001111,%00001111;7
		db %00001111,%00001111;8
		db %00001111,%00001111;9
		db %00001111,%00001111;10
		db %00001111,%00001111;11
		db %00001111,%00001111;12
		db %00001111,%00001111;13
		db %00001111,%00001111;14
		db %11111111,%00000000;15
	endif

