;******************* TEST1.asm *************************
;
			extend
;-----------------------------------------------------
			include		../join/rp5c77
			include		../join/buffer.def
			include		../join/rp5a22
			include     ../join/work.def
			include		../join/label.def
			include		../join/object.def
;-----------------------------------------------------
			glb		TITL_init,TITL_main,TITL_nmi
			glb		Set_DMA,PPU_INT_SET,Zero_clear
			glb		Ending2_int_set,OBJ_ERASE_SET
;
			ext		KEY_CHECK,Init_demokart,Set_demokart
			ext		DMA_demokart,Trigger_sound_1,Random
			ext		BGM_fade_out,Y_iny,X_inx,Backup_Sam_Check
			ext		Color_Set_sub,Back_up_clear
;----------------------------------------------------
oamdat				equ		200h		
msbdat				equ		400h
window_change		equ		180h
;-----------------------------------------------------
select_flag			equ		80h
window_open			equ		81h
addr				equ		83h
Game_select.flag	equ		85h
Counter_Comp		equ		86h	
Koura_speed			equ		88h
Banana_flag			equ		8ah
Moji_counter_cmp	equ		8eh
;
Start_position		equ		70h
Moji_counter		equ		77h
Moji_place			equ		79h
erase_set_flag		equ		7bh
Under_display_flag	equ		7dh
;
Clare_buffer		equ		60h
;
Flag_swing			equ		73h		;2byte
Swing_count			equ		75h		;2byte
button_TG			equ		6ah		;4byte
;
;
;--------(DEBUG)------------
Personal_player		equ		8ch
Button_timer		equ		9eh
Rank_set			equ		62h
;
;
;
; - - - - - - - - - - - - - - - - - - - - - -
_demotimer			equ		40h		;2byet
_demoflag			equ		42h		;2byte
_swing_position		equ		44h		;2byte
;
Special_Cup    		 equ     3067f5h
;-----------------------------------------------------
bank85			sect	rel			;(PBR=85)
;**********************************************************
;			INITIAL ROUTINE
;**********************************************************
;==========================================================
;
TITL_init		EQU		$
				phb
				phk
				plb		
;
				ldy		#OBJ_data2
				JSL		>Init_demokart
				stz		1f2ah
				stz		g_kart_flag
;
				ldx		#1000h					;チビクリアー
Chibi_clear_loop
				stz     kart_status,x
				txa
				clc
				adc		#0100h
				tax
				cmp		#1800h
				bne		Chibi_clear_loop
;
				mem8
				idx8
				sep		#00110000b
				JSL		>PPU_INT_SET
				idx16
				mem16
				rep		#00110000b
;
				JSL		>Backup_Sam_Check
				mem8
				idx8
				sep		#00110000b
;
				JSR		TENSOU_SET
				JSR		PPU_title
;
				idx16
				mem16
				rep		#00110000b
;
				JSR		OBJ_ERASE
;------------------------------------------
;
				lda		#3800h
				sta		<Personal_player		;<-----デバック
				stz		<Button_timer
				stz		<Rank_set
;-------------------------------------------
;
				plb
				RTL
;
;===========================================================											
;***********************************************************
;				MAIN ROUTINE
;***********************************************************
;===========================================================
;(TITLE SET)
;
TITL_main		EQU		$	
				phb
				phk
				plb		
;
				JSL		>Random
				JSR		KEY_CHECK
;
				lda		<erase_set_flag
				bne		owari_M8
;
				mem8
				idx8
				sep		#00110000b
;
				JSR     FLAG_CHECK
;
				lda		<select_flag
				and		#00000111b
				asl		a
				tax
				JMP		(!select_window,x)
;--------------------------------------------
Title_Set		JSR		TITLE_SCROLL
				mem16
				idx16
				rep		#00110000b
				lda		#200h
				sta		<sprite_count
				JSR		OBJ_DEMO
				JSL		>Set_demokart
				mem8
				idx8
				sep		#00110000b
				JMP     owari_M8
;--------------------------------------------
;(MODE SELECT SET)
MODE_0			equ		$
				JSR		WINDOW_SET
				JSR		GAME_SELECT
				JMP		owari_M8
;
MODE_4			equ		$
				JSR		WINDOW_SET
				JSR		ALL.END_CHECK
;
owari_M8		mem16
				idx16
				rep		#00110000b
				lda		<select_flag
				bne		owari_main_title
				JSL		>Back_up_clear
;---------------------------------------------------
				lda     rom_checker	;ファイナルデバック用
				beq		owari_main_title
;
				JSL		>SET_debug		;ファイナルデバック用
owari_main_title
				plb
				RTL
;

;
;------------------(ファイナルデバック用)-------
;
editer			sect	rel			;(PBR=10)
SET_debug		phb
				phk
				plb
;
;;				
;				lda		#8020h
;				sta		oamdat+0c0h
;				lda     Personal_player
;				sta		oamdat+0c2h
;				lda		#9020h
;				sta		oamdat+0c4h
;				lda		world_number
;				ora		#3a00h
;				sta     oamdat+0c6h
;				lda		#0a020h
;				sta     oamdat+0c8h
;				lda		<Rank_set
;				ora		#3a00h
;				inc		a
;				sta		oamdat+0cah
;
				inc		<Button_timer
				lda		<Button_timer
				cmp		#0008
				bne		sos1			
				stz		<Button_timer
;
				lda     4218h
				cmp		#06c0h
				beq		set_debug
				cmp		#00a0h
				beq		Personal_set
				cmp		#4020h
				beq		Rank_change
				cmp		#0060h
				bne		sos1

				inc		world_number
				lda		world_number
				cmp		#0004
				beq		reset_world
				jmp		sos
reset_world		stz		world_number
sos1			jmp     sos

Personal_set	inc		<Personal_player
				inc		<Personal_player
				lda     <Personal_player
				cmp		#3810h
				beq		reset_kart_set
				jmp		sos
reset_kart_set	and		#0ff00h
				sta		Personal_player
				jmp		sos		
;
Rank_change		inc		<Rank_set
				lda		<Rank_set
				cmp		#0008h
				beq		Reset_rank
				jmp		sos
Reset_rank		stz		<Rank_set
				jmp     sos
;
set_debug		ldx		#0
loop_debug		lda		Test_rank_data,x
				sta		point_rank,x
				inx
				inx
				cpx		#0010h
				bne		loop_debug
;
				lda		<Rank_set
				asl		a
				tax
				lda		#1000h
				sta		point_rank,x
;;
				ldx		#1000h
				lda		Personal_player
				and		#00ffh
				sta		<driver_number,x
				ldx		#1100h
				lda		#0002h
				sta		<driver_number,x
				ldx		#1200h
				lda		#0004h
				sta		<driver_number,x
				ldx		#1300h
				lda		#0006h
				sta		<driver_number,x
				ldx		#1400h
				lda		#0008h
				sta		<driver_number,x
				ldx		#1500h
				lda		#000ah
				sta		<driver_number,x
				ldx		#1600h
				lda		#000ch
				sta		<driver_number,x
				ldx		#1700h
				lda		#000eh
				sta		<driver_number,x
;
				lda		#0018h		
				sta		game_selecta		;K-select set.
				lda		#8f00h
				sta		fade_control		;fdadeout set.
sos				plb
				RTL

Test_rank_data  DW      1100h,1200h,1300h,1400h
				DW		1500h,1600h,1700h,1100h
;--------------------------------------
;
bank85			sect	rel			;(PBR=03)
;*****************************************;
;				NMI wari komi.			  ;
;*****************************************;
TITL_nmi		equ		$
				phb
				phk
				plb		
;
				mem8
				idx8
				sep		#00110000b
;-------------------------------------------
;(SCLLOR SET)
;
				lda		<back_2h		;BG1 scroll set:low
				sta		>Scroll_1H
;
				lda		<back_2h+1		;BG1 scroll set:Hi
				sta		>Scroll_1H
;--------------------------------------------
				lda		#02
				sta		DMA_synchronic
;
				mem16
				idx16
				rep		#00110000b
;
				JSL		>DMA_demokart
				plb
				RTL
;
;========================================================
;********************************************************
;				イニシャル　ルーチン
;********************************************************
;=========================================================
;
;**********************************************
;			キャラクター　、スクリーンの転送
;**********************************************
TENSOU_SET		equ		$					
				idx8
				mem8
       		 	lda	 	#80h
       		 	sta    	>Screen_step
;/////////////// BG_1.2 キャラクター転送 //////////////////
   		 	 	ldx		#00h
				jsr		Set_DMA				;character data.
;/////////////// BG1 スクリーン転送 ////////////////////////
   			 	ldx		#02h
				jsr     Set_DMA				;character data.
;/////////////// BG2 スクリーン転送 ////////////////////////
    		 	ldx		#04h
				jsr     Set_DMA				;character data.
;/////////////// OBJ キャラクター転送 //////////////////////
				ldx		#06
				jsr     Set_DMA
