/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2023. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
 /******************************************************************************
* @version: V1.0.0 demo
******************************************************************************/
/*!
 * @file 02_MMDIVSQRT_DIV.c
 *
 * @brief This file is MMDIVSQRT div operate guide.
 *
 */

/* ===========================================  Includes  =========================================== */
#include "gpio.h"
#include "math.h"
#include "ac7803x_debugout.h"
#include "ac780x_mmdivsqrt_reg.h"
/* ============================================  Define  ============================================ */

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */
int32_t sign_quotientResult = 0;            /* signed quotient */
int32_t sign_remainderResult = 0;           /* signed remainder */
uint32_t unsign_quotientResult = 0;         /* unsigned quotient */
uint32_t unsign_remainderResult = 0;        /* unsigned remainder */

/* ====================================  Functions declaration  ===================================== */

/* ======================================  Functions define  ======================================== */

/*!
 * @brief Job_SignDivisionCal_Simple
 *       This function is test the speed of quotient&remainder calculate spped by MMDIVSQRT & Function 
 *
 * @param[in] none
 * @return none
 */
void Job_SignDivisionCal_Simple(void)
{
    int32_t sign_Dend_Simple = 9;            /* signed diviend */
    int32_t sing_Dsor_Simple = -4;           /* signed divisor */
    uint8_t dsft = 1;                        /* division shifter */

    /*  Calculate Quotient by MMDIVSQRT */
    GPIOA->ODR |= 0x800;        /* PA11 output high level */
    MMDIVSQRT_CalSignDivWithDsft(sign_Dend_Simple, sing_Dsor_Simple, dsft, &sign_quotientResult); 
    GPIOA->ODR &= 0x7FF;        /* PA11 output low level */
            
    udelay(5);                  /* Delay time */
            
    /*  Calculate Quotient by Function division */
    GPIOA->ODR |= 0x800;        /* PA11 output high level */
    sign_quotientResult = (sign_Dend_Simple<<dsft)/sing_Dsor_Simple; 
    GPIOA->ODR &= 0x7FF;        /* PA11 output low level */
            
    udelay(10);                  /* Delay Time */

    /* Calculate Remainder by MMDIVSQRT */
    MMDIVSQRT_SetDivisionRem(MMDIVSQRT_DIV_RESULT_REMAINDER);  /* Switching to Remainder */
    GPIOA->ODR |= 0x800;        /* PA11 output high level */
    MMDIVSQRT_CalSignDivWithDsft(sign_Dend_Simple, sing_Dsor_Simple, dsft, &sign_remainderResult); 
    GPIOA->ODR &= 0x7FF;        /* PA11 output low level */
    
    udelay(5);                  /* Delay Time */
    
    /* Calculate Remainder by Function remainder */
    GPIOA->ODR |= 0x800;        /* PA11 output high level */
    sign_remainderResult = (sign_Dend_Simple<<dsft)%sing_Dsor_Simple;  
    GPIOA->ODR &= 0x7FF;        /* PA11 output low level */
}

/*!
 * @brief Job_SignDivisionCal_Complex
 *        This function is test the speed of quotient&remainder calculate spped by MMDIVSQRT & Function 
 *
 * @param[in] none
 * @return EFLASH STATUS: eflash operation status
 */
void Job_SignDivisionCal_Complex(void)
{
    int32_t sign_Dend_Complex = 88777;          /* signed diviend */
    int32_t sing_Dsor_Complex = -7;             /* signed divisor */
    uint8_t dsft = 10;                          /* division shifter */

    
    /*  Calculate Quotient by MMDIVSQRT */
    GPIOA->ODR |= 0x400;        /* PA10 output high level */
    MMDIVSQRT_CalSignDivWithDsft(sign_Dend_Complex, sing_Dsor_Complex, dsft, &sign_quotientResult); 
    GPIOA->ODR &= 0xBFF;        /* PA10 output low level */
            
    udelay(5);                  /* Delay time */
            
    /*  Calculate Quotient by Function division */
    GPIOA->ODR |= 0x400;        /* PA10 output high level */
    sign_quotientResult = (sign_Dend_Complex<<dsft)/sing_Dsor_Complex; 
    GPIOA->ODR &= 0xBFF;        /* PA10 output low level */
            
    udelay(10);                  /* Delay Time */
    
    MMDIVSQRT_SetDivisionRem(MMDIVSQRT_DIV_RESULT_REMAINDER);   /* Switching to Remainder */
    GPIOA->ODR |= 0x400;        /* PA10 output high level */
    MMDIVSQRT_CalSignDivWithDsft(sign_Dend_Complex, sing_Dsor_Complex, dsft, &sign_remainderResult); 
    GPIOA->ODR &= 0xBFF;        /* PA10 output low level */
    
    udelay(5);                  /* Delay Time */
    
    GPIOA->ODR |= 0x400;        /* PA10 output high level */
    sign_remainderResult = (sign_Dend_Complex<<dsft)%sing_Dsor_Complex;  
    GPIOA->ODR &= 0xBFF;        /* PA10 output low level */
}

