//****************************************************************************
// @Module        FOC
// @Filename      FOC_SUB.C
// @Controller    XC16x Series
//
//
// @Description   This file contains FOC subroutines
//
//****************************************************************************

//****************************************************************************
// @Project Includes
//****************************************************************************

#include "CONFIG.H"
#include "VECTOR_TAB.H"

//****************************************************************************
// @Macros
//****************************************************************************

//****************************************************************************
// @Defines
//****************************************************************************

#define TIME_INT        512

//****************************************************************************
// @Typedefs
//****************************************************************************

//****************************************************************************
// @Imported Global Variables
//****************************************************************************


//****************************************************************************
// @Global Variables
//****************************************************************************

#if DAVECG_KEIL
extern int sdata g_speed_ref_max;
#elif DAVECG_TASKING
extern int system g_speed_ref_max;
#endif


//****************************************************************************
// @External Prototypes
//****************************************************************************

//****************************************************************************
// @Prototypes Of Local Functions
//****************************************************************************

int clarke_trans_co(int I_phase_s, int I_phase_t , int *ia);
int park_trans_co(int e_a,int e_b,int phi,int *a_b);
int cart_polar_conv_co(int real,int imaginar,int *phase);
int P_controller_co(long *x,int Kp);
long generator_co(long *x,long y);

#if DAVECG_SINUS_SPEED_CTRL
int mul3_exp_co(int x,int y, int z, int n);
int decoupling_co(int ein_a0,int ein_b0,int ein_c,int *ein_d,int *aus_b);
#elif DAVECG_SINUS_SPEED_FW_CTRL
int mul3_exp_co(int x,int y, int z, int n);
int decoupling_co(int ein_a0,int ein_b0,int ein_c,int *ein_d,int *aus_b);
#endif

//****************************************************************************
// @Function      int cart_polar_conv_co(int real,int imaginaer,int *phase) 
//
//----------------------------------------------------------------------------
// @Description   Cartesian -> Polar Transformation
//
//----------------------------------------------------------------------------
// @Returnvalue   amplitude, angle
//
//
//****************************************************************************

