
       
;*************** runed.asm *******************;
;
			include 	label
			include		label_3
			include		../join/work.def
			extend
;-------------------------------------------------
	ext		OAM,MSB,HDMADAT,SELECTDAT,HDMADAT1
	ext		MENUDAT,test_data,save_numb
	ext		CG1_SET,EDIT_SET,MAPCHG_SET,SUM_CHECK
	ext		MAPCNT_SET,mapch_number,mapco_number
	ext		mapsc_number,S.L_PLACE,BUFCK_SET,BUFERS_SET2
	ext		BG2CHR,BG2SC,BG1CLAR_SET,HDMADAT2,SPEED_SET
	ext		NUM_SET,NUMPOS_SET,BGSAVE_SET,BGLOAD_SET	
	ext		BG2CLAR_SET,NUMPOS1_SET,PTANUM,PTANUM1
	ext		NUMCHG,INSNUM_SET,SUQARE_SET,TRAG_SET2
	ext		TRAG_SET4,TRAG_SET6,TRAG_SET8,TYPE_SUB
	ext		slelct_check,PRASUQ_SET,PRASUQ_SETC
	ext		PALL_CKC,BG2LOAD_SET,MPLA_SET,ERASE_SET
	ext		PTANUM2,PTANUM3,ERSAREA_SET,POINT_SET
	ext		slelct_edit,POSI_MOVE1,POSDSP_SET1
	ext		setsave,fadein_set,FADEIN_SET,SPDLOD_SET
	ext		LOAD_FILE1,SAVE_FILE1,AREA_DATACNT
	ext		error_disp1,no_disk3,Trigger_sound_1
;
	glb    	loop04,loop77,set_save
;
	glb		INIT_SET,Z_SET1,CHARA_SET
	glb		SC_SET,OBJ_SET,OAM_SET,COWD_SET,BG2_CHRSET
	glb		SC2_SET,VRAM_TENSOU,SC1_SET,VRAMZ_SET
;
	glb		TRG_set,MODECHG_SET,POSI_MOVE,POSDSP_SET
	glb		AREA_SET,POSI_MOVE,POSDSP_SET
	glb		S.LMODE_SET,SAVE_CHK,SAVE_SET,MENU_SET
	glb		LOAD_SET,CLEAR_SET,END_SET;,AREA_SET1
;
	glb	    NMI_SET
;
;-----------------------------------------------------
dos_set		equ		90h
;
editer      sect    rel
			idx8
			mem8
;
;-----------------------------------------------
;
;*****************************************;
;	NMI wari komi.			  ;
;*****************************************;
NMI_SET		equ		$
;
loop04		equ		$
			lda		flamecnt		;flame check.
			cmp		#01
			beq		setami1		
;--------------------------------------------
			lda		#01				;flame set.
			sta		flamecnt
;---------------------------------------------
			lda		#0
			sta		VMAinc
;
			lda		#00000000b		;DMA pramater.L,L 
			sta		DMA_1
; 
loop_109	lda		#00011000b		;DMA Bbus.to 2118h
			sta		DMB_1
;
			lda		sccaddl		;DMA Abus_l from 7F bank.
			sta		DMA_l1
;;
			lda		sccaddh		;DMA Abus_h
			sta		DMA_h1
;;
			lda		#01111111b		;DMA Abu_b
			sta		DMA_b1
;
			lda		#20H			;DMA count
			sta		4305h			; 400Hbyte
			lda		#00H
			sta		4306h
;
			lda		vrmadd			;VRAM arr set.
			sta		VMAaddl			;(5c000h)
			lda		vrmadd1
			sta		VMAaddh
			lda		#00000001b		;DMA on.
			sta		Mdmaen
;
			lda		dmacnt2		;counter set.
			clc
			adc		#01h
			cmp		#20h
			beq		ercnt	
			sta		dmacnt2
;;
			lda		vrmadd			;VRAM address set.
			clc
			adc		#20h
			sta		vrmadd
;
			lda		vrmadd1
			adc		#0
			sta		vrmadd1
;;
			lda		sccaddl		;7f4000h address set.
			clc
			adc		#80h
			sta		sccaddl
;
			lda		sccaddh
			adc		#0
			sta		sccaddh
;;-------------------------------
			jmp     loop_109
setami1		jmp		setami
;---------------------------------
ercnt		stz		dmacnt2
			lda		#5ch
			sta		vrmadd1
			stz		vrmadd
			lda		sccadd
			sta		sccaddl
			lda		sccadd1
			sta     sccaddh
			jmp		hdmaset
;;;
;-------------------------------------------
setami		stz		flamecnt
;
			lda		#0
			sta		VMAinc
;
			lda		#00000000b		;DMA pramater.L,L 
			sta		DMA_1
; 
loop05		lda		#00011000b		;DMA Bbus.to 2118h
			sta		DMB_1
;
			lda		sccadd2		;DMA Abus_l from 7F bank.
			sta		DMA_l1
;
			lda		sccadd3		;DMA Abus_h
			sta		DMA_h1
;
			lda		#01111111b		;DMA Abu_b
			sta		DMA_b1
;
			lda		#20H			;DMA count
			sta		4305h			; 400Hbyte
			lda		#00H
			sta		4306h
;
			lda		vramadd2		;VRAM arr set.
			sta		VMAaddl			;(58000h)
			lda		vramadd3
			sta		VMAaddh
			lda		#00000001b		;DMA on.
			sta		Mdmaen
;
			lda		dmacnt			;counter set.
			clc
			adc		#01h
			cmp		#20h
			beq		ercnt3	
			sta		dmacnt
;
			lda		vramadd2		;VRAM address set.
			clc
			adc		#20h
			sta		vramadd2
;
			lda		vramadd3
			adc		#0
			sta		vramadd3
;
			lda		sccadd2		;7f0000h address set.
			clc
			adc		#80h
			sta		sccadd2
;
			lda		sccadd3
			adc		#0
			sta		sccadd3
;
			jmp     loop05
;;;
ercnt3		stz		dmacnt
			lda		#58h
			sta		vramadd3
			stz		vramadd2
			lda		sccadd_l
			sta		sccadd2
			lda		sccadd_h
			sta     sccadd3
;;
;----------------------------------------------------
;(HDMA)
hdmaset	lda		#00000001b	;H_DMA pramater. 1address
		sta		DMA_2
;
		lda		#00100110b	;H_DMA Bbus.to2126h
		sta		DMB_2
;
		lda		#00000000b	;H_DMA Abus_l from 1000h
		sta		DMA_l2
;
		lda		#00010000b
		sta		DMA_h2
;
		lda		#00000000b
		sta		DMA_b2
;
		stz		4325h
		stz		4326h
		stz		4327h
;
		lda		#00000001b	;H_DMA pramater. 1address
		sta		DMA_3
;
		lda		#00100110b	;H_DMA Bbus.to2126h
		sta		DMB_3
;
		lda		#00000000b	;H_DMA Abus_l from 1100h
		sta		DMA_l3
;
		lda		#00010001b
		sta		DMA_h3
;
		lda		#00000000b
		sta		DMA_b3
;
		stz		4335h
		stz		4336h
		stz		4337h
;
;
		lda		#00000001b	;H_DMA pramater. 1address
		sta		DMA_4
;
		lda		#00100110b	;H_DMA Bbus.to2126h
		sta		DMB_4
;
		lda		#00000000b	;H_DMA Abus_l from 1300h
		sta		DMA_l4
;
		lda		#00010011b
		sta		DMA_h4
;
		lda		#00000000b
		sta		DMA_b4
;
		stz		4345h
		stz		4346h
		stz		4347h
;
		lda		hdmsw		;H_DMA on.
		sta		Hdmaen
;
;-------------------------------------------
;(BALNKING SET)
			lda		blankck
			sta		Inidisp
;
;-------------------------------------------
;(BUTTON READ)
;
loop77		equ		$
;			lda		flamecnt
;			cmp		#01
;			beq		setmain
;
;			lda		_Hvbjoy
;			and		#01h		
;			bne		loop77
;
;			lda  	Joy1l 		;control 1
;			sta		button
;			lda		Joy1h
;			sta		button+1
;
;			lda	 	Joy2l		;control 2
;			lda  	Joy2h
; 	
;			lda  	Joy3l 
;			lda		Joy3h
;;
;			lda  	Joy4l 
;			lda		Joy4h
setmain		rts
;
;


