#include <reg52.h>

#include "p_apps.h"
#include "palmchip.h"
#include "v_task.h"
#include "p_http.h"
#include "autoprov.h"

void _OnImpossibleKey()
{
	BootIndication(0);
	DisplayCursor(TRUE);
	DisplayInitialize();
	DisplayOutputVol();
	GetDateTimeDisplay((PXCHAR)0, FALSE, 0);
	GetSoftwareVersion();
	GetTimeDisplay(0, (PUCHAR)0);
	GetVersionText();
	HttpSendError();
	HttpSendOK(0, HTTP_CT_TEXT_HTML);
	IsBroadcastIP((PXCHAR)0);
	IsFunctionMode();
	IsPassword((PXCHAR)0);
	IsUpgrading();
	OnUpdateRingFile(0);
	OutputPXCHAR((PXCHAR)1, 0);
	p_IsNewLine((PXCHAR)0);
	p_line_backup();
	p_line_inc_len(0);
	p_line_restore();
	p_line_set_len(0);
	p_String2Mac((PXCHAR)0, (PXCHAR)0);
	ProxyGetLocalIP((PXCHAR)0);
	SdramCopyData(0, (PXCHAR)0);
	SdramCopyData_0(0, (PXCHAR)0);
	SdramUpdateFlagByte(0, 0);
	SoloFileCloseRead();
	SoloFileIsRunning();
	SoloFileRead((PXCHAR)0, 0);
	SoloFileSetLimit(0);
	SoloFileWriteSdramCache((UCHAR)256);
	SysLoadOptions(0);
	SysStoreOptions(0);
	TaskWaitPacket();
	TcbConnected(0);
	UdpSetTos(0, 0);
	ReadSpeedDial(0);
}

void main(void)
{
	DCHAR iCounter = 0;

	SdramReadOptions();
	if (!X_VAL(Sdram_pDataCache, OPTIONS_UPGRADE_TYPE))
//	if (X_VAL(Sdram_pDataCache, OPTIONS_UPGRADE_TYPE) != 0xff)
	{
		SwitchPage(12);	// continue to check other options in page12
		_OnImpossibleKey();
	}

	SysReadOptions();

#ifdef CALL_H323
	Sys_iProtocol = PROTOCOL_H323;
#elif defined CALL_SIP
	Sys_iProtocol = PROTOCOL_SIP;
#elif defined CALL_MGCP
	Sys_iProtocol = PROTOCOL_MGCP;
#elif defined CALL_N2P
	Sys_iProtocol = PROTOCOL_N2P;
#elif defined CALL_WP
	Sys_iProtocol = PROTOCOL_WP;
#elif defined CALL_IAX2
	Sys_iProtocol = PROTOCOL_IAX2;
#elif defined CALL_EPN
	Sys_iProtocol = PROTOCOL_EPN;
#endif

#ifdef _USE_FXS
	Sys_iDeviceAttr = DEVICE_ATTR_NONE;
#else
	Sys_iDeviceAttr = DEVICE_ATTR_SPEAKER;
#endif
#ifdef _USE_FAKE_FXO
	Sys_iDeviceAttr |= DEVICE_ATTR_DUALMODE;
#endif

	ControlInitialize();
	if (!Ne2000Initialize())
	{
		SwitchPage(0);
	}
	TcpipInitialize();
	switch (Sys_iIPType)
	{
	case IP_TYPE_DHCP:
		DisplayString(g_cDHCP, 0);
		DisplayString(g_cConnecting, 1);
		break;
	case IP_TYPE_PPPOE:
		DisplayString(g_cPPPoE, 0);
		DisplayString(g_cConnecting, 1);
		break;
	}

    while (1)
	{
		Ne2000Run();
		UdpDebugUpdate();
		if (Timer_bIndicated)	// indicate a 20 ms timer
		{
			iCounter ++;
			if (iCounter == 50)
			{
				TcpipHandleTimer();
				iCounter = 0;
			}
			Timer_bIndicated = FALSE;
		}
	}
}

void PalmInterrupt() interrupt 2	// using 2
{
	DCHAR iDPS, iIntVal;

	iDPS = DPS;	// Push DPS value
	DPS = 0;

    iIntVal = XBYTE[INT_VALUE];

    if (iIntVal & INT_MASK_IO)
	{
		// Check PIO 3, 4, 5, 6, 12, 13, 14, 15 key
		XBYTE[IO_LO_INTCLR] = 0xff;	// Clear all io edge irq
		XBYTE[IO_HI_INTCLR] = 0xff;	// Clear all io edge irq
		XBYTE[IO_HHI_INTCLR] = 0xff;	// Clear all io edge irq
	}
	if (iIntVal & INT_MASK_DMA)
	{
		XBYTE[SDRAM_DMA_INT1] = DMA_MASK_AC97_OUT+DMA_MASK_AC97_IN;
	}
	if (iIntVal & INT_MASK_DSP)
	{
		XBYTE[INT_CLR] |= INT_MASK_DSP;
	}
	if (iIntVal & INT_MASK_KEY)
	{
		XBYTE[KEY_INTCLR] = 0x7f;	// Clear keypad irq
	}

	DPS = iDPS;	// Pop DPS value
}
