
;------------------------------------------------------------------------------
;  CHIP: S8P2603A
;  Revision History:
;  V1.0  03/29/2005  First issue
;  V2.0  12/17/2007  1. "GreenMode","SleepMode","SlowMode","Slow2Normal" macro programs. 
;  V3.0   04/03/2008  1. Add "RedWrt_FORMAT    1"  	
;  V4.0   06/29/2010  1. modify MACRO text "SLowMode" to "SlowMode"
;------------------------------------------------------------------------------
.ASSEMBLY    RedWrt_FORMAT    1

.Assembly	INC_VER		4
.Assembly	ASM_FORMAT	1
.Assembly	RAM_Size	48

.Assembly	INSTRUMENT	SN8P2603A
.Assembly	BIT_FILE	SN8P2602A

.Assembly	OTP_BODY_SIZE	0x0400
.Assembly	USER_ROM_SIZE	0x03FC

.Assembly	OTP_TEST_SIZE	0x00A0
;.Assembly	MASK_TEST_FILE	SN8A1602A
.Assembly	MASK_TEST_CODE	0x0000, 0x0000, 0x0000, 0x0000
;.Assembly	OTP_TEST_FILE	SN83P
.Assembly	PGM_TYPE	6
.Assembly	CHIP_ID		0x3200		;0x8300:5-layer change, 0x8301:all-layer change
.Assembly       command		ORG8_LIMIT, Enable_ORG4_7, PCL_LIMIT, GREEN_NOP_LIMIT

.Assembly	Port_Pins	1,6,0,0,0,8
; Code Option 0x3FF
.Assembly	OPTION_Hide		0		Noise_Filter0	Disable Enable	
.Assembly	OPTION		1		Security	Enable	Disable
.Assembly	OPTION		2		Fix_Low
.Assembly	OPTION		3		Fix_High   
.Assembly	OPTION		4		Fix_LOw   

.Assembly	OPTION		5		High_Clk	Ext_RC	32K_X'tal	12M_X'tal	4M_X'tal
.Help
{
	Ext_RC		:	"Connect R to VDD and Xin\nConnect C to GND and Xin"
	32K_X'tal	:	"32.768K Crystal"
	12M_X'tal	:	"Crystal/Resonator: 10Mhz~16Mhz"
	4M_X'tal	:	"Crystal/Resonator: 2Mhz~10Mhz"
}
.Assembly	OPTION		7		Fix_High
.Assembly	OPTION		8		Watch_Dog 	Always_On x x x x Enable x x x x Disable x x x x x
.Help
{
	Always_ON	:	"Watchdog still enable even in\nGreen and Sleep mode"
	Enable		:	"Normal mode: Enable Watchdog\nGreen and Sleep mode: Stop Watchdog"
	Disable         :       "Disable Watchdog"
} 
.Assembly	OPTION		12		Fix_High
.Assembly	OPTION		13		Fix_High
.Assembly	OPTION		14		Fix_High   
.Assembly	OPTION_Hide	15		ROM_Chk0	Disable Enable 

; Code Option 0x3FE
.Assembly	OPTION		16		Fix_Low
.Assembly	OPTION		17		Fix_High
.Assembly	OPTION		18		Fix_Low
.Assembly	OPTION		19		Fix_High
.Assembly	OPTION		20		Fix_High
.Assembly	OPTION		21		Fix_Low
.Assembly	OPTION		22		Fix_High
.Assembly	OPTION		23		Fix_Low
.Assembly	OPTION		24		Fix_Low
.Assembly	OPTION		25		Fix_High
.Assembly	OPTION		26		Fix_Low
.Assembly	OPTION		27		Fix_High
.Assembly	OPTION		28 : 31		Fix_High

; Code Option 0x3FD
.Assembly	OPTION		32 : 63	Fix_High

.Assembly	OPTION_VIRTUAL			Noise_Filter	Enable		Disable

.Assembly	OPTION		2ffh


	R			EQU		0x82
	Z			EQU		0x83
	Y			EQU		0x84
	PFLAG		EQU		0x86
		FZ	EQU		PFLAG.0
		FDC	EQU		PFLAG.1
		FC	EQU		PFLAG.2
		FNPD	EQU		PFLAG.6
		FNT0	EQU		PFLAG.7

	P0M			EQU		0B8H
		FP00M	EQU		P0M.0


	PEDGE		EQU		0BFH
