	 INCLUDE mn_hp_mr3ram.asm
;
;<MR3MAIN>================================================================================
         EXT      JSRSUB,PSCHG0
         EXT      RAMADR,BGADDT1,BGADDT2 
; 
;<MR3BGD1>================================================================================
         EXT      GCIN1,BLBC0,JUMP1,DOOR0,BKBT0,SPJP0,ENQST,GCOIN 
         EXT      BBLK0,NSBLK,NSJMP,NSJPY,CNBLK,BKBT1 
; 
;<MR3BGD3>================================================================================
         EXT      MARUB 
; 
;<MR3BGD4>================================================================================
         EXT      BOX04,BOX05,BOX07 
; 
;<MR3BGD5>================================================================================
         EXT      BGBK0,BGBK1,BGBK2,BGBK3,BGBC0,BWAR0,BWAR1,BWAR2,BWAR3 
; 
;<MR3BGD6>================================================================================
         EXT      PAI00,PAI05,PBT20 
; 
;<MR3BGD8>================================================================================
         EXT      TAI00,TAI01,TAI02,TAI03,TAI04,TAI05,TAI06,TAI07 
; 
;<MR3DAT3>================================================================================
         GLB      BGUTCNG,CLCNCHG 
; 
         GLB      CONB,CLRB,JMPB,DORB,BOTB,BBXB,SBXB,SUPB,BG0B,BG1B 
         GLB      WARB,HATB,JPYB,JPTB,TENB,TAIB,PA0B,PA1B,PA2B,MRTB 
         GLB      BG2B,IPKB,ICNB,PSWB 
; 
;<MR3DATA0>===========================================================================
; -------------------------------------------------------------JSR    BGCLEAR
	EXT	CLRUNTD,CLRDT
; -------------------------------------------------------------JSR    DOORCHG
	EXT	DORADRD,DRDDB0
; -------------------------------------------------------------JSR    ITMBOX0
	EXT	BOXDAT0,BXDT0B0
; -------------------------------------------------------------JSR    TAIHO00
	EXT	TAIHD0,TAIHD1,TAIHD2,TAIHD5,TAIHD6,TAIHD7
	EXT	TAIHD3,TAIHD4
	EXT	TIHD0B0
; -------------------------------------------------------------JSR    CLCNCHG
	EXT	CLCHGD
; -------------------------------------------------------------
	GLB	CONB,CLRB,JMPB,JPYB,JPTB,SUPB,WARB,HATB,BOTB,SBXB
	GLB	TENB,PA0B,PA1B,PA2B,MRTB,IPKB,ICNB,PSWB,DORB,BBXB
	GLB	TAIB,BG0B,BG1B,BG2B
;-----------------------------------------------------------------------------------------
;
         ORG      0278000H
		EXTEND
		IDX8
		MEM8
; 
;-----------------------------------------------------------------------------------------
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;x       JSR      BGUTCNG     ( BG unit-no. data chenge sub )        x
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
BGUTCNG  EQU      $ 
         LDA      !HVSRFG         ;v or h ? 
         BEQ      BTG130          ;h
         LDA      !ONPUYL 
         LDY      !ONPUYH 
         JSL      >PSCHG0
         STA      !ONPUYL
         STY      !ONPUYH
;
;;;         LDY      >ONPUYH         ;v 
         LDA      BGADDT1,Y 
         STA      <BUFFNO
         LDA      BGADDT2,Y 
         STA      <BUFFNO+1
; 
         LDA      !ONPUYL 
         AND      #0F0H 
         STA      <WORK5 
; 
         LDA      !ONPUXL 
         LSR      A 
         LSR      A 
         LSR      A 
         LSR      A 
         ORA      <WORK5 
         STA      <WORK4 
; 
         LDA      #00H
         STA      <WORK6 
         BEQ      BTG150          ;jmp BGT150 
; 
BTG130   EQU      $ 
         LDA      !ONPUXH 
         ASL      A 
         TAX
         LDA      !RAMADR,X 
         STA      <BUFFNO
         LDA      !RAMADR+1,X 
         STA      <BUFFNO+1
