; function library SMDBASIC vr1.0a
; written by Sheldon MacDonald (c)2020
;
;this library is a list of basic functions i have built to make programming assembly easier
;
; **********************************************************************************************************************
; * create sin_cos table
; **********************************************************************************************************************
FUNCTION	MAKESINCOS
REM	; makesc() creates and returns sine table to x register and cosine to y register
REM	; 
SINTBL	FCB	128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173	; 16
	FCB	176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215	; 32
	FCB	218,220,222,224,226,228,230,232,233,235,237,238,240,241,243,244	; 48
	FCB	245,246,247,248,249,250,251,252,252,253,253,254,254,254,254,254	; 64
COSTBL	FCB	254,254,254,254,254,253,253,252,252,251,250,250,249,248,247,246	; 80
	FCB	244,243,242,240,239,237,236,234,232,231,229,227,225,223,221,219	; 96
	FCB	216,214,212,210,207,205,202,200,197,194,192,189,186,183,181,178	; 112
	FCB	175,172,169,166,163,160,157,154,151,148,145,142,138,135,132,129	; 128
	FCB	127,124,121,118,114,111,108,105,102,99,96,93,90,87,84,81	; 144
	FCB	78,75,73,70,67,64,62,59,56,54,51,49,46,44,42,40	; 160
	FCB	37,35,33,31,29,27,25,24,22,20,19,17,16,14,13,12	; 176
	FCB	10,9,8,7,6,6,5,4,4,3,3,2,2,2,2,2	; 192
	FCB	2,2,2,2,2,3,3,4,4,5,6,7,8,9,10,11	; 208
	FCB	12,13,15,16,18,19,21,23,24,26,28,30,32,34,36,38	; 224
	FCB	41,43,45,48,50,53,55,58,60,63,66,68,71,74,77,80	; 240
	FCB	83,86,89,91,94,98,101,104,107,110,113,116,119,122,125,128	; 256
	FCB	128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173	; 16
	FCB	176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215	; 32
	FCB	218,220,222,224,226,228,230,232,233,235,237,238,240,241,243,244	; 48
	FCB	245,246,247,248,249,250,251,252,252,253,253,254,254,254,254,254	; 64
	FCB	254,254,254,254,254,253,253,252,252,251,250,250,249,248,247,246	; 80
	FCB	244,243,242,240,239,237,236,234,232,231,229,227,225,223,221,219	; 96
	FCB	216,214,212,210,207,205,202,200,197,194,192,189,186,183,181,178	; 112
	FCB	175,172,169,166,163,160,157,154,151,148,145,142,138,135,132,129	; 128
	FCB	127,124,121,118,114,111,108,105,102,99,96,93,90,87,84,81	; 144
	FCB	78,75,73,70,67,64,62,59,56,54,51,49,46,44,42,40	; 160
	FCB	37,35,33,31,29,27,25,24,22,20,19,17,16,14,13,12	; 176
	FCB	10,9,8,7,6,6,5,4,4,3,3,2,2,2,2,2	; 192
	FCB	2,2,2,2,2,3,3,4,4,5,6,7,8,9,10,11	; 208
	FCB	12,13,15,16,18,19,21,23,24,26,28,30,32,34,36,38	; 224
	FCB	41,43,45,48,50,53,55,58,60,63,66,68,71,74,77,80	; 240
	FCB	83,86,89,91,94,98,101,104,107,110,113,116,119,122,125,128	; 256
EXTEND	FCB	128,131,134,137,140,143,146,149,152,155,158,162,165,167,170,173	; 16
	FCB	176,179,182,185,188,190,193,196,198,201,203,206,208,211,213,215	; 32
	FCB	218,220,222,224,226,228,230,232,233,235,237,238,240,241,243,244	; 48
	FCB	245,246,247,248,249,250,251,252,252,253,253,254,254,254,254,254	; 64
	FCB	0	;hopefully no glitch here
MAKESINCOS	LDY	#COSTBL
	LDX	#SINTBL
	RTS
ENDFUNCTION
;
;
; ***********************************************************************************************************************
; * rnd(x,y)
; * returns value to b
; ***********************************************************************************************************************
FUNCTION	RND	LOWV,HIGHV
REM	; rnd(low range, high range) value is from range 0-255 and low cannot be higher then high range
REM	; Returns result to b register
DEF	LOWV	BYTE
DEF	HIGHV	BYTE
RNDR	FCB	0
LOWV	FCB	0
HIGHV	FCB	0
STEPR	FCB	0
RES	FCB	0
SUBT	FCB	0
RND	LDB	HIGHV
	INCB
	STB	HIGHV
	SUBB	LOWV
	STB	RNDR
;rnd
RND15	LDB	$113
	ADDB	STEPR
	STB	RES
;now i should have 0-15 in the bit bucket
;wrap it
	ROLB
	STB	STEPR
RNDL15	CMPB	RNDR
	BLS	RNDR15
	SUBB	RNDR
	BRA	RNDL15
RNDR15	ADDB	LOWV
	CMPB	LOWV
	BHI	RNX15
	LDB	LOWV
RNX15	CMPB	HIGHV
	BLS	RNY15
	INC	STEPR
	LDB	HIGHV
RNY15	DECB
	STB	RES
	RTS
ENDFUNCTION
;
; **********************************************************************************************************************
; * circle(display,sin_costable,x1,y1,dx,dy,angle,dist,color)
; * 
; **********************************************************************************************************************
FUNCTION	CIRCLE	DISPLAY,X,PLOTX,PLOTY,RADX,RADY,ANGLE,DIST,SETCOL
REM	; circle(display,[sintbl pointer],x1,y1,diamx,diamy,angle,dist,col) max rad 34 dist=0-255 full circle angle 0-255 as percentage of 360
REM	; Does not push to stack uses X for sine table
DEF	X	STRING
DEF	ANGLE	BYTE
DEF	DIST	BYTE
DEF	PLOTX	BYTE
DEF	PLOTY	BYTE
DEF	RADX	BYTE
DEF	RADY	BYTE
DEF	DISPLAY	BYTE	;new display mode coverage
DEF	SETCOL	BYTE
COLOR0	FCB	128
	FCB	128
	FCB	144
	FCB	160
	FCB	176
	FCB	192
	FCB	208
	FCB	224
	FCB	240
DIST	FCB	0
ANGLE	FCB	0
DISPLAY	FCB	4
XROW	FCB	0
YROW	FCB	0
BIT1	FCB	0
BIT2	FCB	0
BIT3	FCB	0
BIT4	FCB	0
SETCOL	FCB	4
DRAW	FCB	0
ORCHR	FCB	0
WCOL	FCB	0
COLTMP	FCB	0
ODD	FCB	0
RADX	FCB	20	;radius
RADY	FCB	10
PLOTX	FCB	32
PLOTY	FCB	32
MCOL	FCB	4	;color
CNT	FCB	0	;counter from 0-255 to make circle
CALCX	FCB	0
CALCY	FCB	0
SINTB1	FCB	0,0
COSTB1	FCB	0,0
STEPP	FCB	1
CIRCLE	LDD	,X
	TFR	D,X
	LDB	ANGLE	;rotation
	ABX
	STX	SINTB1
	LDB	#64
	ABX
	STX	COSTB1
	CLR	CNT	;counter set
