#include <reg52.h>

#include "p_apps.h"
#include "PalmChip.h"
#include "v_task.h"

void main(void)
{
	DCHAR iVal;

	Led_On(NORMAL_LIGHT);

	Run_22Mhz();		// 22.2Mhz
	DisplayString((PCCHAR)"Auto Test Begin", 0);

	SdramLoadData(Sys_cSafeOptions, 0x0000, 512);
	SysReadOptions();
	Udp_iOutputDebug = DEBUG_DISABLE_PRODUCTID;

	LoadFlags();

	Led_On(SECOND_LIGHT);
	SysSleep(1000000);									// Pause
	Ne2000Initialize_Step2();
	SysSleep(1000000);									// Pause

	KeypadStartup();

	ControlInitialize();

	if (Ne2000Initialize())
	{
		TcpipInitialize();
		DisplayString((PCCHAR)"Network Init OK", 1);
	}
	else
	{
		XBYTE[IO_HI_INTCLR] = 0xff;	// Clear io edge irq
		DisplayString(g_cNetwork, 0);
		DisplayString(g_cFailed, 1);
	}

	iVal = 0;
    while (1)
    {
		KeypadRun();
		Ne2000Run();
		UdpDebugUpdate();
		if (Timer_bIndicated)
		{
			iVal ++;
			if (iVal == 50)
			{
				TcpipHandleTimer();
				Led_Blink(NORMAL_LIGHT);
				Led_Blink(SECOND_LIGHT);
				iVal = 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
		IoKeyHandleInterrupt();
    }
	if (iIntVal & INT_MASK_KEY)
	{
		KeypadHandleInterrupt();
	}
	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;
	}

	DPS = iDPS;	// Pop DPS value
}