; 
         STZ      <WORK6 
; 
         LDA      !ONPUYH 
         BEQ      BTG100
; 
         INC      <BUFFNO+1
; 
BTG100   EQU      $ 
         LDA      !ONPUYL 
         AND      #11110000B
         STA      <WORK5 
; 
         LDA      !ONPUXL 
         LSR      A 
         LSR      A 
         LSR      A 
         LSR      A 
         ORA      <WORK5 
         STA      <WORK4 
; 
         LDA      !ONPUYH
         BNE      BTG120
;
         LDA      !ONPUYL 
         AND      #11110000B
         CMP      #11110000B
         BNE      BTG150
; 
BTG120   EQU      $ 
;;;;         LDA      !ONPUYL	;;;S.FAMICON ha 3C0H no COLOR 
;;;;         CLC		;;;AREA wo habukanakute iikara
;;;;         ADC      #010H 	;;;kono PLOGRAM ha iranai
;;;;         STA      <WORK5 
; 
         LDA      #01H
         STA      <WORK6 
; 
BTG150   EQU      $ 
         LDA      !KINOSW 
         BNE      BTG200
; 
         RTL
; 
BTG200   EQU      $ 
	 JSR	  BTG240
	 RTL
;--------------------------------
BTG240   EQU      $ 
         JSL      >JSRSUB
