;*******************************************************************************
; FILENAME	: 1929Ev.h
; AUTHOR	: SONiX Kai Chen
; PURPOSE	: Constant and Macro definition for Ev Kit emulation code
;                 User must include 1929EV.h to communication with Ev. Kit
; Version1.0	: 09/09/2008 V1.0 	First issue
;*******************************************************************************
;*  (c) Copyright 2008, SONiX TECHNOLOGY CO., LTD.
;*******************************************************************************
;
;-------------------------------------------------------------------------------
;;Define user ram 7DH - 7FH to communication emulation board register 
;-------------------------------------------------------------------------------
IF	ICE_Mode==1

extern data R_Temp3, R_Temp2, R_Temp1, R_Temp0;
extern data R_Temp3_isr, R_Temp2_isr, R_Temp1_isr, R_Temp0_isr;

EndIF
;-------------------------------------------------------------------------------
;;Define user ICE Mode SIO interface  
;-------------------------------------------------------------------------------
INIT_1929EV MACRO
IF	ICE_Mode==1

	Sio_SCK		EQU	p3.2		; To P10		(46)-->32
	Sio_SO		EQU	p3.3		; To P11		(47)-->33
	Sio_SI		EQU	p3.0		; To P12		(44)-->30
	ICE_Reset	EQU	p3.1		; ICE to reset EV Board	(45)-->31 
	Chk_Flag	EQU	P3.1		;			(45)
	V_SioLoopCnt	EQU	8

	mov	a,#00000000b			; Initial port4 
	mov	p3,a				;
	mov	a,#00001110b			; SIO_SI as input mode
	mov	p3m,a				;
	mov	a,#00000000b			;
	mov	p3ur,a				;

Loop_Reset_EV:					; Reset use p3.1 reset emulation board
	b0bset	FWDRST
	decms	R_Temp0				;
	jmp	$-2				;
	decms	R_temp1				;
	jmp	Loop_Reset_EV			;
	b0bset	ICE_Reset			; for sync EV and ICE
	mov	a,#02h				; delay loop 256*256*2
	b0mov	R_Temp2,a			;
Loop_Reset_EV_OK:				;
	b0bset	FWDRST
	decms	R_Temp0				;
	jmp	$-2				;
	decms	R_Temp1				;
	jmp	Loop_Reset_EV_OK		;
	decms	R_Temp2				;
	jmp	Loop_Reset_Ev_OK		;
ENDIF
	ENDM

;-------------------------------------------------------------------------------
; SN8P1917 special register access MACRO 
; Write register Usage : XMOV 	register,A
; Read  register Usage : XMOV 	A, register
;-------------------------------------------------------------------------------

XMOV   MACRO	REG,VAL

IF ICE_Mode==1

	IFIDNI <VAL>,<A>			; check witer mode to emulation board
			b0mov	R_Temp2,a	; save a value
			mov	a, #(REG)	; get address value
			b0mov	R_Temp0,a	; save value in R_tamp0

			B0mov	a,RBANK		; 
			B0mov	R_Temp3,a	; Save RBANK data to R_Temp3			
			mov	a,#00H		; 
			b0mov	RBANK,a		; Change to RBANK0

			b0bclr	R_Temp0.7	; use bit7 mark to write mode flag.
			call	SIO		; sned write address to emulation board
			call	@@delayTime	; waiting for emulation board procrss command & address

			b0mov	a,R_Temp2	; load data to SIO buffer
			b0mov	R_Temp0,a	;	
			call	SIO		; send data into emulation board address 

			b0mov	a,R_Temp3	; 
			b0mov	RBANK,a		; Re-Load RBANK data from R_Temp3	
						
			b0mov	a,R_Temp2	; for keep "Acc" value

	elseIFIDNI	<REG>,<A>		; check read mode from emulation board
		
    			mov	a,#(VAL)	; get register address value
			b0mov	R_Temp0,a	;
			
			B0mov	a,RBANK		; 
			B0mov	R_Temp3,a	; Save RBANK data to R_Temp3			
			mov	a,#00H		; 
			b0mov	RBANK,a		; Change to RBANK0

			b0bset	R_Temp0.7	; use bit7 mark to read mode flag.
			call	SIO		; send read address to emulation board
			call	@@delayTime	; waiting for emulation board process command & address
			call	SIO		; received emulation board data
			
			b0mov	R_Temp2,a
			b0mov	a,R_Temp3	; 
			b0mov	RBANK,a		; Re-Load RBANK data from R_Temp3
			b0mov	a,R_Temp2
	else
		Error!! Check XMOV macro parameter. This macro need "A" parameter.
	
	endif

