; color computer vga adapter revision 1.0
; vg6 mode tools library
;
; 
; ****************************************************************************************************
; * putvga1(page,x1,y1,transparency,address image)
; ****************************************************************************************************
FUNCTION	PUTVGA1	PAGE,X1,Y1,TRANS,IMAGE
REM	; putvga1(page,x1,y1,transparency,+image)
REM	; 128x96
DEF	PAGE	BYTE
DEF	X1	BYTE
DEF	Y1	BYTE
DEF	TRANS	BYTE
DEF	IMAGE	WORD
; put on the screen
SETX	FCB	0
XST	FCB	0
X1	FCB	0
Y1	FCB	0
X2	FCB	0
Y2	FCB	0
XCNT	FCB	0
TRANS	FCB	0
PAGE	FCB	0	;what screen
IMAGE	FCB	0,0	;start draw addr
TEST3	FCB	0
COCO3	FCB	0
COL	FCB	0
DOTST	FCB	0	;odd even
DOTMSK1	FCB	15,240,240,15
DOTCOL	FCB	0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,255	;full colors both dots
SCRNTBL	FDB	3584,9728
PUTVGA1	LDA	X1	;check if pixel is on screen
	LBMI	DONE	;nope exit 128-
	STA	XST
	LDA	Y1
	CMPA	#95
	LBHI	DONE	;nope exit
	LDA	TEST3	;did we test for cc3 yet
	LBEQ	DOTEST	;nope run test once
	LDA	COCO3	;test was done already is it a coco3?
	LBNE	ACTIVECC3	;yes
	BRA	IMBACK
DONE	RTS
; test
ACTIVECC3	LDA	PAGE
	BNE	SET4
SET3	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	LDA	#1
	STA	PAGE
	LBRA	IMBACK
SET4	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	LDA	#1
	STA	PAGE
	LBRA	IMBACK
; test for cc3 once and save result for next time
DOTEST	INC	TEST3
	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	BEQ	ACTIVECC4	;its coco 3 code
	CLR	COCO3
	LBRA	IMBACK
ACTIVECC4	INC	COCO3
	BRA	ACTIVECC3
; setup page
IMBACK	LDB	PAGE	;what active page?
	LDA	#2
	MUL
	LDX	#SCRNTBL	;coco 3 uses 9728 for both pages
	LEAX	D,X
	LDX	,X
	LDB	#64
	LDA	Y1
	MUL
	LEAX	D,X
; no x save
PUT1	LDY	#DOTMSK1
	LDU	IMAGE
	LDD	0,U
	STD	X2
	ADDB	Y1
	STB	Y2	;sizes saved
	LEAU	2,U
; lets process dots
LOOP0	LDA	X2
	STA	XCNT	;x counter
LOOP	LDA	,U+	;got first pixel
	BNE	NOTRANS
	LDB	TRANS
	BNE	DONE2
NOTRANS	STA	COL	;color to set
	CLR	DOTST
	LDB	X1
	BITB	#1
	BEQ	ODD1
	INC	DOTST
ODD1	LSRB
	STB	SETX
	LDA	B,X
	LDB	DOTST
	ANDA	B,Y	;initial mask
	LDB	SETX
	STA	B,X
; masked
	LDA	COL
	ADDA	#4
	LDA	A,Y	;loaded color
	LDB	DOTST
	ADDB	#2
	ANDA	B,Y	;mask
;	anda	col
	LDB	SETX
	ADDA	B,X
	STA	B,X
DONE2	INC	X1
	LDA	XCNT
	BEQ	NEXTY
	DEC	XCNT
	BRA	LOOP
NEXTY	INC	Y1
	LEAX	64,X
	LDA	XST
	STA	X1
	LDA	Y1
	CMPA	Y2
	BLS	LOOP0
	RTS
ENDFUNCTION
;
; 
; ****************************************************************************************************
; * putbytesvga(page,x1,y1,x2,y2,address image)
; ****************************************************************************************************
FUNCTION	PUTVGABYTES	PAGE,X1,Y1,X2,Y2,IMAGE
REM	; putvgabytes(page,x1,y1,sizex,sizey,+image)
REM	; 128x96
DEF	PAGE	BYTE
DEF	X1	BYTE
DEF	Y1	BYTE
DEF	X2	BYTE
DEF	Y2	BYTE
DEF	IMAGE	WORD
; put on the screen
X1	FCB	0
Y1	FCB	0
X2	FCB	0
Y2	FCB	0
PAGE	FCB	0	;what screen
IMAGE	FCB	0,0	;start draw addr
TEST3	FCB	0
COCO3	FCB	0
SCRNTBL	FDB	3584,9728
ADVX	FCB	0
PUTVGABYTES	LDA	X1	;check if pixel is on screen
	LBMI	DONE	;nope exit 128-
	LDA	Y1
	CMPA	#95
	LBHI	DONE	;nope exit
	LDA	TEST3	;did we test for cc3 yet
	LBEQ	DOTEST	;nope run test once
	LDA	COCO3	;test was done already is it a coco3?
	LBNE	ACTIVECC3	;yes
	BRA	IMBACK
DONE	RTS
; test
ACTIVECC3	LDA	PAGE
	BNE	SET4
SET3	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	LDA	#1
	STA	PAGE
	LBRA	IMBACK
SET4	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	LDA	#1
	STA	PAGE
	LBRA	IMBACK
; test for cc3 once and save result for next time
DOTEST	INC	TEST3
	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	BEQ	ACTIVECC4	;its coco 3 code
	CLR	COCO3
	LBRA	IMBACK
ACTIVECC4	INC	COCO3
	BRA	ACTIVECC3
; setup page
IMBACK	LDB	PAGE	;what active page?
	LDA	#2
	MUL
	LDX	#SCRNTBL	;coco 3 uses 9728 for both pages
	ABX
	LDX	,X	;loaded
	LDB	#64
	LDA	Y1	;where on y
	MUL
	LEAX	D,X
	LDB	X1
	LSRB	;half it
	ABX	;ready
	LDU	IMAGE
	CLRB
PUT1	LDA	,U+	;this will be control test
	STA	B,X
	INCB
	CMPB	X2
	BLO	PUT1
	LEAX	64,X	;next line
	CLRB
	DEC	Y2
	BNE	PUT1
	RTS
