#include "Page1.h"
#include "v_task.h"
#include "at_modem.h"
#include "PalmChip.h"

#ifdef HW_MODEM

#define MODEM_BUF_SIZE		20
#define MODEM_BUF_LENGTH	16
#define MODEM_BUF_DWORD		5

static PXCHAR _pReceiveCur;
static XCHAR _iReceiveLen;
static BOOLEAN _bHeadFound;
static PXCHAR _pStoreBuf;
static XCHAR _iLastHdlcCode;
static XCHAR _iModemSdramIndex;

volatile DCHAR Modem_iInputHead, Modem_iInputTail, Modem_iOutputHead, Modem_iOutputTail;

static CSHORT fcstab[256] = {
   0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
   0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
   0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
   0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
   0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
   0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
   0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
   0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
   0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
   0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
   0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
   0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
   0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
   0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
   0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
   0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
   0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
   0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
   0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
   0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
   0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
   0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
   0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
   0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
   0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
   0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
   0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
   0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
   0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
   0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
   0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
   0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
   };


USHORT pppfcs16(USHORT fcs, PXCHAR cp, USHORT len)
{
    while (len--)
	{
//        fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
        fcs = (P_HIBYTE(fcs)) ^ fcstab[(fcs ^ *cp++) & 0xff];
	}

    return (fcs);
/*
	DSHORT i, iVal;

	iVal = fcs;
	for (i = 0; i < len; i ++)
	{
		iVal = (P_HIBYTE(iVal)) ^ fcstab[(iVal ^ cp[i]) & 0xff];
	}
	return iVal;
*/
}

void ModemTcpipRun(USHORT iType)
{
	switch (iType)
	{
	case PPP_PROT_IP:
		// we don't handle broadcast packet
		if (IsBroadcastIP((PXCHAR)(IP_pReceiveHeader+IP_DST)))
		{
			break;
		}
		switch (IP_pReceiveHeader[IP_PROTO])
		{
		case IPT_UDP:
			UdpRun();
			break;

		case IPT_TCP:
			TcpRun();
			break;

		case IPT_ICMP:
			IcmpRun();
			break;
		}
		break;

	case PPP_PROT_LCP:
		LcpRun();
		break;

	case PPP_PROT_PAP:
		PapRun();
		break;

	case PPP_PROT_CHAP:
		ChapRun();
		break;

	case PPP_PROT_IPCP:
		IpcpRun();
		break;

	default:
		OtherProtRun();		//Handle the unacceptable protocol here
		break;
	}
}

void _SdramModemWrite(UCHAR iIndexLow, UCHAR iLen)
{
	X_VAL(Sdram_pDataCache, SDRAM_DATA_LENGTH) = iLen;
	SdramWrite(MODEM_FRAME_SDRAM_HI, iIndexLow, SRAM_DMA_HALF_DWORD);
}

UCHAR _SdramModemRead(UCHAR iIndexLow)
{
	SdramRead(MODEM_FRAME_SDRAM_HI, iIndexLow, SRAM_DMA_HALF_DWORD);
	return X_VAL(Sdram_pDataCache, SDRAM_DATA_LENGTH);
}