else    ; Real chip mode

	IFIDNI  <VAL>,<A>
 		b0mov	(REG),a			;

	elseIFIDNI <REG>,<A>
		b0mov	a,(VAL)			;

	else
		Error!! Check XMOV macro parameter. This macro need "A" parameter.
	endif	
			
ENDIF
	endm
;***********************************************************************************
; Sol add thif sor support xmov_isr in intruput function 2007.01.04
; For XMOV_isr;
;***********************************************************************************
XMOV_isr   MACRO	REG,VAL

IF ICE_Mode==1

	IFIDNI <VAL>,<A>					; check witer mode to emulation board
			b0mov	R_Temp2_isr,a		; save a value
			mov	a, #(REG)					; get address value
			b0mov	R_Temp0_isr,a		; save value in R_tamp0

			B0mov	a,RBANK					; 
			B0mov	R_Temp3_isr,a		; Save RBANK data to R_Temp3			
			mov	a,#00H						; 
			b0mov	RBANK,a					; Change to RBANK0

			b0bclr	R_Temp0_isr.7	; use bit7 mark to write mode flag.
			call	SIO_isr					; sned write address to emulation board
			call	@@delayTime_isr	; waiting for emulation board procrss command & address

			b0mov	a,R_Temp2_isr		; load data to SIO buffer
			b0mov	R_Temp0_isr,a		;	
			call	SIO_isr					; send data into emulation board address 

			b0mov	a,R_Temp3_isr		; 
			b0mov	RBANK,a					; Re-Load RBANK data from R_Temp3	
						
			b0mov	a,R_Temp2_isr		; for keep "Acc" value

	elseIFIDNI	<REG>,<A>			; check read mode from emulation board
		
    			mov	a,#(VAL)			; get register address value
			b0mov	R_Temp0_isr,a		;
			
			B0mov	a,RBANK					; 
			B0mov	R_Temp3_isr,a		; Save RBANK data to R_Temp3			
			mov	a,#00H						; 
			b0mov	RBANK,a					; Change to RBANK0

			b0bset	R_Temp0_isr.7	; use bit7 mark to read mode flag.
			call	SIO_isr					; send read address to emulation board
			call	@@delayTime_isr	; waiting for emulation board process command & address
			call	SIO_isr					; received emulation board data
			
			b0mov	R_Temp2_isr,a
			b0mov	a,R_Temp3_isr		; 
			b0mov	RBANK,a					; Re-Load RBANK data from R_Temp3
			b0mov	a,R_Temp2_isr
	else
		Error!! Check XMOV macro parameter. This macro need "A" parameter.
	
	endif

else    ; Real chip mode

	IFIDNI  <VAL>,<A>
 		b0mov	(REG),a			;

	elseIFIDNI <REG>,<A>
		b0mov	a,(VAL)			;

	else
		Error!! Check XMOV macro parameter. This macro need "A" parameter.
	endif	
			
ENDIF
	endm
;************************************************************************************
;XB0MOV same as SMOV                                                                *
;************************************************************************************

XB0MOV   MACRO	REG,VAL

