;*********************************************************************
;	mario kart object ラウンド表示
;			  /デモ表示
;					Programed by H.Yajima
;*********************************************************************
		INCLUDE	../../SFX/RP5C77
		INCLUDE	../../SFX/RP5A22
		INCLUDE	../../DSP/D77C25
		INCLUDE	../../../kimura/join/work.def
		INCLUDE	../../../kimura/join/buffer.def
		INCLUDE	../DEF/object.def
		include	../DEF/round.def
;-------------------------------------------------------------------
		glb	Init_round,Main_round,DMA_round
		glb	Init_demoword,Main_demoword
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		ext	param_screenA,param_screenB
		ext	Buffer_pause,Execute_VRAM,BD_sprite,Clear_RAM
		ext	set_pause_char
		ext	Clear_VRAM
;-------------------------------------------------------------------
	comn
work	ds	2
counter	ds	2
;-------------------------------------------------------------------
		EXTEND
objdata	SECT	REL
		mem16
		idx16

;*********************************************************************
;		ラウンド表示 INITIAL
;*********************************************************************
Init_demoword	rts
Main_demoword	rts
;
;Init_round	stz	!pause_status
;		rts
;Main_round	rts
;DMA_round	rtl


		if	1

Init_round
;- - - - - - - - - - - - - - - - - - - - - - MODE CHECK
		stz	!pause_status
		lda	!ending_flag	; エンディング中はセットしない
		bne	noset$
		lda	!o_demo_flag	; デモ中はセットしない！
		bne	noset$
		lda	<game_status	; GP MODE ならばセット!
		beq	entry$
noset$		rts
;- - - - - - - - - - - - - - - - - - - - - - RAM CLEAR
entry$		ldx	#buffer_clear
		jsl	>Clear_RAM
;- - - - - - - - - - - - - - - - - - - - - - ROUND CHECK
		lda	!map_pointer
		cmp	#4
		beq	skip$
;- - - - - - - - - - - - - - - - - - - - - - 通常ラップの時
		asl	a
		adc	#data_number_1a
		tay
		ldx	#round_buffer+00c0h
		jsr	Buffer_pause
		lda	!map_pointer
		asl	a
		adc	#data_number_1b
		tay
		ldx	#round_buffer+02c0h
		jsr	Buffer_pause
		ldy	#data_round_a
		ldx	#round_buffer+0000h
		jsr	Buffer_pause
		ldy	#data_round_b
		ldx	#round_buffer+0200h
		jsr	Buffer_pause
		bra	skip2$
;- - - - - - - - - - - - - - - - - - - - - - ファイナルラップの時
skip$		ldy	#data_final_a
		ldx	#round_buffer+0020h
		jsr	Buffer_pause
		ldy	#data_final_b
		ldx	#round_buffer+0220h
		jsr	Buffer_pause
;- - - - - - - - - - - - - - - - - - - - - - VRAM INITIAL
skip2$		

	;	ldx	#vram_clear
	;	jsl	>Clear_VRAM
	;	ldx	#vram_clear2
	;	jsl	>Clear_VRAM


		ldy	#0000h
		ldx	#0000h
		lda	#5000h
		jsr	Test_clear
		lda	#6000h
		jsr	Test_clear

	;	lda	#001fh
	;	sta	>color_buffer+180h+18h
	;	lda	#7fffh
	;	sta	>color_buffer+180h+1ah
	;	lda	#7739h
	;	sta	>color_buffer+180h+1ch

;- - - - - - - - - - - - - - - - - - - - - - OAM INITIAL
		jsr	param_screenA
		ldx	<screen_register
		lda	!player_count,x
		beq	skip3$
		ldx	#260h
		stx	<sprite_count
		lda	#03064h
		ldy	#round_oam
		jsl	>BD_sprite
skip3$
		jsr	param_screenB
		ldx	<screen_register
		lda	!player_count,x
		beq	exit$
		ldx	#260h+220h
		stx	<sprite_count
		lda	#03064h
		clc
		adc	<screen_obj_V
		ldy	#round_oam
		jsl	>BD_sprite
;- - - - - - - - - - - - - - - - - - - - - - SUB CLEAR		
exit$		SEP	#00100000B
		MEM8
		lda	#055h
		sta	!sprite_buffer+206h
		sta	!sprite_buffer+206h+220h
		REP	#00100000B
		MEM16
;- - - - - - - - - - - - - - - - - - - - - -
		rts
;- - - - - - - - - - - - - - - - - - - - - -

Test_clear	phx			; >>
		ldx	#080h	
		stx	!Screen_step
		sta	!Screen_address
		lda	#20h
		sta	<counter
		pla			; <<
