
PrintChar:
	sec
	sbc #32
	sta z_As
	phx
	phy
	PushTriple z_HL
	PushTriple z_DE
	PushTriple z_BC
		lda z_As 
		
		sta z_E
		stz z_D
		stz z_DEU
		
		ldx #3
		jsr DoXShifts		;Get Char
		
		LoadTriple z_HL,BitmapFont
		jsr AddHL_DE
		
		lda CursorX
		sta z_E
		stz z_D
		stz z_DEU
		ldx #3
		jsr DoXShifts		;Get Xpos

		ldy CursorY
	CharYshiftAgain:	
		Cpy #0
		beq CharYshiftDone
		
		AddTriple z_DE,$000280*8	;Ypos
		dey
		bra CharYshiftAgain
		
	CharYshiftDone:	
		
		AddTriple z_DE,$B00000		;Screen Base
		
		
		lda #8
		sta z_C
	NextCLine:	
		PushTriple z_DE
			lda #8
			sta z_B
			lda [z_HL]
			sta z_As
			jsr IncHLU
	NextCPixel:
			lda #15
			rol z_As
			bcs DrawCPixel
			lda #1
	DrawCPixel:	
			sta [z_DE]
			jsr IncDEU
			dec z_B
			bne NextCPixel
		PullTriple z_DE
		AddTriple z_DE,$000280
		
		dec z_C
		bne NextCLine
		inc CursorX
		
	
	PullTriple z_BC
	PullTriple z_DE
	PullTriple z_HL
	ply
	plx
	
	rts	
	
	
ScreenInit:
;	org $000000
	;	assume M:1		;Accumulator 8 bit
	;	assume X:1		;XY 8 bit
		
		;lda #255
		;sta $AF4000
		;sta $AF4100
		;sta $AF4200
	
		lda #$00			;Background Color
		sta $AF000D
		lda #$29
		sta $AF000E
		lda #$53
		sta $AF000F
		
		
		;lda #192
		;sta $AF2000
		;sta $AF2001
		;sta $AF2002
		;sta $AF2003
		
		
		lda #%00001100	;Graphics and Bitmap on
		sta $af0000
		
		
		lda #1			;Bitmap ON
		sta $af0140
		
		lda #0
		sta $AF0141 	;Screen Addr Offset L
		sta $AF0142 	;Screen Addr Offset H
		sta $AF0143 	;Screen Addr Offset U
		
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;			Define screen size
		lda #<640
		sta $af0144		;WidL
		;sta $AF040C		;Stride L
		;sta $AF040E		;Stride L
		lda #>640
		sta $af0145		;WidH
		;sta $AF040D		;Stride H
		;sta $AF040F		;Stride H
		lda #<480
		sta $af0146		;HeiL
		lda #>480
		sta $af0147		;HeiH
		
		
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;			Define Palette
		
		
		;Border Color
		;lda #128
		;sta $AF0005	;B
		;lda #0
		;sta $AF0006	;G
		;sta $AF0007	;R
		
		;Other Colors
		
		LoadTriple z_DE,$AF2000	;Palette Dest
		LoadTriple z_HL,Palette	;RRGGBB
		LoadTriple z_BC,16*4
			
		jsr LDIR
		
		
	rts
Cls:

		;lda #$00
		;sta z_L
		;lda #$20
		;sta z_H
		;lda #$B0
		;sta z_HX
		LoadTriple z_HL,$B00000	
		LoadTriple z_BC,$04b000
		LoadTriple z_BC,640*480
		
		clc			; switch to native
		ldx #255
FillAgain:
		txa
		lda #0
		sta [z_HL]
		inx 
		jsr IncHLU
		jsr DecBCU
		
		; phx 
			; ldy #255
			; ldx #255
; Waits:
			; dey
			; bne Waits
			; dex
			; bne Waits
		; plx
		
		
		lda z_B
		ora z_C
		ora z_BCU
		bne FillAgain
		
	rts
	

DoXShifts:
	asl z_E
	rol z_D
	rol z_DEU
	dex 
	bne DoXShifts
	rts
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SetPalette	;A=Palette entry RGB = z_BCU z_B z_C
		stz z_H
		asl
		rol z_H
		asl
		rol z_H
		sta z_L
		
		lda #$20
		clc
		adc z_H
		sta z_H
		
		lda #$AF	;$AF2000 Base
		sta z_HLU
		
		lda #255		;A
		sta [z_HL],y
		iny 
		ldy #0
		lda z_C			;B
		sta [z_HL],y
		iny 
		lda z_B			;G
		sta [z_HL],y
		iny 
		lda z_BCU		;R
		sta [z_HL],y

GetScreenPos:
	LoadTriple z_DE,$B00000	
	LoadTriple z_BC,$000280
	tya
	cmp #0
	beq GetScreenPosDone
	
GetScreenPosAgain:			;XY=Xpos,Ypos
	jsr AddDE_BC			;Add Y	
	dey
	bne GetScreenPosAgain
GetScreenPosDone:	
	LoadTriple z_BC,0
	
	txa
	sta z_C
	jsr AddDE_BC			;Add X
	
	rts
	

NewLine:
	stz CursorX
	inc CursorY		
	rts
	
Palette:	
    .byte $00,$29,$53,$FF ;0  BGRA
    .byte $00,$00,$00,$FF ;1  BGRA
    .byte $5C,$5C,$5C,$FF ;2  BGRA
    .byte $A0,$A0,$A0,$FF ;3  BGRA
    .byte $FF,$FF,$FF,$FF ;4  BGRA
    .byte $60,$87,$2C,$FF ;5  BGRA
    .byte $37,$D0,$33,$FF ;6  BGRA
    .byte $36,$32,$E2,$FF ;7  BGRA
    .byte $67,$77,$E2,$FF ;8  BGRA
    .byte $56,$A8,$E4,$FF ;9  BGRA
    .byte $4F,$FF,$FF,$FF ;10  BGRA
    .byte $A5,$2E,$A2,$FF ;11  BGRA
    .byte $FF,$00,$FF,$FF ;12  BGRA
    .byte $D3,$32,$03,$FF ;13  BGRA
    .byte $B4,$66,$3D,$FF ;14  BGRA
    .byte $FE,$D1,$01,$FF ;15  BGRA
    .byte $FF,$00,$FF,$FF ;16  BGRA