IF ICE_Mode==1

	IFIDNI <VAL>,<A>			; check witer mode to emulation board
			b0mov	R_Temp2,a	; save a value
			mov	a, #(REG)	; get address value
			b0mov	R_Temp0,a	; save value in R_tamp0

			B0mov	a,RBANK		; 
			B0mov	R_Temp3,a	; Save RBANK data to R_Temp3			
			mov	a,#00H		; 
			b0mov	RBANK,a		; Change to RBANK0

			b0bclr	R_Temp0.7	; use bit7 mark to write mode flag.
			call	SIO		; sned write address to emulation board
			call	@@delayTime	; waiting for emulation board procrss command & address

			b0mov	a,R_Temp2	; load data to SIO buffer
			b0mov	R_Temp0,a	;	
			call	SIO		; send data into emulation board address 
			
			b0mov	a,R_Temp3		; 
			b0mov	RBANK,a		; Re-Load RBANK data from R_Temp3	
			
			b0mov	a,R_Temp2	; for keep "Acc" value

	elseIFIDNI	<REG>,<A>		; check read mode from emulation board
		
    			mov	a,#(VAL)	; get register address value
			b0mov	R_Temp0,a	;

			B0mov	a,RBANK		; 
			B0mov	R_Temp3,a	; Save RBANK data to R_Temp3			
			mov	a,#00H		; 
			b0mov	RBANK,a		; Change to RBANK0

			b0bset	R_Temp0.7	; use bit7 mark to read mode flag.
			call	SIO		; send read address to emulation board
			call	@@delayTime	; waiting for emulation board process command & address
			call	SIO		; received emulation board data
			
			b0mov	R_Temp2,a
			b0mov	a,R_Temp3	; 
			b0mov	RBANK,a		; Re-Load RBANK data from R_Temp3
			b0mov	a,R_Temp2			
	else
		Error!! Check XMOV macro parameter. This macro need "A" parameter.
	
	endif

else    ; Real chip mode

	IFIDNI  <VAL>,<A>
 		b0mov	(REG),a			;

	elseIFIDNI <REG>,<A>
		b0mov	a,(VAL)			;

	else
		Error!! Check XMOV macro parameter. This macro need "A" parameter.
	endif	
			
ENDIF
	endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;-------------------------------------------------------------------------------
; Sol add thif macro for intruput function use xmov 2007.01.04
; SN8P1909 special register access MACRO 
; Write register Usage : XMOV_isr 	register,A
; Read  register Usage : XMOV_isr 	A, register
;-------------------------------------------------------------------------------
XB0MOV_isr   MACRO	REG,VAL

IF ICE_Mode==1

	IFIDNI <VAL>,<A>					; check witer mode to emulation board
			b0mov	R_Temp2_isr,a		; save a value
			mov	a, #(REG)					; get address value
			b0mov	R_Temp0_isr,a		; save value in R_tamp0

			B0mov	a,RBANK					; 
			B0mov	R_Temp3,a				; Save RBANK data to R_Temp3			
			mov	a,#00H						; 
			b0mov	RBANK,a					; Change to RBANK0

			b0bclr	R_Temp0_isr.7	; use bit7 mark to write mode flag.
			call	SIO_isr					; sned write address to emulation board
			call	@@delayTime_isr	; waiting for emulation board procrss command & address

			b0mov	a,R_Temp2_isr		; load data to SIO buffer
			b0mov	R_Temp0_isr,a		;	
			call	SIO_isr					; send data into emulation board address 
			
			b0mov	a,R_Temp3_isr		; 
			b0mov	RBANK,a					; Re-Load RBANK data from R_Temp3	
			
			b0mov	a,R_Temp2_isr		; for keep "Acc" value

	elseIFIDNI	<REG>,<A>			; check read mode from emulation board
		
    			mov	a,#(VAL)			; get register address value
			b0mov	R_Temp0_isr,a		;

			B0mov	a,RBANK					; 
			B0mov	R_Temp3_isr,a		; Save RBANK data to R_Temp3			
			mov	a,#00H						; 
			b0mov	RBANK,a					; Change to RBANK0

			b0bset	R_Temp0_isr.7	; use bit7 mark to read mode flag.
			call	SIO_isr					; send read address to emulation board
			call	@@delayTime_isr	; waiting for emulation board process command & address
			call	SIO_isr					; received emulation board data
			
			b0mov	R_Temp2_isr,a
			b0mov	a,R_Temp3_isr		; 
			b0mov	RBANK,a					; Re-Load RBANK data from R_Temp3
			b0mov	a,R_Temp2_isr			
	else
		Error!! Check XMOV macro parameter. This macro need "A" parameter.
	
	endif