ENDFUNCTION
; 
; 
; ****************************************************************************************************
; * getvga1(page,x1,y1,x2,y2,transparency,address image)
; ****************************************************************************************************
FUNCTION	GETVGA1	PAGE,X1,Y1,X2,Y2,IMAGE
REM	; getvga1(page,x1,y1,x2,y2,+image)
REM	; 128x96 
DEF	PAGE	BYTE
DEF	X1	BYTE
DEF	Y1	BYTE
DEF	X2	BYTE
DEF	Y2	BYTE
DEF	IMAGE	WORD
; put on the screen
SETX	FCB	0
XST	FCB	0
X1	FCB	0
Y1	FCB	0
X2	FCB	0
Y2	FCB	0
XP1	FCB	0
YP1	FCB	0
CNTX	FCB	0
PAGE	FCB	0	;what screen
IMAGE	FCB	0,0	;start draw addr
TEST3	FCB	0
COCO3	FCB	0
COL	FCB	0
DOTST	FCB	0	;odd even
DOTMSK1	FCB	15,240,240,15
DOTCOL	FCB	0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,255	;full colors both dots
SCRNTBL	FDB	3584,9728
GETVGA1	LDA	TEST3	;did we test for cc3 yet
	LBEQ	DOTEST	;nope run test once
	LDA	COCO3	;test was done already is it a coco3?
	LBNE	ACTIVECC3	;yes
	BRA	IMBACK
DONE	RTS
; test
ACTIVECC3	LDA	PAGE
	BNE	SET4
SET3	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	INC	PAGE	;fool into page 1
	LBRA	IMBACK
SET4	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	LBRA	IMBACK
; test for cc3 once and save result for next time
DOTEST	INC	TEST3
	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	BEQ	ACTIVECC4	;its coco 3 code
	CLR	COCO3
	LBRA	IMBACK
ACTIVECC4	INC	COCO3
	BRA	ACTIVECC3
; setup page
IMBACK	LDU	IMAGE
	LDD	X2
	SUBA	X1
	STA	0,U
	STA	X2
	SUBB	Y1
	STB	1,U
	LEAU	2,U
; lets process dots
LOOP0	CLR	CNTX	;x counter
LOOP	LDA	X1
	ADDA	CNTX
	STA	XP1	;plus x
	LDA	Y1
	STA	YP1
	JSR	POINT	;draw dot
	STB	,U+
	INC	CNTX
	LDA	CNTX
	CMPA	X2
	BLS	LOOP	;may need adj
	INC	Y1
	LDA	Y1
	CMPA	Y2
	BHI	DONEA
	BRA	LOOP0
DONEA	RTS
POINT	LDB	PAGE	;what active page?
	LDA	#2
	MUL
	LDX	#SCRNTBL	;coco 3 uses 9728 for both pages
	LEAX	D,X
	LDX	,X
; x index is at start of screen
	LDA	YP1
	LDB	#64
	MUL
	LEAX	D,X	; advance y pos
	CLR	DOTST
	LDB	XP1
	BITB	#1
	BNE	ODD1
	INC	DOTST
ODD1	LSRB
	ABX
	LDB	,X
	LDA	DOTST
	BEQ	ODDS
; evens
	LSRB
	LSRB
	LSRB
	LSRB
	RTS
ODDS	ANDB	#15
	RTS
ENDFUNCTION
;
;
; *********************************************
; *   SETUP COCOVGA BOARD FOR VG6
; *  setupvga()
; *********************************************
FUNCTION	SETUPVGA	MODE,RGB
REM	; setupvga(screen,rgb)  screen=0 or 1. rgb=1 cmp=0
REM	; make sure your code starts at $4000 as this uses two screens of 6k
DEF	MODE	BYTE
DEF	RGB	BYTE
MODE	FCB	0
RGB	FCB	0
PALRGB	FCB	0,56,63,2,16,18,8,9,32,36,28,27,42,53,52,54
PALCMP	FCB	0,32,63,2,15,17,11,28,4,7,30,61,9,37,21,35
;                                b,g,w   ,gr          ,bl       ,red,cya   ,p,s,s,y
SETUPVGA	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	LBEQ	SETUPCC3
	LDA	$FF02
	LDA	$FF03
	ANDA	#252
	ORA	#1
	STA	$FF03
	LDA	$FF02
IVGA	LDA	$FF03
	BPL	IVGA
	LDA	$FF02
; **********************************************
; * fiddle 512 byte *regs*
; **********************************************
	LDA	#$94	;enhanced/ARTIFACT COLS/extras 148
	STA	$401
	LDA	#$00
	STA	$400
	STA	$402
	STA	$403
	STA	$406
	STA	$407
	LDA	#$0B	
	STA	$405	;extras - border/STATUS OVERRIDE
	LDA	#$01	;vg6
	STA	$408
; colors
	LDD	#$8000	;black
	STD	$484	;border color
	STD	$440	;palette 0  (cc3=0)
	LDD	#$C210	
	STD	$442	;pallete 1  (cc3=56)
	LDD	#$E318
	STD	$444	;pallete 2  (cc3=63)
	LDD	#$8100
	STD	$446	;pallete 3  (cc3=2)
	LDD	#$8200
	STD	$448	;pallete 4  (cc3=16)
	LDD	#$8300
	STD	$44A	;pallete 5  (cc3=18)
	LDD	#$8010
	STD	$44C	;pallete 6  (cc3=8)
	LDD	#$8018
	STD	$44E	;pallete 7  (cc3=9)
	LDD	#$C000
	STD	$450	;pallete 8  (cc3=32)
	LDD	#$E000
	STD	$452	;pallete 9  (cc3=36)
	LDD	#$A210
	STD	$454	;pallete 10  (cc3=28)
	LDD	#$8318
	STD	$456	;pallete 11  (cc3=27)
	LDD	#$C110
	STD	$458	;pallete 12  (cc3=42)
	LDD	#$E208
	STD	$45A	;pallete 13  (cc3=skin)
	LDD	#$E200
	STD	$45C	;pallete 14  (cc3=52)
	LDD	#$E300
	STD	$45E	;pallete 15  (cc3=54)
; *
; **********************************************
; *
	LDA   	#$90  	SET COMBO LOCK 1
	STA  	$FF22  	WRITE TO PIA
	LDA   	#$48  	SET COMBO LOCK 2
	STA  	$FF22  	WRITE TO PIA
	LDA   	#$A0  	SET COMBO LOCK 3
	STA  	$FF22  	WRITE TO PIA
	LDA   	#$F8  	SET COMBO LOCK 4
	STA  	$FF22  	WRITE TO PIA
	LDA	#0
	STA	$FF22
WVS2	LDA	$FF03
	BPL	WVS2
	LDA	$FF02
	LDA	#$E0
	STA	$FF22	
	STA	$FFC3
	STA	$FFC5	;cg6
	LDA	MODE
	BNE	SET2
; setup sam at e00 edit $2600view
SET1	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
	RTS
SET2	CLRA	;set 9728 view zone   lets try 19 =9728
	LDX	#65478
	STA	1,X
	STA	3,X
	STA	4,X
	STA	6,X
	STA	9,X
	STA	10,X
	STA	12,X
	RTS