;		.W_LIMIT        0x80,0x00	;Force bit 7 = high 	
		FP00G1	EQU		PEDGE.4
		FP00G0	EQU		PEDGE.3

	P1W			EQU_W		0C0H
		FP14W	EQU_W		P1W.4
		FP13W	EQU_W		P1W.3
		FP12W	EQU_W		P1W.2
		FP11W	EQU_W		P1W.1
		FP10W	EQU_W		P1W.0

	P1M			EQU		0C1H
		FP14M	EQU		P1M.4
		FP13M	EQU		P1M.3
		FP12M	EQU		P1M.2
		FP11M	EQU		P1M.1
		FP10M	EQU		P1M.0

	P5M			EQU		0C5H
     	FP57M	EQU		P5M.7
     	FP56M	EQU		P5M.6
     	FP55M	EQU		P5M.5
     	FP54M	EQU		P5M.4
     	FP53M	EQU		P5M.3
     	FP52M	EQU		P5M.2
     	FP51M	EQU		P5M.1
     	FP50M	EQU		P5M.0

	INTRQ		EQU		0C8H
        FT0IRQ  EQU INTRQ.4
		FP00IRQ	EQU	INTRQ.0

	OSCM		EQU		0CAH		
		FCPUM1	EQU	OSCM.4
		FCPUM0	EQU	OSCM.3
		FCLKMD	EQU	OSCM.2
		FSTPHX	EQU	OSCM.1

	WDTR		EQU_W		0CCH
		FWDTR7	EQU_W	WDTR.7
		FWDTR6	EQU_W	WDTR.6
		FWDTR5	EQU_W	WDTR.5
		FWDTR4	EQU_W	WDTR.4
		FWDTR3	EQU_W	WDTR.3
		FWDTR2	EQU_W	WDTR.2
		FWDTR1	EQU_W	WDTR.1
		FWDTR0	EQU_W	WDTR.0

	PCL			EQU		0CEH
	PCH			EQU		0CFH

	P0			EQU 	0D0H
		FP00	EQU 	P0.0

	P1			EQU		0D1H
		FP14	EQU		P1.4
		FP13	EQU		P1.3
		FP12	EQU		P1.2
		FP11	EQU		P1.1
		FP10	EQU		P1.0

	P5			EQU		0D5H
		FP57	EQU		P5.7
		FP56	EQU		P5.6
		FP55	EQU		P5.5
		FP54	EQU		P5.4
		FP53	EQU		P5.3
		FP52	EQU		P5.2
		FP51	EQU		P5.1
		FP50	EQU		P5.0		

	T0M			EQU		0D8H
		FT0ENB	 EQU		T0M.7
		FT0RATE2 EQU		T0M.6
		FT0RATE1 EQU		T0M.5
		FT0RATE0 EQU		T0M.4

    	T0C                     EQU_RMW         0D9H
               
	STKP		EQU		0DFH
		FSTKPB2	EQU		STKP.2
		FSTKPB1	EQU		STKP.1
		FSTKPB0	EQU		STKP.0

    	P0UR        EQU_W     0E0H
	    	FP00R   EQU_W     P0UR.0

    	P1UR        EQU_W     0E1H
	    	FP14R   EQU_W     P1UR.4
	    	FP13R   EQU_W     P1UR.3
	    	FP12R   EQU_W     P1UR.2
	    	FP11R   EQU_W     P1UR.1
	    	FP10R   EQU_W     P1UR.0

    	P5UR        EQU_W     0E5H
	    	FP57R   EQU_W     P5UR.7
	    	FP56R   EQU_W     P5UR.6
	    	FP55R   EQU_W     P5UR.5
	    	FP54R   EQU_W     P5UR.4
	    	FP53R   EQU_W     P5UR.3
	    	FP52R   EQU_W     P5UR.2
	    	FP51R   EQU_W     P5UR.1
	    	FP50R   EQU_W     P5UR.0

    	P1OC        EQU_W     0E9H
	    	FP10OC  EQU_W     P1OC.0

	@YZ		EQU		0E7H

	STK3L		EQU		0F8h
	STK3H		EQU		0F9h
	STK2L		EQU		0FAh
	STK2H		EQU		0FBh
	STK1L		EQU		0FCh
	STK1H		EQU		0FDh
	STK0L		EQU		0FEh
	STK0H		EQU		0FFh

; -----------------------------------------------------
; Clear watchdog macro for compatibility in S8KD-2 ICE
;  
; Total Cycle: 2 

	@RST_WDT	macro		
 		   MOV  A, #0x5A
		   B0MOV  WDTR, A
	endm
; -----------------------------------------------------
; Sleep Mode control macro for compatibility in S8KD-2 ICE
;  
; Total Cycle: 1

	SleepMode	macro	
		B0BSET	FCPUM0
	endm
; -----------------------------------------------------
; Green Mode control macro for compatibility in S8KD-2 ICE
;  
; Total Cycle: 3

	GreenMode	macro	
		B0BSET	FCPUM1
		NOP
		NOP
	endm
; -----------------------------------------------------
; Slow Mode control macro for compatibility in S8KD-2 ICE
;  
; Total Cycle: 2

	SlowMode	macro	
		B0BSET	FCLKMD
		B0BSET	FSTPHX
	endm
; -----------------------------------------------------
;  Slow mode to Normal Mode control macro for compatibility in S8KD-2 ICE
;  
; Total Cycle: 5

	Slow2Normal	macro	
		B0BCLR	FSTPHX
		NOP
		NOP
		NOP
		B0BCLR	FCLKMD
	endm
