⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbl_math.c

📁 pic16c6x和pic16c7xxx都可以通用
💻 C
字号:
/******************************************************************************
* Double Precision Math Routines
*
* This module contains assembly language routines from "Math Routines for the 
* 16C5x" from Microchip's Embedded Controller Handbook that have been adapted 
* for use with the Bytecraft MPC C Compiler.
*
* Routines are used IIR_FILT.C module written for "Digital Signal Processing 
* with the PIC16C74" Application Note.
*
* D. Mostowfi 3/95
*****************************************************************************/

/*
Start of converted MPASM modules:

;*******************************************************************
;                 Double Precision Addition & Subtraction
;
;*******************************************************************;
;   Addition :  ACCb(16 bits) + ACCa(16 bits) -> ACCb(16 bits)
;      (a) Load the 1st operand in location ACCaLO & ACCaHI ( 16 bits )
;      (b) Load the 2nd operand in location ACCbLO & ACCbHI ( 16 bits )
;      (c) CALL D_add
;      (d) The result is in location ACCbLO & ACCbHI ( 16 bits )
;
;   Performance :
;               Program Memory  :       07
;               Clock Cycles    :       08
;*******************************************************************;
;   Subtraction : ACCb(16 bits) - ACCa(16 bits) -> ACCb(16 bits)
;      (a) Load the 1st operand in location ACCaLO & ACCaHI ( 16 bits )
;      (b) Load the 2nd operand in location ACCbLO & ACCbHI ( 16 bits )
;      (c) CALL D_sub
;      (d) The result is in location ACCbLO & ACCbHI ( 16 bits )
;
;   Performance :
;               Program Memory  :       14
;               Clock Cycles    :       17
;*******************************************************************;
;
*/

char ACCaLO;  //equ     10   changed equ statements to C char variables
char ACCaHI;  //equ     11
char ACCbLO;  //equ     12
char ACCbHI;  //equ     13
;

#asm            /* start of in-line assembly code */

;        include "mpreg.h"     commented out these
;        org     0             two lines (MPASM specific)

;*******************************************************************
;         Double Precision Subtraction ( ACCb - ACCa -> ACCb )
;
D_sub   call    neg_A2           ; At first negate ACCa; Then add
;
;*******************************************************************
;       Double Precision  Addition ( ACCb + ACCa -> ACCb )
;
D_add   movf    ACCaLO,W
	addwf   ACCbLO          ;add lsb
	btfsc   STATUS,C        ;add in carry
	incf    ACCbHI
	movf    ACCaHI,C
	addwf   ACCbHI          ;add msb
	retlw   0

neg_A2  comf    ACCaLO          ; negate ACCa ( -ACCa -> ACCa )
	incf    ACCaLO
	btfsc   STATUS,Z
	decf    ACCaHI
	comf    ACCaHI
	retlw   0


;*******************************************************************
;                       Double Precision Multiplication
;
;               ( Optimized for Speed : straight Line Code )
;
;*******************************************************************;
;   Multiplication : ACCb(16 bits) * ACCa(16 bits) -> ACCb,ACCc ( 32 bits )
;      (a) Load the 1st operand in location ACCaLO & ACCaHI ( 16 bits )
;      (b) Load the 2nd operand in location ACCbLO & ACCbHI ( 16 bits )
;      (c) CALL D_mpy
;      (d) The 32 bit result is in location ( ACCbHI,ACCbLO,ACCcHI,ACCcLO )
;
;   Performance :
;               Program Memory  :       240
;               Clock Cycles    :       233
;
;       Note : The above timing is the worst case timing, when the
;               register ACCb = FFFF. The speed may be improved if
;               the register ACCb contains a number ( out of the two
;               numbers ) with less number of 1s.
;
;               The performance specs are for Unsigned arithmetic ( i.e,
;               with "SIGNED equ  FALSE ").

;*******************************************************************;
;