BGCHG    WORD     BLR150      ; 0 nashi 
CONBG    WORD     BGCLEAR     ; 1 coin unit-no. data set sub
CLRBG    WORD     BGCLEAR     ; 2 jump dai BG data clear sub
JMPBG    WORD     BGCLEAR     ; 3 all cross jump dai BG data set sub
JPYBG    WORD     BGCLEAR     ; 4 left & rigth jump dai BG data set sub 
JPTBG    WORD     BGCLEAR     ; 5 up & down jump dai BG data set sub
SUPBG    WORD     BGCLEAR     ; 6 supar jump dai BG data set sub
WARBG    WORD     BGCLEAR     ; 7 wareru block BG data set sub
HATBG    WORD     BGCLEAR     ; 8 hatena block BG data set sub
BOTBG    WORD     BGCLEAR     ; 9 bakudan swich BG data set sub 
SBXBG    WORD     BGCLEAR     ; A Item box open small BG data set sub 
TENBG    WORD     BGCLEAR     ; B ten coin BG data set sub
PA0BG    WORD     BGCLEAR     ; C paip wareru BG data set sub 
PA1BG    WORD     BGCLEAR     ; D paip moteru BG data set sub 
PA2BG    WORD     BGCLEAR     ; E paip moteru BG data set sub 
MRTBG    WORD     BGCLEAR     ; F tikuwa BG data set sub
IPKBG    WORD     BGCLEAR     ;10 pakkun BG data set sub
ICNBG    WORD     BGCLEAR     ;11 coin BG data set sub
PSWBG    WORD     BGCLEAR     ;12 coin BG data set sub
DORBG    WORD     DOORCHG     ;13 door unit-no. data set sub
BBXBG    WORD     ITMBOX0     ;14 Item box open big unit-no. data sub 
TAIBG    WORD     TAIHO00     ;15 taiho unit-no. data sub 
BG0BG    WORD     TAIHO00     ;16 Big BG unit-no. clear data set
BG1BG    WORD     TAIHO00     ;17 Big BG unit-no. warenai data set
BG2BG    WORD     TAIHO00     ;18 Big BG unit-no. wareru data set 
; 
CONB     EQU      (CONBG-BGCHG)/2 
CLRB     EQU      (CLRBG-BGCHG)/2 
JMPB     EQU      (JMPBG-BGCHG)/2 
JPYB     EQU      (JPYBG-BGCHG)/2 
JPTB     EQU      (JPTBG-BGCHG)/2 
SUPB     EQU      (SUPBG-BGCHG)/2 
WARB     EQU      (WARBG-BGCHG)/2 
HATB     EQU      (HATBG-BGCHG)/2 
BOTB     EQU      (BOTBG-BGCHG)/2 
SBXB     EQU      (SBXBG-BGCHG)/2 
TENB     EQU      (TENBG-BGCHG)/2 
PA0B     EQU      (PA0BG-BGCHG)/2 
PA1B     EQU      (PA1BG-BGCHG)/2 
PA2B     EQU      (PA2BG-BGCHG)/2 
MRTB     EQU      (MRTBG-BGCHG)/2 
IPKB     EQU      (IPKBG-BGCHG)/2 
ICNB     EQU      (ICNBG-BGCHG)/2 
PSWB     EQU      (PSWBG-BGCHG)/2 
DORB     EQU      (DORBG-BGCHG)/2 
BBXB     EQU      (BBXBG-BGCHG)/2 
TAIB     EQU      (TAIBG-BGCHG)/2 
BG0B     EQU      (BG0BG-BGCHG)/2 
BG1B     EQU      (BG1BG-BGCHG)/2 
BG2B     EQU      (BG2BG-BGCHG)/2 
; 
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;x       JSR      BGCLEAR                                            x
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;K;CLRUNTD  EQU      $ 
;K;         BYTE     GCIN1,BLBC0,JUMP1,NSJPY,NSJMP,SPJP0,BBLK0,NSBLK 
;K;         BYTE     BKBT0,BOX07,CNBLK,PAI00,PBT20,PAI05,MARUB,ENQST 
;K;         BYTE     GCOIN,BKBT1 
;K;; 
;K;CLRDT    EQU      $ 
;K;         HEX      05,07,06,08          ; coin data
;K;         HEX      05,07,06,08          ; nuki data
;K;         HEX      B8,BA,B9,BB          ; all cross jump data
;K;         HEX      C0,C2,C1,C3          ; left & rigth jump data 
;K;         HEX      B8,BA,B9,BB          ; up & down jump data
;K;         HEX      B8,BA,B9,BB          ; supar jump data
;K;         HEX      B4,B6,B5,B7          ; wareru block data
;K;         HEX      D8,DA,D9,DB          ; hatena block data
;K;         HEX      FF,FF,E6,E7          ; bottan data
;K;         HEX      60,61,6D,6F          ; Item box small data
;K;         HEX      B4,B6,B5,B7          ; ten coin data
;K;         HEX      52,50,62,60          ; paip wareru data 
;K;         HEX      05,07,06,08          ; paip moteru data 
;K;         HEX      18,1A,19,1B          ; paip moteru data 
;K;         HEX      5C,5E,5D,5F          ; tikuwa data
;K;         HEX      92,CA,93,CB          ; pakkun data
;K;         HEX      DC,DE,DD,DF          ; coin data
;K;         HEX      E0,E2,E1,E3          ; P swich data 
;
BGCLEAR		EQU	$
		LDX	!KINOSW
		DEX
		LDY	<WORK4
		LDA	CLRUNTD,X
		STA	[<BUFFNO],Y
;
		TXA
		ASL	A
		ASL	A
		ASL	A
		TAX
		LDY	#00H
;
BLR050		EQU	$
		LDA	CLRDT,X
		STA	!UNTDAT,Y
		INX
		INY
		CPY	#08H
		BNE	BLR050
;
		LDA	<WORK5
		ASL	A
		ADC	#00
		ASL	A
		ADC	#00
		PHA
		AND	#00001111B
;;K;;         ORA      #020H 
;
		LDY	<WORK6
		BEQ	BLR100
		LDY	ONPUYH
		BEQ	BLR100
;
		ORA	#08H
;
BLR100		EQU	$
		STA	!UNTADH
;;K;;
		LDA	ONPUXH
		LSR	A
		BCC	BLR105
		LDA	!UNTADH
		ORA	#04H
		STA	!UNTADH
