//----------------------------------------------------------------------------------------------------
// ID Code      : Misc.c No.0002
// Update Note  :
//
//----------------------------------------------------------------------------------------------------

#define __MISC__

#include "Core\Header\Include.h"

//----------------------------------------------------------------------------------------------------
// Polling Events
//----------------------------------------------------------------------------------------------------

#if(_RESET_TYPE == _RESET_IN)
//--------------------------------------------------
// Description  : Scaler Reset polling event
// Input Value  : None
// Output Value : Return _TRUE if Scaler Reset finished
//--------------------------------------------------
bit CMiscScalerResetEvent(void)
{
    if(!(bit)CScalerGetBit(bSCALERRESET, _BIT0))
        return _TRUE;
    else
        return _FALSE;
}
#endif  // End of #if(_RESET_TYPE == _RESET_IN)

#if(_HSYNC_TYPE_DETECTION == _AUTO_RUN)
/**
 * CMiscStableFlagPollingEvent
 * Stable flag polling event
 * @param <none>
 * @return {_TRUE if event occurs,_FALSE if not}
 *
*/
//--------------------------------------------------
// Description  : Stable flag polling event
// Input Value  : None
// Output Value : Return _TRUE if event occurs
//--------------------------------------------------
#if 0
bit CMiscStableFlagPollingEvent(void)
{
    if((bit)CScalerGetBit(_STABLE_MEASURE_4F, _BIT7))
        return _TRUE;
    else
        return _FALSE;
}
#endif
#endif	// End of #if(_HSYNC_TYPE_DETECTION == _NORMAL_RUN)

#if(_HSYNC_TYPE_DETECTION == _AUTO_RUN)
/**
 * CMiscHsyncTypeAutoRunFlagPollingEvent
 * Hsync Type Detection Auto Run flag polling event
 * @param <none>
 * @return {_TRUE if event occurs,_FALSE if not}
 *
*/
//--------------------------------------------------
// Description  : Hsync Type Detection Auto Run flag polling event
// Input Value  : None
// Output Value : Return _TRUE if event occurs
//--------------------------------------------------
bit CMiscHsyncTypeAutoRunFlagPollingEvent(void)
{
    if((bit)CScalerGetBit(_VSYNC_COUNTER_LEVEL_MSB_4C, _BIT7))
        return _TRUE;
    else
        return _FALSE;
}
#endif	// End of #if(_HSYNC_TYPE_DETECTION == _AUTO_RUN)

/**
 * CMiscModeMeasurePollingEvent
 * Measure start/end polling event
 * @param <none>
 * @return {none}
 *
*/
//--------------------------------------------------
// Description  : Measure start/end polling event
// Input Value  : None
// Output Value : Return _TRUE if measure finished
//--------------------------------------------------
bit CMiscModeMeasurePollingEvent(void)
{
    if(!(bit)CScalerGetBit(_MEAS_HS_PERIOD_H_52, _BIT5))
        return _TRUE;
    else
        return _FALSE;
}

#if((_TMDS_SUPPORT == _ON) || (_HDMI_SUPPORT == _ON))
//--------------------------------------------------
// Description  : Auto measure start/end polling event
// Input Value  : None
// Output Value : Return _TRUE if auto measure finished
//--------------------------------------------------
bit CMiscAutoMeasurePollingEvent(void)
{
	if(!(bit)CScalerGetBit(_AUTO_ADJ_CTRL1_7D, _BIT0))
       	return _TRUE;
    else
       	return _FALSE;
}
#endif

//--------------------------------------------------
// Description  : Apply double buffer polling event
// Input Value  : None
// Output Value : Return _TRUE if apply double buffer finished
//--------------------------------------------------
bit CMiscApplyDoubleBufferPollingEvent(void)
{
    if(!(bit)CScalerGetBit(_VGIP_CTRL_10, _BIT5))
        return _TRUE;
    else
        return _FALSE;
}

#if(((_TMDS_SUPPORT == _ON) || (_HDMI_SUPPORT == _ON)) && (_DVI_LONG_CABLE_SUPPORT == _ON))
//--------------------------------------------------
// Description  : TMDS transition measure polling event
// Input Value  : None
// Output Value : Return _TRUE if measure finished
//--------------------------------------------------
bit CMiscTMDSMeasureEvent(void)
{
	CScalerPageSelect(_PAGE2);
 	if(!(bit)CScalerGetBit(_P2_TMDS_MEAS_RESULT0_A2, _BIT7))
       	return _TRUE;
    else
       	return _FALSE;
}
#endif  // End of #if(((_TMDS_SUPPORT == _ON) || (_HDMI_SUPPORT == _ON)) && (_DVI_LONG_CABLE_SUPPORT == _ON))

