; 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
;
;
;**************************************************************************************
;* clsspritelayer8() 0-8 SEMI GRAPHICS DEFAULT MODE
;**************************************************************************************
FUNCTION	CLSSPRITELAYER	
REM	; CLSSPRITELAYER() CLEARS THE SPRITE LAYER MAP WITH BLACK
REM	; Does not push to stack and uses A,X,Y
CLSSPRITELAYER	LDX	#7680
	LDD	#$8080	;load the color
LP4	STD	,X++
	CMPX	#9728
	BLO	LP4
	RTS
ENDFUNCTION
;
; ***************************************************************************************************************
; *   drawsprite(xpos,ypos,xlength,ylength,address)
; ***************************************************************************************************************
FUNCTION	DRAWSPRITE	XPOS,YPOS,XLEN,YLEN,UU
REM	; drawsprite(xpos,ypos,xlength,ylength,address in address) 
REM	; Does not push to stack
DEF	XPOS	BYTE
DEF	YPOS	BYTE
DEF	XLEN	BYTE
DEF	YLEN	BYTE
DEF	UU	WORD
UU	FCB	0,0
XPOS	FCB	0
YPOS	FCB	0
XLEN	FCB	0
YLEN	FCB	0
YCNT	FCB	0
DRAWSPRITE	LDU	UU
	LDX	#7680
	LDA	YPOS
	LDB	#64	;jump 2 lines
	MUL
	LEAX	D,X
	LDB	XPOS
	ABX
; setup
; draw sprite
	LDA	YLEN
	STA	YCNT	
WRIY	DEC	YCNT
	LDB	XLEN
WRIX	DECB
	LDA	,U+
	STA	,X+
	CMPB	#0
	BHI	WRIX
	LDB	#32
	SUBB	XLEN
	LEAX	B,X	;adjust row
	LDB	YCNT
;	CMPB	#0	**SIMON** (the LDB sets the Z flag - so no need to cmp)
	BHI	WRIY
	RTS
ENDFUNCTION
;
;
;**************************************************************************************
;* printsg8(caps on,posx,posy,string) 
;**************************************************************************************
FUNCTION	PRINTSG8	CAP,CX,CY,Y
REM	;PRINTsg8(c,posx,posy,string)c=0 or 1 for starting in caps mode. cx=0-31 cy=0-15 for screen position.
REM	;Does not push to stack and uses A,B,X,Y. uses sg 8
DEF	CAP	BYTE	;start in caps mode or not?
DEF	CX	BYTE
DEF	CY	BYTE
DEF	Y	STRING	;populate y register with string at y
CAP	FCB	0	;caps flag
CX	FCB	0
CY	FCB	0
ROWY	FCB	0
PRINTSG8	LDA	CX	;check if out of bounds
	CMPA	#31
	BLS	LP0
	RTS	;off screen
LP0	LDA	CY
	CMPA	#15
	BLS	LP01
	RTS	;off screen
LP01	LDX	#5632
	LDA	CY
	LDB	#128
	MUL
	LEAX	D,X
	LDB	CX
	ABX
;
;
;should be there?
	LDB	CAP	;load b with caps flag
LP1	LDA	,Y+
;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	,X
	STA	32,X
	STA	64,X
	STA	96,X
	LEAX	1,X	;advance it
	LDA	CX
	INCA
	STA	CX
	CMPA	#31
	BHI	LP6	;new overflow exit
;
	LDA	,Y	;string done check
;	CMPA	#0	**SIMON** (the LDA sets the Z flag - so no need to cmp)
	BNE	LP1
LP6	RTS	;return
ENDFUNCTION
;
;
; **********************************************************************************************************************
; * circlesg8(sin_costable,x1,y1,dx,dy,angle,dist,color,fill,fillcol)
; * 
; **********************************************************************************************************************
FUNCTION	CIRCLESG8	X,PLOTX,PLOTY,RADX,RADY,ANGLE,DIST,SETCOL,FILL,FILLCOL
REM	; circlesg8([sintbl pointer],x1,y1,diamx,diamy,angle,dist,col,fill,fillcol) 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	SETCOL	BYTE
DEF	FILL	BYTE
DEF	FILLCOL	BYTE
FILL	FCB	0
FILLCOL	FCB	0
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	0	;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
X1	FCB	0
Y1	FCB	0
X2	FCB	0
Y2	FCB	0
XD	FCB	0
YD	FCB	0
WN	FCB	0
XC	FCB	0
YC	FCB	0
CNT2	FCB	0
SETX	FCB	0
SETY	FCB	0
STEPP	FCB	1
CIRCLESG8
*
;	LDD	,X
;	TFR	D,X
	LDX	,X	**SIMON** - ldx,x is valid and alot cheaper than tfr
