-- PIC18Fxx2 (Indirect access) 32bit 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 ************ -- -- > I32_CGT ( op1 , op2 : ^Address ) : bit -- < I32_CLT ( op1 , op2 : ^Address ) : bit -- = I32_CEQ ( op1 , op2 : ^Address ) : bit -- >= I32_CGE ( op1 , op2 : ^Address ) : bit -- <= I32_CLE ( op1 , op2 : ^Address ) : bit -- <> I32_CNE ( op1 , op2 : ^Address ) : bit -- -- Swap I32_SWP ( op1 , op2 : ^Address ) -- Load I32_LDL ( op1 : ^Address ; byte3 , byte2 , byte1 , byte0 : byte ) -- Save I32_SVL ( op1 : ^Address ; byte3 , byte2 , byte1 , byte0 : byte ) -- -- Shift L I32_SLF ( op1 : ^Address ) -- Shift R I32_SRF ( op1 : ^Address ) -- C Shift L I32_SLFC ( op1 : ^Address ; C ) -- C Shift R I32_SRFC ( op1 : ^Address ; C ) -- -- Incr I32_INC ( op1 : ^Address ) -- Decr I32_DEC ( op1 : ^Address ) -- -- Add I32_ADD ( op1 , op2 : ^Address ) -- Sub I32_SUB ( op1 , op2 : ^Address ) -- Add8 I32_ADD8 ( op1 : ^Address ; data : byte ) -- Add16 I32_ADD16 ( op1 : ^Address ; dataH,dataL : byte ) -- Sub8 I32_Sub8 ( op1 : ^Address ; data : byte ) -- Sub16 I32_Sub16 ( op1 : ^Address ; dataH,dataL : byte ) -- -- And I32_AND ( op1 , op2 : ^Address ) -- IOr I32_IOR ( op1 , op2 : ^Address ) -- XOr I32_XOR ( op1 , op2 : ^Address ) -- Compl I32_COM ( op1 : ^Address ) -- Neg I32_NEG ( op1 : ^Address ) -- Set I32_SET ( op1 : ^Address ) -- Clear I32_CLR ( op1 : ^Address ) -- -- Bit Test I32_BTT ( op1 : ^Address , offset : byte ) : bit -- -- Used FSRs -- -- -- -- -- ********* Variable declaration ************ include ./javi_lib/pic18_pointers -- ********* Body ************ -- -- -- Test ( op1 > op2 ) -- I32_CGT ( op1 , op2 : ^Address ) : bit -- function I32_CGT ( byte in OP1 , byte in OP2 ) return bit is begin var bit CGT_result LoadPointer_0 ( OP1 , 3 ) -- Start at MSB LoadPointer_1 ( OP2 , 3 ) -- Start at MSB assembler local CGT_FALSE , CGT_TRUE , CGT_1 , CGT_2 , CGT_3 , CGT_4 , 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 movf postdec1, w cpfsgt indf0 bra CGT_3 bra CGT_TRUE CGT_3: cpfseq postdec0 bra CGT_FALSE movf postdec1, w cpfsgt indf0 bra CGT_4 bra CGT_TRUE CGT_4: 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 ) -- I32_CLT ( op1 , op2 : ^Address ) : bit -- function I32_CLT ( byte in OP1 , byte in OP2 ) return bit is begin var bit CLT_result LoadPointer_0 ( OP1 , 3 ) -- Start at MSB LoadPointer_1 ( OP2 , 3 ) -- Start at MSB assembler local CLT_FALSE , CLT_TRUE , CLT_END , CLT_1 , CLT_2 , CLT_3 , CLT_4 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 movf postdec1, w cpfslt indf0 bra CLT_3 bra CLT_TRUE CLT_3: cpfseq postdec0 bra CLT_FALSE movf postdec1, w cpfslt indf0 bra CLT_4 bra CLT_TRUE CLT_4: 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 ) -- I32_CEQ ( op1 , op2 : ^Address ) : bit -- function I32_CEQ ( byte in OP1 , byte in OP2 ) return bit is begin var bit CEQ_result LoadPointer_0 ( OP1 , 3 ) -- Start at MSB LoadPointer_1 ( OP2 , 3 ) -- 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 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 ) -- I32_CGE ( op1 , op2 : ^Address ) : bit -- function I32_CGE ( byte in OP1 , byte in OP2 ) return bit is begin var bit CGE_result LoadPointer_0 ( OP1 , 3 ) -- Start at MSB LoadPointer_1 ( OP2 , 3 ) -- Start at MSB assembler local CGE_FALSE , CGE_TRUE , CGE_1 , CGE_2 , CGE_3 , CGE_4 , 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_2 bra CGE_FALSE CGE_2: cpfseq postdec0 bra CGE_TRUE movf postdec1, w cpfslt indf0 bra CGE_3 bra CGE_FALSE CGE_3: cpfseq postdec0 bra CGE_TRUE movf postdec1, w cpfslt indf0 bra CGE_4 bra CGE_FALSE CGE_4: cpfseq postdec0 bra CGE_TRUE 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 ) -- I32_CLE ( op1 , op2 : ^Address ) : bit -- function I32_CLE ( byte in OP1 , byte in OP2 ) return bit is begin var bit CLE_result LoadPointer_0 ( OP1 , 3 ) -- Start at MSB LoadPointer_1 ( OP2 , 3 ) -- Start at MSB assembler local CLE_FALSE , CLE_TRUE , CLE_END , CLE_1 , CLE_2 , CLE_3 , CLE_4 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_2 bra CLE_FALSE CLE_2: cpfseq postdec0 bra CLE_TRUE movf postdec1, w cpfsgt indf0 bra CLE_3 bra CLE_FALSE CLE_3: cpfseq postdec0 bra CLE_TRUE movf postdec1, w cpfsgt indf0 bra CLE_4 bra CLE_FALSE CLE_4: cpfseq postdec0 bra CLE_TRUE 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 ) -- I32_CNE ( op1 , op2 : ^Address ) : bit -- function I32_CNE ( byte in OP1 , byte in OP2 ) return bit is begin var bit CNE_result LoadPointer_0 ( OP1 , 3 ) -- Start at MSB LoadPointer_1 ( OP2 , 3 ) -- 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 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 ) -- I32_SWP ( op1 , op2 : ^Address ) -- procedure I32_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 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 <- Byte3..Byte0 ) -- I32_LDL ( op1 : ^Address ; byte3 , byte2 , byte1 , byte0 : byte ) -- procedure I32_LDL ( byte in OP1 , byte in byte3 , byte in byte2 , 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 movf byte2, w movwf postinc0 movf byte3, w movwf postinc0 end assembler end procedure -- -- -- -- Save Literal ( op1 -> Byte3..Byte0 ) -- I32_SVL ( op1 : ^Address ; byte3 , byte2 , byte1 , byte0 : byte ) -- procedure I32_SVL ( byte in OP1 , byte out byte3 , byte out byte2 , 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 movf postinc0, w movwf byte2 movf postinc0, w movwf byte3 end assembler end procedure -- -- -- -- Shift Left (op1 = op1 << 1 ) -- I32_SLF ( op1 : ^Address ) -- procedure I32_SLF ( byte in OP1 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler bcf status_c rlf postinc0, f rlf postinc0, f rlf postinc0, f rlf postinc0, f end assembler end procedure -- -- -- -- Shift Right ( op1 = op1 >> 1 ) -- I32_SRF ( op1 : ^Address ) -- procedure I32_SRF ( byte in OP1 ) is begin LoadPointer_0 ( OP1 , 3 ) -- Start at MSB assembler bcf status_c rrf postdec0, f rrf postdec0, f rrf postdec0, f rrf postdec0, f end assembler end procedure -- -- -- -- Shift Left With Carry ( op1= op1 << 1,C ) -- I32_SLFC ( op1 : ^Address ; C : bit ) -- procedure I32_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 rlf postinc0, f rlf postinc0, f end assembler SLFC_carry = Status_C end procedure -- -- -- -- Shift Right With Carry ( op1 = op1 >> 1,C ) -- I32_SRFC ( op1 : ^Address ; C : bit) -- procedure I32_SRFC ( byte in OP1 , bit in out SRFC_carry ) is begin LoadPointer_0 ( OP1 , 3 ) -- Start at MSB Status_C = SRFC_carry assembler rrf postdec0, f rrf postdec0, f rrf postdec0, f rrf postdec0, f end assembler SRFC_carry = Status_C end procedure -- -- -- -- Increment ( op1 = op1 + 1 ) -- I32_INC ( op1 : ^Address ) -- procedure I32_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 BNC INC_END INCF postinc0, f BNC INC_END INCF postinc0, f INC_END: end assembler end procedure -- -- -- -- Decrement ( op1 = op1 - 1 ) -- I32_DEC ( op1 : ^Address ) -- procedure I32_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 BC DEC_END DECF postinc0,f BC DEC_END DECF postinc0,f DEC_END: end assembler end procedure -- -- -- -- Add ( op1 = op1 + op2 ) -- I32_ADD ( op1, op2 : ^Address ) -- procedure I32_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 movf postinc1, w addwfc postinc0, f movf postinc1, w addwfc postinc0, f end assembler end procedure -- -- -- -- Add8 ( op1 = op1 + byte ) -- I32_ADD8 ( op1 : ^Address ; data : byte ) -- procedure I32_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 movlw 0x00 addwfc postinc0, f movlw 0x00 addwfc postinc0, f end assembler end procedure -- -- -- -- Add16 ( op1 = op1 + word ) -- I32_ADD16 ( op1 : ^Address ; dataH,dataL : byte ) -- procedure I32_ADD16 ( byte in OP1 , byte in ADD16_dataH , byte in ADD16_dataL ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler movf ADD16_dataL, w addwf postinc0, f movf ADD16_dataH, w addwfc postinc0, f movlw 0x00 addwfc postinc0, f movlw 0x00 addwfc postinc0, f end assembler end procedure -- -- -- -- Sub ( op1 = op1 - op2 ) -- I32_SUB ( op1, op2 : ^Address ) -- procedure I32_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 movf postinc1, w subwfb postinc0,f movf postinc1, w subwfb postinc0,f end assembler end procedure -- -- -- -- Sub8 ( op1 = op1 - byte ) -- I32_SUB8 ( op1 : ^Address ; data : byte ) -- procedure I32_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 movlw 0x00 subwfb postinc0,f movlw 0x00 subwfb postinc0,f end assembler end procedure -- -- -- -- Sub16 ( op1 = op1 - word ) -- I32_SUB16 ( op1 : ^Address ; dataH,dataL : byte ) -- procedure I32_SUB16 ( byte in OP1 , byte in SUB16_dataH , byte in SUB16_dataL ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler movf SUB16_dataL, w subwf postinc0,f movf SUB16_dataH, w subwfb postinc0,f movlw 0x00 subwfb postinc0,f movlw 0x00 subwfb postinc0,f end assembler end procedure -- -- -- -- And ( op1 = op1 .and. op2 ) -- I32_AND ( op1, op2 : ^Address ) -- procedure I32_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 movf postinc1, w andwf postinc0, f movf postinc1, w andwf postinc0, f end assembler end procedure -- -- -- -- IOr ( op1 = op1 .ior. op2 ) -- I32_IOR ( op1, op2 : ^Address ) -- procedure I32_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 movf postinc1, w iorwf postinc0, f movf postinc1, w iorwf postinc0, f end assembler end procedure -- -- -- -- XOr ( op1 = op1 .xor. op2 ) -- I32_XOR ( op1, op2 : ^Address ) -- procedure I32_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 movf postinc1, w xorwf postinc0, f movf postinc1, w xorwf postinc0, f end assembler end procedure -- -- -- -- Complement (op1 = .not. op1 ) -- I32_COM ( op1 : ^Address ) -- procedure I32_COM ( byte in OP1 ) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler comf postinc0, f comf postinc0, f comf postinc0, f comf postinc0, f end assembler end procedure -- -- -- -- Negate ( op1 = (-1) x op1 ) -- I32_NEG ( op1 : ^Address ) -- procedure I32_NEG ( byte in OP1 ) is begin I32_COM ( OP1 ) I32_INC ( OP1 ) end procedure -- -- -- -- Set ( OP1 <- 0xFFFFFFFF ) -- I32_SET ( op1 : ^Address ) -- procedure I32_SET ( byte in OP1) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler setf postinc0 setf postinc0 setf postinc0 setf postinc0 end assembler end procedure -- -- -- -- Clear ( OP1 <- 0x00000000 ) -- I32_CLR ( op1 : ^Address ) -- procedure I32_CLR ( byte in OP1) is begin LoadPointer_0 ( OP1 ) -- Start at LSB assembler clrf postinc0 clrf postinc0 clrf postinc0 clrf postinc0 end assembler end procedure -- -- -- -- Bit Test ( OP1:offset ) -- I32_BTT ( op1 : ^Address , offset : byte ) : bit -- function I32_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 < 31 then if BTT_offset > 23 then LoadPointer_0 ( OP1 , 3 ) BTT_byte = indf0 if BTT_offset == 24 then BTT_bit = BTT_test_bit else BTT_byte = BTT_byte >> ( BTT_offset - 24 ) BTT_bit = BTT_test_bit end if elsif BTT_offset > 15 then LoadPointer_0 ( OP1 , 2 ) BTT_byte = indf0 if BTT_offset == 16 then BTT_bit = BTT_test_bit else BTT_byte = BTT_byte >> ( BTT_offset - 16 ) BTT_bit = BTT_test_bit end if elsif 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