;************************* runed2.asm **********************
		extend
;
	include 	label
	include		label_3
;
		ext		select_erase,MPLA_SET,DISP_SET,LDPLC_SET
		ext		TYPE_SUB,set_code,PTANUM4,speed_color,Trigger_sound_1
;
		glb		ERSAREA_SET,ERASE_SUQ,ERASE_TRG1,ERASE_TRG2
		glb		ERASE_PALL,ERASE_PALL1,SPEED_SET
		glb		POINT_SET,ERASE_SET,POSI_MOVE1,POSDSP_SET1
;----------------------------------------------------
editer      sect    rel
			mem8
			idx8
;=====================================================
;***************************************************
;			AREA SET MODE SUB ROUTINE
;***************************************************
;=====================================================
;
;*************************************
;			ERASE AREA SET
;*************************************
ERSAREA_SET	equ		$
			idx8
			mem8
			lda		modeflg
			cmp		#01
			beq     owari_212
;-------------------------------
;			lda		slectflg		;modeB?:yes-->owari_14
;			cmp		#01h
;			beq		owari_212
;--------------------------------
			lda		trg_button1
			cmp		#40h
			bne		owari_212
;--------------------------------
			idx16					;(same address check &
			rep		#00010000b		;flag set. 7e8000h)
			ldx		#02
loop_212	lda		1400h,x
			inx
			cmp		poscnt_x
			bne		adcx1			
;
			lda		1400h,x
			cmp		poscnt_y
			beq		seterase	
;
adcx1		inx
			inx
			inx 
			cpx		#202h
			bne		loop_212
owari_212	idx8
			sep		#00010000b
			rts
;----------------------------------
seterase	idx16
			dex						;(7e8000h zero set.)
			dex
			dex							
;
			ldy		#0
			lda		#0
loop_988	sta		1400h,x
			inx
			iny
			cpy		#04
			bne		loop_988			
;
			idx8
			mem8
			sep		#00110000b
;----------------------------------
			idx16					;(same address check &
			rep		#00010000b		;flag set from 7e8200h)
			ldx		#01
loop_222	lda		1600h,x
			inx
			cmp		poscnt_x
			bne		adcx7			
;
			lda		1600h,x
			stx		pointcnt
			cmp		poscnt_y
			beq		seterase1	
;
adcx7		inx
			inx
			inx 
			inx
			inx
			inx
			inx
			jmp		loop_222
;
seterase1	dex
			dex
			lda		1600h,x
			sta		subnum
;
			idx8
			sep		#00010000b
;-----------------------------------------
			ldx		subnum
			JSR     (!select_erase,x)	;position set & (pos) set..
;-----------------------------------------
loop_982	JSR     MPLA_SET			;place set.
;-----------------------------------------
			JSR		ERSDSP_SET
;-----------------------------------------
			ldx		subnum				;(pos) counter set.
			JSR		(!TYPE_SUB,x)
			lda		owari_flg
			cmp		#01
			bne		loop_982
;------------------------------------------
			mem8
			idx8
			sep		#00110000b
			ldx		subnum
			JSR     (!set_code,x)		;code erase set.
			JSR     MPLA_SET			;place set
			JSR		ERSDSP_SET			;erase dosplay set.
;------------------------------------------
			stz		owari_flg
			stz     pointcnt			
			rts
;
;============(SUB ROUTINE)================
;
;************************************
;			BG2 ERASE SUQARE
;************************************
ERASE_SUQ	equ		$
			idx16
			rep		#00010000b
			ldx		pointcnt
			inx
			lda		1600h,x
			dec		a
			sta		poscnt_x1
			inx
;
			lda		1600h,x
			dec		a
			sta		poscnt_y1
;
			dex
			dex
			dex	
			lda		1600h,x
			clc
			adc		poscnt_x1
			sta		poscnt_x2
;
			inx
			lda		1600h,x
			clc
			adc		poscnt_y1
			sta		poscnt_y2
;-------------------------------
			JSR		LDPLC_SET		;(pos) set.
			rts
;************************************
;			BG2  TRIANGLE(02,04)
;************************************
ERASE_TRG1	equ		$
			idx16					;positionset.
			rep		#00010000b
			stz		poscnt_x1
;
			ldx		pointcnt
			inx
			lda		1600h,x
			dec		a
			sta		poscnt_y1
;
			dex
			dex	
			lda		1600h,x
			clc
			adc		poscnt_x1
			sta		poscnt_x2
			inx
			lda		1600h,x
			clc
			adc		poscnt_y1
			sta		poscnt_y2
;-------------------------------
			JSR		LDPLC_SET		;(pos) set.
			rts
;************************************
;			BG2  TRIANGLE(06,08)
;************************************
ERASE_TRG2	equ		$				;positionset.
			idx16
			rep		#00010000b
			stz		poscnt_x1
;
			ldx		pointcnt
			inx
			lda		1600h,x
			dec		a
			sta		poscnt_y1
;
			dex
			dex	
			lda		1600h,x
			clc
			adc		poscnt_x1
			sta		poscnt_x2
			inx
			lda		1600h,x
			sec
			sbc		poscnt_y1
			sta		poscnt_y2
;
			lda		poscnt_y1
			eor		#0ffh
			inc		a
			sta		poscnt_y1
;-------------------------------
			JSR		LDPLC_SET		;(pos) set.
			rts
;************************************
;			BG2  PALALLER(0a)
;************************************
ERASE_PALL	equ		$			;positionset.
			idx16
			rep		#00010000b
			ldx		pointcnt
			inx
			lda		1600h,x
			dec		a
			sta		poscnt_x1
			inx
;
			lda		1600h,x
			dec		a
			sta		poscnt_y2
;
			dex
			dex
;
			lda		1600h,x
			sta		poscnt_y1
			dex
;
			lda     1600h,x
			sec
			sbc		poscnt_x1
			sta		poscnt_x2
			inx

;
			lda		1600h,x
			clc
			adc		poscnt_y2
			clc
			adc		poscnt_x1
			sta		poscnt_y2
;
			sec
			sbc		poscnt_y1
			sta     poscnt_y1
;		
			lda		poscnt_x1
			eor		#0ffh
			inc		a
			sta		poscnt_x1
;-------------------------------
			JSR		LDPLC_SET		;(pos) set.
			rts
;************************************
;			BG2  PALALLER(0c)
;************************************	
ERASE_PALL1	equ		$				;positionset.
			idx16
			rep		#00010000b
			ldx		pointcnt
			inx
			lda		1600h,x
			dec		a
			sta		poscnt_x1
			inx
;
			lda		1600h,x
			dec		a
			sta		poscnt_y2
;
			dex
			dex

			lda		1600h,x
			sta		poscnt_y1
			dex
;
			lda     1600h,x
			clc
			adc		poscnt_x1
			sta		poscnt_x2
			inx

;
			lda		1600h,x
			clc
			adc		poscnt_y2
			clc
			adc		poscnt_x1
			sta		poscnt_y2
;
			sec
			sbc		poscnt_y1
			sta		poscnt_y1
;-------------------------------
			JSR		LDPLC_SET		;(pos) set.
			rts
;
;***********************************
;			ERASE AREA DISP
;***********************************
ERSDSP_SET	equ		$
			idx16						;erasearea set display.
			rep		#00010000b
			ldx		place_set
			lda		#02
			sta		>7f0000h,x
			idx8
			sep		#00010000b
;
			idx16
			rep		#00010000b
			ldx		place_set
			lda		#01
			dex
			sta		>7f0000h,x
			idx8
			sep		#00010000b
			rts
;
;=====================================================
;***************************************************
;			TERGET POINT SET ROUTINE
;**************************************************
;=====================================================
;*****************************************
;			POSITION MOVE
;*****************************************
POSI_MOVE1	equ		$
			lda		modeflg
			cmp		#01
			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_rit2
;
			cmp		#06
			beq		d_lft2
;
			cmp		#09
			beq		u_rit2
;
			cmp		#0ah
			beq		u_lft2
			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_rit2		lda		button
			cmp		#40h
			bne		owari_15
;
			JSR		sccrit2
			JSR		sccdwn2
			rts
;
d_lft2		lda		button
			cmp		#40h
			bne		owari_15
;
			JSR		sccdwn2
			JSR		scclft2
			rts
;
u_rit2		lda		button
			cmp		#40h
			bne		owari_15
;
			JSR		sccup2
			JSR		sccrit2
			rts
;
u_lft2		lda		button
			cmp		#40h
			bne		owari_15
;
			JSR		sccup2
			JSR		scclft2
owari911	rts
;=========================================
;
;
;---------------------------------
lftmov1		jmp		lftmov
ritmov1		jmp		ritmov
upmov1		jmp		upmov
;---------------------------------
dwnmov		lda		button
			cmp		#40h
			beq		sccdwn1	
;
			lda		poscnt_y
			cmp		#7fh
			beq		owari911
;
			lda		oamdat+1		;Yposition up.
			clc
			adc		#08h
			
			cmp		#0c0h
			beq		sccdwn1
			sta		oamdat+1
			sta		oamset1
;
			lda		bgadd			;address set.
			clc
			adc		#80h
			sta		bgadd
			sta     bgaddl
;			sta		adress
			sta		bufadd
;
			lda		bgadd1
			adc		#0
			sta		bgadd1
			sta     bgaddh
;			sta		adress+1
			sta     bufadd+1
			jsr		posyadc			;kasoru position check.
			jmp		owari_1
owari_60	rts
;
upmov		lda		button
			cmp		#40h
			beq		sccup1	
;
			lda		poscnt_y
			beq		owari911
;
			lda     oamdat+1		;Yposition down.
			sec
			sbc		#08h
			cmp		#20h
			beq		sccup1
			sta		oamdat+1
			sta		oamset1
;
			lda		bgadd			;address set.
			sec
			sbc		#80h
			sta		bgadd
			sta     bgaddl
;			sta		adress
			sta     bufadd
;
			lda		bgadd1
			sbc		#0
			sta		bgadd1
			sta     bgaddh
			sta     bufadd+1
;			sta		adress+1
			jsr		posysbc			;kasoru position check.
			jmp     owari_1
;----------------------------------
sccdwn1		jmp		sccdwn2
sccup1		jmp		sccup2
;----------------------------------
ritmov		lda		button
			cmp		#40h
			beq		sccrit1	
;
			lda		poscnt_x
			cmp		#7fh
			beq		owari_1
;
			lda		oamdat			;Xposition right.
			clc
			adc		#08h
			cmp		#0d0h
			beq		sccrit1
			sta		oamdat
			sta		oamset
;
			lda		bgadd			;address set.
			clc
			adc		#01h
			sta		bgadd
			sta     bgaddl
;			sta		adress
			sta     bufadd
;
			lda		bgadd1
			adc		#0
			sta		bgadd1
			sta     bgaddh
;			sta		adress+1
			sta     bufadd+1
			jsr		posxadc			;kasoru position check.
			jmp     owari_1
;--------------------------------
			rts
;--------------------------------
sccrit1		jmp		sccrit2
;--------------------------------
lftmov		lda		button
			cmp		#40h
			beq		scclft1	
;
			lda		poscnt_x
			beq		owari_1
;
			lda		oamdat			;Xposition left.
			sec
			sbc		#08h
			cmp		#30h
			beq		scclft1
 			sta     oamdat
			sta		oamset
;
			lda		bgadd			;address set.
			sec
			sbc		#01h
			sta		bgadd
			sta     bgaddl
;			sta		adress
			sta     bufadd
;
			lda		bgadd1
			sbc		#0
			sta		bgadd1
			sta     bgaddh
;			sta		adress+1
			sta     bufadd+1
			jsr		posxsbc			;kasoru position check.
owari_1		rts			
;------------------------------------------
scclft1		jmp		scclft2
;-------------------------------------------
sccup2		equ		$
			lda		poscnt_y
			cmp		#0
			beq		owari_959
			idx8
			mem16
			rep		#00100000b
			lda		sccadd
			sec
			sbc		#80h
			sta		sccadd
			sta		sccaddl
;
			lda		sccadd_l
			sec
			sbc		#80h
			sta		sccadd_l
			sta		sccadd2
;
			lda		bgadd
			sec
			sbc		#80h
			sta		bgadd
			sta		bgaddl
;			sta		adress
			sta     bufadd
;
			idx8
			mem8
			sep		#00100000b
			jsr		posysbc
owari_959	mem8
			sep		#00100000b
			rts
;
sccdwn2		lda		poscnt_y
			cmp		#07fh
			beq		owari_959
;
			idx8
			mem16
			rep		#00100000b
;
			lda		bgadd
			clc
			adc		#80h
			sta		bgadd
			sta		bgaddl
;			sta		adress
			sta     bufadd
;
			lda		sccadd
			clc
			adc		#80h
			sta		sccadd
			sta		sccaddl
			sta		sccset
;
			lda		sccadd_l
			clc
			adc		#80h
			sta		sccadd_l
			sta		sccadd2
			sta		sccset2
;
			idx8
			mem8
			sep		#00100000b
			jsr		posyadc
			rts
;
sccrit2		lda		poscnt_x
			cmp		#07fh
			beq		owari_954
;
			idx8
			mem16
			rep		#00100000b
			lda		sccadd
			clc
			adc		#01h
			sta		sccadd
			sta		sccaddl
			sta		sccset
;
			lda		sccadd_l
			clc
			adc		#01h
			sta		sccadd_l
			sta		sccadd2
			sta		sccset2
;
			lda		bgadd
			clc
			adc		#01h
			sta		bgadd
			sta		bgaddl
;			sta		adress
			sta     bufadd
;
			idx8
			mem8
			sep		#00100000b
			jsr		posxadc
owari_954	idx8
			mem8
			sep		#00100000b
			rts
;
scclft2		lda		poscnt_x
			cmp		#0
			beq		owari_954
		
			idx8
			mem16
			rep		#00100000b
			lda		sccadd
			sec
			sbc		#01h
			sta		sccadd
			sta		sccaddl
			sta		sccset
;
			lda		sccadd_l
			sec
			sbc		#01h
			sta		sccadd_l
			sta		sccadd2
			sta		sccset2
;

			lda		bgadd
			sec
			sbc		#01h
			sta		bgadd
			sta		bgaddl
;			sta		adress
			sta     bufadd
;
			idx8
			mem8
			sep		#00100000b
			jsr		posxsbc
			rts
;-------------------------------------------
;									(KASORU POSITION SET)
posyadc		equ		$
			idx8
			mem8
			sep		#00110000b
			lda		posdsp_y
			clc
			adc		#08h
			sta		posdsp_y
			lda		posdsp_y+1
			adc		#0
			sta		posdsp_y+1
			mem16
			rep		#00100000b
			lda		posdsp_y
			sta		posdp_y
			lsr		a
			lsr		a
			lsr		a
			mem8
			sep		#00100000b
			sta		poscnt_y
			sta		posct_y
			rts
;
;
posysbc		equ		$
			idx8
			mem8
			sep		#00110000b
			lda		posdsp_y
			sec
			sbc		#08h
			sta		posdsp_y
			lda		posdsp_y+1
			sbc		#0
			sta		posdsp_y+1
			mem16
			rep		#00100000b
			lda		posdsp_y
			sta		posdp_y
			lsr		a
			lsr		a
			lsr		a
			mem8
			sep		#00100000b
			sta		poscnt_y
			sta		posct_y
			rts
;;
posxadc		equ		$
			idx8
			mem8
			sep		#00110000b
			lda		posdsp_x
			clc
			adc		#08h
			sta		posdsp_x
			lda		posdsp_x+1
			adc		#0
			sta		posdsp_x+1;
			mem16
			rep		#00100000b
			lda		posdsp_x
			sta		posdp_x
			lsr		a
			lsr		a
			lsr		a
			mem8
			sep		#00100000b

			sta		poscnt_x
			sta		posct_x
			rts
;
posxsbc		equ		$
			idx8
			mem8
			sep		#00110000b
			lda		posdsp_x
			sec
			sbc		#08h
			sta		posdsp_x
			lda		posdsp_x+1
			sbc		#0
		  	sta		posdsp_x+1
			mem16
			rep		#00100000b
			lda		posdsp_x
			sta		posdp_x
			lsr		a
			lsr		a
			lsr		a
			mem8
			sep		#00100000b
			sta		poscnt_x
			sta		posct_x
			rts
;
;
;*****************************************
;			POSITION DISPLAY
;*****************************************
POSDSP_SET1	equ		$				;(X POSITION)
			idx8
			mem8
			sep		#00110000b
;---------------------------------
			lda		modeflg
			cmp		#01
			beq     owari_14
;--------------------------------
;			lda		<slectflg		;modeB?:yes-->owari_14
;			cmp		#01h
;			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
;
;****************************************
;			BG_SET	DISPLAY
;****************************************
POINT_SET	equ		$
;---------------------------------
			lda		modeflg
			cmp		#01
			beq     owari_14
;------------------------------------
			lda		trg_button1
			and		#0ffh
			cmp		#80h
			bne		owari_14
;--------------------------------
			lda		setcnt			;terget counter chech.
			cmp		#80h
			beq		owari_70
;--------------------------------
			mem16
			rep		#00100000b
			lda		#0020h
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b
			idx16
			rep		#00010000b
			ldx		#0				;same address check.
loop_968	lda		1a00h,x
			cmp		poscnt_x
			bne		inxset
;
			inx
			lda     1a00h,x
			cmp		poscnt_y
			beq		owari_70
			dex
;
inxset		inx
			inx
;
			cpx		#100h
			bne		loop_968
			idx8
			sep		#00010000b
;------------------------------
setget		jsr		BUFCK_SET		;buffer set to 7e8600h.
;------------------------------	
	   		idx16					;point set to 7f4000h.
			rep		#00010000b
			ldx		bgadd
			lda		#0f0h
			sta		>7f4000h,x
;
			lda		trgflg
			cmp		#01
			beq		owari_73		
;			
			lda		setcnt
			inc		a
			sta		setcnt		
;
owari_73	stz		trgflg
owari_70	idx8
			sep		#00110000b
			rts
;
;*****************************************
;			ERASE_SET	(BG)
;*****************************************
ERASE_SET	equ		$
;---------------------------------
			lda		modeflg
			cmp		#01
			beq     owari_70
;------------------------------------
			lda		trgflg
			bne		owari_70
;---------------------------------
			lda		trg_button1
			cmp		#40h
			bne		owari_70
;--------------------------------

			stz		buffpos
			ldx		#0				;same address check.
loop_969	lda		1a00h,x
			cmp		poscnt_x
			bne		inxset1
;
			inx
			lda     1a00h,x
			cmp		poscnt_y
			beq		ers_set	
			dex
;
inxset1		inx
			inx
			stx		buffpos
			cpx		#0
			bne		loop_969
			jmp		owari_70
;--------------------------------
ers_set		idx16
			rep		#00010000b
;
			ldx		bgadd
			lda		>7f8000h,x
			sta		>7f4000h,x
;
			idx8
			sep		#00010000b
;
			jsr		BUFERS_SET2
;
			lda		#01
			sta		trgflg
			mem16
			rep		#00100000b
			lda		#0027h
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b
			rts
;
;******************************************
;			SPEED SET
;******************************************			
SPEED_SET	equ		$
			lda		modeflg
			cmp		#01
			beq     owari_70
;--------------------------------
			ldx		#0				;same address check.
			stz		speed_cnt
loop_964	lda		1a00h,x
			cmp		poscnt_x
			bne		inxset4
;
			inx
			lda     1a00h,x
			cmp		poscnt_y
			beq		sped_chang	
			dex
;
inxset4		inx
			inx
;
			lda		speed_cnt
			inc		a
			sta		speed_cnt				
			cpx		#0
			bne		loop_964
;--------------------------------
			ldx		#6ah			;speed display 0 set.
			lda		#0
			sta		oamdat,x
;
			ldx		#66h
			sta		oamdat,x
;
			lda		#00fh
			ldx		#0aeh
			sta		oamdat,x
			ldx		#0b2h
			sta		oamdat,x
			jmp		owari_70
;--------------------------------
sped_chang	ldx		speed_cnt		;speed display set.
			lda		1b00h,x
;
			ldx		#6ah
			pha
			and		#00001111b
			sta		oamdat,x
;
			ldx		#66h
			pla
			and		#11110000b
			lsr		a
			lsr		a
			lsr		a
			lsr		a
			sta		oamdat,x
;
			ldx		#0b2h
			lda		speed_cnt
			and		#0fh
			sta		oamdat,x
;
			ldx		#0aeh
			lda		speed_cnt
			and		#0f0h
			lsr		a
			lsr		a
			lsr		a
			lsr		a
			sta		oamdat,x
;---------------------------------
			lda		trg_button
			cmp		#40h
			bne		owari_09
;
			mem16
			rep		#00100000b
			lda		#002eh
			JSL		>Trigger_sound_1
			mem8
			sep		#00100000b

;--------------------------------
			ldx		speed_cnt		;speed up set.
			lda		1b00h,x
;
			inc		a
			cmp		#04
			beq		z_speed
z_spset		sta		1b00h,x
;			
			ldx		#6ah
			pha
			and		#00001111b
			sta		oamdat,x
;
			ldx		#66h
			pla
			and		#11110000b
			lsr		a
			lsr		a
			lsr		a
			lsr		a
			sta		oamdat,x
;-----------------------------------
owari_09	ldx		speed_cnt		;speed color set.
			lda		1b00h,x
			tax
			lda		speed_color,x
	   		idx16					
			rep		#00010000b
			ldx		bgadd
			sta		>7f4000h,x
			idx8
			sep		#00010000b		

			rts
;
z_speed		lda		#0
			jmp		z_spset
;================================================
;*****************************************
;			7e8600h BUFFER SET INSERT DATA
;*****************************************
;================================================
BUFCK_SET	equ		$     
			lda		trgflg
			cmp		#01
			beq		nocnt
;-----------------------------------
			lda		setcnt			;oder check.
			asl		a
			tax
			lda		poscnt_x
			sta		1a00h,x
			inx
;
			lda		poscnt_y
			sta		1a00h,x
;
owari_37	rts
;------------------------------------
nocnt		ldx		buffpos
			lda		poscnt_x
			sta		1a00h,x
			inx
;
			lda		poscnt_y
			sta		1a00h,x
;
			rts
;*****************************************
;			ERASE DAT SET
;*****************************************
BUFERS_SET2	equ		$
			mem16
			rep		#00100000b
			ldx		buffpos
			lda		#0ffffh
			sta		1a00h,x
			mem8
			sep		#00100000b
			lda		buffpos
			lsr		a
			tax
			stz		1b00h,x
			rts
;
			END