else    ; Real chip mode

	IFIDNI  <VAL>,<A>
 		b0mov	(REG),a			;

	elseIFIDNI <REG>,<A>
		b0mov	a,(VAL)			;

	else
		Error!! Check XMOV macro parameter. This macro need "A" parameter.
	endif	
			
ENDIF
	endm
;-------------------------------------------------------------------------------
; SN8P1909 special register Bit test 0 MACRO
; this macro is only available in R/W register of bank 0
; e.g XB0BTS0  FADCENB
;     XB0BTS0  ADCM.0    <== this syntax Not available now
;-------------------------------------------------------------------------------

XB0BTS0 	MACRO 	REG
IF ICE_Mode==1
		mov	a,#@int(REG)		; get reg. address value
		b0mov	R_Temp0,a		;

		B0mov	a,RBANK			; 
		B0mov	R_Temp3,a		; Save RBANK data to R_Temp3			
		mov	a,#00H			; 
		b0mov	RBANK,a			; Change to RBANK0

     		b0bset	R_Temp0.7		; use bit7 mark to read mode flag.
		call	SIO			; send read address to emulation board
		call	@@delayTime		; waiting for emulation board process command & address

		call	SIO			; received emulation board data
		mov	R_Temp0,a		; save received data 
		
		b0mov	a,R_Temp3		; 
		b0mov	RBANK,a			; Re-Load RBANK data from R_Temp3		
		
		b0bts0	R_Temp0.@bit(REG)	; judge data bit
ELSE
	b0bts0	REG
ENDIF
	ENDM

;-------------------------------------------------------------------------------
; SN8P1909 special register Bit test 1 MACRO
; this macro is only available in R/W register of bank 0
; e.g XB0BTS1  FADCENB
;     XB0BTS1  ADCM.0    <== this syntax Not available now
;-------------------------------------------------------------------------------
XB0BTS1 	MACRO 	REG
IF ICE_Mode==1
		mov	a,#@int(REG)		; get reg. address value
		b0mov	R_Temp0,a		;
		
		B0mov	a,RBANK			; 
		B0mov	R_Temp3,a		; Save RBANK data to R_Temp3			
		mov	a,#00H			; 
		b0mov	RBANK,a			; Change to RBANK0		

     		b0bset	R_Temp0.7		; use bit7 mark to read mode flag.
		call	SIO			; send read address to emulation board
		call	@@delayTime		; waiting for emulation board process command & address

		call	SIO			; received emulation board data
		mov	R_Temp0,a		; save reecived data
		
		b0mov	a,R_Temp3		; 
		b0mov	RBANK,a			; Re-Load RBANK data from R_Temp3			
		
		b0bts1	R_Temp0.@bit(REG)	; judge data bit
ELSE
	b0bts1	REG
ENDIF
	ENDM

;-------------------------------------------------------------------------------
; SN8P1909 special register Bit set MACRO
; this macro is only available in R/W register of bank 0
; e.g XB0BSET  FADCENB
;     XB0BSET  ADCM.0    <== this syntax Not available now
;-------------------------------------------------------------------------------    

XB0BSET 	MACRO 	REG
IF ICE_Mode==1
    		mov	a,#@int(REG)		; get reg. address value
		b0mov	R_Temp0,a		;
		
		B0mov	a,RBANK			; 
		B0mov	R_Temp3,a		; Save RBANK data to R_Temp3			
		mov	a,#00H			; 
		b0mov	RBANK,a			; Change to RBANK0		

     		b0bset	R_Temp0.7		; use bit7 mark to read mode flag.
		call	SIO			; send read address to emulation board
		call	@@delayTime		; waiting for emulation board process command & address

		call	SIO			; received emulation board data
		mov	R_Temp2,a		; save data 
		b0bset	R_Temp2.@bit(REG)	; process data bit

    		mov	a,#@int(REG)	        ; get reg. address value
		b0mov	R_Temp0,a		;

     		b0bclr	R_Temp0.7		; use bit7 mark to writer mode flag.
		call	SIO			; sned write address to emulation board
		call	@@delayTime		; waiting for emulation board process command & address
		b0mov	a,R_Temp2	        ; load data to SIO buffer
		b0mov	R_Temp0,a		;
		call	SIO			; send data into emulation board address
		
		b0mov	a,R_Temp3		; 
		b0mov	RBANK,a			; Re-Load RBANK data from R_Temp3			
