;*****************************************
;	SAMPLE MAIN PROGRAM 
;*****************************************
$MAXIMUM						;
	module  game_main				;

;  ---------------------------------
;	    EXTERNAL LOOK UP
;  ---------------------------------
	extern	SYS_PATCH				;
	extern	SYSTEM_CALL
	extern init_seting				;

;  ---------------------------------
;           EXTERNAL DEFINITION
;  ---------------------------------
	public	reset,v_int,h_int,dummy,timer1,timer2
	public	alarm_int,z80_int,sound_int

;  ---------------------------------
;	INCLUDE
;  ---------------------------------
	$include	"system.inc"
	$include	"k1head.inc"
	$include	"glbwork.inc"
	$include	"game.inc"


;-------------------------------------------------------
PROG	section code large				;
;-------------------------------------------------------

		public	__STARTUP
__STARTUP	equ	0xff1800
;-------------------------------------------------------
;		START			
;-------------------------------------------------------
reset:
	calr	SYS_PATCH				;

;------------------- INITIALIZATION --------------------
	calr	init_seting				;VRAM, work initialization

	ldb	ra3,3					;
	ldb	rw3,VECT_SYSFONTSET			;
	calr	SYSTEM_CALL				;system font definition


	EI	0
;-------------------------------------------------------
;		MAIN
;-------------------------------------------------------
main:
	cpb	(stopper),0
	jr	z,main

	cpb	(User_Shutdown),0			;shutdown request
	jr	eq,main_100

	ldb	rw3,VECT_SHUTDOWN			;shutdown
	calr	SYSTEM_CALL

main_100:
	calr	time_set

	ldb	(stopper),0
	jr	main
;-------------------------------------------------------
;-------------------------------------------------------
;		VINT
;-------------------------------------------------------
v_int:
	ldb	(WDCR),WD_CLR				;watch dog clear

	cpb	(stopper),0xff
	jr	z,v_int_skip

	pushl	xix
	pushl	xiy
	pushl	xiz
	pushl	xwa
	pushl	xbc
	pushl	xde
	pushl	xhl
;	--------------------------------
	calr	scr_set
	
	ldb	(stopper),0xff
;	--------------------------------
	popl	xhl
	popl	xde
	popl	xbc
	popl	xwa
	popl	xiz
	popl	xiy
	popl	xix

v_int_skip:
	reti

;-------------------------------------------------------
;			OTHER INTERRUPTS
;-------------------------------------------------------
h_int:
	reti

timer1:
	reti

timer2:
	reti

alarm_int:
	reti

z80_int:
	reti

sound_int:
	reti

dummy:
	reti
;-------------------------------------------------------

;-------------------------------------------------------
time_set:
	ldl	xhl3,rtc_work
	ldb	rw3,VECT_RTCGET			;obtain real time clock
	calr	SYSTEM_CALL

	lda	xiy,mozi_data
	lda	xix,time_work
	ldb	b,15

time_set_100:
	ldb	a,(xiy+)
	ldb	(xix+),a
	djnz	time_set_100

	lda	xiy,time_data
	lda	xhl,rtc_work+3

	ldb	a,(xhl+)				;hour
	calr	ascii_chg
	ldb	(xix+),':'

	ldb	a,(xhl+)				;minute
	calr	ascii_chg
	ldb	(xix+),':'

	ldb	a,(xhl+)				;second
	calr	ascii_chg
	ldb	(xix+),' '
	ret

;-------------------------------------------------------
ascii_chg:
	ldb	w,a
	andb	w,0x0f
	andb	a,0xf0
	srlb	4,a
	ldb	a,(xiy+a)
	ldb	w,(xiy+w)
	ldb	(xix+),a
	ldb	(xix+),w
	ret

;-------------------------------------------------------
time_data:
	db	"0"	;0
	db	"1"	;1
	db	"2"	;2
	db	"3"	;3
	db	"4"	;4
	db	"5"	;5
	db	"6"	;6
	db	"7"	;7
	db	"8"	;8
	db	"9"	;9
	db	"A"	;A
	db	"B"	;B
	db	"C"	;C
	db	"D"	;D
	db	"E"	;E
	db	"F"	;F
	align	2

mozi_data:
	db	"Current time is"
	align	2

;-------------------------------------------------------
scr_set:
	lda	xix,time_work
	lda	xiy,SCRL1_VRAM+0x40*8+2*2
	ldw	wa,0
	ldb	b,15

scr_set_100:
	ldb	a,(xix+)
	ldw	(xiy+),wa
	djnz	b,scr_set_100

	lda	xiy,SCRL1_VRAM+0x40*10+2*6
	ldb	b,3

scr_set_200:
	ldb	a,(xix+)
	ldw	(xiy+),wa
	ldb	a,(xix+)
	ldw	(xiy+),wa
	ldb	a,(xix+)
	ldw	(xiy+),wa
	djnz	b,scr_set_200
	ret
;-------------------------------------------------------
;-------------------------------------------------------
	end
;-------------------------------------------------------