;/////////////// BG3 スクリーン転送 ////////////////////////
	 	     	idx16						;16bit mode.
	   	     	mem16
	   	     	rep     #00110000b
;
  	   		 	lda		#1c00h				;Vram adrr hi.
	    	 	sta	    Screen_address
;
 	 		 	ldx		#0					;character data.
			 	lda		#1c00h
loop9		 	sta		>Screen_write
			 	inx
			 	inx
			 	cpx		#800h				;1000? :no-->lopp1.
			 	bne		loop9
;//////////////// BG_3 キャラクター転送 //////////////////////
	 	 		ldx		#0					;character data.
			 	lda		#00ffh
loop911		 	sta		Screen_write
			 	inx
			 	inx
			 	cpx		#400h				;1000? :no-->lopp1.
			 	bne		loop911
			 	mem8
			 	sep		#00100000b
;//////////////// カラー転送 ////////////////////////////////
CG_SET		 	equ		$				;(CG_DATA TENSOU)
;
			 	lda		#00000000b			
				sta		Color_address
;
				ldy		#0
				ldx		#4000h
tite_color		JSR		Color_Set_sub
				cpy		#0200h
				bne		tite_color
;///////////////// OAM SET /////////////////////////////////	
				idx16
			 	mem16
			 	rep		#00110000b
;
				lda		#804ch
				sta		oamdat+100h
				lda		#0c9dh
				sta		oamdat+102h
				sta     oamdat+106h
				sta     oamdat+10ah
				sta     oamdat+10eh
;
				ldy		#0
				ldx		#0
				lda		#805ch
				sta		Moji_place
				lda		#805ch
				JSR		Player_set
;
				ldx		#0002
				lda		#905ch
				sta		Moji_place
				JSR		Player_set
			 	sep		#00110000b
			 	mem8
			 	idx8
			 	rts
;

Moji_character_set
				mem16
				idx16
				tax
Moji_loop		lda		!0,x		
				bne		NO_Space_set
				lda     Start_position
				clc
				adc		#0008
				sta     Start_position
				jmp		Space_set
;
NO_Space_set 	sta		oamdat+112h,y
				and		#00ffh
				cmp		#002eh
				beq		dakuten_set
				cmp		#002fh
				beq     dakuten_set
;
				lda		Start_position
				sta		oamdat+110h,y
				clc
				adc		#0008
				sta		Start_position
Set_next_position
				JSR		Y_iny
Space_set		inx
				inx
				inc		Moji_counter
				lda		Moji_counter
				cmp		Moji_counter_cmp	
				bne		Moji_loop
				stz		Moji_counter
				rts
;
dakuten_set		lda     Start_position
				sec
				sbc		#0706h
				sta     oamdat+110h,y
				jmp		Set_next_position
;/////////////////// DAM SET //////////////////////////////
Set_DMA			equ		$
				mem8
				lda		#01h			; low,high 2byte
				sta		DMA_0
; 
				lda		#18h			; 2118h
				sta		DMA_0+1
;
				lda		Abus_address,x	
				sta		DMA_0+2
;
				lda		Abus_address+1,x
				sta		DMA_0+3
;
				lda		#7fh
				sta		DMA_0+4
;
				lda		DMA_counter,x			
				sta		DMA_0+5			
				lda		DMA_counter+1,x
				sta		DMA_0+6
;
				lda		Vram_address,x	
				sta		Screen_address
				lda		Vram_address+1,x
				sta		Screen_address+1
				lda		#01h
				sta		DMA_burst
				rts
;-----------------------------------------------------
;
Abus_address	DW		00000h,02000h,02800h,0a000h
				DW		0c000h,0c800h,0d800h,0ea00h
				DW		03800h,04400h,0f900h,00000h
				DW		03800h,0c000h,00000h,04000h
				DW		03800h,0a400h,0a400h
;
DMA_counter		DW		2000h,0800h,1000h,2000h
				DW		0800h,0800h,0800h,0500h
				DW		0800h,1000h,0700h,2000h
				DW		0800h,3000h,2800h,0800h
				DW		0800h,1000h,1800h
;
Vram_address	DW		0000h,1000h,1400h,4000h
				DW		2400h,2800h,2c00h,3000h
				DW		1000h,0000h,0800h,2000h
				DW      1c00h,0000h,4000h,6000h
				DW		6400h,5800h,0600h
;**************************************************************
;					PPU &  WORK SET
;**************************************************************
PPU_title		equ		$					
				mem8
				idx8
;////////// Game_select.flag のイニシャルセット //////////////
				lda		game_level				
				beq		set_player				;クラスチェック
				cmp		#04
				beq		Set_150cc_class
				lda		#02
				sta		<Game_select.flag		;100cc クラス
				jmp		set_player
;
Set_150cc_class	lda		>Special_Cup
				and		#03
				cmp		#01
				bne		set_player
				lda		#04
				sta    	<Game_select.flag       ;150cc クラス
;
set_player		lda		<game_mode
				bne		set_oneplayer			;プレイヤーチェック			
;-----------------(二人用ゲームセット)-----------------
				lda		<Game_select.flag
				ora		#01
				sta		<Game_select.flag		;二人用ゲームセット
;
				lda		<game_status
				beq		set_check				;ゲーム内容チェック
;
				cmp		#02
				beq		vs_set					
;
				lda     <Game_select.flag
				ora     #10h
				sta     <Game_select.flag		;battle セット.
				jmp		set_check
;
vs_set			lda     <Game_select.flag
				ora     #08
				sta     <Game_select.flag		;VS セット.
				jmp     set_check
;-------------------(一人用ゲームセット)---------------------
set_oneplayer	lda     <game_status
				beq		set_check				;ゲーム内容チェック
				lda		<Game_select.flag
				ora		#20h
				sta		<Game_select.flag		;time attack セット
;//////////////// 文字とキノコの表示 //////////////////////////////////
set_check		lda		<game_mode
				cmp		#02h
				bcs		Set_PPU					;1player?:yes-->Set_PPU
;
				lda		#01
				sta		<select_flag
				JSR     MOJI_SET				;NOKO set.
;
				stz		<select_flag			;reset flag.
;////////////////// PPU SET ///////////////////////////////////////////
Set_PPU			lda		#00000001b
				sta		Screen_size		;BG mode1 set.
;
				lda		#00010111b
				sta		Through_screen		;TM
;
				lda		#00100010b			;W12SEL
				sta		Window_control		
;
				lda		#00000011b
				sta		Through_window
;
				lda		#00010000b
				sta		Screen_bank
;
				lda		#00010101b
				sta		Screen_bank+1
;
				lda		#00011100b
				sta		Screen_bank+2
;
				lda		#0
				sta		Screen_segment
				lda		#00100010b
				sta		Screen_segment+1
;
				lda		#00000010b			;4000h
				sta		Sprite_size
;
				lda		#10000000b
				sta		Screen_step
;///////////////// H_DMA SET //////////////////////////////////
				lda		#01000001b			;2address set.
				sta		DMA_1
;
				lda		#00100110b			;to 2126h
				sta		DMA_1+1
;
				lda		#10000000b			;from 180h
				sta		DMA_1+2
;
				lda		#00000001b
				sta		DMA_1+3
;
				lda		#0
				sta		DMA_1+4
;
				lda		#0
				sta		DMA_1+5
				sta		DMA_1+6
				lda		#85h
				sta		DMA_1+7
;////////////////// ウインドウのデーターセット //////////////////
				ldx		#0	
loop_323		lda		MODE_WIN,x
				sta		window_change,x
				inx
				cpx		#0dh
				bne		loop_323			;19?:no-->loop_323
				rts
;---------------------------------------------------
MODE_WIN		DB			2ch
				DW			NO_WINDOW       
				DB			60h
				DW          NO_WINDOW				
				DB			28h
				DW          NO_WINDOW				
				DB			70h
				DW          NO_WINDOW				
				DB			0
;-------------------------------------------------------------
;
WINDOW_SIZE		DW			NO_WINDOW,SIZE_0,SIZE_1,SIZE_2
				DW			SIZE_3,SIZE_4,SIZE_5,SIZE_6
				DW			SIZE_7,SIZE_8,SIZE_9,SIZE_a
				DW			SIZE_b
;- - - - - - - - - - - - - - - - - - - - - - - - - - - -
;------(横開きのダーター)------
NO_WINDOW		DB			01h,000h
SIZE_0			DB			7fh,081h
SIZE_1			DB			7ch,084h
SIZE_2			DB			78h,088h
SIZE_3			DB			74h,08ch
SIZE_4			DB			70h,090h
SIZE_5			DB			6ch,094h
SIZE_6			DB			68h,098h
SIZE_7			DB			60h,0a0h
SIZE_8			DB			58h,0a8h
SIZE_9			DB			50h,0b0h
SIZE_a			DB			48h,0b8h
SIZE_b			DB			40h,0bch
;---------------------------------------------------------------
;-------(縦開きのダーター)------
WINDOW_SIZE1	DW			WINDOW_0,WINDOW_0,WINDOW_1
				DW			WINDOW_0,WINDOW_0,WINDOW_2
				DW			WINDOW_3,WINDOW_1