#if((_TMDS_SUPPORT == _ON) || (_HDMI_SUPPORT == _ON))
#if(_SCALER_TYPE!=_RTD2662)
//--------------------------------------------------
// Description  : TMDS CRC measure polling event
// Input Value  : None
// Output Value : Return _TRUE if CRC measure finished
//--------------------------------------------------
bit CMiscTMDSCRCEvent(void)
{
    CScalerPageSelect(_PAGE2);
 	if(!(bit)CScalerGetBit(_P2_TMDS_CTRL_A4, _BIT0))
       	return _TRUE;
    else
       	return _FALSE;
}
#endif
#endif  // End of #if(_TMDS_SUPPORT == _ON)

//----------------------------------------------------------------------------------------------------
// Misc Functions
//----------------------------------------------------------------------------------------------------

//--------------------------------------------------
// Description  : Enable double buffer
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CMiscEnableDoubleBuffer(void)
{
    CScalerSetBit(_VGIP_CTRL_10, ~_BIT4, _BIT4);
}

//--------------------------------------------------
// Description  : Disable double buffer
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CMiscDisableDoubleBuffer(void)
{
    CScalerSetBit(_VGIP_CTRL_10, ~_BIT4, 0x00);
}

//--------------------------------------------------
// Description  : Apply double buffer
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CMiscApplyDoubleBuffer(void)
{
	BYTE ucTimeout=12;
 	if((bit)CScalerGetBit(_VGIP_CTRL_10, _BIT4))
    {
      	CScalerSetBit(_VGIP_CTRL_10, ~_BIT5, _BIT5);
		do
    		{
        		CTimerDelayXms(5);
        		if(CMiscApplyDoubleBufferPollingEvent())
        		{
            		break;
        		}
    		}
    		while(--ucTimeout);	
       	//CTimerPollingEventProc(90, CMiscApplyDoubleBufferPollingEvent);
    }
}

//--------------------------------------------------
// Description  : Clear status Reg[02] and Reg[03]
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CMiscClearStatusRegister(void)
{
    CScalerSetByte(_STATUS0_02, 0x00);
    CScalerSetByte(_STATUS1_03, 0x00);
}