;---------------------------------------------------
;===================================================
;***************************************************
;			INICHIAL SUB ROUTINE
;***************************************************
;===================================================
;**********************************;
;	        INICHIAL SET		   ;
;**********************************;
INIT_SET	equ		$
  			stz		OBJsel
			stz		OAMaddl
			stz		OAMaddh
			stz		BGmode
			stz		Mozaiku
 			stz		BG1sc
			stz		BG2sc
			stz		BG3sc
			stz		BG4sc
 			stz		BG12nba
			stz		BG34nba
;
			stz		BG1hofs
			stz		BG1hofs
			stz		BG1vofs
			stz		BG1vofs
;
			stz		BG2hofs
			stz		BG2hofs
			stz		BG2vofs
			stz		BG2vofs
;
			stz		BG3hofs
			stz		BG3hofs
			stz		BG3vofs
			stz		BG3vofs
;
			stz		BG4hofs
			stz		BG4hofs
			stz		BG4vofs
			stz		BG4vofs
;
			lda		#80h
			sta		VMAinc
;
			stz		VMAaddl
			stz		VMAaddh
;
			lda		#11000000b
			sta		M7sel
;
			stz		M7a
			stz		M7a
			stz		M7b
			stz		M7b
			stz		M7c
			stz		M7c
			stz		M7d
			stz 	M7d
			stz		M7x
			stz		M7x
			stz		M7y
			stz		M7y
;
 	  		stz		CGadd
;
			lda		#00000000b	
			sta		W12sel
;
			stz		W34sel
;
			lda		#00100000b
			sta		WOBJsel
;
			stz		Wh0
			stz		Wh1
			stz		Wh2
			stz		Wh3
			stz		WBGlog
			stz		WOBJlog
			stz		TM
			stz		TS
;
			lda		#00000000b
			sta		TMW
;
			lda		#00010000b
			sta		CGswsel
;
			lda		#10110111b
			sta		CGaddsub
;
			lda		#0e7h
			sta		Coldata
;
			stz		Setini
;
			lda		#00000001b
			sta		NMItimen
;
			lda		#0ffh
			sta		Wrio
;
			stz		Wrmpya
			stz		Wrmpyb
			stz		Wrdivl
			stz		Wrdivh
			stz		Wrdivb
			stz		Htimel
			stz		Htimeh
			stz		Vtimel
			stz		Vtimeh
			stz		Mdmaen
			stz		Hdmaen
			stz		Memsel
;
			lda		#10001111b
			sta		Inidisp
;
			lda		#00000011b
			sta		BGmode
;
			lda		#01011100b
			sta		BG1sc
;
			lda		#01011000b
			sta		BG2sc
;
 			lda		#01010110b
			sta		BG12nba
;
			lda		#00010011b
			sta		TM
;	
			lda		#00000000b	
			sta		VMAinc
;		
			lda		#00000000b
			sta		Setini
;
			lda		#00000010b
  			sta		OBJsel
;----------------------------------------------
								;(WORK INICHAL)
			ldx		#0
			lda		#0
loop_605	sta		180h,x
			inx
			cpx		#80h
			bne		loop_605
;
			ldx		#0
			lda		#0
loop_606	sta		60h,x
			inx
			cpx		#30h
			bne		loop_606
;
			lda		#01
			sta		buffcnt1
;
			lda		#0feh
			sta		numset
;
			lda		#5ch
			sta		vrmadd1
;
			lda		#58h
			sta		vramadd3
;
			lda		#40h
			sta		sccadd1
			sta		sccaddh
			sta		sccset+1
			sta		sccset3+1
;
			lda		#01
			sta		<dos_set
			stz		<dos_set+1
;
			lda		#00000110b
			sta		hdmsw
;
			lda     #07h
			sta		bgaddh
			sta		bgadd1
;			sta		adress+1
			sta		bufadd+1
;
			lda		#10h
			sta		bgadd
			sta		bgaddl
;			sta		adress
			sta		bufadd
;
			lda		#80h
			sta		oamset
			sta		oamset2
;
			lda		#70h
			sta		oamset1
			sta		oamset3
;--------------------------------
			idx16
			mem16
			rep		#00110000b
;
			stz		codecnt
;
			lda		#0004h
			sta		place_set
;

			lda		#0008h
			sta		poscnt_x
			sta		posct_x1
			lda		#0007h
			sta		poscnt_y
			sta		posct_y1
;
			lda		#0080h
    		sta		posdsp_x
			sta		posdp_x
			sta		posdp_x1
;
			lda		#0070h
			sta		posdsp_y
			sta		posdp_y
			sta		posdp_y1
;
			lda		#0010h
			sta		posct_x
			lda		#000eh
			sta		posct_y
;
			idx8
			mem8
			sep		#00110000b
			rts
;***************************************
;			ZERO SET	from 1400h
;***************************************
Z_SET1		equ		$
			idx16
			mem8
			rep		#00010000b
			ldx		#0
			lda		#0
loop_99		sta		>1400h,x
			inx
			cpx		#200h
			bne		loop_99
;
			ldx		#0
			lda		#0ffh
loop_525	sta		>1600h,x
			inx
			cpx		#700h
			bne		loop_525
;
			ldx		#0
			lda		#03
loop_527	sta		>1b00h,x
			inx
			cpx		#100h
			bne		loop_527
;
			idx8
			mem8
			sep		#00010000b

			rts


;****************************************
;			VRAM ZERO CLEAR
;***************************************
VRAMZ_SET	equ		$
			mem8
			idx16
			rep		#00010000b
;
			lda		#80h
			sta		VMAinc
;
			lda		#0				;VRAM 5c00h set.
			sta		VMAaddl
			lda		#01011100b
			sta		VMAaddh
;
			ldx		#0				;zero set.
clr_loop	lda	#0
			sta		VMAdatal
			inx
			sta		VMAdatah
			inx
			cpx		#800h
			bne		clr_loop
;
;---------------------------------
			lda		#80h
			sta		VMAinc
;
			lda		#0
			sta		VMAaddl			;VRAM 5800h set.
			lda		#01011000b
			sta		VMAaddh
;
			ldx		#0				;30h set.(scdata hi side)
			lda		#30h
loop_110	sta		VMAdatal
			inx
			sta		VMAdatah
			inx
			cpx		#800h
			bne		loop_110

			mem8
			idx8
			sep		#00010000b
			rts

;****************************************
;			ZERO SET	from 7f8000h
;****************************************
Z_SET		equ		$
			idx16
			mem8
			rep		#00010000b
			ldx		#0
			lda		#0
loop_20		sta		>7f8000h,x
			inx
			cpx		#8000h
			bne		loop_20
			idx8
			mem8
			sep		#00010000b
			rts
;

;*****************************************
;			SC1 VRAM_TENSOU
;*****************************************
SC_SET		equ		$

			idx8
			mem8
			sep		#00110000b
;
			lda		#0
			sta		VMAinc
;

			lda		#00000000b		;Vram address set.
			sta		VMAaddl			;low.
			lda		#01011100b
			sta		VMAaddh			;hi.
;
			mem8
			idx16
			rep		#00010000b
			ldx		#00h
res			ldy     #0
loop50		lda		>7f4000h,x		;data tensou.
			sta		VMAdatal
			inx
			iny	
			cpy		#20h
			bne		loop50
;
			mem16
			rep		#00100000b
			txa
			clc
			adc		#60h
			tax
			mem8
			sep		#00100000b
;			
			lda		loopcnt
			inc		a
			sta		loopcnt
			cmp		#20h
			bne		res
;
			idx8
			mem8
			sep		#00010000b

;
			stz		loopcnt
			rts
;

;****************************************
;			BG1 CHARACTER VRAM TENSOU
;****************************************
CHARA_SET	equ		$
	        lda 	#80h
    	    sta    	VMAinc
; 
	    	lda		#00000000b	;Vram adrr low.
		    sta	    VMAaddl
		    lda		#01100000b	;Vram adrr hi.
		    sta	    VMAaddh
;
			idx16
			mem16
			rep		#00110000b
	    	ldy		#0			;character data.
;loop2		lda		[<addr1],y
			sta		VMAdatal
			iny
			iny
;			cpy		#4000h		;4000? :no-->lopp1.
;			bne		loop2
; 
			idx8		        ;8bit mode.
			mem8
			sep		#00110000b
			rts
;
;
;****************************************
;			SC1 SET  to 7F4000h,7f8000h
;****************************************
SC1_SET		equ		$
			idx16
			mem8
			rep		#00010000b
;
			ldy		buffcnt
			ldx		#0
loop_buff	lda		>7f0000h,x			
			sta		>7f4000h,x
			iny
			inx
			cpx		#4000h
			bne		loop_buff
;
			ldy		buffcnt
			ldx		#0
