;**************************************************************************
;*
;* move some color bars
;*
;**************************************************************************

	;.define	DEBUG

	org $2000			   ;prg start
	
	icl "custom.i"
	
start	;

	sei				  ;disable irqs
	lda	#$00
	sta	NMIEN			  ;NMIEN all off 
	sta	DMACTL			  ;DMACTL all off

	;set display list (use shadow regs)
	
	lda	#<displayList
	sta	DLISTL	
	lda	#>displayList
	sta	DLISTH
	
	;set display list interrupt
	  
	lda	#<dli_irq
        sta	VDSLSTL           
        lda	#>dli_irq
        sta	VDSLSTH    
	
	;vertical blank interrupt 
	
	lda 	#>vbi_irq
	sta	VVBLKIH	
	lda 	#<vbi_irq
	sta	VVBLKIL	
	
	;chbase
	
	lda	#$e0	
	sta	CHBASE
	
	;color setup
	
	lda	#$0	;black
	sta	COLBK
	lda	#$34	;red		
	sta	COLPF0
	lda	#$b6	;green	
	sta	COLPF1
	lda	#$1a	;yellow		
	sta	COLPF2
	lda	#$70	;blue		
	sta	COLPF3
	
	
	;disable player & missile

	lda	#0
	sta	GRACTL
	
	;

	
	;setup displays
	   	
	lda	#DMACTL_DL|DMACTL_PF_NORMAL
		
		;enable display list dma 		
		;playfield normal display (40)
		
			
	sta	DMACTL	;DMACTL
	
	lda	#$0
	sta	HSCROL
	
	;enable dli
	
	lda	#NMIEN_VBI|NMIEN_DLI	;
	sta	NMIEN	;VBI + DLI enbable
	
	;
	
	cli		;irq enable

	
	
	;
loop
	;Wait VBlank

?WaitVC0
        lda	VCOUNT		
        beq	?WaitVC0   	
?WaitVCnot0
        lda	VCOUNT		
        bne	?WaitVCnot0   	                

	;
	
	jmp loop			;end
		
;************************************************
;*
;* vertical blank interrupt (immidiate, x,y regs save bei os rom)
;*
;************************************************

vbi_irq

	;
	
	lda	#0
	sta	dliCounter
	
	;
	
	inc	bar0Counter
	inc	bar1Counter
	inc	bar2Counter
	inc	bar3Counter
	inc	bar4Counter
	inc	bar5Counter
	inc	bar6Counter
	inc	bar7Counter
	inc	bar8Counter
	inc	bar9Counter
	inc	bar10Counter
	inc	bar11Counter
	inc	bar12Counter
	inc	bar13Counter
	inc	bar14Counter
	inc	bar15Counter	
	
	;
	
	lda	#$0
	sta	zd0
	ldx	bar0Counter
	jsr	drawBar	
	
	lda	#$10
	sta	zd0
	ldx	bar1Counter
	jsr	drawBar	
	
	lda	#$20
	sta	zd0
	ldx	bar2Counter
	jsr	drawBar	
	
	lda	#$30
	sta	zd0
	ldx	bar3Counter
	jsr	drawBar	
	
	lda	#$40
	sta	zd0
	ldx	bar4Counter
	jsr	drawBar	
	
	lda	#$50
	sta	zd0
	ldx	bar5Counter
	jsr	drawBar	
	
	lda	#$60
	sta	zd0
	ldx	bar6Counter
	jsr	drawBar	
	
	lda	#$70
	sta	zd0
	ldx	bar7Counter
	jsr	drawBar	
	
	lda	#$80
	sta	zd0
	ldx	bar8Counter
	jsr	drawBar	
	
	lda	#$90
	sta	zd0
	ldx	bar9Counter
	jsr	drawBar	
	
	lda	#$a0
	sta	zd0
	ldx	bar10Counter
	jsr	drawBar	
	
	lda	#$b0
	sta	zd0
	ldx	bar11Counter
	jsr	drawBar	
	
	lda	#$c0
	sta	zd0
	ldx	bar12Counter
	jsr	drawBar	
	
	lda	#$d0
	sta	zd0
	ldx	bar13Counter
	jsr	drawBar	
	
	lda	#$e0
	sta	zd0
	ldx	bar14Counter
	jsr	drawBar	
	
	lda	#$f0
	sta	zd0
	ldx	bar15Counter
	jsr	drawBar	
	
	
	;prolog (like XITVBL)
	
	pla					  
	tay
	pla					  
	tax
	pla					  
	rti
	