/*!
 * @brief Job_UnsignDivisionCal_Simple
 *        This function is test the speed of quotient&remainder calculate spped by MMDIVSQRT & Function 
 *
 * @param[in] none
 * @return EFLASH STATUS: eflash operation status
 */
void Job_UnsignDivisionCal_Simple(void)
{
    uint32_t unsign_Dend_Simple = 600;       /* Unsigned dividend */
    uint32_t unsign_Dsor_simple = 500;       /* unsigned dividor */
    uint8_t dsft = 1;          /* division shifter */

    
    /*  Calculate Quotient by MMDIVSQRT */
    GPIOA->ODR |= 0x800;        /* PA11 output high level */
    MMDIVSQRT_CalSignDivWithDsft(unsign_Dend_Simple, unsign_Dsor_simple, dsft, &sign_quotientResult); 
    GPIOA->ODR &= 0x7FF;        /* PA11 output low level */
            
    udelay(5);                  /* Delay time */
            
    /*  Calculate Quotient by Function division */
    GPIOA->ODR |= 0x800;        /* PA11 output high level */
    sign_quotientResult = (unsign_Dend_Simple<<dsft)/unsign_Dsor_simple; 
    GPIOA->ODR &= 0x7FF;        /* PA11 output low level */
            
    udelay(10);                  /* Delay Time */


    MMDIVSQRT_SetDivisionRem(MMDIVSQRT_DIV_RESULT_REMAINDER);  /* Switching to Remainder */
    GPIOA->ODR |= 0x800;        /* PA11 output high level */
    MMDIVSQRT_CalSignDivWithDsft(unsign_Dend_Simple, unsign_Dsor_simple, dsft, &sign_remainderResult); 
    GPIOA->ODR &= 0x7FF;        /* PA11 output low level */
    
    udelay(5);                  /* Delay Time */
    
    GPIOA->ODR |= 0x800;        /* PA11 output high level */
    sign_remainderResult = (unsign_Dend_Simple<<dsft)%unsign_Dsor_simple;  
    GPIOA->ODR &= 0x7FF;        /* PA11 output low level */
}



/*!
 * @brief Job_UnsignDivisionCal_Complex
 *        This function is test the speed of quotient&remainder calculate spped by MMDIVSQRT & Function  
 *
 * @param[in] none
 * @return EFLASH STATUS: eflash operation status
 */
void Job_UnsignDivisionCal_Complex(void)
{
    uint32_t unsign_Dend_Complex = 88777;        /* Unsigned dividend */
    uint32_t unsign_Dsor_Complex = 8;            /* unsigned dividor */
    uint8_t dsft = 10;                           /* division shifter */

    
    /*  Calculate Quotient by MMDIVSQRT */
    GPIOA->ODR |= 0x400;        /* PA10 output high level */
    MMDIVSQRT_CalSignDivWithDsft(unsign_Dend_Complex, unsign_Dsor_Complex, dsft, &sign_quotientResult); 
    GPIOA->ODR &= 0xBFF;        /* PA10 output low level */
            
    udelay(5);                  /* Delay time */
            
    /*  Calculate Quotient by Function division */
    GPIOA->ODR |= 0x400;        /* PA10 output high level */
    sign_quotientResult = (unsign_Dend_Complex<<dsft)/unsign_Dsor_Complex; 
    GPIOA->ODR &= 0xBFF;        /* PA10 output low level */
            
    udelay(10);                 /* Delay Time */

    MMDIVSQRT_SetDivisionRem(MMDIVSQRT_DIV_RESULT_REMAINDER);  /* Switching to Remainder */
    GPIOA->ODR |= 0x400;        /* PA10 output high level */
    MMDIVSQRT_CalSignDivWithDsft(unsign_Dend_Complex, unsign_Dsor_Complex, dsft, &sign_remainderResult); 
    GPIOA->ODR &= 0xBFF;        /* PA10 output low level */
    
    udelay(5);                  /* Delay Time */
    
    GPIOA->ODR |= 0x400;        /* PA10 output high level */
    sign_remainderResult = (unsign_Dend_Complex<<dsft)%unsign_Dsor_Complex;  
    GPIOA->ODR &= 0xBFF;        /* PA10 output low level */
}




/* =============================================  EOF  ============================================== */
