#include "OB90A64M3.h"
#include "mdu.h"


MDU_RESULT_INFO MDU_16_DIV_16(OB_MDU_Type* pMDU, uint32_t nSign, uint32_t nNumerator, uint32_t nDenominator)
{
    MDU_RESULT_INFO mdu_result;
    pMDU->ARCON = 0x00;
    pMDU->MD0   = (uint16_t)nNumerator;
    pMDU->MD2   = (uint16_t)nDenominator;

    if (nSign == 0x01)
        pMDU->ARCON = pMDU->ARCON | MDU_ARCON_SIGNE;

    pMDU->ARCON = pMDU->ARCON | MDU_ARCON_ENABLE | MDU_ARCON_16DIV16;
    while(1)
    {
        if ((pMDU->ARCON & MDU_ARCON_ENABLE) != MDU_ARCON_ENABLE)
            break;
    }
    mdu_result.nResultH = pMDU->MD0 ;  //quotient
    mdu_result.nResultL = pMDU->MD2 ;  //remainder
    return mdu_result;
}

MDU_RESULT_INFO MDU_32_DIV_16(OB_MDU_Type* pMDU, uint32_t nSign, uint32_t nNumerator, uint32_t nDenominator)
{
    MDU_RESULT_INFO mdu_result;
    pMDU->ARCON = 0x00;
    pMDU->MD0   = nNumerator;
    pMDU->MD2   = (uint16_t)nDenominator;

    if (nSign == 0x01)
        pMDU->ARCON = pMDU->ARCON | MDU_ARCON_SIGNE;

    pMDU->ARCON = pMDU->ARCON | MDU_ARCON_ENABLE | MDU_ARCON_32DIV16;
    while(1)
    {
        if ((pMDU->ARCON & MDU_ARCON_ENABLE) != MDU_ARCON_ENABLE)
            break;
    }
    mdu_result.nResultH = pMDU->MD0 ;  //quotient
    mdu_result.nResultL = pMDU->MD2 ;  //remainder
    return mdu_result;
}

MDU_RESULT_INFO MDU_32_DIV_32(OB_MDU_Type* pMDU, uint32_t nSign, uint32_t nNumerator, uint32_t nDenominator)
{
    MDU_RESULT_INFO mdu_result;
    pMDU->ARCON = 0x00;
    pMDU->MD0   = nNumerator;
    pMDU->MD2   = nDenominator;

    if (nSign == 0x01)
        pMDU->ARCON = pMDU->ARCON | MDU_ARCON_SIGNE;

    pMDU->ARCON = pMDU->ARCON | MDU_ARCON_ENABLE | MDU_ARCON_32DIV32;
    while(1)
    {
        if ((pMDU->ARCON & MDU_ARCON_ENABLE) != MDU_ARCON_ENABLE)
            break;
    }
    mdu_result.nResultH = pMDU->MD0 ;  //quotient
    mdu_result.nResultL = pMDU->MD2 ;  //remainder
    return mdu_result;
}

MDU_RESULT_INFO MDU_32_MUL_32(OB_MDU_Type* pMDU, uint32_t nSign, uint32_t nNumerator, uint32_t nDenominator)
{
    MDU_RESULT_INFO mdu_result;

    pMDU->ARCON = 0x00;
    pMDU->MD0   = nNumerator;
    pMDU->MD2   = nDenominator;

    if (nSign == 0x01)
        pMDU->ARCON = pMDU->ARCON | MDU_ARCON_SIGNE;

    pMDU->ARCON = pMDU->ARCON | MDU_ARCON_ENABLE | MDU_ARCON_32MUL32;
    while(1)
    {
        if ((pMDU->ARCON & MDU_ARCON_ENABLE) != MDU_ARCON_ENABLE)
            break;
    }
    mdu_result.nResultH = pMDU->MD1 ;  //product HI byte
    mdu_result.nResultL = pMDU->MD0 ;  //product LOW byte
    return mdu_result;
}