LOOP	LDA	CNT
	LDX	SINTB1
	LDB	CNT
	ABX
	LDB	,X
	LDA	RADX
	MUL
	ASLB	;taken from simons code.... i dont understand it fyi
	ROLA	;same
	SUBA	RADX
	ADDA	PLOTX
	STA	CALCX
;cos
	LDB	CNT
	LDX	COSTB1
	ABX
	LDB	,X
	LDA	RADY
	MUL
	ASLB	;taken from simons code.... i dont understand it fyi
	ROLA	;same
	SUBA	RADY
	ADDA	PLOTY
	STA	CALCY
;ok lets try it out...??
;	LDD	CALCX	;temp code for test
	JSR	DOT
	LDA	CNT
	INCA
	STA	CNT
	CMPA	DIST
	BLO	LOOP
DONEE	RTS
;
;
;
;
;
DOT	CLRA
	CLRB
	STD	XROW
	STD	BIT1
	STD	BIT3
	STD	DRAW
	LDA	DISPLAY
	CMPA	#2
	BEQ	DOTS2
	CMPA	#4
	LBEQ	DOTSG8
	RTS	;not supported
;sg4 mode
DOTS2	LDD	CALCX	;save x,y
	CMPA	#63
	BLS	CHKY1
	RTS	;dot is off the screen
CHKY1	CMPB	#31
	BLS	DRAWD1
	RTS	;dots off the screen
DRAWD1	LDA	SETCOL
	BEQ	DRAWD2
	INC	DRAW
DRAWD2	CLRA
	CLRB
	STD	BIT1
	STD	BIT3
	STD	XROW
	LDX	#1024
	LDA	CALCY
	BITA	#1
	BEQ	SETRD	;odd or even y
	INC	YROW
SETRD	LSRA	;half value y
	LDB	#32
	MUL
	LEAX	D,X
	LDA	CALCX
	BITA	#1
	BEQ	SETCND	;odd or even x
	INC	XROW
SETCND	LSRA	;half value from 64 to 32
	LEAX	A,X
	LDA	,X	;ready to check
;set color correctly
	STA	ORCHR	;saved original chr code
	STA	WCOL	;lets destroy this copy
;see if color is wrong
;if so and not black then errase it all
	LDA	SETCOL
CONTD3	LDU	#COLOR0	;first color index
	LEAU	A,U
	LDA	,U	;color loaded in a
	LDB	WCOL	;load b with whats on screen currently
	ANDB	#240	;extract color from it
	STB	WCOL	;only has color now
	CMPA	WCOL	;compare the two colors
	BEQ	CONTD	;same color? ok we are good to go
;ok we need to kill it
	LDA	,U
	STA	ORCHR	;destroy original color cause its diff
CONTD	LDB	#1	;this is needed to set bit flags
	LDA	ORCHR	;load the one we are using
	ANDA	#15	;extract dots
	BITA	#8
	BNE	SETDOT1
RETD1	BITA	#4
	BNE	SETDOT2
RETD2	BITA	#2
	BNE	SETDOT3
RETD3	BITA	#1
	BNE	SETDOT4
	BRA	SETDOTS
SETDOT1	STB	BIT1
	BRA	RETD1
SETDOT2	STB	BIT2
	BRA	RETD2
SETDOT3	STB	BIT3
	BRA	RETD3
SETDOT4	STB	BIT4
;now we are here
SETDOTS	LDU	#BIT1	;working color= wcol
	LDA	YROW	
	LDB	#2
	MUL
	ADDB	XROW
	LEAU	B,U
	LDB	DRAW
	STB	,U	;set draw on or off for that bit
;reconstruct it
	LDU	#COLOR0
	LDA	SETCOL
	LEAU	A,U
	LDA	,U	;ok got color
	LDB	BIT1	;load the bits in
	LSLB
	ADDB	BIT2
	LSLB
	ADDB	BIT3
	LSLB
	ADDB	BIT4
	STB	COLTMP
	ADDA	COLTMP
	STA	,X
	RTS
;sg8 mode
DOTSG8	LDD	CALCX	;save x,y
	CMPA	#63
	BLS	CHKY8
	RTS	;dot is off the screen
CHKY8	CMPB	#63
	BLS	DRAWD4
	RTS	;dots off the screen
DRAWD4	LDA	SETCOL
	BEQ	DRAWD8
	INC	DRAW
DRAWD8	CLRA
	STA	ODD
	LDX	#5632	;screen start
	LDA	CALCY
	LDB	#32
	MUL
	LEAX	D,X
	LDA	CALCX
	BITA	#1
	BEQ	SGSK1
	INC	ODD
SGSK1	LSRA	;half x
	LEAX	A,X
;ok we are set
	LDA	SETCOL
	STA	WCOL
	BEQ	NOCOLOR	;there is no color its black
;	STA	wCOL
;there is color....lets see if same
	LDB	,X
	ANDB	#240
	STB	ORCHR	;has original color
	LDU	#COLOR0
	LEAU	A,U
	LDA	,U
	STA	WCOL	;fixed color
	CMPA	ORCHR	;colors the same?
	BEQ	ISCOLOR	;same color
;not the same so mt it out
	STA	,X
;draw already equals 0
NOCOLOR	LDA	,X
	ANDA	#240	;strip color out
	STA	WCOL	;saved at set color
ISCOLOR	LDA	,X
	BITA	#1
	BEQ	SETD81
	INC	BIT1
SETD81	BITA	#2
	BEQ	CONST8
	INC	BIT2
;ok what row gets draw 0
CONST8	LDA	DRAW
	LDB	ODD
	BEQ	SDOT82
	STA	BIT1
	BRA	SDOT83
SDOT82	STA	BIT2	
SDOT83	LDA	BIT2
	LSLA
	ADDA	BIT1
	LSLA
	ADDA	BIT2
	LSLA
	ADDA	BIT1
	ADDA	WCOL
	STA	,X
	RTS
ENDFUNCTION
;
;
; ***************************************************************************************************************
; *   syncon (d)
; ***************************************************************************************************************
FUNCTION	SYNCON	DISPLAY
REM	; syncon(display) display mode (2,4) is only supported at the moment sg4 sg8 mode
REM	; Does not push to stack.  Must call sync(d,c) to copy draw screen to actual screen.
DEF	DISPLAY	BYTE
DISPLAY	FCB	0
SYNCON	LDA	DISPLAY
	CMPA	#2
	BEQ	BUFFER1
	CMPA	#4
	BEQ	BUFFER2
	RTS
;
;semigraphics 4 mode 2
;draw setup 1024
;start of view 1536-2048
BUFFER1	CLRA	;set value 7 so its at 3584 view
	LDX	#65478
	STA	1,X
	STA	3,X
	STA	5,X
	STA	6,X
	STA	8,X
	STA	10,X
	STA	12,X
;also setdisplaymode
	LDX	#65472
	STA	,X
	STA	2,X
	STA	4,X