;
WINDOW_0		DB			00h,2ch,02h,2bh,04h,2ah,08h,28h
				DB			0ch,26h,10h,24h,14h,22h,18h,20h
				DB			1ch,1eh,20h,1ch,24h,1ah,28h,18h,28h,18h
;
WINDOW_1		DB			00h,34h,04h,32h,08h,30h,10h,2ch
				DB			14h,2ah,18h,28h,20h,24h,24h,22h
				DB			28h,20h,30h,1ch,34h,1ah,38h,18h,38h,18h
;
WINDOW_2		DB			00h,3bh,08h,37h,10h,33h,18h,2fh
				DB			20h,2bh,28h,27h,30h,23h,38h,1fh
				DB			40h,1bh,48h,17h,50h,13h,54h,12h,56h,10h
;
WINDOW_3		DB			00h,34h,04h,30h,08h,2ch,10h,24h
				DB			18h,20h,20h,1eh,28h,1ch,30h,1ah
				DB			38h,18h,3ch,16h,40h,14h,44h,12h,48h,10h
;---------------------------------------------------------------
select_window	DW			Title_Set,MODE_0,MODE_0,MODE_0			
				DW			MODE_0,MODE_4,MODE_4
;---------------------------------------------------------------
select_position	DW			 Select_0, Select_1
				DW			 Select_2,Select_3
;---------------------------------------------------------------
select_noko		DW			NOKO_1,NOKO_1,NOKO_2,NOKO_3,NOKO_4,NOKO_5
				DW			NOKO_5
;---------------------------------------------------------------
;-----(player data)-----
ONE_PLAY		DW			0801h,087bh,082fh,0889h,0861h
				DW			0883h,089ch,0000h,0868h,082eh
				DW			089ch,0880h
;
TWO_PLAY		DW          0a02h,0a7bh,0a2fh,0a89h,0a61h
				DW          0a83h,0a9ch,0000h,0a68h,0a2eh
				DW          0a9ch,0a80h
;------(game data.2play)--------
MARIO_GP		DW			087eh,0887h,0864h,0865h,089ch
				DW			0873h,0820h,0821h
;
VS_RACE			DW			0a22h,0a23h,0a7eh,0a97h,0a70h
				DW			0a89h,0a9ch,0a6ch
;
BATTLE_GAME		DW			0c79h,0c2eh,0c73h,0c88h,0c68h
				DW			0c2eh,0c9ch,0c80h
;------(game data.1play)--------
MARIO_GP1		DW          087eh,0887h,0864h,0865h,089ch
				DW          0873h,0820h,0821h
;
TIME_ATTCK		DW			0a6fh,0a61h,0a80h,0a60h,0a6fh
				DW			0a97h,0a67h
;-----(class data)-------
KIDS_CLASS		DW			0805h,0800h,082ah,0000h,0867h
				DW			0886h,086ch
;  
ADULT_CLASS		DW			0a01h,0a00h,0a00h,0a2ah,0000h
				DW			0a67h,0a86h,0a6ch
;
SPECIAL_CLASS	DW			0c01h,0c05h,0c00h,0c2ah,0000h
				DW			0c67h,0c86h,0c6ch
;			
OK_CHECK		DW			0a868h,0a39h
				DW			0a870h,0a59h
				DW			0a878h,0a42h
				DW			0a07ah,0a2eh
				DW			0a888h,0a31h
				DW			0a890h,0a31h
				DW			0a898h,0a42h
				DW			0a09ah,0a2eh
				DW			0a8a0h,0a3ch
				DW			0a8a8h,0a35h
				DW			0a8b0h,0a24h		 			   
;
				DW			0b878h,0a49h
				DW			0b880h,0a31h
				DW			0b898h,0a31h
				DW			0b8a0h,0a31h
				DW			0b8a8h,0a33h
;
				DW			0b870h,085eh
				DW			0b888h,485eh
				DW			0ffffh
;- - - - - - - - - - - - - - - - - - - - - - - - - - -
Ending2_int_set	equ			$
				phb
				phk
				plb
				JSL		>PPU_INT_SET
				JSR		Set_PPU
;
				plb
				rtl
;======================================================================
;********************************************************************
;					メインルーチン　サブ
;********************************************************************
;======================================================================
;******************************************************
;					スクロールセット
;******************************************************
TITLE_SCROLL	equ		$
				mem16
				idx8
				rep		#00100000b
;
				inc		<back_2h
;
				idx8
				mem8
				sep		#00100000b
;
owari_605		rts
;********************************************************
;					ウインドウのセレクトチェック
;********************************************************
FLAG_CHECK		equ		$
				lda		<select_flag
				and		#00110000b
				bne		owari_405					;ウインドウセット中?:yes-->owari_405	
;--------------------------------------------
				lda		<button_TG+1
				ora		<button_TG+3
				and		#10010000b
				bne		mode_up						;mode_up?yes-->mode_up
;--------------------------------------------
				lda		<button_TG
				ora		<button_TG+2
				and		#01000000b	
				bne		mode_dwn1					;mode_dw?yes-->mode_dwn
owari_405		rts
;/////////////////// MODE UP セット ////////////////////
mode_up			lda		<Game_select.flag
				and		#00000001b
				bne		opne_window
;
				lda     <button_TG+3
				and     #10010000b
				beq		opne_window
;	
				lda     <select_flag				;２コントローラー
				beq		opne_window
				cmp		#01							;使用チェック
				beq		Check_L.R
;
				lda		Under_display_flag
				beq		owari_405
				jmp		opne_window
mode_dwn1		jmp		mode_dwn
;
Check_L.R		lda     Under_display_flag
				cmp		#01
				beq		opne_window
				
				lda		button_status+2
				cmp		#30h
				bne		owari_405
				lda		#01
				sta		Under_display_flag
;
opne_window		mem16
				idx16
				rep		#00110000b
				lda		#200h						;オブジェを消す
				sta		<sprite_count
				JSR		OBJ_ERASE
				JSL		>Set_demokart
				mem8
				idx8
				sep		#00110000b
;
				lda		<Game_select.flag			;最終チェック.
				and		#10000000b
				bne		Reset_Select				
;
				mem16
				idx16
				rep		#00110000b
				lda		#002eh
				JSL		>Trigger_sound_1			;サウンドセット
				mem8
				idx8
				sep		#00110000b
;
				lda		<select_flag
				inc		a							;モードアップセット
;
				cmp		#06
				beq		Kart_Selelct				;インデックスセット
				cmp		#07
				beq     Kart_Selelct
;
				cmp		#01							;一番最初のウインドウ?:yes-->set_open
				beq		set_open					;ウインドウオープンのみセット
;/////// ウインドウクローズ、モードアップのフラグセット //////
set_close		lda		<select_flag
				ora		#60h						;windoe close bit set.		
				sta		<select_flag		
				jmp		owari_M1
;//////////// 最終チェックが”いいえ”の時 ///////////////////
Reset_Select	mem16
				idx16
				rep		#00110000b
				lda		#002fh
				JSL		>Trigger_sound_1			;サウンドセット
				mem8
				idx8
				sep		#00110000b
;
				lda		<Game_select.flag			;window close Player select
				and		#01111111b
				sta		<Game_select.flag
				lda		<select_flag
				cmp		#06
				beq		Set_small_close		
;
				lda		#65h						;mode set.
				sta		<select_flag
				jmp		owari_M1
;
Set_small_close	lda		#66h
				sta     <select_flag
				jmp     owari_M1
;//////// ウインドウオープンとモードアップフラグセット ///////
set_open		lda		<select_flag				;ウインドウオープンとモードアップセット.
				ora		#51h
				sta		<select_flag
owari_M1		rts
;//////////////// モードダウンセット /////////////////////////
mode_dwn		lda		<Game_select.flag			;最終チェック.
				and		#10000000b
				bne		owari_323
;
				lda		<select_flag
				beq		owari_323					;タイトル画面?:yes-->owari_323
Set_Close		ora		#0a0h						;ウインドウクローズとモードダウン.
				sta		<select_flag
				mem16
				rep		#00100000b
				lda		#002fh
				JSL		>Trigger_sound_1
				mem8
				sep		#00100000b
owari_323		rts
;/////////////////// 最終フラグセット ///////////////////////
;
;--------(排気リョウセット)--------
Kart_Selelct	lda		<Game_select.flag
				pha
				and		#00000110b
				sta		game_level					;100cc class set.
;------(二人用セット)--------
Player_Set		pla
				pha
				and		#00000001b
				beq		One_Player					;2player?:no-->One_Player
;
				stz		<game_mode					;2player set.
;-----(ゲーム選択セット)------
				pla		
				and		#00011000b
				beq		Mario_GP					;GP?:yes-->Mario_GP
;	
				cmp		#08
				beq		VS_mode						;VS_mode?:yes-->VS_mode
