; INIT.C

	.include "library.inc"
	.include "macro.inc"
	.include "equ.inc"
	.nomlist
	.list

; variables

	.rsset $2000
vsync:	.rs   1		; VBL counter, increased 60 times per seconde

	;...


;[ STARTUP CODE ]

; initialize the interrupt vectors

	.bank 0
	.org  $FFF6

	.dw null_int	; IRQ2,  used by the BRK instruction
	.dw vdc_int	; IRQ1,  interrupt from the VDC
	.dw null_int	; TIMER, not used in this demo
	.dw null_int	; NMI,   never used
	.dw reset_int	; RESET, the most important one :)

	.org $E000

null_int:
	rti

; reset_int:
; ---------
; it's here that everything starts!

reset_int:
	sei			; disable interrupts 
	csh			; select the 7.16 MHz clock
	cld			; clear the decimal flag 
	ldx   #$FF		; initialize the stack
	txs 
	lda   #$FF		; map the first page to the I/O bank
	tam   #0
	lda   #$F8		; and the second to the RAM bank
	tam   #1
	stz   $2000		; clear all the RAM
	tii   $2000,$2001,$1FFF
	jsr   init_vdc		; initialize the video controller
	jsr   init_psg		; and the sound generator
	stz   $C01		; stop the timer
	stz   $1402		; unmask and enable interrupts
	cli


;[ USER PROGRAM ]

	;...


;[ INTERRUPT CODE ]

; vdc_int:
; -------
; interrupt for the VDC

vdc_int:
	pha
	phx
	phy
	lda   $0000		; load the status register

; vertical blanking interrupt code

.vbl:	bit   #$20		; vbl?
	beq   .hbl

	;...			; user vbl code

	inc   <vsync		; update the vsync flag
	jmp   .exit

; scanline interrupt code

.hbl:	bit   #$04		; scanline?
	beq   .exit

	;...			; user hbl code

; exit the interrupt handler

.exit:	lda   <_vreg
	sta   $0000
	ply
	plx
	pla
	rti


;[ LIBRARY ]

	.include "library.asm"

;[ USER DATA ]