; -----------------------------------------------------
; In S8KD-2 ICE P0.0 is input only, but SN8P2051 P0.0 is I/O pin
; Connect the P6.0 and P0.0 to emulate P0.0 output pin
; Use @P00_Mode macro to set P0.0 output mode
;     @P00_Mode 0, Set P0.0 as input pin
;     @P00_Mode 1, Set P0.0 as output pin
; Total Cycle: 1

;
; Use @P00_Out to set P0.0 output high or low
;     @P00_Out 0, Set P0.0 output low
;     @P00_Out 1, Set P0.0 output high
; Total Cycle: 1

	@P00_MODE	macro	val
	
		IFNDEF  val
			ECHO	Error: @P00_MODE macro need one parameter
			ECHO	Syntax: @P00_MODE  Mode
			ECHO	Example:
			ECHO	@P00_MODE 0  Configure P0.0 as input mode
			ECHO	@P00_MODE 1  Configure P0.0 as output mode			
			ERROR
			ECHO
		ENDIF
			
		IF val == 0		; val = 0 input mode	
		
			   B0BCLR FP00M
			
		ELSEIF val == 1		; val = 1 output mode
			
			    B0BSET FP00M
			
		ELSE
			ECHO	Error: val is invalid parameter of @P00_MODE macro
			ECHO	Syntax: @P00_MODE  Mode
			ECHO	Example:
			ECHO	@P00_MODE 0  Configure P0.0 as input mode
			ECHO	@P00_MODE 1  Configure P0.0 as output mode			
			ERROR
			ECHO				
		ENDIF
	endm
	
	@P00_OUT	macro	val
	
		IFNDEF  val
			ECHO	Error: @P00_OUT macro need one parameter
			ECHO	Syntax: @P00_OUT    Level
			ECHO	Example:
			ECHO	@P00_OUT 0  P0.0 output high
			ECHO	@P00_OUT 1  P0.0 output low
			ERROR
			ECHO
		ENDIF
					
		IF val == 0		; val = 0 P0.0 output low
			
			   B0BCLR FP00
			
		ELSEIF val == 1		; val = 1 P0.0 output high
			
			   B0BSET FP00
			
		ELSE
			ECHO	Error: val is invalid parameter of @P00_OUT macro
			ECHO	Syntax: @P00_OUT    Level
			ECHO	Example:
			ECHO	@P00_OUT 0  P0.0 output high
			ECHO	@P00_OUT 1  P0.0 output low
			ERROR
			ECHO			
		ENDIF
	endm		
	
; -----------------------------------------------------	
; Set P0.0 interrupt edge MACRO
; The Falling and rising edge definition of SN8P2XXX are different to S8KD-2 ICE
; @P00_EDGE VAL
;           VAL = 1,  Rising edge  
;           VAL = 2,  Falling edge
;           VAL = 3,  Level Change (Bi-direction)
; Total Cycle = 2

	@P00_EDGE	MACRO       VAL
		
		IFNDEF  VAL
			ECHO	Error: @P00_EDGE macro need one parameter
			ECHO	Syntax: @P00_EDGE  EDGE
			ECHO	<EDGE = 1, Rising edge trigger for P0.0 interrupt>
			ECHO 	<EDGE = 2, Falling edge trigger for P0.0 interrupt>
			ECHO 	<EDGE = 3, Level change(Bi-direction) trigger for P0.0 interrupt>
			ECHO    Example:
			ECHO	@P00_EDGE 2   Configure P0.0 interrupt as falling edge trigger
			ERROR
			ECHO
		ENDIF
	
			IF VAL == 1
			   MOV		A,#00001000B
			   B0MOV	PEDGE,A
			ELSEIF VAL == 2
			   MOV		A,#00010000B
			   B0MOV	PEDGE,A
			ELSEIF VAL == 3
			   MOV		A,#00011000B
			   B0MOV	PEDGE,A
		        ELSE
			   ECHO	Error: VAL is invalid parameter of @P00_EDGE macro
			   ECHO	Syntax: @P00_EDGE  EDGE
			   ECHO	<EDGE = 1, Rising edge trigger for P0.0 interrupt>
			   ECHO	<EDGE = 2, Falling edge trigger for P0.0 interrupt>
			   ECHO	<EDGE = 3, Level change(Bi-direction) trigger for P0.0 interrupt>
			   ECHO	Example:
			   ECHO	@P00_EDGE 2   Configure P0.0 interrupt as falling edge trigger
			   ERROR
			   ECHO
		        ENDIF			

			
	ENDM
; -----------------------------------------------------	


.TEXT
{
	//{{SONIX_OPTION_RULE

	IF Noise_Filter == Enable
	{
		Noise_Filter0 = Enable		
		ROM_Chk0 = Enable		
	}
	
	IF Noise_Filter == Disable	
	{
		Noise_Filter0 = Disable
		ROM_Chk0 = Disable
  	}

  	   
	ENDP
	//}}SONIX_OPTION_RULE
}