BLR105		EQU	$
;;K;;
;
		PLA
		AND	#11110000B
		STA	<WORK1
		LDA	!ONPUXL
		LSR	A
		LSR	A
		LSR	A
		ORA	<WORK1
		STA	!UNTADL
;
		LDA	#00H
		STA	!KINOSW 
		LDA	!HVSRFG
		BEQ	BLR150
;
		MEM16
		REP	#00100000B	; memory 16bit mode
		LDA	<WORK6
		PHA
		LDA	!ONPUYH
		XBA
		AND	#0FF00H
		LSR	A
		LSR	A
		STA	<WORK6
		LDA	!UNTADH
		XBA
		SEC
		SBC	<WORK6
		AND	#0BFFH
		XBA
		STA	!UNTADH
		PLA
		STA	<WORK6
		MEM8
		SEP	#00100000B	; memory 8bitmode
;
		LDA	!ONPUYH
		AND	#01H
		BEQ	BLR150
		LDA	!UNTADH
		EOR	#08H
		STA	!UNTADH
;
BLR150		EQU	$
		RTS
;
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
;x       JSR      DOORCHG                             x 
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
;K;DORADRD  EQU      $ 
;K;         HEX      00,00,C4,FC 
;K;         HEX      00,00,C4,FC 
;K;         HEX      00
; 
DOORCHG  EQU      $ 
         LDA      #00H
         STA      !KINOSW 
; 
         LDY      <WORK4 
         LDA      #DOOR0
;;K;;         STA      (<BUFFNO),Y
         STA      [<BUFFNO],Y
         TYA
         CLC
         ADC      #010H 
         TAY
         BCC      DRG050
; 
         INC      <BUFFNO+1
; 
DRG050   EQU      $ 
         LDA      #DOOR0
;;K;;         STA      (<BUFFNO),Y
         STA      [<BUFFNO],Y
;
	IDX16
	REP	#00010000B		; index 16bit mode
;
         LDY      #00H
         LDX      !VRAMD
;
DRG100   EQU      $
         LDA      DORADRD,Y 
         STA      !VRAMD+2,X
         INX
         INY
;;K;;         CPY      #(DOORCHG-DORADRD)
         CPY      #DRDDB0
         BNE      DRG100
;
	IDX8
	SEP	#00010000B		; index 8bit mode
;
         LDA      <WORK5 
         ASL      A 
         ADC      #00H
         ASL      A 
         ADC      #00H
         PHA
; 
         AND      #00001111B
;;K;;         ORA      #020H 
;
         LDY      <WORK6 
         BEQ      DRG150
; 
         ORA      #08H
; 
DRG150   EQU      $ 
;
	IDX16
	REP	#00010000B		; index 16bit mode
;
         LDX      !VRAMD
         STA      !VRAMD+2,X
         STA      !VRAMD+8,X		; BG address H set
;
         PLA
         AND      #11110000B
         STA      <WORK1 
         LDA      !ONPUXL 
         LSR      A 
         LSR      A 
         LSR      A 
         ORA      <WORK1
         STA      !VRAMD+3,X
         CLC
         ADC      #01H
         STA      !VRAMD+9,X		; BG address L set
;
	MEM16
	REP	#00100000B		; memory 16bit mode
;
         LDA      !VRAMD
         CLC
;;K;;         ADC      #(DOORCHG-DORADRD)-1
         ADC      #DRDDB0-1
         STA      !VRAMD
;
	MEM8
	IDX8
	SEP	#00110000B		; memory , index 8bit mode
;
         RTS
; 
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;x       JSR      ITMBOX0     (Item box open unit-no. data set sub)  x
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;K;BOXDAT0  EQU      $ 
;K;         HEX      00,00,04,50,51,52,53
;K;         HEX      00,00,04,54,55,56,57
;K;         HEX      00
; 
ITMBOX0  EQU      $ 
         LDA      #00H
         STA      !KINOSW 
; 
         LDY      <WORK4 
         LDA      #BOX04
;;k;;         STA      (<BUFFNO),Y
         STA      [<BUFFNO],Y
         INY
         LDA      #BOX05