loop_buff1	lda		>7f0000h,x			
			sta		>7f8000h,x
			iny
			inx
			cpx		#4000h
			bne		loop_buff1
;
			idx8
			mem8	
			sep		#00010000b
			rts
;----------------------------------

;
;***************************************
;			OBJ TENSOU
;***************************************
OBJ_SET		equ		$
			lda		#80h
			sta		VMAinc
;

			lda		#00000000b		;Vram address set.
			sta		VMAaddl			;low.
			lda		#01000000b
			sta		VMAaddh			;hi.
;
			idx16					;16bit mode.
			mem16
			rep		#00110000b
;
			ldx		#0
loop10		lda		>99800h,x		;data tensou.
			sta		VMAdatal
			inx
			inx
			cpx		#1000h
			bne		loop10
;
			ldx		#0
loop11		lda		>9a800h,x		;data tensou.
			sta		VMAdatal
			inx
			inx
			cpx		#1000h
			bne		loop11
;
			idx8		        ;8bit mode.
			mem8
			sep		#00110000b
;
			rts
;
;***************************************
;			OAM SET
;***************************************
OAM_SET		equ		$
			idx16
 			mem8
			rep		#00010000b
;  
			ldy		#000h
hhh			lda		#080h		 
			sta		oamdat,y
			iny
			cpy		#200h
			bne		hhh
;
			ldy		#00h
oam_loop	lda		#055h
			sta		msbdat,y
			iny
			cpy		#20h
			bne		oam_loop
;--------------------------------
			ldy		#00h
eee			lda		OAM,y		
			cmp		#0ffh
			beq		msbset
			sta		oamdat,y
			iny
			jmp		eee
;--------------------------------
msbset		sep		#00010000b
 			mem8
			idx8

			ldy		#00h
loop19		lda		MSB,y
			sta		msbdat,y
			iny		
			cpy		#20h
			bne		loop19
;
			rts
;
;

;
;*********************************************
;			COLOR WIND SET
;*********************************************
COWD_SET	equ		$
			ldx		#0
loop_8		lda		HDMADAT,x
			sta		cwiddat,x
			inx
			cpx		#0bh
			bne		loop_8
;
			ldx		#0
loop_9		lda		HDMADAT1,x
			sta		cwiddat1,x
			inx
			cpx		#0dh
			bne		loop_9
;
			ldx		#0
loop_119	lda		HDMADAT2,x
			sta		cwiddat2,x
			inx
			cpx		#0dh
			bne		loop_119
;
			stz		trgcnt
			rts
	
;===============================================
;********************************************
;			BG2 INITIHAL SET
;********************************************
;================================================
;***************************************
;			BG2 VRAM TENSOU
;***************************************
BG2_CHRSET	equ		$
	        lda 	#80h
    	    sta    	VMAinc
; 
	    	lda		#00000000b	;Vram adrr low.
		    sta	    VMAaddl
		    lda		#01010000b	;Vram adrr hi.
		    sta	    VMAaddh
;
			idx16
			mem8
			rep		#00010000b
	    	ldy		#0			;character data.
loop102		lda		BG2CHR,y
			sta		VMAdatal
			iny
			sta     VMAdatah
			iny
			cpy		#80h		;4000? :no-->lopp1.
			bne		loop102
;
			ldx		#0
loop111		lda		>99000h,x
			sta		VMAdatal
			inx
			sta     VMAdatah
			inx
			cpx		#800h		;4000? :no-->lopp1.
			bne		loop111
;					 
			idx8		        ;8bit mode.
			mem8
			sep		#00010000b
			rts
;
;*******************************************
;			SC2 DATA BUFFER TENSOU
;*******************************************
SC2_SET		equ		$
			idx16
			mem8	
			rep		#00010000b
			ldx		#0
loop_104	ldy     #0
loop_103	lda		BG2SC,y
			sta		>7f0000h,x
			sta		>7fc000h,x
			iny
			inx
			cpy		#08
			bne		loop_103
			lda		dmacnt
			inc		a
			sta		dmacnt
			cmp		#10h
			bne		loop_104
			stz		dmacnt
;
loop_107	ldy		#08
loop_108	lda		BG2SC,y
			sta		>7f0000h,x
			sta		>7fc000h,x
			iny
			inx
			cpy		#10h
			bne		loop_108
;
			lda		dmacnt
			inc		a
			sta		dmacnt
			cmp		#10h
			bne		loop_107
			stz     dmacnt
;
;
			lda		sccnt
			inc		a
			sta		sccnt
			cmp		#01
			bne		loop_107
			stz		sccnt
;

			cpx		#4000h
			bne		loop_104
			stz		dmacnt
			stz		sccnt
;
;
			idx8
			mem8
			sep		#00010000b
			rts
;
;*****************************************
;			BG2 VRAM TENSOU
;*****************************************
VRAM_TENSOU	equ		$
			
			idx8
			mem8
			sep		#00110000b
;
			lda		#0
			sta		VMAinc
;

			lda		#00000000b		;Vram address set.
			sta		VMAaddl			;low.
			lda		#01011000b
			sta		VMAaddh			;hi.
;
			mem8
			idx16
			rep		#00010000b
			ldx		#00h
res1		ldy     #0
loop51		lda		>7f0000h,x		;data tensou.
			sta		VMAdatal
			inx
			iny	
			cpy		#20h
			bne		loop51
;
			mem16
			rep		#00100000b
			txa
			clc
			adc		#60h
			tax
			mem8
			sep		#00100000b
;			
			lda		loopcnt
			inc		a
			sta		loopcnt
			cmp		#20h
			bne		res1
;
			idx8
			mem8
			sep		#00010000b

;
			stz		loopcnt
			rts
;

;======================================================
;******************************************************
;			MAIN ROUTINE
;******************************************************
;======================================================
;
;
;**************************************
;			TRG_SET
;**************************************
TRG_set		equ		$
			mem16
			rep		#00100000b
			lda		<button_trigger
			ora		<button_trigger+2
			sta		trg_button
;
			lda		<button_status
			ora		<button_status+2
			sta		button
owari_221	mem8
			sep		#00100000b
		;
;		lda		button			;button trger set.
;		eor		bak_button
;		and		button
;		sta		trg_button
;		lda		button
;		sta		bak_button
;
;
;		lda		button+1		;button1 trger set.
;		eor		bak_button1
;		and		button+1
;		sta		trg_button1
;		lda		button+1
;		sta		bak_button1
		rts
;
;*****************************************
;			POSITION MOVE
;*****************************************
POSI_MOVE	equ		$
			lda		modeflg		;save load?
			cmp		#01
			beq     owari_15
;----------------------------------
;			lda		<slectflg		;modeB?:yes-->owari_15
;			cmp		#01h
;			beq		owari_15

;=========================================
;(AUTO SWICH SET)
			lda		button+1		;tenkey check.
			and		#00001111b
			beq		zcnt			;push key? :no-->zcnt.
;--------------------------------
			lda		trgcnt			;push counter.
			beq		add			;0 ? :yes-->add1.
			clc
			adc		#01h
			cmp		#15h
			beq		sub1			;12 ? :yes-->sub15.
			sta		trgcnt
owari_15	rts
;--------------------------------
setbtn		lda		button+1
			cmp		#02h
			beq		lftmov1
;
			cmp		#01h
			beq		ritmov1
;
			cmp		#08h
			beq		upmov1
;
			cmp		#04h
			beq		dwnmov
;
			cmp		#05
			beq		d_rit
;
			cmp		#06
			beq		d_lft
;
			cmp		#09
			beq		u_rit
;
			cmp		#0ah
			beq		u_lft
			rts
;--------------------------------
add			lda		trgcnt			;counter +1 set.
			clc
			adc		#01h
			sta		trgcnt
			jmp		setbtn
;
sub1		lda		#12h			;counter 10 set.
			sta		trgcnt
			jmp		setbtn
;
zcnt		stz		trgcnt			;counter zero set.
			stz		sclcnt
			rts
;=========================================
;								(NANAME SCROLL SET)
d_rit		lda		button
			cmp		#40h
			bne		owari_15
;
			JSR		sccrit
			JSR		sccdwn
			rts
;
d_lft		lda		button
			cmp		#40h
			bne		owari_15
;
			JSR		sccdwn
			JSR		scclft
			rts
;
u_rit		lda		button
			cmp		#40h
			bne		owari_15
;
			JSR		sccup
			JSR		sccrit
			rts
;
u_lft		lda		button
			cmp		#40h
			bne		owari_15
;
			JSR		sccup
			JSR		scclft