ELSE
	b0bset	REG
ENDIF
	endm

;-------------------------------------------------------------------------------
; SN8P1909 special register Bit clear MACRO
; this macro is only available in R/W register of bank 0
; e.g XB0BCLR  FADCENB
;     XB0BCLR  ADCM.0    <== this syntax Not available now
;-------------------------------------------------------------------------------    

XB0BCLR 	MACRO 	REG
IF ICE_Mode==1
    		mov	a,#@int(REG)		; get reg. address value
		b0mov	R_Temp0,a		;

		B0mov	a,RBANK			; 
		B0mov	R_Temp3,a		; Save RBANK data to R_Temp3			
		mov	a,#00H			; 
		b0mov	RBANK,a			; Change to RBANK0

     		b0bset	R_Temp0.7		; use bit7 mark to read mode flag.
		call	SIO			; send read address to emulation board
		call	@@delayTime		; waiting for emulation board process command & address

		call	SIO			; received emulation board data
		mov	R_Temp2,a		; save data
		b0bclr	R_Temp2.@bit(REG)	; process data bit

    		mov	a,#@int(REG)		; get reg.  address value
		b0mov	R_Temp0,a		;

     		b0bclr	R_Temp0.7	 	; use bit7 mark to writer mode flag.
		call	SIO			; sned write address to emulation board
		call	@@delayTime		; waiting for emulation board process command & address
		b0mov	a,R_Temp2		; load data to SIO buffer
		b0mov	R_Temp0,a
		call	SIO			;send data into emulation board address
		
		b0mov	a,R_Temp3		; 
		b0mov	RBANK,a			; Re-Load RBANK data from R_Temp3			
ELSE
	b0bclr	reg
ENDIF
	endm

;******************************************************
; @SET_LCDM1 Macro to emulate 1917 LCDM in S8KD-2 ICE
	
	@SET_LCDM1	macro	val
		IF  ICE_MODE ==	1			;Set S8KD-2 ICE LCDM register
			mov	a,#(val)
			and	a,#0xfc
			b0mov	LCDM1,a
			mov	a,#(val)
;			and	a,#0x01
;			xor	a,#0x01
;			b0bts0	FZ
;			b0bset	FP2SEG
		ELSE					;;Set SN8P1917 ICE LCDM1 register
			mov	a,#(val)
			b0mov	LCDM1,a
		ENDIF
	endm
;**********************************************************	


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;----------------------------------------------------
; For XMOV : Modified by sol
;			 Assume RBANK has already been setup
;	     Don't care about Y value
;----------------------------------------------------
;Source A
;Dest	Z:	
;----------------------------------------------------
XMOVA2M   MACRO	
		  b0mov	R_Temp2,a	; save a value
			mov	a, Z	; get address value
			b0mov	R_Temp0,a	; save value in R_tamp0

			B0mov	a,RBANK		; 
			B0mov	R_Temp3,a	; Save RBANK data to R_Temp3			
			mov	a,#00H		; 
			b0mov	RBANK,a		; Change to RBANK0

			b0bclr	R_Temp0.7	; use bit7 mark to write mode flag.
			call	SIO		; sned write address to emulation board
			call	@@delayTime	; waiting for emulation board procrss command & address

			b0mov	a,R_Temp2	; load data to SIO buffer
			b0mov	R_Temp0,a	;	
			call	SIO		; send data into emulation board address 
			
			b0mov	a,R_Temp3		; 
			b0mov	RBANK,a		; Re-Load RBANK data from R_Temp3	
			
			b0mov	a,R_Temp2	; for keep "Acc" value