;;k;;         STA      (<BUFFNO),Y
         STA      [<BUFFNO],Y
;
;;;;         LDY      #00H
;;;;         LDX      !VRAMD
         LDX      #00H
         LDY      !VRAMD
;
	 MEM16
	 REP	  #00100000B	;memory 16bit mode
;
;;;;         LDA      BOXDAT0,Y 
;;;;         STA      !VRAMD+2,X
;;;;         INX
;;;;         INY
;;;;;;k;;         CPY      #(ITMBOX0-BOXDAT0)
;;;;	CPY	#BXDT0B0
;;;;         BNE      IX0200
; 
IX0200   EQU      $ 
         LDA      BOXDAT0,X 
         STA      !VRAMD+2,Y
         INX
         INX
         INY
         INY
;;k;;         CPY      #(ITMBOX0-BOXDAT0)
	CPX	#BXDT0B0
         BNE      IX0200
; 
	 MEM8
	 SEP	  #00100000B	;memory 8bit mode
;
         LDX      !VRAMD
         LDA      <WORK5 
         ASL      A 
         ADC      #00H
         ASL      A 
         ADC      #00H
         PHA
; 
         AND      #00001111B
;;;;         ORA      #020H 
; 
         LDY      <WORK6 
         BEQ      IX0250
; 
         ORA      #08H
; 
IX0250   EQU      $ 
         STA      !VRAMD+2,X
         STA      !VRAMD+14,X  : BG address H set
; 
         PLA
         AND      #11110000B
         STA      <WORK1 
         LDA      !ONPUXL 
         LSR      A 
         LSR      A 
         LSR      A 
         ORA      <WORK1 
         STA      !VRAMD+3,X
         CLC
         ADC      #020H 
         STA      !VRAMD+15,X  : BG address L set
; 
         LDA      !VRAMD
         CLC
         ADC      #BXDT0B0
         STA      !VRAMD
; 
         RTS
; 
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;x       JSR      TAIHO00     ( taiho unit-no. data set sub)         x
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;K;TAIHD0   EQU      $ 
;K;         BYTE     TAIHD1-TAIHD1 
;K;         BYTE     TAIHD2-TAIHD1 
;K;         BYTE     TAIHD5-TAIHD1 
;K;         BYTE     TAIHD6-TAIHD1 
;K;         BYTE     TAIHD7-TAIHD1 
;K;; 
;K;TAIHD1   EQU      $ 
;K;         HEX      00,00,04,FF,A6,A7,FF
;K;         HEX      00,00,04,8D,B2,BC,8D
;K;         HEX      00,00,04,9D,B3,BD,9D
;K;         HEX      00,00,04,FF,AE,AF,FF
;K;         HEX      00
;K;; 
;K;TAIHD2   EQU      $ 
;K;         HEX      00,00,04,09,28,36,49
;K;         HEX      00,00,04,EC,ED,EE,EF
;K;         HEX      00,00,04,E8,E9,EA,EB
;K;         HEX      00,00,04,F8,F9,FA,FB
;K;         HEX      00
;K;; 
;K;TAIHD5   EQU      $ 
;K;         HEX      00,00,04,05,07,05,07
;K;         HEX      00,00,04,06,08,06,08
;K;         HEX      00,00,04,05,07,05,07
;K;         HEX      00,00,04,06,08,06,08
;K;         HEX      00
;K;; 
;K;TAIHD6   EQU      $ 
;K;         HEX      00,00,04,04,0D,0D,0E
;K;         HEX      00,00,04,24,FE,FE,27
;K;         HEX      00,00,04,24,FE,FE,27
;K;         HEX      00,00,04,34,48,48,37
;K;         HEX      00
;K;; 
;K;TAIHD7   EQU      $ 
;K;         HEX      00,00,04,00,01,02,03
;K;         HEX      00,00,04,10,11,12,10
;K;         HEX      00,00,04,20,21,22,23
;K;         HEX      00,00,04,30,31,32,33
;K;         HEX      00
;K;; 
;K;TAIHD3   EQU      $ 
;K;         BYTE     TAI04,TAI00 
;K;; 
;K;TAIHD4   EQU      $ 
;K;         BYTE     TAI00,TAI01,TAI02,TAI03 
;K;         BYTE     TAI04,TAI05,TAI06,TAI07 
;K;         BYTE     BGBC0,BGBC0,BGBC0,BGBC0 
;K;         BYTE     BGBK0,BGBK1,BGBK2,BGBK3 
;K;         BYTE     BWAR0,BWAR1,BWAR2,BWAR3 
; 
;------- Program -----------------------------------------------------
TAIHO00  EQU      $ 
         LDX      #00H
         LDA      !KINOSW 
         CMP      #TAIB 
         BNE      TAI050