void _HdlcDecode()
{
	DCHAR iTmp, iHdlcGetProtocol, iHdlcIpPacket;
	XSHORT iDstLen;
	XSHORT iType;
	PXCHAR pSrc;
	PXCHAR pDst;

	iHdlcGetProtocol = 0;
	iHdlcIpPacket = 0;
	pSrc = (PXCHAR)Adapter_pReceivePacket;
	pDst = (PXCHAR)Adapter_pReceivePacket;
	iDstLen = 0;

	if (pSrc[0] != 0x7e)
	{
		return;
	}	
	pSrc ++;

	while(TRUE)
	{
		iTmp = pSrc[0];
		pSrc ++;
		if (iTmp == 0x7d)
		{
			pDst[0] = pSrc[0] ^ 0x20;
			pSrc ++;
			pDst ++;
		}
		else if (iTmp == 0x7e)
		{
//			iType = PXCHAR2USHORT((PXCHAR)Adapter_pReceivePacket + HDLC_PROT);
//			iDstLen -= HDLC_INFO + HDLC_FCS_LEN;
//			Since I have get Adapter_iReceiveLen from IP header, so I don't need use iDstLen to get this value now.
/*			iDstLen -= HDLC_FCS_LEN;
			Adapter_iReceiveLen = iDstLen;
*/
//			p_memcpy_x2x((PXCHAR)Adapter_pReceivePacket, (PXCHAR)(Adapter_pReceivePacket+HDLC_INFO), iDstLen);
			ModemTcpipRun(iType);
			return;
		}
		else
		{
			pDst[0] = iTmp;
			pDst ++;
		}
		iDstLen ++;		

		if (iDstLen == HDLC_INFO)
		{
			if (!iHdlcGetProtocol)
			{
				iType = PXCHAR2USHORT((PXCHAR)Adapter_pReceivePacket + HDLC_PROT);
				iDstLen = 0;
				pDst = (PXCHAR)Adapter_pReceivePacket;
				iHdlcGetProtocol = 1;
			}
		}
		else if (iDstLen == IP_DATA)
		{			
			if (iType == PPP_PROT_IP)
			{
				if (!iHdlcIpPacket)
				{
					p_memcpy_x2x(IP_pReceiveHeader, (PXCHAR)Adapter_pReceivePacket, IP_DATA);
					// get Adapter_iReceiveLen from IP header.
					Adapter_iReceiveLen = PXCHAR2USHORT((PXCHAR)(IP_pReceiveHeader+IP_LEN));
					Adapter_iReceiveLen -= IP_DATA;

					iDstLen = 0;
					pDst = (PXCHAR)Adapter_pReceivePacket;
					iHdlcIpPacket = 1;
				}
			}
		}
	}
}

void ModemRun()
{
	DCHAR iTail, iLen, i, j;
	DCHAR iTmp;
	XCHAR pModemBuf[MODEM_BUF_LENGTH];

	// receive part
	iTail = Modem_iInputTail;
	iTail ++;
	if (iTail != Modem_iInputHead)
	{	// something in tail to send
		Modem_iInputTail = iTail;
		SdramRead(MODEM_RECV_SDRAM_HI, Modem_iInputTail, MODEM_BUF_DWORD);

		if (Modem_bAtMode)
		{
			iLen = X_VAL(Sdram_pDataCache, MODEM_BUF_LENGTH);
			if (iLen)
			{
				OutputPXCHAR((PXCHAR)Sdram_pDataCache, iLen);

				p_memcpy_x2x((PXCHAR)_pStoreBuf, (PXCHAR)Sdram_pDataCache, iLen);
				_pStoreBuf += iLen;

				for (i = 0; i < iLen; i ++)
				{
					if (X_VAL(Sdram_pDataCache, i) == 'T')
					{	
						iTmp = 0;
						if (!p_memcmp_c2x((PXCHAR)(_pStoreBuf-iLen+i-6), (PCCHAR)"CONNECT", 7))
						{
							Modem_bAtMode = FALSE;
//							DebugCString((PCCHAR)"Modem Connected");
							LcpConfigReq();		
							return;
						}
					}
				}

			}
		}
		else
		{
			iLen = X_VAL(Sdram_pDataCache, MODEM_BUF_LENGTH);
			p_memcpy_x2x(pModemBuf, (PXCHAR)Sdram_pDataCache, MODEM_BUF_LENGTH);
			if (iLen)
			{
				for (i = 0; i < iLen; i ++)
				{
//					iTmp = X_VAL(Sdram_pDataCache, i);
					iTmp = pModemBuf[i];
					if (_bHeadFound)
					{
						if (iTmp == FLAG_VALUE)
						{
							_bHeadFound = FALSE;
							_pReceiveCur[0] = iTmp;
							_iReceiveLen ++;
			
							p_memcpy_x2x((PXCHAR)Sdram_pDataCache, pModemBuf, MODEM_BUF_LENGTH);
							p_memcpy_x2x((PXCHAR)Sdram_pDataCache, (PXCHAR)(Sdram_pDataCache+i), (UCHAR)(iLen-i));
							X_VAL(Sdram_pDataCache, MODEM_BUF_LENGTH) = iLen - i;
							SdramWrite(MODEM_RECV_SDRAM_HI, Modem_iInputTail, MODEM_BUF_DWORD);
							Modem_iInputTail --;

							p_memcpy_x2x((PXCHAR)Sdram_pDataCache, (PXCHAR)Adapter_pReceivePacket, _iReceiveLen);
							_SdramModemWrite(_iModemSdramIndex, _iReceiveLen);
							_iModemSdramIndex ++;
							_pReceiveCur = (PXCHAR)Adapter_pReceivePacket;

							for (j = MODEM_FRAME_SDRAM_LO; j < _iModemSdramIndex; j ++)
							{
								_SdramModemRead(j);
								p_memcpy_x2x(_pReceiveCur, (PXCHAR)Sdram_pDataCache, 64);
								_pReceiveCur += 64; 
							}
							_HdlcDecode();
							return;
						}
						else
						{
							_pReceiveCur[0] = iTmp;
							_pReceiveCur ++;
							_iReceiveLen ++;
						}
					}
					else
					{
						if (iTmp == FLAG_VALUE)
						{
							_pReceiveCur = (PXCHAR)Adapter_pReceivePacket;
							_iReceiveLen = 0;
							_iModemSdramIndex = MODEM_FRAME_SDRAM_LO;

							_pReceiveCur[0] = iTmp;
							_pReceiveCur ++;
							_iReceiveLen ++;
						}
						else if (iTmp == ADDR_VALUE && _iLastHdlcCode == FLAG_VALUE)
						{
							_bHeadFound = TRUE;
							_pReceiveCur[0] = iTmp;
							_pReceiveCur ++;
							_iReceiveLen ++;
						}
					}
					_iLastHdlcCode = iTmp;
					
					if (_iReceiveLen == 64)
					{
						p_memcpy_x2x((PXCHAR)Sdram_pDataCache, (PXCHAR)Adapter_pReceivePacket, _iReceiveLen);
						_SdramModemWrite(_iModemSdramIndex, _iReceiveLen);
						_pReceiveCur = (PXCHAR)Adapter_pReceivePacket;
						_iReceiveLen = 0;
						_iModemSdramIndex ++;
					}
				}
			}
		}
	}
}