; hopefully drawing at 3584 will show on screen
SETUPCC3	LDA	RGB
	BNE	SETRGB
	LDX	#PALCMP
	BRA	COLORSET
SETRGB	LDX	#PALRGB
; **********************************************
; * CC3 / MMU / SCS / 1616
; **********************************************
COLORSET	LDD	#$4C00
	STD	$FF90
; set palette
SETPAL	LDA	$FF03
	BPL	SETPAL
	LDA	$FF02
	LDU	#$FFB0
PALLP	LDD	,X++
	STD	,U++
	CMPU	#$FFC0
	BLO	PALLP
; **********************************************
; * 128 x 96 x 16 videomode + gime setup
; **********************************************
	LDD	#$8212	;$82 > $80 = gfx mode $02 = 2LPR
	STD	$FF98	;$12 > 64 bytes, 192 lines, 16 colors 
; **********************************************
; * screen offset at $60000
; *
; * physical address / 8192 = MMU PAGE
; *
; * mmu page * 1024 = offset address
; **********************************************
	LDD	#$C0C0	;$60000/$2000=$30 > $30*$400=$C000  used c0c0
	STD	$FF9D
; **********************************************
; * 8k of mmu bank(s) at $2000 
; **********************************************
	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	LDD	#0	;clear out 2x 6k of screens
	LDX	#$2600
CLS	STD	,X++
	CMPX	#$3E00
	BLO	CLS
; blank page 0 also
	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	LDD	#0	;clear out 2x 6k of screens
	LDX	#$2600
CLS2	STD	,X++
	CMPX	#$3E00
	BLO	CLS2
	RTS
ENDFUNCTION
; 
; 
; ************************************************************************************************************************
; * syncvga(page)
; ************************************************************************************************************************
FUNCTION	SYNCVGA	PAGE
REM	; syncvga(page) 0-1 page  vga page0=e00 page0=3584 page1=9728-3e00
REM	; cc3 page0=9728, page 1=9728  membank swap
DEF	PAGE	BYTE
PAGE	FCB	0
SYNCVGA	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	BEQ	ACTIVECC3	;its coco 3 code
; vga it is
; setup sam at e00 edit $2600view
	LDA	PAGE
	BEQ	SET1
	BRA	SET2
SET1	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
	RTS
SET2	CLRA	;set value so its at 3584 edit 9728 view zone   lets try 19 =9728
	LDX	#65478
	STA	1,X
	STA	3,X
	STA	4,X
	STA	6,X
	STA	9,X
	STA	10,X
	STA	12,X
	RTS
; coco 3 it is
ACTIVECC3	LDA	PAGE
	BEQ	SET3
	BRA	SET4
SET3	LDD	#$C0C0	;$60600
	STD	$FF9D
	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	RTS
SET4	LDD	#$C4C0	;$62600
	STD	$FF9D
	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	RTS
ENDFUNCTION
; 
; *
; ************************************************************************************************************************
; * clsvga(page)
; ************************************************************************************************************************
FUNCTION	CLSVGA	PAGE,COL
REM	; clsvga(page,color) page=0-1 color=0-15
REM	; vga screen clear
DEF	COL	BYTE
DEF	PAGE	BYTE
COL	FCB	0,0
PAGE	FCB	0
CLSVGA	LDA	COL
	CMPA	#16
	BHS	EXIT1
	BRA	NOEX
EXIT1	RTS
NOEX	LDA	COL
	LSLA
	LSLA
	LSLA
	LSLA
	ADDA	COL
	STA	COL
	LDB	COL
	STD	COL
; vga or cc3?
	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	LBEQ	ACTIVECC3	;its coco 3 code
; vga code
	LDU	#$2600
	LDA	PAGE
	BEQ	LOOP2
SET2	LDU	#$3E00
LOOP2	LDX	COL
	LDY	COL
	LDB	#96
LOOP1	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	PSHU	X,Y
	DECB
	BNE	LOOP1
	RTS
ACTIVECC3	LDA	PAGE
	BNE	SET3
SET4	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	BRA	SET2
SET3	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	BRA	SET2
ENDFUNCTION
;
;
; *************************************************************************************************************************
; * psetvga(page,xp1,yp1,col)
; *************************************************************************************************************************
FUNCTION	PSETVGA	PAGE,XP1,YP1,COL
REM	;psetvga(page,x,y,color)  page=0-1 x=0-127 y=0-95 color=0-15
REM	; 0-black 1-grey 2-white 3,5-green 6,7-blue 8,9-red 10,11-cyan 12-purp 13,14-peach 15-yellow
DEF	PAGE	BYTE
DEF	XP1	BYTE
DEF	YP1	BYTE
DEF	COL	BYTE
PAGE	FCB	0
XP1	FCB	0
YP1	FCB	0
COL	FCB	0
DOTST	FCB	0
COCO3	FCB	0
TEST3	FCB	0
SCRNTBL	FDB	3584,9728
DOTCOL	FCB	0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,255	;full colors both dots
DOTMSK1	FCB	15,240,240,15
PSETVGA	LDA	XP1	;check if pixel is on screen
	BMI	DONE	;nope exit 128-
	LDA	YP1
	CMPA	#95
	BHI	DONE	;nope exit
	LDY	#DOTCOL	;dot color has both pixels filled to be masked
	LDU	#DOTMSK1	;mask sets
	LDA	TEST3	;did we test for cc3 yet
	BEQ	DOTEST	;nope run test once
	LDA	COCO3	;test was done already is it a coco3?
	BNE	ACTIVECC3	;yes
IMBACK	LDB	PAGE	;what active page?
	LSLB	;*2
	LDX	#SCRNTBL	;coco 3 uses 9728 for both pages
	ABX
	LDX	,X
; x=start of screen
	LDA	YP1
	LDB	#64	;2 pixel per byte
	MUL
	LEAX	D,X	; advance y pos
	LDB	XP1
	CLR	DOTST	;even number
	BITB	#1	;mask odd dot
	BEQ	ODD1
	INC	DOTST
ODD1	LSRB
	ABX	;ok we are ready to draw
; mask the screen
	LDB	DOTST
	LDA	,X	;the 2 dots
	ANDA	B,U
	STA	,X	;saved
; get new dot
	LDA	COL
	LDA	A,Y
	ADDB	#2
	ANDA	B,U
	ADDA	,X
	STA	,X
	RTS	;done
ACTIVECC3	LDA	PAGE
	BNE	SET4
SET3	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	INC	PAGE	;fool into page 1
	BRA	IMBACK
SET4	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	BRA	IMBACK
DONE	RTS
; test for cc3 once and save result for next time
DOTEST	INC	TEST3
	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	BEQ	ACTIVECC4	;its coco 3 code
	CLR	COCO3
	BRA	IMBACK