#endasm
//char ACCaLO;  equ     10   Commented out - already defined in Dbl_add
//char ACCaHI;  equ     11
//char ACCbLO;  equ     12
//char ACCbHI;  equ     13
char ACCcLO;  //equ     14   changed equ statements to C char variables 
char ACCcHI;  //equ     15
char ACCdLO;  //equ     16
char ACCdHI;  //equ     17
char temp;    //equ     18
char sign;    //equ     19

#asm
;
;        include "mpreg.h"       commented out these 
;        org     0               two lines (MPASM specific)
;*******************************************************************
SIGNED  equ     1               ; Set This To 'TRUE' if the routines
;                               ; for Multiplication & Division needs
;                               ; to be assembled as Signed Integer
;                               ; Routines. If 'FALSE' the above two
;                               ; routines ( D_mpy & D_div ) use
;                               ; unsigned arithmetic.
;*******************************************************************
;       multiplication macro
;
 .MACRO mulMac                  ; changed macro to conform to MPC macro
;        LOCAL   NO_ADD         ; language - declaration is different
;                               ; and macro labels are preceded by "/"

	rrf     ACCdHI          ; rotate d right
	rrf     ACCdLO
	btfss   STATUS,C        ; need to add?
	goto    \NO_ADD         ; no addition necessary
	movf    ACCaLO,W        ; Addition ( ACCb + ACCa -> ACCb )
	addwf   ACCbLO          ; add lsb
	btfsc   STATUS,C        ; add in carry
	incf    ACCbHI
	movf    ACCaHI,W
	addwf   ACCbHI          ;add msb
\NO_ADD rrf     ACCbHI
	rrf     ACCbLO
	rrf     ACCcHI
	rrf     ACCcLO
;
	.ENDM                   ; end of modified macro
;
;*******************************************************************;
;               Double Precision Multiply ( 16x16 -> 32 )
;         ( ACCb*ACCa -> ACCb,ACCc ) : 32 bit output with high word
;  in ACCb ( ACCbHI,ACCbLO ) and low word in ACCc ( ACCcHI,ACCcLO ).
;
D_mpyF                          ;results in ACCb(16 msb's) and ACCc(16 lsb's)
;
     .IF   SIGNED
     CALL    S_SIGN
     .ENDIF
;
	call    setup
;
; use the mulMac macro 16 times
;
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
	mulMac
;
    .IF    SIGNED
	btfss   sign,7
	retlw   0
	comf    ACCcLO          ; negate ACCa ( -ACCa -> ACCa )
	incf    ACCcLO
	btfsc   STATUS,Z
	decf    ACCcHI
	comf    ACCcHI
	btfsc   STATUS,Z
neg_B   comf    ACCbLO          ; negate ACCb
	incf    ACCbLO
	btfsc   STATUS,Z
	decf    ACCbHI
	comf    ACCbHI
	retlw   0
    .ELSE
	retlw   0
    .ENDIF
;
;*******************************************************************
;
setup   movlw   16             ; for 16 shifts
	movwf   temp
	movf    ACCbHI,W          ;move ACCb to ACCd
	movwf   ACCdHI
	movf    ACCbLO,W
	movwf   ACCdLO
	clrf    ACCbHI
	clrf    ACCbLO
	retlw   0
;
;*******************************************************************
;
neg_A   comf    ACCaLO          ; negate ACCa ( -ACCa -> ACCa )
	incf    ACCaLO
	btfsc   STATUS,Z
	decf    ACCaHI
	comf    ACCaHI
	retlw   0
;
;*******************************************************************
;  Assemble this section only if Signed Arithmetic Needed
;
     .IF    SIGNED
;
S_SIGN  movf    ACCaHI,W
	xorwf   ACCbHI,W
	movwf   sign
	btfss   ACCbHI,7        ; if MSB set go & negate ACCb
	goto    chek_A
;
	comf    ACCbLO          ; negate ACCb
	incf    ACCbLO
	btfsc   STATUS,Z
	decf    ACCbHI
	comf    ACCbHI
;
chek_A  btfss   ACCaHI,7        ; if MSB set go & negate ACCa
	retlw   0
	goto    neg_A
;
     .ENDIF

#endasm

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -