-- PIC18Fxx2 (Indirect access) 16bit Math Library. -- Using JAL (jal.sf.net) language. -- -- Copyright (C) 2005 Javier Martinez -- -- This library is free software; you can redistribute it and/or -- modify it under the terms of the GNU Lesser General Public -- License as published by the Free Software Foundation; either -- version 2.1 of the License, or (at your option) any later version. -- -- This library is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -- Lesser General Public License for more details. -- -- You should have received a copy of the GNU Lesser General Public -- License along with this library; if not, write to the Free Software -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -- -- Javier Martinez, 16 march 2005 -- japus11 (at) gmail (dot) com -- -- -- ********* Exported functions/procedures in pseudocode format ************ -- -- > I16_CGT ( op1 , op2 : ^Address ) : bit -- < I16_CLT ( op1 , op2 : ^Address ) : bit -- = I16_CEQ ( op1 , op2 : ^Address ) : bit -- >= I16_CGE ( op1 , op2 : ^Address ) : bit -- <= I16_CLE ( op1 , op2 : ^Address ) : bit -- <> I16_CNE ( op1 , op2 : ^Address ) : bit -- -- Swap I16_SWP ( op1 , op2 : ^Address ) -- Load I16_LDL ( op1 : ^Address ; byte1 , byte0 : byte ) -- Save I16_SVL ( op1 : ^Address ; byte1 , byte0 : byte ) -- -- Shift L I16_SLF ( op1 : ^Address ) -- Shift R I16_SRF ( op1 : ^Address ) -- C Shift L I16_SLFC ( op1 : ^Address ; C ) -- C Shift R I16_SRFC ( op1 : ^Address ; C ) -- -- Incr I16_INC ( op1 : ^Address ) -- Decr I16_DEC ( op1 : ^Address ) -- -- Add I16_ADD ( op1 , op2 : ^Address ) -- Sub I16_SUB ( op1 , op2 : ^Address ) -- Add8 I16_ADD8 ( op1 : ^Address ; data : byte ) -- Sub8 I16_Sub8 ( op1 : ^Address ; data : byte ) -- -- And I16_AND ( op1 , op2 : ^Address ) -- IOr I16_IOR ( op1 , op2 : ^Address ) -- XOr I16_XOR ( op1 , op2 : ^Address ) -- Compl I16_COM ( op1 : ^Address ) -- Neg I16_NEG ( op1 : ^Address ) -- Set I16_SET ( op1 : ^Address ) -- Clear I16_CLR ( op1 : ^Address ) -- -- Bit Test I16_BTT ( op1 : ^Address , offset : byte ) : bit -- -- Used FSRs -- -- -- -- -- ********* Variable declaration ************ include ./javi_lib/pic18_pointers -- ********* Body ************ -- -- -- Test ( op1 > op2 ) -- I16_CGT ( op1 , op2 : ^Address ) : bit -- function I16_CGT ( byte in OP1 , byte in OP2 ) return bit is begin var bit CGT_result LoadPointer_0 ( OP1 , 1 ) -- Start at MSB LoadPointer_1 ( OP2 , 1 ) -- Start at MSB assembler local CGT_FALSE , CGT_TRUE , CGT_1 , CGT_2 , CGT_END movf postdec1, w cpfsgt indf0 bra CGT_1 bra CGT_TRUE CGT_1: cpfseq postdec0 bra CGT_FALSE movf postdec1, w cpfsgt indf0 bra CGT_2 bra CGT_TRUE CGT_2: cpfseq postdec0 bra CGT_FALSE CGT_TRUE: bsf CGT_result bra CGT_END CGT_FALSE: bcf CGT_result CGT_END: end assembler return CGT_result end function -- -- -- -- Test ( op1 < op2 ) -- I16_CLT ( op1 , op2 : ^Address ) : bit -- function I16_CLT ( byte in OP1 , byte in OP2 ) return bit is begin var bit CLT_result LoadPointer_0 ( OP1 , 1 ) -- Start at MSB LoadPointer_1 ( OP2 , 1 ) -- Start at MSB assembler local CLT_FALSE , CLT_TRUE , CLT_END , CLT_1 , CLT_2 movf postdec1, w cpfslt indf0 bra CLT_1 bra CLT_TRUE CLT_1: cpfseq postdec0 bra CLT_FALSE movf postdec1, w cpfslt indf0 bra CLT_2 bra CLT_TRUE CLT_2: cpfseq postdec0 bra CLT_FALSE CLT_TRUE: bsf CLT_result bra CLT_END CLT_FALSE: bcf CLT_result CLT_END: end assembler return CLT_result end function -- -- -- -- Test ( op1 = op2 ) -- I16_CEQ ( op1 , op2 : ^Address ) : bit -- function I16_CEQ ( byte in OP1 , byte in OP2 ) return bit is begin var bit CEQ_result LoadPointer_0 ( OP1 , 1 ) -- Start at MSB LoadPointer_1 ( OP2 , 1 ) -- Start at MSB assembler local CEQ_FALSE , CEQ_TRUE , CEQ_END movf postdec1, w cpfseq postdec0 bra CEQ_FALSE movf postdec1, w cpfseq postdec0 bra CEQ_FALSE CEQ_TRUE: bsf CEQ_result bra CEQ_END CEQ_FALSE: bcf CEQ_result CEQ_END: end assembler return CEQ_result end function -- -- -- -- Test ( op1 >= op2 ) -- I16_CGE ( op1 , op2 : ^Address ) : bit -- function I16_CGE ( byte in OP1 , byte in OP2 ) return bit is begin var bit CGE_result LoadPointer_0 ( OP1 , 1 ) -- Start at MSB LoadPointer_1 ( OP2 , 1 ) -- Start at MSB assembler local CGE_FALSE , CGE_TRUE , CGE_1 , CGE_END movf postdec1, w cpfslt indf0 bra CGE_1 bra CGE_FALSE CGE_1: cpfseq postdec0 bra CGE_TRUE movf postdec1, w cpfslt indf0 bra CGE_TRUE bra CGE_FALSE CGE_TRUE: bsf CGE_result bra CGE_END CGE_FALSE: bcf CGE_result CGE_END: end assembler return CGE_result end function -- -- -- -- Test ( op1 <= op2 ) -- I16_CLE ( op1 , op2 : ^Address ) : bit -- function I16_CLE ( byte in OP1 , byte in OP2 ) return bit is begin var bit CLE_result LoadPointer_0 ( OP1 , 1 ) -- Start at MSB LoadPointer_1 ( OP2 , 1 ) -- Start at MSB assembler local CLE_FALSE , CLE_TRUE , CLE_END , CLE_1 movf postdec1, w cpfsgt indf0 bra CLE_1 bra CLE_FALSE CLE_1: cpfseq postdec0 bra CLE_TRUE movf postdec1, w cpfsgt indf0 bra CLE_TRUE bra CLE_FALSE CLE_TRUE: bsf CLE_result bra CLE_END CLE_FALSE: bcf CLE_result CLE_END: end assembler return CLE_result end function -- -- -- -- Test ( op1 <> op2 ) -- I16_CNE ( op1 , op2 : ^Address ) : bit -- function I16_CNE ( byte in OP1 , byte in OP2 ) return bit is begin var bit CNE_result LoadPointer_0 ( OP1 , 1 ) -- Start at MSB LoadPointer_1 ( OP2 , 1 ) -- Start at MSB assembler local CNE_FALSE , CNE_TRUE , CNE_END movf postdec1, w cpfseq postdec0 bra CNE_TRUE movf postdec1, w cpfseq postdec0 bra CNE_TRUE bra CNE_FALSE CNE_TRUE: bsf CNE_result bra CNE_END CNE_FALSE: bcf CNE_result CNE_END: end assembler return CNE_result end function -- -- -- -- Swap ( op1 <-> op2 ) -- I16_SWP ( op1 , op2 : ^Address ) -- procedure I16_SWP ( byte in OP1 , byte in OP2 ) is begin var byte SWP_temp LoadPointer_0 ( OP1 ) -- Start at LSB LoadPointer_1 ( OP2 ) -- Start at LSB assembler movf indf0, w movwf SWP_temp movf indf1, w movwf postinc0 movf SWP_temp, w movwf postinc1 movf indf0, w movwf SWP_temp movf indf1, w movwf postinc0 movf SWP_temp, w movwf postinc1 end assembler end procedure -- -- -- -- Load Literal ( op1 <- Byte1:Byte0 ) -- I16_LDL ( op1 : ^Address ; byte1 , byte0 : byte ) -- procedure I16_LDL ( byte in OP1 , byte in byte1 , byte in byte0 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler movf byte0, w movwf postinc0 movf byte1, w movwf postinc0 end assembler end procedure -- -- -- -- Save Literal ( op1 -> Byte1:Byte0 ) -- I16_SVL ( op1 : ^Address ; byte1 , byte0 : byte ) -- procedure I16_SVL ( byte in OP1 , byte out byte1 , byte out byte0 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler movf postinc0, w movwf byte0 movf postinc0, w movwf byte1 end assembler end procedure -- -- -- -- Shift Left (op1 = op1 << 1 ) -- I16_SLF ( op1 : ^Address ) -- procedure I16_SLF ( byte in OP1 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler bcf status_c rlf postinc0, f rlf postinc0, f end assembler end procedure -- -- -- -- Shift Right ( op1 = op1 >> 1 ) -- I16_SRF ( op1 : ^Address ) -- procedure I16_SRF ( byte in OP1 ) is begin LoadPointer_0 ( OP1 , 1 ) -- Start at MSB assembler bcf status_c rrf postdec0, f rrf postdec0, f end assembler end procedure -- -- -- -- Shift Left With Carry ( op1= op1 << 1,C ) -- I16_SLFC ( op1 : ^Address ; C : bit ) -- procedure I16_SLFC ( byte in OP1 , bit in out SLFC_carry ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB Status_C = SLFC_carry assembler rlf postinc0, f rlf postinc0, f end assembler SLFC_carry = Status_C end procedure -- -- -- -- Shift Right With Carry ( op1 = op1 >> 1,C ) -- I16_SRFC ( op1 : ^Address ; C : bit) -- procedure I16_SRFC ( byte in OP1 , bit in out SRFC_carry ) is begin LoadPointer_0 ( OP1 , 1 ) -- Start at MSB Status_C = SRFC_carry assembler rrf postdec0, f rrf postdec0, f end assembler SRFC_carry = Status_C end procedure -- -- -- -- Increment ( op1 = op1 + 1 ) -- I16_INC ( op1 : ^Address ) -- procedure I16_INC ( byte in OP1 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler local INC_END INCF postinc0, f BNC INC_END INCF postinc0, f INC_END: end assembler end procedure -- -- -- -- Decrement ( op1 = op1 - 1 ) -- I16_DEC ( op1 : ^Address ) -- procedure I16_DEC ( byte in OP1 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler local DEC_END DECF postinc0,f BC DEC_END DECF postinc0,f DEC_END: end assembler end procedure -- -- -- -- Add ( op1 = op1 + op2 ) -- I16_ADD ( op1, op2 : ^Address ) -- procedure I16_ADD ( byte in OP1 , byte in OP2 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB LoadPointer_1 ( OP2 ) -- Start at LSB assembler movf postinc1, w addwf postinc0, f movf postinc1, w addwfc postinc0, f end assembler end procedure -- -- -- -- Add8 ( op1 = op1 + byte ) -- I16_ADD8 ( op1 : ^Address ; data : byte ) -- procedure I16_ADD8 ( byte in OP1 , byte in ADD8_data ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler movf ADD8_data, w addwf postinc0, f movlw 0x00 addwfc postinc0, f end assembler end procedure -- -- -- -- Sub ( op1 = op1 - op2 ) -- I16_SUB ( op1, op2 : ^Address ) -- procedure I16_SUB ( byte in OP1 , byte in OP2 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB LoadPointer_1 ( OP2 ) -- Start at LSB assembler movf postinc1, w subwf postinc0,f movf postinc1, w subwfb postinc0,f end assembler end procedure -- -- -- -- Sub8 ( op1 = op1 - byte ) -- I16_SUB8 ( op1 : ^Address ; data : byte ) -- procedure I16_SUB8 ( byte in OP1 , byte in SUB8_data ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler movf SUB8_data, w subwf postinc0,f movlw 0x00 subwfb postinc0,f end assembler end procedure -- -- -- -- And ( op1 = op1 .and. op2 ) -- I16_AND ( op1, op2 : ^Address ) -- procedure I16_AND ( byte in OP1 , byte in OP2 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB LoadPointer_1 ( OP2 ) -- Start at LSB assembler movf postinc1, w andwf postinc0, f movf postinc1, w andwf postinc0, f end assembler end procedure -- -- -- -- IOr ( op1 = op1 .ior. op2 ) -- I16_IOR ( op1, op2 : ^Address ) -- procedure I16_IOR ( byte in OP1 , byte in OP2 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB LoadPointer_1 ( OP2 ) -- Start at LSB assembler movf postinc1, w iorwf postinc0, f movf postinc1, w iorwf postinc0, f end assembler end procedure -- -- -- -- XOr ( op1 = op1 .xor. op2 ) -- I16_XOR ( op1, op2 : ^Address ) -- procedure I16_XOR ( byte in OP1 , byte in OP2 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB LoadPointer_1 ( OP2 ) -- Start at LSB assembler movf postinc1, w xorwf postinc0, f movf postinc1, w xorwf postinc0, f end assembler end procedure -- -- -- -- Complement (op1 = .not. op1 ) -- I16_COM ( op1 : ^Address ) -- procedure I16_COM ( byte in OP1 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler comf postinc0, f comf postinc0, f end assembler end procedure -- -- -- -- Negate ( op1 = (-1) x op1 ) -- I16_NEG ( op1 : ^Address ) -- procedure I16_NEG ( byte in OP1 ) is begin I16_COM ( OP1 ) I16_INC ( OP1 ) end procedure -- -- -- -- Set ( OP1 <- 0xFFFF ) -- I16_SET ( op1 : ^Address ) -- procedure I16_SET ( byte in OP1) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler setf postinc0 setf postinc0 end assembler end procedure -- -- -- -- Clear ( OP1 <- 0x0000 ) -- I16_CLR ( op1 : ^Address ) -- procedure I16_CLR ( byte in OP1) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler clrf postinc0 clrf postinc0 end assembler end procedure -- -- -- -- Bit Test ( OP1:offset ) -- I16_BTT ( op1 : ^Address , offset : byte ) : bit -- function I16_BTT ( byte in OP1 , byte in BTT_offset) return bit is begin var bit BTT_bit = false var byte BTT_byte var volatile bit BTT_test_bit at BTT_byte : 0 if BTT_offset < 15 then if BTT_offset > 7 then LoadPointer_0 ( OP1 , 1 ) BTT_byte = indf0 if BTT_offset == 8 then BTT_bit = BTT_test_bit else BTT_byte = BTT_byte >> ( BTT_offset - 8 ) BTT_bit = BTT_test_bit end if else LoadPointer_0 ( OP1 ) BTT_byte = indf0 if BTT_offset == 0 then BTT_bit = BTT_test_bit else BTT_byte = BTT_byte >> ( BTT_offset ) BTT_bit = BTT_test_bit end if end if end if return BTT_bit end function