//--------------------------------------------------
// Description  : Set pin share
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CMiscSetPinShare(void)
{
#if(_SCALER_TYPE==_RTD2662)
	#if(_MCU_TYPE == _REALTEK_RTD2662_EMCU)
		
	MCU_PIN_SHARE_CTRL00_FF96 = (((BYTE)_PIN_58_59_DDC1_ENABLE<<7) | (_PIN_58<<5) | (_PIN_59<<3) | (_PIN_50));
    MCU_PIN_SHARE_CTRL01_FF97 = (((BYTE)_PIN_51<<6) | (_PIN_64<<3) | (_PIN_65));
    MCU_PIN_SHARE_CTRL02_FF98 = (((BYTE)_PIN_52<<6) | (_PIN_66<<3) | (_PIN_67));
    MCU_PIN_SHARE_CTRL03_FF99 = (((BYTE)_PIN_53<<6) | (_PIN_69<<3) | (_PIN_70));
    MCU_PIN_SHARE_CTRL04_FF9A = (((BYTE)_PIN_55<<5) | (_PIN_56_57_IIC_ENABLE<<4) | (_PIN_56<<2) | (_PIN_57));
    MCU_PIN_SHARE_CTRL05_FF9B = (((BYTE)_PIN_68<<4) | (_PIN_71));
    MCU_PIN_SHARE_CTRL06_FF9C = (((BYTE)_PIN_54<<6) | (_PIN_96<<3) | (_PIN_97));
    MCU_PIN_SHARE_CTRL07_FF9D = (((BYTE)_PIN_74to83<<6) | (_PIN_99<<3) | (_PIN_100));
    MCU_PIN_SHARE_CTRL08_FF9E = (((BYTE)_PIN_102<<3) | (_PIN_105));
    MCU_PIN_SHARE_CTRL09_FF9F = (((BYTE)_PIN_98<<6) | (_PIN_101<<3) | (_PIN_108));
    MCU_PIN_SHARE_CTRL0A_FFA0 = (((BYTE)_PIN_103<<4) | (_PIN_104<<1));
    MCU_PIN_SHARE_CTRL0B_FFA1 = (((BYTE)_PIN_109<<4) | (_PIN_110));
    MCU_PIN_SHARE_CTRL0C_FFA2 = (((BYTE)_PIN_111<<4) | (_PIN_112));
    MCU_PIN_SHARE_CTRL0D_FFA3 = (((BYTE)_PIN_113<<4) | (_PIN_114));
    MCU_PIN_SHARE_CTRL0E_FFA4 = (((BYTE)_PIN_124<<6) | (_PIN_123<<4) | (_PIN_122<<2) | (_PIN_121));
	MCU_PIN_DRIVING_CTRL10_FFA6 |= (_BIT0);
	#else
	
 	pData[0] = (((BYTE)_PIN_58_59_DDC1_ENABLE<<7) | (_PIN_58<<5) | (_PIN_59<<3) | (_PIN_50));
    pData[1] = (((BYTE)_PIN_51<<6) | (_PIN_64<<3) | (_PIN_65));
    pData[2] = (((BYTE)_PIN_52<<6) | (_PIN_66<<3) | (_PIN_67));
    pData[3] = (((BYTE)_PIN_53<<6) | (_PIN_69<<3) | (_PIN_70));
    pData[4] = (((BYTE)_PIN_55<<5) | (_PIN_56_57_IIC_ENABLE<<4) | (_PIN_56<<2) | (_PIN_57));
    pData[5] = (((BYTE)_PIN_68<<4) | (_PIN_71));
    pData[6] = (((BYTE)_PIN_54<<6) | (_PIN_96<<3) | (_PIN_97));
    pData[7] = (((BYTE)_PIN_74to83<<6) | (_PIN_99<<3) | (_PIN_100));
    pData[8] = (((BYTE)_PIN_102<<3) | (_PIN_105));
    pData[9] = (((BYTE)_PIN_98<<6) | (_PIN_101<<3) | (_PIN_108));
    pData[10] = (((BYTE)_PIN_103<<4) | (_PIN_104<<1));
    pData[11] = (((BYTE)_PIN_109<<4) | (_PIN_110));
    pData[12] = (((BYTE)_PIN_111<<4) | (_PIN_112));
    pData[13] = (((BYTE)_PIN_113<<4) | (_PIN_114));
    pData[14] = (((BYTE)_PIN_124<<6) | (_PIN_123<<4) | (_PIN_122<<2) | (_PIN_121));

	CScalerPageSelect(_PAGEE);
    CScalerWrite(0xD6, 15, pData, _AUTOINC);
		
	#endif
#else
   	pData[0] = _MAIN_TYPE;
//Video 8 or Video 16
  #if(_VIDEO_SUPPORT == _OFF)
    pData[1] = (((BYTE)_PIN_V8_ENABLE<<7) | (_PIN_V16_ENABLE<<6) | (_PIN_37<<4) | (_PIN_38<<3) | (_PIN_39<<2) | (_PIN_32));
    pData[2] = (((BYTE)_PIN_33<<6) | (_PIN_34<<4) | (_PIN_35<<2) | (_PIN_36));
  #else//_VIDEO_SUPPORT == _OFF
  #if((_VIDEO_IN == _VIDEO_8_IN) || (_VIDEO_IN == _VIDEO_16_IN))
    pData[1] = (((BYTE)_PIN_V8_ENABLE<<7) | (_PIN_V16_ENABLE<<6));
    pData[2] = 0x00;
    #endif
  #endif//_VIDEO_SUPPORT == _OFF

	//pin share
    pData[3]   = (((BYTE)_PIN_41<<6) | (_PIN_42<<4) | (_PIN_43<<2) | (_PIN_44));
    pData[4]   = (((BYTE)_PIN_45<<6) | (_PIN_46<<4) | (_PIN_47<<2) | (_PIN_44to47));
    pData[5]   = 0x00;
    pData[6]   = (((BYTE)_PIN_51<<6) | (_PIN_52<<4) | (_PIN_53<<2) | (_PIN_54));
    pData[7]   = (((BYTE)_PIN_61<<1) | (_PIN_62));
    pData[8]   = (((BYTE)_PIN_67<<6) | (_PIN_68<<4) | (_PIN_69<<2) | (_PIN_70));
    pData[9]   = (((BYTE)_PIN_76<<7) | (_PIN_77<<6));
    pData[10] = 0x00;
    pData[11] = 0x00;
    pData[12] = 0x00;
    pData[13] = 0x00;
    pData[14] = (((BYTE)_PIN_124to125<<6) | (_PIN_118<<4) | (_PIN_119<<2) | (_PIN_120));//Select to DDC2 for HDCP

    CScalerSetByte(_PS_ACCESS_PORT_8D, _PS_CTRL00_00);
    CScalerWrite(_PS_DATA_PORT_8E, 15, pData, _NON_AUTOINC);

	//over drive
    pData[0] = (((BYTE)_PIN_DRIVING_MD8toMD15<<4) | (_PIN_DRIVING_MD0toMD7));
    pData[1] = (((BYTE)_PIN_DRIVING_32to40<<4) | (_PIN_DRIVING_41to43));
    pData[2] = (((BYTE)_PIN_DRIVING_44to47<<4));
    pData[3] = (((BYTE)_PIN_DRIVING_51to54<<4) | (_PIN_DRIVING_55to58_59to66));
    pData[4] = (((BYTE)_PIN_DRIVING_SDR_CTRL<<4) | (_PIN_DRIVING_A11));
    pData[5] = (((BYTE)_PIN_DRIVING_A0toA10<<4) | (_PIN_DRIVING_CKE_CLK));
    pData[6] = (((BYTE)_PIN_DRIVING_67to70_76to81<<4) | (_PIN_DRIVING_82to91_93to102_105to114));
    pData[7] = 0x00;
    pData[8] = (((BYTE)_PIN_DRIVING_118to120<<4) | (_PIN_DRIVING_124to125));

    CScalerSetByte(_PS_ACCESS_PORT_8D, _PS_PIN_DRIVING_CTRL0_20);
    CScalerWrite(_PS_DATA_PORT_8E, 9, pData, _NON_AUTOINC);
#endif
}

