subtitle "branches.h by L. Wyard-Scott" ;; ********************************************************************* ;;; File Name: branches.h ;;; Purpose: Macros for conditional branching based on ;;; unsigned values. ;;; Revision: 0.0 ;;; Date: 27 September 2003 ;;; Author: L. Wyard-Scott ;;; Copyright: (c) 2003 ;;; Device: PIC16F873, PIC16F874, PIC16F876, PIC16F877. ;;; PIC16F873A, PIC16F874A, PIC16F876A, PIC16F877A. ;;; ********************************************************************* ;;; Revision History: ;;; 0.0 - 27 September 2003 ;;; - Creation ;;; ********************************************************************* ;;; Ideas for future work: ;;; - make signed equivalents of these routines (this is a lot more work). ;;; ********************************************************************* ;;; -------------------------------------------------------------------- ;;; Macros. ;;; -------------------------------------------------------------------- ;;; -------------------------------------------------------------------- ;;; The following 2 macros can be used to to determine if W is equal ;;; or not equal to a constant. These values may be considered signed ;;; or unsigned. ;;; -------------------------------------------------------------------- ;;; ******************************************************************** ;;; Macro Name: brweqk ;;; Description: Branch if W is equal to a specified byte ;;; constant. Page selection bits must be established ;;; appropriately in order to successfully branch ;;; to the destination. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; value - the other value for the comparison. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W == the unsigned value. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brweqk: MACRO value, destination sublw value btfsc STATUS,Z goto destination ENDM ;;; ******************************************************************** ;;; Macro Name: brwneqk ;;; Description: Branch if W is not equal to a specified byte ;;; constant. Page selection bits must be established ;;; appropriately in order to successfully branch ;;; to the destination. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; value - the other value for the comparison. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W != the unsigned value. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brwneqk: MACRO value, destination sublw value btfss STATUS,Z goto destination ENDM ;;; -------------------------------------------------------------------- ;;; The following 4 macros are used for comparing unsigned literal values ;;; with the contents of the W register. ;;; -------------------------------------------------------------------- ;;; ******************************************************************** ;;; Macro Name: brwlok ;;; Description: Branch if W is lower than a specified byte ;;; constant. Page selection bits must be established ;;; appropriately in order to successfully branch ;;; to the destination. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; value - the other value for the comparison. ;;; This value must be non-zero. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W < the unsigned value. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brwlok: MACRO value, destination addlw (0x100-value) btfss STATUS,C goto destination ENDM ;;; ******************************************************************** ;;; Macro Name: brwlsk ;;; Description: Branch if W is lower than or the same as the ;;; specified byte constant. Page selection bits ;;; must be established appropriately in order to ;;; successfully branch to the destination. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; value - the other value for the comparison. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W <= the unsigned value. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brwlsk: MACRO value, destination addlw (0xFF-value) btfss STATUS,C goto destination ENDM ;;; ******************************************************************** ;;; Macro Name: brwhik ;;; Description: Branch if W is higher than a specified byte ;;; constant. Page selection bits must be established ;;; appropriately in order to successfully branch ;;; to the destination. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; value - the other value for the comparison. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W > value. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brwhik: MACRO value, destination addlw (0xFF-value) btfsc STATUS,C goto destination ENDM ;;; ******************************************************************** ;;; Macro Name: brwhsk ;;; Description: Branch if W is higher than or the same as the ;;; specified byte constant. Page selection bits ;;; must be established appropriately in order to ;;; successfully branch to the destination. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; value - the other value for the comparison. ;;; This value must be non-zero. ;;; Use other techniques in the case ;;; where you'd like to see if W is ;;; greater than 0. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W >= the unsigned value. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brwhsk: MACRO value, destination addlw (0x100-value) btfsc STATUS,C goto destination ENDM ;;; -------------------------------------------------------------------- ;;; The following 2 macros can be used to to determine if W is equal ;;; or not equal to the contents of a file register. These values ;;; may be considered signed or unsigned. ;;; -------------------------------------------------------------------- ;;; ******************************************************************** ;;; Macro Name: brweqf ;;; Description: Branch if W is equal to the contents of the ;;; specified file register. Page selection bits ;;; must be established appropriately in order to ;;; successfully branch to the destination. ;;; Bank selection bits must be established ;;; in order to access the file register. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; fileregister - the location of the other ;;; value. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W == the file register contents. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brweqf: MACRO fileregister, destination subwf fileregister,W btfsc STATUS,Z goto destination ENDM ;;; ******************************************************************** ;;; Macro Name: brwneqf ;;; Description: Branch if W is not equal to the contents of the ;;; specified file register. Page selection bits ;;; must be established appropriately in order to ;;; successfully branch to the destination. ;;; Bank selection bits must be established ;;; in order to access the file register. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; fileregister - the location of the other ;;; value. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W != the file register contents. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brwneqf: MACRO fileregister, destination subwf fileregister,W btfss STATUS,Z goto destination ENDM ;;; -------------------------------------------------------------------- ;;; The following 4 macros are used for comparing unsigned values ;;; located in specified file registers with the contents of the ;;; W register. ;;; -------------------------------------------------------------------- ;;; ******************************************************************** ;;; Macro Name: brwlof ;;; Description: Branch if W is lower than the contents of the ;;; specified file register. Page selection bits ;;; must be established appropriately in order to ;;; successfully branch to the destination. ;;; Bank selection bits must be established ;;; in order to access the file register. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; fileregister - the location of the other ;;; value. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W < the file register contents. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brwlof: MACRO fileregister, destination subwf fileregister,W ; F-W -> W ;; Branches iff Z=0, C=1 btfsc STATUS,Z goto $ + 3 btfsc STATUS,C goto destination ENDM ;;; ******************************************************************** ;;; Macro Name: brwlsf ;;; Description: Branch if W is lower than or the same as the ;;; contents of the specified file register. ;;; Page selection bits must be established ;;; appropriately in order to successfully branch ;;; to the destination. ;;; Bank selection bits must be established ;;; in order to access the file register. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; fileregister - the location of the other ;;; value. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W <= the file register contents. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brwlsf: MACRO fileregister, destination subwf fileregister,W ;; Branches iff Z=1, C=1, or Z=0, C=1 (resulting in C=1). btfsc STATUS,C goto destination ENDM ;;; ******************************************************************** ;;; Macro Name: brwhif ;;; Description: Branch if W is higher than the contents of the ;;; specified file register. Page selection bits ;;; must be established appropriately in order to ;;; successfully branch to the destination. ;;; Bank selection bits must be established ;;; in order to access the file register. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; fileregister - the location of the other ;;; value. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W > the file register contents. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brwhif: MACRO fileregister, destination subwf fileregister,W ;; Branches iff Z=0, C=0 btfsc STATUS,Z goto $ + 3 btfss STATUS,C goto destination ENDM ;;; ******************************************************************** ;;; Macro Name: brwlsf ;;; Description: Branch if W is lower than or the same as the ;;; contents of the specified file register. ;;; Page selection bits must be established ;;; appropriately in order to successfully branch ;;; to the destination. ;;; Bank selection bits must be established ;;; in order to access the file register. ;;; Requires: W contains one of the values being compared to. ;;; Two additional parameters are: ;;; fileregister - the location of the other ;;; value. ;;; destination - the destination address. ;;; Returns: Nothing. Program execution goes to "destination" ;;; if W <= the file register contents. ;;; Locations Affected: W is destroyed. ;;; ******************************************************************** brwhsf: MACRO fileregister, destination subwf fileregister,W ;; Branches iff Z=1, C=1, or Z=0, C=0 (resulting in Z=C). btfss STATUS,Z goto $ + 4 btfsc STATUS,C goto destination goto $ + 3 btfss STATUS,C goto destination ENDM