ACTIVECC4	INC	COCO3
	BRA	ACTIVECC3
ENDFUNCTION
;
; 
; *************************************************************************************************************************
; * pointvga(page,xp1,yp1)
; *************************************************************************************************************************
FUNCTION	POINTVGA	PAGE,XP1,YP1
REM	;pointvga(page,x,y)  page=0-1 x=0-127 y=0-95  returns color to b register
REM	; 0-black 1-grey 2-white 3,5-green 6,7-blue 8,9-red 10,11-cyan 12-purp 13,14-peach 15-yellow
DEF	PAGE	BYTE
DEF	XP1	BYTE
DEF	YP1	BYTE
PAGE	FCB	0
XP1	FCB	0
YP1	FCB	0
COL	FCB	0
DOTST	FCB	0
COCO3	FCB	0
TEST3	FCB	0
SCRNTBL	FDB	3584,9728
DOTCOL	FCB	0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,255	;full colors both dots
DOTMSK1	FCB	240,15
POINTVGA	LDA	XP1	;check if pixel is on screen
	BMI	DONE	;nope exit 128-
	LDA	YP1
	CMPA	#95
	BHI	DONE	;nope exit
	LDY	#DOTCOL	;dot color has both pixels filled to be masked
	LDU	#DOTMSK1	;mask sets
	LDA	TEST3	;did we test for cc3 yet
	BEQ	DOTEST	;nope run test once
	LDA	COCO3	;test was done already is it a coco3?
	BNE	ACTIVECC3	;yes
IMBACK	LDB	PAGE	;what active page?
	LSLB	;*2
	LDX	#SCRNTBL	;coco 3 uses 9728 for both pages
	ABX
	LDX	,X
; x=start of screen
	LDA	YP1
	LDB	#64	;2 pixel per byte
	MUL
	LEAX	D,X	; advance y pos
	LDB	XP1
	CLR	DOTST	;even number
	BITB	#1	;mask odd dot
	BEQ	ODD1
	INC	DOTST
ODD1	LSRB
	ABX	;ok we are ready to draw
; mask the screen
	LDA	DOTST
	BEQ	GET1
	LDB	,X	;the 2 dots
	ANDB	A,U
	RTS	;done
GET1	LDB	,X	;the 2 dots
	ANDB	A,U
	LSRB
	LSRB
	LSRB
	LSRB
	RTS	;done
ACTIVECC3	LDA	PAGE
	BNE	SET4
SET3	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	INC	PAGE	;fool into page 1
	BRA	IMBACK
SET4	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	BRA	IMBACK
DONE	RTS
; test for cc3 once and save result for next time
DOTEST	INC	TEST3
	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	BEQ	ACTIVECC4	;its coco 3 code
	CLR	COCO3
	BRA	IMBACK
ACTIVECC4	INC	COCO3
	BRA	ACTIVECC3
ENDFUNCTION
;
;
; ***************************************************************************************************************
; *   linevga(x1,y1,x2,y2,color)   sg mode
; ***************************************************************************************************************
FUNCTION	LINEVGA	PAGE,X1,Y1,X2,Y2,COL
REM	; linevga(page,x1,y1,x2,y2,col)  color 0-15
REM	; 
DEF	PAGE	BYTE
DEF	X1	BYTE
DEF	Y1	BYTE
DEF	X2	BYTE
DEF	Y2	BYTE
DEF	COL	BYTE
PAGE	FCB	0
COL	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
TEST3	FCB	0
COCO3	FCB	0
SAVEX	FCB	0,0
DOTST	FCB	0
SCRNTBL	FDB	3584,9728
DOTCOL	FCB	0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,255	;full colors both dots
DOTMSK1	FCB	15,240,240,15
; **************************************************************************
LINEVGA	LDA	TEST3
	BNE	DONETST
	INC	TEST3
	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	BEQ	ACTIVECC4	;its coco 3 code
	CLR	COCO3
DONETST	LDA	COCO3
	BEQ	START
	BRA	SETCC3
ACTIVECC4	INC	COCO3
SETCC3	LDA	PAGE
	BNE	SET4
SET3	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	INC	PAGE	;fool into page 1
	BRA	START
SET4	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
; ***************************************************************************
START	LDY	#DOTCOL
	LDU	#DOTMSK1
	LDB	PAGE	;what active page?
	LSLB	;*2
	LDX	#SCRNTBL	;coco 3 uses 9728 for both pages
	ABX
	LDX	,X
	STX	SAVEX
; saved page
	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
; main loop
LINE22	LDA	X1	;check if pixel is on screen
	BMI	DONE	;nope exit 128-
	LDA	Y1
	CMPA	#95
	BHI	DONE	;nope exit
	LDX	SAVEX
	LDA	Y1	;yp1
	LDB	#64	;2 pixel per byte
	MUL
	LEAX	D,X	; advance y pos
	LDB	X1	;xp1
	CLR	DOTST	;even number
	BITB	#1	;mask odd dot
	BEQ	ODD1
	INC	DOTST
ODD1	LSRB
	ABX	;ok we are ready to draw
; mask the screen
	LDB	DOTST
	LDA	,X	;the 2 dots
	ANDA	B,U
	STA	,X	;saved
; get new dot
	LDA	COL
	LDA	A,Y
	ADDB	#2
	ANDA	B,U
	ADDA	,X
	STA	,X
DONE	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
	LBNE	LINE22	;nope keep plotting the line
	RTS	;we are done
