#include <reg52.h>

#include "settings.h"
#include "PalmChip.h"
#include "p_h323.h"
#include "p_dsp.h"

extern BOOLEAN Menu_bPasswordOK;

void main(void)
{
	DCHAR iCount;

	BootIndication(2);

	SdramReadFlags();
	if (X_VAL(Sdram_pDataCache, FLAGS_UPGRADE_MODE) == '#')
	{
		SdramUpdateFlagByte(FLAGS_UPGRADE_MODE, 0);
		goto StartSetting;
	}

#ifndef MT28F016
#if defined AM29LV004T || defined AM29LV008T || defined MX29LV004T
	// load settings and quick dial
	SdramLoadData(Sys_cOptions, 0x0000, OPTIONS_TOTAL_LEN);
#endif
#ifndef _USE_FXS	// In Gateway's setting, there has no RingType now, so we use RING_TYPE_DTMF for gateway.
	LoadPcmRing();
#endif
#endif	// MT28F016

	BootIndication(3);

	if (KeypadInitialize() != KEYPAD_WELL)
	{
		SwitchPage(2);
	}

StartSetting:
	iCount = 0;
	SysReadOptions();	// Load settings here
	SysStoreOptions(0);
	KeypadStartup();
	ControlInitialize();
	
	DisplayString(g_cPasswordPrompt, 0);
	KeypadClearAll();
	while (1)
	{
		KeypadRun();
		if (Timer_bIndicated)	// timer is 20ms
		{
			if(Menu_bPasswordOK)
			{
				KeyInputHandleTimer();
			}
			else
			{
				Key_bPressed = FALSE;
				KeypadDisplay();
			}
			SettingsHandleTimer();
			iCount ++;
			if (iCount == 50)
			{
				Led_Blink(NORMAL_LIGHT);
				iCount = 0;
			}
			Timer_bIndicated = FALSE;
		}
	}
}

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

	iDPH = DPH1;
	iDPL = DPL1;
	iDPS = DPS;	// Push DPS value
	DPS = 0;

    iIntVal = XBYTE[INT_VALUE];

    if (iIntVal & INT_MASK_IO)
	{
		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;
	}

	DPH1 = iDPH;
	DPL1 = iDPL;
	DPS = iDPS;	// Pop DPS value
}


