;**************************************************************************
;*
;* draw text (lets move and change text with riot support)
;*
;*
;**************************************************************************

	opt h-f+			   ;raw ROM image

	;.define	DEBUG

	org $80					;zero page/stack

ramStart	.ds	0

zd0	.ds	1
zd1	.ds	1
zd2	.ds	1
zd3	.ds	1
zd4	.ds	1
zd5	.ds	1
zd6	.ds	1
zd7	.ds	1

frameCounter	.ds	2
fontColor	.ds	1
fontOffset	.ds	1
textPtr		.ds	2
lineOffset	.ds	1
lastState	.ds	1

PFAdr	.ds	2

WorkVBlankAdr	.ds	2
WorkViewAdr	.ds	2
WorkOverscanAdr	.ds	2

PFCache		.ds	(6*8)	;PF0..PF2 PF0n..PF2n (6 Byte)

?ramEnd	.ds	0

.if ?ramEnd > $f0
.error "RIOT RAM overflow"
.endif


	org $1000				;rom start
	
	icl "custom.i"
	
start
	;

	sei		;should no help on atari 2600
	cld		;binary
	ldx	#$ff	;stack to $ff
	txs
	
	;video off

	lda	#VBLANK_BLANK
	sta	VBLANK
	
	;safe init 
	
	lda	#0
	sta	VSYNC

	sta	REFP0
	sta	REFP1
		
	sta	NUSIZ0
	sta	NUSIZ1
		
	sta	RESMP0
	sta	RESMP1
		
	sta	AUDV0
	sta	AUDV1
		
	sta	VDELP0
	sta	VDELP1
	sta	VDELBL
		
	sta	ENAM0
	sta	ENAM1
	sta	ENABL
		
	sta	GRP0
	sta	GRP1
	sta	GRP0
		
	sta	HMCLR
	sta	CXCLR
	
	;clear RAM
	
	ldx	#127
	lda	#0
@	sta	ramStart,x
	dex
	bpl	@-
	
	;colors

	lda	#$0
	sta	COLUP0	;Player/Missile 0
	sta	COLUP1  ;Player/Missile 1
	sta	COLUBK	;Background

	lda	#HUE_BLUE
	sta	COLUPF 	;Playfield / Ball

	;config

	lda	#0	
	sta	CTRLPF
	
	lda	#0
	sta	PF0
	sta	PF1
	sta	PF2		;blank
	
	;
	
	;setup Frame Worker
	
	lda	#-1
	sta	lastState
	
	lda	#<WorkVBlank
	sta	WorkVBlankAdr
	lda	#>WorkVBlank
	sta	WorkVBlankAdr+1
	
	lda	#<WorkView
	sta	WorkViewAdr
	lda	#>WorkView
	sta	WorkViewAdr+1
	
	lda	#<WorkOverscan
	sta	WorkOverscanAdr
	lda	#>WorkOverscan
	sta	WorkOverscanAdr+1
	
	;
loop

	;**********************************************
	;* Vsync
	;**********************************************

	lda	#2
	sta	VBLANK
	sta	VSYNC

	.rept	PAL_VSYNC_LINES	;3
	sta	WSYNC
	.endr
	
	lda	#0
	sta	VSYNC
	
	inc	frameCounter
	
	;**********************************************
	;* VBlank PAL_VBLANK_LINES 
	;**********************************************
	
	.rept 3
	sta	WSYNC	;3
	.endr

	lda	#20
	sta	T1024T
	
	;**********************************************
	; 42 VBlank + 228 visible Scanlines
	;**********************************************
	
	;Sync to visible scanlines if necessary
	
	jsr	CallWorkVBlank
	
	lda	#0
	sta	VBLANK

	;**********************************************
	;* Work in Visible
	;**********************************************

	jsr	CallWorkView

	;**********************************************
	; Wait for Timer (1024)
	;**********************************************

@	bit	TIMINT	;MSB = 0, timer finished
	bpl	@-

	;**********************************************
	;* Overscan start
	;**********************************************

	lda	#42		;42 * 64 -> 36 Scanlines
	sta	TIM64T
	
	lda	#VBLANK_BLANK
	sta	VBLANK
	
	;**********************************************
	;* Work 2 (~36 Scanlines)
	;**********************************************

	jsr	CallWorkOverscan
	
	;**********************************************
	;* Wait overscan
	;**********************************************