ENDFUNCTION
;
;
; ***************************************************************************************************************
; *   linevga1(page,x1y1,x2y2,x3y3,x4y4,image)   sg mode
; ***************************************************************************************************************
FUNCTION	LINEVGA1	PAGE,XP1,XP2,XP3,XP4,IMAGE
REM	; linevga1(page,x1y1,x2y2,x3y3,x4y4,image)  color 0-15
REM	; experimental
DEF	PAGE	BYTE
DEF	XP1	WORD
DEF	XP2	WORD
DEF	XP3	WORD
DEF	XP4	WORD
DEF	IMAGE	WORD
; new addition
IMAGE	FCB	0,0
XP1	FCB	0
YP1	FCB	0
XP2	FCB	0
YP2	FCB	0
XP3	FCB	0
YP3	FCB	0
XP4	FCB	0
YP4	FCB	0
; more adds
XLD1	FCB	0
XDIR1	FCB	0	;test for quicker
YLD1	FCB	0
YDIR1	FCB	0
XCNT1	FCB	0
YCNT1	FCB	0
WN1	FCB	0
WN2	FCB	0
; more adds
XLD2	FCB	0
YLD2	FCB	0
XCNT2	FCB	0
YCNT2	FCB	0
XXDIR1	FCB	0
XXDIR2	FCB	0
YYDIR1	FCB	0
YYDIR2	FCB	0
; wb counters
WBC1	FCB	0
WBC2	FCB	0
; extra
WB	FCB	0	;biggest of the lines =wn1 or wn2
; old
PAGE	FCB	0
COL	FCB	0
COLA	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
BBC	FCB	0	;big boy counter
TEST3	FCB	0
COCO3	FCB	0
DOTST	FCB	0
; new additions for test
COLDST1	FCB	16	;distance
COLDST2	FCB	16	;distance
COLCNT1	FCB	0	;counter for color swap
COLCNT2	FCB	0	;counter for color swap
CWN1	FCB	0	;whole fraction
CWN2	FCB	0	;whole fraction
COLC	FCB	0
;SAVEX	FCB	0,0
; end test
SCRNTBL	FDB	3584,9728
YTABLE1	FDB	3584,3648,3712,3776,3840,3904,3968,4032,4096,4160,4224,4288,4352,4416,4480,4544,4608,4672
	FDB	4736,4800,4864,4928,4992,5056,5120,5184,5248,5312,5376,5440,5504,5568,5632,5696,5760,5824
	FDB	5888,5952,6016,6080,6144,6208,6272,6336,6400,6464,6528,6592,6656,6720,6784,6848,6912,6976
	FDB	7040,7104,7168,7232,7296,7360,7424,7488,7552,7616,7680,7744,7808,7872,7936,8000,8064,8128
	FDB	8192,8256,8320,8384,8448,8512,8576,8640,8704,8768,8832,8896,8960,9024,9088,9152,9216,9280
	FDB	9344,9408,9472,9536,9600,9664
YTABLE2	FDB	9728,9792,9856,9920,9984,10048,10112,10176,10240,10304,10368,10432,10496,10560,10624,10688
	FDB	10752,10816,10880,10944,11008,11072,11136,11200,11264,11328,11392,11456,11520,11584,11648
	FDB	11712,11776,11840,11904,11968,12032,12096,12160,12224,12288,12352,12416,12480,12544,12608
	FDB	12672,12736,12800,12864,12928,12992,13056,13120,13184,13248,13312,13376,13440,13504,13568
	FDB	13632,13696,13760,13824,13888,13952,14016,14080,14144,14208,14272,14336,14400,14464,14528
	FDB	14592,14656,14720,14784,14848,14912,14976,15040,15104,15168,15232,15296,15360,15424,15488
	FDB	15552,15616,15680,15744,15808
; 
DOTCOL	FCB	0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,255,0	;full colors both dots
DOTMSK1	FCB	15,240,240,15
; **************************************************************************
LINEVGA1	LDA	TEST3
	BNE	DONETST
	INC	TEST3
	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	BEQ	ACTIVECC4	;its coco 3 code
	CLR	COCO3
DONETST	LDA	COCO3
	BEQ	START
	BRA	SETCC3
ACTIVECC4	INC	COCO3
SETCC3	LDA	PAGE
	BNE	SET4
SET3	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	INC	PAGE	;fool into page 1
	BRA	START
SET4	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
; ***************************************************************************
START	LDY	#DOTCOL	;dot color has both pixels filled to be masked
	LDU	IMAGE
	LDD	,U++
	INCB
	INCA
	STD	COLDST1
; u is ready with texture
	LDB	PAGE	;what active page?
	BEQ	SETPAGE1
	LDX	#YTABLE2
	BRA	SKIPPAGE
SETPAGE1	LDX	#YTABLE1
; new setup code for two line tracking
SKIPPAGE	STX	SAVEX+1
	LDD	#$0000
	STD	XXDIR1
	STD	YYDIR1
	LDA	XP1
	CMPA	XP3
	BLS	LONS1
	SUBA	XP3
	STA	XLD1
	BRA	LONS2
LONS1	LDA	XP3
	SUBA	XP1
	DEC	XXDIR1
	STA	XLD1
LONS2	LDA	YP1
	CMPA	YP3
	BLS	LONS3
	SUBA	YP3
	STA	YLD1
	BRA	LONS4
LONS3	LDA	YP3
	SUBA	YP1
	DEC	YYDIR1
	STA	YLD1
; which is greater?
LONS4	LDA	YLD1
	CMPA	XLD1
	BLO	LONS5	;y is lower then goto lins5
	STA	WN1	;x is lower so save y as whole fraction
	BRA	LANS0	;done
LONS5	LDA	XLD1	;nope y is lower so save x as whole fraction
	STA	WN1	;stored it
; second line track
LANS0	LDA	XP2
	CMPA	XP4
	BLS	LANS1
	SUBA	XP4
	STA	XLD2
	BRA	LANS2
LANS1	LDA	XP4
	SUBA	XP2
	DEC	XXDIR2
	STA	XLD2
LANS2	LDA	YP2
	CMPA	YP4
	BLS	LANS3
	SUBA	YP4
	STA	YLD2
	BRA	LANS4
LANS3	LDA	YP4
	SUBA	YP2
	DEC	YYDIR2
	STA	YLD2
; which is greater?
LANS4	LDA	YLD2
	CMPA	XLD2
	BLO	LANS5	;y is lower then goto lins5
	STA	WN2	;x is lower so save y as whole fraction
	BRA	LBNS0	;done
LANS5	LDA	XLD2	;nope y is lower so save x as whole fraction
	STA	WN2	;stored it
; which line is bigger
LBNS0	LDA	WN1
	CMPA	WN2
	BLO	LBNS1	;y is lower then goto lins5
	STA	WB	;big num fraction
	STA	BBC
	BRA	LBNSDN	;done
LBNS1	LDA	WN2	;nope y is lower so save x as whole fraction
	STA	WB	;stored it
	STA	BBC
; ok lets get this line going clear the start
LBNSDN	CLR	COLC
	CLRA
	STA	XCNT1	;x counter
	STA	YCNT1	;y counter
	CLRA
	CLR	WBC1	;line counter
	CLR	WBC2
	STA	XCNT2	;x counter
	STA	YCNT2	;y counter
; new track
	LDA	WB
	STA	CWN2	;color
	CLR	COLCNT2	;half boost start
MAINLINE	LDD	XP1
	STD	X1
	LDD	XP2
	STD	X2
; saved page
; ****************************************************************************************************** base loop
	CLR	XDIR1	;test
	CLR	YDIR1
	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
	DEC	XDIR1	;test
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
	DEC	YDIR1	;test
; 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	CLR	XC	;x counter
	CLR	YC	;y counter
	LDA	WN
	STA	CNT	;set counter
	INC	CNT