owari_123	rts
;---------------------------------
;
;
;---------------------------------
lftmov1		jmp		lftmov
ritmov1		jmp		ritmov
upmov1		jmp		upmov
;---------------------------------
dwnmov		lda		button
			cmp		#40h
			beq		sccdwn1
;
			lda		poscnt_y
			cmp		#3fh
			beq		owari_123
;
			lda		oamdat+1		;Yposition up.
			clc
			adc		#10h
			cmp		#0c0h
			beq		sccdwn1
			sta		oamdat+1
			sta		oamset3
;
			jsr		posyadc			;kasoru position check.
			jmp		owari_1
owari_60	rts
;
upmov		lda		button
			cmp		#40h
			beq		sccup
;
			lda		poscnt_y
			beq		owari_1
;
			lda     oamdat+1		;Yposition down.
			sec
			sbc		#10h
			cmp		#20h
			beq		sccup1
			sta		oamdat+1
			sta		oamset3
;
			jsr		posysbc			;kasoru position check.
			jmp     owari_1
;----------------------------------
sccdwn1		jmp		sccdwn
sccup1		jmp		sccup
;----------------------------------
ritmov		lda		button
			cmp		#40h
			beq		sccrit1
;
			lda		poscnt_x
			cmp		#3fh
			beq		owari_1
;
			lda		oamdat			;Xposition right.
			clc
			adc		#10h
			cmp		#0d0h
			beq		sccrit1
			sta		oamdat
			sta		oamset2
			jsr		posxadc			;kasoru position check.
			jmp     owari_1
;--------------------------------
sccrit1		jmp		sccrit
;--------------------------------
lftmov		lda		button
			cmp		#40h
			beq		scclft1		
;
			lda		poscnt_x
			beq		owari_1
;
			lda		oamdat			;Xposition left.
			sec
			sbc		#10h
			cmp		#30h
			beq		scclft1
 			sta     oamdat
			sta		oamset2
			jsr		posxsbc			;kasoru position check.
owari_1		rts			
;------------------------------------------
scclft1		jmp		scclft
;-------------------------------------------
sccup		equ		$
			lda		poscnt_y
			cmp		#0h
			beq		owari_959
			idx8
			mem16
			rep		#00100000b
			lda		sccadd
			sec
			sbc		#100h
			sta		sccadd
			sta		sccaddl
			sta		sccset3
;
			lda		sccadd_l
			sec
			sbc		#100h
			sta		sccadd_l
			sta		sccadd2
			sta		sccset4
;
			idx8
			mem8
			sep		#00100000b
			jsr		posysbc
owari_959	mem8
			sep		#00100000b
			rts
;
sccdwn		lda		poscnt_y
			cmp		#03fh
			beq		owari_959
;
			idx8
			mem16
			rep		#00100000b
;
			lda		sccadd
			clc
			adc		#100h
			sta		sccadd
			sta		sccaddl
			sta		sccset3
;
			lda		sccadd_l
			clc
			adc		#100h
			sta		sccadd_l
			sta		sccadd2
			sta		sccset4
;
			idx8
			mem8
			sep		#00100000b
			jsr		posyadc
			rts
;
sccrit		lda		poscnt_x
			cmp		#03fh
			beq		owari_959
;
			idx8
			mem16
			rep		#00100000b
			lda		sccadd
			clc
			adc		#02h
			sta		sccadd
			sta		sccaddl
			sta		sccset3
;
			lda		sccadd_l
			clc
			adc		#02h
			sta		sccadd_l
			sta		sccadd2
			sta		sccset4
;
			idx8
			mem8
			sep		#00100000b
			jsr		posxadc
			rts
;
scclft		lda		poscnt_x
			cmp		#0
			beq		owari_959
		
			idx8
			mem16
			rep		#00100000b
			lda		sccadd
			sec
			sbc		#02h
			sta		sccadd
			sta		sccaddl
			sta		sccset3
;
			lda		sccadd_l
			sec
			sbc		#02h
			sta		sccadd_l
			sta		sccadd2
			sta		sccset4
;
			idx8
			mem8
			sep		#00100000b
			jsr		posxsbc
			rts
;-------------------------------------------
;									(KASORU POSITION SET)
posyadc		equ		$
			idx8
			mem8
			sep		#00110000b
			lda		posdsp_y
			clc
			adc		#10h
			sta		posdsp_y
			lda		posdsp_y+1
			adc		#0
			sta		posdsp_y+1
			mem16
			rep		#00100000b
			lda		posdsp_y
			sta		posdp_y1
			lsr		a
			lsr		a
			lsr		a
			lsr		a
			mem8
			sep		#00100000b
			sta		poscnt_y
			sta		posct_y1
			rts
;
;
posysbc		equ		$
			idx8
			mem8
			sep		#00110000b
			lda		posdsp_y
			sec
			sbc		#10h
			sta		posdsp_y
			lda		posdsp_y+1
			sbc		#0
			sta		posdsp_y+1
			mem16
			rep		#00100000b
			lda		posdsp_y
			sta		posdp_y1
			lsr		a
			lsr		a
			lsr		a
			lsr		a
			mem8
			sep		#00100000b
			sta		poscnt_y
			sta		posct_y1
			rts
;;
posxadc		equ		$
			idx8
			mem8
			sep		#00110000b
			lda		posdsp_x
			clc
			adc		#10h
			sta		posdsp_x
			lda		posdsp_x+1
			adc		#0
			sta		posdsp_x+1;
			mem16
			rep		#00100000b
			lda		posdsp_x
			sta		posdp_x1
			lsr		a
			lsr		a
			lsr		a
			lsr		a
			mem8
			sep		#00100000b
			sta		poscnt_x
			sta		posct_x1

			rts
;
posxsbc		equ		$
			idx8
			mem8
			sep		#00110000b
			lda		posdsp_x
			sec
			sbc		#10h
			sta		posdsp_x
			lda		posdsp_x+1
			sbc		#0
		  	sta		posdsp_x+1
			mem16
			rep		#00100000b
			lda		posdsp_x
			sta		posdp_x1
			lsr		a
			lsr		a
			lsr		a
			lsr		a
			mem8
			sep		#00100000b
			sta		poscnt_x
			sta		posct_x1
			rts
;
;
;*****************************************
;			POSITION DISPLAY
;*****************************************
POSDSP_SET	equ		$				;(X POSITION)
			idx8
			mem8
			sep		#00110000b
;---------------------------------
			lda		modeflg
			cmp		#01
			beq     owari_14
;------------------------------------
;
			ldx		#12h
			lda		poscnt_x
			and		#0fh
			sta		oamdat,x
;
			ldx		#0eh
			lda		poscnt_x
			and		#0f0h
			lsr     a
			lsr		a
			lsr		a
			lsr     a
			sta		oamdat,x
;

;--------------------------------
bbb			ldx		#22h			;(Y POSITION)
			lda		poscnt_y
			and     #0fh
			sta		oamdat,x
;
			ldx		#1eh
			lda		poscnt_y
			and     #0f0h
			lsr     a
			lsr		a
			lsr		a
			lsr     a
			sta		oamdat,x
;
owari_14	rts
;
;**************************************
;			AREA NUMBER & DISTANS SET
;**************************************
AREA_SET	equ		$
;--------------------------------
			lda		modeflg
			cmp		#01
			beq     owari_14
;--------------------------------
;			lda		<slectflg		;modeB?:yes-->owari_14
;			cmp		#01h
;			beq		owari_14
;--------------------------------
			lda		trg_button1
			and		#0ffh
			cmp		#80h
			bne		owari_14
;--------------------------------
			lda		areaflg
			cmp		#01
			beq		anumset3