endm
		
;----------------------------------------------------
; For XMOV : Modified by sol
;----------------------------------------------------
;Source A
;Dest	Z
XMOVA2M_isr   MACRO	
	    b0mov	R_Temp2_isr,a		; save a value
			mov	a, Z					; get address value
			b0mov	R_Temp0_isr,a		; save value in R_tamp0

			B0mov	a,RBANK					; 
			B0mov	R_Temp3,a				; Save RBANK data to R_Temp3			
			mov	a,#00H						; 
			b0mov	RBANK,a					; Change to RBANK0

			b0bclr	R_Temp0_isr.7	; use bit7 mark to write mode flag.
			call	SIO_isr					; sned write address to emulation board
			call	@@delayTime_isr	; waiting for emulation board procrss command & address

			b0mov	a,R_Temp2_isr		; load data to SIO buffer
			b0mov	R_Temp0_isr,a		;	
			call	SIO_isr					; send data into emulation board address 
			
			b0mov	a,R_Temp3_isr		; 
			b0mov	RBANK,a					; Re-Load RBANK data from R_Temp3	
			
			b0mov	a,R_Temp2_isr		; for keep "Acc" value
endm

;----------------------------------------------------
; For XMOV : Modified by Joe
; 			 Assume RBANK has already been set
;----------------------------------------------------
;Source Z
;Dest	A
XMOVM2A   MACRO	
	    mov	a,Z	; get register address value
			b0mov	R_Temp0,a	;
			
			B0mov	a,RBANK		; 
			B0mov	R_Temp3,a	; Save RBANK data to R_Temp3			
			mov	a,#00H		; 
			b0mov	RBANK,a		; Change to RBANK0

			b0bset	R_Temp0.7	; use bit7 mark to read mode flag.
			call	SIO		; send read address to emulation board
			call	@@delayTime	; waiting for emulation board process command & address
			call	SIO		; received emulation board data
			
			b0mov	R_Temp2,a
			b0mov	a,R_Temp3	; 
			b0mov	RBANK,a		; Re-Load RBANK data from R_Temp3
			b0mov	a,R_Temp2
endm

	
;----------------------------------------------------
; For XMOV : Modified by Joe
;----------------------------------------------------
;Source Z
;Dest	A
XMOVM2A_isr   MACRO	
	    mov	a,Z			; get register address value
			b0mov	R_Temp0_isr,a		;
			
			B0mov	a,RBANK					; 
			B0mov	R_Temp3_isr,a		; Save RBANK data to R_Temp3			
			mov	a,#00H						; 
			b0mov	RBANK,a					; Change to RBANK0

			b0bset	R_Temp0_isr.7	; use bit7 mark to read mode flag.
			call	SIO_isr					; send read address to emulation board
			call	@@delayTime_isr	; waiting for emulation board process command & address
			call	SIO_isr					; received emulation board data
			
			b0mov	R_Temp2_isr,a
			b0mov	a,R_Temp3_isr		; 
			b0mov	RBANK,a					; Re-Load RBANK data from R_Temp3
			b0mov	a,R_Temp2_isr
endm

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;******************************************************
; @B0BTS1_FVPPCHK	ICE_mode = 1 skip VPPCHK because of VPPCHK is always 0 on ICE
;			ICE_mode = 0 same as b0bts1 FVPPCHK
	
	@B0BTS1_FVPPCHK  	macro	
		IF  ICE_MODE ==	1			;
			JMP	$+2			;Skip the next intruction JMP $-1 because of in ICE mode VPPCHK always = 0
		ELSE					;;
			B0BTS1 FVPPCHK
		ENDIF
	endm
;**********************************************************	

;******************************************************
; @B0BTS0_FVPPCHK	Match the marco @B0BTS0_FVPPCHK
;	
	
	@B0BTS0_FVPPCHK   	macro	
			B0BTS0 FVPPCHK
	endm
;**********************************************************	