;extra vgd
	CLRA
	LDX	#65314
	STA	,X	;cleared vdg
	RTS
;
;semigraphics 8 mode 4
;3584 start of view to 5632
;5632 draw setup
BUFFER2	CLRA	;7
	LDX	#65478
	STA	1,X
	STA	3,X
	STA	5,X
	STA	6,X
	STA	8,X
	STA	10,X
	STA	12,X
;set 65314
	LDX	#65314
	LDA	,X
	ANDA	#7
	STA	,X	;set bit 7
;also setdisplaymode	;sg8
	LDX	#65472
	STA	,X
	STA	3,X
	STA	4,X
	RTS
ENDFUNCTION
;
; *********************************************************************************************************************
; * set displaymode with no buffers
; * displaymode( set)
; *********************************************************************************************************************
FUNCTION	DISPLAYMODE	SET
REM	; displaymode(display) Only two modes supported are sg4,sg8 ( 2=sg4 , 4=sg8)
REM	; Does not push to stack.  Does not support sync calls (use syncon(mode) followed by sync(mode,c) )
DEF	SET	BYTE
SET	FCB	0
DISPLAYMODE	LDA	SET
	CMPA	#2
	BEQ	BUFFER1	;sg4 mode 2
	CMPA	#4
	BEQ	BUFFER2	;sg8 mode 4
	RTS	;display mode not supported
BUFFER1	CLRA	;set value 2 so its at 1024 view
	LDX	#65478
	STA	0,X
	STA	3,X
	STA	4,X
	STA	6,X
	STA	8,X
	STA	10,X
	STA	12,X
;also setdisplaymode
	LDX	#65472
	STA	,X
	STA	2,X
	STA	4,X
;extra vgd
	CLRA
	LDX	#65314
	STA	,X	;cleared vdg
	RTS
;
;semigraphics 8 mode 4
;3584 start of view to 5632
;5632 draw setup
BUFFER2	CLRA	;11
	LDX	#65478
	STA	1,X
	STA	3,X
	STA	4,X
	STA	7,X
	STA	8,X
	STA	10,X
	STA	12,X
;set 65314
	LDX	#65314
	LDA	,X
	ANDA	#7
	STA	,X	;set bit 7
;also setdisplaymode	;sg8
	LDX	#65472
	STA	,X
	STA	3,X
	STA	4,X
	RTS
ENDFUNCTION
;
; ***************************************************************************************************************
; *   sync(d,col)
; ***************************************************************************************************************
FUNCTION	SYNC	DISPLAY,COLOR
REM	; sync(display,color) display 2,4 only supported so far. color 0-8 to clear after copy. 9=copy no erase.
REM	; Does not push to stack
DEF	DISPLAY	BYTE
DEF	COLOR	BYTE
DISPLAY	FCB	0
COLOR	FCB	0
COLORD	FCB	0,0
DOTS	FCB	0
SYNC	LDA	DISPLAY
	CMPA	#2
	BEQ	BUFFER1
	CMPA	#4
	BEQ	BUFFER2
	RTS	;add more displays as i go
;mode 2 sg4
BUFFER1	LDA	COLOR
	CMPA	#0
	BEQ	NOCOL1
	CMPA	#9
	BEQ	NOERR1	;no erase
	DECA	;there is color
	LSLA
	LSLA
	LSLA
	LSLA
	ADDA	#128
	ADDA	#15
	STA	COLOR
	BRA	SETMD1
NOCOL1	LDA	#128
	STA	COLOR
SETMD1	LDA	COLOR
	LDB	COLOR
	STD	COLORD
	LDX	#3584	;view area
	LDY	#1024	;copy from
	LDU	#COLORD
PLC1	LDD	,Y
	STD	,X++
	LDD	,U
	STD	,Y++
	CMPY	#1536
	BLO	PLC1
	RTS
NOERR1	LDX	#3584
	LDY	#1024
PLCC1	LDD	,Y++
	STD	,X++
	CMPY	#1536
	BLO	PLCC1
	RTS
;
;mode 4 sg8
BUFFER2	LDA	COLOR
	CMPA	#0
	BEQ	NOCOL2
	CMPA	#9
	BEQ	NOERR2	;no erase
	DECA	;there is color
	BEQ	NOS2	;no need for shift
	LSLA
	LSLA
	LSLA
	LSLA
NOS2	ADDA	#128
	ADDA	#15
	STA	COLOR
	BRA	SETMD2
NOCOL2	LDA	#128	;no color
	STA	COLOR
SETMD2	LDA	COLOR
	LDB	COLOR
	STD	COLORD
	LDX	#3584
	LDY	#5632
	LDU	#COLORD
PLC2	LDD	,Y
	STD	,X++
	LDD	,U
	STD	,Y++
	CMPY	#7680
	BLO	PLC2
	RTS
NOERR2	LDX	#3584	;no erase just copy
	LDY	#5632
PLCC2	LDD	,Y++
	STD	,X++
	CMPY	#7680
	BLO	PLCC2
	RTS
ENDFUNCTION
;
; *************************************************************************************************************************
; * set(d,x,y,c)
; *************************************************************************************************************************
FUNCTION	SET	DISPLAY,X1,Y1,SETCOL
REM	; set(display,x,y,color)  display=(2 or 4) x,y,color 0-8
REM	; Does not push to stack
DEF	X1	BYTE
DEF	Y1	BYTE
DEF	SETCOL	BYTE
DEF	DISPLAY	BYTE
COLOR0	FCB	128
	FCB	128
	FCB	144
	FCB	160
	FCB	176
	FCB	192
	FCB	208
	FCB	224
	FCB	240
DISPLAY	FCB	4
XROW	FCB	0
YROW	FCB	0
X1	FCB	0
Y1	FCB	0
BIT1	FCB	0
BIT2	FCB	0
BIT3	FCB	0
BIT4	FCB	0
SETCOL	FCB	4
DRAW	FCB	0
ORCHR	FCB	0
WCOL	FCB	0
COLTMP	FCB	0
ODD	FCB	0
SET	CLRA
	CLRB
	STD	XROW
	STD	BIT1
	STD	BIT3
	STD	DRAW
	LDA	DISPLAY
	CMPA	#2
	BEQ	DOTS2
	CMPA	#4
	LBEQ	DOTSG8
	RTS	;not supported
;sg4 mode
DOTS2	LDD	X1	;save x,y
	CMPA	#63
	BLS	CHKY1
	RTS	;dot is off the screen
CHKY1	CMPB	#31
	BLS	DRAWD1
	RTS	;dots off the screen
DRAWD1	LDA	SETCOL
	BEQ	DRAWD2
	INC	DRAW
DRAWD2	CLRA
	CLRB
	STD	BIT1
	STD	BIT3
	STD	XROW
	LDX	#1024
	LDA	Y1
	BITA	#1
	BEQ	SETRD	;odd or even y
	INC	YROW
SETRD	LSRA	;half value y
	LDB	#32
	MUL
	LEAX	D,X
	LDA	X1
	BITA	#1
	BEQ	SETCND	;odd or even x
	INC	XROW