*
	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
	LDA	FILL
	LBNE	LINE2SG8	;innerdot line
	JSR	DOTSG8	;outer dot
	LDA	CNT
	INCA
	STA	CNT
	CMPA	DIST
	BLO	LOOP
DONEE	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
;*************************************************************************************************************************
LINESG8
LINE2SG8	LDD	PLOTX	;deals with registers for load
	STD	X1
	LDD	CALCX
	STD	X2
	CLR	DRAW
	LDA	FILLCOL
;	CMPA	#0	**SIMON** - the LDA sets Z flag - cmp not needed
	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	LDD	#0	;**SIMON** xc/YC ARE BACK TO BACK - SO LDD/std is cheaper	
	STD	XC	
;	CLRA	
;	STA	XC	;x counter
;	STA	YC	;y counter
	LDA	WN
	STA	CNT2	;set counter
	INC	CNT2
;ok check what display
;
LINE22	LDD	X1	;main loop
	STA	SETX
;	LDA	Y1
	STB	SETY
;draw the solid
;mock up fill
	LDD	SETX	;save x,y
	CMPA	#63
	BLS	CKY8
	RTS	;dot is off the screen
CKY8	CMPB	#63
	BLS	DAWD4
	RTS	;dots off the screen
DAWD4	LDA	FILLCOL
	BEQ	DAWD8
	INC	DRAW
DAWD8	CLRA
	STA	ODD
	LDX	#5632	;screen start
	LDA	SETY
	LDB	#32
	MUL
	LEAX	D,X
	LDA	SETX
	BITA	#1
	BEQ	SSK1
	INC	ODD
SSK1	LSRA	;half x
	LEAX	A,X
;ok we are set
	LDY	#COLOR0
	LDA	FILLCOL
	LEAY	A,Y
	LDA	,Y	;color to set
	LDB	DRAW
	BNE	SETBITS8
	STA	,X
	BRA	OKWN
SETBITS8	ADDA	#15
	STA	,X
;end draw solid
OKWN	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	CNT2	;check the counter
	BNE	LINE22	;nope keep plotting the line
	RTS	;we are done
