;***************************************************************************
;
;	Standard Macros for Microchip PIC MCUs  V2.01
;	=============================================
;
;	written by Peter Luethi, 1999/01/08, Dietikon, Switzerland
;	http://www.electronic-engineering.ch
;	last update: 2010/05/06
;
;	This code and accompanying files may be distributed freely and
;	modified, provided this header with my name and this notice remain
;	intact. Ownership rights remain with me.
;	You may not sell this software without my approval.
;
;	This software comes with no guarantee or warranty except for my
;	good intentions. By using this code you agree to indemnify me from
;	any liability that might arise from its use.
;
;
;	SPECIFICATIONS
;	==============
;	Processor:			any 8-bit Microchip PIC controller
;
;
;	DESCRIPTION
;	===========
;	Standard macros for bank and page handling and conditional branching.
;	Device-specific customization of macros by using #DEFINE TWO_BANKS for
;	adjusting macro instructions to small PIC MCUs with only two register
;	banks in order to optimize the corresponding code size.
;
;
;	DECLARATIONS needed in MAIN PROGRAM
;	===================================
;	To reduce code size for small PIC MCUs, e.g., PIC 12F615:
;		#DEFINE  TWO_BANKS
;
;
;	LIMITATIONS
;	===========
;	-
;
;
;	REQUIRED MEMORY
;	===============
;	-
;
;
;	HISTORY
;	=======
;	1999/01/08	1.00  Initial release of BANK macros
;	2004/08/20	2.00  Added various conditional branching macros
;	2010/05/05	2.01  Added constant TWO_BANKS to customize BANK macros
;					  for small PICs, default: support for four banks
;
;***************************************************************************
#DEFINE	M_BANK_ID dummy

	IFDEF	TWO_BANKS
	  MESSG "Bank macros customized for two-bank devices"
	ELSE
	  MESSG "Bank macros customized for four-bank devices"
	ENDIF

BANK0	macro			; select register bank 0
	IFDEF	TWO_BANKS
	  bcf	STATUS,RP0	; for two-bank devices
	ELSE
	  bcf	STATUS,RP0	; for four-bank devices
	  bcf	STATUS,RP1
	ENDIF
	endm
	
BANK1	macro			; select register bank 1
	IFDEF	TWO_BANKS
	  bsf	STATUS,RP0	; for two-bank devices
	ELSE
	  bsf	STATUS,RP0	; for four-bank devices
	  bcf	STATUS,RP1
	ENDIF
	endm

BANK2	macro			; select register bank 2
	IFDEF	TWO_BANKS
	  ERROR "Unsupported configuration"
	ELSE
	  bcf	STATUS,RP0	; for four-bank devices
	  bsf	STATUS,RP1
	ENDIF
	endm

BANK3	macro			; select register bank 3
	IFDEF	TWO_BANKS
	  ERROR "Unsupported configuration"
	ELSE
	  bsf	STATUS,RP0	; for four-bank devices
	  bsf	STATUS,RP1
	ENDIF
	endm


;*** Conditional branching ***
; Macros to simplify 'IF THEN ELSE' queries
; Pre:  valid w
; Post: compare w vs. m_val, branch to m_target or not

;branch on equal w and m_val
BEQ	macro	m_val, m_target
	sublw	m_val
	bz	m_target	; zero bit set, branch
	endm

;branch on 0 value in register m_file, jump to m_target
BZF	macro	m_file, m_target
	tstf	m_file		; check register (Z)
	bz	m_target	; zero bit set, branch
	endm	

;branch on not equal w and m_val
BNEQ	macro	m_val, m_target
	sublw	m_val
	bnz	m_target	; zero bit not set, branch
	endm

;branch on not 0 value in register m_file, jump to m_target
BNZF	macro	m_file, m_target
	tstf	m_file		; check register (Z)
	bnz	m_target	; zero bit set, branch
	endm	

;branch on greater w than m_val
BRG	macro	m_val, m_target
	sublw	m_val		; result = m_val - w
	bnc	m_target	; no carry if result negative, branch
	endm

;branch on equal or greater w than m_val
BREG	macro	m_val, m_target
	sublw	m_val-0x1	; result = (m_val-1) - w
	bnc	m_target	; no carry if result negative, branch
	endm

;branch on smaller w than m_val
BRS	macro	m_val, m_target
	sublw	m_val-0x1	; result = (m_val-1) - w
	bc	m_target	; carry if result zero or positive, branch
	endm

;branch on equal or smaller w than m_val
BRES	macro	m_val, m_target
	sublw	m_val		; result = m_val - w
	bc	m_target	; carry if result zero or positive, branch
	endm


;*** Microchip Tips'n Tricks ***
; swaps the contents of W and REG without using a second register
; from 'Microchip Tips'n Tricks'
SWAPWF macro REG
	XORWF	REG,F
	XORWF	REG,W
	XORWF	REG,F
	endm