SETCND	LSRA	;half value from 64 to 32
	LEAX	A,X
	LDA	,X	;ready to check
;set color correctly
	STA	ORCHR	;saved original chr code
	STA	WCOL	;lets destroy this copy
;see if color is wrong
;if so and not black then errase it all
	LDA	SETCOL
CONTD3	LDU	#COLOR0	;first color index
	LEAU	A,U
	LDA	,U	;color loaded in a
	LDB	WCOL	;load b with whats on screen currently
	ANDB	#240	;extract color from it
	STB	WCOL	;only has color now
	CMPA	WCOL	;compare the two colors
	BEQ	CONTD	;same color? ok we are good to go
;ok we need to kill it
	LDA	,U
	STA	ORCHR	;destroy original color cause its diff
CONTD	LDB	#1	;this is needed to set bit flags
	LDA	ORCHR	;load the one we are using
	ANDA	#15	;extract dots
	BITA	#8
	BNE	SETDOT1
RETD1	BITA	#4
	BNE	SETDOT2
RETD2	BITA	#2
	BNE	SETDOT3
RETD3	BITA	#1
	BNE	SETDOT4
	BRA	SETDOTS
SETDOT1	STB	BIT1
	BRA	RETD1
SETDOT2	STB	BIT2
	BRA	RETD2
SETDOT3	STB	BIT3
	BRA	RETD3
SETDOT4	STB	BIT4
;now we are here
SETDOTS	LDU	#BIT1	;working color= wcol
	LDA	YROW	
	LDB	#2
	MUL
	ADDB	XROW
	LEAU	B,U
	LDB	DRAW
	STB	,U	;set draw on or off for that bit
;reconstruct it
	LDU	#COLOR0
	LDA	SETCOL
	LEAU	A,U
	LDA	,U	;ok got color
	LDB	BIT1	;load the bits in
	LSLB
	ADDB	BIT2
	LSLB
	ADDB	BIT3
	LSLB
	ADDB	BIT4
	STB	COLTMP
	ADDA	COLTMP
	STA	,X
	RTS
;sg8 mode
DOTSG8	LDD	X1	;save x,y
	CMPA	#63
	BLS	CHKY8
	RTS	;dot is off the screen
CHKY8	CMPB	#63
	BLS	DRAWD4
	RTS	;dots off the screen
DRAWD4	LDA	SETCOL
	BEQ	DRAWD8
	INC	DRAW
DRAWD8	CLRA
	STA	ODD
	LDX	#5632	;screen start
	LDA	Y1
	LDB	#32
	MUL
	LEAX	D,X
	LDA	X1
	BITA	#1
	BEQ	SGSK1
	INC	ODD
SGSK1	LSRA	;half x
	LEAX	A,X
;ok we are set
	LDA	SETCOL
	STA	WCOL
	BEQ	NOCOLOR	;there is no color its black
;there is color....lets see if same
	LDB	,X
	ANDB	#240
	STB	ORCHR	;has original color
	LDU	#COLOR0
	LEAU	A,U
	LDA	,U
	STA	WCOL	;fixed color
	CMPA	ORCHR	;colors the same?
	BEQ	ISCOLOR	;same color
;not the same so mt it out
	STA	,X
;draw already equals 0
NOCOLOR	LDA	,X
	ANDA	#240	;strip color out
	STA	WCOL	;saved at set color
ISCOLOR	LDA	,X
	BITA	#1
	BEQ	SETD81
	INC	BIT1
SETD81	BITA	#2
	BEQ	CONST8
	INC	BIT2
;ok what row gets draw 0
CONST8	LDA	DRAW
	LDB	ODD
	BEQ	SDOT82
	STA	BIT1
	BRA	SDOT83
SDOT82	STA	BIT2	
SDOT83	LDA	BIT2
	LSLA
	ADDA	BIT1
	LSLA
	ADDA	BIT2
	LSLA
	ADDA	BIT1
	ADDA	WCOL
	STA	,X
	RTS
ENDFUNCTION
;
; ***************************************************************************************************************
; *   line2 (display,vect1,vect2,color)   sg mode
; ***************************************************************************************************************
FUNCTION	LINE2	DISPLAY,X,Y,SETCOL
REM	; line(display,vecx,vecy,col)  display mode (2,4) only.  x and y need to be indexed. vector should be x,y data color 0-8
REM	; Does not push to stack
DEF	X	STRING
DEF	Y	STRING
DEF	DISPLAY	BYTE	;new display mode coverage
DEF	SETCOL	BYTE
DISPLAY	FCB	0
SETCOL	FCB	0
TMP	FCB	0
X1	FCB	0
Y1	FCB	0
X2	FCB	0
Y2	FCB	0
XD	FCB	0
YD	FCB	0
XC	FCB	0
YC	FCB	0
WN	FCB	0
CNT	FCB	0
COLOR0	FCB	128	;green but we can take original screen value
COLOR1	FCB	128	;green
COLOR2	FCB	144	;yellow
COLOR3	FCB	160	;blue
COLOR4	FCB	176	;red
COLOR5	FCB	192	;white
COLOR6	FCB	208	;cyan
COLOR7	FCB	224	;magenta
COLOR8	FCB	240	;orange
SETX	FCB	0
SETY	FCB	0
WCOL	FCB	0	;working with
XROW	FCB	0
YROW	FCB	0
ORCHR	FCB	0
DRAW	FCB	0
ODD	FCB	0	;is it setting or erasing
BIT1	FCB	0
BIT2	FCB	0
BIT3	FCB	0
BIT4	FCB	0
COLTMP	FCB	0
LINE2	LDD	,X	;deals with registers for load
	STD	X1
	LDD	,Y
	STD	X2
	CLR	DRAW
	LDA	SETCOL
	CMPA	#0
	BEQ	CONTD2	;this is a dot off command
	INC	DRAW	
CONTD2	LDA	X1	;ok no swapping just get distances
	CMPA	X2
	BLS	LINS1	;if x1 is lower
	SUBA	X2	;its greater so sub x2
	STA	XD	;save x distance
	BRA	LINS2
LINS1	LDA	X2
	SUBA	X1
	STA	XD
;
LINS2	LDA	Y1	;ok now lets look at y
	CMPA	Y2
	BLS	LINS3	;y1 is lower?
	SUBA	Y2
	STA	YD	;save y distance
	BRA	LINS4
LINS3	LDA	Y2
	SUBA	Y1
	STA	YD
;
;which is greater?
LINS4	LDA	YD
	CMPA	XD
	BLO	LINS5	;y is lower then goto lins5
	STA	WN	;x is lower so save y as whole fraction
	BRA	LINS6	;done
LINS5	LDA	XD	;nope y is lower so save x as whole fraction
	STA	WN	;stored it
; ok now the main loop
LINS6	CLRA
	STA	XC	;x counter
	STA	YC	;y counter
	LDA	WN
	STA	CNT	;set counter
	INC	CNT
;ok check what display
	LDA	DISPLAY
	CMPA	#4
	LBEQ	LINE22	;sg8 mode line
