			extend
;-----------------------------------------------------
			include		../join/rp5c77
			include		../join/buffer.def
			include		../join/rp5a22
			include     ../join/work.def
			include		../join/label.def
			include		../join/object.def
;-----------------------------------------------------
			glb		Ending2_init,Ending2_main,Ending2_nmi
			glb		Y_set
;
			ext		Ending2_int_set,Upper_body_set2
			ext		Banzai_Set_everyone,Trigger_sound_1
;----------------------------------------------------
Wave_acceleration	equ		1800h
Wave_point			equ		1810h
Wave_stop_flag		equ		1820h
Wave_cmp			equ		1830h
Wave_land_cmp		equ		1840h
;
Moji_acceleration	equ		1850h
Moji_fall_flag		equ		1860h
;
Banzai_counter		equ		18f0h
;
oamdat				equ		200h		
msbdat				equ		400h
;-----------------------------------------------------
button_TG			equ		6ah		;4byte
Pose_counter    	equ     63h
Bnzai_timer			equ		65h
Character_count		equ		67h
;
Now_Wave_point		equ		70h
Ending_timer		equ		72h
Fade_set			equ		74h
Pose_counter_set	equ		76h
Fade_counter		equ		78h
Fade_timer			equ		7ah
Color_timer			equ		7ch
;
;
bank83			sect	rel			;(PBR=8f)
;==========================================================
;**********************************************************
;			INITIAL ROUTINE
;**********************************************************
;==========================================================
;
Ending2_init	EQU		$
				phb
				phk
				plb		
				mem8
				idx8
				sep		#00110000b
				JSL		>Ending2_int_set			;title-e.asmにあります
				JSR		Tensou_ending2_set
				idx16
				mem16
				rep		#00110000b
;
				plb
				RTL
;
;===========================================================											
;***********************************************************
;				MAIN ROUTINE
;***********************************************************
;===========================================================
;(TITLE SET)
;
Ending2_main	EQU		$	
				phb
				phk
				plb		
;------(タイマーセット、チェック)---------
				inc		Ending_timer
				lda		Ending_timer
				cmp		#00d0h
				bcc		Banzai_set		
				cmp		#0100h
				beq		Set_fall_moji
				cmp		#020ch
				beq		Set_Wave_play
				cmp		#03e0h
				bcs		End_Set_timer
				jmp		Main_set
;----------(バンザイセット)----------
Banzai_set		JSR		Banzai_set_everydody
				jmp     Main_set
;----------(文字落下セット)----------
Set_fall_moji	inc		Moji_fall_flag
				jmp		Main_set
;---------(ウェーブセット)-----------
Set_Wave_play	inc		Wave_stop_flag
				lda		#0002
				sta		Banzai_counter
;---------(メインセット)--------------
Main_set		JSR		Wave_set
				JSR		Noji_Fall_set
				JSR		Color_change_set
				jmp		Ending_owari
;----------(タイマー無限ループ)--------
End_Set_timer	lda		Ending_timer
				cmp		#0800h
				bne		End_Set
				lda		#600h
				sta		Ending_timer
;--------(ＴＨＥ　ＥＮＤセット)---------
End_Set			JSR		The_End_set
				lda		Fade_set
				cmp		#00ffh
				bne		Ending_owari
				JSR		Wave_set
				JSR     Color_change_set
Ending_owari	plb
				RTL
;===========================================================
;***********************************************************
;				NMI ROUTINE
;***********************************************************
;===========================================================
Ending2_nmi		equ		$
				phb
				phk
				plb		
;
				mem8
				sep		#00100000b
				lda		Fade_counter
				sta		PPU_control
				mem16
				rep		#00100000b
;
				plb
				RTL
;=========================================================
;*********************************************************
;				イニシャルルーチン
;*********************************************************
;=========================================================
;***************************************************
;				PPU&
;***************************************************
Tensou_ending2_set
;<<<<<<<<<<<<<<< ＰＰＵセット >>>>>>>>>>>>>>>>>>>>>
				mem8
				sep		#00100000b
				lda		#14h
				sta		Through_screen
				mem16
				idx16
				rep		#00110000b
;<<<<<<<<<<<<<<<< ＭＳＢセット >>>>>>>>>>>>>>>>>>>>
				ldx		#0
				lda		#0aaaah
loop_big_msb	sta		msbdat,x
				inx
				inx
				cpx		#0020h
				bne		loop_big_msb
				stz		msbdat+0ch
				stz		msbdat+0eh
				stz		msbdat+10h
;<<<<<<<<<<<<<<< フェードコントロール >>>>>>>>>>>>>
				lda		#00ffh
				sta		Fade_set
				lda		#000fh
				sta		Fade_counter
				lda		#000ch
				sta		Fade_timer