;
				lda		#06h
				sta		<game_status				;battle set.
				jmp		End_Title
;
Mario_GP		stz     <game_status				;GP set.
				jmp		End_Title
;
VS_mode			lda		#02							;VS set.
				sta		<game_status
				jmp		End_Title
;-------(一人用セット)--------
One_Player		pla
				and		#00100000b
				bne		Time_Attack					;Time_Attack?:yes-->Time_Attack
;
				stz		<game_status				;GP set.
				jmp     Set_game_mode
;
Time_Attack		lda		#04
				sta		<game_status				;time attack set.
;
Set_game_mode	lda		<button_TG+1
				bne		Up_dispaly
				lda		#04
				sta		<game_mode
				jmp		End_Title
Up_dispaly		lda		#02
				sta		<game_mode
;---(カートセレクト用のフラグセット)------
End_Title		mem16
				rep		#00100000b
				JSL		>BGM_fade_out
				mem8
				sep		#00100000b
				lda		#06		
				sta		game_selecta				;K-select set.
				lda		#8fh
				sta		fade_control+1				;fdadeout set.
				stz     race_init
				stz     race_init+1
				rts
;******************************************************
;				ウィンドウの開き閉めデーターセット
;******************************************************
WINDOW_SET		equ		$
				lda		<select_flag
				and		#00110000b
				cmp		#10h
				beq		wind_openset		;ウインドウオープンセット?:yes-->wind_openset
;
				cmp		#20h
				beq		window_closeset		;ウインドウクローズセット?:yes-->window_closeset
				rts
;/////////////// ウインドウオプンセット ////////////////
wind_openset	lda		<window_open		;ウインドウの大きさカウンターセット。
				cmp		#0ch
				beq		noset_window
				inc		a
				sta		<window_open
				asl		a
				tax
				jmp		set_window
;/////////////// ウインドウクロズセット ////////////////
window_closeset	lda		<window_open		;ウインドウの大きさカウンターセット。
				beq		noset_window1
				dec		a
				sta		<window_open
				asl		a
				tax
;/////////////// 横開きのデーターセット ////////////////
set_window		mem16
				rep		#00100000b
				lda		WINDOW_SIZE,x
;
				ldx		#0007
				sta		window_change,x	
				mem8
				sep		#00100000b
;/////////////// 縦開きのデーターセット /////////////////
				lda		<select_flag
				and		#00000111b
				cmp		#04
				beq		Check_150cc
Set_window_data	asl		a
				tax
;
				mem16
				idx16
				rep		#00110000b
				lda		<window_open
				asl		a
				clc
				adc		WINDOW_SIZE1,x
				tax
				phx
;
				mem8
				sep		#00100000b
				lda		!0,x
				idx8
				sep		#00010000b
;
				ldx		#06
				sta		window_change,x
;------------------------------------------------
				idx16
				rep		#00010000b
				plx
				lda		!1,x
				idx8
				sep		#00010000b
;
				ldx		#0
				sta		window_change,x
				rts
;<<<<<<<<<<<<<<< １５０ｃｃクラスのチェック >>>>>>>>>>>>>
Check_150cc		lda		>Special_Cup
				and		#03h
				cmp		#01
				bne		Standard_window_data
				lda		#07
				jmp		Set_window_data
Standard_window_data
				lda		#04
				jmp     Set_window_data
;////////////// ウインドウセットの中止 //////////////////
noset_window	lda		<select_flag
				and		#00000111b			;window set off.
				sta     <select_flag
				rts
;///////// ウインドウオープンとモードアップダウンセット ///////
noset_window1	lda		<select_flag
				pha
				and		#00000111b
				beq		noset_window
				ora		#10h				;ｳｲﾝﾄﾞｳｵﾌﾟﾝｾｯﾄ.
				sta		<select_flag
;
				pla
				and		#11000000b
				cmp		#40h				;モードアップ?:yes-->mode_upset
				beq		mode_upset
;//////////////////// モードダウンセット ////////////////////// 
				lda		<select_flag		
				dec		a					;モードダウンセット.
				and		#00010111b
				sta     <select_flag
;
				cmp		#10h	
				beq		NOKO_replace		;タイトル画面セット
;
				cmp		#12h				;二人用ゲームセレクトのジャンプ
				beq		jump_tow_player
;
				cmp		#13h
				beq		Check_game
;
				cmp		#15h
				beq		end_back
				jmp     owari_205
;-------------------------------------------------
Check_game		lda		<Game_select.flag	;人数チェック
				and		#00000001b
				beq		owari_205			
				jmp		jump_tow_player
;
end_back		dec		<select_flag
				lda		<Game_select.flag	;人数チェック
				and		#00000001b
				beq		jump_tow_player
				dec		<select_flag			
;			
jump_tow_player	dec		<select_flag		;モードダウンセット.
				jmp     owari_205
;--------------------------------------------------
NOKO_replace	stz		Under_display_flag	;２コン使用禁止フラグ
				JSR		MOJI_SET		
				jmp		noset_window
;/////////////////// モードアップセット ////////////////////
mode_upset		lda		<select_flag		
				inc		a					;モードアップセット.
				and		#00010111b
				sta     <select_flag
;
				cmp		#12h				;一人用か二人用のゲームセレクト
				beq		game_choose
;
				cmp		#13h
				beq		class_choose
				cmp		#14h		
				beq		class_choose			
;
				cmp		#16h
				beq		Reset_player
				cmp		#17h
				beq		Reset_player
				jmp		owari_205
;------------------------------------------
Reset_player	lda		#11h				;Player select mode set.
				sta		<select_flag
				jmp     owari_205
;-------------------------------------------
game_choose		lda		<Game_select.flag	;人数チェック
				and		#00000001b
				bne		owari_205			
				jmp		jump_class
;
class_choose	lda     <Game_select.flag	;人数チェック
				and		#00000001b
				beq		one_game_set		
;
				inc		<select_flag
				lda		<Game_select.flag	;二人用ゲーム内容チェック
				and		#00011000b
				beq		owari_205			
				jmp		Small_window
;
one_game_set	lda		<Game_select.flag	;一人用ゲーム内容チェック
				and		#00100000b
				beq		owari_205
;
Small_window	inc     <select_flag
jump_class		inc		<select_flag
;			
owari_205		lda		<select_flag
				JSR     MOJI_SET					;moji data set.
				rts
;***************************************************
;					選択セット
;***************************************************
GAME_SELECT		equ		$
				lda     <select_flag				;ウインドウ選択中?:yes-->owari_309
				and		#11110000b
				bne		owari_309
;------------------------------------------------
				lda		<button_TG+1
				ora		<button_TG+3
				and		#00101100b
				beq		owari_309
;------------------------------------------------
				pha
				lda		<select_flag
				dec		a
				asl		a
				tax
				jmp		(!select_position,x)
owari_309		rts
;//////////////// プレイヤーセレクト ///////////////////
Select_0		pla  								;ボタンチェック。
				cmp		#04
				beq		down_0
				cmp		#08
				beq		up_0
				cmp		#20h
				beq		down_0
				rts
;----------(二人用セット)----------
down_0			lda		<Game_select.flag
				and		#00000001b
				bne		up_0
				inc		<Game_select.flag     		;2 Player set.
				jsr		noko_down
				rts
;----------(一人用セット)-----------
up_0			lda		<Game_select.flag
				and		#00000001b
				beq		down_0			
				dec		<Game_select.flag			;1 Player set.
				jsr		noko_up
				rts
;/////////////// 二人用のゲームセレクト ////////////////
Select_1		pla
				cmp		#04
				beq		down_2
				cmp		#08
				beq		up_2
				cmp		#20h
				beq		down_2
				rts
;
down_2			lda		<Game_select.flag
				and		#00011000b
				cmp		#10h
				beq		up_set
				lda		<Game_select.flag
				clc		
				adc		#08
				sta		<Game_select.flag
				jsr     noko_down
				rts
;
up_2			lda		<Game_select.flag
				and		#00011000b
				beq		under_set
				lda		<Game_select.flag
				sec
				sbc		#08
				sta		<Game_select.flag
				jsr     noko_up
				rts
;-------(繰り返しセット)--------
under_set		lda     <Game_select.flag
				ora		#10h
				sta     <Game_select.flag
				jsr     noko_down
				jsr     noko_down
				rts
;
up_set			lda     <Game_select.flag
				and     #11100111b
				sta     <Game_select.flag
				jsr     noko_up
				jsr     noko_up
				rts
;/////////////// 一人用のゲームセレクト /////////////////
Select_2		pla
				cmp		#04
				beq		down_3
				cmp		#08
				beq		up_3
				cmp		#20h
				beq		down_3
				rts
;
down_3			lda		<Game_select.flag
				and		#00100000b
				bne		up_3
				lda		<Game_select.flag
				clc
				adc		#20h
				sta		<Game_select.flag
				jsr     noko_down
				rts