;main loop for sg 2
LINE1	LDD	X1	;main loop
	STD	SETX
	JSR	DOT2	;jump to draw dot
	LDA	XC
	ADDA	XD	;add distance to xc counter
	CMPA	WN	;has it gone over or same as our whole fraction?
	BHS	ADV1	;it has on x.... lets advance it
	STA	XC	;nope keep counting 
	BRA	CHK1	;lets check y now
;
ADV1	SUBA	WN	;advance the x counter....subtract the whole fraction
	STA	XC	;store remainder to play with next loop
	LDA	X1
	CMPA	X2
	BHI	ADVX1
	INC	X1	;increase y plot or dec?
	BRA	CHK1
ADVX1	DEC	X1
CHK1	LDA	YC	;now the y axis.... loader up
	ADDA	YD	;add its distance
	CMPA	WN	;has it gone over or the same as our whole fraction
	BHS	ADVY1	;it has on y.... lets advance it
	STA	YC	;nope... keep counting
	BRA	CHKX1	;done
;advance it
ADVY1	SUBA	WN	;subtract the whole number from y counter
	STA	YC	;store the remainder for next loop
	LDA	Y1
	CMPA	Y2
	BHI	ADY1
	INC	Y1	;increase y plot or dec?
	BRA	CHKX1
ADY1	DEC	Y1
;done?
CHKX1	DEC	CNT	;check the counter
	BNE	LINE1	;nope keep plotting the line
	RTS	;we are done
;
DOT2	CLRA
	CLRB
	STD	BIT1
	STD	BIT3
	STD	XROW
	LDX	#1024
	LDA	SETY
	BITA	#1
	BEQ	SETRD	;odd or even y
	INC	YROW
SETRD	LSRA	;half value y
	LDB	#32
	MUL
	LEAX	D,X
	LDA	SETX
	BITA	#1
	BEQ	SETCND	;odd or even x
	INC	XROW
SETCND	LSRA	;half value from 64 to 32
	LEAX	A,X
	LDA	,X	;ready to check
;set color correctly
	STA	ORCHR	;saved original chr code
	STA	WCOL	;lets destroy this copy
;see if color is wrong
;if so and not black then errase it all
	LDA	SETCOL
CONTD3	LDU	#COLOR0	;first color index
	LEAU	A,U
	LDA	,U	;color loaded in a
	LDB	WCOL	;load b with whats on screen currently
	ANDB	#240	;extract color from it
	STB	WCOL	;only has color now
	CMPA	WCOL	;compare the two colors
	BEQ	CONTD	;same color? ok we are good to go
;ok we need to kill it
	LDA	,U
	STA	ORCHR	;destroy original color cause its diff
CONTD	LDB	#1	;this is needed to set bit flags
	LDA	ORCHR	;load the one we are using
	ANDA	#15	;extract dots
	BITA	#8
	BNE	SETDOT1
RETD1	BITA	#4
	BNE	SETDOT2
RETD2	BITA	#2
	BNE	SETDOT3
RETD3	BITA	#1
	BNE	SETDOT4
	BRA	SETDOTS
SETDOT1	STB	BIT1
	BRA	RETD1
SETDOT2	STB	BIT2
	BRA	RETD2
SETDOT3	STB	BIT3
	BRA	RETD3
SETDOT4	STB	BIT4
;now we are here
SETDOTS	LDU	#BIT1	;working color= wcol
	LDA	YROW	
	LDB	#2
	MUL
	ADDB	XROW
	LEAU	B,U
	LDB	DRAW
	STB	,U	;set draw on or off for that bit
;reconstruct it
	LDU	#COLOR0
	LDA	SETCOL
	LEAU	A,U
	LDA	,U	;ok got color
	LDB	BIT1	;load the bits in
	LSLB
	ADDB	BIT2
	LSLB
	ADDB	BIT3
	LSLB
	ADDB	BIT4
	STB	COLTMP
	ADDA	COLTMP
	STA	,X
	RTS
;
; sg8 mode
LINE22	LDD	X1	;main loop
	STA	SETX
;	LDA	Y1
	STB	SETY
	JSR	DOTSG8	;jump to draw dot
	LDA	XC
	ADDA	XD	;add distance to xc counter
	CMPA	WN	;has it gone over or same as our whole fraction?
	BHS	ADV2	;it has on x.... lets advance it
	STA	XC	;nope keep counting 
	BRA	CHK2	;lets check y now
;
ADV2	SUBA	WN	;advance the x counter....subtract the whole fraction
	STA	XC	;store remainder to play with next loop
	LDA	X1
	CMPA	X2
	BHI	ADVX2
	INC	X1	;increase y plot or dec?
	BRA	CHK2
ADVX2	DEC	X1
CHK2	LDA	YC	;now the y axis.... loader up
	ADDA	YD	;add its distance
	CMPA	WN	;has it gone over or the same as our whole fraction
	BHS	ADVY2	;it has on y.... lets advance it
	STA	YC	;nope... keep counting
	BRA	CHKX2	;done
;advance it
ADVY2	SUBA	WN	;subtract the whole number from y counter
	STA	YC	;store the remainder for next loop
	LDA	Y1
	CMPA	Y2
	BHI	ADY2
	INC	Y1	;increase y plot or dec?
	BRA	CHKX2
ADY2	DEC	Y1
;done?
CHKX2	DEC	CNT	;check the counter
	BNE	LINE22	;nope keep plotting the line
	RTS	;we are done
;
;sg8 mode
DOTSG8	CLRA
	CLRB
	STD	BIT1
	STA	ODD
	LDX	#5632	;screen start
	LDA	SETY
	LDB	#32
	MUL
	LEAX	D,X
	LDA	SETX
	BITA	#1
	BEQ	SGSK1
	INC	ODD
SGSK1	LSRA	;half x
	LEAX	A,X
;ok we are set
	LDA	SETCOL
	STA	WCOL
	BEQ	NOCOLOR	;there is no color its black
;	STA	wCOL
;there is color....lets see if same
	LDB	,X
	ANDB	#240
	STB	ORCHR	;has original color
;	LSLA
;	LSLA
;	LSLA
;	LSLA
;	ADDA	#128
	LDU	#COLOR0
	LEAU	A,U
	LDA	,U
	STA	WCOL	;fixed color
	CMPA	ORCHR	;colors the same?
	BEQ	ISCOLOR	;same color
;not the same so mt it out
	STA	,X
;draw already equals 0
NOCOLOR	LDA	,X
	ANDA	#240	;strip color out
	STA	WCOL	;saved at set color
ISCOLOR	LDA	,X
	BITA	#1
	BEQ	SETD81
	INC	BIT1
SETD81	BITA	#2
	BEQ	CONST8
	INC	BIT2
;ok what row gets draw 0
CONST8	LDA	DRAW
	LDB	ODD
	BEQ	SDOT82
	STA	BIT1
	BRA	SDOT83
SDOT82	STA	BIT2	
SDOT83	LDA	BIT2
	LSLA
	ADDA	BIT1
	LSLA
	ADDA	BIT2
	LSLA
	ADDA	BIT1
	ADDA	WCOL
	STA	,X
	RTS