;<<<<<<<<<<<<<<<<<< 加速度セット >>>>>>>>>>>>>>>>>>
				ldx		#0
				lda		#0fc00h
loop_acceleration
				sta		Wave_acceleration,x
				inx
				inx
				cpx		#0010h
				bne		loop_acceleration
;<<<<<<<<<<<<<<<<<< 決定座標セット >>>>>>>>>>>>>>>>>>
				ldx		#0
				lda		#00a0h
loop_point		sta		Wave_point,x
				inx
				inx
				cpx		#0010h
				bne		loop_point
				lda		#009ch
				sta     Wave_point+4
				sta		Wave_point+0eh
;<<<<<<<<<<<<<<<<<< 着地セット >>>>>>>>>>>>>>>>>>>>
				ldx		#0
				lda		#00a3h
loop_land_cmp	sta		Wave_land_cmp,x
				inx
				inx
				cpx     #0010h
				bne		loop_land_cmp
				lda		#009fh
				sta		Wave_land_cmp+4
				sta     Wave_land_cmp+0eh
;
;<<<<<<<<<<<<<<<<< 文字速度セット >>>>>>>>>>>>>>>>>
				ldx		#0
				lda		#0400h
loop_fall_speed	sta		Moji_acceleration,x
				inx
				inx
				cpx		#0010h
				bne		loop_fall_speed
;<<<<<<<<<<<<<<<<<<  BG3 スクリーン転送 >>>>>>>>>>>>>>>>>>>>>
;
  	   		 	lda		#1c00h				;Vram adrr hi.
	    	 	sta	    Screen_address
;
 	 		 	ldx		#0					;character data.
			 	lda		#0000h
loop9		 	sta		>Screen_write
			 	inx
			 	inx
			 	cpx		#800h				;1000? :no-->lopp1.
			 	bne		loop9
;<<<<<<<<<<<<<<<<<<<<< BG_3 キャラクター転送 >>>>>>>>>>>>>>>>
	 	 		ldx		#0					;character data.
			 	lda		#0f0fh
loop911		 	sta		Screen_write
			 	inx
			 	inx
			 	cpx		#400h				;1000? :no-->lopp1.
			 	bne		loop911
;<<<<<<<<<<<<<<<<<<< カラーセット  >>>>>>>>>>>>>>>>>>>>>>>>>>>
Color_wave_set	equ		$
				ldx		#0182h
				lda		#00c1h
				JSR		Color_set
;
				ldx		#018ah
				lda		#00d1h
				JSR		Color_set
;
				ldx		#0192h
				lda		#00e1h
				JSR		Color_set
;
				ldx		#019ah
				lda		#00f1h
				JSR		Color_set
;
;<<<<<<<<<<<<<<< ＴＨＡＮＫ　ＹＯＵセット >>>>>>>>>>>>>>>>>>>>
				ldx		#0
				ldy		#00c0h
Loop_thanks		lda		Thank_you_position,x
				sta     oamdat,y
				lda		Thank_you_data,x
				sta		oamdat+2,y
				JSR		Y_set
				lda		Thank_you_position,x
				clc
				adc		#0800h
				sta     oamdat,y
				lda		Thank_you_data,x
				inc		a
				sta		oamdat+2,y
				JSR     Y_set
				inx
				inx
				cpx		#0010h
				bne		Loop_thanks
;
				lda		#0f038h
				sta		oamdat+100h
				lda		#08dah
				sta		oamdat+102h
			
				JSL		>Upper_body_set2			;Final-e.asmにあります
				JSL		>Banzai_Set_everyone
				rts
;
Color_set		mem8
				sep		#00100000b
				sta		Color_address
				ldy		#0
loop_Color_wave	lda		>color_buffer,x
				sta     Color_write
				inx
				lda		>color_buffer,x
				sta     Color_write
				inx
				iny
				cpy		#0003h
				bne		loop_Color_wave
				mem16
				rep		#00100000b
				rts
;
Y_set			equ		$
				iny
				iny
				iny
				iny
				rts
;==========================================================
;**********************************************************
;				メインルーチンサブ
;**********************************************************
;==========================================================
;*****************************************************
;				バンザイセット
;*****************************************************
Banzai_set_everydody
				stz     Color_timer
				inc		<Bnzai_timer
				lda		<Bnzai_timer
				cmp		#0010h
				bne		NO_set_banzai
				stz		<Bnzai_timer
;
			
				inc		<Pose_counter_set
				inc		<Pose_counter_set
				lda		<Pose_counter_set
				cmp		#0004
				bne		Set_banzai	
				lda		#0
				stz		<Pose_counter_set
;
Set_banzai		lda		<Pose_counter_set
				ldx		#0
loop_counter_set
				sta		Banzai_counter,x
				inx
				inx
				cpx		#0010h
				bne		loop_counter_set
;
				JSL     >Banzai_Set_everyone