@	bit	TIMINT
	bpl	@-
	
	sta	WSYNC
	
	;Frame finish

	jmp loop
	
CallWorkVBlank		jmp	(WorkVBlankAdr)		
CallWorkView		jmp	(WorkViewAdr)	
CallWorkOverscan	jmp	(WorkOverscanAdr)

;**********************************************
;*
;* WorkVBlank
;*
;**********************************************	

WorkVBlank

	ldx	#42		;Wait 42 Scanlines
@   	sta	WSYNC
	dex
	bne	@-
	
	rts

;**********************************************
;*
;* WorkView
;*
;**********************************************	
	
WorkView

	;pre gfx lines
	
	ldx	lineOffset
@	sta 	WSYNC	;sync
	dex
	bne	@-

	;draw font cache (timing is a monster)
			
	ldx	#0	;cache counter
	
?textDraw	
	
	ldy	#8
	
?scanSpace	
	
	sta	WSYNC
	lda	fontColor
	sta	COLUPF
	
	lda	PFCache,x
	sta	PF0
	lda	PFCache+1,x
	sta	PF1
	lda	PFCache+2,x
	sta	PF2

	inc	fontColor
	
	lda	PFCache+3,x
	sta	PF0
	lda	PFCache+4,x
	sta	PF1
	lda	PFCache+5,x
	sta	PF2
	
	dey	
	bne	?scanSpace
	
	txa
	clc
	adc	#6	;interleave cache for more speed
	tax
	
	cpx	#(5+1)*8
	bne	?textDraw
	
	;post gfx lines (clear)
	
	
	lda	#PAL_PICTURE_LINES-64
	sec
	sbc	lineOffset
	tax
	lda	#0	
@	sta 	WSYNC	;sync
	sta	PF0
	sta	PF1
	sta	PF2
	dex
	bne	@-

	rts
		
;**********************************************
;*
;* WorkOverscan 
;*
;**********************************************
	
WorkOverscan

	;increment frame counter (16 bit)
	
	inc	frameCounter
	bne	@+
	inc	frameCounter+1
@
	;color
	
	lda	frameCounter	
	lsr			;lost msb, maybe use 16 bit here
	;lsr	
	sta	fontColor
	
	;line offset
	
	lda	frameCounter
	lsr
	and	#$3f
	tax
	lda	sin64_b,x
	sta	lineOffset

	;check states

	lda	frameCounter+1	;upper 8 bit	  
	and	#$07		;8 texts
	cmp	lastState
	bne	@+
	rts
	
@	sta	lastState
	
	asl
	asl
	clc
	adc	lastState	;state * 5
	sta	zd0
	
	lda	#<texte
	clc
	adc	zd0
	sta	textPtr
	lda	#>texte
	adc	#0
	sta	textPtr+1
	
	jsr	calculateFontData

	rts

;**********************************************
;*
;* calculateFontData	
;*
;* wast: see setPF
;*
;**********************************************	

calculateFontData
	
	lda	#<PFCache
	sta	PFAdr
	lda	#>PFCache
	sta	PFAdr+1
	
	lda	#0
	sta	fontOffset
?lineLoop
	
	jsr	setPF		;calculate
	
	;
	
	lda	PFAdr
	clc
	adc	#6
	sta	PFAdr
	
	;
	
	inc	fontOffset
	lda	fontOffset
	cmp	#8
	bne	?lineLoop
	rts	
	
;**********************************************
;*
;* setPF
;*
;* fontOffset for y offset in font
;*
;* trash zd0-zd4, a,x,y
;*
;* write into PFAdr
;*
;**********************************************
	