ENDFUNCTION
;
*
;
; ***************************************************************************************************************
; *   line(display,x1,y1,x2,y2,color)   sg mode
; ***************************************************************************************************************
FUNCTION	LINE	DISPLAY,X1,Y1,X2,Y2,SETCOL
REM	; line(display,x1,y1,x2,y2,col)  display mode (2,4) only.   color 0-8
REM	; Does not push to stack
DEF	X1	BYTE
DEF	Y1	BYTE
DEF	X2	BYTE
DEF	Y2	BYTE
DEF	DISPLAY	BYTE	;new display mode coverage
DEF	SETCOL	BYTE
DISPLAY	FCB	0
SETCOL	FCB	0
TMP	FCB	0
X1	FCB	0
Y1	FCB	0
X2	FCB	0
Y2	FCB	0
XD	FCB	0
YD	FCB	0
XC	FCB	0
YC	FCB	0
WN	FCB	0
CNT	FCB	0
COLOR0	FCB	128	;green but we can take original screen value
COLOR1	FCB	128	;green
COLOR2	FCB	144	;yellow
COLOR3	FCB	160	;blue
COLOR4	FCB	176	;red
COLOR5	FCB	192	;white
COLOR6	FCB	208	;cyan
COLOR7	FCB	224	;magenta
COLOR8	FCB	240	;orange
SETX	FCB	0
SETY	FCB	0
WCOL	FCB	0	;working with
XROW	FCB	0
YROW	FCB	0
ORCHR	FCB	0
DRAW	FCB	0
ODD	FCB	0	;is it setting or erasing
BIT1	FCB	0
BIT2	FCB	0
BIT3	FCB	0
BIT4	FCB	0
COLTMP	FCB	0
LINE	CLR	DRAW
	LDA	SETCOL
	CMPA	#0
	BEQ	CONTD2	;this is a dot off command
	INC	DRAW	
CONTD2	LDA	X1	;ok no swapping just get distances
	CMPA	X2
	BLS	LINS1	;if x1 is lower
	SUBA	X2	;its greater so sub x2
	STA	XD	;save x distance
	BRA	LINS2
LINS1	LDA	X2
	SUBA	X1
	STA	XD
;
LINS2	LDA	Y1	;ok now lets look at y
	CMPA	Y2
	BLS	LINS3	;y1 is lower?
	SUBA	Y2
	STA	YD	;save y distance
	BRA	LINS4
LINS3	LDA	Y2
	SUBA	Y1
	STA	YD
;
;which is greater?
LINS4	LDA	YD
	CMPA	XD
	BLO	LINS5	;y is lower then goto lins5
	STA	WN	;x is lower so save y as whole fraction
	BRA	LINS6	;done
LINS5	LDA	XD	;nope y is lower so save x as whole fraction
	STA	WN	;stored it
; ok now the main loop
LINS6	CLRA
	STA	XC	;x counter
	STA	YC	;y counter
	LDA	WN
	STA	CNT	;set counter
	INC	CNT
;ok check what display
	LDA	DISPLAY
	CMPA	#4
	LBEQ	LINE22	;sg8 mode line
;main loop for sg 2
LINE1	LDD	X1	;main loop
	STD	SETX
	JSR	DOT2	;jump to draw dot
	LDA	XC
	ADDA	XD	;add distance to xc counter
	CMPA	WN	;has it gone over or same as our whole fraction?
	BHS	ADV1	;it has on x.... lets advance it
	STA	XC	;nope keep counting 
	BRA	CHK1	;lets check y now
;
ADV1	SUBA	WN	;advance the x counter....subtract the whole fraction
	STA	XC	;store remainder to play with next loop
	LDA	X1
	CMPA	X2
	BHI	ADVX1
	INC	X1	;increase y plot or dec?
	BRA	CHK1
ADVX1	DEC	X1
CHK1	LDA	YC	;now the y axis.... loader up
	ADDA	YD	;add its distance
	CMPA	WN	;has it gone over or the same as our whole fraction
	BHS	ADVY1	;it has on y.... lets advance it
	STA	YC	;nope... keep counting
	BRA	CHKX1	;done
;advance it
ADVY1	SUBA	WN	;subtract the whole number from y counter
	STA	YC	;store the remainder for next loop
	LDA	Y1
	CMPA	Y2
	BHI	ADY1
	INC	Y1	;increase y plot or dec?
	BRA	CHKX1
ADY1	DEC	Y1
;done?
CHKX1	DEC	CNT	;check the counter
	BNE	LINE1	;nope keep plotting the line
	RTS	;we are done
;
DOT2	CLRA
	CLRB
	STD	BIT1
	STD	BIT3
	STD	XROW
	LDX	#1024
	LDA	SETY
	BITA	#1
	BEQ	SETRD	;odd or even y
	INC	YROW
SETRD	LSRA	;half value y
	LDB	#32
	MUL
	LEAX	D,X
	LDA	SETX
	BITA	#1
	BEQ	SETCND	;odd or even x
	INC	XROW
SETCND	LSRA	;half value from 64 to 32
	LEAX	A,X
	LDA	,X	;ready to check
;set color correctly
	STA	ORCHR	;saved original chr code
	STA	WCOL	;lets destroy this copy
;see if color is wrong
;if so and not black then errase it all
	LDA	SETCOL
CONTD3	LDU	#COLOR0	;first color index
	LEAU	A,U
	LDA	,U	;color loaded in a
	LDB	WCOL	;load b with whats on screen currently
	ANDB	#240	;extract color from it
	STB	WCOL	;only has color now
	CMPA	WCOL	;compare the two colors
	BEQ	CONTD	;same color? ok we are good to go
;ok we need to kill it
	LDA	,U
	STA	ORCHR	;destroy original color cause its diff
CONTD	LDB	#1	;this is needed to set bit flags
	LDA	ORCHR	;load the one we are using
	ANDA	#15	;extract dots
	BITA	#8
	BNE	SETDOT1
RETD1	BITA	#4
	BNE	SETDOT2
RETD2	BITA	#2
	BNE	SETDOT3
RETD3	BITA	#1
	BNE	SETDOT4
	BRA	SETDOTS
SETDOT1	STB	BIT1
	BRA	RETD1
SETDOT2	STB	BIT2
	BRA	RETD2
SETDOT3	STB	BIT3
	BRA	RETD3
SETDOT4	STB	BIT4
;now we are here
SETDOTS	LDU	#BIT1	;working color= wcol
	LDA	YROW	
	LDB	#2
	MUL
	ADDB	XROW
	LEAU	B,U
	LDB	DRAW
	STB	,U	;set draw on or off for that bit
;reconstruct it
	LDU	#COLOR0
	LDA	SETCOL
	LEAU	A,U
	LDA	,U	;ok got color
	LDB	BIT1	;load the bits in
	LSLB
	ADDB	BIT2
	LSLB
	ADDB	BIT3
	LSLB
	ADDB	BIT4
	STB	COLTMP
	ADDA	COLTMP
	STA	,X
	RTS
;
; sg8 mode
LINE22	LDD	X1	;main loop
	STA	SETX