; 
         LDY      <WORK4 
;;k;;         LDA      (<BUFFNO),Y
         LDA      [<BUFFNO],Y
         CMP      #TAI04
         BEQ      TAI150
; 
         BNE      TAI100
; 
TAI050   EQU      $ 
         SEC
         SBC      #BG0B 
         TAX
         INX
; 
TAI100   EQU      $ 
         INX
; 
TAI150   EQU      $
         LDY      TAIHD0,X 
         STX      <WORKA 
; 
;;K;;         LDX      !VRAMD
;;K;;         LDA      #(TAIHD2-TAIHD1)
;;K;;	LDA	#TIHD0B0
;;K;;         STA      <WORK0 
;
;;K;;TAI200   EQU      $ 
;;K;;         LDA      TAIHD1,Y
;;K;;         STA      !VRAMD+1,X
;;K;;         INX
;;K;;         INY
;;K;;         DEC      <WORK0 
;;K;;         BNE      TAI200
;
	MEM16
	IDX16
	REP	#00110000B		; memory , index 16bit mode
;
	TYA
	AND	#00FFH
	TAY
;
	MEM8
	SEP	#00100000B		; memory 8bit mode
;
	LDX	!VRAMD
	LDA	#31H
	STA	<WORK0
;
TAI200	EQU	$
	LDA	TAIHD1,Y
	STA	!VRAMD+2,X
	INX
	INY
	DEC	<WORK0
	BNE	TAI200
;
	IDX8
	SEP	#00010000B		; index 8bit mode
;
         LDA      <WORKA 
         ASL      A 
         ASL      A 
         STA      <WORKA 
; 
         LDA      #00H
         STA      <WORK2 
; 
TAI250   EQU      $ 
         LDA      <WORK4 
         AND      #11110000B
         LDY      <WORK6 
         BEQ      TAI270
;
         CLC
         ADC      #010H 
;
TAI270   EQU      $ 
         STA      <WORK5 
; 
         LDA      <WORK4 
         AND      #00001111B
         ASL      A 
         STA      <WORK7 
; 
         LDA      <WORK5 
         ASL      A 
         ADC      #00H
         ASL      A 
         ADC      #00H
         PHA
         AND      #00000011B
;;K;;         ORA      #020H 
;
         LDY      <WORK6 
         BEQ      TAI300
; 
         ORA      #08H
;
TAI300	EQU	$
	STA	<WORK8
;;K;;
	LDA	ONPUXH
	LSR	A
	BCC	TAI305
	LDA	<WORK8
	ORA	#04H
	STA	<WORK8
TAI305	EQU	$
;;K;;
         LDX      #00H
         LDA      <WORK2 
         BEQ      TAI350
;
;;K;;         LDX      #0EH
	LDX	#18H
;
TAI350	EQU	$
;
	MEM16
	IDX16
	REP	#00110000B		; memory , index 16bit mode
;
	TXA
	AND	#00FFH
	CLC
	ADC	!VRAMD
	TAX
;
	MEM8
	SEP	#00100000B		; memory 8bit mode
;
	PLA
	AND	#11110000B
	ORA	<WORK7
	PHA
