;**************************************************************************
;*
;* random (pokey) to pixel 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        
		
	;color setup
	
	lda	#$0	
	sta	COLBK
	lda	#$4	
	sta	COLPF0
	lda	#$2	
	sta	COLPF1
	lda	#$6	
	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
	
	lda	#$20	;1.79 MHz
	STA	AUDCTL  
	
	;
	
	cli		;irq enable

	

loop
	
	;Wait VBlank

;?WaitVC0
;        lda	VCOUNT		
;        beq	?WaitVC0   	
;?WaitVCnot0
;        lda	VCOUNT		
;        bne	?WaitVCnot0 

	;
	lda	bigCounter
	clc
	adc	#1
	sta	bigCounter
	lda	bigCounter+1
	adc	#0
	sta	bigCounter+1
	lda	bigCounter+2
	adc	#0
	sta	bigCounter+2
	
	lsr		;lsb -> c
	lsr		;lsb -> c
	lsr		;lsb -> c
	lsr		;lsb -> c
	lda	#0
	adc	#0
	;
	
	;lda	#1	;set pixel
	sta	zd0
	;ldx	#10
	;ldy	#10
	
	ldy	RANDOM
	ldx	mod160Table,y
	
	ldy	RANDOM
	lda	mod96Table,y
	tay

	lda	#<charData
	sta	za0
	lda	#>charData
	sta	za0+1
	jsr	setPixel
	
	jmp loop			;end
	
bigCounter	.byte	0x00,0x00,0x08
	
mod160Table
	.rept	160
	.byte	#
	.endr
	.rept	96
	.byte	#
	.endr	
	
mod96Table
	.rept	96
	.byte	#
	.endr
	.rept	96
	.byte	#
	.endr
	.rept	96
	.byte	#
	.endr
	.rept	32
	.word	#
	.endr
	
;************************************************
;*
;* setPixel
;* 
;* x   = xpos (0..159)
;* y   = ypos (0..95)
;* zd0 = 0 is clear pixel otherwise set pixel
;* za0 = dest (gfx)
;*
;* waste x,y,za0
;*
;************************************************

setPixel
	
	tya			;a = y
	asl			;* 2
	tay
	lda	?pixelYTable,y	;get y offset low
	clc
	adc	za0
	sta	za0
	iny
	lda	?pixelYTable,y	;get y offset high
	adc	za0+1
	sta	za0+1		;16 bit add to dest address
	
	txa
	and	#$7		;&7	
	tay			;store (y = x)
	
	txa
	lsr
	lsr
	lsr			;x / 8
	
	clc
	adc	za0
	sta	za0
	lda	za0+1
	adc	#0
	sta	za0+1		;add to dest address

	;tay
	ldx	#0	
	lda	zd0
	beq	?clearPixel
	
	;set pixel
	
	lda	(za0,x)	
	ora	?pixelMask,y
	sta	(za0,x)	
	rts
	
?clearPixel	
	;clear pixel
	
	lda	(za0,x)	
	and	?pixelNotMask,y
	sta	(za0,x)	
	rts

?pixelMask

    .byte %10000000
    .byte %01000000
    .byte %00100000
    .byte %00010000
    .byte %00001000
    .byte %00000100
    .byte %00000010
    .byte %00000001
    
?pixelNotMask

    .byte %01111111
    .byte %10111111
    .byte %11011111
    .byte %11101111
    .byte %11110111
    .byte %11111011
    .byte %11111101
    .byte %11111110    
    
?pixelYTable 	.rept	96
		.word	#*20
		.endr
			


;************************************************
;*
;* 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
		
	;epilog
	
        pla					  
	tay
	pla					  
	tax
	pla
        rti   	;direct (no stack used)
	
	
dliCounter	.byte	0

; 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 $70  		;DLI 8 blank lines
	.byte $4b,<charData,>charData   	;Mode b + LMS
	.rept 95
	.byte $b
	.endr
	.byte $41,<displayList,>displayList ; wait vblank, restart same display list on next frame	
	
	org	$4000
	 	
charData

	;160 x 96 (1bit)
	
	.byte	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
	.rept	94
	.byte	0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
	.endr
	.byte	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
	
	