BOOLEAN ModemInitialize()
{
    XBYTE[MODEM_BASE+MODEM_LCR] = LCR_VALUE & 0x7f;                /* Clear DLAB */
    XBYTE[MODEM_BASE+MODEM_FCR] = FCR_VALUE;                       /* Set FIFO control */
    XBYTE[MODEM_BASE+MODEM_FCR] = FCR_VALUE | 0x06;                /* Reset all FIFO */
    XBYTE[MODEM_BASE+MODEM_MCR] = MCR_VALUE;                       /* Set Modem control */
    XBYTE[MODEM_BASE+MODEM_LCR] = LCR_VALUE;                       /* Set Line control */
    XBYTE[MODEM_BASE+MODEM_IER] = IER_VALUE;		                /* Set interrupt control */
    XBYTE[MODEM_BASE+MODEM_LCR] = LCR_VALUE | 0x80;                /* Set DLAB */
    XBYTE[MODEM_BASE+MODEM_DLL] = DLL_VALUE;                       /* Set Divisor LSB Value */
    XBYTE[MODEM_BASE+MODEM_DLM] = DLM_VALUE;                       /* Set Divisor MSB Value */
	if (XBYTE[MODEM_BASE+MODEM_DLL] != DLL_VALUE)
	{
		return FALSE;
	}
    XBYTE[MODEM_BASE+MODEM_LCR] = LCR_VALUE & 0x7f;                /* Clear DLAB */

	Modem_iInputHead = 0;
	Modem_iInputTail = 255;
	Modem_iOutputHead = 0;
	Modem_iOutputTail = 255;

	Modem_bAtMode = TRUE;
	_pStoreBuf = (PXCHAR)Adapter_pReceivePacket;
	_iLastHdlcCode = 0;
	_iModemSdramIndex = MODEM_FRAME_SDRAM_LO;
	_iReceiveLen = 0;

	_bHeadFound = FALSE;

	PPP_Initialize();

	return TRUE;
}

void ModemSend(USHORT iLength, PXCHAR pData)
{
	DSHORT iLen;
	PXCHAR_D pCur;
	DCHAR iBufLength;

	iLen = iLength;
	pCur = (PXCHAR)pData;

	while (iLen)
	{
		if (iLen >= MODEM_TXD_FIFO_SIZE)
		{
			iBufLength = MODEM_TXD_FIFO_SIZE;
		}
		else 
		{
			iBufLength = (UCHAR)iLen;
		}

		if (Modem_iOutputHead != Modem_iOutputTail)
		{	// send buffer still available
			p_memcpy_x2x((PXCHAR)Sdram_pDataCache, pCur, iBufLength);
			X_VAL(Sdram_pDataCache, MODEM_BUF_LENGTH) = iBufLength;
			SdramWrite(MODEM_SEND_SDRAM_HI, Modem_iOutputHead, MODEM_BUF_DWORD);
			Modem_iOutputHead ++;
		}
		else
		{
//			DebugCString((PCCHAR)"Send buf full");
		}

		iLen -= iBufLength;
		pCur += iBufLength;
	}

	XBYTE[MODEM_BASE+MODEM_IER] = IER_VALUE | 0x02;
}

