;**************************************************************************
;*
;* read keyboard (something is wrong here, press/release seems not to work)
;*
;**************************************************************************
	
	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	;black
	sta	COLBK
	lda	#$34	;red		
	sta	COLPF0
	lda	#$b6	;green	
	sta	COLPF1
	lda	#$1a	;yellow		
	sta	COLPF2
	lda	#$70	;blue (usable width GTIA mode 2)			
	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   
	
	lda	keyPressed       
        ;beq	noKeyPressed
	
	;key press dosnt work
	

        lda	KBCODE
	and	#$3f	

	;sta	COLBK		;debug
	
	tay
	lda	kbd2ATASCII,y
	ldy	textScreenOffset
	sta	charData+20*4+2,y
	
	lda	textScreenOffset
	clc
	add	#1
	and	#$f
	sta	textScreenOffset
	
noKeyPressed	
	
	jmp loop			;end
	
	
lastKeyCode		.byte	0	
textScreenOffset	.byte	0	
	
kbd2ATASCII 

	.byte $2C,$2A,$1B,$00,$00,$2B,$0B,$0A,$2F,$00,$30,$35,$0D,$29,$0D,$1D
	.byte $36,$00,$23,$00,$00,$22,$38,$3A,$14,$00,$13,$16,$00,$15,$12,$11
	.byte $0C,$00,$0E,$2E,$00,$2D,$0F,$00,$32,$00,$25,$39,$7F,$34,$37,$31
	.byte $19,$00,$10,$17,$7E,$18,$1C,$1E,$26,$28,$24,$00,$00,$27,$33,$21
	.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	.byte $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	
	
;************************************************
;*
;* dummy
;*
;************************************************
	
noKeyRead	
	rts
	
;************************************************
;*
;* vertical blank interrupt (immidiate, x,y regs save bei os rom)
;*
;************************************************

vbi_irq

	;
	
	lda	#0
	sta	keyPressed
	
	lda	SKCTL       
        and	#1
	beq	keyIsPressed
	
	
	
noKeyIsPressed
		
	;prolog (like XITVBL)
	
	pla					  
	tay
	pla					  
	tax
	pla					  
	rti
	
keyIsPressed

	lda	#$1
	sta	keyPressed
	jmp	noKeyIsPressed
	
keyPressed	.byte	
		
;************************************************
;*
;* display list interrupt
;*
;************************************************

dli_irq

	;prolog

        pha 
	txa	
	pha
	tya	
	pha
	
	
	;epilog
	
        pla					  
	tay
	pla					  
	tax
	pla
        rti   	;direct (no stack used)

		
; 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,$70,$70  ; 8 blank lines (mode 0) * 3
	.byte $47,<charData,>charData  ; Mode 7 + LMS, setting screen memory to $4000
	.byte $7  ;1
	.byte $7  ;2
	.byte $7  ;3 
	.byte $7  ;4
	.byte $7  ;5 
	.byte $7  ;6 
	.byte $7  ;7 
	.byte $7  ;8 
	.byte $7  ;9 
	.byte $7  ;10
	.byte $7  ;11
	.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	"--------------------" ;0
	.byte	"I type text with   I" ;1
	.byte	"I the keyboard     I" ;2
	.byte	"I                  I" ;3
	.byte	"I                  I" ;4
	.byte	"I                  I" ;5
	.byte	"I                  I" ;6
	.byte	"I                  I" ;7
	.byte	"I                  I" ;8
	.byte	"I                  I" ;9
	.byte	"I                  I" ;10
	.byte	"--------------------" ;12
	
	
	org	$8000
	
	
	
