;
;	>>> SillyPack Noter by JAC! <<<
;
;

	icl "Noter-Kernel-Equates.asm"

	org $2000

	.proc note

cnt	 = $14
char	 = $80
sound_zp = $81
x1	 = $80

.if .not .def ntsc_mode
wait_time_second	= 10
wait_time_click		= 1
wait_page		= 212
.else
wait_time_second	= 15
wait_time_click		= 1
wait_page		= 255
.endif

	.proc main
	mva #1 lmargn
	mva #3 skctl

	ldx #0
init_color_tab
	txa
	and #$f0
	sta x1
	txa
	and #$0f
	cmp #8
	scc
	eor #$0f
	asl
	ora x1
	sta color_tab,x
	inx
	bne init_color_tab

	lda #setvbv.immediate
	ldx #>vbi
	ldy #<vbi
	jsr setvbv

	.proc text_loop
lda_ptr	= *+1
	lda text
 	sta char
	jeq text_end
	inw lda_ptr
	cmp #text.CR
	beq text_loop
	cmp #text.LF
	bne no_lf
	lda #text.EOL
	sta char
	bne do_print
no_lf
do_print
	jsr print_char
no_print
	lda char
	cmp #' '
	beq no_click
	ora #$60
;	jsr click
no_click
	lda char
	cmp #text.EOL
	bne no_wait
	lda #wait_time_second
	.byte $2c
no_wait
	lda #wait_time_click
	jsr wait
	lda rowcrs
	cmp #23
	bne not_last
	lda #$ff
	sta 764
	lda #4
	sta 623
	lda #$ff
	sta $d00d
	sta $d008
	sta $d00e
	sta $d009
wait_key
	lda $d40b
	bne wait_key
	ldx $14
	ldy #240
wait_kernel
	lda color_tab,x
	sta $d40a
	sta $d012
	sta $d000
	clc
	adc #$40
	sta $d013
	sta $d001
	inx
	dey
	bne wait_kernel

	lda #$ff
	cmp 764
	beq wait_key

	lda #0
	sta $d000
	sta $d001

	lda 710
	clc
	adc #$10
	sne
	lda #$10
	sta 710

	lda #text.CLEAR
	sta char
	jsr print_char	
not_last
	jmp text_loop
	.endp
text_end
	lda #wait_page
	jsr wait
	mwa #text text_loop.lda_ptr
	jmp text_loop
	.endp
	
	.proc vbi
	jsr sound.play
	jmp sysvbv
	.endp

	.proc wait	;IN: <A>=number of frames
	clc
	adc cnt
loop	cmp cnt
	bne loop
return	rts
	.endp

	.proc print_char;IN: char, changes <A>, <X>, <Y>
	lda $e407	;Use PUT_CHAR from E: handler
	pha
	lda $e406
	pha
	lda char
	rts
	.endp

	.local text
	CR = 13
	LF = 10
	EOL = 155
	WAIT = 156
	SOUND = 157
	CLEAR = 125

	.byte CLEAR,EOL
	m_text
	.byte 0
	.endl

	.proc sound
	play = mpt_player.play
	position = mpt_player.pozsng
	pointer = mpt_player.pozptr
	volumes = mpt_player.volume

	icl "snd/Noter-MPT-Relocator.mac"
	icl "snd/Noter-MPT-Player.asm"
sound_module
	mpt_relocator 'snd/Noter-Quicky.mpt' , sound_module
	.sav [6] ?length
	.endp

	.align $100
color_tab	.ds $100
	.endp

	run note.main