main_loop$	ldx	#8
loop$		sty	!Screen_write
		dex
		bne	loop$
		ldx	#8
loop2$		sta	!Screen_write
		dex
		bne	loop2$		
		dec	<counter
		bne	main_loop$
		
		rts

;*********************************************************************

round_oam	db	4+80h		; big size
		dw	0000h,0800h
		db	0
		dw	0010h,0802h
		db	0
		dw	0020h,0804h
		db	0
		dw	0030h,0806h
		db	0

data_round_a	db	uR,uO,uU,uN,uD,0
data_round_b	db	dR,dO,dU,dN,dD,0
data_final_a	db	uF,uI,uN,uA,uL,0
data_final_b	db	dF,dI,dN,dA,dL,0

data_number_1a	db	0cah,0
data_number_2a	db	0cch,0
data_number_3a	db	0ceh,0
data_number_4a	db	0d0h,0
data_number_1b	db	0cbh,0
data_number_2b	db	0cdh,0
data_number_3b	db	0cfh,0
data_number_4b	db	0d1h,0

buffer_clear	DW	0000H 		( 2 byte clear patern )
		DL	round_buffer	( RAM start address )
		DW	0400H		( RAM clear count )

vram_clear	dw	0000h
		dw	5000h
		dw	0200h
vram_clear2	dw	0000h
		dw	6000h
		dw	0200h

;--------------------------------------------------------------------

;*********************************************************************
;		ラウンド表示 NMI
;*********************************************************************
dmaexit		plb
		rtl

DMA_round	PHB
		PHK
		PLB
		lda	!round_pointer
		bpl	dmaexit
		bit	#0020h
		beq	entry$
		stz	!round_pointer
		bra	dmaexit
;
entry$
		ldx	#81h		; oam pointer 20h skip
		stx	!Screen_step

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; ROM address の計算
		bit	!round_pointer
		bvc	skip00$
		eor	#001fh
skip00$		and	#00ffh
		bit	#0010h
		beq	rom_open$
		and	#0ffefh
		bit	#0008h
		beq	rom_next$
;- - - - - - - - - - - - - - - - - - - 
		ora	#0100h
rom_next$	ora	#0008h
		bra	skip0$
;- - - - - - - - - - - - - - - - - - - 
rom_open$	bit	#0008h
		beq	skip0$
		and	#0007h
		ora	#0100h
skip0$		asl	a		; *2
		bit	!round_pointer
		bvc	skip1$
		clc
		adc	#00100h
skip1$		tax			; ROM index
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; VRAM address の計算
		lda	!round_pointer
		and	#00ffh
		bit	!round_pointer
		bvc	skip2$
		eor	#001fh
skip2$		bit	#0010h
		beq	vram_open$
		and	#0ffefh
		bit	#00008h
		beq	vram_next$
		ora	#00100h
vram_next$	ora	#00008h
		bra	skip$
vram_open$	bit	#00008h
		beq	skip$
		and	#00007h
		ora	#00100h			; 0100 -
skip$		ora	#05000h			; 5000 -
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		ldy	!player_count+0
		beq	p2$	
		jsr	set_1		; 1P 側
p2$		ldy	!player_count+2
		beq	p3$	
		clc
		adc	#01000h
		jsr	set_1		; 2P 側
p3$
		inc	!round_pointer
		
exit$		plb
		rtl

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set_1		phx
		pha
		jsr	setvram
		clc
		adc	#10h		; VRAM 10H +
		tay
		txa
		clc
		adc	#20h		; ROM 20H +
		tax
		tya
		jsr	setvram
		pla
		plx
		rts
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
setvram		pha				;  <<
		phx				;<<
		sta	!Screen_address+0
		ldy	#4
loop$		lda	>round_buffer,x
		sta	!Screen_write+0
		txa
		clc
		adc	#40h
		tax
		dey
		bne	loop$
		plx				;>>
		pla				;  >>
		rts
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


;*********************************************************************
;		ラウンド表示 MAIN
;*********************************************************************
Main_round
		lda	!round_process
		asl	a
		tax
		jmp	(!process$,x)
;----------------------------------------------------------------------
process$	dw	round_off	; ラウンド表示なし
		dw	round_open	; ラウンド表示開始
		dw	round_disp	; ラウンド表示中
		dw	sprite_erase	; 表示消去/ケムリ再転送(1P)
		dw	round_close	; ケムリ再転送(2P)