;************************************************
;*
;* drawBar
;*
;* zd0 = or offset of color
;* x   = y offset
;*
;************************************************	
	
drawBar
	
	lda	sinTable,x	
	add	#80+4			;optimize -> offset to table
	lsr
	tax
	ldy	#13			;optimize -> unroll
?cloop	
	lda	colorTable-1,y
	ora	zd0
	sta	colorList,x
	inx
	dey	
	bne	?cloop

	rts
	
.define	barYDiff	10
	
bar0Counter	.byte	barYDiff*0
bar1Counter	.byte	barYDiff*1
bar2Counter	.byte	barYDiff*2
bar3Counter	.byte	barYDiff*3
bar4Counter	.byte	barYDiff*4
bar5Counter	.byte	barYDiff*5
bar6Counter	.byte	barYDiff*6
bar7Counter	.byte	barYDiff*7	
bar8Counter	.byte	barYDiff*8	
bar9Counter	.byte	barYDiff*9	
bar10Counter	.byte	barYDiff*10	
bar11Counter	.byte	barYDiff*11	
bar12Counter	.byte	barYDiff*12
bar13Counter	.byte	barYDiff*13	
bar14Counter	.byte	barYDiff*14	
bar15Counter	.byte	barYDiff*15	

;256
sinTable	.byte	0,1,3,5,7,9,11,13,15,17,19,21,23,25,26,28,30,32,34,35,37,39,41,42,44,46,47,49,50,52,53,55,56,57,59,60,61,63,64,65,66,67,68,69,70,71,72,73,73,74,75,75,76,77,77,78,78,78,79,79,79,79,79,79,79,79,79,79,79,79,79,78,78,78,77,77,76,75,75,74,73,73,72,71,70,69,68,67,66,65,64,63,61,60,59,57,56,55,53,52,50,49,47,46,44,42,41,39,37,35,34,32,30,28,26,25,23,21,19,17,15,13,11,9,7,5,3,1,0,-2,-4,-5,-7,-9,-11,-13,-15,-17,-19,-21,-23,-25,-27,-28,-30,-32,-34,-36,-37,-39,-41,-42,-44,-46,-47,-49,-50,-52,-53,-55,-56,-58,-59,-60,-61,-63,-64,-65,-66,-67,-68,-69,-70,-71,-72,-73,-73,-74,-75,-76,-76,-77,-77,-78,-78,-78,-79,-79,-79,-79,-79,-79,-79,-79,-79,-79,-79,-79,-79,-78,-78,-78,-77,-77,-76,-75,-75,-74,-73,-73,-72,-71,-70,-69,-68,-67,-66,-65,-64,-62,-61,-60,-59,-57,-56,-55,-53,-52,-50,-49,-47,-45,-44,-42,-40,-39,-37,-35,-34,-32,-30,-28,-26,-24,-23,-21,-19,-17,-15,-13,-11,-9,-7,-5,-3,-1
	
colorTable	.byte	$1,$4,$6,$8,$a,$d,$f,$d,$a,$8,$6,$4,$1	
		
;************************************************
;*
;* display list interrupt
;*
;************************************************

dli_irq

	;prolog
       
	pha
	tya	
	pha
	
	;
	
	ldy	dliCounter
	lda	colorList,y
	sta	WSYNC
	sta	COLBK
	
	inc	dliCounter
		
	;epilog
	
        pla					  
	tay	
	pla
        rti   	;direct
        
	
dliCounter .byte	0	

colorList	
	.rept	96
	.byte	$0
	.endr

	
	
; display list

	;7	6	5	4	3	2	1	0
	;DLI	LMS	VSCROLL	HSCOLL	Mode
	;
	;Mode 0 Blank lines 7..4 is count+1 of blank lines
	;Mode 1 JMP (use with LMS to set destination)
	;Mode $2-$f is ANTIC mode
		
	 .align $400
	 
displayList	 
	.byte 	0			   ; Mode 0
	.rept	96
	.byte 	$90	; 2 blank lines (mode 0) + DLI
	.endr
	.byte $41,<displayList,>displayList ; wait vblank, restart same display list on next frame	
	
	
	
	org	$4000	 
	
	