setPF

	;PF0      PF1     PF2
	;3210---- 456789A HGFEDCB
	;reverse  normal  reverse
	
	.rept	5
	ldy	##
	lda	(textPtr),y
	clc
	adc	fontOffset
	tax
	lda	font,x	
	sta	zd0+#
	.endr
	
	;char 0
	
	lda	zd0	;$AAAA aaaa
	lsr
	lsr
	lsr
	lsr		;$0000 AAAA
	tax
	lda	swapNibbleHigh,x
	ldy	#0
	sta	(PFAdr),y	;$AAAA 0000 (reverse)
	
	;char 1 (+0)
	
	lda	zd0	;$AAAA aaaa
	asl
	asl
	asl
	asl
	sta	zd0	;$aaaa 0000
	lda	zd1	;$BBBB bbbb
	lsr
	lsr
	lsr
	lsr		;$0000 BBBB
	ora	zd0	;$aaaa BBBB
	ldy	#1
	sta	(PFAdr),y	
	
	;char 2 (+1)
	
	lda	zd1	;$BBBB bbbb
	and	#$f	;$0000 bbbb
	tax
	lda	swapNibbleLow,x
	sta	zd1	;$0000 bbbb (reverse)
	
	lda	zd2	;$CCCC cccc
	lsr
	lsr
	lsr
	lsr		;$0000 CCCC
	tax
	lda	swapNibbleHigh,x
			;$CCCC 0000 (reverse)
	ora	zd1	;$CCCC bbbb (reverse)
	ldy	#2
	sta	(PFAdr),y	
	
	;char 3 (+2)
	
	lda	zd2	;$CCCC cccc
	and	#$f	;$0000 cccc
	tax
	lda	swapNibbleHigh,x
	ldy	#3
	sta	(PFAdr),y	;$cccc 0000 (reverse)
	
	lda	zd3	;$DDDD DDDD
	ldy	#4
	sta	(PFAdr),y	
	
	;char 4
	
	lda	zd4	;$EEEE eeee
	lsr
	lsr
	lsr
	lsr		;$0000 EEEE
	tax
	lda	swapNibbleLow,x		
	sta	zd0		;$0000 EEEE  (reverse)
	
	lda	zd4 		;$EEEE eeee
	and	#$f		;$0000 eeee
	tax			
	lda	swapNibbleHigh,x ;$eeee 0000  (reverse)
	ora	zd0
	ldy	#5
	sta	(PFAdr),y	
		
	rts
	
swapNibbleLow	.byte	%0000 ;%0000
		.byte	%1000 ;%0001
		.byte	%0100 ;%0010
		.byte	%1100 ;%0011
		.byte	%0010 ;%0100
		.byte	%1010 ;%0101
		.byte	%0110 ;%0110
		.byte	%1110 ;%0111
		.byte	%0001 ;%1000
		.byte	%1001 ;%1001
		.byte	%0101 ;%1010
		.byte	%1101 ;%1011
		.byte	%0011 ;%1100
		.byte	%1011 ;%1101
		.byte	%0111 ;%1110
		.byte	%1111 ;%1111
		              
swapNibbleHigh	.byte	%00000000 ;%0000
		.byte	%10000000 ;%0001
		.byte	%01000000 ;%0010
		.byte	%11000000 ;%0011
		.byte	%00100000 ;%0100
		.byte	%10100000 ;%0101
		.byte	%01100000 ;%0110
		.byte	%11100000 ;%0111
		.byte	%00010000 ;%1000
		.byte	%10010000 ;%1001
		.byte	%01010000 ;%1010
		.byte	%11010000 ;%1011
		.byte	%00110000 ;%1100
		.byte	%10110000 ;%1101
		.byte	%01110000 ;%1110
		.byte	%11110000 ;%1111
		
	;8 pixel per char -> 5 char per scan

texte	.byte	['H'-65]*8,['A'-65]*8,['L'-65]*8,['L'-65]*8,['O'-65]*8 ;HALLO 0
	.byte	['G'-65]*8,['M'-65]*8,['O'-65]*8,['M'-65]*8,['O'-65]*8 ;GMOMO 1
	.byte	['W'-65]*8,['I'-65]*8,['L'-65]*8,['L'-65]*8,26*8       ;WILL  2
	.byte	['C'-65]*8,['H'-65]*8,['E'-65]*8,['C'-65]*8,['K'-65]*8 ;CHECK 3
	.byte	['O'-65]*8,['U'-65]*8,['T'-65]*8,26*8,26*8             ;OUT   4
	.byte	['A'-65]*8,['T'-65]*8,['A'-65]*8,['R'-65]*8,['I'-65]*8 ;ATARI 5
	.byte	['V'-65]*8,['C'-65]*8,['S'-65]*8,26*8,26*8             ;VCS   6
	.byte	['N'-65]*8,['O'-65]*8,['W'-65]*8,26*8,26*8             ;NOW   7