;	LDA	Y1
	STB	SETY
	JSR	DOTSG8	;jump to draw dot
	LDA	XC
	ADDA	XD	;add distance to xc counter
	CMPA	WN	;has it gone over or same as our whole fraction?
	BHS	ADV2	;it has on x.... lets advance it
	STA	XC	;nope keep counting 
	BRA	CHK2	;lets check y now
;
ADV2	SUBA	WN	;advance the x counter....subtract the whole fraction
	STA	XC	;store remainder to play with next loop
	LDA	X1
	CMPA	X2
	BHI	ADVX2
	INC	X1	;increase y plot or dec?
	BRA	CHK2
ADVX2	DEC	X1
CHK2	LDA	YC	;now the y axis.... loader up
	ADDA	YD	;add its distance
	CMPA	WN	;has it gone over or the same as our whole fraction
	BHS	ADVY2	;it has on y.... lets advance it
	STA	YC	;nope... keep counting
	BRA	CHKX2	;done
;advance it
ADVY2	SUBA	WN	;subtract the whole number from y counter
	STA	YC	;store the remainder for next loop
	LDA	Y1
	CMPA	Y2
	BHI	ADY2
	INC	Y1	;increase y plot or dec?
	BRA	CHKX2
ADY2	DEC	Y1
;done?
CHKX2	DEC	CNT	;check the counter
	BNE	LINE22	;nope keep plotting the line
	RTS	;we are done
;
;sg8 mode
DOTSG8	CLRA
	CLRB
	STD	BIT1
	STA	ODD
	LDX	#5632	;screen start
	LDA	SETY
	LDB	#32
	MUL
	LEAX	D,X
	LDA	SETX
	BITA	#1
	BEQ	SGSK1
	INC	ODD
SGSK1	LSRA	;half x
	LEAX	A,X
;ok we are set
	LDA	SETCOL
	STA	WCOL
	BEQ	NOCOLOR	;there is no color its black
;	STA	wCOL
;there is color....lets see if same
	LDB	,X
	ANDB	#240
	STB	ORCHR	;has original color
;	LSLA
;	LSLA
;	LSLA
;	LSLA
;	ADDA	#128
	LDU	#COLOR0
	LEAU	A,U
	LDA	,U
	STA	WCOL	;fixed color
	CMPA	ORCHR	;colors the same?
	BEQ	ISCOLOR	;same color
;not the same so mt it out
	STA	,X
;draw already equals 0
NOCOLOR	LDA	,X
	ANDA	#240	;strip color out
	STA	WCOL	;saved at set color
ISCOLOR	LDA	,X
	BITA	#1
	BEQ	SETD81
	INC	BIT1
SETD81	BITA	#2
	BEQ	CONST8
	INC	BIT2
;ok what row gets draw 0
CONST8	LDA	DRAW
	LDB	ODD
	BEQ	SDOT82
	STA	BIT1
	BRA	SDOT83
SDOT82	STA	BIT2	
SDOT83	LDA	BIT2
	LSLA
	ADDA	BIT1
	LSLA
	ADDA	BIT2
	LSLA
	ADDA	BIT1
	ADDA	WCOL
	STA	,X
	RTS
ENDFUNCTION
;
*
*
;**************************************************************************************
;* cls(display,color) 0-8 SEMI GRAPHICS DEFAULT MODE
;**************************************************************************************
FUNCTION	CLS	DISPLAY,CC
REM	; CLS(display,n)  display 2,4 supported.This is a number from 0 to 8.
REM	; Does not push to stack and uses A,X,Y
DEF	CC	BYTE
DEF	DISPLAY	BYTE
CC	FCB	0	;cc store
DISPLAY	FCB	0
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	LDA	DISPLAY
	CMPA	#2
	BEQ	MODE2
	CMPA	#4
	BEQ	MODE4
	RTS	;display not supported
MODE2	LDX	#1024
	LDA	CC
	CMPA	#8
	BLS	LOP2
	LDA	#8
	STA	CC
LOP2	LDA	CC
	LDY	#C0
	LDA	CC
	LEAY	A,Y
	LDA	,Y	;color loaded
	LDB	,Y
LP2	STD	,X++
	CMPX	#1536
	BLO	LP2
	RTS
;sg8
MODE4	LDX	#5632
	LDA	CC	;load the color
	CMPA	#8
	BLS	LOP4	;color value is ok
	LDA	#8
	STA	CC
LOP4	LDY	#C0	;setup y with color
	LEAY	A,Y
	LDA	,Y	;color loaded
	LDB	,Y
LP4	STD	,X++
	CMPX	#7680
	BLO	LP4
	RTS
ENDFUNCTION
;**************************************************************************************
;* print(display,caps on,pos,string) 
;**************************************************************************************
FUNCTION	PRINT	DISPLAY,CAP,CC,X
REM	;PRINT(d,c,pos,string) d=2,4.c=0 or 1 for starting in caps mode. pos=0-511 for screen position.
REM	;Does not push to stack and uses A,B,X,Y. uses display mode 2
DEF	CAP	BYTE	;start in caps mode or not?
DEF	DISPLAY	BYTE
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
MULTI	FCB	0
ROWY	FCB	0
DISPLAY	FCB	0
PRINT	LDA	DISPLAY
	CMPA	#2
	BEQ	SG4	
	CMPA	#4
	BEQ	SG8
	RTS
SG4	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
;****************************************************
SG8	RTS
	LDD	CC	;check if out of bounds
	CMPD	#511
	BLS	LP7
	LDD	#511
	STD	CC
;
LP7	CLR	ROWY
	LDD	CC
	CMPD	#32
	BLS	LP15	;no subtract needed its on first row
LP14	INC	ROWY
	SUBD	#32
	CMPD	#32
	BHI	LP14
LP15	TFR	X,Y
	LDX	#5632
	LDA	#32
	LDB	ROWY
	MUL
	ABX
	ABX
	ABX
	ABX
	EXG	X,Y
	LDB	CAP	;load b with caps flag
LP8	LDA	,X+
;process this ascii code
	CMPA	#97
	BLO	LP9
	CLRB
	STB	CAP
;assume lowercase
	SUBA	#96
LP9	CMPA	#65
	BLO	LP10
	CMPA	#90
	BHI	LP10
	LDB	#1
	STB	CAP
LP10	CMPA	#32
	BNE	LP11
	CMPB	#1	;is cap on
	BNE	LP11
	ADDA	#64	;fix green space
;lets look at numbers
LP11	CMPA	#33
	BLO	LP12
	CMPA	#63
	BHI	LP12
;its a number
	CMPB	#1
	BNE	LP12
;change number to cap
	ADDA	#64
LP12	STA	,Y+
	STA	31,Y
;	STA	63,Y
;	STA	95,Y
	CMPY	#7584
	BHS	LP13
	LDA	,X
	CMPA	#0
	BNE	LP8
LP13	RTS	;return
ENDFUNCTION
;**************************************************************************************
;* inkey$
;**************************************************************************************
FUNCTION	INKEY$	
REM	;inkey$()  returns keypress value into A register...  =0 if nothing pressed
REM	;Does not push to stack and assume it uses A,B,X,Y,U but you can test to confirm.
INKEY$	JSR	[$A000]
	RTS