ENDFUNCTION
;
;
; *************************************************************************************************************************
; * setsg8(d,x,y,c)
; *************************************************************************************************************************
FUNCTION	SETSG8	X1,Y1,SETCOL
REM	; setsg8(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
COLOR0	FCB	128
	FCB	128
	FCB	144
	FCB	160
	FCB	176
	FCB	192
	FCB	208
	FCB	224
	FCB	240
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	0
DRAW	FCB	0
ORCHR	FCB	0
WCOL	FCB	0
COLTMP	FCB	0
ODD	FCB	0
SETSG8	LDD	#$0000	**SIMON** - LDD is cheaper by 1 cycle
;	CLRA
;	CLRB
	STD	XROW
	STD	BIT1
	STD	BIT3
	STD	DRAW
;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
;
; ***************************************************************************************************************
; *   line2sg8 (vect1,vect2,color)   sg mode
; ***************************************************************************************************************
FUNCTION	LINE2SG8	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	SETCOL	BYTE
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
LINE2SG8	LDD	,X	;deals with registers for load
	STD	X1
	LDD	,Y
	STD	X2
	CLR	DRAW
	LDA	SETCOL
;	CMPA	#0	** SIMON ** Z is set by the LDA - so don*T CMPA
	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
;
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	LDD	#$0000	**SIMON** - ldd is cheaper by 1 cycle
;	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
*
	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
;
;
; ***************************************************************************************************************
; *   linesg8(x1,y1,x2,y2,color)   sg mode
; ***************************************************************************************************************
FUNCTION	LINESG8	X1,Y1,X2,Y2,SETCOL
REM	; linesg8(x1,y1,x2,y2,col)  color 0-8
REM	; Does not push to stack
DEF	X1	BYTE
DEF	Y1	BYTE
DEF	X2	BYTE
DEF	Y2	BYTE
DEF	SETCOL	BYTE
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
LINESG8	CLR	DRAW
	LDA	SETCOL
;	CMPA	#0	**SIMON** - LDA SETS Z FLAG - so don*T CMPA
	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
;
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	LDD 	#$0000	**SIMON** - LDD CHEAPER BY 1 CYCLE
;	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
;
;
;**************************************************************************************
;* clssg8(color) 0-8 SEMI GRAPHICS DEFAULT MODE
;**************************************************************************************
FUNCTION	CLSSG8	CC
REM	; CLSsg8(n)  This is a number from 0 to 8.
REM	; Does not push to stack and uses A,X,Y
DEF	CC	BYTE
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
CLSSG8	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
;
; ***************************************************************************************************************
; *   syncscr8(direction,speed)
; ***************************************************************************************************************
FUNCTION	SYNCSCR8	DIRECTION,SPEED
REM	; syncscr8(direction,speed) display 8 only supported so far.  leaves top 8 lines alone
REM	; Does not push to stack
DEF	SPEED	WORD
DEF	DIRECTION	BYTE
DISPLAY	FCB	0
DIRECTION	FCB	0
SPEED	FCB	0,0
COLORD	FCB	0,0
DOTS	FCB	0
TIMER	FCB	0,0
SAVEU	FCB	0,0
SYNCSCR8	LDD	$112
	ADDD	SPEED
	STD	TIMER
	CMPD	#62000
	BLO	BUFF2
	CLRA
	CLRB
	STD	$112
	LDD	SPEED
	STD	TIMER
;lets copy top first
BUFF2	LDX	#5632
	LDU	#3584
BUFF3	LDD	,X++
	STD	,U++
	CMPU	#3840
	BLO	BUFF3
;top two copied
	LDA	DIRECTION
	CMPA	#0
	LBEQ	UP2
	CMPA	#1
	LBEQ	DOWN2
	CMPA	#2
	LBEQ	RIGHT2
	CMPA	#3
	LBEQ	LEFT2
	RTS
; left2
LEFT2	LDU	#7679
	STU	SAVEU
LVWAIT2	ORCC	#$50
LVWAIT22	LDA	$FF03
	BPL	LVWAIT22
	LDA	$FF02
	ANDCC	#$EF
LPLCB2	LDX	#5632	;normal start 5632
	LDY	#5631	;1 byte more 5631
	CLRB
LPLCC2	LDA	,-Y
	STA	,-X
	INCB
	CMPB	#31
	BLO	LPLCC2
	LDA	,U
	STA	,-X
	LEAY	-1,Y
	LEAU	-32,U
	CLRB
	CMPY	#3840	; old 3584
	BHS	LPLCC2	;possible?
	LDU	SAVEU
	LEAU	-1,U
	STU	SAVEU
	CMPU	#7648
	BHS	LVWAIT2
	RTS
; right2
RIGHT2	LDU	#5888
	STU	SAVEU
RVWAIT2	ORCC	#$50
RVWAIT22	LDA	$FF03
	BPL	RVWAIT22
	LDA	$FF02
	ANDCC	#$EF
RPLCB2	LDX	#3840	;3584normal start
	LDY	#3841	;3585 1 byte more
	CLRB
RPLCC2	LDA	,Y+
	STA	,X+
	INCB
	CMPB	#31
	BLO	RPLCC2
	LDA	,U+
	STA	,X+
	LEAY	1,Y
	LEAU	31,U
	CLRB
	CMPY	#5632	;old5632
	BLO	RPLCC2
	LDU	SAVEU
	LEAU	1,U
	STU	SAVEU
	CMPU	#5920
	BLO	RVWAIT2
	RTS
; up2
UP2	LDU	#7680
UVWAIT2	ORCC	#$50
UVWAIT22	LDA	$FF03
	BPL	UVWAIT22
	LDA	$FF02
	ANDCC	#$EF
UPLCB2	LDX	#5632	;normal end
	LDY	#5568	;drop 2 lines short
UPLCC2	LDD	,--Y
	STD	,--X
	CMPY	#3840	;3584
	BHI	UPLCC2
UPLCA2	LDD	,--U
	STD	,--X
	CMPX	#3840	; 3584
	BHI	UPLCA2
; UWAIT2	LDD	$112
;	CMPD	TIMER
;	BLO	UWAIT2
;	ADDD	SPEED
;	STD	TIMER
	CMPU	#5888	;5632
	BHI	UVWAIT2
	RTS
; dn
DOWN2	LDU	#5888	;5632
DVWAIT2	ORCC	#$50
DVWAIT22	LDA	$FF03
	BPL	DVWAIT22
	LDA	$FF02
	ANDCC	#$EF
DPLCB2	LDX	#3840	;3584 normal end
	LDY	#3904	;3648 1 line more
DPLCC2	LDD	,Y++
	STD	,X++
	CMPY	#5632
	BLO	DPLCC2
DPLCA2	LDD	,U++	;may mirror?
	STD	,X++
	CMPX	#5632
	BLO	DPLCA2
; DWAIT2	LDD	$112
;	CMPD	TIMER
;	BLO	DWAIT2
;	ADDD	SPEED
;	STD	TIMER
	CMPU	#7680
	BLO	DVWAIT2
	RTS
ENDFUNCTION
;