//--------------------------------------------------
// Description  : Select panel
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CMiscSelectPanel(void)
{
    //ucPanelSelect = _PANEL_TYPE;
}

//----------------------------------------------------------------------------------------------------
// Debug Functions
//----------------------------------------------------------------------------------------------------

//--------------------------------------------------
// Description  : Kingmice process
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CMiscKingmice(void)
{
#if(_DEBUG_TOOL == _KINGMICE)

    static BYTE idata ucI2C_Addr;
    static BYTE idata ucI2C_Data;
    static BYTE idata ucRTD_Data;

    do
    {
        while(ucROM_A4 & 0x01)                          // if action
        {
            if(ucROM_A4 & 0x02)                         // read
            {
                if(ucROM_A4 & 0x10)                     // if IIC_index
                {
                    CI2cRead(ucI2C_Addr, ucROM_A3, 0x01, pData);
                    ucPRN_7 = pData[0];
                }
                else if(ucROM_A4 & 0x40)                // if RTD_index
                {
                    CScalerRead(ucROM_A3, 0x01, pData, _NON_AUTOINC);
                    ucPRN_7 = pData[0];
                }
                else if(ucROM_A4 & 0x80)                // if 8051_index
                {
                    ucPRN_7 = *((BYTE *)ucROM_A3);
                }
            }
            else                                        // write
            {
                if(ucROM_A4 & 0x04)                     // if IIC_addr
                {
                    ucI2C_Addr  = ucROM_A3;
                }
                else if(ucROM_A4 & 0x08)                // if IIC_data
                {
                    ucI2C_Data  = ucROM_A3;
                }
                else if(ucROM_A4 & 0x10)                // if IIC_index
                {
                    CI2cWrite(ucI2C_Addr, ucROM_A3, 0x01, &ucI2C_Data);
                }
                else if(ucROM_A4 & 0x20)                // if RTD_data
                {
                    ucRTD_Data  =  ucROM_A3;
                }
                else if(ucROM_A4 & 0x40)                // if RTD_index
                {
                    CScalerWrite(ucROM_A3, 0x01, &ucRTD_Data, _NON_AUTOINC);
                }
                else if(ucROM_A4 & 0x80)                // if 8051_index
                {
                    *((BYTE *)ucROM_A3) = pData[0];
                }
            }

            if(ucROM_A5)   { }                          // clear action
        }
    }
    while(ucROM_A0 & 0x80);

#endif  // End of #if(_DEBUG_TOOL == _KINGMICE)

}