; setup color test y axis
	LDU	IMAGE
	LEAU	2,U
	LDA	COLC
	LDB	COLDST2
	MUL
	LEAU	D,U	;ready for line
; setup x axis color
	LDA	WN
	STA	CWN1	;colors is longer
	CLR	COLCNT1	;half boost start
; main loop of original line
; no this is dot
LINE22	LDA	,U
	BEQ	DONE2
	LDA	X1	;check if pixel is on screen
	BMI	DONE2	;nope exit 128-
	LDB	Y1
	CMPB	#95
	BHI	DONE2	;nope exit
; different code 
******************************************************************************
SAVEX	LDX	#$0000
	LSLB
	ABX
	LDX	,X
; 
;	LDA	Y1	;yp1
;	LDB	#64	;2 pixel per byte
;	MUL
;	LEAX	D,X	; advance y pos
	LDB	X1	;xp1
ODD1	LSRB
	ABX	;ok we are ready to draw
; get new dot
	LDB	,U
	LDA	B,Y	;hit both pixels
	STA	,X
; check color advancement
; DONE	
DONE2	LDA	COLCNT1
	ADDA	COLDST1
	CMPA	CWN1
	BLS	CARRYON
; continue loop
CARRYM	SUBA	CWN1
	LEAU	1,U
	CMPA	CWN1
	BHI	CARRYM
CARRYON	STA	COLCNT1	;save it
; color check done
; end test
	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	XDIR1
	BPL	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	YDIR1
	BPL	ADY2
	INC	Y1	;increase y plot or dec?
	BRA	CHKX2
ADY2	DEC	Y1
; done?
CHKX2	DEC	CNT	;check the counter
	LBNE	LINE22	;nope keep plotting the line
; 
; end of original line
; 
; use to be done....   now we need to advance other two lines
; 
; line 1
	LDA	WBC1	;line 1 counter
	ADDA	WN1	;add distance to xc counter
	CMPA	WB	;big boy number
	BHS	WADV2	;it has on x.... lets advance it*****************************************************
	STA	WBC1	;nope keep counting	
; line 2
WCHK2	LDA	WBC2	;line 2 counter
	ADDA	WN2	;add distance to xc counter
	CMPA	WB	;big boy number
	BHS	WADV3	;it has on x.... lets advance it *****************************************************
	STA	WBC2	;nope keep counting 
; end lets loop
; check color advancement
LASTCNT	LDA	COLCNT2
	ADDA	COLDST2
	CMPA	CWN2
	BLS	CARRYON2
; continue loop
CARRYM2	SUBA	CWN2
	INC	COLC	;the advancer
	CMPA	CWN2
	BHI	CARRYM2
CARRYON2	STA	COLCNT2	;save it
	DEC	BBC	;big boy counter
	LDA	BBC
	LBNE	MAINLINE
	RTS
; correct
WADV2	SUBA	WB	;advance the x counter....subtract the whole fraction
	STA	WBC1	;store remainder to play with next loop
; move  down line1 here
	LDA	XCNT1
	ADDA	XLD1	;add distance to xc counter
	CMPA	WN1	;has it gone over or same as our whole fraction?
	BHS	AADV2	;it has on x.... lets advance it
	STA	XCNT1	;nope keep counting 
	BRA	ACHK2	;lets check y now
AADV2	SUBA	WN1	;advance the x counter....subtract the whole fraction
	STA	XCNT1	;store remainder to play with next loop
	LDA	XXDIR1
	BPL	AADVX2
	INC	XP1	;increase y plot or dec?
	BRA	ACHK2
AADVX2	DEC	XP1
ACHK2	LDA	YCNT1	;now the y axis.... loader up
	ADDA	YLD1	;add its distance
	CMPA	WN1	;has it gone over or the same as our whole fraction
	BHS	AADVY2	;it has on y.... lets advance it
	STA	YCNT1	;nope... keep counting
;	LBRA	WCHK2	;done lets look at other line
	JMP	WCHK2	;done lets look at other line
; advance it
AADVY2	SUBA	WN1	;subtract the whole number from y counter
	STA	YCNT1	;store the remainder for next loop
	LDA	YYDIR1	;YP1
	BPL	AADY2
	INC	YP1	;increase y plot or dec?
;	LBRA	WCHK2
	JMP	WCHK2
AADY2	DEC	YP1
;	LBRA	WCHK2
	JMP	WCHK2
; done that line
WADV3	SUBA	WB	;advance the x counter....subtract the whole fraction
	STA	WBC2	;store remainder to play with next loop
; move  down line1 here
	LDA	XCNT2
	ADDA	XLD2	;add distance to xc counter
	CMPA	WN2	;has it gone over or same as our whole fraction?
	BHS	BADV2	;it has on x.... lets advance it
	STA	XCNT2	;nope keep counting 
	BRA	BCHK2	;lets check y now
BADV2	SUBA	WN2	;advance the x counter....subtract the whole fraction
	STA	XCNT2	;store remainder to play with next loop
	LDA	XXDIR2	;XP2
;	CMPA	XP4
	BPL	BADVX2
	INC	XP2	;increase y plot or dec?
	BRA	BCHK2
BADVX2	DEC	XP2
BCHK2	LDA	YCNT2	;now the y axis.... loader up
	ADDA	YLD2	;add its distance
	CMPA	WN2	;has it gone over or the same as our whole fraction
	BHS	BADVY2	;it has on y.... lets advance it
	STA	YCNT2	;nope... keep counting
;	LBRA	LASTCNT
	JMP	LASTCNT
; advance it
BADVY2	SUBA	WN2	;subtract the whole number from y counter
	STA	YCNT2	;store the remainder for next loop
	LDA	YYDIR2	;YP2
	BPL	BADY2
	INC	YP2	;increase y plot or dec?
;	LBRA	LASTCNT
	JMP	LASTCNT
BADY2	DEC	YP2
;	LBRA	LASTCNT
	JMP	LASTCNT
ENDFUNCTION
;
;
; **********************************************************************************************************************
; * plot(sin_costable,x1,y1,dx,dy,angle)
; * 
; **********************************************************************************************************************
FUNCTION	PLOT	SINTB1,PLOTX,PLOTY,RADX,RADY,ANGLE
REM	; plot([sintbl pointer],x1,y1,diamx,diamy,angle) max rad 34 circle angle 0-255 as percentage of 360
REM	; Does not push to stack uses X for sine table
DEF	SINTB1	WORD
DEF	ANGLE	BYTE
DEF	PLOTX	BYTE
DEF	PLOTY	BYTE
DEF	RADX	BYTE
DEF	RADY	BYTE
ANGLE	FCB	0
RADX	FCB	0	;radius
RADY	FCB	0
PLOTX	FCB	0
PLOTY	FCB	0
SINTB1	FCB	0,0
COSTB1	FCB	0,0
X1	FCB	0
Y1	FCB	0
PLOT	LDX	SINTB1
	LDB	ANGLE	;rotation
	ADDB	#64
	ABX
	STX	COSTB1
	LDX	SINTB1
	LDB	ANGLE
	ABX
	STX	SINTB1