;
up_3			lda		<Game_select.flag
				and		#00100000b
				beq		down_3
				lda		<Game_select.flag
				sec
				sbc		#20h
				sta		<Game_select.flag
				jsr     noko_up
owari_914		rts

;////////////////// クラスセレクト ///////////////////////
Select_3		pla								;ボタンセット。
				cmp		#04
				beq		down_1
				cmp		#08
				beq		up_1
				cmp		#20h
				beq		down_1
				rts
;-------------(大人用)-------------
down_1			lda		>Special_Cup			;150cc check
				and		#03h
				cmp		#01
				bne		Standerd_class
				lda		<Game_select.flag		;adlut
				and		#00000110b	
				cmp		#04		
				beq		up_set1
				jmp		Set_down_class
;			
Standerd_class	lda		<Game_select.flag		;adlut
				and		#00000110b			
				bne		up_1
Set_down_class	lda		<Game_select.flag
				clc
				adc		#02
				sta		<Game_select.flag
				jsr     noko_down
				rts
;-------------(子供用)-------------
up_1			lda		>Special_Cup			;150cc check
				and		#03h
				cmp		#01
				bne		Standerd_class1
				lda		<Game_select.flag		;adlut
				and		#00000110b	
				beq		under_set1
				jmp		Set_up_class
;	
Standerd_class1	lda		<Game_select.flag		;kids
				and		#00000110b
				beq		down_1
Set_up_class	lda		<Game_select.flag
				sec
				sbc		#02
				sta     <Game_select.flag
				jsr     noko_up
				rts
;
;-------(繰り返しセット)--------
under_set1		lda     <Game_select.flag
				ora		#04h
				sta     <Game_select.flag
				jsr     noko_down
				jsr     noko_down
				rts
;
up_set1			lda     <Game_select.flag
				and     #11111001b
				sta     <Game_select.flag
				jsr     noko_up
				jsr     noko_up
				rts
;////////////// キノコのポジションセット ///////////////
noko_down		lda		oamdat+101h
				clc
				adc		#10h
				sta		oamdat+101h
				jmp		Set_sound
;------------------------------------------------
noko_up			lda		oamdat+101h
				sec
				sbc		#10h
				sta		oamdat+101h
;
Set_sound		mem16
				rep		#00100000b
				lda		#002ch
				JSL		>Trigger_sound_1
				mem8
				sep		#00100000b
				rts
;********************************************************
;					文字のセット
;********************************************************
MOJI_SET		equ		$
;-------------------------------------------------
				mem16
				idx16
				rep		#00110000b
;--------------------------------------------------
				ldy		#0
				lda		#0e0f8h						;OAMのクリアー.
loop_535		sta		oamdat+110h,y
				iny
				iny
				cpy		#00f0h
				bne		loop_535
;
				ldy		#0
loop_kinoko_erase
				sta		oamdat+100h,y
				JSR		Y_iny
				cpy		#0010h
				bne		loop_kinoko_erase
				mem8
				idx8
				sep		#00110000b
;////////////// キノコの場所と文字の移動セット/////////
;
				lda     <select_flag
				and     #00000111b
				asl		a
				tax
				lda		<Game_select.flag
				pha
				jmp		(!select_noko,x)
;/////////////// プレイヤーのチェック ///////////////////
NOKO_1			lda		#4ch
				sta		oamdat+100h
;
				idx16
			 	mem16
			 	rep		#00110000b
;
				ldy		#0
				lda		#805ch
				sta		Moji_place
				JSR		Player_1p
;
				lda		#905ch
				sta		Moji_place
				JSR		Player_2p
			 	mem8
			 	idx8
			 	sep		#00110000b
;
				pla
				and		#00000001b
				jmp		Set_Noko
;
Player_1p		equ		$
				mem16
				idx16
				lda		Moji_place
				ldx		#0
				JSR		Player_set
				rts
;
Player_2p		equ		$
				lda		Moji_place
				ldx     #0002
				JSR		Player_set
				rts
;
Player_set		equ		$
				sta		Start_position
				lda		Player_Cmp,x
				sta		Moji_counter_cmp
				lda		Player_Check,x
				JSR		Moji_character_set
				rts
;//////////////// 二人用ゲーム内容 /////////////////////
NOKO_2			mem8
				lda		#50h
				sta		oamdat+100h
;
				idx16
			 	mem16
			 	rep		#00110000b
;
				ldy		#0
				lda		#8060h
				sta		Moji_place
				JSR		Game2_1
;
				lda		#9060h
				sta		Moji_place
				JSR		Game2_2
;
				lda		#0a060h
				sta		Moji_place
				JSR		Game2_3
;
			 	mem8
			 	idx8
			 	sep		#00110000b
;
				pla
				and		#00011000b
				lsr		a
				lsr		a
				lsr		a
				jmp		Set_Noko		
;
Game2_1			equ		$
				mem16
				idx16
				ldx		#0
				lda		Moji_place
				JSR		Game2_set
				rts
;
Game2_2         equ     $
				ldx     #0002
				lda		Moji_place
				JSR		Game2_set
				rts
;
Game2_3         equ     $
				ldx     #0004
				lda		Moji_place
				JSR		Game2_set
				rts
Game2_set		equ		$
				sta		Start_position
				lda		Content_Cmp,x
				sta		Moji_counter_cmp
				lda		Content_Check,x
				JSR		Moji_character_set
				rts
;;//////////////// 一人用ゲーム内容 /////////////////////
NOKO_3			mem8
				lda		#50h
				sta		oamdat+100h
;
				idx16
			 	mem16
			 	rep		#00110000b
;
				ldy		#0
				lda		#8060h
				sta		Moji_place
				JSR		Game1_1
;
				lda		#9060h
				sta		Moji_place
				JSR		Game1_2
;
			 	mem8
			 	idx8
			 	sep		#00110000b
				pla
				and		#00100000b
				lsr		a
				lsr		a
				lsr		a
				lsr		a
				lsr		a
				jmp		Set_Noko
;
Game1_1			equ		$
				mem16
				idx16
				ldx		#0
				lda		Moji_place
				JSR		Game1_set
				rts
;
Game1_2         equ     $
				ldx     #0002
				lda		Moji_place
				JSR		Game1_set
				rts
;
Game1_set		equ		$
				sta		Start_position
				lda		Content1_Cmp,x
				sta		Moji_counter_cmp
				lda		Content1_Check,x
				JSR		Moji_character_set
				rts
;
;;///////////////// クラスのチェック ///////////////////
NOKO_4			mem8
				lda		#50h
				sta		oamdat+100h
;
				idx16
			 	mem16
			 	rep		#00110000b
;
				ldy		#0
				lda		#8060h
				sta		Moji_place
				JSR		Class_50
;
				lda		#9060h
				sta		Moji_place
				JSR		Class_100
;
				lda		#0a060h
				sta		Moji_place
				JSR		Class_150
			 	mem8
			 	idx8
			 	sep		#00110000b
				pla
				and		#00000110b
				lsr		a
				jmp		Set_Noko
;
Class_50			equ		$
				mem16
				idx16
				ldx		#0
				lda		Moji_place
				JSR		Class_set
				rts
;
Class_100        equ     $
				ldx     #0002
				lda		Moji_place
				JSR		Class_set
				rts
;
Class_150        equ     $
				ldx     #0004
				lda		Moji_place
				JSR		Class_set
				rts
;
Class_set		equ		$
				sta		Start_position
				lda		Class_Cmp,x
				sta		Moji_counter_cmp
				lda		Class_Check,x
				JSR		Moji_character_set
				rts
;///////////////////// 最終 ////////////////////////////
NOKO_5			mem8
				pla
				jsr		ALL_DISPLAY				;All message set.
				rts
;///////////////// キノコ Vポシションセット //////////
Set_Noko		tax								;NOKO display.
				lda		nokoplace,x
				sta		oamdat+101h
				rts
;---------------------------------------------
;**********************************************************
;					最終チェックの文字セット
;**********************************************************
ALL_DISPLAY		equ		$
;//////////////// プレイヤーの文字セット ////////////////
				lda		<Game_select.flag			;プレイヤー数チェック
				and		#0000001b
				asl		a
				tax
;
				mem16
				idx16
				rep		#00110000b
				ldy		#0
				lda		#7858h
				sta		Moji_place
				JSR		(Player_moji_set,x)
;//////////////// 二人用ゲーム内容セット/////////////////////
				mem8
				idx8
				sep		#00110000b
				lda		<Game_select.flag
				and		#00000001b
				beq		Set_oneplay
;		
				lda     <Game_select.flag
				and		#00011000b
				lsr		a
				lsr		a
				tax
;;
				mem16
				idx16
				rep		#00110000b
				lda		#8858h
				sta		Moji_place
				JSR		(Select_game2,x)
				jmp		Check_class
;//////////////// 一人用ゲーム内容セット/////////////////////
Set_oneplay		mem8
				idx8
				sep		#00110000b
				lda     <Game_select.flag
				and		#00100000b
				lsr		a
				lsr		a
				lsr		a
				lsr		a
				tax