//--------------------------------------------------
// Description  : ISP process
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CMiscIspack(void)
{
#if(_DEBUG_TOOL == _ISPACK)
#define 	_DEBUGREAD		0
#define  	_DEBUGWRITE		1

//Device ID Define
#define 	_LOOP_			0X88
#define 	_STOP_			0X99

#define	_EDID_			0
#define 	_SCALER_		1
#define	_TCON_			2
#define  	_ADC_			3
#define 	_MCUFLASH_	4
#define	_EEPROM_		5
#define   _DECODE_		6
#define   _TUNER_		7
#define  	_2662_ADC_		8
#define  	_2662_PLL_		9
#define  	_2662_HDMI_	0x0A
#define  	_2662_DE_NR_	0x0B
#define  	_2662_VDP7_	0x0C
#define  	_2662_VDP8_	0x0D
#define  	_2662_VDP9_	0x0E
#define  	_2662_VDPA_	0x0F
#define  	_2662_VDPB_	0x10   //add by xyw 071025

#define	_IF_WR_SW		0x20
#define	_IF_WR_ADJ		0x21
#define	_IF_WR_DATA	0x22
#define	_IF_READ		0x23
#define	_TUNER_READ	0x24
#define	_TUNER_WR_CB	0x25

#define	_2314_VOL		0x30
#define	_2314_ATTL		0x31
#define	_2314_ATTR		0x32
#define	_2314_SW		0x33
#define	_2314_BASE		0x34
#define	_2314_TREBLE	0x35

#define	_2662_SYNC		0x40
#define	_DISP_			0x50

//#define	_NULL_			0X77
unsigned char idata ucstop = 0; 

	do
	{

		if(MCU_I2C_STATUS_FF27& 0x80)							// detect write end
		{
			switch(MCU_DDCRAM_VGA[0])
			{
				case _LOOP_:
					ucstop =0;

					break;
				case _STOP_:
					ucstop = 1;
					break;
				case _EDID_:                     
                      
  
					break;
				case _SCALER_:
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
				
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);
					}	
					break;
				case _TCON_:
// **20080510**
//support TCON R/W through debugtool.exe
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerSetByte(_TCON_ADDR_PORT_8B, MCU_DDCRAM_VGA[2]);
						CScalerRead(_TCON_DATA_PORT_8C, 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(_TCON_ADDR_PORT_8B, MCU_DDCRAM_VGA[2]);
						CScalerSetByte(_TCON_DATA_PORT_8C, MCU_DDCRAM_VGA[3]);
					}	
// **20080510**
					break;
				case _DISP_:
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerSetByte(_DISP_ACCESS_PORT_2A, MCU_DDCRAM_VGA[2]);
						CScalerRead(_DISP_DATA_PORT_2B, 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(_DISP_ACCESS_PORT_2A, MCU_DDCRAM_VGA[2]);
						CScalerSetByte(_DISP_DATA_PORT_2B, MCU_DDCRAM_VGA[3]);
					}	
					break;


				case _2662_SYNC:
// **20080515**
//support SYNC processor R/W through debugtool.exe
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerGetDataPortByte(_SYNC_PROC_ACCESS_PORT_5C, MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetDataPortByte(_SYNC_PROC_ACCESS_PORT_5C, MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);
						CScalerSetDataPortByte(_SYNC_PROC_ACCESS_PORT_5C, _SYNC_BR_CLAMP_START_02, 0x04);
					}	
// **20080515**
					break;
				case _ADC_:
				case _2662_ADC_:
					CScalerPageSelect(_PAGE0);

					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);

					}	

					break;					
				case _2662_PLL_:
					CScalerPageSelect(_PAGE1);

					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);

					}	

					break;					
				case _2662_DE_NR_:
					CScalerPageSelect(_PAGE6);

					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);

					}	

					break;					
				case _MCUFLASH_://page 2
						CScalerPageSelect(_PAGE7);			
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);
					}

					break;
				case _EEPROM_:
						CScalerPageSelect(_PAGEB);			
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);

					}
					break;
//debug for video source  page8 page9 pagea
//-----------------------------------------------------------------------------------------------------------------------
				case _2662_VDP7_:
						CScalerPageSelect(_PAGE8);	
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);

					}

					break;
				case _2662_VDP8_:
						CScalerPageSelect(_PAGE8);	
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);

					}

					break;
				case _2662_VDP9_:
					CScalerPageSelect(_PAGE9);			
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);

					}	

					break;
				case _2662_VDPA_:
					CScalerPageSelect(_PAGEA);	
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);

					}
					break;

                         //add by xyw 071025
				case _2662_VDPB_:
					CScalerPageSelect(_PAGEB);	
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						CScalerRead(MCU_DDCRAM_VGA[2], 1, &pData[13], _NON_AUTOINC);
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CScalerSetByte(MCU_DDCRAM_VGA[2], MCU_DDCRAM_VGA[3]);

					}
					break;	
					
#if _TUNER_TYPE!=_TUNER_NONE
//-----TUNER DEBUG START-------------------------------------------------------------------------------------------------------------------					
#if 1//_TUNER_TYPE != _TSN3311_S25
{
extern BIT CTunerIFWrite(BYTE ucIF_addr, BYTE ucIF_value);
extern UINT8 CTunerRead(void);

				case _IF_READ:
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						pData[13] = CTunerRead();
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					break;
				case _IF_WR_SW:
					if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CTunerIFWrite(_TUNER_B_ADDR, MCU_DDCRAM_VGA[3]);
					}
					else if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CTunerIFWrite(_TUNER_B_ADDR, MCU_DDCRAM_VGA[3]);
					}
					break;
				case _IF_WR_ADJ:
					if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CTunerIFWrite(_TUNER_C_ADDR, MCU_DDCRAM_VGA[3]);
					}
					break;
				case _IF_WR_DATA:
					if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						CTunerIFWrite(_TUNER_E_ADDR, MCU_DDCRAM_VGA[3]);
					}
					break;
}
#endif	//#if _TUNER_TYPE != _TSN3311_S25
#if (_TUNER_TYPE == _ET5C511_TDA9886ST||_TUNER_TYPE == _ET6T1E_TDA9886ST||_TUNER_TYPE == _GDC8F2D_106L)
				case _TUNER_READ:
					if(MCU_DDCRAM_VGA[1] == _DEBUGREAD)
					{
						extern UINT8 CTunerReadStatus(void);
						pData[13] = CTunerReadStatus();
						MCU_DDCRAM_VGA[10] = pData[13];
					}
					break;
#endif	//#if _TUNER_TYPE == _ET5C511_TDA9886ST
				case _TUNER_WR_CB:
					if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						WORD freq;
						
						if (GET_TV_AFC_ENABLE())
							freq = stTvPdb.pdb;
						else
							freq = stTvPdb.pdb + ucTvFinetuneData-50;
						
						if (stTvPdb.Afc&0x3f)
						{
							if (1)//(IS_AFC_ON(afc))	//0 is ON
							{
								if (stTvPdb.Afc&_BIT6)	//0=+, 1=-
									freq -= stTvPdb.Afc&0x3f;
								else
									freq += stTvPdb.Afc&0x3f;
							}
						}

						pData[0] = (BYTE)((freq>>8)& 0x7f);
						pData[1] = (BYTE)(freq & 0xff);
						pData[2] = MCU_DDCRAM_VGA[3];
						if (freq<_VH_HIGH_STAR)
							pData[3]	 = _VHFL_BAND;
						else if (freq<_UHF_STAR)
							pData[3]	 = _VHFH_BAND;
						else if (freq<_UHF_END)
							pData[3]	 = _UHF_BAND;
						else
							pData[3]	 = _VHFL_BAND;

						do {
							CI2cStart(_TUNER_WRITE_ADDRESS);
							CI2cSendByte(pData[0]);
							CI2cSendByte(pData[1]);
							CI2cSendByte(pData[2]);
							CI2cSendByte(pData[3]);
							CI2cStop();
						}while(0);
					}
					break;
//-----TUNER DEBUG END-------------------------------------------------------------------------------------------------------------------					
#endif	//#if _TUNER_TYPE!=_TUNER_NONE


#if _VOLUME_PT2314_SUPPORT
				case _2314_VOL:
				case _2314_ATTL:
				case _2314_ATTR:
				case _2314_SW:
				case _2314_BASE:
				case _2314_TREBLE:
					if(MCU_DDCRAM_VGA[1] == _DEBUGWRITE)
					{
						switch (MCU_DDCRAM_VGA[0])
						{
							case _2314_VOL:
								pData[0] = 0x00 | (MCU_DDCRAM_VGA[3]&0x3f);
								break;
							case _2314_ATTL:
								pData[0] =  0xc0 | (MCU_DDCRAM_VGA[3]&0x1f);
								break;
							case _2314_ATTR:
								pData[0] = 0xe0 | (MCU_DDCRAM_VGA[3]&0x1f);
								break;
							case _2314_SW:
								pData[0] = 0x40 | (MCU_DDCRAM_VGA[3]&0x1f);
								break;
							case _2314_BASE:
								pData[0] = 0x60 | (MCU_DDCRAM_VGA[3]&0x0f);
								break;
							case _2314_TREBLE:
								pData[0] = 0x70 | (MCU_DDCRAM_VGA[3]&0x0f);
								break;
						}
						do {
							CPt2314Start();
						   	CPt2314DataIn(0x88);
							CPt2314DataIn(pData[0]);
						   	CPt2314Stop();
						}while(0);
					}
					break;
#endif	//#if _VOLUME_PT2314_SUPPORT
				default:
					break;
		
			}
			MCU_DDCRAM_VGA[0] = _NULL_;
			MCU_I2C_STATUS_FF27=0;    
		}

		
	}while(ucstop);
#endif

#if(_DEBUG_TOOL == _ISP_FOR_MYSON)

    BYTE halt = 0;

    do
    {
        if(bRunCommand)
        {
            switch(ucDdcciData[0])
            {
                case 0x80:
                    halt = ucDdcciData[1];
                    break;

                case 0x41:
                    CScalerRead(ucDdcciData[1], 1, pData, _NON_AUTOINC);
                    TxBUF= pData[0];
                    break;

                case 0x44:
                    CI2cRead(ucDdcciData[2], ucDdcciData[1], 1, pData);
                    TxBUF = pData[0];
                    break;

                default:
                    switch (ucDdcciData[0] & 0x0F)                //Write command
                    {
                        case 0x00:
                            CScalerSetByte(ucDdcciData[2], ucDdcciData[1]);
                            break;

                        case 0x01:
                        case 0x02:                          // for RTD & I2c Device
                            ucDdcciData[2] = ucDdcciData[1];
                            break;

                        case 0x06:
                        case 0x03:                          //for I2c Device
                            ucDdcciData[3] = ucDdcciData[1];
                            break;

                        case 0x04 :                         //for I2c Device
                            CI2cWrite(ucDdcciData[2], ucDdcciData[1], 1, ucDdcciData+3);
                            break;
                    }
                    break;
			}

            bRunCommand=0;
            ucDdcciCommandNumber = 0;
        }
    }
    while(halt != 0);

#endif  // End of #if(_DEBUG_TOOL == _ISP_FOR_MYSON)

#if _VGA_EDID
//20080421, DDC support for VGA. start...
       do
	{
		if(MCU_I2C_STATUS_FF27&0x80)
		{
			MCU_I2C_STATUS_FF27=0;
			CTimerDelayXms(500);//MIN 25-35MS for safety to set 500MS
			CEepromSaveFlashEdidData();
        	}

        }while(0);
//20080421, DDC support for VGA. start...
#endif
//20080421, DDC support for digital port 0. start...
#if _HDMI_EDID_RW
	do
	{
		if(MCU_I2C_STATUS_FF27&_BIT6)
		{
			MCU_I2C_STATUS_FF27=0;
			CTimerDelayXms(500);//MIN 25-35MS for safety to set 500MS
			CEepromSaveFlashEdidHdmiData();
		}
        }while(0);
#endif	//#if _HDMI_EDID_RW
//20080421, DDC support for digital port 0. end...

}

#if(_DEBUG_TOOL == _ISP_FOR_MYSON)
//--------------------------------------------------
// Description  : ISP for Myson Initial
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CDdcciInitial(void)
{
    MCU_IICCTR_F00		=0xc0;
    //MCU_IICSTUS_F01     =0x01;
    MCU_IICSTUS_F01     =0x00;
    MCU_INTFLG_F03		=0x08;
    //MCU_IICCTR_F00		=0xc0;
    MCU_INTEN_F04		=0x60;
    MCU_CTRSLVB_F0A		=0x00;
    MCU_SLVBADR_F09		=0x80|(0x6a>>1);

    MCU_DDCCTRA1_F06	=0x00;
    MCU_SLVA1ADR_F07	=0x00;
    MCU_SLVA2ADR_F87	=0x00;
    //MCU_RCTXBBUF_F08 = 0x00;
    //MCU_RCTXBBUF_F08 = 0x38;
    EA = 1;
    EX0 = 1;
}
#endif  // End of #if(_DEBUG_TOOL == _ISP_FOR_MYSON)


#if(1/*_DEBUG_TOOL == _ISP_FOR_RTD2662_EMCU*/)
//--------------------------------------------------
// Description  : ISP for RTD2662 Embeded MCU Initial
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CDdcciInitial(void)
{

//20080421, DDC support for digital port 0. start...
#if _HDMI_EDID_RW
    MCU_I2C_SET_SLAVE_FF23 = 0x6b;
    MCU_I2C_IRQ_CTRL_FF28  = 0x3f;
    MCU_IRQ_PRIORITY_FF01 = 0x01;      //assign int1 (IRQ2)
#else	//#if _HDMI_EDID_RW
    MCU_I2C_SET_SLAVE_FF23 = 0x6a;
    MCU_I2C_IRQ_CTRL_FF28  = 0x3f;
    MCU_IRQ_PRIORITY_FF01 = 0x01;      //assign int1 (IRQ2)
#endif	//#if _HDMI_EDID_RW
//20080421, DDC support for digital port 0. end...


//20080421, DDC support for digital port 0. start...
#if _HDMI_EDID_RW
//hdmi port left:
	MCU_DVI_DDC_ENA_FF1E = 0x07;//open ddc2
	MCU_HDMI_DDC_ENA_FF2C = 0x00;//close ddc3
//hdmi port right:
//	MCU_DVI_DDC_ENA_FF1E = 0x00;//close ddc2
//	MCU_HDMI_DDC_ENA_FF2C = 0x07;//open ddc3
#else	//#if _HDMI_EDID_RW
#if((_HDMI_EDID==_ON)&&(_HDMI_DDC_CHANNEL_SELECT==_DDC2))

	MCU_DVI_DDC_ENA_FF1E = 0x03;//open ddc2
    MCU_HDMI_DDC_ENA_FF2C = 0x00;//close ddc3
    
#elif((_HDMI_EDID==_ON)&&(_HDMI_DDC_CHANNEL_SELECT==_DDC3))

	MCU_DVI_DDC_ENA_FF1E = 0x00;//close ddc2
    MCU_HDMI_DDC_ENA_FF2C = 0x03;//open ddc3
    
#else     

    MCU_DVI_DDC_ENA_FF1E = 0x00;//close ddc2
    MCU_HDMI_DDC_ENA_FF2C = 0x00;//close ddc3
    
#endif
#endif	//#if _HDMI_EDID_RW
//20080421, DDC support for digital port 0. end...

#if _VGA_EDID

	MCU_ADC_DDC_ENA_FF1B = 0x07; // open ddc1       
   
#else
	MCU_ADC_DDC_ENA_FF1B = 0x00;//close ddc1       
#endif


	
//20080421, DDC support for digital port 0. start...
#if _HDMI_EDID_RW
    MCU_I2C_CHANNEL_CTRL_FF2B = 0x01; // HDMI DDC channel
#else	//#if _HDMI_EDID_RW
    MCU_I2C_CHANNEL_CTRL_FF2B = 0x00; // ADC DDC channel
#endif	//#if _HDMI_EDID_RW
//20080421, DDC support for digital port 0. end...

	MCU_I2C_IRQ_CTRL2_FF2A  = 0x40; // Rst_Data_buf
	MCU_CTRL_FFED          = 0x80;
	//    MCU_CTRL_FFED          = 0x8c;
	EA = 1;
	EX0 = 1;
	IE = 0x8e;
	//    MCU_CTRL_FFED          = 0x8e;
}
#endif  // End of #if(_DEBUG_TOOL == _ISP_FOR_RTD2662_EMCU)