;---------------------------------
			idx16					;(same address check &
			rep		#00010000b		;flag set)
			ldx		#02
loop_212	lda		1400h,x
			sta		poscnt_x1
			inx
			cmp		poscnt_x
			bne		adcx1			
;
			lda		1400h,x
			sta		poscnt_y1
			cmp		poscnt_y
			beq		setflg	
;
adcx1		inx
			inx
			inx 
			cpx		#202h
			bne		loop_212
			idx8
			sep		#00010000b
			rts
;
setflg		stx		pointcnt		;7e8000h address set.
			idx8
			sep		#00010000b		;flag set.
			lda		#01
			sta		areaflg
			rts	
;==============================================
;----------------------------------------------
anumset3	idx16
			rep		#00010000b
			ldx		pointcnt		;(code number check)
			dex		
			dex		
			lda		1400h,x
			sta		subnum
			idx8
			sep		#00010000b
;-------------------------------
anumset		lda		poscnt_x		;(area number set)
			sec		
			sbc		poscnt_x1		;X,X distanse set.	
			sta		poscnt_x1
;
			lda		poscnt_y
			sec		
			sbc		poscnt_y1
			sta		poscnt_y1
;--------------------------------
			ldx		subnum				;(kasor eroor ckeck)
			JSR		(!slelct_check,x)
;
			lda		owari_flg
			cmp		#01
			beq		owari_405
;--------------------------------
			mem16
			rep		#00100000b
			lda		#0001h				;buffer initial address set.
			sta		place_set			;(7f0000h)
;
			lda		poscnt_y			;2chr*2chr place set.
			asl		a
			asl		a
			asl		a
			asl		a
			asl		a
			asl		a
			sta     pos
;
			lda		poscnt_x
			eor		pos
			sta		pos
			mem8
			sep		#00100000b
;----------------------------
anumset1	JSR		MPLA_SET			;7f0000h address set.
			jmp		owari_317
;
;---------------------------------
owari_405	mem8
			idx8
			sep		#00110000b
			stz		owari_flg
			stz		areaflg
			rts
;---------------------------------
owari_317	mem8
			idx8
			sep		#00110000b
;
			lda		areacnt			;area number display set.
			and		#00001111b			;(areacnt) is 
			tax							;area number.
			lda     PTANUM2,x
;
			idx16
			rep		#00010000b
			ldx		place_set
			sta		>7f0000h,x
			idx8
			sep		#00010000b
;
			lda		areacnt
			and		#11110000b
			lsr		a
			lsr		a
			lsr		a
			lsr		a
			tax
			lda     PTANUM3,x		
;
			idx16
			rep		#00010000b
			ldx		place_set
			dex
			sta		>7f0000h,x
			idx8
			sep		#00010000b
;------------------------------
			ldx		subnum			;subroutine set.
			JSR		(!TYPE_SUB,x)
			lda		owari_flg
			cmp		#01
			beq		owari_962
			jmp		anumset1	
;------------------------------	
owari_962	mem8					;(BUFFER SET TO 7e8200)
			sep		#00100000b
			idx16
			rep		#00010000b
;
			ldy		#0000
			ldx		pointcnt
			dex
			dex
;
loop_520	lda		1400h,x			;buffer set.
			sta		datformat,y			;7e8000h->1200h
			inx							;code,x,ypos set.
			iny
			cpy		#03h
			bne		loop_520
;
			lda		subnum				;x_length noset check.	
			cmp		#02
			bcs		no_xset1
;
set_x		lda		poscnt_x1			;x_length set.
			inc		a
			sta		datformat,y
;
			mem16
			rep		#00100000b				
			lda		#0005
			sta		ycnt
			mem8
			sep		#00100000b
;
			iny
			inx
no_xset		lda		poscnt_y1			;y_length set.
			inc		a
			sta		datformat,y
;
			mem16
			rep		#00100000b
			lda		areacnt			;1200h-->7e8200h
			and		#00ffh				;areacnt is 7e8200h
;										adress counter.	
			asl		a
			asl		a
			asl		a
			tax
			mem8
			sep		#00100000b
;	
			ldy		#0
loop_356	lda		datformat,y
			sta		1600h,x
			inx
			iny
			cpy		ycnt
			bne		loop_356
;		
			stz		areaflg			
			stz		arodcnt
			stz		lthcnt
;
			lda		areacnt			;area count inc.
			inc		a
			sta		areacnt
			idx8
			sep		#00010000b
;
			stz		owari_flg
;
			rts
;
no_xset1	cmp		#0ah
			bcs		set_x
;
			mem16
			rep		#00100000b
			lda		#0004
			sta		ycnt
			mem8
			sep		#00100000b
			jmp		no_xset
;**************************************
;			AREA NUMBER & DISTANS SET
;**************************************
AREA_SET1	equ		$
;--------------------------------
			lda		modeflg
			cmp		#01
			beq     owari_141
;--------------------------------
			lda		trg_button
			and		#0ffh
			cmp		#80h
			bne		owari_141
;--------------------------------
			lda		areaflg
			cmp		#01
			beq		anumset31
;---------------------------------
			idx16					;(same address check &
			rep		#00010000b		;flag set)
			ldx		#02
loop_2121	lda		1400h,x
			sta		poscnt_x1
			inx
			cmp		poscnt_x
			bne		adcx11			
;
			lda		1400h,x
			sta		poscnt_y1
			cmp		poscnt_y
			beq		setflg1	
;
adcx11		inx
			inx
			inx 
			cpx		#202h
			bne		loop_2121
			idx8
			sep		#00010000b
owari_141	rts
;
setflg1		stx		pointcnt		;7e8000h address set.
			idx8
			sep		#00010000b		;flag set.
			lda		#01
			sta		areaflg
;			rts	
;==============================================
;----------------------------------------------
anumset31	idx16
			rep		#00010000b
			ldx		pointcnt		;(code number check)
			dex		
			dex		
			lda		1400h,x
			sta		subnum
			idx8
			sep		#00010000b
;-------------------------------
			lda		poscnt_x		;(area number set)
			sec		
			sbc		poscnt_x1		;X,X distanse set.	
			sta		poscnt_x1
;
			lda		poscnt_y
			sec		
			sbc		poscnt_y1
			sta		poscnt_y1
;--------------------------------
			ldx		subnum				;(kasor eroor ckeck)
			JSR		(!slelct_check,x)
;
			lda		owari_flg
			cmp		#01
			beq		owari_4051
;--------------------------------
			mem16
			rep		#00100000b
			lda		#0001h				;buffer initial address set.
			sta		place_set			;(7f0000h)
;
			lda		poscnt_y			;2chr*2chr place set.
			asl		a
			asl		a
			asl		a
			asl		a
			asl		a
			asl		a
			sta     pos
;
			lda		poscnt_x
			eor		pos
			sta		pos
			mem8
			sep		#00100000b
;----------------------------
anumset11	JSR		MPLA_SET			;7f0000h address set.
			jmp		owari_3171
;
;---------------------------------
owari_4051	mem8
			idx8
			sep		#00110000b
			stz		owari_flg
			stz		areaflg
			rts
;---------------------------------
owari_3171	mem8
			idx8
			sep		#00110000b
;
			lda		areacnt			;area number display set.
			and		#00001111b			;(areacnt) is 
			tax							;area number.
			lda     PTANUM2,x
;
			idx16
			rep		#00010000b
			ldx		place_set
			sta		>7f0000h,x
			idx8
			sep		#00010000b
;
			lda		areacnt
			and		#11110000b
			lsr		a
			lsr		a
			lsr		a
			lsr		a
			tax
			lda     PTANUM3,x		
;
			idx16
			rep		#00010000b
			ldx		place_set
			dex
			sta		>7f0000h,x
			idx8
			sep		#00010000b
;------------------------------
			ldx		subnum			;subroutine set.
			JSR		(!TYPE_SUB,x)
			lda		owari_flg
			cmp		#01
			beq		owari_9621
			jmp		anumset11	
;------------------------------	
owari_9621	mem8					;(BUFFER SET TO 7e8200)
			sep		#00100000b
			idx16
			rep		#00010000b
;
;		
;			stz		areaflg			
			stz		arodcnt
			stz		lthcnt
;
			lda		areacnt			;area count inc.
			inc		a
			sta		areacnt
			idx8
			sep		#00010000b
;
			stz		owari_flg
;
			rts
;================================================

;================================================

;************************************************
;			MODE B SET (CODE SET)
;************************************************
;================================================
;
;
;*****************************************
;			MODE CHANGE SET
;*****************************************
MODECHG_SET	equ		$
			lda		modeflg
			cmp		#01
			beq     owari_12
;--------------------------------
			lda		trg_button1
			cmp		#20h
			bne		owari_12
			mem16
			rep		#00100000b
			lda		#0024h
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b

;--------------------------------
			lda		slectflg		;flag set.
			inc		a
			cmp		#03
			beq		reset1		
			sta		slectflg
;
flcheck		lda		slectflg		;flg check.
			cmp		#0
			beq		flgzro
			cmp		#02
			beq		flgthr1
;---------------------------------
			ldx		#26h			;character name display.
			lda		#01				;code set mode.
			asl		a
			asl		a
			asl     a
			tay
loop_241	lda		SELECTDAT,y
			sta		oamdat,x
			inx
			inx
			inx
			inx
			iny
			cpx		#3eh
			bne		loop_241
;
			lda		oamset2
			sta		oamdat
			lda		oamset3
			sta		oamdat+1
			mem16
			rep		#00100000b
			lda		sccset3
			sta		sccadd
			sta		sccaddl
;
			lda		sccset4
			sta		sccadd_l
;
			lda		posct_y1
			sta		poscnt_y
			lda		posct_x1
			sta		poscnt_x
			lda		posdp_x1
			sta		posdsp_x
			lda		posdp_y1
			sta		posdsp_y
			mem8
			sep		#00100000b
;

owari_12	lda		slectflg
			asl		a
			tax
			rts
;--------------------------------
reset1		stz		slectflg
			jmp		flcheck
;
flgthr1		jmp		flgthr
;--------------------------------
flgzro		ldx		#26h			;character name display.
			lda		#0				;set mode.
			asl		a
			asl		a
			asl     a
			tay
loop_242	lda		SELECTDAT,y
			sta		oamdat,x
			inx
			inx
			inx
			inx
			iny
			cpx		#3eh
			bne		loop_242
;
			lda		oamset2
			sta		oamdat
			lda		oamset3
			sta		oamdat+1
			mem16
			rep		#00100000b
			lda		sccset3
			sta		sccadd
			sta		sccaddl
;
			lda		sccset4
			sta		sccadd_l
;
			lda		posct_y1
			sta		poscnt_y
			lda		posct_x1
			sta		poscnt_x
			lda		posdp_x1
			sta		posdsp_x
			lda		posdp_y1
			sta		posdsp_y
			mem8
			sep		#00100000b
;
			ldx		#6ah			;speed display 0 set.
			lda		#0
			sta		oamdat,x
			ldx		#66h
			sta		oamdat,x
;
			lda		slectflg
			asl		a
			tax
			rts			
;--------------------------------------
flgthr		ldx		#26h			;character name display.
			lda		#02				;point mode.
			asl		a
			asl		a
			asl     a
			tay
loop_243	lda		SELECTDAT,y
			sta		oamdat,x
			inx
			inx
			inx
			inx
			iny
			cpx		#3eh
			bne		loop_243
;
			lda		oamset
			sta		oamdat
			lda		oamset1
			sta		oamdat+1
			mem16
			rep		#00100000b
			lda		sccset
			sta		sccadd
			sta		sccaddl
;
			lda		sccset2
			sta		sccadd_l
;
			lda		posct_y
			sta		poscnt_y
			lda		posct_x
			sta		poscnt_x
			lda		posdp_x
			sta		posdsp_x
			lda		posdp_y
			sta		posdsp_y
			mem8
			sep		#00100000b
;
			lda		slectflg
			asl		a
			tax
			rts			

;
;******************************************
;			
;===================================================
;***************************************************
;			LOAD SAVE MODE
;***************************************************
;===================================================
;
;*****************************************
;			SAVE LOAD MODE SET
;*****************************************
S.LMODE_SET	equ		$
;			lda		<slectflg		;modeB?:yes-->owari_16
;			cmp		#01h
;			beq		owari_24
;---------------------------------------
			lda		trg_button
			cmp		#10h
			bne		owari_24
;--------------------------------------
			lda		modeflg
			cmp		#01
			beq		cansel
;--------------------------------------
			lda		#01
			sta		modeflg	
;
			ldx		#26h
			lda		s.l_set
			asl		a
			asl		a
			asl		a
			tay
loop_26		lda		MENUDAT,y
			sta		oamdat,x
			inx
			inx
			inx
			inx
			iny
			cpx		#3eh
			bne		loop_26
			ldx		#34h
			lda		#90h
			sta		oamdat,x
			ldx		#38h
			lda		#098h
			sta		oamdat,x
;
			ldx		#03
			lda		#40h
			sta		msbdat,x
			lda		#55h
			inx
			sta     msbdat,x
			inx
			sta		msbdat,x
;
;
			lda		#06				;"yes no'erase check.
			sta		hdmsw

			lda		s.l_set
			cmp		#02
			bcs		owari_24
			ldx		#03
			lda		#0
loop_499	sta		msbdat,x
			inx		
			cpx		#06
			bne		loop_499
;;
			lda		#0eh
			sta		hdmsw
			mem16
			rep		#00100000b
			lda		#0024h
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b

owari_24	rts
;------------------------------------------
cansel		mem16
			rep		#00100000b
			lda		#0024h
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b

			stz		modeflg
;
			lda		slectflg
			cmp		#01
			beq		canse2
			cmp		#02
			beq		canse31
;-----------------------------------------
			ldx		#26h			;character name display.
			lda		#0
			asl		a
			asl		a
			asl     a
			tay
loop_24		lda		SELECTDAT,y
			sta		oamdat,x
			inx
			inx
			inx
			inx
			iny
			cpx		#3eh
			bne		loop_24
			ldx		#34h
			lda		#98h
			sta		oamdat,x
			ldx		#38h
			lda		#0a0h
			sta		oamdat,x
;
			ldx		#03
			lda		#40h
			sta		msbdat,x
			lda		#55h
			inx
			sta     msbdat,x
			inx
			sta		msbdat,x
;
			lda		#06
			sta		hdmsw
;
			stz		save_flg		;(SAVE--> NO.)
;
			ldx		#3fh
			lda		#24h
loop_0932	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#4bh
			bne		loop_0932
;
			lda		#28h
loop_0832	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#57h
			bne		loop_0832

			rts			
;------------------------------------
canse31		jmp		canse3
;------------------------------------
canse2		ldx		#26h			;character name display.
			lda		#01
			asl		a
			asl		a
			asl     a
			tay
loop_924	lda		SELECTDAT,y
			sta		oamdat,x
			inx
			inx
			inx
			inx
			iny
			cpx		#3eh
			bne		loop_924
			ldx		#34h
			lda		#98h
			sta		oamdat,x
			ldx		#38h
			lda		#0a0h
			sta		oamdat,x
;
			ldx		#03
			lda		#40h
			sta		msbdat,x
			lda		#55h
			inx
			sta     msbdat,x
			inx
			sta		msbdat,x
;
;
			lda		#06
			sta		hdmsw
;
			stz		save_flg		;(SAVE--> NO.)
;
			ldx		#3fh
			lda		#24h
loop_0933	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#4bh
			bne		loop_0933
;
			lda		#28h
loop_0833	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#57h
			bne		loop_0833


			rts			
;
;------------------------------------
canse3		ldx		#26h			;character name display.
			lda		#02
			asl		a
			asl		a
			asl     a
			tay
loop_926	lda		SELECTDAT,y
			sta		oamdat,x
			inx
			inx
			inx
			inx
			iny
			cpx		#3eh
			bne		loop_926
			ldx		#34h
			lda		#98h
			sta		oamdat,x
			ldx		#38h
			lda		#0a0h
			sta		oamdat,x
;
			ldx		#03
			lda		#40h
			sta		msbdat,x
			lda		#55h
			inx
			sta     msbdat,x
			inx
			sta		msbdat,x
;
;
			lda		#06
			sta		hdmsw
;
			stz		save_flg		;(SAVE--> NO.)
;
			ldx		#3fh
			lda		#24h
loop_0934	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#4bh
			bne		loop_0934
;
			lda		#28h
loop_0834	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#57h
			bne		loop_0834


owari_251	rts			
;

;*****************************************
;			MENU SELECT SET
;*****************************************
MENU_SET	equ		$
			lda		modeflg
			cmp		#01
			bne		owari_251
;----------------------------------
			lda		trg_button1
			cmp		#08h
			beq		sbcmen
;
			cmp		#04h
			bne		owari_251			
;----------------------------------
			lda		s.l_set
			clc
			adc		#01
			cmp		#05
			beq		zero
			sta		s.l_set
			jmp		dispset
;
sbcmen		lda		s.l_set
			sec
			sbc		#01
			cmp		#0ffh
			beq		two
			sta		s.l_set
			jmp		dispset
;
two			lda		#04
			sta		s.l_set
			jmp		dispset
;
zero		stz		s.l_set
;----------------------------------
dispset		mem16
			rep		#00100000b
			lda		#002ch
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b

			ldx		#26h
			lda		s.l_set
			asl		a
			asl		a
			asl		a
			tay
loop_23		lda		MENUDAT,y
			sta		oamdat,x
			inx
			inx
			inx
			inx
			iny
			cpx		#3eh
			bne		loop_23
			ldx		#34h
			lda		#90h
			sta		oamdat,x
			ldx		#38h
			lda		#98h
			sta		oamdat,x

;-------------------------------
n_save1		stz		save_flg		;(SAVE--> NO.)
;
			ldx		#3fh			;'no'display set.
			lda		#24h
loop_0931	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#4bh
			bne		loop_0931
;
			lda		#28h
loop_0831	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#57h
			bne		loop_0831
;-------------------------------

			ldx		#03
			lda		#40h
			sta		msbdat,x
			lda		#55h
			inx
			sta     msbdat,x
			inx
			sta		msbdat,x
;
			lda		#06
			sta		hdmsw

;-----------------------------
			lda		s.l_set
			cmp		#02
			bcs		owari_25
			ldx		#03
			lda		#0
loop_498	sta		msbdat,x
			inx		
			cpx		#06
			bne		loop_498
;
			lda		#0eh
			sta		hdmsw
;

owari_25	rts
;
;****************************************
;			SAVE CHECK
;****************************************
SAVE_CHK	equ		$
			lda		modeflg
			cmp		#01
			bne		owari_25
;----------------------------------
			lda		s.l_set
			cmp		#02
			bcs		owari_25
;----------------------------------
			lda		trg_button1
			cmp		#02h
			beq		y_save
;
			cmp		#01
			beq		n_save
			rts
;----------------------------------
y_save		lda		#01				;(SAVE--> YES.)
			sta		save_flg
;
			ldx		#3fh
			lda		#28h
loop_097	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#4bh
			bne		loop_097
;
			lda		#24h
loop_082	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#57h
			bne		loop_082
			mem16
			rep		#00100000b
			lda		#002ch
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b

			rts
;----------------------------------
n_save		stz		save_flg		;(SAVE--> NO.)
;
			ldx		#3fh
			lda		#24h
loop_093	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#4bh
			bne		loop_093
;
			lda		#28h
loop_083	sta		oamdat,x
			inx
			inx
			inx
			inx
			cpx		#57h
			bne		loop_083
			mem16
			rep		#00100000b
			lda		#002ch
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b

			rts
;*****************************************
;			SAVE SET
;*****************************************
SAVE_SET	equ		$
			lda		modeflg
			cmp		#01
			bne		owari_254
;----------------------------------
			lda		s.l_set
			cmp		#02
			bcs		owari_254
;----------------------------------
			lda		save_disp
			bne		save_now
;
;----------------------------------
			lda		trg_button1
			and		#0ffh
			cmp		#80h
			bne		owari_254
;----------------------------------
			lda		save_flg
			cmp		#0
			beq		owari_254
;----------------------------------
			ldx		#06				;now save display set.
			lda		#0
			sta     msbdat,x
			inx
loop_27		sta		msbdat,x
			inx
			sta     msbdat,x
;
			lda		#01
			sta		save_disp	
owari_254	rts
;-----------------------------------
save_now	stz		save_disp
;
			lda		s.l_set			;disk or ram save chose.
			cmp		#01
			beq		disc_set
;-----------------------------------------
;(RAM SAVE)
			lda		map_number
			sta		modecnt
set_save	asl		a
			tax
;
			idx16
			mem16
			rep		#00110000b
;
			lda		save_numb,x
			tax
			lda		!0,x
			sta		<addr
;
			mem8
			sep		#00100000b
;
			lda		!2,x
			sta		<addr+2
;
;-----------------------------------
			idx8						;70bank counter set.
			sep		#00010000b
			lda		s.l_set
			asl		a

			tay
			mem16
			rep		#00100000b
			lda		S.L_PLACE,y
			sta		s.l_bup
			mem8
			sep		#00100000b
			jmp		set_ff
;-----------------------------------			
;										;(disk save set.)
disc_set	idx8
			mem16
			rep		#00100000b
;
			lda		#0800h
			sta		<addr
;
			mem8
			sep		#00100000b
;
			lda		#0
			sta		<addr+2
;
;-----------------------------------
			ldy		#0
			mem16
			rep		#00100000b
			lda		S.L_PLACE,y
			sta		s.l_bup
			mem8
			sep		#00100000b
;-------------------------------------------			
set_ff		idx16
			rep		#00010000b

			ldy		s.l_bup				;FF(end) set.
			ldx		#0					;to 70bank.
			lda		#0ffh
loop_30		sta		[<addr],y
			iny
			inx
			cpx		#400h
			bne		loop_30
;-----------------------------------
									;(from 7e8200h
										;to 70bank.)
;
			stz		ycnt			
		    ldy     s.l_bup			
			ldx		#0
code_ck		lda		1600h,x
			cmp		#0ffh			;end check.
			beq		owari_22
;
			cmp		#0				;code check.
			beq		save_cnt
			cmp		#02
			bcs		save_cnt1
;
loop_22		lda		1600h,x
;
			sta		[<addr],y
			iny
			inx
;
			lda		savecnt		;loop counter set.
			inc		a
			sta		savecnt
			cmp		ycnt
			bne		loop_22
			stz		savecnt
;
			lda		ycnt			;inx check.
			cmp		#05
			beq		x_cnt5
			cmp		#04
			beq		x_cnt4
;	
ck_xcnt		cpx		#400h
			bne		code_ck
;
			lda		#05
			sta		ycnt
owari_22	idx8
			sep		#00010000b
;-------------------------------
			JSR		BGSAVE_SET			;(BG1 SAVE)
;--------------------------------
			lda		s.l_set				;SUM_CHECK?:no-->disk_set
			bne		disk_set
;			JSR		SUM_CHECK
;-----------------------------------
;(DISK SAVE SET)
disk_set	lda		s.l_set				;disk save?:no-->nodisk
			beq		nodisk
;
			JSR		SAVE_FILE1			;disk save rtoutine.
;
			lda		error_code2
			beq		nodisk
;
;
			idx16
			rep		#00010000b
			ldy		#0
			ldx		#102h	
loop_317	lda		no_disk3,y
			sta		oamdat,x
			iny
			inx
			inx
			inx
			inx
			cpy		#06
			bne		loop_317
			idx8
			sep		#00010000b
;
			ldx		#10h				;disk eroor message set.
			stz		msbdat,x
			inx
			stz     msbdat,x
			jmp		set_go2
;
save_cnt	jmp		save_cnt10
save_cnt1	jmp		save_cnt11
x_cnt5		jmp		x_cnt55	
x_cnt4		jmp		x_cnt45
;---------------------------------------------------	
nodisk		ldx		#10h				;disk error erase set.
			lda		#55h
			sta		msbdat,x
			inx
			sta     msbdat,x
;-----------------------------------
set_go2		mem8
			idx8
			sep		#00110000b
			ldx		#06					;display erase set.
			lda		#40h
			sta		msbdat,x
			lda		#55h
			inx
			sta     msbdat,x
			inx
			lda		#05
			sta     msbdat,x
			mem16
			rep		#00100000b
			lda		#002bh
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b

owari_30	rts
;---------------------------------
save_cnt10	lda		#05			;loop countre set.
			sta		ycnt
			jmp		loop_22
;
save_cnt11	bmi		owari_30
			cmp		#0ah
			bcs		save_cnt
			lda		#04
			sta		ycnt
			jmp		loop_22
;
x_cnt55		idx16				;x set.
			inx
			inx
			inx
			jmp		ck_xcnt	
;
x_cnt45		idx16				;x set.
			inx
			inx
			inx
			inx
			jmp		ck_xcnt	

;***************************************
;		LOAD SET
;***************************************
LOAD_SET	equ		$
			lda		modeflg
			cmp		#01
			bne		owari_30
;----------------------------------
			lda		s.l_set
			sec
			sbc		#02
			cmp		#02
			bcs		owari_30
;----------------------------------
			lda		blankck
			cmp		#8fh
			beq		vramset
;----------------------------------
			lda		trg_button1
			and		#0ffh
			cmp		#80h
			bne		owari_30
;-----------------------------------
;
			lda		#10001111b		;blanking on.
			sta		blankck
			rts
;;	
;--------------------------------------------
vramset		lda		#00000001b		;(!!!!!!!!)
			sta		NMItimen
;--------------------------------------------
			lda		s.l_set
			cmp		#03
			beq		vramset3
;-------------------------------------
;(RAM LOAD)
			lda		map_number			;(BANK & ADDRESS SET)
			asl		a
			tax
;
			idx16
			mem16
			rep		#00110000b
;
			lda		save_numb,x
			tax
			lda		!0,x
			sta		addr
;
			mem8
			sep	 	#00100000b
;
			lda		!2,x
			sta		addr+2
;
;-----------------------------------	
			idx8					;bank side counter.
			sep		#00010000b		
			lda		s.l_set
			sec
			sbc		#01
			asl		a
			tay
			mem16
			rep		#00100000b
			lda		S.L_PLACE,y
			sta		s.l_bup
			mem8
			sep		#00100000b
			jmp		setini
;---------------------------------------------------	
;(DISK LOAD)
vramset3	mem16
			rep		#00100000b
;
			lda		#0800h
			sta		addr
;
			mem8
			sep	 	#00100000b
;
			lda		#0
			sta		addr+2
;
;-----------------------------------	
			ldy		#0
			mem16
			rep		#00100000b
			lda		S.L_PLACE,y
			sta		s.l_bup
			mem8
			sep		#00100000b
;
			JSR		LOAD_FILE1		;DISK LOAD SET.
;------------------------------------------
			lda     #01h			;(!!!!!!!!)
		 sta     >4200h
;------------------------------------------
			lda		error_code2
			beq		setini
;
			asl		a
			tax			
			idx16
			mem16
			rep		#00110000b
			lda		error_disp1,x
			sta		<error1
			mem8
			sep		#00100000b
;
			ldy		#0
			ldx		#102h	
loop_319	lda		(<error1),y
			sta		oamdat,x
			iny
			inx
			inx
			inx
			inx
			cpy		#06
			bne		loop_319
			idx8
			sep		#00110000b
;
			idx16
			rep		#00010000b
			ldx		#0				;disk buffer clare.
			lda		#0ffh
loop_M5		sta		800h,x
			inx
			cpx		#400h
			bne		loop_M5
			stz		error_code2
			idx8
			sep		#00010000b
;
			ldx		#10h
			stz		msbdat,x
			inx
			stz     msbdat,x
			jmp		set_go
;
;---------------------------------------------------	

setini		ldx		#10h
			lda		#55h
			sta		msbdat,x
			inx
			sta     msbdat,x
;	
set_go		idx16					;(7f0000h inichal set)
			mem8
			rep		#00010000b
			ldx		#0
loop_101	lda		>7fc000h,x
			sta		>7f0000h,x
			inx
			cpx		#4000h
			bne		loop_101
;------------------------------------
			ldx		#0				;(FROM 7e8000H ZERO SET)
			lda		#0
loop_32		sta		1400h,x
			inx
			cpx		#200h
			bne		loop_32
;-------------------------------------
			ldx		#0				;(FROM 7e8200H #FFH set)
			lda		#0ffh
loop_906	sta		1600h,x
			inx
			cpx		#600h
			bne		loop_906
;
			ldx		#0				;(FROM 7e8200H #FFH set)
			lda		#03
loop_996	sta		1b00h,x
			inx
			cpx		#100h
			bne		loop_996

;-------------------------------------
			ldx		#0				;(from 70BANK to 7e8000H)
			ldy		s.l_bup
buffflg		lda		[<addr],y
			cmp		#0ffh
			beq		setform
;
			lda		#01
			sta		1400h,x
			inx
;
buff		lda		[<addr],y
			sta		1400h,x
			iny  
			inx
;
			lda		loadcnt
			inc		a
			sta		loadcnt
			cmp		#03
			bne		buff
			stz		loadcnt
;
			dey
			dey
			dey
			lda		[<addr],y
			beq		two_up1
			cmp		#02
			bcs		one_up1
;
set_buff	cpx		#200h
			bne		buffflg
;-------------------------------------
setform		stz		loadcnt
			stz		ycnt			;(from 70BANK to 7e8200H
		    ldy     s.l_bup			
			ldx		#0
code_ck1	lda		[<addr],y
			beq		save_cnt6
			cmp		#02
			bcs		save_cnt5
;
loop_320	lda		[<addr],y
;
			sta		1600h,x
			iny
			inx
;
			lda		loadcnt			;loop counter set.
			inc		a
			sta		loadcnt
			cmp		ycnt
			bne		loop_320
			stz		loadcnt
;
			lda		ycnt			;inx check.
			cmp		#05
			beq		x_cnt51
			cmp		#04
			beq		x_cnt41
;	
ck_xcnt1	cpx		#400h
			bne		code_ck1
;
owari_233	lda		#05
			sta		ycnt
;
			stz		loadcnt
			idx8
			sep		#00010000b

;-------------------------------------
			JSR		BG2LOAD_SET		;(FROM 70bank to 7f0000h)
			JSR		AREA_DATACNT
;--------------------------------------
;
			stz		owari_flg
			stz		areacnt1
			stz		pointcnt
;-------------------------------------
;
			JSR		SPDLOD_SET		;color set .buffer set.
			JSR		BGLOAD_SET		;BG1 (point)
;-------------------------------------
;			ldx		#0
;loop_911	lda		>7e8000h,x
;			cmp		#0ffh
;			beq		cntset
;			inx
;			inx
;			cpx		#0
;			beq		cntset1
;			jmp		loop_911
;cntset		txa		
;			lsr		a
;			sta		<setcnt
						
blnkset		lda		#0				;blanking off.
			sta		blankck		;Inidisp
;
			stz		fade_mode		;fade in set
;-----------------------------------------------
			lda		#10000001b		;(!!!!!!!!)
			sta		NMItimen
;------------------------------------------------
			mem16
			rep		#00100000b
			lda		#002bh
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b
owari_39	rts
two_up1		jmp		two_up
one_up1		jmp		one_up
save_cnt5	jmp		save_cnt3
save_cnt6	jmp		save_cnt2
;-----------------------------------
;cntset1		lda		#80h
;			sta		setcnt
;			jmp		blnkset
;-----------------------------------
two_up		iny
			iny
			iny
			iny
			iny
			jmp		set_buff		
;
x_cnt51		jmp		x_cnt09
x_cnt41		jmp		x_cnt08
one_up		cmp		#0ah
			bcs		two_up
			iny
			iny
			iny
			iny
			jmp		set_buff
;------------------------------------
save_cnt2	lda		#05			;loop countre set.
			sta		ycnt
			jmp		loop_320
;
save_cnt3	bmi		owari_233
			cmp		#0ah
			bcs		save_cnt2
			lda		#04
			sta		ycnt
			jmp		loop_320
;
x_cnt09		idx16				;x set.
			inx
			inx
			inx
			jmp		ck_xcnt1	
;
x_cnt08		idx16				;x set.
			inx
			inx
			inx
			inx
			jmp		ck_xcnt1	

;		
;*****************************************
;			CLEAR SET
;*****************************************
CLEAR_SET	equ		$
			lda		modeflg
			cmp		#01
			bne		owari_39
;----------------------------------
			lda		s.l_set
			cmp		#04
			bne		owari_39
;----------------------------------
			lda		blankck
			cmp		#8fh
			beq		vramset1
;----------------------------------
			lda		trg_button1
			and		#0ffh
			cmp		#80h
			bne		owari_39
;-----------------------------------
			lda		#10001111b		;blanking on.
			sta		blankck;Inidisp
			rts
;
vramset1	lda		#00000001b		;(BG2 claer set)
			sta		NMItimen
;-------	
;			idx16					;(7f0000h inichal set)
;			mem8
;			rep		#00010000b
;			ldx		#0
;loop_100	lda		>7fc000h,x
;			sta		>7f0000h,x
;			inx
;			cpx		#4000h
;			bne		loop_100
;------------------------------------
;			ldx		#0				;(7e8000H ZERO SET)
;			lda		#0
;loop_35		sta		>7e8000h,x
;			inx
;			cpx		#100h
;			bne		loop_35
;
;			ldx		#0				;(7e8200h #ffh set)
;			lda		#0ffh
;loop_425	sta		>7e8200h,x
;			inx
;			cpx		#400h
;			bne		loop_425
;
;			stz		<areacnt		;area counter zero set.
;
;			idx8
;			mem8
;			sep		#00110000b
;---------------------------------------------
			JSR		BG1CLAR_SET
;---------------------------------------------
			idx8
			mem8
			sep		#00110000b
;
			stz		setcnt			;terget counter zero set
;
			lda		#0		;blanking off.
			sta		blankck		;Inidisp
;
			lda		#10000001b
			sta		NMItimen
;
			stz		fade_mode
;-----------------------------------	
			rts			
;
;****************************************
;			END
;****************************************
END_SET		equ		$
;----------------------------------
			lda		button
			cmp		#0b0h
			bne		owari_44
;-----------------------------------
;
			idx16
			mem8
			rep		#00010000b
;
			ldx		#0				;(7f8000H ZERO SET)
			lda		#0
loop_44		sta		>7f8000h,x
			inx
			cpx		#100h
			bne		loop_44
;
			idx8
			mem8
			sep		#00010000b
;-------------------------------------

			lda		#10001111b		;blanking on.
			sta		blankck
owari_44	rts
;
			END