;
				mem16
				idx16
				rep		#00110000b
				lda		#8858h
				sta		Moji_place
				JSR		(Select_game1,x)		
;//////////////// クラスセット ///////////////////////////
Check_class	mem8
				idx8
				sep		#00110000b
				lda     <Game_select.flag			;クラスセットを飛ばすか
				and		#00000001b					;どうかのチェック
				beq		one_player_set
;
				lda     <Game_select.flag
				and		#00011000b
				bne		Set_OK_CHECK
				jmp		Set_Class

one_player_set	lda		<Game_select.flag
				and		#00100000b
				bne		Set_OK_CHECK
;-----------------------------------------------
Set_Class		lda		<Game_select.flag
				and		#0000110b
				tax
;
				mem16
				idx16
				rep		#00110000b
				lda		#9858h
				sta		Moji_place
				JSR		(Select_class,x)
;/////////////// OKセット ///////////////////////
Set_OK_CHECK
;-------------(文字セット)-----------------
				mem16
				idx16
				rep		#00110000b
				ldy		#0090h
				ldx		#0
loop_check		lda		OK_CHECK,x
				cmp		#0ffffh
				beq		Noko_check
				sta		oamdat+110h,y
				inx
				inx
				iny
				iny
				jmp		loop_check
;---(キノコ数とＶポシション飛ばしチェック)----
Noko_check		mem8
				idx8
				sep		#00110000b
;
				ldy		#0
				lda		#08h
loop_color_moji	sta		oamdat+113h,y
				JSR		Y_iny
				cpy		#0090h
				bne		loop_color_moji
;
				lda     <Game_select.flag
				and		#00000001b
				beq		one_player_set1
;
				lda     <Game_select.flag
				and		#00011000b
				beq		All_kinoko
				jmp		Two_kinoko
;
one_player_set1	lda		<Game_select.flag
				and		#00100000b
				beq		All_kinoko
;
Two_kinoko		lda		#78h				;"NOKO" all display set
				sta		oamdat+101h
				lda		#88h
				sta     oamdat+105h
				jmp		Check_set_up
;
All_kinoko		lda		#78h			;"NOKO" all display set.
				sta		oamdat+101h
				lda		#88h
				sta     oamdat+105h
				lda		#98h
				sta     oamdat+109h
				jmp		owari_check
;-----------(Ｖポシションアップセット)-----------
Check_set_up	mem16
				idx16
				rep		#00110000b
				ldx		#0
				ldy		#0090h
loop_L28		lda		OK_CHECK,x
				cmp		#0ffffh
				beq		owari_check
				sec
				sbc		#1000h
				sta		oamdat+110h,y
				JSR		Y_iny		
				JSR		X_inx
				jmp		loop_L28
owari_check		mem8
				idx8
				sep		#00110000b
;////////////// キノコ Hセット //////////////////////
				ldx		#0				
				lda		#48h
loop_300		sta		oamdat+100h,x
				JSR		X_inx
				cpx		#10h
				bne		loop_300
				rts
;(DATA)
Player_Check	DW		ONE_PLAY,TWO_PLAY				
Player_Cmp		DW		000ch,000ch
;
Class_Check		DW		KIDS_CLASS,ADULT_CLASS,SPECIAL_CLASS
Class_Cmp		DW		0007h,0008h,0008h
;
Content_Check	DW		MARIO_GP,VS_RACE,BATTLE_GAME
Content_Cmp		DW		0008h,0008h,0008h
;
Content1_Check	DW		MARIO_GP1,TIME_ATTCK
Content1_Cmp	DW		0008h,0007h
;
nokoplace		DB		80h,90h,0a0h,78h
;
Player_moji_set	DW		Player_1p,Player_2p
;
Select_game2	DW		Game2_1,Game2_2,Game2_3
;
Select_game1	DW		Game1_1,Game1_2
;
Select_class	DW		Class_50,Class_100,Class_150
;**************************************************
;				ALL END CHECK
;**************************************************
ALL.END_CHECK	equ		$
				mem8
				idx8
				lda		<select_flag
				and		#0f0h
				bne		owari_RX7
;
				lda		<button_TG+1
				ora		<button_TG+3
				cmp		#02
				beq		Select_Yes
				cmp		#01
				beq		Select_No
owari_RX7		rts
;
;//////////////// 最終チェック”はい”////////////
Select_Yes		lda     <Game_select.flag
				and		#10000000b
				beq		owari_RX7				;aer you OK?:yes-->owari_RX7				
;
				lda     <Game_select.flag
				and		#01111111b
				sta		<Game_select.flag
;
				lda		#70h					;display set.
				sta		oamdat+1e0h
				lda		#088h
				sta		oamdat+1e4h
				jmp		Set_sound_1
;//////////////// 最終チェック”いいえ”////////////
Select_No		lda     <Game_select.flag
				and		#10000000b
				bne		owari_RX7				;aer you OK?:no-->owar_RX7
;
				lda		<Game_select.flag
				ora		#80h
				sta		<Game_select.flag					
;
				lda		#90h					;display set.
				sta		oamdat+1e0h
				lda		#0b0h
				sta		oamdat+1e4h
;
Set_sound_1		mem16
				rep		#00100000b
				lda		#002ch
				JSL		>Trigger_sound_1
				mem8
				sep		#00100000b
				rts
;-------------------------------------------------
;**********************************************************
;					タイトルデモ用
;**********************************************************
OBJ_DEMO		equ		$
;<<<<<<<<<<<<<<<< フェード中のデモ禁止 >>>>>>>>>>>>>>>>
				mem16
				idx16
				lda     fade_control
				beq		Demo_start
				rts
Demo_start		jsr		Swing_kart
;<<<<<<<<<<<<<<<<<<< マリオ  セット>>>>>>>>>>>>>>>>>>>>>
;-------(タイマーチェックとセット)---------
MARIO_timer		ldx		#player_r0
				inc		<_demotimer,x							;タイマーセット
				lda		<_demotimer,x
				cmp		#00d2h									;最初の走行タイマーチェック
				beq		MARIO_select
				cmp		#01f2h
				beq		MARIO_select							;最初のイレースタイマーチェック
				cmp		#0452h
				beq		MARIO_select							;２回目の走行タイマーチェック
				cmp		#046ah
				beq		MARIO_select							;回転タイマーチェック
				cmp		#053ah
				beq		MARIO_select							;回転後の走行タイマーチェック
				cmp		#0592h
				beq		MARIO_select							;２回目のイレースタイマーチェック
				jmp		MARIO_set
;---------(フラグセット)-----------
MARIO_select	inc		<_demoflag,x
MARIO_set		lda		<_demoflag,x
				asl		a
				tax
				jmp		(!MARIO_jmp,x)
;
MARIO_jmp		DW		LUIZI_timer,run_mario_1,erase_mario
				DW		run_mario_2,revolve_mario,run_mario_3
				DW		erase_mario
;-------------(セット)------------
run_mario_1		ldx		#1000h
				lda		#120h
				jsr		Run_set
				jmp		LUIZI_timer
;
run_mario_2		ldx		#1000h
				lda		#200h
				jsr		Run_set
				jmp     LUIZI_timer
;
run_mario_3		ldx		#1000h
				lda		#3800h
				sta		<_poseQ,x
				lda		#300h
				jsr     Run_set
				lda		#8000h
				sta		<_muteki_timer,x
				jmp     LUIZI_timer
;
revolve_mario	ldx     #1000h
				stz		<_velocityX,x
				jsr     Revolve_set
				jmp     LUIZI_timer	
erase_mario		ldy		#0
				ldx     #1000h
				jsr		erase_set
				stz		<_muteki_timer,x
				lda		#00c0h
				sta		<_swing_position,x
				sta		<_situationYH,x
;<<<<<<<<<<<<<<<<<< ルイージ セット >>>>>>>>>>>>>>>>>>>>>
;-------(タイマーチェックとセット)---------
LUIZI_timer		ldx		#player_r1
				inc		<_demotimer,x			;タイマーセット
				lda		<_demotimer,x
				cmp		#0112h					;最初の走行タイマーチェック
				beq		LUIGI_select
				cmp		#0232h
				beq		LUIGI_select			;イレースタイマーチェック
				cmp		#0492h
				beq		LUIGI_select			;２回目の走行タイマーチェック
				cmp		#0572h
				beq		LUIGI_select			;回転タイマーチェック
				jmp		LUIGI_set
;--------(フラグセット)------------
LUIGI_select	inc		<_demoflag,x
LUIGI_set		lda		<_demoflag,x
				asl		a
				tax
				jmp		(!LUIGI_jmp,x)
;
LUIGI_jmp		DW		KUPPA_timer,run_luizi,erase_luizi
				DW		run_luizi,revolve_luizi
;
;-----------(セット)----------------
run_luizi		ldx		#player_r1
				lda		#120h					
				jsr		Run_set
				jmp		KUPPA_timer