void ModemSendData(USHORT iLength, PXCHAR pData)
{
	XCHAR pSendBuf[MODEM_TXD_FIFO_SIZE];
	PXCHAR_D pSrc;
	PXCHAR_D pDst;
	DSHORT iSrcLen;
	DCHAR iDstLen, iTmp;

	pSrc = pData;
	pDst = pSendBuf;

	iSrcLen = pppfcs16(PPPINITFCS16, pSrc, iLength);
	iSrcLen ^= 0xffff;             /* complement */
	USHORT2PXCHAR_L(iSrcLen, (PXCHAR)(pSrc+iLength));
//	X_VAL(pSrc, iLength) = P_LOBYTE(iSrcLen);
//	X_VAL(pSrc, iLength+1) = P_HIBYTE(iSrcLen);
	iLength += 2;

	iSrcLen = iLength;
	pDst[0] = FLAG_VALUE;
	pDst ++;
	iDstLen = 1;

	while (iSrcLen)
	{
		iTmp = pSrc[0];
		if ((iTmp < 0x20) || (iTmp == 0x7e) || (iTmp == 0x7d))
		{
			pDst[0] = 0x7d;
			pDst ++;
			iDstLen ++;
			if (iDstLen == MODEM_TXD_FIFO_SIZE)
			{
				ModemSend(iDstLen, pSendBuf);
				pDst = (PXCHAR)pSendBuf;
				iDstLen = 0;
			}
			pDst[0] = iTmp ^ 0x20;
			pDst ++;
			iDstLen ++;
		}
		else 
		{
			pDst[0] = iTmp;
			pDst ++;
			iDstLen ++;
		}
		pSrc ++;
		iSrcLen --;

		if (iDstLen == MODEM_TXD_FIFO_SIZE)
		{
			ModemSend(iDstLen, pSendBuf);
			pDst = (PXCHAR)pSendBuf;
			iDstLen = 0;
		}
	}

	pDst[0] = FLAG_VALUE;
	pDst ++;
	iDstLen ++;
	ModemSend(iDstLen, pSendBuf);
}

//void ModemSend(USHORT iLength, PXCHAR pData)
//{
/*	DSHORT iLen;
	PXCHAR_D pCur;
	DCHAR iBufLength;

	iLen = iLength;
	pCur = pData;

	while (iLen)
	{
		if (iLen >= MODEM_TXD_FIFO_SIZE)
		{
			iBufLength = MODEM_TXD_FIFO_SIZE;
		}
		else 
		{
			iBufLength = P_LOBYTE(iLen);
		}

		if (Modem_iOutputHead != Modem_iOutputTail)
		{	// send buffer still available
			p_memcpy_x2x((PXCHAR)Sdram_pDataCache, pCur, iBufLength);
			X_VAL(Sdram_pDataCache, MODEM_BUF_LENGTH) = iBufLength;
			SdramWrite(MODEM_SEND_SDRAM_HI, Modem_iOutputHead, MODEM_BUF_DWORD);
			Modem_iOutputHead ++;
		}
		else
		{
			DebugCString((PCCHAR)"Send buf full");
		}

		iLen -= iBufLength;
		pCur += iBufLength;
	}
*/
/*		if (Modem_iOutputHead != Modem_iOutputTail)
		{	// send buffer still available
			p_memcpy_x2x((PXCHAR)Sdram_pDataCache, pData, iLength);
			X_VAL(Sdram_pDataCache, MODEM_BUF_LENGTH) = iLength;
			SdramWrite(MODEM_SEND_SDRAM_HI, Modem_iOutputHead, MODEM_BUF_DWORD);
			Modem_iOutputHead ++;
		}
	XBYTE[MODEM_BASE+MODEM_IER] = IER_VALUE | 0x02;
}
*/