sin64_b
	    .byte  82, 90, 98,106,113,120,127,133
	    .byte 139,145,149,153,157,160,161,163
	    .byte 163,163,161,160,157,153,149,145
	    .byte 139,133,127,120,113,106, 98, 90
	    .byte  82, 74, 66, 58, 51, 44, 37, 31
	    .byte  25, 19, 15, 11,  7,  4,  3,  1
	    .byte   1,  1,  3,  4,  7, 11, 15, 19
	    .byte  25, 31, 37, 44, 51, 58, 66, 74

	;8 bytes per char
		
font
	.byte %00011000,%00111100,%01100110,%01100110,%01111110,%01100110,%01100110,%00000000 ; A	0 
	.byte %01111100,%01100110,%01100110,%01111100,%01100110,%01100110,%01111100,%00000000 ; B	1
	.byte %00111100,%01100110,%01100000,%01100000,%01100000,%01100110,%00111100,%00000000 ; C	2
	.byte %01111000,%01101100,%01100110,%01100110,%01100110,%01101100,%01111000,%00000000 ; D	3
	.byte %01111110,%01100000,%01100000,%01111100,%01100000,%01100000,%01111110,%00000000 ; E	4
	.byte %01111110,%01100000,%01100000,%01111100,%01100000,%01100000,%01100000,%00000000 ; F	5
	.byte %00111100,%01100110,%01100000,%01101110,%01100110,%01100110,%00111110,%00000000 ; G	6
	.byte %01100110,%01100110,%01100110,%01111110,%01100110,%01100110,%01100110,%00000000 ; H	7
	.byte %01111110,%00011000,%00011000,%00011000,%00011000,%00011000,%01111110,%00000000 ; I	8
	.byte %00011110,%00001100,%00001100,%00001100,%00001100,%01101100,%00111000,%00000000 ; J	9
	.byte %01100110,%01101100,%01111000,%01110000,%01111000,%01101100,%01100110,%00000000 ; K	10
	.byte %01100000,%01100000,%01100000,%01100000,%01100000,%01100000,%01111110,%00000000 ; L	11
	.byte %10000010,%11000110,%11101110,%11010110,%10000010,%10000010,%10000010,%00000000 ; M	12
	.byte %01100110,%01110110,%01111110,%01111110,%01101110,%01100110,%01100110,%00000000 ; N	13
	.byte %00111100,%01100110,%01100110,%01100110,%01100110,%01100110,%00111100,%00000000 ; O	14
	.byte %01111100,%01100110,%01100110,%01111100,%01100000,%01100000,%01100000,%00000000 ; P	15
	.byte %00111100,%01100110,%01100110,%01100110,%01101110,%00111100,%00001110,%00000000 ; Q	16
	.byte %01111100,%01100110,%01100110,%01111100,%01111000,%01101100,%01100110,%00000000 ; R	17
	.byte %00111100,%01100110,%01100000,%00111100,%00000110,%01100110,%00111100,%00000000 ; S	18
	.byte %11111110,%00010000,%00010000,%00010000,%00010000,%00010000,%00010000,%00000000 ; T	19
	.byte %01100110,%01100110,%01100110,%01100110,%01100110,%01100110,%00111100,%00000000 ; U	20
	.byte %10000010,%10000010,%10000010,%01000100,%01000100,%00101000,%00010000,%00000000 ; V	21
	.byte %10000010,%10000010,%10000010,%10010010,%10010010,%10101010,%01000100,%00000000 ; W	22
	.byte %01100110,%01100110,%00111100,%00011000,%00111100,%01100110,%01100110,%00000000 ; X	23
	.byte %10000010,%01000100,%00101000,%00010000,%00010000,%00010000,%00010000,%00000000 ; Y	24
	.byte %01111110,%00000110,%00001100,%00011000,%00110000,%01100000,%01111110,%00000000 ; Z	25
	.byte %00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000,%00000000 ; 	26
                                                                                                       
	org $1FFC			   ;vectors                                                    
                                                                                                       
	.word start			   ;reset                                                      
	.word start			   ;brk                                                        
                                                                                                                                                                                                      