ENDFUNCTION
; *************************************************************************************
; * str$(word address,retstring adr)
; *************************************************************************************
; convert number to string
; put numbers in for string add
; 
FUNCTION	STR$	NUMSTA,X
REM	;str$(word,[return string])  word is a value or 0-65535 and needs a return address to populate string. 6 BYTES REQUIRED
REM	;Does not push to stack and uses A,B,X,Y,U registers
DEF	X	STRING	;populate x register with string at x
DEF	NUMSTA	WORD	;save 
NUMTMP	FCB	0	;temporary a storage
NUMSTA	FCB	0	;storage of number
NUMSTB	FCB	0
NUMADR1	FCB	0,0
NUMST1	FCN	"00000"	;string 1
NUMST2	FCN	"00000"	;string 2
NUMB16	FCN	"32768"
NUMB15	FCN	"16384"
NUMB14	FCN	"08192"
NUMB13	FCN	"04096"
NUMB12	FCN	"02048"
NUMB11	FCN	"01024"
NUMB10	FCN	"00512"
NUMB09	FCN	"00256"
NUMB08	FCN	"00128"
NUMB07	FCN	"00064"
NUMB06	FCN	"00032"
NUMB05	FCN	"00016"
NUMB04	FCN	"00008"
NUMB03	FCN	"00004"
NUMB02	FCN	"00002"
NUMB01	FCN	"00001"
NUMB00	FCN	"00000"
; ***********************************************************
; *  write word not 2s comp
; ***********************************************************
STR$	STX	NUMADR1	;may need to fix
	STD	NUMSTA	;fixed i hope
	CMPD	#0
	LBEQ	WRIST0
	LDY	#NUMST2
	LDD	#$3030
	STD	,Y
	STD	2,Y
	STD	3,Y	;cleared storage with 0*S
	LDX	#NUMSTA	;first byte
	LDA	,X
	BEQ	WRIDR7	;one byte only
	BITA	#128
	BNE	STRA16
WRIDR0	BITA	#64
	BNE	STRA15
WRIDR1	BITA	#32
	BNE	STRA14	
WRIDR2	BITA	#16
	BNE	STRA13	
WRIDR3	BITA	#8
	BNE	STRA12	
WRIDR4	BITA	#4
	BNE	STRA11
WRIDR5	BITA	#2
	BNE	STRA10	
WRIDR6	BITA	#1
	BNE	STRA09
WRIDR7	LDX	#NUMSTB
	LDA	,X	;test second bit
	BITA	#128
	BNE	STRA08
WRIDR8	BITA	#64
	BNE	STRA07
WRIDR9	BITA	#32
	BNE	STRA06	
WRID10	BITA	#16
	BNE	STRA05	
WRID11	BITA	#8
	BNE	STRA04	
WRID12	BITA	#4
	BNE	STRA03
WRID13	BITA	#2
	BNE	STRA02	
WRID14	BITA	#1
	BNE	STRA01
	LBRA	STRFIX1	; done the string is now stored in numst2
STRA16	LDU	#NUMB16
	JSR	STRINGA
	BRA	WRIDR0
STRA15	LDU	#NUMB15
	JSR	STRINGA
	BRA	WRIDR1
STRA14	LDU	#NUMB14
	JSR	STRINGA
	BRA	WRIDR2
STRA13	LDU	#NUMB13
	JSR	STRINGA
	BRA	WRIDR3
STRA12	LDU	#NUMB12
	JSR	STRINGA
	BRA	WRIDR4
STRA11	LDU	#NUMB11
	JSR	STRINGA
	BRA	WRIDR5
STRA10	LDU	#NUMB10
	JSR	STRINGA
	BRA	WRIDR6
STRA09	LDU	#NUMB09
	JSR	STRINGA
	BRA	WRIDR7
STRA08	LDU	#NUMB08
	JSR	STRINGA
	BRA	WRIDR8
STRA07	LDU	#NUMB07
	JSR	STRINGA
	BRA	WRIDR9
STRA06	LDU	#NUMB06
	JSR	STRINGA
	BRA	WRID10
STRA05	LDU	#NUMB05
	JSR	STRINGA
	BRA	WRID11
STRA04	LDU	#NUMB04
	JSR	STRINGA
	BRA	WRID12
STRA03	LDU	#NUMB03
	JSR	STRINGA
	BRA	WRID13
STRA02	LDU	#NUMB02
	JSR	STRINGA
	BRA	WRID14
STRA01	LDU	#NUMB01
	JSR	STRINGA
STRFIX1	LDX	NUMADR1	;load value thats there
	LDY	#NUMST2
	LDB	#6
STRFLP0	DECB
	CMPB	#1
	BLS	STRFLP2
	LDA	,Y+
	CMPA	#48
	BEQ	STRFLP3	;get rid of extra 0*S and make spaces
	BRA	STRFLP1
STRFLP2	LDA	,Y+
STRFLP1	STA	,X+
	DECB
	CMPB	#0
	BHI	STRFLP2
	CLRA
	STA	,X
	RTS	;done sub
STRFLP3	LDA	#32	;this can be modified to kill extra space
	STA	,X+
	BRA	STRFLP0
WRIST0	LDX	NUMADR1	;load value within
	LDA	#32
	STA	,X
	STA	1,X
	STA	2,X
	STA	3,X
	LDA	#48
	STA	4,X
	CLRA
	STA	5,X
	RTS	;done 0 fix
; add two strings together 1 into 2
STRINGA	STA	NUMTMP	;store temp value and load back after done
; need to copy u to numst1
	LDY	#NUMST1
	LDB	#5
STRLP0	DECB
	LDA	B,U
	STA	B,Y
	CMPB	#0
	BHI	STRLP0
;	ok its copied so i cant fuckup the fixed string
	LDU	#NUMST1
	LDY	#NUMST2	;load string 2 index
	LDB	#5	;lets add the two strings together 5 chrs long and store answer in string 2
;start adding together
STRLP1	DECB	;main loop for math
	LDA	B,U	;set start x reference number to add to result
	ADDA	B,Y	;add u to y
	SUBA	#48	;subtract 48 ascii "0" to bring ascii back into number range
	STA	B,Y	;store answer
; //CHECK FOR CARRY
	CMPA	#58	;if the ascii=58 then the number is out of range.....this produces a carry
	BHS	STRS0
	BRA	STRS1
STRS0	SUBA	#10	;subtract 10 from the ascii code to bring it back in range
	STA	B,Y	;store remainder value
	SUBA	#48	;need to subtract 48 ascii to bring this number back in range before saving it
	CMPB	#0	;if its already 0....cant carry
	BEQ	STRS1
	DECB	;lets go to next place for the carry
	LDA	#1	;the carry
	ADDA	B,U	;add it
	STA	B,U	;store it
	INCB	;put the index back because it will decrease in the loop
STRS1	CMPB	#0
	BHI	STRLP1	
	LDA	NUMTMP
	RTS	;return from subroutine with a register restored
ENDFUNCTION