int cart_polar_conv_co(int real,int imaginar,int *phase)
{
#if DAVECG_KEIL
int a;

#pragma asm
    mov     R2,R8                  
    jmpr    cc_nn,kpw1co                             
    neg     R2                             
kpw1co:                                                           
    mov     R3,R9                  
    jmpr    cc_nn,kpw2co                     
    neg     R3                             
kpw2co:                                   
    mov     R7,#1024
    cmp     R2,R3                  
    jmpr    cc_c,kpw3co
    mulu    R3,R7                  
    divlu   R2                     
    mov     R7,mdl                         
    shl     R7,#1                                                          
        EXTS    #SEG (betab),#1                                                                                 
        mov     R5,[R7+#SOF(betab)]    
    CoMULu  R2,R5                                          
    CoSHL   #1                     
    CoSTORE R4,MAS                 

        EXTS    #SEG (phitab),#1                                                        
    mov     R5,[R7+#SOF(phitab)]   
    cmp     R8,#0                  
    jmpr    cc_n,kpw4co              
    cmp     R9,#0                  
    jmpr    cc_n,kpw5co              
    mov     [R10],R5               
    ret                                    
kpw5co:                                                                           
    neg     R5                     
    mov     [R10],R5                                       
    ret                                                            
kpw4co:                                                                   
    cmp     R9,#0                  
    jmpr    cc_n,kpw6co                                      
    mov     R7,#8000h              
    sub     R7,R5                                                  
    mov     [R10],R7                                               
    ret                                                    
kpw6co:                                                                           
    add     R5,#8000h                                              
    mov     [R10],R5                                       
    ret                                                            
kpw3co:                                                                   
    mulu    R2,R7                                                     
    divlu   R3                     
    mov     R7,mdl                                 
    shl     R7,#1                                                  
                                                        
        EXTS    #SEG (betab),#1                                         
        mov     R5,[R7+#SOF(betab)]    
    CoMULu  R3,R5                                          
    CoSHL   #1                     
    CoSTORE R4,MAS                 
                                        
        EXTS    #SEG (phitab),#1                                                                
        mov     R5,[R7+#SOF(phitab)]   
        cmp     R8,#0                  
    jmpr    cc_n,kpw7co                              
    cmp     R9,#0                  
    jmpr    cc_n,kpw8co
    mov     R7,#4000h              
    sub     R7,R5                          
    mov     [R10],R7                               
    ret                                                            
kpw8co:                                                   
    add     R5,#0C000h             
    mov     [R10],R5                       
    ret                                    
kpw7co:                                                                   
    cmp     R9,#0                  
    jmpr    cc_n,kpw9co                              
    add     R5,#4000h              
    mov     [R10],R5                                                                       
    ret                                                    
kpw9co:                                                   
    mov     R7,#0C000h             
    sub     R7,R5                                  
    mov     [R10],R7                               
                                                                
#pragma endasm                                                  
                                                                                
a = real;
a = imaginar;
a = (int)*phase;

#elif DAVECG_TASKING
int retvalue;

#pragma asm(@w1 = real,@w2 = imaginar,@w3 = phase, @4)

    mov     R2,@w1                  
    jmpr    cc_nn,kpw1co                             
    neg     R2                             
kpw1co:                                                           
    mov     R3,@w2                  
    jmpr    cc_nn,kpw2co                     
    neg     R3                             
kpw2co:                                   
    mov     R7,#1024

;   CoLOAD  R7,@w1      ;;;
;   CoABS               ;;;
;   CoSTORE R2,MAS      ;;;           
;   CoLOAD  R7,@w2      ;;;
;   CoABS               ;;;
;   CoSTORE R3,MAS      ;;;           
    
    cmp     R2,R3                  
    jmpr    cc_c,kpw3co
    mulu    R3,R7                  
    divlu   R2                     
    mov     R7,mdl                         
    shl     R7,#1                                                          
    EXTS    #SEG (_betab),#1                                                                                 
    mov     R5,[R7+#SOF(_betab)]    
    CoMULu  R2,R5                                          
    CoSHL   #1                     
    CoSTORE @4,MAS                 

    EXTS    #SEG (_phitab),#1                                                        
    mov     R5,[R7+#SOF(_phitab)]   
    cmp     @w1,#0                  
    jmpr    cc_n,kpw4co              
    cmp     @w2,#0                  
    jmpr    cc_n,kpw5co              
    mov     [@w3],R5               
    mov     R4,@4                       
    rets                                    
kpw5co:                                                                           
    neg     R5                     
    mov     [@w3],R5                                       
    mov     R4,@4                       
    rets                                                            
kpw4co:                                                                   
    cmp     @w2,#0                  
    jmpr    cc_n,kpw6co                                      
    mov     R7,#8000h              
    sub     R7,R5                                                  
    mov     [@w3],R7                                               
    mov     R4,@4                       
    rets                                                    
kpw6co:                                                                           
    add     R5,#8000h                                              
    mov     [@w3],R5                                       
    mov     R4,@4                       
    rets                                                            
kpw3co:                                                                   
    mulu    R2,R7                                                     
    divlu   R3                     
    mov     R7,mdl                                 
    shl     R7,#1                                                  
                                                        
    EXTS    #SEG (_betab),#1                                         
    mov     R5,[R7+#SOF(_betab)]    
    CoMULu  R3,R5                                          
    CoSHL   #1                     
    CoSTORE @4,MAS                 
                                        
    EXTS    #SEG (_phitab),#1                                                                
    mov     R5,[R7+#SOF(_phitab)]   
    cmp     @w1,#0                  
    jmpr    cc_n,kpw7co                              
    cmp     @w2,#0                  
    jmpr    cc_n,kpw8co
    mov     R7,#4000h              
    sub     R7,R5                          
    mov     [@w3],R7                               
    mov     R4,@4                       
    rets                                                            
kpw8co:                                                   
    add     R5,#0C000h             
    mov     [@w3],R5                       
    mov     R4,@4                       
    rets                                    
kpw7co:                                                                   
    cmp     @w2,#0                  
    jmpr    cc_n,kpw9co                              
    add     R5,#4000h              
    mov     [@w3],R5                                                                       
    mov     R4,@4                       
    rets                                                    
kpw9co:                                                   
    mov     R7,#0C000h             
    sub     R7,R5                                  
    mov     [@w3],R7                               
                                                                
#pragma endasm(retvalue = @4 )                                                  
                                                                                
return retvalue;

#endif
}

//*************************************************************************

//****************************************************************************
// @Function   int clarke_trans_co(int I_phase_s, int I_phase_t , int *ia)
//
//----------------------------------------------------------------------------
// @Description     ia = is
//                  ib = 1/sqrt(3)( (2 * it) + is)
//
//----------------------------------------------------------------------------
// @Returnvalue     (ib * 4) ,"(ia * 4)" 
//
//
//****************************************************************************
#if DAVECG_KEIL
int clarke_trans_co(int I_phase_s, int I_phase_t , int *ia)
{
int a;
#pragma asm
    shl     R9,#1
    add     R9,R8           
    mov     R7,#37837       
    CoMULsu R9,R7
    CoSHL   #4
    CoSTORE R4,MAS
    mov     R7,#0       
    CoLOAD  R7,R8
    CoSHL   #4
    CoSTORE [R10],MAS
#pragma endasm

a = I_phase_s;
a = I_phase_t;

a = (int)*ia;

}
#elif   DAVECG_TASKING
int clarke_trans_co(int Phase_s, int Phase_t, int *ia)
{
int retvalue;

#pragma asm(@w1 = Phase_s,@w2 = Phase_t,@w3 = ia, @4)

    shl     @w2,#1
    add     @w2,@w1           
    mov     R7,#37837       
    CoMULsu @w2,R7
    CoSHL   #4
    CoSTORE @4,MAS
    mov     R7,#0       
    CoLOAD  R7,@w1
    CoSHL   #4
    CoSTORE [@w3],MAS

#pragma endasm(retvalue = @4 )                                                  
                                                                                
return retvalue;

}

#endif
//****************************************************************************
// @Function   int park_trans_co(int e_a,int e_b,int phi,int *a_b)
//
//----------------------------------------------------------------------------
// @Description  park transformation 
//
//----------------------------------------------------------------------------
// @Returnvalue   Id_comp , Iq_comp
//
//
//****************************************************************************

int park_trans_co(int e_a,int e_b,int phi,int *a_b)
{  
#if DAVECG_KEIL
int b;

#pragma asm
    mov     R7,MCW               
    mov     MCW,#1536               
        shr     R10,#6
        shl     R10,#1
        EXTS    #SEG (sincostab),#2
        mov     R3,[R10 + #SOF(sincostab+512)]
        mov     R2,[R10 + #SOF(sincostab)]
        CoMUL   R9,R2
        CoMAC   R8,R3
        CoSTORE R4,MAS 
        CoMUL   R9,R3
        CoMAC-  R8,R2
        CoSTORE [R11],MAS 
    mov     MCW,R7               
#pragma endasm

b = e_a;
b = e_b;
b = phi;
b = (int)*a_b;

#elif DAVECG_TASKING
int retvalue;

#pragma asm(@w1 = e_a,@w2 = e_b,@w3 = phi, @w4 = a_b , @5)

    mov     R7,MCW               
    mov     MCW,#1536               
    shr     @w3,#6
    shl     @w3,#1
    EXTS    #SEG (_sincostab),#2
    mov     R3,[@w3 + #SOF(_sincostab+512)]
    mov     R2,[@w3 + #SOF(_sincostab)]
    CoMUL   @w2,R2
    CoMAC   @w1,R3
    CoSTORE @5,MAS 
    CoMUL   @w2,R3
    CoMAC-  @w1,R2
    CoSTORE [@w4],MAS 
    mov     MCW,R7               

#pragma endasm(retvalue = @5 )                                                  

return retvalue;

#endif
}

//****************************************************************************
// @Function   
//
//----------------------------------------------------------------------------
// @Description  
//                  
//
//----------------------------------------------------------------------------
// @Returnvalue   
//
//
//****************************************************************************
#if DAVECG_SINUS_POSITION_CTRL

int P_controller_co(long *x,int Kp)
{
#if DAVECG_KEIL

int a;
long b;
#pragma asm
EXTERN DPP3:g_speed_ref_max:WORD 

    mov     R12,MCW               
    mov     MCW,#1536               
//////////////////////////////////////////////////////////////
        mov     R5 ,[R8+]                                  // reference
        CoLOAD  R5 ,[R8+]
        mov     R6 ,[R8+]                                  // actual  
        CoSUB   R6 ,[R8]                                   // (reference - actual)
////////////////////////////////////////////////////////////////
                                                           //  (reference - actual) * Kp 

        CoSTORE R8,MAL 
        CoSTORE R5,MAS 
        CoSTORE R6,MAS 
        shr     R8,#1
        and     R6,#8000h 
        or      R8,R6
        shr     R6,#15
        add     R5,R6
        CoMUL   R8,R9
        CoASHR  #7              
        CoSTORE R7,MAL 
        CoSTORE R8,MAS 
        CoMUL   R9,R5
        CoSHL   #8              
        CoADD   R7,R8
        CoASHR  #6              

        mov     R6 ,g_speed_ref_max   
        mov     R7 ,ZEROS   
        sub     R7 ,g_speed_ref_max   
        mov     R11,ONES   
        CoMAX   R7 ,R11 
        mov     R11,ZEROS   
        CoMIN   R6 ,R11 

/////////////////////////////////////////////////////////
        CoSTORE R4 ,MAL  
    mov     MCW,R12               
#pragma endasm

b = *x;
a = Kp;

#elif DAVECG_TASKING
int retvalue;

#pragma asm(@w1 = x, @w2 = Kp ,@3)
	EXTERN DPP3:_g_speed_ref_max:WORD

    mov     R8,MCW               
    mov     MCW,#1536               
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    mov     R5 ,[@w1+]                                     ; load reference
    CoLOAD  R5 ,[@w1+]
    mov     R6 ,[@w1+]                                     ; load actual value  
    CoSUB   R6 ,[@w1]                                      ; (reference - actual)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  (reference - actual) * Kp 

    CoSTORE R8,MAL 
    CoSTORE R5,MAS 
    CoSTORE R6,MAS 
    shr     R8,#1
    and     R6,#8000h 
    or      R8,R6
    shr     R6,#15
    add     R5,R6
    CoMUL   R8,@w2
    CoASHR  #7              
    CoSTORE R7,MAL 
    CoSTORE R8,MAS 
    CoMUL   R5,@w2
    CoSHL   #8              
    CoADD   R7,R8
    CoASHR  #6              

    mov     R6 ,_g_speed_ref_max      
    mov     R7 ,ZEROS   
    sub     R7 ,_g_speed_ref_max     
    mov     R9,ONES   
    CoMAX   R7 ,R9                                         ;Output limit_min
    mov     R9,ZEROS   
    CoMIN   R6 ,R9                                         ;Output limit_max

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    CoSTORE @3 ,MAL                                        ;return 
    mov     MCW,R8               
#pragma endasm(retvalue = @3)

return retvalue;

#endif
}   

//****************************************************************************
// @Function   long generator_co(long *x,long y)
//
//----------------------------------------------------------------------------
// @Description  
//                  
//
//----------------------------------------------------------------------------
// @Returnvalue   
//
//
//****************************************************************************

long generator_co(long *x,long y)
{
#if DAVECG_KEIL

long a;
#pragma asm

    mov     R12,MCW               
    mov     MCW,#1536               
        mov     R3 ,R8
        mov     R11,#TIME_INT
        mov     R5 ,[R3+]                                  // Integ 2 
        CoLOAD  R5 ,[R3+]
        mov     R6 ,[R3+]                                  // Integ 1   
        CoADD2  R6 ,[R3-]                                  // Integ 2 + ( integ 1 * 2)
        CoNEG                                              // -(Integ 2 + ( integ 1 * 2))     
        CoADD   R9 ,R10                                    // reference - (Integ 2 + ( integ 1 * 2))
        mov     R5 ,#94                                    // reference + 94 
        mov     R6 ,ZEROS                                  //  
        CoADD   R5 ,R6      


        CoSTORE R8 ,MAL 
        CoSTORE R9 ,MAS 
        CoSTORE R6 ,MAS 
        shr     R8 ,#1
        and     R6 ,#32768 
        or      R8 ,R6
        shr     R6 ,#15
        add     R9 ,R6
        CoMUL   R8 ,R11 
        CoASHR  #15
        CoSTORE R7 ,MAL 
        CoSTORE R8 ,MAS 
        CoMUL   R11,R9
        CoADD   R7 ,R8

        mov     R6 ,[R3+]   
        CoADD   R6 ,[R3]
        CoSTORE [R3-],MAS 
        CoSTORE [R3-],MAL 

        CoSTORE R8 ,MAL 
        CoSTORE R9 ,MAS 
        CoSTORE R6 ,MAS 
        shr     R8 ,#1
        and     R6 ,#32768 
        or      R8 ,R6
        shr     R6 ,#15
        add     R9 ,R6
        CoMUL   R8 ,R11
        CoASHR  #15
        CoSTORE R7 ,MAL 
        CoSTORE R8 ,MAS 
        CoMUL   R11,R9
        CoADD   R7,R8

        sub     R3 ,#2 
        mov     R7 ,[R3+]   
        CoADD   R7 ,[R3-]
        CoSTORE [R3+],MAL 
        CoSTORE [R3+],MAS 
        CoSTORE R4,MAL 
        CoSTORE R5,MAS 
    mov     MCW,R12               

#pragma endasm

a = *x;
a = y;

#elif DAVECG_TASKING
long a;

#pragma asm(@w1 = x,@w2 = y)

TIME_INTEGRAL   EQU   512

    mov     R10,MCW               
    mov     MCW,#1536               
    mov     R11,#TIME_INTEGRAL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    mov     R5 ,[@w1+]                                     ;; Integ 2 
    CoLOAD  R5 ,[@w1+]
    mov     R6 ,[@w1+]                                     ;; Integ 1   
    CoADD2  R6 ,[@w1-]                                     ;; Integ 2 + ( integ 1 * 2)
    CoNEG                                                  ;; -(Integ 2 + ( integ 1 * 2))     
    CoADD   R14 ,R15                                       ;; reference - (Integ 2 + ( integ 1 * 2))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    mov     R5 ,#94                                        ;; reference + 94 
    mov     R6 ,ZEROS                                      ;;  
    CoADD   R5 ,R6      
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;/
;   Integ 1   

    CoSTORE R8 ,MAL 
    CoSTORE R9 ,MAS 
    CoSTORE R6 ,MAS 
    shr     R8 ,#1
    and     R6 ,#32768 
    or      R8 ,R6
    shr     R6 ,#15
    add     R9 ,R6
    CoMUL   R8 ,R11 
    CoASHR  #15
    CoSTORE R7 ,MAL 
    CoSTORE R8 ,MAS 
    CoMUL   R11,R9
    CoADD   R7 ,R8

    mov     R6 ,[@w1+]   
    CoADD   R6 ,[@w1]
    CoSTORE [@w1-],MAS 
    CoSTORE [@w1-],MAL 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; integ 2

    CoSTORE R8 ,MAL 
    CoSTORE R9 ,MAS 
    CoSTORE R6 ,MAS 
    shr     R8 ,#1
    and     R6 ,#32768 
    or      R8 ,R6
    shr     R6 ,#15
    add     R9 ,R6
    CoMUL   R8 ,R11
    CoASHR  #15
    CoSTORE R7 ,MAL 
    CoSTORE R8 ,MAS 
    CoMUL   R11,R9
    CoADD   R7,R8
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    sub     @w1,#2 
    mov     R7 ,[@w1+]   
    CoADD   R7 ,[@w1-]
    CoSTORE [@w1+],MAL 
    CoSTORE [@w1+],MAS 
    CoSTORE R4,MAL 
    CoSTORE R5,MAS 
    mov     MCW,R10               
    rets
#pragma endasm

a = y;
return 0;

#endif
}

#endif
//****************************************************************************

#if DAVECG_SINUS_SPEED_CTRL
#if DAVECG_ENABLE_DECOUPLING

//****************************************************************************
// @Function   int mul3_exp_co(int x,int y, int z, int n)
//
//----------------------------------------------------------------------------
// @Description  mul3_exp  = x * y * z * / 2^(30 - n)  ->  n max = 8
//                  
//
//----------------------------------------------------------------------------
// @Returnvalue   
//
//----------------------------------------------------------------------------
// @Parameters    
//
//****************************************************************************

int mul3_exp_co(int x,int y, int z, int n)
{
#if DAVECG_TASKING

int retvalue;

#pragma asm(@w1 = x,@w2 = y,@w3 = z, @w4 = n , @5)
 
    mov     R8,MCW               
    mov     MCW,#1536               
    CoMUL   @w1,@w2
    CoSTORE R7,MAL 
    CoSTORE R6,MAS 
    CoSTORE @w2,MAS 
    shr     R7,#1
    and     R6,#32768 
    or      R7,R6
    shr     R6,#15
    add     @w2,R6
    CoMUL   R7,@w3
    mov     R5,#15
    sub     R5,@w4
    CoASHR  R5
    CoSTORE R7,MAL 
    CoSTORE @w1,MAS 
    CoMUL   @w3,@w2
    CoSHL   @w4
    CoADD   R7,@w1
    CoSTORE @5,MAS 
    mov     MCW,R8               

#pragma endasm(retvalue = @5 )                                                  

return retvalue;

#elif DAVECG_KEIL
int a;
#pragma asm

    mov     R12,MCW               
    mov     MCW,#1536               
        CoMUL   R8,R9
        CoSTORE R7,MAL 
        CoSTORE R6,MAS 
        CoSTORE R9,MAS 
        shr     R7,#1
        and     R6,#32768 
        or      R7,R6
        shr     R6,#15
        add     R9,R6
        CoMUL   R7,R10
        mov     R5,#15
        sub     R5,R11
        CoASHR  R5
        CoSTORE R7,MAL 
        CoSTORE R8,MAS 
        CoMUL   R10,R9
        CoSHL   R11
        CoADD   R7,R8
        CoSTORE R4,MAS 
    mov     MCW,R12               

#pragma endasm

a = x;
a = y;
a = z;
a = n;

#endif
}

//****************************************************************************
// @Function   int decoupling_co(int ein_a0,int ein_b0,int ein_c,int *ein_d,int *aus_b)
//
//----------------------------------------------------------------------------
// @Description   Decoupling network
//
//----------------------------------------------------------------------------
// @Returnvalue   
//
//----------------------------------------------------------------------------
// @Parameters    
//
//****************************************************************************

int decoupling_co(int ein_a0,int ein_b0,int ein_c,int *ein_d,int *aus_b)
{     
#if DAVECG_TASKING

int retvalue;
int b;
int c;

b = BEMF_FACTOR;
c = BEMF_EXP;

#pragma asm(@w1 = ein_a0, @w2 = ein_b0, @w3 = ein_c, @w4 = ein_d, @w5 = aus_b, @w6 = b, @w7 = c, @8)

        mov     R7,ZEROS
        mov     R6,@w6         
        CoMULsu @w3,R6
        CoSHL   @w7         
        CoADD   R7,@w2
        CoADD   R7,[@w4+]
        CoSTORE [@w5],MAS 
        CoLOAD  R7,@w1
        CoSUB   R7,[@w4]
#pragma endasm(retvalue = @8 )    

return retvalue;

#elif DAVECG_KEIL
int b;

#pragma asm
        mov     R7,ZEROS
        mov     R6,#BEMF_FACTOR         
        CoMULsu R10,R6
        CoSHL   #BEMF_EXP         
        CoADD   R7,R9
        CoADD   R7,[R11+]
        CoSTORE [R12],MAS 

        CoLOAD  R7,R8
        CoSUB   R7,[R11]
#pragma endasm

b = ein_a0;
b = ein_b0;
b = ein_c;
b = (int)*ein_d;
b = (int)*aus_b;
    
#endif
}

#endif
#endif 

//****************************************************************************

#if DAVECG_SINUS_SPEED_FW_CTRL
#if DAVECG_ENABLE_DECOUPLING

//****************************************************************************
// @Function   int mul3_exp_co(int x,int y, int z, int n)
//
//----------------------------------------------------------------------------
// @Description  mul3_exp  = x * y * z * / 2^(30 - n)  ->  n max = 8
//                  
//
//----------------------------------------------------------------------------
// @Returnvalue   
//
//----------------------------------------------------------------------------
// @Parameters    
//
//****************************************************************************

int mul3_exp_co(int x,int y, int z, int n)
{
#if DAVECG_TASKING
int retvalue;

#pragma asm(@w1 = x,@w2 = y,@w3 = z, @w4 = n , @5)
 
    mov     R8,MCW               
    mov     MCW,#1536               
    CoMUL   @w1,@w2
    CoSTORE R7,MAL 
    CoSTORE R6,MAS 
    CoSTORE @w2,MAS 
    shr     R7,#1
    and     R6,#32768 
    or      R7,R6
    shr     R6,#15
    add     @w2,R6
    CoMUL   R7,@w3
    mov     R5,#15
    sub     R5,@w4
    CoASHR  R5
    CoSTORE R7,MAL 
    CoSTORE @w1,MAS 
    CoMUL   @w3,@w2
    CoSHL   @w4
    CoADD   R7,@w1
    CoSTORE @5,MAS 
    mov     MCW,R8               

#pragma endasm(retvalue = @5 )                                                  

return retvalue;

#elif DAVECG_KEIL

int a;
#pragma asm

    mov     R12,MCW               
    mov     MCW,#1536               
        CoMUL   R8,R9
        CoSTORE R7,MAL 
        CoSTORE R6,MAS 
        CoSTORE R9,MAS 
        shr     R7,#1
        and     R6,#32768 
        or      R7,R6
        shr     R6,#15
        add     R9,R6
        CoMUL   R7,R10
        mov     R5,#15
        sub     R5,R11
        CoASHR  R5
        CoSTORE R7,MAL 
        CoSTORE R8,MAS 
        CoMUL   R10,R9
        CoSHL   R11
        CoADD   R7,R8
        CoSTORE R4,MAS 
    mov     MCW,R12               

#pragma endasm

a = x;
a = y;
a = z;
a = n;

#endif
}

//****************************************************************************
// @Function   int decoupling_co(int ein_a0,int ein_b0,int ein_c,int *ein_d,int *aus_b)
//
//----------------------------------------------------------------------------
// @Description   Decoupling network
//
//----------------------------------------------------------------------------
// @Returnvalue   
//
//----------------------------------------------------------------------------
// @Parameters    
//
//****************************************************************************

int decoupling_co(int ein_a0,int ein_b0,int ein_c,int *ein_d,int *aus_b)
{   
#if DAVECG_TASKING

int retvalue;
int b;
int c;

b = BEMF_FACTOR;
c = BEMF_EXP;

#pragma asm(@w1 = ein_a0, @w2 = ein_b0, @w3 = ein_c, @w4 = ein_d, @w5 = aus_b, @w6 = b, @w7 = c, @8)

        mov     R7,ZEROS
        mov     R6,@w6         
        CoMULsu @w3,R6
        CoSHL   @w7         
        CoADD   R7,@w2
        CoADD   R7,[@w4+]
        CoSTORE [@w5],MAS 
        CoLOAD  R7,@w1
        CoSUB   R7,[@w4]
#pragma endasm(retvalue = @8 )    

return retvalue;

#elif DAVECG_KEIL

int b;

#pragma asm
        mov     R7,ZEROS
        mov     R6,#BEMF_FACTOR         
        CoMULsu R10,R6
        CoSHL   #BEMF_EXP         
        CoADD   R7,R9
        CoADD   R7,[R11+]
        CoSTORE [R12],MAS 

        CoLOAD  R7,R8
        CoSUB   R7,[R11]
#pragma endasm

b = ein_a0;
b = ein_b0;
b = ein_c;
b = (int)*ein_d;
b = (int)*aus_b;

#endif
}

#endif
#endif 
//****************************************************************************
