#include "OB6710.h"
#include "MDU.h"
#include "MATH.h"
#define d_RESULT_ADDR 0xF0

#define d_RESULT1_ADDR 0xF8
#define d_RESULT2_ADDR 0xFC

extern void Delay (unsigned int nDelay);
unsigned long xdata g_Result     _at_ d_RESULT_ADDR;
unsigned long xdata g_Result1     _at_ d_RESULT1_ADDR;
unsigned long xdata g_Result2     _at_ d_RESULT2_ADDR;
extern	unsigned long u32Dividend;
void Delay (unsigned int nDelay)
{
   while(nDelay--);
}

void  MDU_16_MUL_16_32bit(unsigned int nNumerator,  unsigned int nDenominator, unsigned char nShift, unsigned char nShiftDir )
{
	unsigned char xdata *pData;
	g_Result = 0x00000000;
	MD0 = (unsigned char)(nNumerator >> 0);		
  MD1 = (unsigned char)(nNumerator >> 8);
  MD2 = (unsigned char)(nDenominator >> 0);	
  MD3 = (unsigned char)(nDenominator >> 8);
	MDUCON = nShiftDir;
	if(nShift == 0x00)
		ARCON = 0xc0| nShift;
	else
		ARCON = 0x60| nShift;
	pData = d_RESULT_ADDR;
	*(pData + 3) = MD0;
	*(pData + 2) = MD1;
	*(pData + 1) = MD2;
	*(pData + 0) = MD3;	

}

void  MDU_16_MUL_16_16bit(unsigned int nNumerator,  unsigned int nDenominator, unsigned char nShift, unsigned char nShiftDir )
{
	unsigned char xdata *pData;
	g_Result = 0x00000000;
	MD0 = (unsigned char)(nNumerator >> 0);		
  MD1 = (unsigned char)(nNumerator >> 8);
  MD2 = (unsigned char)(nDenominator >> 0);	
  MD3 = (unsigned char)(nDenominator >> 8);
	MDUCON = nShiftDir;
	if(nShift == 0x00)
		ARCON = 0xc0| nShift;
	else
		ARCON = 0x20| nShift;
	pData = d_RESULT_ADDR;
	*(pData + 3) = MD0;
	*(pData + 2) = MD1;
	*(pData + 1) = MD2;
	*(pData + 0) = MD3;	

}



void MDU_16Bit_Shift(unsigned int nData, unsigned char nShift, unsigned char nShiftDir)
{
	unsigned char xdata *pData;
	g_Result = 0x00000000;
	MD0 = (unsigned char)(nData >> 0);		
  MD1 = (unsigned char)(nData >> 8);
	MDUCON = nShiftDir;
	ARCON = 0x00| nShift;
	pData = d_RESULT_ADDR;
	*(pData + 3) = MD0;
	*(pData + 2) = MD1;

}


void MDU_32Bit_Shift(unsigned int nDataH,unsigned int nDataL, unsigned char nShift, unsigned char nShiftDir)
{
	unsigned char xdata *pData;
	g_Result = 0x00000000;
	MD0 = (unsigned char)(nDataL >> 0);		
  MD1 = (unsigned char)(nDataL >> 8);
	MD2 = (unsigned char)(nDataH >> 0);		
  MD3 = (unsigned char)(nDataH >> 8);	
	MDUCON = nShiftDir;
	ARCON = 0x40| nShift;
	pData = d_RESULT_ADDR;
	*(pData + 3) = MD0;
	*(pData + 2) = MD1;
	*(pData + 1) = MD2;
	*(pData + 0) = MD3;
}


unsigned long gTest;
void  MDU_32_DIV_16(unsigned int n16Divisor)
{
	unsigned char xdata *pData;
	unsigned char Loop;
  
	g_Result = 0x00000000;
	g_Result1 = 0x00000000;
	g_Result2 = 0x00000000;

	g_Result2 = u32Dividend;
  pData = d_RESULT2_ADDR;	
	MD0 = *(pData + 3);		
  MD1 = *(pData + 2);
  MD2 = *(pData + 1);
  MD3 = *(pData + 0);
  
	MD4 = (unsigned char)(n16Divisor >> 0);
  MD5 = (unsigned char)(n16Divisor >> 8);
	
	ARCON = 0x80;
	Loop= 100;
	while(Loop--)
	{
	  if((MDUCON & 0x01) == 0x00)
			break;
	}
	Delay(100);
	
	pData = d_RESULT_ADDR;
	*(pData + 3) = MD0;
	*(pData + 2) = MD1;
	*(pData + 1) = MD2;
	*(pData + 0) = MD3;	
	

	pData = d_RESULT1_ADDR;
	*(pData + 3) = MD4;
	*(pData + 2) = MD5;	
	*(pData + 1) = 0x00;
	*(pData + 0) = 0x00;	
}