
GetScreenPos:
		;Screen layout is split in 3 parts according to Y line
		;AABBBCCC - AA*$0028  BBB*$0080  CCC*$0400
		clc	
		lda #0
		sta z_E
		tya 				;--BBB---	;Multiply by $0080
		and #%00111000
		ror
		ror
		ror					
		ror					;Shift 1 bit left 
		ror z_E
		adc #$40			;Screen base
		sta z_D
		
		tya					;AA------		;multiply by $0028
		rol 				;Get 1st A from AA------ 
		bcc GetScreenPos_SecondThird
GetScreenPos_ThirdThird:
		lda z_E
		clc
		adc #$50			;3/3 = Add $0050 to address
		jmp GetScreenPos_ThirdDone
GetScreenPos_SecondThird:
		rol 				;Get 2nd A from AA------ 
		bcc GetScreenPos_FirstThird
		lda z_E
		clc
		adc #$28			;3/2 = Add $0028 to address
GetScreenPos_ThirdDone:	
		sta z_E
GetScreenPos_FirstThird:	;1/3 = Add nothing to addreess
		
		tya 				;-----CCC	;Multiply by 4
		and #%00000111
		rol
		rol
		adc z_D
		sta z_D
		
		txa				;Process X
		clc
		.ifdef ScrWid256	;Screen is 280 wide (7 pixels per byte = 40 bytres)
			adc #4		;if we want to simulate a 32 byte wide screen, add 4 to X
		.endif
		adc z_E			;Add X to calculated address
		sta z_E
	rts
	
GetNextLine:
		lda z_D
		clc
		adc #$04		;add $0400 to the line number 
		sta z_D			;  (only works within an  8 line block)
	rts
	
	

ScreenInit:
	lda #%11000000		;Bit 5=0 16 color 
	sta $C029
	
	LoadTriple z_DE,$E19E00	;Palette Dest
	LoadTriple z_HL,DefaultPalette	;$-RGB
	LoadTriple z_BC,16*2			
	jsr LDIR
	
	rts
	
	
	
DefaultPalette:
    .word $0520 ;0  -RGB
    .word $0000 ;1  -RGB
    .word $0555 ;2  -RGB
    .word $0AAA ;3  -RGB
    .word $0FFF ;4  -RGB
    .word $0286 ;5  -RGB
    .word $03D3 ;6  -RGB
    .word $0E33 ;7  -RGB
    .word $0E76 ;8  -RGB
    .word $0EA5 ;9  -RGB
    .word $0FF4 ;10  -RGB
    .word $0A2A ;11  -RGB
    .word $0F0F ;12  -RGB
    .word $003D ;13  -RGB
    .word $036B ;14  -RGB
    .word $00DF ;15  -RGB