;----------------------------------------------------------------------
;		ラウンド表示なし
;----------------------------------------------------------------------
round_off	rts
;----------------------------------------------------------------------
;		ラウンドキャラ転送開始
;----------------------------------------------------------------------
round_open	bit	!o_demo_flag	; デモ中ならばセットしない！
		bpl	skip$
		stz	!round_process
		rts
skip$		lda	#180
		sta	!round_timer
		inc	!round_process
		lda	#08000h			; 転送セット
		sta	!round_pointer
		lda	#0AAh
		ldx	#6
		bra	disp_obj
;----------------------------------------------------------------------
;		 表示ウェイト			<< WAIT >>		
;----------------------------------------------------------------------
round_disp	dec	!round_timer
		bne	exit$
		lda	#0c000h			; 消去セット
		sta	!round_pointer
		lda	#40h
		sta	!round_timer
		inc	!round_process
exit$		rts
;----------------------------------------------------------------------
;		表示消去/ケムリ再転送(1P)	<< 1 frame >>
;----------------------------------------------------------------------
sprite_erase
		dec	!round_timer
		bne	exit$
;
		lda	#08h
		sta	!GP_limitter
;
		inc	!round_process
		ldy	#0			; 1P側
		jsr	trans_kemuri
		lda	#055h			; 表示消去
		ldx	#6
		jmp	disp_obj
exit$		rts
;----------------------------------------------------------------------
;		ケムリ再転送(2P)		; << 1 frame >>
;----------------------------------------------------------------------
round_close
		stz	!round_process
		ldy	#2			; 2P側
		bra	trans_kemuri	



;- - - - - -  UTILITY - - - - - -  - - - - -
;----------------------------------------------------------------------
						; 表示の ＯＮ／ＯＦＦ
disp_obj	SEP	#00100000B
		MEM8
		bit	<player_index+0+1	; 1P MODE
		bmi	skip$
		sta	!sprite_buffer+200h,x
skip$		bit	<player_index+2+1	; 2P MODE
		bmi	exit$
		sta	!sprite_buffer+420h,x
exit$		REP	#00100000B
		MEM16
		rts
;----------------------------------------------------------------------
						; ケムリ再転送
trans_kemuri	lda	!player_count,y
		beq	skip$
		ldx	#2			; ケムリキャラ転送
		jsr	set_pause_char		
skip$		rts
;----------------------------------------------------------------------

	endif




	if	0
;*********************************************************************
;		デモメッセージ表示 INITIAL
;*********************************************************************
demoword_oam	db	8+80h		; big size
		dw	0000h,0820h+06h
		db	0
		dw	0800h,0820h+07h
		db	0
		dw	0008h,0820h+08h
		db	0
		dw	0808h,0820h+09h
		db	0
		dw	0010h,0820h+18h
		db	0
		dw	0810h,0822h+19h
		db	0
		dw	0018h,0820h+1ch
		db	0
		dw	0818h,0822h+1dh
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - -- 
demoword_1	db	80H
		dw	5200h
		dw	1801H
		dl	MOJI_buffer+800h
		dw	800h		
demoword_2	db	80H		
		dw	6200h		
		dw	1801H		
		dl	MOJI_buffer+800h
		dw	800h		
;---------------------------------------------------------------------
Init_demoword	bit	!o_demo_flag
		bpl	exit$
;- - - - - - - - - - - - - - - - - - - - - - VRAM  INITIAL
;
		ldx	#demoword_1
		jsl	>Execute_VRAM
		ldx	#demoword_2
		jsl	>Execute_VRAM
;
;- - - - - - - - - - - - - - - - - - - - - - OAM INITIAL
;
		jsr	param_screenA
		ldx	#200h
		stx	<sprite_count
		lda	#03064h
		ldy	#demoword_oam
		jsl	>BD_sprite
;
		jsr	param_screenB
		ldx	#200h+220h
		stx	<sprite_count
		lda	#03064h
		clc
		adc	<screen_obj_V
		ldy	#demoword_oam
		jsl	>BD_sprite
;
;- - - - - - - - - - - - - - - - - - - - - - SUB CLEAR		
		lda	#05555h
		sta	!sprite_buffer+0
		sta	!sprite_buffer+220h
exit$		rts
;---------------------------------------------------------------------
Main_demoword	bit	!o_demo_flag
		bpl	exit$
		ldy	#55h
		lda	<frame_counter
		and	#01000b
		beq	skip$
		ldy	#00h
skip$		tya				; デモスプライトの表示
		ldx	#0
		jsr	disp_obj
		inx
		jmp	disp_obj
exit$		rts
;---------------------------------------------------------------------
	endif
		END