NO_set_banzai	rts
;*****************************************************
;				 ウエーブセット
;*****************************************************
Wave_set		equ		$
				JSL     >Banzai_Set_everyone
				ldy		#0
loop_wave		lda		Wave_address,y
				tax
				JSR		Wave_everyone_set
				iny
				iny
				cpy		#0010h
				bne		loop_wave
				rts
;
Wave_everyone_set
				lda		Wave_stop_flag,y
				beq		Donot_wave
;-------(加速度セット)-------
				lda		Wave_acceleration,y
				cmp		#0380h
				beq		Wave_position_set
				clc
				adc		#0040h
				sta		Wave_acceleration,y
;-----(ポジションセット)-------
Wave_position_set
				mem8
				sep		#00100000b
;
				lda		Wave_point,y				;落下セット
				clc
				adc		Wave_acceleration+1,y
				cmp		Wave_land_cmp,y
				bcs		Wave_stop_set
				sta		Wave_point,y
;
				lda		Wave_acceleration+1,y
				bne		owari_wave_set
				phy
				iny
				iny
				cpy		#0010h
				bne		No_return
				ldy		#0		
No_return		lda		#01
				sta		Wave_stop_flag,y
				lda		#02
				sta		Banzai_counter,y
				ply
;-------(ＯＡＭセット)----------
owari_wave_set	lda		Wave_point,y
				sta		Now_Wave_point
;
loop_V_set		lda		Now_Wave_point
				sta		oamdat+1,x
				inc		Character_count
				lda		Character_count
				cmp		#02
				beq		Next_character
SetNext_char	inx
				inx
				inx
				inx
				txa	
				cmp		Select_Wave_cmp,y
				bne		loop_V_set
				stz		Character_count
				mem16
				rep		#00100000b	
				tya
				tax
				JSR		(!Select_jmp,x)
Donot_wave		mem16
				rep		#00100000b
				rts
;
Next_character	mem8
				stz		Character_count
				lda		Now_Wave_point
				clc
				adc		#10h
				sta		Now_Wave_point
				jmp		SetNext_char
;--------(ウエーブストップセット)-------
Wave_stop_set	mem8
				lda		#0fch
				sta		Wave_acceleration+1,y
;
				lda		#0
				sta		Wave_stop_flag,y
				sta		Banzai_counter,y
				jmp		owari_wave_set			
;
				mem16
Kong_banzaiset	lda		Banzai_counter,y
				cmp		#0002
				bne		NO_banzaiset
				lda		#3648h
				sta		oamdat+9ah
				ora		#4000h
				sta		oamdat+9eh
NO_banzaiset	rts	
;
Yossy_banzaiset	lda     Banzai_counter,y
				cmp		#0002
				bne		NO_banzaiset
				lda		#304eh
				sta		oamdat+8ah
				ora		#4000h
				sta		oamdat+8eh
				rts
;
Peach_banzaiset	lda     Banzai_counter,y
				cmp		#0002
				bne		NO_banzaiset
				lda		#364ah
				sta		oamdat+22h
				ora		#4000h
				sta		oamdat+26h
				lda		#0f0f8h
				sta		oamdat+28h
				sta     oamdat+2ch
				rts
;
Mario_banzaiset	rts
;
Luigi_banzaiset	rts
;
Kinop_banzaiset	rts
;
Noko_banzaiset	rts
;
Kuppa_banzaiset	lda     Banzai_counter,y
				cmp		#0002
				bne		NO_banzaiset
				lda		#304ch
				sta		oamdat+0ah
				ora		#4000h
				sta		oamdat+0eh
				lda		#0f0f8h
				sta		oamdat+10h
				sta     oamdat+14h
				rts

Select_jmp		DW		Kuppa_banzaiset,Peach_banzaiset	
				DW		Noko_banzaiset,Mario_banzaiset
				DW		Luigi_banzaiset,Kinop_banzaiset
				DW		Yossy_banzaiset,Kong_banzaiset
;*****************************************************
;				文字落下セット
;*****************************************************
Noji_Fall_set	equ		$
				mem16
				ldy		#0
loop_moji_fall	JSR		Moji_one_fall
				iny
				iny
				cpy		#0010h
				bne		loop_moji_fall
				rts
;
Moji_one_fall	equ		$
				tya
				asl		a
				asl		a
				tax

;
				lda		Moji_fall_flag,y
				beq		owari_moji_fall
				stz     Color_timer
;-------(加速度セット)-------
				lda		Moji_acceleration,y
				cmp		#0400h
				beq		moji_speed
				clc
				adc		#0020h
				sta		Moji_acceleration,y
;-----(ポジションセット)-------
moji_speed		mem8
				sep		#00100000b
;
				cpy		#0
				bne		No_T_set
;
				lda		oamdat+0101h
				clc
				adc		Moji_acceleration+1,y
				sta		oamdat+0101h