/*
void ModemInterruptSdramRead(UCHAR iIndexLow)
{
//	XBYTE[SDRAM_CON_REQ] = 0x04;		// SDRAM to controller, request level = 2
	XBYTE[SDRAM_CON_REQ] = 0x02;		// SDRAM to controller, request level = 1
	XBYTE[SRAM_CON_SWAP] = 0x01;
	XBYTE[SRAM_CON_CH1ADDRL] = 0;		// Set Controller Buf Address, using 0x7000
//	XBYTE[SRAM_CON_CH1ADDRH] = 0;		// Can be omitted

	XBYTE[DMA9_START_ADDR2] = iIndexLow;
	XBYTE[DMA9_START_ADDR3] = MODEM_SEND_SDRAM_HI;
	XBYTE[DMA9_X1] = MODEM_BUF_DWORD;			// Dwords to be transfered
//	XBYTE[DMA9_Y1] = 0;					// Can be omitted
//	XBYTE[DMA9_HIGH] = 0;				// Can be omitted
//	while (XBYTE[SDRAM_STATUS2]);
	XBYTE[DMA9_START_ADDR1] = 0;

    while(XBYTE[SDRAM_STATUS1] & 0x80);
	XBYTE[SRAM_CON_SWAP] = 0;
}

void ModemInterruptSdramWrite(UCHAR iIndexLow)
{
//	XBYTE[SDRAM_CON_REQ] = 0x05;		// SDRAM to controller, request level = 2
	XBYTE[SDRAM_CON_REQ] = 0x03;		// SDRAM to controller, request level = 1
	XBYTE[SRAM_CON_SWAP] = 0x01;
	XBYTE[SRAM_CON_CH1ADDRL] = 0;		// Set Controller Buf Address, using 0x7000
//	XBYTE[SRAM_CON_CH1ADDRH] = 0;		// Can be omitted

	XBYTE[DMA9_START_ADDR2] = iIndexLow;
	XBYTE[DMA9_START_ADDR3] = MODEM_RECV_SDRAM_HI;
	XBYTE[DMA9_X1] = MODEM_BUF_DWORD;			// DWords to be transfered
//	XBYTE[DMA9_Y1] = 0;					// Can be omitted
//	XBYTE[DMA9_HIGH] = 0;				// Can be omitted
//	while (XBYTE[SDRAM_STATUS2]);
	XBYTE[DMA9_START_ADDR1] = 0;
	
    while(XBYTE[SDRAM_STATUS1] & 0x80);
	XBYTE[SRAM_CON_SWAP] = 0;
}
*/

void ModemHandleInterrupt()
{
	DCHAR iIntType, iTail;
	XCHAR pTempBuf[MODEM_BUF_SIZE];

	p_memcpy_x2x(pTempBuf, (PXCHAR)Sdram_pDataCache, MODEM_BUF_SIZE);	// save 
	while (TRUE) 
	{
		iIntType = XBYTE[MODEM_BASE+MODEM_IIR] & 0xf;
		switch(iIntType)
		{
		case 0x02:                          // MODEM TX Empty 
			iTail = Modem_iOutputTail;
			iTail ++;
			if (iTail != Modem_iOutputHead)
			{	// something in tail to send
//				if (XBYTE[MODEM_BASE+MODEM_MSR] & MSR_DSR_OR_MASK)		// Check if DSR is 1
				{
					Modem_iOutputTail = iTail;
//					ModemInterruptSdramRead(Modem_iOutputTail);
					sdram_read(MODEM_SEND_SDRAM_HI, Modem_iOutputTail, MODEM_BUF_DWORD);
					p_outp(MODEM_BASE+MODEM_THR, (PXCHAR)Sdram_pDataCache, X_VAL(Sdram_pDataCache, MODEM_BUF_LENGTH));
				}
			}
			else 
			{
//				iIntType = XBYTE[MODEM_BASE+MODEM_IER];
				XBYTE[MODEM_BASE+MODEM_IER] = IER_VALUE;
			}
			break;

		case 0x04:                          // MODEM RX FIFO ready 
			XBYTE[MODEM_BASE+MODEM_MCR] &= MCR_DTR_AND_MASK;		// Set DTR to 0

			p_inp(MODEM_BASE+MODEM_RBR, (PXCHAR)Sdram_pDataCache, MODEM_RXD_FIFO_TRIGGER);
			X_VAL(Sdram_pDataCache, MODEM_BUF_LENGTH) = MODEM_RXD_FIFO_TRIGGER;

			if (Modem_iInputHead != Modem_iInputTail)
			{	// receive buffer still available
//				ModemInterruptSdramWrite(Modem_iInputHead);
				sdram_write(MODEM_RECV_SDRAM_HI, Modem_iInputHead, MODEM_BUF_DWORD);
				Modem_iInputHead ++;
			}

			XBYTE[MODEM_BASE+MODEM_MCR] |= MCR_DTR_OR_MASK;		// Set DTR to 1
			break;

		case 0x0c:                          // MODEM RX buf timeout 
			X_VAL(Sdram_pDataCache, 0) = XBYTE[MODEM_BASE+MODEM_RBR];			
			X_VAL(Sdram_pDataCache, MODEM_BUF_LENGTH) = 1;

			if (Modem_iInputHead != Modem_iInputTail)
			{	// receive buffer still available
//				ModemInterruptSdramWrite(Modem_iInputHead);
				sdram_write(MODEM_RECV_SDRAM_HI, Modem_iInputHead, MODEM_BUF_DWORD);
				Modem_iInputHead ++;
			}
			break;

		case 0x06:                          // MODEM Line error 
			iTail = XBYTE[MODEM_BASE+MODEM_LSR];
			break;

		case 0x01:							// No interrupt
			goto End;

		case 0x00:
			iTail = XBYTE[MODEM_BASE+MODEM_MSR];
			break;

		default:
			break;
		}
	}

End:
	p_memcpy_x2x((PXCHAR)Sdram_pDataCache, pTempBuf, MODEM_BUF_SIZE);	// restore
}