;;K;;	LDA	ONPUYH
;;K;;	AND	#00FFH
;;K;;	BEQ	TAI358
	LDA	<WORK8
	AND	#08H
	BEQ	TAI358
	PLA
	CMP	#040H
	BCS	TAI355
	PHA
	DEC	<WORK8
	LDA	<WORK8
	AND	#08H
	BNE	TAI353
	LDA	<WORK8
	AND	#03H
	STA	<WORK8
TAI353	EQU	$
	PLA
;
TAI355	EQU	$
	SEC				; adress low
	SBC	#040H
	BRA	TAI359
TAI358	EQU	$
	PLA
TAI359	EQU	$
	STA	<WORK3
	STA	!VRAMD+3,X
	CLC
	ADC	#020H
	STA	!VRAMD+15,X
;
	LDA	<WORK8			; adress high
	STA	!VRAMD+2,X
	STA	!VRAMD+14,X
;
	IDX8
	SEP	#00010000B		; index 8bit mode
;
	LDY	<WORK4
	LDX	<WORKA
	LDA	TAIHD4,X
	STA	[<BUFFNO],Y
;
	INY
	LDA	TAIHD4+1,X
	STA	[<BUFFNO],Y
;
	LDA	<WORK4
	CLC
	ADC	#010H
	STA	<WORK4
	LDA	<BUFFNO+1
	ADC	#00H
	STA	<BUFFNO+1
;
	INC	<WORKA
	INC	<WORKA
	INC	<WORK2
	LDA	<WORK2
	CMP	#02H
	BEQ	TAI255
	JMP	TAI250
;
TAI255		EQU	$
		MEM16
		REP	#00100000B		; memory 16bit mode
;
		LDA	!VRAMD
		CLC
		ADC	#0030H
		STA	!VRAMD
;
		MEM8
		SEP	#00100000B		; memory 8bit mode
;
		STZ	!KINOSW
		INC	!TAIHFG
;
		RTS
