;**************************************************************************
;*
;* text mode width test
;*
;**************************************************************************
	
	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	
	
	;os key remove
	
	lda	#<noKeyRead
        sta	VSERINL        
        lda	#>noKeyRead
        sta	VSERINH        
	
	;chbase
	
	lda	#$E0
	sta	CHBASE
	
	;color setup
	
	lda	#$0	
	sta	COLBK
	lda	#$a	
	sta	COLPF0
	lda	#$e	
	sta	COLPF1
	lda	#$8	
	sta	COLPF2
	lda	#$88				
	sta	COLPF3
				
	;disable player/missile dma
	
	lda	#$00
	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
	
	
;************************************************
;*
;* dummy
;*
;************************************************
	
noKeyRead	
	rts
	
;************************************************
;*
;* vertical blank interrupt (immidiate, x,y regs save bei os rom)
;*
;************************************************

vbi_irq

	;
	
	lda	#0
	sta	dliCounter
	
	;prolog (like XITVBL)
	
	pla					  
	tay
	pla					  
	tax
	pla					  
	rti
			
;************************************************
;*
;* display list interrupt
;*
;************************************************

dli_irq

	;prolog

        pha 
	txa	
	pha
	tya	
	pha
	
	ldx	dliCounter
	lda	dmaValues,x	
	sta	WSYNC
	sta	DMACTL

	inc	dliCounter
	
	;epilog
	
        pla					  
	tay
	pla					  
	tax
	pla
        rti   	;direct (no stack used)
	
	
dliCounter	.byte	0

dmaValues	.byte	DMACTL_DL|DMACTL_PF_NORMAL
		.byte	DMACTL_DL|DMACTL_PF_NARROW
		.byte	DMACTL_DL|DMACTL_PF_WIDE
		
; 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
	.byte $70		;8 blank lines	
	.byte $70		;8 blank lines
	.byte $c0  		;DLI 8 blank lines
	.byte $47,<charData,>charData   	;Mode 7 + LMS	 
	.byte $7   
	.byte $7
	.byte $7
	.byte $7
	.byte $80  		;DLI 1 blank lines
	.byte $7
	.byte $7
	.byte $7
	.byte $7
	.byte $80  		;DLI 1 blank lines
	.byte $7
	.byte $7
	.byte $7
	.byte $7
	.byte $41,<displayList,>displayList ; wait vblank, restart same display list on next frame	
	
	org	$4000
	 
	;note here its a 8x16 pixel mode
	
charData

	;20 chars per screen width (20*8 = 160Pixel)
	;12 char height (12*16 = 192 (/2))

	;        --------------------////////////////////

	.byte	"MODE 7              "	;color mux by map
	.byte	" normal 20 chars    "
	.byte	" width              "
	.byte   "01234567890123456789"
	.byte	">------------------<"
	.byte	" narrow 16 chars"
	.byte	" width          "
	.byte   "0123456789012345"
	.byte	">--------------<"
	.byte	" wide 24 chars          "	
	.byte	" width                  " ;not all visible (maybe 44)
	.byte   "012345678901234567890123"	
	.byte	">----------------------<"
	