revolve_luizi	ldx		#player_r1
				lda		#-130h
				jsr		Run_set
				jsr		Revolve_set	
				jmp     KUPPA_timer
erase_luizi		ldx     #player_r1
				ldy		#02						
				jsr		erase_set
;<<<<<<<<<<<<<<<<<<< クッパ セット >>>>>>>>>>>>>>>>>>>>>
;---------(タイマーチェックとセット)---------
KUPPA_timer		ldx		#player_r2
				inc		<_demotimer,x
				lda		<_demotimer,x
				cmp		#01f2h
				beq		KUPPA_select			;最初の走行タイマーチェック
				cmp		#0217h
				beq		KUPPA_select			;甲羅のセットタイマーチェック
				cmp		#0218h
				beq		KUPPA_select			;甲羅の走行タイマーチェック
				cmp		#023eh
				beq		KUPPA_select			;甲羅の消しタイマーチェック
				cmp		#0302h
				beq		KUPPA_select			;消しタイマーチェック
				cmp		#04c2h
				beq		KUPPA_select			;２回目の走行タイマーチェック
				cmp		#055ah
				beq		KUPPA_select			;回転タイマーチェック
				jmp		KUPPA_set
;----------(フラグセット)----------------
KUPPA_select	inc		<_demoflag,x
KUPPA_set		lda		<_demoflag,x
				asl		a
				tax
				jmp		(!KUPPA_jmp,x)
;
KUPPA_jmp		DW		PEACH_timer,run_kuppa,koura_kuppa
				DW		koura_run_kuppa,koura_run_era,erase_kuppa
				DW		run_kuppa_2,revolve_kuppa
;------------(セット)-------------------
run_kuppa		ldx     #player_r2
				lda		#140h
				jsr		Run_set
				jmp		PEACH_timer
run_kuppa_2		ldx     #player_r2
				lda		#120h
				jsr     Run_set
				jmp     PEACH_timer
revolve_kuppa	ldx     #player_r2
				lda		#-150h
				jsr		Run_set
				jsr		Revolve_set	
				jmp     PEACH_timer
koura_kuppa		lda		#0b028h
				sta		oamdat+84h
				jmp     PEACH_timer	
koura_run_kuppa	jsr		Koura_run
				jmp     PEACH_timer
koura_run_era	lda		#0f0e0h
				sta		oamdat+84h
				jmp     PEACH_timer
erase_kuppa		ldx     #player_r2
				ldy		#04
				jsr		erase_set
;<<<<<<<<<<<<<<<<< ピーチ セット >>>>>>>>>>>>>>>>>>>>>>>>
;--------(タイマーセットとチェック)-----------
PEACH_timer		ldx		#player_r3
				inc		<_demotimer,x
				lda		<_demotimer,x
				cmp		#0152h
				beq		PEACH_select				;最初の走行タイマーチェック
				cmp		#0272h
				beq		PEACH_select				;消しタイマーチェック
				cmp		#04dah
				beq		PEACH_select				;２回目走行タイマーチェック
				cmp		#054ah
				beq		PEACH_select				;回転タイマーチェック
				cmp		#05bah
				beq     PEACH_select
				jmp		PEACH_set
;----------(フラグセット)-----------
PEACH_select	inc		<_demoflag,x
PEACH_set		lda		<_demoflag,x
				asl		a
				tax
				jmp		(!PEACH_jmp,x)
;
PEACH_jmp		DW		KONG_timer,run_peach,erase_peach
				DW		run_peach_2,revolve_peach,erase_peach
;---------(セット)-----------
run_peach		ldx     #player_r3
				lda		#120h
				jsr		Run_set
				jmp		KONG_timer
run_peach_2		ldx     #player_r3
				lda		#120h
				jsr		Run_set
				jmp     KONG_timer
revolve_peach	ldx     #player_r3
				lda		#-140h
				jsr		Run_set
				jsr		Revolve_set	
				jmp		KONG_timer
erase_peach		ldx     #player_r3
				ldy		#06
				jsr		erase_set
				lda		#00c0h
				sta		<_swing_position,x
				sta		<_situationYH,x
;<<<<<<<<<<<<<<<<<<< コング セット >>>>>>>>>>>>>>>>>>>>>
;-------(タイマーセットとチェック)--------
KONG_timer		ldx		#player_r4
				inc		<_demotimer,x
				lda		<_demotimer,x
				cmp		#02feh
				beq		KONG_select					;最初の走行タイマーチェック
				cmp		#0422h
				beq		KONG_select					;消しタイマーチェック
				jmp		KONG_set
;-------(フラグセット)-----------
KONG_select		inc		<_demoflag,x
KONG_set		lda		<_demoflag,x
				asl		a
				tax
				jmp		(!KONG_jmp,x)
;
KONG_jmp		DW		KAME_timer,run_kong,erase_kong
;-------(セット)------------
run_kong		ldx     #player_r4
				lda		#120h
				jsr		Run_set
				jmp		KAME_timer
erase_kong		ldx     #player_r4
				ldy		#08
				jsr		erase_set
;<<<<<<<<<<<<<<<<<<<<< カメ セット >>>>>>>>>>>>>>>>>>>>>
;-------(タイマーセットとチェック)--------
KAME_timer		ldx		#player_r5
				inc		<_demotimer,x
				lda		<_demotimer,x
				cmp		#04aah
				beq		KAME_select				;最初の走行タイマーチェック
				cmp		#0568h
				beq		KAME_select				;回転タイマーチェック
				jmp		KAME_set
;--------(フラグセット)------------
KAME_select		inc		<_demoflag,x
KAME_set		lda		<_demoflag,x
				asl		a
				tax
				jmp		(!KAME_jmp,x)
;
KAME_jmp		DW		KINOPIO_timer,run_kame,revolve_kame
;------------(セット)-------------
run_kame		ldx     #player_r5
				lda		#120h
				jsr		Run_set
				jmp		KINOPIO_timer
revolve_kame	ldx     #player_r5
				lda		#-150h
				jsr		Run_set
				jsr		Revolve_set	
				jmp     KINOPIO_timer		
erase_kame		ldx     #player_r5
				ldy		#0ah
				jsr		erase_set
;<<<<<<<<<<<<<<<<< キノピオ セット >>>>>>>>>>>>>>>>>>>>>
;-------(タイマーセットとチェック)--------
KINOPIO_timer	ldx		#player_r6
				inc		<_demotimer,x
				lda		<_demotimer,x
				cmp		#0192h
				beq		KINOPIO_select			;走行タイマーチェック
				cmp		#023ch
				beq		KINOPIO_select			;回転(星あり)タイマーチェック
				cmp		#0245h
				beq		KINOPIO_select			;回転(星なし)タイマーチェック	
				cmp		#02c2h
				beq		KINOPIO_select			;消しタイマーチェック
				jmp		KINOPIO_set
;-------(フラグセット)----------
KINOPIO_select	inc		<_demoflag,x
KINOPIO_set		lda		<_demoflag,x
				asl		a
				tax
				jmp		(!KINOPIO_jmp,x)
;
KINOPIO_jmp		DW		YOSSY_timer,run_kinopio,revolve_kinopio
				DW		nostar_kinopio,erase_kinopio
;--------(セット)-----------
run_kinopio		ldx     #player_r6
				lda		#120h
				jsr		Run_set
				jmp		YOSSY_timer
revolve_kinopio	ldx     #player_r6
				jsr		Revolve_set
				lda		#-170h
				jsr     Run_set
				lda		#0ad8fh
				jsr		Star_set
				jmp     YOSSY_timer
nostar_kinopio	ldx     #player_r6
				jsr     Revolve_set
				lda     #0f0e0h
				sta		oamdat+88h
				jmp     YOSSY_timer
erase_kinopio	ldx     #player_r6
				ldy		#0ch
				jsr		erase_set
;<<<<<<<<<<<<<<<< ヨッシー セット >>>>>>>>>>>>>>>>>>>>>
;--------(タイマーセットとチェック)-------
YOSSY_timer		ldx		#player_r7
				inc		<_demotimer,x
				lda		<_demotimer,x
				cmp		#0362h
				beq		YOSSY_select				;走行タイマーチェック
				cmp		#03f0h
				beq		YOSSY_select				;回転タイマーチェック
				cmp		#0472h
				beq		YOSSY_select				;消しタイマーチェック
				jmp		YOSSY_set
;---------(フラグセット)--------
YOSSY_select	inc		<_demoflag,x
YOSSY_set		lda		<_demoflag,x
				asl		a
				tax
				jmp		(!YOSSY_jmp,x)
;
YOSSY_jmp		DW		BANANA_timer,run_yossy,revolve_yossy
				DW		erase_yossy
;--------(セット)------------
run_yossy		ldx		#player_r7
				lda		#160h
				jsr		Run_set
				jmp		BANANA_timer
revolve_yossy	ldx     #player_r7
				jsr		Revolve_set
				lda		#-220h
				jsr     Run_set
				jmp     BANANA_timer
erase_yossy		ldx     #player_r7
				ldy		#0eh
				jsr		erase_set