/**
 * CMiscIspDebugProc
 * Debug tool process
 * @param <none>
 * @return {none}
 *
 */
void CMiscIspDebugProc(void)
{
    CMiscIspack();
    CMiscKingmice();
#if(_DEBUG_TOOL == _RS232)

#if (_DEBUG_Printf_OUTPUT_CONTROL == _ON || _HDCP_WR_BY_RS232 == _ON)
	if (fTest == 1)
	{
		fTest = 0;
		CUartHandler();
	}
#endif	//#if (_DEBUG_Printf_OUTPUT_CONTROL == _ON || _HDCP_WR_BY_RS232 == _ON)

#endif
}

#if(_GOLDEN_MODE != _NONE)
//--------------------------------------------------
// Description  : Golden Mode
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void CMiscGoldenMode(void)
{
    // Initial MCU
	CMcuInitial();

    // Initial timer events
    CTimerInitialTimerEvent();

    // Check eeprom and load eeprom settings
    CEepromStartupCheck();

    // Set panel select settings
    CMiscSelectPanel();

#if(_RESET_TYPE == _RESET_IN)
    // Scaler reset
    CScalerResetInForResetIn();
#endif  // End of #if(_RESET_TYPE == _RESET_IN)

    // Initial scaler settings
    //CScalerInitial();

#if(_DEBUG_TOOL == _RS232)
    // Initial uart settings
    CUartInitial();
#endif  // End of #if(_DEBUG_TOOL == _RS232)

    // Initial key scan status
    //CKeyInitial();

	CTimerDelayXms(1000);

    // Initial source input port settings
    //CSourceInitialInputPort();

    // Initial user settings
    //CMainUserInitial();
	CScalerCodeW(tGOLDEN_TABLE);
	//stModeInfo.ModeCurr = _MODE_1024x768_60HZ;
	//CModeMeasureData();
	//CModeSetupModeVGA();

    CPowerPanelPowerOn();
    CPowerLightPowerOn();

	CMiscClearStatusRegister();

    while(_TRUE)
    {
        CMiscIspDebugProc();
    }

}
#endif  //End of #if(_GOLDEN_MODE != _NONE)



//premier
//-----------------------------------------------
// led message
//-----------------------------------------------
void LED_Message(UINT8 msg)
{
	static UINT8 FlashCount;
	static bit bledstatus = 0;

	switch(msg)
	{
		case _LED_MSG_RED:
			SET_LEDG(_LED_OFF);
			SET_LEDR(_LED_ON);
			break;
		case _LED_MSG_GREEN:
			SET_LEDG(_LED_ON);
			SET_LEDR(_LED_OFF);
			break;
		case _LED_MSG_YELLOW:
			SET_LEDG(_LED_ON);
			SET_LEDR(_LED_ON);
			break;
		case _LED_MSG_BLACK:
			SET_LEDG(_LED_OFF);
			SET_LEDR(_LED_OFF);
			break;
		case _LED_MSG_FLASH_RED:

				//apply with second tick:
				SET_LEDG(_LED_OFF);
				if(bledstatus)
					SET_LEDR(_LED_OFF);
				else
					SET_LEDR(_LED_ON);
				bledstatus = ~bledstatus;
			break;
		case _LED_MSG_FLASH_GREEN:

				//apply with second tick:
				SET_LEDR(_LED_OFF);
				if(bledstatus)
					SET_LEDG(_LED_OFF);
				else
					SET_LEDG(_LED_ON);
				bledstatus = ~bledstatus;
			break;
		case _LED_MSG_FLASH_YELLOW:
			if(!FlashCount)
			{
				if(bledstatus)
				{
					SET_LEDG(_LED_OFF);
					SET_LEDR(_LED_OFF);
				}			
				else
				{
					SET_LEDG(_LED_ON);
					SET_LEDR(_LED_ON);
				}	
				FlashCount = 1000/20;
				bledstatus = ~bledstatus;
			}
			else
				FlashCount--;
			
			break;
		default:
			break;
	}

}