; fix plot
	LDA	ANGLE
	CMPA	#128
	BHI	NSUBX1
	INC	PLOTX
NSUBX1	CMPA	#64
	BLO	NSUBY1
	CMPA	#192
	BHI	NSUBY1
	DEC	PLOTY
NSUBY1	INC	PLOTY
	LDX	SINTB1
	LDB	,X
;	SUBB	#1
	LDA	RADX
	MUL
	ASLB
	ROLA
	SUBA	RADX
	ADDA	PLOTX
	STA	X1
; cos
	LDX	COSTB1
	LDB	,X
;	SUBB	#1	;adjustment factor here
	LDA	RADY
	MUL
	ASLB
	ROLA
	SUBA	RADY
	ADDA	PLOTY
	STA	Y1
	LDD	X1
	RTS
ENDFUNCTION
;
;
;
; ****************************************************************************************************
; * printvga(page,x,y,text,font,transparency,color)
; ****************************************************************************************************
FUNCTION	PRINTVGA	PAGE,X1,Y1,ASCTXT,TRANS,COLOR
REM	; printvga(page,x1,y1,asctxt,transparency,color)
REM	;
DEF	PAGE	BYTE
DEF	X1	BYTE
DEF	Y1	BYTE
DEF	ASCTXT	WORD
DEF	TRANS	BYTE
DEF	COLOR	BYTE
TRANS	FCB	0
ASCTXT	FCB	0,0
COLOR	FCB	9
X1	FCB	0
Y1	FCB	0
PAGE	FCB	0
COCO3	FCB	0
TEST3	FCB	0
SCREEN	FDB	3584,9728
SCREENSTR	FCB	0,0
XP1	FCB	0
YP1	FCB	0
COL	FCB	0
DOTST	FCB	0
CNTX	FCB	0
CNTY	FCB	0
SAVEY	FCB	0,0
SAVEU	FCB	0,0
DOTCOL	FCB	0,17,34,51,68,85,102,119,136,153,170,187,204,221,238,255	;full colors both dots
DOTMSK1	FCB	15,240,240,15
; ascii 32-128
ASCII	FCB	0,0,0,0,0,0	; 32  
	FCB	4,4,4,0,4,0	; 33 !
	FCB	10,10,0,0,0,0	; 34 "
	FCB	10,31,10,31,10,0	; 35 #
	FCB	10,14,6,12,14,10	; 36 $
	FCB	0,18,8,4,18,0	; 37 %
	FCB	12,18,12,10,28,0	; 38 &
	FCB	4,4,0,0,0,0	; 39 *
	FCB	8,4,4,4,4,8	; 40 (
	FCB	4,8,8,8,8,4	; 41 )
	FCB	0,10,4,10,0,0	; 42 *
	FCB	0,4,14,4,0,0	; 43 +
	FCB	0,0,0,0,12,0	; 44 ,
	FCB	0,0,14,0,0,0	; 45 -
	FCB	0,0,0,0,12,6	; 46 .
	FCB	32,16,8,4,2,1	; 47 /
	FCB	31,17,17,17,31,0	; 48 0
	FCB	4,6,4,4,31,0	; 49 1
	FCB	31,16,31,1,31,0	; 50 2
	FCB	31,16,31,16,31,0	; 51 3
	FCB	9,9,31,8,8,0	; 52 4
	FCB	31,1,31,16,31,0	; 53 5
	FCB	1,1,31,17,31,0	; 54 6
	FCB	31,16,16,16,16,0	; 55 7
	FCB	31,17,31,17,31,0	; 56 8
	FCB	31,17,31,16,16,0	; 57 9
	FCB	12,0,0,0,12,0	; 58 :
	FCB	12,0,0,0,12,6	; 59 ;
	FCB	8,4,2,2,4,8	; 60 <
	FCB	0,30,0,0,30,0	; 61 =
	FCB	4,8,16,16,8,4	; 62 >
	FCB	12,18,24,12,0,12	; 63 ?
	FCB	12,18,29,13,2,28	; 64 @
	FCB	14,17,31,17,17,0	; 65 A
	FCB	15,17,15,17,31,0	; 66 B
	FCB	14,17,1,17,14,0	; 67 C
	FCB	15,17,17,17,15,0	; 68 D
	FCB	31,1,15,1,31,0	; 69 E
	FCB	31,1,7,1,1,0	; 70 F
	FCB	31,1,25,17,31,0	; 71 G
	FCB	17,17,31,17,17,0	; 72 H
	FCB	31,4,4,4,31,0	; 73 I
	FCB	31,8,8,9,6,0	; 74 J
	FCB	17,9,7,9,17,0	; 75 K
	FCB	1,1,1,1,31,0	; 76 L
	FCB	17,27,21,21,17,0	; 77 M
	FCB	17,19,21,25,17,0	; 78 N
	FCB	14,17,17,17,14,0	; 79 O
	FCB	15,17,15,1,1,0	; 80 P
	FCB	14,17,21,25,14,0	; 81 Q
	FCB	15,17,15,17,17,0	; 82 R
	FCB	30,1,14,16,15,0	; 83 S
	FCB	31,4,4,4,4,0	; 84 T
	FCB	17,17,17,17,14,0	; 85 U
	FCB	17,10,10,4,4,0	; 86 V
	FCB	17,21,21,10,10,0	; 87 W
	FCB	17,10,4,10,17,0	; 88 X
	FCB	17,10,4,4,4,0	; 89 Y
	FCB	31,16,12,3,31,0	; 90 Z
	FCB	12,4,4,4,4,12	; 91 [
	FCB	1,2,4,8,16,32	; 92 \
	FCB	12,8,8,8,8,12	; 93 ]
	FCB	4,10,17,0,0,0	; 94 ^
	FCB	0,0,0,0,31,0	; 95 _
	FCB	6,12,0,0,0,0	; 96 `
	FCB	0,0,30,25,30,0	; 97 a
	FCB	1,1,15,17,15,0	; 98 b
	FCB	0,0,30,1,30,0	; 99 c
	FCB	16,16,30,17,30,0	; 100 d
	FCB	0,14,31,3,30,0	; 101 e
	FCB	12,18,7,2,2,0	; 102 f
	FCB	0,14,31,28,14,0	; 103 g
	FCB	1,1,15,17,17,0	; 104 h
	FCB	4,0,4,4,4,0	; 105 i
	FCB	16,0,16,17,14,0	; 106 j
	FCB	1,9,7,9,17,0	; 107 k
	FCB	4,4,4,4,4,0	; 108 l
	FCB	0,10,31,21,17,0	; 109 m
	FCB	0,22,25,17,17,0	; 110 n
	FCB	0,14,17,17,14,0	; 111 o
	FCB	0,14,17,15,1,0	; 112 p
	FCB	0,14,17,30,16,0	; 113 q
	FCB	0,14,17,1,1,0	; 114 r
	FCB	0,30,3,28,15,0	; 115 s
	FCB	0,8,28,8,8,0	; 116 t
	FCB	0,0,17,17,14,0	; 117 u
	FCB	0,0,17,10,4,0	; 118 v
	FCB	0,0,21,31,10,0	; 119 w
	FCB	0,0,17,14,17,0	; 120 x
	FCB	0,17,10,4,4,0	; 121 y
	FCB	0,31,12,6,31,0	; 122 z
	FCB	8,4,6,6,4,8	; 123 {
	FCB	12,12,12,12,12,12	; 124 |
	FCB	4,8,24,24,8,4	; 125 }
	FCB	0,38,25,0,0,0	; 126 ~
	FCB	30,18,18,18,18,30	; 127 
	FCB	6,11,2,2,11,6	; 128 ?
MASKDOT	FCB	1,2,4,8,16,32
PRINTVGA	LDA	TEST3
	BEQ	DOTEST
	LDA	COCO3
	BNE	ACTIVECC3
	BRA	PRINT
ACTIVECC3	LDA	PAGE
	BNE	SET4
SET3	LDA	#$30	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	INC	PAGE	;fool into page 1
	BRA	PRINT
SET4	LDA	#$31	;setup MMU window (addressable screen)
	STA	$FFA1	;$2000- $
	BRA	PRINT
; test for cc3 once and save result for next time
DOTEST	INC	TEST3
	LDD	$FFFE	;check if coco3
	ADDD	$FFFF
	CMPA	#167
	BEQ	ACTIVECC4	;its coco 3 code
	CLR	COCO3
	LBRA	PRINT
ACTIVECC4	INC	COCO3
	BRA	ACTIVECC3
; setup page
PRINT	LDX	#SCREEN
	LDB	#2
	LDA	PAGE	;what buffer
	MUL
	LEAX	B,X	;screen ready on x
	LDX	,X
	STX	SCREENSTR	;save screen start
	LDY	ASCTXT	;the text to print
	STY	SAVEY
; advance here when in function
	CLR	CNTX
	CLR	CNTY
LOOP0	LDU	#ASCII
	LDY	SAVEY
	LDA	,Y+
	STY	SAVEY
	LBEQ	WEDONE
	CMPA	#32
	LBLO	WEDONE
	SUBA	#32
	LDB	#6	; 6 bytes per char
	MUL
	LEAU	D,U	;u ready with graphic
	STU	SAVEU
LOOP1	LDU	SAVEU
	LDB	CNTX
	ADDB	X1
	STB	XP1
	LDB	CNTY
	ADDB	Y1
	STB	YP1
	LDB	CNTX
	LDY	#MASKDOT
	LDA	,U	;dot colors all 6
	ANDA	B,Y	;
	BEQ	DARKDOT
	LDA	COLOR
	STA	COL
	BRA	DRAWDOTS
; transparancy here
DARKDOT	CLR	COL
	LDA	TRANS
	BEQ	DRAWDOTS
	BRA	SKIPDOT
DRAWDOTS	LDX	SCREENSTR
	LDY	#DOTCOL	;dot color has both pixels filled to be masked
	LDU	#DOTMSK1	;mask sets
; x=start of screen
	LDA	YP1
	LDB	#64	;2 pixel per byte
	MUL
	LEAX	D,X	; advance y pos
	LDB	XP1
	CLR	DOTST	;even number
	BITB	#1	;mask odd dot
	BEQ	ODD1
	INC	DOTST
ODD1	LSRB
	ABX	;ok we are ready to draw
; mask the screen
	LDB	DOTST
	LDA	,X	;the 2 dots
	ANDA	B,U
	STA	,X	;saved
; get new dot
	LDA	COL
	LDA	A,Y
	ADDB	#2
	ANDA	B,U
	ADDA	,X
	STA	,X
; ok last
SKIPDOT	INC	CNTX
	LDA	CNTX
	CMPA	#6
	LBLO	LOOP1
	CLR	CNTX
	LDU	SAVEU
	LEAU	1,U
	STU	SAVEU	;next line
	INC	CNTY
	LDA	CNTY
	CMPA	#6
	LBLO	LOOP1
	LDA	X1
	ADDA	#6
	STA	X1
	CLR	CNTY
	CLR	CNTX
	LBRA	LOOP0
WEDONE	RTS
ENDFUNCTION
;
;
; ***************************************************************************************************************
; *   lineangle(x1,y1,x2,y2,color)
; ***************************************************************************************************************
FUNCTION	LINEANGLE	X1,Y1,X2,Y2
REM	; lineangle(x1,y1,x2,y2) 
REM	; returns rough angle of a line on register b
DEF	X1	BYTE
DEF	Y1	BYTE
DEF	X2	BYTE
DEF	Y2	BYTE
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
CNT	FCB	0
	FCB	160,154,147,139,128,117,109,102,096	;0
	FCB	166,160,152,141,128,115,104,096,090	;1
	FCB	173,168,000,000,000,000,000,088,083	;2
	FCB	182,179,000,000,000,000,000,077,074	;3
JOYTAN	FCB	192,192,000,000,000,000,000,064,064	;4
	FCB	202,205,000,000,000,000,000,051,054	;5
	FCB	211,216,000,000,000,000,000,040,045	;6
	FCB	218,224,232,243,255,013,024,032,038	;7
	FCB	224,230,237,246,255,010,019,026,032	;8
; **************************************************************************
LINEANGLE	LDY	#JOYTAN
	LEAY	4,Y	;center
	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
; fix to 4 max
	CMPA	#4
	BLS	LINEGG
	LDA	#4	;max plot is 4 dots on whole fraction
LINEGG	STA	CNT	;set counter
; counters start
LINE22	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?
	LEAY	1,Y
	BRA	CHK2
ADVX2	DEC	X1
	LEAY	-1,Y
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
	LEAY	9,Y	;downline
	INC	Y1	;increase y plot or dec?
	BRA	CHKX2
ADY2	DEC	Y1
	LEAY	-9,Y
;done?
CHKX2	DEC	CNT	;check the counter
	LBNE	LINE22	;nope keep plotting the line
	LDB	0,Y
	RTS	;we are done
ENDFUNCTION
;