;==============================================
;				バナナ
;============================================== 
BANANA_timer	lda		<Banana_flag
				beq		Timer_check
				jsr		Banana_move
;
Timer_check		ldx		#1000h				;バナナタイマーチェック
				lda		<_demotimer,x
				cmp		#03d2h
				beq		Banana_1
				cmp		#03f2h
				beq		Banana_erase
				cmp		#0402h
				beq		Banana_2
				cmp		#46dh
				beq		Banana_erase
				cmp		#0652h				;デモ終了タイマーチェック
				bne		demo_owari
				jsr		OBJ_ERASE			;イニシャルセット
demo_owari		rts
; - - - - - - - - - - - - - - - - - - - - - -
;--------(バナナセット)---------
Banana_1		lda		#0b0d0h
				jsr     Banana_set
				lda		#0001
				sta		<Banana_flag
				jmp		demo_owari
Banana_2		lda		#0b08eh
				jsr		Banana_set
				lda		#0001
				sta		<Banana_flag
				jmp     demo_owari
Banana_erase	lda     #0e0f0h
				jsr     Banana_set
				stz		<Banana_flag
				jmp     demo_owari
;- - - - - - - - - - - - - - - - - - - - - -
;------(走行セット)-----
Run_set			equ		$
				sta		<_velocityX,x
				rts
;------(振動セット)-----
Swing_kart		equ		$
				lda     <Swing_count
				tax	
				lda		Select_kart,x
				tax
				lda		<_situationYH,x
				cmp		<_swing_position,x
				bne		kart_down
				inc		a
				jmp		Kart_Yposition
kart_down		dec		a
Kart_Yposition	sta		<_situationYH,x				
;
				inc		<Swing_count
				inc		<Swing_count
				lda		<Swing_count
				cmp		#10h
				bne		owari_swing
				stz		<Swing_count
owari_swing		rts
;------(回転セット)-----
Revolve_set		lda		#800h
				clc
				adc		<_poseQ,x
				sta		<_poseQ,x
				rts
;-----(バナナセット)--------
Banana_set		equ		$
				sta		oamdat+80h
				lda		#3f00h
				sta		oamdat+82h
				lda		#0aaaah
				sta		msbdat+08h
				rts
;
Banana_move		equ		$
				dec		oamdat+80h
				lda		#0aaaah
				sta		msbdat+08h
				rts
;-----(こうらセット)---------
Koura_run		equ		$
				lda		#0aaaah
				sta		msbdat+08h
				lda		oamdat+84h
				clc
				adc		#0003
				sta		oamdat+84h
;
				lda		<Koura_speed
				inc		a
				cmp		#05h
				beq		Koura_revolve
				sta		<Koura_speed
				rts
;
Koura_revolve	stz		<Koura_speed
				lda		oamdat+86h
				cmp		#3f02h
				bne		koura_reset
;
				lda		#3f04h
				sta     oamdat+86h
				jmp		owari_koura
;
koura_reset		lda		#3f02h
				sta		oamdat+86h
owari_koura		rts
;--------(星セット)-----------
Star_set		equ		$
				sta		oamdat+88h
;
				lda		#0aaaah
				sta		msbdat+08h
				lda		oamdat+8ah
				cmp		#3f06h
				bne		v_set
				lda		#0bf06h
				sta		oamdat+8ah
				jmp		owari_star
v_set			lda		#3f06h
				sta     oamdat+8ah
owari_star		rts		
;- - - - - - - - - - - - - - - - - - - - - - - - - -
;------(総てのオブジェを消す、ウインドウオープン時とイニシャルでにセット)------
OBJ_ERASE		equ		$
				mem16
				idx16
				ldy		#0
loop_erase		lda		Select_kart,y
				tax
				phy
				jsr		erase_set
				stz		<_demotimer,x
				stz		<_demoflag,x
				ply
				iny
				iny
				cpy		#0010h
				bne		loop_erase
;
				lda		#0e0f0h
				sta		oamdat+84h
				sta		oamdat+88h
				jsr		Banana_set
				lda		#3f02h
				sta		oamdat+86h
				lda		#3f06h
				sta		oamdat+8ah
;
				stz     <Koura_speed
				stz     <Banana_flag
				rts
;-------(1オブジェだけをけしイニシャル状態にする。)------
erase_set		equ		$
				lda		Select_init,y
				tay
				stz		<_velocityX,x
				lda		!0,y
				sta		<_situationXH,x
				lda     !2,y
				sta     <_situationYH,x
				lda     !4,y
				sta		<_poseQ,x
				lda     !6,y
				sta		<_demokart_size,x
				lda     !8,y
				sta		<_swing_position,x
				rts
;- - - - - - - - - - - - - - - - - - - - - - -
;
OBJ_ERASE_SET	phb
				phk
				plb
				lda		#200h						;オブジェを消す
				sta		<sprite_count
				JSR		OBJ_ERASE
				JSL		>Set_demokart
				plb
				rtl
;
Select_kart		DW		player_r0,player_r1,player_r2,player_r3
				DW		player_r4,player_r5,player_r6,player_r7
;
Select_init		DW		mario_set,luizi_set,kuppa_set,peach_set
				DW		kong_set,kame_set,kinopio_set,yossy_set
;-----------(イニシャルデーター)--------
mario_set		DW		0fff0h,00b8h,03800h,0000h,00b8h
luizi_set		DW		0fff0h,00c0h,03800h,0000h,00c0h
kuppa_set		DW		0fff0h,00c0h,03800h,0000h,00c0h
peach_set		DW		0fff0h,00b8h,03800h,0000h,00b8h
kong_set		DW		0fff0h,00c0h,03800h,0000h,00c0h
kame_set		DW		0fff0h,00c0h,03800h,0000h,00c0h
kinopio_set		DW		0fff0h,00c0h,03800h,0000h,00c0h
yossy_set		DW		0fff0h,00c0h,03800h,0000h,00c0h
;
OBJ_data2       DW      player_r0,0fff0h,00c8h,03800h
				DW      player_r1,0fff0h,00c0h,03800h
				DW      player_r2,0fff0h,00a8h,03800h
				DW      player_r3,0fff0h,00c8h,03800h
				DW      player_r4,0fff0h,00c8h,03800h
				DW      player_r5,0fff0h,00c8h,03800h
				DW      player_r6,0fff0h,00c8h,03800h
				DW      player_r7,0fff0h,00c8h,03800h
				DW		0
;========================================================
bank84          sect    rel
;========================================================
;************************************************
;				PPU&OAM.MSB のイニシャル　セット
;************************************************
PPU_INT_SET		equ		$
				phb
				phk
				plb
				mem8
				idx8
				STZ		!DMA_synchronic 	;B HDMA off 
				STZ		!DMA_burst		;B VDMA off
				LDA		#80H			;B	
				STA		!PPU_control		;B screen blanking
;---------------------------------------------- ; reset window -------
				STZ		!Through_window+0	;B
				STZ		!Through_window+1	;B
				STZ		!Window_control+0	;B
				STZ		!Window_control+1	;B
				STZ		!Window_control+2	;B
;---------------------------------------------- ; reset CG ------------
				STZ		!Color_control		;B
				STZ		!Color_brend		;B
				LDA		#0E0H			;B
				STA		!Color_bias		;B
;-----------------------------------------------; reset scroll --------
				LDX		#7			;B
clr_scrl		STZ		!Scroll_,X		;B
				STZ		!Scroll_,X		;B
				DEX				;B
				BPL		clr_scrl		;B
;--------------------------------------------------------------------
				STZ		!Screen_mosaic		;B mosaic off
				STZ		!PPU_option		;B nomal mode
				STZ		!Through_screen+1	;B through sub
;--------------------------------------------------------------------
;(OAM)
			 	idx16
 			 	mem16
			 	rep		#00110000b
;
			 	ldx		#0000h
loop_735	 	lda		#0e0f8h		 
			 	sta		200h,x
			 	inx
				inx
			 	cpx		#200h
			 	bne		loop_735
				mem8
				idx8
				sep		#00110000b
;
				ldx		#0
loop_750	 	lda		#0
			 	sta		400h,x
			 	inx
			 	cpx		#20h
			 	bne		loop_750
;
				JSR		Zero_clear1
				plb
				rtl
;
Zero_clear1		mem16
				idx16
				rep		#00110000b
				ldx		#0
Loop_Zero_1		stz		0060h,x
				inx
				inx
				cpx		#0040h
				bne		Loop_Zero_1
;
				ldx		#0
Loop_Zero_2		stz		180h,x
				inx
				inx
				cpx		#0080h
				bne		Loop_Zero_2
;
				ldx		#0
Loop_Zero_3		stz		1800h,x
				inx
				inx
				cpx		#0200h
				bne		Loop_Zero_3
				mem8
				idx8
				sep		#00110000b
				rts
;
Zero_clear		equ		$
				phb
				phk
				plb
				JSR		Zero_clear1
				plb
				rtl
;

				END
