; function library
; written by Sheldon MacDonald (c)2020
;
;**************************************************************************************
;* cls(color) 0-7
;**************************************************************************************
FUNCTION	CLS	CC	;CLS(0) 0-7 colors
REM	CLS(n)	This is a number from 0 to 7. Does not save to stack.
DEF	CC	BYTE	;define data type    byte,word,string # RETBYTE RETWORD
CC	FCB	0	;cc store
C0	FCB	128	;black
	FCB	143	;green
	FCB	159	;yellow
	FCB	175	;blue
	FCB	191	;red
	FCB	207	;white
	FCB	223	;cyan
	FCB	239	;megenta
	FCB	255	;orange
CLS	LDX	#1024
	LDA	CC
	CMPA	#8
	BLS	LP1
	LDA	#8
	STA	CC
LP1	LDY	#C0
	LDA	CC
	LEAY	A,Y
	LDA	,Y	;color loaded
LP2	STA	,X+
	CMPX	#1536
	BLO	LP2
ENDFUNCTION
;**************************************************************************************
;* print(caps on,pos,string) 
;**************************************************************************************
FUNCTION	PRINT	CAP,CC,X
REM	PRINT(c,pos,string)	c=0 or 1 for starting in caps. pos=0 to 511 for screen. string can be an address if populated with [var]
DEF	CAP	BYTE	;start in caps mode or not?
DEF	CC	WORD	;its 2 bytes print position
DEF	X	STRING	;populate x register with string at x
CAP	FCB	0	;caps flag
CC	FCB	0,0	;dword
PRINT	LDD	CC	;check if out of bounds
	CMPD	#511
	BLS	LP0
	LDD	#511
	STD	CC
LP0	LDD	#1024	; x=has string  y is screen
	ADDD	CC
	TFR	D,Y	;y is set
	LDB	CAP	;load b with caps flag
LP1	LDA	,X+
;process this ascii code
	CMPA	#97
	BLO	LP2
	CLRB
	STB	CAP
;assume lowercase
	SUBA	#96
LP2	CMPA	#65
	BLO	LP3
	CMPA	#90
	BHI	LP3
	LDB	#1
	STB	CAP
LP3	CMPA	#32
	BNE	LP4
	CMPB	#1	;is cap on
	BNE	LP4
	ADDA	#64	;fix green space
;lets look at numbers
LP4	CMPA	#33
	BLO	LP5
	CMPA	#63
	BHI	LP5
;its a number
	CMPB	#1
	BNE	LP5
;change number to cap
	ADDA	#64
LP5	STA	,Y+
	CMPY	#1536
	BHS	LP6
	LDA	,X
	CMPA	#0
	BNE	LP1
LP6	RTS	;return
ENDFUNCTION