;
No_T_set		lda		oamdat+0c1h,x
				clc
				adc		Moji_acceleration+1,y
				sta		oamdat+0c1h,x
				inx
				inx
				inx
				inx
;
				lda     oamdat+0c1h,x
				clc
				adc		Moji_acceleration+1,y
				sta		oamdat+0c1h,x
				cmp		#50h
				bcs		Change_set
;
owari_moji_fall	mem16
				rep		#00100000b
				rts
;
;--------(反転セット)-------
Change_set		mem8
				bmi		owari_moji_fall
				lda		Moji_acceleration+1,y			;縦方向反転セット
				lsr		a
				beq		No_moji_fall
				eor		#0ffh
				inc		a
				sta		Moji_acceleration+1,y
;				jmp     owari_moji_fall

				iny
				iny
				lda		#01
				sta		Moji_fall_flag,y
				dey
				dey
				jmp		owari_moji_fall
;
No_moji_fall	lda		#0
				sta     Moji_fall_flag,y
				jmp     owari_moji_fall
;*****************************************************
;				カラー書き換え
;*****************************************************
Color_change_set
				mem16
				inc		Color_timer
				lda		Color_timer
				cmp		#0004
				bne		owari_change_set
				stz		Color_timer
;
				mem8
				sep		#00100000b
				ldx		#00c3h
loop_Color_change
				lda		oamdat,x
				clc
				adc		#02h
				sta		oamdat,x
				cmp		#10h
				bne		no_reset_color
;
				lda		#08h
				sta		oamdat,x
;
no_reset_color	inx
				inx
				inx
				inx
				cpx		#0107h
				bne		loop_Color_change
owari_change_set
				mem16
				rep		#00100000b
				rts
;*****************************************************
;				最終画面セット
;*****************************************************
The_End_set		equ		$
;<<<<<<<<<< フェードコントロールセット >>>>>>>>>>>>
				mem16
				idx16
;
				dec		Fade_timer
				lda		Fade_timer
				bne		owari_fade_set
				lda		#0008h
				sta		Fade_timer
;
				lda		Fade_counter
				clc	
				adc		Fade_set
				and		#00ffh
				sta		Fade_counter
;
				beq		Set_oam
				cmp		#000fh
				beq		NO_fade_set
owari_fade_set	rts
;<<<<<<<<<<<<<< ＴＨＥ　ＥＮＤ　セット　>>>>>>>>>>>
Set_oam			ldx		#0
				lda		#0f0f8h
Oam_clear		sta		oamdat,x
				inx
				inx
				cpx		#0200h
				bne		Oam_clear
;
				ldy		#00c0h
				ldx		#0
Loop_end		lda		The_end_position,x
				sta     oamdat,y
				lda		The_end_data,x
				sta		oamdat+2,y
				JSR     Y_set
				lda		The_end_position,x
				clc
				adc		#0800h
				sta     oamdat,y
				lda		The_end_data,x
				inc		a
				sta		oamdat+2,y
				JSR     Y_set
				inx
				inx
				cpx		#000ch
				bne		Loop_end
				lda		#6848h
				sta		oamdat+0f0h
				lda		#00dah
				sta		oamdat+0f2h
;
				lda		#003ch
				sta		Fade_timer
;
				lda		#000fh
				sta		Fade_set
				rts
;<<<<<<<<<<<<< フェードストップセット >>>>>>>>>>>>>>>>
NO_fade_set		stz		Fade_set
				lda     Ending_timer
				cmp		#049fh
				bne		No_coin_set
;
				lda		#0020h					;コイン音
				JSL		>Trigger_sound_1
;
No_coin_set		rts
;*****************************************************
;					データー
;*****************************************************
;
;<<<<<<<<<<<<<<<< ウエーブ用データー　>>>>>>>>>>>>
Wave_address	DW		0000h,0018h,0030h,0044h
				DW		0058h,006ch,0080h,0090h
Select_Wave_cmp	DW		0018h,0030h,0040h,0054h
				DW		0068h,007ch,0090h,00a0h
;<<<<<<<<< ｔｈａｎｋ　ｙｏｕデーター >>>>>>>>>>>>
Thank_you_data	DW		08c6h,0aaeh,0ca0h,0ebah
				DW		08b4h,0ad0h,0cbch,0ec8h
;
Thank_you_position
				DW		0f040h,0f050h,0f060h,0f070h
				DW		0f080h,0f098h,0f0a8h,0f0b8h
;<<<<<<<<<<<<< Ｔｈｅ　Ｅｎｄ データー >>>>>>>>>>>　
The_end_position
				DW		06850h,06860h,06870h,06888h
				DW		06898h,068a8h
;
The_end_data	DW		0ec6h,0eaeh,0ea8h,0ea8h
				DW		0ebah,0ea6h

				END