;
;
;;K;;         TXA
;;K;;         CLC
;;K;;         ADC      !VRAMD
;;K;;         TAX
;;K;;; 
;;K;;         LDA      <WORK8 
;;K;;         STA      !VRAMD+1,X
;;K;;         STA      !VRAMD+8,X
;;K;;; 
;;K;;         PLA
;;K;;         AND      #11110000B
;;K;;         ORA      <WORK7 
;;K;;         STA      <WORK3 
;;K;;         STA      !VRAMD+2,X
;;K;;         CLC
;;K;;         ADC      #020H 
;;K;;         STA      !VRAMD+9,X
;;K;;; 
;;K;;         LDY      <WORK4 
;;K;;         LDX      <WORKA 
;;K;;         LDA      TAIHD4,X
;;K;;;;k;;         STA      (<BUFFNO),Y
;;K;;         STA      [<BUFFNO],Y
;;K;;; 
;;K;;         INY
;;K;;         LDA      TAIHD4+1,X
;;K;;;;k;;         STA      (<BUFFNO),Y
;;K;;         STA      [<BUFFNO],Y
;;K;;; 
;;K;;         LDA      <WORK4 
;;K;;         CLC
;;K;;         ADC      #010H 
;;K;;         STA      <WORK4 
;;K;;         LDA      <BUFFNO+1
;;K;;         ADC      #00H
;;K;;         STA      <BUFFNO+1
;;K;;; 
;;K;;         INC      <WORKA 
;;K;;         INC      <WORKA 
;;K;;         INC      <WORK2 
;;K;;         LDA      <WORK2 
;;K;;         CMP      #02H
;;K;;         BNE      TAI250
;;K;;; 
;;K;;         LDA      !VRAMD
;;K;;         CLC
;;K;;;;k;;         ADC      #(TAIHD2-TAIHD1)-1
;;K;;	ADC	#TIHD0B0-1
;;K;;         STA      !VRAMD
;;K;;; 
;;K;;         LDA      #00H
;;K;;         STA      !KINOSW 
;;K;;         INC      !TAIHFG 
;;K;;; 
;;K;;         RTS
; 
;;;;;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;;;;;x       JSR      BANKCHG     ( bank chenge data set sub )           x
;;;;;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;;;;BANKDAT  EQU      $ 
;;;;         HEX      02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02    : 0
;;;;         HEX      02,02,02,02,02,02,02,02,02,02,02,02,02,02,02,02 
;;;;; 
;;;;         HEX      02,02,02,02,01,01,01,01,01,01,01,01,02,02,02,02    : 1
;;;;         HEX      02,02,02,02,03,03,03,03,03,03,03,03,02,02,02,02 
;;;;; 
;;;;         HEX      02,02,01,01,01,01,00,00,00,00,01,01,01,01,02,02    : 2
;;;;         HEX      02,02,03,03,03,03,04,04,04,04,03,03,03,03,02,02 
;;;;; 
;;;;         HEX      02,02,02,02,03,03,03,03,03,03,03,03,02,02,02,02    : 3
;;;;         HEX      02,02,02,02,01,01,01,01,01,01,01,01,02,02,02,02 
;;;;; 
;;;;         HEX      00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00    : 4
;;;;         HEX      00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00 
;;;;; 
;;;;         HEX      02,02,03,03,03,03,04,04,04,04,03,03,03,03,02,02    : 5
;;;;         HEX      02,02,01,01,01,01,00,00,00,00,01,01,01,01,02,02 
;;;;; 
;;;;BANKCHG  EQU      $ 
;;;;         LDA      FRCNT 
;;;;         AND      #00000111B
;;;;         BNE      BKG150
;;;;; 
;;;;         INC      BANKFG
;;;;         LDA      BANKFG
;;;;         CMP      >BAKCNT 
;;;;         BNE      BKG050
;;;;; 
;;;;         LDA      #00H
;;;;         STA      BANKFG
;;;;; 
;;;;BKG050   EQU      $ 
;;;;         TAY
;;;;; 
;;;;         LDA      >BAKDAT,Y 
;;;;         STA      >BANKNO+1 
;;;;; 
;;;;         LDX      >BAKFLG 
;;;;         CPX      #02H
;;;;         BNE      BKG150
;;;;; 
;;;;         SEC
;;;;         SBC      #034H 
;;;;         ASL      A 
;;;;         ASL      A 
;;;;         ASL      A 
;;;;         ASL      A 
;;;;         TAY
;;;;         LDX      #00H
;;;;; 
;;;;BKG100   EQU      $ 
;;;;         LDA      BANKDAT,Y 
;;;;         STA      >BNBGDT,X 
;;;;         INY
;;;;         INX
;;;;         CPX      #020H 
;;;;         BNE      BKG100
;;;;; 
;;;;BKG150   EQU      $ 
;;;;         RTS
;;;;; 
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;x       JSR      CLCNCHG                                  x
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;K;CLCHGD   EQU      $ 
;K;         BYTE     03CH,00FH,036H,027H 
;K;         BYTE     03CH,00FH,036H,017H 
;K;         BYTE     03CH,00FH,036H,007H 
;K;         BYTE     03CH,00FH,036H,00FH 
; 
CLCNCHG  EQU      $ 
         LDA      <FRCNT 
         AND      #00000111B
         BNE      CNG100
; 
         LDA      <FRCNT 
         AND      #00011000B
         LSR      A 
         TAX
         LDY      !VRAMD
         LDA      #03FH 
         STA      !VRAMD+1,Y
         LDA      #04H
         STA      !VRAMD+2,Y
         STA      !VRAMD+3,Y
; 
CNG050   EQU      $ 
         LDA      CLCHGD,X
         STA      !VRAMD+4,Y
         INY
         INX
         TXA
         AND      #00000011B
         BNE      CNG050
; 
         LDA      #00H
         STA      !VRAMD+4,Y
; 
         LDA      !VRAMD
         CLC
         ADC      #07H
         STA      !VRAMD
; 
CNG100   EQU      $ 
         RTS
; 
		END