void DelayTime()
{
	DCHAR i;

	i = 1;
	while (i)
	{
		i ++;
		TaskWaitPacket();
	}
}

void ModemDial()
{
	XCHAR pSendBuf[64];

	// dial out isp number
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT\r");
	Modem_bAtCmd = TRUE;
	TaskSendPacket(3, pSendBuf);

	DelayTime();

#ifdef RES_JP
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S2\r");
#elif defined RES_SG
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S3\r");
#elif defined RES_IT
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S4\r");
#elif defined RES_EG
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S5\r");
#elif defined RES_NZ_E || defined RES_NZ_M
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S6\r");
#elif defined RES_TW
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S7\r");
#elif defined RES_AU
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S8\r");
#elif defined RES_MX
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S9\r");
#elif defined RES_NO
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S11\r");
#elif defined RES_BE_F || defined RES_BE_N
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S12\r");
#elif defined RES_NL
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S13\r");
#else	// RES_US
	p_strcpy_c2x(pSendBuf, (PCCHAR)"AT%S0\r");
#endif

	Modem_bAtCmd = TRUE;
#if defined RES_NO || defined RES_BE_F || defined RES_BE_N || defined RES_NL
	TaskSendPacket(7, pSendBuf);
#else
	TaskSendPacket(6, pSendBuf);
#endif
	DelayTime();

	p_strcpy_c2x(pSendBuf, (PCCHAR)"ATDT");
	p_strcat_x2x(pSendBuf, Sys_pForwardNumber);
	p_strcat_char2x(pSendBuf, '\r');
	Modem_bAtCmd = TRUE;
	TaskSendPacket(p_strlen_x(pSendBuf), pSendBuf);
}

void ModemRedial()
{
	XBYTE[IO_HI_INTCLR] = 0xff;	// Clear io edge irq

	ModemDial();
	SysSleep(2000000);
	ModemDial();
	DisplayString(g_cModem, 0);
	DisplayString(g_cConnecting, 1);
}

void ModemDisConnect()
{
	XCHAR pSendBuf[64];

	p_strcpy_c2x(pSendBuf, (PCCHAR)"+++");
	Modem_bAtCmd = TRUE;
	TaskSendPacket(3, pSendBuf);

	DelayTime();

	p_strcpy_c2x(pSendBuf, (PCCHAR)"ATH0\r");
	Modem_bAtCmd = TRUE;
	TaskSendPacket(5, pSendBuf);
}

void ModemStartup()
{
	XBYTE[IO_HI_INTCLR] = 0xff;	// Clear io edge irq
	if (Sys_iIPType != IP_TYPE_MODEM)
	{
		XBYTE[IO_HI_INTEN] &= MODEM_PIO_INT_AND;			// disable modem irq, IO pin 11
	}
}

void ModemInit()
{
	if (Sys_iIPType == IP_TYPE_MODEM)
	{
		if (ModemInitialize())
		{
			XBYTE[IO_HI_INTEN] |= MODEM_PIO_INT_OR;			// enable modem irq, IO pin 11
			Modem_bInUse = TRUE;
//#ifndef VER_IPH_E00
			ModemDial();
//#endif
			TcpipInitialize();
			DisplayString(g_cModem, 0);
			DisplayString(g_cConnecting, 1);
		}
		else
		{
			Modem_bInUse = FALSE;
			Sys_iIPType = IP_TYPE_DHCP;
		}
	}
}

#endif
