///////////////////////////////////////////////////////////////////////////////////
//
//  STR Test Script for 3.5 F1-1D EXPERT
//
//  Script Name : EXPERT.STR 
//  History     :
//     						Version    Date     Description of changes
//     						-------  --------   --------------------------------------------------------
//      					v1.00    05-16-08    Initial release - dong gyu & JungHoon
//
////////////////////////////////////////////////////////////////////////////////////
//               Copyright (C) SEC Storage System , 2006
////////////////////////////////////////////////////////////////////////////////////
#include "strfiles.inc"

// Define Alphabet //
#define CHAR_A			65	//'A'
#define CHAR_B			66	//'B'
#define CHAR_C			67	//'C'
#define CHAR_D			68	//'D'
#define CHAR_E			69	//'E'
#define CHAR_F			70	//'F'
#define CHAR_G			71	//'G'
#define CHAR_H			72	//'H'
#define CHAR_I			73	//'I'
#define CHAR_J			74	//'J'
#define CHAR_K			75	//'K'
#define CHAR_L			76	//'L'
#define CHAR_M			77	//'M'
#define CHAR_N			78	//'N'
#define CHAR_O			79	//'O'
#define CHAR_P			80	//'P'
#define CHAR_Q			81	//'Q'
#define CHAR_R			82	//'R'
#define CHAR_S			83	//'S'
#define CHAR_T			84	//'T'
#define CHAR_U			85	//'U'
#define CHAR_V			86	//'V'
#define CHAR_W			87	//'W'
#define CHAR_X			88	//'X'
#define CHAR_Y			89	//'Y'
#define CHAR_Z			90	//'Z'

#define CHAR_a			97	//'a'
#define CHAR_b			98	//'b'
#define CHAR_c			99	//'c'
#define CHAR_d			100	//'d'
#define CHAR_e			101	//'e'
#define CHAR_f			102	//'f'
#define CHAR_g			103	//'g'
#define CHAR_h			104	//'h'
#define CHAR_i			105	//'i'
#define CHAR_j			106	//'j'
#define CHAR_k			107	//'k'
#define CHAR_l			108	//'l'
#define CHAR_m			109	//'m'
#define CHAR_n			110	//'n'
#define CHAR_o			111	//'o'
#define CHAR_p			112	//'p'
#define CHAR_q			113	//'q'
#define CHAR_r			114	//'r'
#define CHAR_s			115	//'s'
#define CHAR_t			116	//'t'
#define CHAR_u			117	//'u'
#define CHAR_v			118	//'v'
#define CHAR_w			119	//'w'
#define CHAR_x			120	//'x'
#define CHAR_y			121	//'y'
#define CHAR_z			122	//'z'

// Define //
#define POSTFIX_SC 		83	//'S'
#define POSTFIX_HT 		70	//'F'
#define POSTFIX_BI 		66	//'B'
#define POSTFIX_SMT 	80	//'P'
#define POSTFIX_MAIN	77	//'M'

#define POSTFIX_sc 		115	//'s'
#define POSTFIX_ht 		102	//'f'
#define POSTFIX_bi 		98	//'b'
#define POSTFIX_smt 	112	//'p'
#define POSTFIX_main	109	//'m'
#define POSTFIX_prc	    116	//'t'

#define TRK_DFT_REAL  0
#define TRK_DFT_SF    2
#define TRK_DFT_DATA  3
#define TRK_DFT_SDS   6
#define DEF_MAX_ZN    25
#define DEF_MAX_HD    2

#define HEAD_DMR      0x01
#define HEAD_FAIL     0x02
#define HEAD_SWAP     0x03

#define STR_PORT_SCRN 0


struct MES_EXPERT_INFO
{
    int  MES_STW_COUNT;
    int  MES_PCBA_USED_COUNT;
    int  MES_BI_COUNT;
    int  MES_REASSY_COUNT;
    char MES_DISK_DMR;
    char MES_HEAD_DMR;
    char MES_RWK_FLAG;
    char MES_MCODE[8];
    char MES_PREV_ERROR[8];
    char MES_DEBUG[8];
    int  MES_FT_COUNT;
}; //str_mesgetexpertinfo(MES_EXPERT_INFO);
    
int  INVALID_LED;
int  DEBUG;
char BI_FWREV[16];
int  HEAD_INFO;
int  BI_STATUS;
int  BI_ERRORCODE;
int  BI_LASTCMD;
int  BI_LASTSTEP;
int  BI_AVILZONE0;
int  BI_AVILZONE1;
int  BI_AVILZONE2;
int  BI_AVILZONE3;
int  BI_AVILZONE4;
int  BI_AVILZONE5;

int   MAX_HEAD, MAX_ZONE, CAPA, RPM, MASS, HDDETECT, MES_CODE;                    //a unit : 1 ~ 6, 1 ~ 25
int   RDY_MRR[DEF_MAX_HD],RDY_FODRES[DEF_MAX_HD],RDY_WRRES[DEF_MAX_HD]; //CHK_HEAD
int   TRKDFT_LIST_CNT, SRVDFT_LIST_CNT;                                 //CHK_GetTrackandServoDefect
int   DEF_TTLDEFCNT, DEF_MAXHDRATE, DEF_MAXDEFTARGETHD;                 //CHK_DefTargetHDandMaxRatio
int   TRKSRV_TTLDEFCNT, TRKSRV_MAXHDRATE, TRKSRV_MAXDEFTARGETHD;        //CHK_TrkSrvTargetHDandMaxRatio
int   MAXFODFH, MINFODFH, MAXFODTARGETHD, MINFODTARGETHD;               //CHK_FODMaxandMinValue
float MAXREFBER, MAX747BER, MAX10SBER;                                  //CHK_TPI74710SData
int   MAXREFTARGETHD, MAX747TARGETHD, MAX10STARGETHD;                   //CHK_TPI74710SData
int   MAXDEFTARGETHD, MAXHDRATE;
int   FORCINGDECISION, KILLFLAG;

int  gD_HD,gD_DISK,gD_STW;
char BAD_DISK_3B[DEF_MAX_HD];

char  engfw[16];
char  RDBUF[512];
char  EXPLOG[128];
char  REPAIRCODE[16];

int   TBL_ZONE[DEF_MAX_HD][DEF_MAX_ZN];
//int   DLISTCNT[DEF_MAX_HD][DEF_MAX_ZN];      // 6*25
//int   ELogListCnt[DEF_MAX_HD][DEF_MAX_ZN];   // 6*25
int   TRKSRVCNT[DEF_MAX_HD][DEF_MAX_ZN];     // 6*25
int   SRVCONTINOUS[DEF_MAX_HD];    // 6hd  VList Continous 0 = Not Continous, 1 = have a Continous
int   DEFCONTINOUS[DEF_MAX_HD];    // 6hd  DList Continous 0 = Not Continous, 1 = have a Continous

int   HDMAXFODFH[DEF_MAX_HD];
int   HDMINFODFH[DEF_MAX_HD];
int   HDMAX2NDFODFH[DEF_MAX_HD];
int   HDMIN2NDFODFH[DEF_MAX_HD];

int   READCNT10S[DEF_MAX_HD][DEF_MAX_ZN];    //6*25
int   ERRCNT10S[DEF_MAX_HD][DEF_MAX_ZN];     //6*25
int   FOD_FH[DEF_MAX_HD][DEF_MAX_ZN];
int   ZONESKIP[DEF_MAX_HD];        //6hd
int   ZONEUSED[DEF_MAX_HD];        //6hd

//int   TBL_PARAM_MRR[DEF_MAX_HD][12];
//int   TBL_PARAM_SGAIN[DEF_MAX_HD][12];
//int   TBL_PARAM_CSM[DEF_MAX_HD][12];
int   TBL_DLIST[DEF_MAX_HD][DEF_MAX_ZN];
int   TBL_ELOG[DEF_MAX_HD][DEF_MAX_ZN];
int   TTL_DLISTandELOG[DEF_MAX_HD];

// continous check variable
int   CIRCLE[DEF_MAX_HD][2000]; //6hd*2000sector

int REFCSMCnt[4][DEF_MAX_HD][21];     //6*16*4
int ATWCSMCnt[4][DEF_MAX_HD][21];     //6*16*4
int HIGHREFCSM[DEF_MAX_HD];
int HIGHATWCSM[DEF_MAX_HD];
int HIGHESTREFCSM[DEF_MAX_HD];
int HIGHESTATWCSM[DEF_MAX_HD];

int FINAL_CAPA[DEF_MAX_HD];
int ORG_CAPA[DEF_MAX_HD];
int FINAL_TPI[DEF_MAX_HD];
int ORG_TPI[DEF_MAX_HD];
int ORG_BPI[DEF_MAX_HD][DEF_MAX_ZN];
int ORG_BPIAVG[DEF_MAX_HD];
int ORG_BPIBER[DEF_MAX_HD][DEF_MAX_ZN];
int ORG_BPIAVGBER[DEF_MAX_HD];
int FINAL_BPI[DEF_MAX_HD][DEF_MAX_ZN];
int FINAL_BPIAVG[DEF_MAX_HD];
int FINAL_BPIBER[DEF_MAX_HD][DEF_MAX_ZN];
int FINAL_BPIAVGBER[DEF_MAX_HD];

int  TBL_TPI747[90];  //MAX_HEAD*15    float can not used dimansion
int  fstAVGBER[6];
//int f2ndAVGber[DEF_MAX_HD];   //F1 not uesd
//int f3rdAVGber[DEF_MAX_HD];   //F1 not uesd
//int f4thAVGber[DEF_MAX_HD];   //F1 not uesd
int  MIN_SGAIN[DEF_MAX_HD];
int  MAX_SGAIN[DEF_MAX_HD];
int  AVG_SGAIN[DEF_MAX_HD];

void Check_Kill_HeadNum();

int   MGET(int mcid, int offset); //F1 MCFS
void  CHK_BISTATUS(int type);
void  CHK_DList();
void  CHK_ELogList();
void  CHK_HTDList(int nThres);
void  CHK_HTELogList();
void  CHK_DefTargetHDandMaxRatio();
void  CHK_GetBER();
void  CHK_GetFOD();
void  CHK_FODMaxandMinValue(int ntargetHd);
void  CHK_GetBPIandTPI();
void  CHK_GetREFandATWCSM();
void  CHK_GetTPIand747();
void  CHK_TPI74710SData(int nHd);
void  CHK_Head(int flag);     //int RDY_MRR[DEF_MAX_HD]
int   CHK_HeadDetect(int type);
void  CHK_GetTrackandServoDefect(int ntype);     // Get TRKSRVCNT[144];, ntype 1 : 0x2812 only TLIST Access
void  CHK_TrkSrvTargetHDandMaxRatio();
void  CHK_DiskDetect(int thres);
void  CHK_DiskDefect_L3B(int pct);

void  ReadZoneInfo();
int   ContinousCheck(int nHd, int nCyl, int nSN, int DCyl, int DSN, int nThres);
int   GetCylMark(int nHd,int nNum);
int   GetCntMark(int nHd,int nNum);
void  SetCircleMark(int nHd,int nNum,int nCyl,int nCnt);
void  ExpertDecision(int type);
int   EXPF1KILL(int hd);
int   CHK_MESHistory(int ntype, int module);
void  MakeMassData();
void  OnMassError();
void  NotReady();
void  InitReadyError(int flag);
void  SetDecisionHeadSwap();
void  SetDecisionHeadTest();
void  SetDecisionHead(int nNum,int Type);
void  SetDecisionDisk(int nNum);
void  SetDecisionSTW(int nNum);
int   GetDecisionHead(int nNum);
int   GetDecisionDisk(int nNum);
int   GetDecisionSTW(int nNum);
int   GetNumOfZone(int nCyl,int nHd);

void  Set_TestCondition(int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry);
int   IsSeekFail(int nstartCyl, int ntargetCyl, int nstartHd, int ntargetHd, int nMode, int nLoop, float nthres);
int   IsBERFail(int ntargetCyl,int ntargetHd,int nLoop,float nthres);
void  IsServoGainFail(int ntargetCyl,int ntargetHd,int nMode,int nLoop,int nPgain,int mode);

int   EXP_SCFail();
void  EXP_HTFail();
void  EXP_BURN();
void  EXP_BIEND();
void  EXP_CONT();
void  EXP_FTFail();
void  EXP_BIFail();
void  EXP_L1AXX();
void  EXP_L1B01();
void  EXP_L2105();
void  EXP_L28XX();
void  EXP_L1704();

void  exe_BI(int minute, int type);
void  Restore_MESmodel();

void  xqerror();
void  reset_1st();
void  reseterror();
void  dummyerror();
void  pkerror();
int   DOWNLOAD_EXPSCCODE(int delay, int type);
int   DOWNLOAD_EXPBICODE(int delay);
void  Send_Debug_info();
char  Get3rdCode(char ch, int type);


void main()
{
    int  i, tmp, nRet, ret, i, ready, nHd, temp, nCnt, cmdlen;
    int  led, icyl, ihd,nKill;
    char debug[16];
    char engfw[16];
    char model[16];
    char pn[16];

    memset(debug,0,0,16);
    memset(engfw,0,0,16);
    memset(model,0,0,16);
    memset(pn,0,0,16);
    
    DEBUG = 1;
    HEAD_INFO = 0;
    CAPA = 0;
    HDDETECT = 0;
    ready = 0;
    FORCINGDECISION = 0;
    KILLFLAG = 0;
    
    //memset(TBL_ZONE,0,0,DEF_MAX_HD*DEF_MAX_ZN);
    //memset(TRKSRVCNT,0,0,DEF_MAX_HD*DEF_MAX_ZN);
    //memset(TBL_ELOG,0,0,DEF_MAX_HD*DEF_MAX_ZN);
    //memset(TBL_TPI747,0,0,DEF_MAX_HD*15);
    //memset(f1stAVGBER,0,0,6);
    //memset(f2ndAVGber,0,0,DEF_MAX_HD);
    //memset(f3rdAVGber,0,0,DEF_MAX_HD);
    //memset(f4thAVGber,0,0,DEF_MAX_HD);

    //memset(TBL_PARAM_MRR,0,0,DEF_MAX_HD*15);   //DEF_MAX_HD * 15
    //memset(TBL_PARAM_SGAIN,0,0,DEF_MAX_HD*15); //DEF_MAX_HD * 15
    
    //memset(CIRCLE,0,0,MAX_HEAD*2000*4);
    //memset(MES_PREV_ERROR, 0, 0, 6);  

    gD_HD = 0;
    gD_DISK = 0;
    gD_STW = 0;
    MAX_HEAD = DEF_MAX_HD;
    MAX_ZONE = DEF_MAX_ZN;

    for(i=0; i<=DEF_MAX_HD; i++)
    {
        SRVCONTINOUS[DEF_MAX_HD]=0;
        DEFCONTINOUS[DEF_MAX_HD]=0;
        HDMAXFODFH[DEF_MAX_HD]=0;
        HDMINFODFH[DEF_MAX_HD]=0;
        HDMAX2NDFODFH[DEF_MAX_HD]=0;
        HDMIN2NDFODFH[DEF_MAX_HD]=0;
    }

    MES_EXPERT_INFO.MES_STW_COUNT = 0;
    MES_EXPERT_INFO.MES_PCBA_USED_COUNT=0;
    MES_EXPERT_INFO.MES_BI_COUNT=0;
    MES_EXPERT_INFO.MES_REASSY_COUNT=0;
    MES_EXPERT_INFO.MES_DISK_DMR=CHAR_P;
    MES_EXPERT_INFO.MES_HEAD_DMR=CHAR_P;
    MES_EXPERT_INFO.MES_RWK_FLAG=CHAR_P;
    strcpy(MES_EXPERT_INFO.MES_MCODE,"0000");
    strcpy(MES_EXPERT_INFO.MES_PREV_ERROR,"NNNN");
    strcpy(MES_EXPERT_INFO.MES_DEBUG,"NONE");
    
    //Start Main()
    str_mesoff();
    str_setbyteswap(1);
    str_setdumpsectorsize(1699); //for ARM Core

    siolog(1);      //SIO log ON
    power(0);
    delays(2);
	power(1);
    poststatus("Power ON");
    //for(i=0; i<90; i++)
    //{
        //sputd(debug,"Remain:%d",90-i);
        //delays(1);
    	delays(90);
    	//poststatus(debug);
    //}
    //str_nopoweroff(1);
    str_setdumpsectorsize(1710);
    //check invalid led
    str_getinvalidled(INVALID_LED);
    sputd(EXPLOG,"INVALID_LED:%x",INVALID_LED);
    userfilelog(EXPLOG);
    for(nCnt=0; nCnt<5; nCnt++)
    {
        if(INVALID_LED == 0x0000)
        {
            delays(1);
            str_getinvalidled(INVALID_LED);
            sputd(EXPLOG,"INVALID_LED:%x",INVALID_LED);
        }
        else
        {
            sputd(EXPLOG,"INVALID_LED:%x",INVALID_LED);
            break;
        }
    }
    nKill = EXPF1KILL(0);
    if(nKill == 0) // Hd kill ok
    {
        //str_exp_setdectype(2, 3);
        sputd(EXPLOG,"Kill_HD:%d",1);
        poststatus(EXPLOG);
        stoptest();
    }
    poststatus("KillFail");
    stoptest();
    
    if(INVALID_LED != 0x0000)
    {
        if(INVALID_LED == 0x1102)
        {
            str_debugmode();
            ONERROR(dummyerror);
            reset();
            ONERRORCLEAR();
            
            //Servo Copy Code D/L
            poststatus("SC Code D/L");
            userfilelog("LED11XX F2EG-2D 500G Special SC CODE D/L");
            reset();
            sputs(EXPLOG,"EXSCCODE:%s",EXSCCODE);
            userfilelog(EXPLOG);
            dw(EXSCCODE);
            str_checkprompt("BOOT>");
            delays(50);
            power(0);
            delays(3);
            power(1);
            delays(40);
            ONERROR(reseterror);
            reset();
            ONERRORCLEAR();
            str_debugmode();
            nRet = CHK_HeadDetect(3);
            if(nRet != 0)
            {
                ExpertDecision(1);
                stoptest();
            }
            
            power(0);
            delays(2);
            power(1);
            poststatus("Re-Power ON");
            delays(90);
            str_getinvalidled(INVALID_LED);
            sputd(EXPLOG,"INVALID_LED:%x",INVALID_LED);
            userfilelog(EXPLOG);
            if(INVALID_LED == 0x0000)
            {
                CHK_MESHistory(2,0);     //type -> 1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                ExpertDecision(0);
                stoptest();
            }
            else
            {
                str_exp_setdectype(1, 0);
                sputd(EXPLOG,"L%x OOB",INVALID_LED);
                poststatus(EXPLOG);
                stoptest();
            }
        }
        if((INVALID_LED == 0x1701) || (INVALID_LED == 0x1702) || (INVALID_LED == 0x1703) || (INVALID_LED == 0x1102))    //too many parking during process
        {
            str_exp_setdectype(1, 0);
            poststatus("L17xx OOB");
            stoptest();
        }
        if(INVALID_LED == 0x1705)    //Spin up too long
        {
            userfilelog("LED0x1705 PCBA Screen");
            str_exp_setdectype(8, 1);
            poststatus("L0x1705 PCBA");
            strcpy(REPAIRCODE,"PAA018");
            Send_Debug_info();
            stoptest();
        }
        if((INVALID_LED == 0x2A03) || (INVALID_LED == 0x2A04) || (INVALID_LED == 0x2A05) || (INVALID_LED == 0x2A06))
        //0x2A03:chip rev error    0x2A04:NLS Register error   0x2A05:Linked List Register error    0x2A06:Free Fall Sensor is out of order
        {
            userfilelog("LED0x2AXX PCBA Screen");
            str_exp_setdectype(8, 1);
            poststatus("PCBA Fail");
            strcpy(REPAIRCODE,"PAA018");
            Send_Debug_info();
            stoptest();
        }
        if((INVALID_LED == 0x2B01) || (INVALID_LED == 0x2B02) || (INVALID_LED == 0x2B03) || (INVALID_LED == 0x2B04) ||
           (INVALID_LED == 0x2B06) || (INVALID_LED == 0x2B07) || (INVALID_LED == 0x2B08) || (INVALID_LED == 0x2B09))
         //0x2B01:CPURAM_ERR    0x2B02:SEQRAM_ERR    0x2B03:BUFRAM_ERR    0x2B04:FLASH_ERR
         //0x2B06:Servo SPIRAM/YRAM/XRAM error  0x2B07:ECCRAM_ERR    0x2B08:Hybrid ONENAND flash error    0x2B09:BufferAlloc reuturned NULL
        {
            userfilelog("LED0x2BXX PCBA Screen");
            str_exp_setdectype(8, 1);
            poststatus("PCBA Fail");
            strcpy(REPAIRCODE,"PAA002");
            Send_Debug_info();        
            stoptest();
        }
        
        str_debugmode();
        ONERROR(NotReady);
        reset();
        ONERRORCLEAR();
        ONERROR(NotReady);
        id(0);
        ONERRORCLEAR();
        str_getengfw(engfw);
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            temp = GetDecisionHead(nHd);
            if(temp != 0)	ExpertDecision(1);
        }
        
        if(INVALID_LED == 0x1102)
        {
            str_exp_setdectype(1, 0);
            poststatus("L1102 OOB");
            stoptest();
        }
        if((INVALID_LED & 0xFF00) == 0x1100)    //LATCH / Ramp
        {
            if(engfw[7] == POSTFIX_sc || engfw[7] == POSTFIX_SC)
            {
                //Servo Copy Code D/L
                poststatus("SC Code D/L");
                userfilelog("LED11XX F1-3D 1TB Special SC CODE D/L");
                reset();
                sputs(EXPLOG,"EXSCCODE:%s",EXSCCODE);
                userfilelog(EXPLOG);
                dw(EXSCCODE);
                str_checkprompt("BOOT>");
                delays(50);
                power(0);
                delays(3);
                power(1);
                delays(40);
                ONERROR(reseterror);
                reset();
                ONERRORCLEAR();
                str_debugmode();
                nRet = CHK_HeadDetect(3);
                if(nRet != 0)
                {
                    ExpertDecision(1);
                    stoptest();
                }
                nRet = EXP_SCFail();
                if(nRet != 0)
                {
                    ExpertDecision(1);
                    stoptest();
                }
            }
            else
            {
                SetDecisionHeadTest();
                userfilelog("L0x11XX HSA Test");
                stoptest();
            }
        }
        if(((INVALID_LED & 0xFF00) == 0x1300) || ((INVALID_LED & 0xFF00) == 0x1400) || ((INVALID_LED & 0xFF00) == 0x1500))
        //SPSTW (offline), ASTW, Offline STW
        {
            if(engfw[7] == POSTFIX_sc || engfw[7] == POSTFIX_SC)
            {
                //Servo Copy Code D/L
                poststatus("SC Code D/L");
                userfilelog("F1-3D 1TB Special SC CODE D/L");
                reset();
                sputs(EXPLOG,"EXSCCODE:%s",EXSCCODE);
                userfilelog(EXPLOG);
                dw(EXSCCODE);
                str_checkprompt("BOOT>");
                delays(50);
                power(0);
                delays(3);
                power(1);
                delays(40);
                ONERROR(reseterror);
                reset();
                ONERRORCLEAR();
                str_debugmode();
                nRet = CHK_HeadDetect(3);
                if(nRet != 0)
                {
                    ExpertDecision(1);
                    stoptest();
                }
                nRet = EXP_SCFail();
                if(nRet != 0)
                {
                    ExpertDecision(1);
                    stoptest();
                }
            }
            else
            {
                SetDecisionHeadTest();
                userfilelog("L0x11XX HSA Test");
                stoptest();
            }
        }
        if(INVALID_LED == 0x1702)    //too many parking during process
        {
            str_exp_setdectype(1, 0);
            poststatus("L1702 OOB");
            stoptest();
        }
        if(INVALID_LED == 0x1704)    //too many parking during process
        {
            //Servo Copy Code D/L
            poststatus("SC Code D/L");
            userfilelog("F1-1D 320G Special SC CODE D/L");
            reset();
            sputs(EXPLOG,"EXSCCODE:%s",EXSCCODE);
            userfilelog(EXPLOG);
            dw(EXSCCODE);
            str_checkprompt("BOOT>");
            delays(50);
            power(0);
            delays(3);
            power(1);
            delays(40);
            ONERROR(reseterror);
            reset();
            ONERRORCLEAR();
            str_debugmode();
            nRet = CHK_HeadDetect(3);
            if(nRet != 0)
            {
                ExpertDecision(1);
                stoptest();
            }
            EXP_L1704();
            ExpertDecision(1);
            stoptest();
        }
        if(INVALID_LED == 0x1705)    //Spin up too long
        {
            userfilelog("LED0x1705 PCBA Screen");
            str_exp_setdectype(8, 1);
            poststatus("L0x1705 PCBA");
            strcpy(REPAIRCODE,"PAA018");
            Send_Debug_info();
            stoptest();
        }
        if((INVALID_LED == 0x2A01) || (INVALID_LED == 0x2A02))
        //0x2A01 preamp is out of order , 0x2A02 preamp voltage regulator err
        {
            //Servo Copy Code D/L
            poststatus("SC Code D/L");
            userfilelog("F1-1D 320G Special SC CODE D/L");
            reset();
            sputs(EXPLOG,"EXSCCODE:%s",EXSCCODE);
            userfilelog(EXPLOG);
            dw(EXSCCODE);
            str_checkprompt("BOOT>");
            delays(50);
            power(0);
            delays(3);
            power(1);
            delays(40);
            ONERROR(reseterror);
            reset();
            ONERRORCLEAR();
            str_debugmode();
            nRet = CHK_HeadDetect(3);
            if(nRet != 0)
            {
                ExpertDecision(1);
                stoptest();
            }
                SetDecisionHeadTest();
                userfilelog("L0x2AXX HSA Test");
                stoptest();
        }
        if((INVALID_LED == 0x2A03) || (INVALID_LED == 0x2A04) || (INVALID_LED == 0x2A05) || (INVALID_LED == 0x2A06))
        //0x2A03:chip rev error    0x2A04:NLS Register error   0x2A05:Linked List Register error    0x2A06:Free Fall Sensor is out of order
        {
            userfilelog("LED0x2AXX PCBA Screen");
            str_exp_setdectype(8, 1);
            poststatus("PCBA Fail");
            strcpy(REPAIRCODE,"PAA018");
            Send_Debug_info();
            stoptest();
        }
        if((INVALID_LED == 0x2B01) || (INVALID_LED == 0x2B02) || (INVALID_LED == 0x2B03) || (INVALID_LED == 0x2B04) ||
           (INVALID_LED == 0x2B06) || (INVALID_LED == 0x2B07) || (INVALID_LED == 0x2B08) || (INVALID_LED == 0x2B09))
         //0x2B01:CPURAM_ERR    0x2B02:SEQRAM_ERR    0x2B03:BUFRAM_ERR    0x2B04:FLASH_ERR
         //0x2B06:Servo SPIRAM/YRAM/XRAM error  0x2B07:ECCRAM_ERR    0x2B08:Hybrid ONENAND flash error    0x2B09:BufferAlloc reuturned NULL
        {
            userfilelog("LED0x2BXX PCBA Screen");
            str_exp_setdectype(8, 1);
            poststatus("PCBA Fail");
            strcpy(REPAIRCODE,"PAA002");
            Send_Debug_info();        
            stoptest();
        }
        if(INVALID_LED != 0x00)
        {
            //Servo Copy Code D/L
            poststatus("SC Code D/L");
            userfilelog("F1-1D 320G Special SC CODE D/L");
            reset();
            sputs(EXPLOG,"EXSCCODE:%s",EXSCCODE);
            userfilelog(EXPLOG);
            dw(EXSCCODE);
            str_checkprompt("BOOT>");
            delays(50);
            power(0);
            delays(3);
            power(1);
            delays(40);
            ONERROR(reseterror);
            reset();
            ONERRORCLEAR();
            str_debugmode();
            nRet = CHK_HeadDetect(3);
            if(nRet != 0)
            {
                ExpertDecision(1);
                stoptest();
            }
            str_getengfw(engfw);
            str_exp_setdectype(1, 0);
            poststatus("OOB LXX");
            stoptest();
        }
    }

    poststatus("Chk Port");
    userfilelog("Chk Port");
	//str_checkportempty();	//detect hdd 768P -> DBG> 1.8 only
    //str_checkprompt("eng>");
    ONERROR(reset_1st);
	reset();
    ONERRORCLEAR();
    ONERROR(NotReady);
    cmdtimeout(50000);
    id(0);
    cmdtimeout(0);
    ONERRORCLEAR(); 

    //Check CAPA
    str_getbasicmodel(model);
    model[4]=0;
    atoi(model,2,10,CAPA);
    
    //Check RPM
    //debug[0]=model[5];
    debug[0]=model[6];
    strcmp(debug,"I",1,ret);  //I:5400rpm
    if(ret == 0)
    {
        RPM = 54;
        sputd(EXPLOG,"CAPA : %d , I : 5400rpm",CAPA);
        userfilelog(EXPLOG);
    }
    else                        //J:7200rpm
    {
        RPM = 72;
        sputd(EXPLOG,"CAPA : %d , J : 7200rpm",CAPA);
        userfilelog(EXPLOG);
    }
    sprintf(EXPLOG,"MAX_HEAD : %d, MAX_ZONE : %d",MAX_HEAD,MAX_ZONE);
    userfilelog(EXPLOG);
    
    //Check f/w
    str_getengfw(engfw);
    sputs(EXPLOG,"Ready F/W:%s",engfw);
    userfilelog(EXPLOG);

    //get MES info
    poststatus("GetMESinfo");
    getsn(debug);
    atoi(debug,4,10,MES_CODE);
    strlen(debug,cmdlen);
    sprintf(EXPLOG,"PN:%s , MES_CODE:%d",debug,MES_CODE);
    userfilelog(EXPLOG);
    if((cmdlen==14) && (MES_CODE!=0))
    {
        userfilelog("PN OK");
        str_meson();
        poststatus("GetMESinfo");
        str_mesreqexpertinfo();
        delays(300);
        str_mesgetexpertinfo(MES_EXPERT_INFO);
        str_mesoff();
    }
    else if(MES_CODE!=0)
    {
        userfilelog("PN Fail");
        if((engfw[7]==POSTFIX_sc) || (engfw[7]==POSTFIX_SC))
        {
            nRet = DOWNLOAD_EXPSCCODE(60,0);
            if(nRet != 0)
            {
                str_exp_setdectype(1, 0);
                poststatus("SC D/LFail");
                userfilelog("SC Code D/L Fail");
                stoptest();
            }
        }
        else if(engfw[7] == POSTFIX_bi || engfw[7] == POSTFIX_BI)
        {
            nRet = DOWNLOAD_EXPBICODE(60);
            if(nRet != 0)
            {
                str_exp_setdectype(1, 0);
                poststatus("BI D/LFail");
                userfilelog("BI Code D/L Fail");
                stoptest();
            }
        }
        //Re-idfy
        ONERROR(reset_1st);
        reset();
        ONERRORCLEAR();
        ONERROR(NotReady);
        id(0);
        ONERRORCLEAR(); 
        getsn(debug);
        strlen(debug,cmdlen);
        sputs(EXPLOG,"PN:%s",debug);
        userfilelog(EXPLOG);
        if(cmdlen==14)
        {
            userfilelog("PN OK");
            str_meson();
            poststatus("GetMESinfo");
            str_mesreqexpertinfo();
            delays(300);
            str_mesgetexpertinfo(MES_EXPERT_INFO);
            str_mesoff();
        }
    }
    sputd(EXPLOG,"MES_EXPERT_INFO.MES_STW_COUNT = %d",MES_EXPERT_INFO.MES_STW_COUNT);
    userfilelog(EXPLOG);
    sputd(EXPLOG,"MES_EXPERT_INFO.MES_PCBA_USED_COUNT = %d",MES_EXPERT_INFO.MES_PCBA_USED_COUNT);
    userfilelog(EXPLOG);
    sputd(EXPLOG,"MES_EXPERT_INFO.MES_BI_COUNT = %d",MES_EXPERT_INFO.MES_BI_COUNT);
    userfilelog(EXPLOG);
    sputd(EXPLOG,"MES_EXPERT_INFO.MES_REASSY_COUNT = %d",MES_EXPERT_INFO.MES_REASSY_COUNT);
    userfilelog(EXPLOG);
    sputd(EXPLOG,"MES_EXPERT_INFO.MES_DISK_DMR=%c",MES_EXPERT_INFO.MES_DISK_DMR);
    userfilelog(EXPLOG);
    sputd(EXPLOG,"MES_EXPERT_INFO.MES_HEAD_DMR=%c",MES_EXPERT_INFO.MES_HEAD_DMR);
    userfilelog(EXPLOG);
    sputd(EXPLOG,"MES_EXPERT_INFO.MES_RWK_FLAG=%c",MES_EXPERT_INFO.MES_RWK_FLAG);
    userfilelog(EXPLOG);
    sputs(EXPLOG,"MES_EXPERT_INFO.MES_MCODE=%s",MES_EXPERT_INFO.MES_MCODE);
    userfilelog(EXPLOG);
    sputs(EXPLOG,"MES_EXPERT_INFO.MES_PREV_ERROR=%s",MES_EXPERT_INFO.MES_PREV_ERROR);
    userfilelog(EXPLOG);
    sputs(EXPLOG,"MES_EXPERT_INFO.MES_DEBUG=%s",MES_EXPERT_INFO.MES_DEBUG);
    userfilelog(EXPLOG);

    //LED44 Screen
    //strcpy(MES_EXPERT_INFO.MES_PREV_ERROR,"FSC0S44");
    //sputs(EXPLOG,"MES_EXPERT_INFO.MES_PREV_ERROR=%s",MES_EXPERT_INFO.MES_PREV_ERROR);
    //userfilelog(EXPLOG);
    nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"FSC0S44",7,nRet);    //yes:0, no:1
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        poststatus("HDI Fail");
        userfilelog("1 HDI Fail");
        str_exp_setdectype(6, 8);
        strcpy(REPAIRCODE,"E00009");	//Head, Disk Touch
        Send_Debug_info(); 
        stoptest();
    }
    nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"FSC0Z44",7,nRet);
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        poststatus("HDI Fail");
        userfilelog("2 HDI Fail");
        str_exp_setdectype(6, 8);
        strcpy(REPAIRCODE,"E00009");	//Head, Disk Touch
        Send_Debug_info(); 
        stoptest();
    }
    nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"FPT0S44",7,nRet);
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        poststatus("HDI Fail");
        userfilelog("3 HDI Fail");
        str_exp_setdectype(6, 8);
        strcpy(REPAIRCODE,"E00009");	//Head, Disk Touch
        Send_Debug_info(); 
        stoptest();
    }
    nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"FPT0Z44",7,nRet);
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        poststatus("HDI Fail");
        userfilelog("4 HDI Fail");
        str_exp_setdectype(6, 8);
        strcpy(REPAIRCODE,"E00009");	//Head, Disk Touch
        Send_Debug_info(); 
        stoptest();
    }
    nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"FBI0S44",7,nRet);
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        poststatus("HDI Fail");
        userfilelog("5 HDI Fail");
        str_exp_setdectype(6, 8);
        strcpy(REPAIRCODE,"E00009");	//Head, Disk Touch
        Send_Debug_info(); 
        stoptest();
    }
    nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"FBI0Z44",7,nRet);
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        poststatus("HDI Fail");
        userfilelog("6 HDI Fail");
        str_exp_setdectype(6, 8);
        strcpy(REPAIRCODE,"E00009");	//Head, Disk Touch
        Send_Debug_info(); 
        stoptest();
    }
        nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"FSC1701",7,nRet);
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        str_exp_setdectype(1, 0);
        poststatus("OOB");
        stoptest();
    }
    nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"FSC1702",7,nRet);
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        str_exp_setdectype(1, 0);
        poststatus("OOB");
        stoptest();
    }
    nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"FSC1703",7,nRet);
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        str_exp_setdectype(1, 0);
        poststatus("OOB");
        stoptest();
    }
        nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"FSC1102",7,nRet);
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        str_exp_setdectype(1, 0);
        poststatus("OOB");
        stoptest();
    }
    
    /*
    nRet=0xffff;
    strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"NONE",4,nRet);
    sputd(EXPLOG,"nRet:%d",nRet);
    userfilelog(EXPLOG);
    if(nRet == 0)
    {
        str_exp_setdectype(1, 0);
        poststatus("NO MES");
        userfilelog("MES_PREV_ERROR");
        stoptest();
    }
    */
    //Check Kill Model
    strcmp(MES_EXPERT_INFO.MES_MCODE,"0000",4,ret);
    if(ret != 0)
    {
        if( MES_EXPERT_INFO.MES_MCODE[3] >= CHAR_A && MES_EXPERT_INFO.MES_MCODE[3] <= CHAR_Z )
        {
            poststatus("Killing Model");
            stoptest();
        }
    }
    else
    {
        getsn(debug);
        pn[0]=debug[0]; pn[1]=debug[1]; pn[2]=debug[2]; pn[3]=debug[3];
        strcpy(MES_EXPERT_INFO.MES_MCODE,pn);
    }
    
    //Check New MASS firmware
    if(engfw[5] == CHAR_U || engfw[5] == CHAR_u || engfw[5] == CHAR_Q || engfw[5] == CHAR_q || engfw[5] == CHAR_N || engfw[5] == CHAR_n)
    {
        MASS = 1;
        sputd(EXPLOG,"MASS : %d -> Disable MASS",MASS);
        userfilelog(EXPLOG);        
    }
    else
    {
        MASS = 0;
        sputd(EXPLOG,"MASS : %d -> Enable MASS",MASS);
        userfilelog(EXPLOG);        
    }
    
    //Start Expert
    if(engfw[7] == POSTFIX_smt || engfw[7] == POSTFIX_SMT)
    {
    	if(MES_EXPERT_INFO.MES_BI_COUNT <= 2)
		{
	        str_exp_setdectype(3, 1);
	        poststatus("Re-S/C");
            strcpy(REPAIRCODE,"T00004");	//Re-S/C
            Send_Debug_info();
            Restore_MESmodel();
            MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],0);     //0:ORG, 1:DS, 2:DDS
            //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x01);    //#define ESPECTYPE_HBT 0x01 //!<SC->BI
	        stoptest();
		}
		else
		{
            CHK_MESHistory(1,0);     //type -> 1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            ExpertDecision(0);
            stoptest();
		}
    }

	if((engfw[7]==POSTFIX_sc) || (engfw[7]==POSTFIX_SC) || (engfw[7]==POSTFIX_smt) || (engfw[7]==POSTFIX_SMT))
	{
        //Servo Copy Code D/L
        poststatus("SC Code D/L");
        userfilelog("F1-3D 1TB Special SC CODE D/L");
        reset();
        sputs(EXPLOG,"EXSCCODE:%s",EXSCCODE);
        userfilelog(EXPLOG);
        dw(EXSCCODE);
        str_checkprompt("BOOT>");
        delays(50);
        power(0);
        delays(3);
        power(1);
        delays(40);
        ONERROR(reseterror);
        reset();
        ONERRORCLEAR();
        
        str_getinvalidled(INVALID_LED);
        if(INVALID_LED != 0x0000)
        {
            str_exp_setdectype(1, 0);
            poststatus("OOB");
            userfilelog("F1-3D 1TB Special SC CODE D/L Fail");
            stoptest();
        }
        str_debugmode();
        nRet = CHK_HeadDetect(3);
        if(nRet != 0)
        {
            ExpertDecision(0);
            stoptest();
        }
        
        nRet = EXP_SCFail();
        if(nRet != 0)
        {
            ExpertDecision(1);
            stoptest();
        }
        if(MES_EXPERT_INFO.MES_STW_COUNT <= 2)
        {
            CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            ExpertDecision(0);
            stoptest();
        }
        else
        {
            str_exp_setdectype(1, 0);
            poststatus("OOB");
            userfilelog("OOB SCFail");
            stoptest();
        }
	}
	else if(engfw[7] == POSTFIX_bi || engfw[7] == POSTFIX_BI)
	{
        power(0);
        delays(3);
        power(1);
        delays(90);
        ONERROR(reseterror);
        reset();
        ONERRORCLEAR();
        
        CHK_BISTATUS(1);     //LB CMD -> Setting CAPA, RPM
        nRet = DOWNLOAD_EXPBICODE(50);
        if(nRet != 0)	return;
        
        //Head detect
        nRet = CHK_HeadDetect(1);
        if(nRet != 0)
        {
            ExpertDecision(0);
            stoptest();
        }
        
        //Killing Check
        Check_Kill_HeadNum();
        
        //Hd, Zn, sCyl, ecyl mapping
        ReadZoneInfo();
        
        sputd(EXPLOG,"MAX_HEAD = %d",MAX_HEAD);
        userfilelog(EXPLOG);
        sputd(EXPLOG,"MAX_ZONE = %d",MAX_ZONE);
        userfilelog(EXPLOG);
        
        //Check B/I Status
        CHK_BISTATUS(0);     //LB CMD -> All Setting
        sputd(EXPLOG,"BI_STATUS : %d",BI_STATUS);
        poststatus(EXPLOG);
        
        //CHK_TrkSrvTargetHDandMaxRatio();
        //CHK_FODMaxandMinValue(0xff);
        //CHK_GetTPIand747();
        //CHK_TPI74710SData(0xff);
        //CHK_DiskDetect(300);
        //stoptest();
        //return;
        //delays(1);
        
		switch(BI_STATUS)	//0=burn 1=end 2=fail 3=cont
		{
		case 0:             //MAX_HEAD : ch 0,1,2,3,4,5
			poststatus("No B/I");
		    EXP_HTFail();
            ExpertDecision(0);
            stoptest();
            break;
		case 1:
			poststatus("B/I Pass");
            EXP_BIEND();
            ExpertDecision(0);
			stoptest();
            break;
		case 2:
            if((BI_ERRORCODE == 0x0000) && (BI_LASTSTEP > 140))
            {
                poststatus("B/I Pass");
                EXP_BIEND();
                ExpertDecision(0);
                stoptest();
                break;
            }
			if(BI_LASTSTEP == 0)
			{
                poststatus("No B/I");
                EXP_HTFail();
                ExpertDecision(0);
                stoptest();
                break;
			}
			if(BI_LASTSTEP < 40)
			{
                poststatus("HT Fail");
                EXP_HTFail();
                ExpertDecision(0);
                stoptest();
                break;
			}
			else
			{
                poststatus("B/I Fail");
                EXP_BIFail();
                ExpertDecision(0);
                stoptest();
                break;
			}
		case 3:
			//CONT Rule
			poststatus("B/I Cont");
			EXP_CONT();
            ExpertDecision(0);
			stoptest();
            break;
		case 0xffff:
            poststatus("HT Fail");
            EXP_HTFail();
            ExpertDecision(0);
            stoptest();
            break;
		default:
            EXP_BURN();
            ExpertDecision(0);
            stoptest();
		}
    }
	else if(engfw[7] == POSTFIX_main || engfw[7] == POSTFIX_MAIN)
    {
        CHK_BISTATUS(1);     //LB CMD -> Setting CAPA, RPM
        nRet = DOWNLOAD_EXPBICODE(50);
        if(nRet != 0)	return;
 
        //Killing Check
        Check_Kill_HeadNum();
        
        //Head detect
        nRet = CHK_HeadDetect(1);
        if(nRet != 0)
        {
            ExpertDecision(0);
            stoptest();
        }
        
        //Hd, Zn, sCyl, ecyl mapping
        ReadZoneInfo();
        
        sputd(EXPLOG,"MAX_HEAD = %d",MAX_HEAD);
        userfilelog(EXPLOG);
        sputd(EXPLOG,"MAX_ZONE = %d",MAX_ZONE);
        userfilelog(EXPLOG);

        CHK_BISTATUS(0);     //LB CMD
        sprintf(EXPLOG,"BI_STATUS : %d",BI_STATUS);
        poststatus(EXPLOG);

        poststatus("F/T Fail");
        EXP_FTFail();
        ExpertDecision(0);
	    stoptest();
    } 
	else
	{
        CHK_BISTATUS(1);     //LB CMD -> Setting CAPA, RPM
        nRet = DOWNLOAD_EXPBICODE(70);
        if(nRet != 0)	return;
        //Killing Check
        Check_Kill_HeadNum();
        
        //Head detect
        nRet = CHK_HeadDetect(1);
        if(nRet != 0)
        {
            ExpertDecision(0);
            stoptest();
        }
        
        //Hd, Zn, sCyl, ecyl mapping
        ReadZoneInfo();
        
        sputd(EXPLOG,"MAX_HEAD = %d",MAX_HEAD);
        userfilelog(EXPLOG);
        sputd(EXPLOG,"MAX_ZONE = %d",MAX_ZONE);
        userfilelog(EXPLOG);
        
        CHK_BISTATUS(0);     //LB CMD
        sputd(EXPLOG,"BI_STATUS : %d",BI_STATUS);
        poststatus(EXPLOG);
        
        str_exp_setdectype(1, 0);        
        poststatus("UnDef Fail");
        stoptest();
	}
    str_exp_setdectype(1, 0);        
    poststatus("Script XXX");	
	stoptest();
    return;
}

int EXP_SCFail()
{
    int tled, flag, nRFLedNum, nRFCylNum, nRFHdNum, icyl, ihd, led, nHd, nRet, nOffs, nValue, cmdlen, ntemp, ntemp1, ntemp2, ntemp3, nRefhd, nCnt;
    int debug[4];
    int RdyFailHdNum[4];
    int cmd[16];
    int nSAGC[4];
    int nSAC[4];
    int nFodRW[4];
    int nFod_Resistance[4];
    int nMRR[4];
    int nT1modeMag[4];
    memset(debug,0,0,4);
    memset(RdyFailHdNum,0,0,4);
    memset(cmd,0,0,16);
    memset(nSAGC,0,0,4);    
    memset(nSAC,0,0,4);
    memset(nFodRW,0,0,4);    
    memset(nFod_Resistance,0,0,4);    
    memset(nMRR,0,0,4);
    memset(nT1modeMag,0,0,4);

    KILLFLAG = 1;
    
    nRFLedNum=0xFFFF;
    nRFCylNum=0xFFFF;
    nRFHdNum=0xFF;
    
    if((MES_EXPERT_INFO.MES_STW_COUNT <= 1) && (MES_EXPERT_INFO.MES_HEAD_DMR == 0x50))
    {
        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        stoptest();
    }
    
    //Special Rule
    ONERROR(dummyerror);
    str_usercmd("RF 1", 4, 5000, 3584, 1, RDBUF, 512);
    ONERRORCLEAR();
    
    peek(RDBUF,0x100,4,nRFCylNum);
    peek(RDBUF,0x105,1,nRFHdNum);
    peek(RDBUF,0xDE,2,nRFLedNum);
    sprintf(EXPLOG,"nRFLedNum%x nRFCylNum:%x nRFHdNum:%x",nRFLedNum,nRFCylNum,nRFHdNum);
    userfilelog(EXPLOG);
    
    if((nRFLedNum != 0x0000) && (nRFLedNum != 0xFFFF) && (nRFCylNum != 0xFFFF) && (nRFHdNum != 0xFF) && (nRFHdNum < MAX_HEAD))
    {
        if(((nRFLedNum & 0xFF00) != 0x1100) || ((nRFLedNum & 0xFF00) != 0x1200))
        {
            //SetDecisionHead(nRFHdNum, HEAD_FAIL);	//type 1 : Head Fail
            sputd(EXPLOG,"RF 1 : HD[%d] Fail",nRFHdNum);
            userfilelog(EXPLOG);
            //poststatus(EXPLOG);
            //ExpertDecision(0);
            //stoptest();
        }
    }
    else
    {
        //str_exp_setdectype(6, 3);
        //poststatus("HeadSwap");
        userfilelog("RF 1 : Special Rule No Detect");
        //strcpy(REPAIRCODE,"H11005");    //"HeadSwap" + DECISION_HeadSwap
        //Send_Debug_info();
        //stoptest();
    }
    //END

    tled=0xff;
    ntemp=0; ntemp1=0; ntemp2=0; ntemp3=0;
    //########################################################
    //  4-Burst pattern check
    //########################################################
    //Retrack & Unlatch
    userfilelog("Retrack & Unlatch");
    ONERROR(reseterror);
    reset();
    ONERRORCLEAR();
    //str_checkprompt("ENG>");
    str_sendserialcmd("OU",2);
    delays(5);
    //str_checkprompt("ENG>");
    
    //Servo Copy
    for(nCnt=0; nCnt<2; nCnt++)
    {
        tled=0; flag=0;
        //Seek (nHd 1 -> 0)
        if(nCnt==0)  nRefhd=1;
        if(nCnt==1)  nRefhd=0;
        //if(nCnt==2)  nRefhd=3;
        sputd(EXPLOG,"4 burst Seek Hd[%d]",nRefhd);
        userfilelog(EXPLOG);
        nRet = IsSeekFail(1600,1800,nRefhd,nRefhd,0,2,2);
        if(nRet == 0)
        {
            poststatus("OK");
        }
        else
        {
            ntemp++;
            if(ntemp == 3)
            {
                if(MES_EXPERT_INFO.MES_HEAD_DMR == 0x52)
                {
                    if((nRFLedNum != 0x0000) && (nRFLedNum != 0xFFFF) && (nRFCylNum != 0xFFFF) && (nRFHdNum != 0xFF))
                    {
                        SetDecisionHead(nRFHdNum, HEAD_FAIL);	//type 1 : Head Fail
                        sputd(EXPLOG,"HD[%d] Fail",ihd);
                        userfilelog(EXPLOG);
                        poststatus(EXPLOG);
                        ExpertDecision(0);
                        stoptest();
                    }
                }
                else if(MES_EXPERT_INFO.MES_DISK_DMR == 0x52)
                {
                    SetDecisionHeadTest();
                    stoptest();
                }
                else
                {
                    if((nRFLedNum != 0x1415) || (nRFLedNum != 0x1441) || (nRFLedNum != 0x1442) ||
                       (nRFLedNum != 0x1446) || (nRFLedNum != 0x1453) || (nRFLedNum != 0x1456) ||
                       (nRFLedNum != 0x1904))
                    {
                        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                        return 1;
                    }
                    else
                    {
                        SetDecisionHeadTest();
                        stoptest();
                    }
                }
            }
            continue;
        }
        sputd(EXPLOG,"nRefHd : %d",nRefhd);
        userfilelog(EXPLOG);
        
        //Power OFF/ON
        userfilelog("Power OFF/ON .. No Reset()");
        power(0);
        delays(3);
        power(1);
        delays(50);
        //str_checkprompt("ENG>");
        
        //Servo Copy script D/L
        poststatus("SCSPT D/L");
        sputd(debug,"F12SPTH%d.s85",nRefhd);    //#define problem
        str_downloadbiscript(debug);
        userfilelog(debug);
        delays(10);
        //str_checkprompt("ENG>");
        
        //Servo Copy (conv.Area 1000 ~ 4000)
        str_starttest();
        sputd(EXPLOG,"Servo Copy ~ing HD[%d]",nRefhd);
        userfilelog(EXPLOG);
        ONERROR(dummyerror);
        str_monitoring("SP", 200, tled, flag);
        ONERRORCLEAR();
        sprintf(EXPLOG,"HD[%d] Servo Copy tled : L0x%x",nRefhd,tled);
        userfilelog(EXPLOG);
        sputd(EXPLOG,"SP END[L0x%x]",tled);
        poststatus(EXPLOG);
        str_debugmode();    //F1 No Reset()
        
        if(tled != 0x0000)
        {
            nRet = CHK_HeadDetect(3);
            if(nRet != 0)
            {
                return 1;
            }
            ntemp1++;
            power(0);
            delays(3);
            power(1);
            delays(50);
            ONERROR(reseterror);
            reset();
            ONERRORCLEAR();
            //str_checkprompt("ENG>");
        }
        else
        {
            break;
        }
    }
    if(tled == 0x0000)
    {
        sputd(EXPLOG,"========= S/C success nRefhd : %d =========",nRefhd);
        userfilelog(EXPLOG);
        //Power OFF/ON
        power(0);
        delays(3);
        power(1);
        delays(50);
        ONERROR(reseterror);
        reset();
        ONERRORCLEAR();
        //str_checkprompt("ENG>");
        
        //########################################################
        //  Copy Pattern check
        //########################################################
        //1st Seek
        sputd(EXPLOG,"Copy pattern 1st Seek Hd[%d]",nRefhd);
        userfilelog(EXPLOG);
        
        sputd(cmd,"SK 708 %d 1",nRefhd);
        strlen(cmd,cmdlen);
        str_sendserialcmd(cmd,cmdlen);
        delays(5);
        //str_checkprompt("ENG>");
        
        //Burst Switching
        userfilelog("Burst Switching");
        str_sendserialcmd("PT 1",4);
        delays(5);
        //str_checkprompt("ENG>");
        
        /*
        sputd(cmd,"SK 708 %d 1",nRefhd);
        strlen(cmd,cmdlen);
        str_sendserialcmd(cmd,cmdlen);
        delays(5);
        //str_checkprompt("ENG>");
        */
        
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            //Copy Pattern Seek test
            sputd(EXPLOG,"Copy Pattern Seek Test Hd[%d]",nHd);
            userfilelog(EXPLOG);
            nRet = IsSeekFail(1600,1800,nHd,nHd,1,2,2);
            if(nRet == 1)
            {
                SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"Copy Pattern IsSeekFail : Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
                ntemp2++;
                nRet=0;
            }
        }
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            IsServoGainFail(1800,nHd,1,240,3,1);
            if(MAX_SGAIN[nHd] > 160)
            {
                SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"Copy Pattern IsServoGain MAX Fail Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
                ntemp3++;
            }
            else if((MIN_SGAIN[nHd]!=0) && (MIN_SGAIN[nHd]<5))
            {
                SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"Copy Pattern IsServoGain MIN Fail Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
                ntemp3++;
            }
            else if((MIN_SGAIN[nHd]!=0) && (MAX_SGAIN[nHd]>160))
            {
                SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"Copy Pattern IsServoGain MIN Fail Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
                ntemp3++;
            }
            else
            {
                sputd(EXPLOG,"Copy Pattern IsServoGain Hd:%d Pass",nHd);
                userfilelog(EXPLOG);
            }
        }
    }

    if(ntemp2 == MAX_HEAD)
    {
        if((MES_EXPERT_INFO.MES_HEAD_DMR == 0x52) || (MES_EXPERT_INFO.MES_DISK_DMR == 0x52))
        {
            if((nRFLedNum != 0x00) && (nRFLedNum != 0xFF) && (nRFCylNum != 0xFFFF) && (nRFHdNum != 0xFF))
            {
                SetDecisionHead(nRFHdNum, HEAD_FAIL);	//type 1 : Head Fail
                sputd(EXPLOG,"HD[%d] Fail",nRFHdNum);
                userfilelog(EXPLOG);
                poststatus(EXPLOG);
                ExpertDecision(0);
                stoptest();
            }
            else
            {
                SetDecisionHeadTest();
                stoptest();
            }
        }
        else
        {
            SetDecisionHeadSwap();
            userfilelog("Seek Head All Fail : HeadSwap");
            stoptest();
        }
    }
    if((ntemp==3) || (ntemp1==3))
    {
        if((nRFLedNum != 0x00) && (nRFLedNum != 0xFF) && (nRFCylNum != 0xFFFF) && (nRFHdNum != 0xFF))
        {
            SetDecisionHead(nRFHdNum, HEAD_FAIL);	//type 1 : Head Fail
            sputd(EXPLOG,"HD[%d] Fail",nRFHdNum);
            userfilelog(EXPLOG);
            poststatus(EXPLOG);
            ExpertDecision(0);
            stoptest();
        }
        else if((MES_EXPERT_INFO.MES_HEAD_DMR == 0x52) || (MES_EXPERT_INFO.MES_DISK_DMR == 0x52))
        {
            SetDecisionHeadTest();
            stoptest();
        }
        else
        {
            if((nRFLedNum != 0x1415) || (nRFLedNum != 0x1441) || (nRFLedNum != 0x1442) ||
               (nRFLedNum != 0x1446) || (nRFLedNum != 0x1453) || (nRFLedNum != 0x1456) ||
               (nRFLedNum != 0x1904))
            {
                CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                return 1;
            }
            else
            {
                SetDecisionHeadTest();
                stoptest();
            }
        }
    }
    else
    {
        sprintf(EXPLOG,"SeekFail:%d ServoCopyFail:%d, ServoGainFail:%d",ntemp,ntemp1,ntemp2);
        userfilelog(EXPLOG);
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            nRet = GetDecisionHead(nHd);
            if(nRet != 0)
            {
                return 1;
            }
        }
    }
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        tled=0; flag=0;
        //2nd Seek
        sputd(EXPLOG,"Copy pattern 2nd Seek Hd[%d]",nHd);
        userfilelog(EXPLOG);
        //str_checkprompt("ENG>");
        sputd(cmd,"SK 708 %d 1",nHd);
        strlen(cmd,cmdlen);
        str_sendserialcmd(cmd,cmdlen);
        delays(2);
        //str_checkprompt("ENG>");
        
        //Burst Switching
        userfilelog("Burst Switching");
        str_sendserialcmd("PT 1",4);
        delays(5);
        //str_checkprompt("ENG>");
    }
    if((MES_EXPERT_INFO.MES_HEAD_DMR == 0x52) || (MES_EXPERT_INFO.MES_STW_COUNT > 1))
    {
        if((nRFLedNum != 0x00) && (nRFLedNum != 0xFF) && (nRFCylNum != 0xFFFF) && (nRFHdNum != 0xFF))
        {
            SetDecisionHead(nRFHdNum, HEAD_FAIL);	//type 1 : Head Fail
            sputd(EXPLOG,"HD[%d] Fail",nRFHdNum);
            userfilelog(EXPLOG);
            poststatus(EXPLOG);
            ExpertDecision(0);
            stoptest();
        }
    }
    if(MES_EXPERT_INFO.MES_DISK_DMR == 0x52)
    {
        SetDecisionHeadTest();
        stoptest();
    }
    SetDecisionHeadSwap();
    userfilelog("SC Matrial All Pass : HeadSwap");
    stoptest();
    return 0;
    /*
    //poststatus("ReSTW");
    userfilelog("ReSTW");
    if((nRFLedNum != 0x1415) || (nRFLedNum != 0x1441) || (nRFLedNum != 0x1442) ||
       (nRFLedNum != 0x1446) || (nRFLedNum != 0x1453) || (nRFLedNum != 0x1456) ||
       (nRFLedNum != 0x1904))
    {
        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return 1;
    }
    else
    {
        SetDecisionHeadTest();
        stoptest();
    }
    */
}

void EXP_HTFail()
{
    int led, tled, ttled, flag, icyl, ihd, i, izn, chk_Fail, nHd, nZn, nCnt, nRet;
    int ntype, cmdlen, nTargetHd, nTargetZn;
    int ntemp, ntemp0, ntemp1, ntemp2, ntemp3;
    int nTTLHD_MAX_Sgain, nTTLHD_MIN_Sgain, nTargetHD_MAX_Sgain, nTargetHD_MIN_Sgain;
    int find, nAddress, ivalue, nErrCode, nOffs;
    int nODSGC, nMDSGC, nIDSGC, nDiff1, nDiff2, nDiff3;
    int nSlimTDvlaue[8];
    char addr[8];
    char buff[16];
    char debug[64];
    char model[16];
	char engfw[16];
    char cmd[32];
    memset(nSlimTDvlaue,0,0,8);
    memset(addr,0,0,8);
    memset(buff,0,0,16);
    memset(model,0,0,16);    
    memset(debug,0,0,64);
    memset(engfw,0,0,16);
    memset(cmd,0,0,32);

    tled=0; flag=0;
    chk_Fail=0;
    nCnt=0;ntemp=0;
	str_getengfw(engfw);
    led = BI_ERRORCODE;

	switch(led)
	{
	// LATCH or Ramp Error
    case 0x1106:	//Latch crack error
	case 0x1107:	//latch force too low
	case 0x1108:	//latch force too high
    case 0x1109:    // BAD Pawl latch
    case 0x110B:    // BAD Pawl latch at loading
    case 0x110E:    // Strong magnet latch
        sputd(EXPLOG,"0x%d Fail : VCM Fail",tled);
        userfilelog(EXPLOG);
        str_exp_setdectype(6, 7);
        poststatus("VCM Fail");
        strcpy(REPAIRCODE,"S00023");    //1D:Bottom(S00023), 2D:top(S00022)
        Send_Debug_info();        
		stoptest();
		break;

	//IC(preamp, channel, engine, FFS)
	case 0x2A01:	//preamp is out of order
	case 0x2A02:	//preamp voltage regulator err
        sputd(EXPLOG,"0x%d Fail : PCBA",tled);
        userfilelog(EXPLOG);
        str_exp_setdectype(8, 1);
        poststatus("PCBA");
        strcpy(REPAIRCODE,"H11012");
        Send_Debug_info();
		stoptest();
		break;

	case 0x2A03:	//chip rev error
	case 0x2A04:	//NLS Register error
	case 0x2A05:	//Linked List Register error
	case 0x2A06:	//Free Fall Sensor is out of order
        sputd(EXPLOG,"0x%d Fail : PCBA",tled);
        userfilelog(EXPLOG);
        str_exp_setdectype(8, 1);
        poststatus("PCBA");
        strcpy(REPAIRCODE,"PAA018");
        Send_Debug_info();
		stoptest();
		break;

	//Ram/buffer
	case 0x2B01:	//CPURAM_ERR
	case 0x2B02:	//SEQRAM_ERR
	case 0x2B03:	//BUFRAM_ERR
	case 0x2B04:	//FLASH_ERR
	case 0x2B06:	//Servo SPIRAM/YRAM/XRAM error
	case 0x2B07:	//ECCRAM_ERR
	case 0x2B08:	//Hybrid ONENAND flash error
	case 0x2B09:	//BufferAlloc reuturned NULL
        sputd(EXPLOG,"0x%d Fail : PCBA",tled);
        userfilelog(EXPLOG);
        str_exp_setdectype(8, 1);
        poststatus("PCBA");
        strcpy(REPAIRCODE,"PAA002");
        Send_Debug_info();
		stoptest();
		break;

    default:
        break;
    }
    
    userfilelog("EXP_HTFail");
    if(MAX_HEAD != 2)
    {
        userfilelog("MissSET");
        str_exp_setdectype(1, 0);
        poststatus("OOB");
        stoptest();
        return;
    }
    
    power(0);
    delays(3);
    power(1);
    delays(90);
    str_checkprompt("ENG>");
    
//##################################################################################################
// MECHA Test Start
//##################################################################################################
//////////////////////////////
//Bias Cal. & Kt (QX)
//////////////////////////////
    tled=0xffff; flag=0; find=0; cmdlen=0; nAddress=0;
    userfilelog("MECHA Test Start");
    userfilelog("Special serial CMD : QX");
    str_checkprompt("ENG>");
    str_sendserialcmd("QX",2);
    ONERROR(dummyerror);
    str_monitoring("QX",100,tled,flag);
    ONERRORCLEAR();
    sputd(EXPLOG,"QX Results : LED%x",tled);
    userfilelog(EXPLOG);
    poststatus("QX END");
    str_debugmode();    //F1 can not Reset()
    
    if(tled==0x1314)
    {
        userfilelog("L0x1314 HSA bearing Fail");
        poststatus("HSA bearing Fail");
        str_exp_setdectype(6, 8);
        strcpy(REPAIRCODE,"H11004");	//HSA bearing
        Send_Debug_info();
        stoptest();
    }
    else if(tled==0xffff)
    {
        userfilelog("Bias Cal. & Kt Delay");
    }
    else
    {
        //Get Memory Address
        ONERROR(dummyerror);
        str_usercmd("QY",2, 3000, 60, 0, RDBUF, 512);
        ONERRORCLEAR();
        //sputs(EXPLOG,"RDBUF:%s",RDBUF);
        //userfilelog(EXPLOG);
        
        strfind(RDBUF,"Buffer start :",find);
        if(find>0)
        {
            memset(addr,0,0,8);
            memnmov(addr,RDBUF,0,find+15,8,0);
            atoi(addr,0,16,nAddress);
            sputd(EXPLOG,"QY addr:%x",nAddress);
            userfilelog(EXPLOG);
        }
        if(nAddress != 0)
        {
            sputd(cmd,"DS %x 100",nAddress);
            strlen(cmd,cmdlen);
            ONERROR(dummyerror);
            str_usercmd(cmd,cmdlen, 3000, 1687, 1, RDBUF, 512);
            ONERRORCLEAR();
            
            nErrCode=0xffff;
            peek(RDBUF,0x00,2,nErrCode);
            sputd(EXPLOG,"nErrCode=%x",nErrCode);
            userfilelog(EXPLOG);
            
            if(nErrCode == 0x1314)
            {
                poststatus("L1314 HSA bearing Fail");
                str_exp_setdectype(6, 8);
                strcpy(REPAIRCODE,"H11004");	//HSA bearing
                Send_Debug_info();
                stoptest();
            }
            else if(nErrCode == 0x1510)  //bias Seek Fail : only Head 0
            {
                SetDecisionHead(0,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                userfilelog("IsSeekFail Hd:0 Fail");
                return;
            }
            else if(nErrCode == 0x1602)  //Kt Fail : only Head 0 or 1
            {
                icyl=0; ihd=0xff;
                str_usercmd("RI", 2, 3000, 1716, 1, RDBUF, 512);
                peek(RDBUF,0x00,4,icyl);
                peek(RDBUF,0x05,1,ihd);
                sprintf(EXPLOG,"L%x ihd %x",tled,ihd);
                userfilelog(EXPLOG);
                if(icyl != 0)
                {
                    if(ihd != 0xFF)
                    {
                        sputd(EXPLOG,"HD:%d Fail",ihd);
                        userfilelog(EXPLOG);
                        SetDecisionHead(ihd, HEAD_FAIL);	//type 1 : Head Fail
                        return;
                    }
                    else
                    {
                        for(nHd=0; nHd<MAX_HEAD; nHd++)
                        {
                            nRet = GetDecisionHead(nHd);
                            if(nRet != 0)	return;
                        }
                        sputd(EXPLOG,"LED%x Head Swap",tled);
                        userfilelog(EXPLOG);
                        SetDecisionHeadTest();
                        stoptest();
                    }
                }
                else
                {
                    userfilelog("MECHA Test LED1602 Miss");
                    str_exp_setdectype(1, 0);
                    poststatus("MECHA OOB");
                    stoptest();
                }
            }
            else if((nErrCode != 0x0000) && (nErrCode != 0xffff))
            {
                userfilelog("MECHA Test Miss");
                str_exp_setdectype(1, 0);
                poststatus("MECHA OOB");
                stoptest();
            }
            else if(nErrCode == 0xffff)
            {
                userfilelog("MECHA Test nErrCode Fail");
            }
            else
            {
                userfilelog("MECHA Test Pass");
            }
        }
        else
        {
            userfilelog("MECHA Test Address Fail");
        }
    }
    //Power Off/ON
    power(0);
    delays(3);
    power(1);
    delays(40);
    ONERROR(reseterror);
    reset();
    ONERRORCLEAR();

//////////////////////////////
//Resonance (QT 0x400)
//////////////////////////////
    tled=0; flag=0; find=0; cmdlen=0; nAddress=0;
    userfilelog("Resonance Test Start");
    userfilelog("Special serial CMD : QT 0x400");
    str_checkprompt("ENG>");
    str_sendserialcmd("QT 400",6);
    str_monitoring("QT 0x400",60,tled,flag);
    sputd(EXPLOG,"QT 0x400 Results : LED%x",tled);
    userfilelog(EXPLOG);
    poststatus("QT 0x400 END");
    str_debugmode();    //F1 can not Reset()

    //Get Memory Address
    ONERROR(dummyerror);
    str_usercmd("QZ",2, 3000, 60, 0, RDBUF, 512);
    ONERRORCLEAR();
    //sputs(EXPLOG,"RDBUF:%s",RDBUF);
    //userfilelog(EXPLOG);
    
    strfind(RDBUF,"Buffer start :",find);
    if(find>0)
    {
        memset(addr,0,0,8);
        memnmov(addr,RDBUF,0,find+15,8,0);
        atoi(addr,0,16,nAddress);
        sputd(EXPLOG,"QZ addr:%x",nAddress);
        userfilelog(EXPLOG);
    }

    if(nAddress != 0)
    {
        sputd(cmd,"DS %x 100",nAddress);
        strlen(cmd,cmdlen);
        ONERROR(dummyerror);
        str_usercmd(cmd,cmdlen, 3000, 1687, 1, RDBUF, 512);
        ONERRORCLEAR();
        /*
        nErrCode=0xffff;
        peek(RDBUF,0x00,2,nErrCode);
        sputd(EXPLOG,"nErrCode=%x",nErrCode);
        userfilelog(EXPLOG);
        
        if((nErrCode != 0x0000) && (nErrCode != 0xffff))
        {
            sputd(EXPLOG,"L0x%x Resonance Fail",nErrCode);
            poststatus(EXPLOG);
            //strcpy(REPAIRCODE,"H11004");	//HSA bearing
            //Send_Debug_info();
            str_exp_setdectype(1, 0);
            poststatus("Resonance OOB");
            stoptest();
        }
        else if(nErrCode == 0xffff)
        {
            userfilelog("Resonance nErrCode Fail");
        }
        else
        {
            userfilelog("Resonance Test Pass");
        }
        */
    }
    else
    {
        userfilelog("Resonance Test Address Fail");
    }
    //Power Off/ON
    power(0);
    delays(3);
    power(1);
    delays(40);
    ONERROR(reseterror);
    reset();
    ONERRORCLEAR();

//////////////////////////////
//Misalignment (QU)
//////////////////////////////
    tled=0; flag=0; find=0; cmdlen=0; nAddress=0;
    userfilelog("Misalignment Test Start");
    userfilelog("Special serial CMD : QU");
    str_checkprompt("ENG>");
    str_sendserialcmd("QU",2);
    str_monitoring("QU",60,tled,flag);
    sputd(EXPLOG,"QU Results : LED%x",tled);
    userfilelog(EXPLOG);
    poststatus("QU END");
    str_debugmode();    //F1 can not Reset()

    //Get Memory Address
    ONERROR(dummyerror);
    str_usercmd("QV",2, 3000, 60, 0, RDBUF, 512);
    ONERRORCLEAR();
    //sputs(EXPLOG,"RDBUF:%s",RDBUF);
    //userfilelog(EXPLOG);
    
    strfind(RDBUF,"Buffer start :",find);
    if(find>0)
    {
        memset(addr,0,0,8);
        memnmov(addr,RDBUF,0,find+15,8,0);
        atoi(addr,0,16,nAddress);
        sputd(EXPLOG,"QV addr:%x",nAddress);
        userfilelog(EXPLOG);
    }

    if(nAddress != 0)
    {
        sputd(cmd,"DS %x 100",nAddress);
        strlen(cmd,cmdlen);
        ONERROR(dummyerror);
        str_usercmd(cmd,cmdlen, 3000, 1687, 1, RDBUF, 512);
        ONERRORCLEAR();
        
        nErrCode=0xffff;
        peek(RDBUF,0x00,2,nErrCode);
        sputd(EXPLOG,"nErrCode=%x",nErrCode);
        userfilelog(EXPLOG);
        
        if(nErrCode == 0x1208)
        {
            SetDecisionHeadSwap();
            userfilelog("L1208 Misalign : HeadSwap");
            stoptest();
        }
        else if((nErrCode != 0x0000) && (nErrCode != 0xffff))
        {
            userfilelog("Misalignment Test Miss");
            str_exp_setdectype(1, 0);
            poststatus("Misalign OOB");
            stoptest();
        }
        else if(nErrCode == 0xffff)
        {
            userfilelog("Misalignment nErrCode Fail");
        }
    }
    else
    {
        userfilelog("Misalignment Test Address Fail");
    }
    //Power Off/ON
    power(0);
    delays(3);
    power(1);
    delays(40);
    ONERROR(reseterror);
    reset();
    ONERRORCLEAR();
//##################################################################################################
// MECHA Test END
//##################################################################################################

//##################################################################################################
// Head brief Start
//##################################################################################################
    //////////////////////////////
    //Seek Test
    //////////////////////////////
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,8,1,1,1);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
		nRet = IsSeekFail(62,63,nHd,nHd,0,25,5);  //0 is OK, argument : int ntargetCyl,int ntargetHd,int nMode,int nLoop,float nthres
		if (nRet != 0)
		{
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
	    	sputd(EXPLOG,"IsSeekFail Hd:%d Fail]",nHd);
            userfilelog(EXPLOG);
		}
	}
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}
    //////////////////////////////
    //Servo Gain Test
    //////////////////////////////
	for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nRet = IsSeekFail(62,63,nHd,nHd,1,2,1);
        if(nRet == 0)
        {
            IsServoGainFail(63,nHd,1,240,3,0);
            if(MAX_SGAIN[nHd] > 160)
            {
                SetDecisionHead(nHd, HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"Copy Pattern IsServoGain MAX Fail Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
            }
            else if(MIN_SGAIN[nHd] < 5)
            {
                SetDecisionHead(nHd, HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"Copy Pattern IsServoGain MIN Fail Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
            }
            else
            {
                poststatus("OK");
            }
        }
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}
//##################################################################################################
// Head brief END
//##################################################################################################

//##################################################################################################
// Head Performance Start
//##################################################################################################
    //////////////////////////////
    //Skew & gap cal @mc
    //////////////////////////////
    str_sendserialcmd("XQ",2);      //loading Servo table
    delays(90);
    ONERROR(xqerror);
    str_checkprompt("ENG>");
    ONERRORCLEAR();
    
    str_sendserialcmd("QT 4018",7); //QT 4018 : cal. vlaue save mc
    userfilelog("QT CMD 4018");
    str_monitoring("QT 0x4018",300,tled,flag);
    sputd(EXPLOG,"QT 0x4018 RES:L%x",tled);
    userfilelog(EXPLOG);
    poststatus(EXPLOG);
    str_debugmode();

    if(tled == 0x00)
    {
        //Power Off/ON
        power(0);
        delays(3);
        power(1);
        delays(40);
        ONERROR(reseterror);
        reset();
        ONERRORCLEAR();
        str_sendserialcmd("XQ",2);      //loading Servo table
        delays(90);
        str_checkprompt("ENG>");
        
        //////////////////////////////
        //Slim TD Test (XK 9)
        //////////////////////////////
        tled=0; flag=0; find=0; cmdlen=0; nAddress=0;
        userfilelog("Slim TD Test Start");
        userfilelog("Special serial CMD : XK 9");
        str_checkprompt("ENG>");
        str_sendserialcmd("XK 9",4);
        str_monitoring("XK 9",30,tled,flag);
        sputd(EXPLOG,"XK 9 Results : LED%x",tled);
        userfilelog(EXPLOG);
        poststatus("XK 9 END");
        str_debugmode();    //F1 can not Reset()
        
        //Get Memory Address
        ONERROR(dummyerror);
        str_usercmd("XK A",4, 3000, 60, 0, RDBUF, 512);
        ONERRORCLEAR();
        //sputs(EXPLOG,"RDBUF:%s",RDBUF);
        //userfilelog(EXPLOG);
        
        strfind(RDBUF,"Buffer Start :",find);
        if(find>0)
        {
            memset(addr,0,0,8);
            memnmov(addr,RDBUF,0,find+15,8,0);
            atoi(addr,0,16,nAddress);
            sputd(EXPLOG,"XK A addr:%x",nAddress);
            userfilelog(EXPLOG);
        }
        if(nAddress != 0)
        {
            sputd(cmd,"DS %x 100",nAddress);
            strlen(cmd,cmdlen);
            ONERROR(dummyerror);
            str_usercmd(cmd,cmdlen, 3000, 1687, 1, RDBUF, 512);
            ONERRORCLEAR();
            
            nOffs=0x08; nRet=0;
            for(nHd=0; nHd<MAX_HEAD; nHd++)
            {
                peek(RDBUF,nOffs+nHd*4,2,nRet);
                nSlimTDvlaue[nHd]=nRet;
                sprintf(EXPLOG,"HD[%d] nSlimTDvlaue=%x",nHd,nSlimTDvlaue[nHd]);
                userfilelog(EXPLOG);
                
                if((nSlimTDvlaue[nHd] != 0) && (nSlimTDvlaue[nHd] < 170) && (nSlimTDvlaue[nHd] > 240))
                {
                    SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                    sprintf(EXPLOG,"HD[%d] Slim TD Fail : 170 <= %d <= 240",nSlimTDvlaue);
                    poststatus(EXPLOG);
                }
            }
        }
        else
        {
            userfilelog("Slim TD Test Address Fail");
        }
        //Power Off/ON
        power(0);
        delays(3);
        power(1);
        delays(40);
        ONERROR(reseterror);
        reset();
        ONERRORCLEAR();
        str_sendserialcmd("XQ",2);      //loading Servo table
        delays(90);
        str_checkprompt("ENG>");
        
        //////////////////////////////
        //OD-MD-ID Servo Gain Test
        //////////////////////////////
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            nRet = IsSeekFail(62,63,nHd,nHd,1,2,1);
            if(nRet == 0)
            {
                IsServoGainFail(1000,nHd,1,240,3,0);
                nODSGC = AVG_SGAIN[nHd];
                
                IsServoGainFail(80000,nHd,1,240,3,0);
                nMDSGC = AVG_SGAIN[nHd];
                
                IsServoGainFail(170000,nHd,1,240,3,0);
                nIDSGC = AVG_SGAIN[nHd];
                
                ntemp0=nODSGC-nMDSGC;
                abs(ntemp0,nDiff1);
                ntemp0=nODSGC-nIDSGC;
                abs(ntemp0,nDiff2);
                ntemp0=nMDSGC-nIDSGC;
                abs(ntemp0,nDiff3);
                if((nDiff1 >= 80) || (nDiff2 >= 80) || (nDiff3 >= 80))
                {
                    SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                    sprintf(EXPLOG,"nDiff1:%d nDiff2:%d nDiff3:%d",nDiff1,nDiff2,nDiff3);
                    userfilelog(EXPLOG);
                    sprintf(EXPLOG,"ServoGain difference Fail Hd:%d Fail",nHd);
                    userfilelog(EXPLOG);
                }
            }
        }
        //////////////////////////////
        //BER Test
        //////////////////////////////
        nRet = 0;
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            Set_TestCondition(0,4,0,0,0);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,nHd,100,5.0);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsBERFail Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
            }
        }
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            nRet = GetDecisionHead(nHd);
            if(nRet != 0)	return;
        }
    }
    else
    {
        icyl=0; ihd=0xff;
        str_usercmd("RI", 2, 3000, 1716, 1, RDBUF, 512);
        peek(RDBUF,0x00,4,icyl);
        peek(RDBUF,0x05,1,ihd);
        sprintf(EXPLOG,"L%x ihd %x",tled,ihd);
        userfilelog(EXPLOG);
        if(icyl != 0)
        {
            if(ihd != 0xFF)
            {
                sputd(EXPLOG,"HD:%d Fail",ihd);
                userfilelog(EXPLOG);
                SetDecisionHead(ihd, HEAD_FAIL);	//type 1 : Head Fail
                return;
            }
            else
            {
                for(nHd=0; nHd<MAX_HEAD; nHd++)
                {
                    nRet = GetDecisionHead(nHd);
                    if(nRet != 0)	return;
                }
                sputd(EXPLOG,"LED%x Head Swap",tled);
                userfilelog(EXPLOG);
                SetDecisionHeadTest();
                stoptest();
            }
        }
        else
        {
            for(nHd=0; nHd<MAX_HEAD; nHd++)
            {
                nRet = GetDecisionHead(nHd);
                if(nRet != 0)	return;
            }
            sputd(debug,"LED%x Head Swap",tled);
            userfilelog(EXPLOG);
            SetDecisionHeadTest();
            stoptest();
        }
    }

	switch(led)
	{
	// LATCH or Ramp Error
	case 0x1107:	//latch force too low
	case 0x1108:	//latch force too high
        sputd(EXPLOG,"0x%d Fail : VCM Fail",tled);
        userfilelog(EXPLOG);
        str_exp_setdectype(6, 7);
        poststatus("VCM Fail");
        strcpy(REPAIRCODE,"V00005");
        Send_Debug_info();        
		stoptest();
		break;

	// SPSTW (offline) Error		
	case 0x1314:	//Difference of bias calibration value is big
        if(MES_EXPERT_INFO.MES_STW_COUNT <= 1)
        {
            CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            break;
        }
        else
        {
            sputd(EXPLOG,"0x%d Fail : Head Test",tled);
            userfilelog(EXPLOG);
            SetDecisionHeadTest();
            userfilelog("L0x2AXX HSA Test");
            stoptest();
            break;
        }

	case 0x1705:	//Spin up too long
        userfilelog("1705 Fail : PCBA");
        str_exp_setdectype(8, 1);
        poststatus("PCBA");
        strcpy(REPAIRCODE,"PAA002");
        Send_Debug_info();        
        stoptest();
        break;

	//IC(preamp, channel, engine, FFS)
	case 0x2A01:	//preamp is out of order
	case 0x2A02:	//preamp voltage regulator err
        sputd(EXPLOG,"0x%d Fail : PCBA",tled);
        userfilelog(EXPLOG);
        str_exp_setdectype(8, 1);
        poststatus("PCBA");
        strcpy(REPAIRCODE,"H11012");
        Send_Debug_info();
		stoptest();
		break;

	case 0x2A03:	//chip rev error
	case 0x2A04:	//NLS Register error
	case 0x2A05:	//Linked List Register error
	case 0x2A06:	//Free Fall Sensor is out of order
        sputd(EXPLOG,"0x%d Fail : PCBA",tled);
        userfilelog(EXPLOG);
        str_exp_setdectype(8, 1);
        poststatus("PCBA");
        strcpy(REPAIRCODE,"PAA018");
        Send_Debug_info();
		stoptest();
		break;

	//Ram/buffer
	case 0x2B01:	//CPURAM_ERR
	case 0x2B02:	//SEQRAM_ERR
	case 0x2B03:	//BUFRAM_ERR
	case 0x2B04:	//FLASH_ERR
	case 0x2B06:	//Servo SPIRAM/YRAM/XRAM error
	case 0x2B07:	//ECCRAM_ERR
	case 0x2B08:	//Hybrid ONENAND flash error
	case 0x2B09:	//BufferAlloc reuturned NULL
        sputd(EXPLOG,"0x%d Fail : PCBA",tled);
        userfilelog(EXPLOG);
        str_exp_setdectype(8, 1);
        poststatus("PCBA");
        strcpy(REPAIRCODE,"PAA002");
        Send_Debug_info();
		stoptest();
		break;

    default:
        break;
    }

//IsSeekFail
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,8,1,1,1);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
		nRet = IsSeekFail(62,62,nHd,nHd,0,25,5);  //0 is OK, argument : int ntargetCyl,int ntargetHd,int nMode,int nLoop,float nthres
		if (nRet != 0)
		{
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
	    	sputd(EXPLOG,"IsSeekFail Hd:%d Fail]",nHd);
            userfilelog(EXPLOG);
		}
	}
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//CHK_ParameterMonitoring	
	//CHK_ParameterMonitoring();	//Paramtermonitoring : //AGC/ASC/MRR/CSM/ALARM/FODR/FODW/BER/SGAIN/QM/FODRES/TMPR [72]12*6
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        //ntemp = ParaMonLast[nHd * 12 + 2];	//2 = MRR
        if((ntemp != 0) && (ntemp < 0x100))        
        {
            sprintf(EXPLOG,"Hd:%d ParaMonLastMRR Fail:%d < 0x100]",nHd,ntemp);
            userfilelog(EXPLOG);
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
        }
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//CHK_TrkSrvTargetHDandMaxRatio , CHK_DefTargetHDandMaxRatio
//SrvContinous & DefContinous
//CHK_DList, CHK_ELogList
	CHK_HTDList(500);		// int TBL_DLIST[MAX_HEAD][MAX_ZONE]
	CHK_HTELogList();		// int TBL_ELOG[MAX_HEAD][MAX_ZONE]
	CHK_DefTargetHDandMaxRatio();
    //Get Data : DEF_MAXHDRATE, DEF_MAXDEFTARGETHD , ContinousCheck(nHd,nCyl,nSkew,50,5,300)
    CHK_TrkSrvTargetHDandMaxRatio();
    //Get Data : TRKSRV_MAXHDRATE, TRKSRV_MAXDEFTARGETHD , //ContinousCheck(nHd,nCyl,nSkew,50,5,50)
	sputd(EXPLOG,"DEF_TTLDEFCNT : %d", DEF_TTLDEFCNT);
    userfilelog(EXPLOG);
	sputd(EXPLOG,"TRKSRV_TTLDEFCNT : %d", TRKSRV_TTLDEFCNT);
    userfilelog(EXPLOG);
    if(DEF_TTLDEFCNT > 5)
    {
        MAXHDRATE = DEF_MAXHDRATE;
        MAXDEFTARGETHD = DEF_MAXDEFTARGETHD;    
    }
    else
    {
        if(TRKSRV_TTLDEFCNT > 3)
        {
            MAXHDRATE = TRKSRV_MAXHDRATE;
            MAXDEFTARGETHD = TRKSRV_MAXDEFTARGETHD;
        }
        else
        {
            MAXHDRATE = 0;
            MAXDEFTARGETHD = 0xff;
        }
    }
    CHK_DiskDetect(300);    // DEFCONTINOUS
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		ntemp=SRVCONTINOUS[nHd];
		if(ntemp != 0)
		{
			SetDecisionDisk(nHd);
	    	sprintf(EXPLOG,"SrvCont nDK:%d Fail [ContThres:%d >= 50]",nHd,ntemp);
            userfilelog(EXPLOG);
		}
		ntemp1=DEFCONTINOUS[nHd];
		if(ntemp1 != 0)
		{
			SetDecisionDisk(nHd);
	    	sprintf(EXPLOG,"DefCont nDK:%d Fail [ContThres:%d >= 300]",nHd,ntemp1);
            userfilelog(EXPLOG);
		}
	}
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionDisk(nHd);
		if(nRet != 0)	return;
	}
    /*
    CHK_DiskDefect_L3B(70);
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		if(BAD_DISK_3B[nHd] == CHAR_X)
		{
            str_exp_setdectype(1, 0);
            poststatus("DSK_GoExp");
            stoptest();
		}
	}
    */
	sprintf(EXPLOG,"HTRULE MAXDEFTARGETHD:%d,MAXHDRATE:%d",MAXDEFTARGETHD,MAXHDRATE);
    userfilelog(EXPLOG);
    if(MAXDEFTARGETHD != 0xff)
    {
        //CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MAXFODTARGETHD
    	CHK_FODMaxandMinValue(MAXDEFTARGETHD);	//0xff -> All Head Fail
    	if(MAXFODFH >= 70866)
    	{
    		SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
    		sprintf(EXPLOG,"FODFHMax_Hd:%d Fail,MAXFODFH:%d < 70866",MAXDEFTARGETHD,MAXFODFH);
            userfilelog(EXPLOG);
    		return;
    	}
    	if((MINFODFH != 0) && (MINFODFH <= 19685))
    	{
    		SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
    		sprintf(EXPLOG,"FODFHMin_Hd:%d Fail,MINFODFH:%d < 19685",MAXDEFTARGETHD,MINFODFH);
            userfilelog(EXPLOG);
    		return;		
    	}
    	if(MAXHDRATE > 550)
    	{
            Set_TestCondition(1,20,5,3,3);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
    		if (nRet != 0)
    		{
    			SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sprintf(EXPLOG,"TTLDef Hd:%d Fail -> Rate:%d>55.0 IsBERFail1 Fail",MAXDEFTARGETHD,MAXHDRATE);
                userfilelog(EXPLOG);
    			return;
    		}
            Set_TestCondition(0,8,2,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
    		if (nRet != 0)
    		{
    			SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
    			sprintf(EXPLOG,"TTLDef Hd:%d Fail -> Rate:%d>55.0 IsBERFail2 Fail",MAXDEFTARGETHD,MAXHDRATE);
                userfilelog(EXPLOG);
    			return;
    		}
            Set_TestCondition(0,2,1,1,1);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
    		if (nRet != 0)
    		{
    			SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sprintf(EXPLOG,"TTLDef Hd:%d Fail -> Rate:%d>55.0 IsBERFail3:%d",MAXDEFTARGETHD,MAXHDRATE,nRet);
                userfilelog(EXPLOG);
    			return;
    		}
            else
            {
                if((MAXHDRATE > 700) && (BI_ERRORCODE != 0x2813) && (MAX_HEAD != 1))
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                    sputd(EXPLOG,"IMAXHDRATE > 700 Hd:%d -> Head_Fail",MAXDEFTARGETHD);
                    userfilelog(EXPLOG);
                    return;
                }
                if((MAXHDRATE > 700) && (BI_ERRORCODE != 0x2813) && (MAX_HEAD == 2))
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                    sputd(EXPLOG,"IMAXHDRATE > 700 Hd:%d -> Head_Swap",MAXDEFTARGETHD);
                    userfilelog(EXPLOG);
                    return;
                }
                if((MAXHDRATE > 700) && (BI_ERRORCODE ==0x2813) && ((MES_EXPERT_INFO.MES_DISK_DMR == 0x52) || (MES_EXPERT_INFO.MES_DISK_DMR == 0x52)))
                {
                    SetDecisionDisk(MAXDEFTARGETHD);
                    sputd(EXPLOG,"IMAXHDRATE > 700 0x2813 Hd:%d -> Disk_Fail",MAXDEFTARGETHD);
                    userfilelog(EXPLOG);
                    return;
                }
                else
                {
                    if(BI_ERRORCODE != 0x1904)
                    {
                        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                        return;
                    }
                    else
                    {
                        SetDecisionHeadTest();
                        stoptest();
                    }
                }
            }
    	}
    	if(MAXHDRATE > 400)	//MAXHDRATE 40.0%
    	{
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
    		if (nRet != 0)
    		{
    	    	SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
    	    	sputd(EXPLOG,"IsBERFail1 Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
    	    	return;
    		}
            Set_TestCondition(0,8,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
    		if (nRet != 0)
    		{
    	    	SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
    	    	sputd(EXPLOG,"IsBERFail2 Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
    	    	return;
    		}	
            Set_TestCondition(0,8,2,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
    		if (nRet != 0)
    		{
    	    	SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
    	    	sputd(EXPLOG,"IsBERFail3 Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
    	    	return;
    		}
            else
            {
                if(BI_ERRORCODE != 0x1904)
                {
                    CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
                else
                {
                    SetDecisionHeadTest();
                    stoptest();
                }
            }
    	}
    	if(MAXHDRATE > 250)	//MAXHDRATE 25.0%
    	{
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
    		if (nRet != 0)
    		{
    	    	sputd(EXPLOG,"IsBERFail1 Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
    	    	SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
    	    	return;
    		}
            Set_TestCondition(0,8,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
    		if (nRet != 0)
    		{
    	    	sputd(EXPLOG,"IsBERFail2 Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
    	    	SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
    	    	return;
    		}
            Set_TestCondition(0,8,2,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
    		if (nRet != 0)
    		{
    	    	sputd(EXPLOG,"IsBERFail3 Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
    	    	SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
    	    	return;
    		}
            else
            {
                if(BI_ERRORCODE != 0x1904)
                {
                    CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
                else
                {
                    SetDecisionHeadTest();
                    stoptest();
                }
            }
    	}
    	else
    	{
    	    MAXDEFTARGETHD = 0xff;
    	}
    }

//CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MAXFODTARGETHD
    nCnt=0;
	CHK_FODMaxandMinValue(0xff);	//0xff -> All Head Fail
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        ntemp0 = HDMAXFODFH[nHd];
        ntemp1 = HDMINFODFH[nHd];
        ntemp2 = HDMAX2NDFODFH[nHd];
        ntemp3 = HDMIN2NDFODFH[nHd];
        if( (ntemp0 >= 70866) && (ntemp2 >= 70000) )
        {
            sprintf(EXPLOG,"FODFHMax_Hd:%d Fail [HDMAXFODFH:%d >= 70866][HDMAX2NDFODFH:%d >= 70866]",nHd,ntemp0,ntemp2);
            userfilelog(EXPLOG);
            SetDecisionHead(nHd,HEAD_FAIL);
            nCnt++;
        }
        if((ntemp1 != 0) && (ntemp3 != 0) && (ntemp1 <= 19685) && (ntemp3 <= 20000))
        {
            sprintf(EXPLOG,"FODFHMin_Hd:%d Fail [HDMINFODFH:%d <= 19685][HDMIN2NDFODFH:%d <= 19685]",nHd,ntemp1,ntemp3);
            userfilelog(EXPLOG);
            SetDecisionHead(nHd,HEAD_FAIL);
            nCnt++;
        }
    }
    if(nCnt != 0)   return;

//IsServoGainFail
    nTTLHD_MAX_Sgain = 0;
    nTTLHD_MIN_Sgain = 1000;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        IsServoGainFail(62,nHd,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
        
        if(MAX_SGAIN[nHd] > nTTLHD_MAX_Sgain)
        {
            nTargetHD_MAX_Sgain = nHd;
            nTTLHD_MAX_Sgain = MAX_SGAIN[nHd];
        }
        if(MIN_SGAIN[nHd] < nTTLHD_MIN_Sgain)
        {
            nTargetHD_MIN_Sgain = nHd;
            nTTLHD_MIN_Sgain = MIN_SGAIN[nHd];
        }
    }
    if (nTTLHD_MAX_Sgain > 160)
    {
        SetDecisionHead(nTargetHD_MAX_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",nTargetHD_MAX_Sgain);
        userfilelog(EXPLOG);
    }
    if ((nTTLHD_MIN_Sgain != 0) && (nTTLHD_MIN_Sgain < 5))
    {
        SetDecisionHead(nTargetHD_MIN_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",nTargetHD_MIN_Sgain);
        userfilelog(EXPLOG);
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//IsBERFail
    nRet = 0;
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        nRet = IsBERFail(62,nHd,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
        if (nRet != 0)
        {
            SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
            sputd(EXPLOG,"IsBERFail1 Hd:%d Fail",nHd);
            userfilelog(EXPLOG);
        }
    }
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nRet = GetDecisionHead(nHd);
        if(nRet != 0)	return;
    }
    nRet = 0;
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        Set_TestCondition(8,8,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        nRet = IsBERFail(62,nHd,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
        if (nRet != 0)
        {
            sputd(EXPLOG,"IsBERFail2 Hd:%d Fail",nHd);
            userfilelog(EXPLOG);
            SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        }
    }
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nRet = GetDecisionHead(nHd);
        if(nRet != 0)	return;
    }
// HTRule Pass -> Re-STW
    sprintf(EXPLOG,"HTRule Pass -> Re-STW");
    userfilelog(EXPLOG);    
    if(BI_ERRORCODE != 0x1904)
    {
        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    else
    {
        SetDecisionHeadTest();
        stoptest();
    }
}

void EXP_BIEND()
{
    int nHd, nRet, ntemp, ntemp1, nCnt;
    int nTTLHD_MAX_Sgain, nTTLHD_MIN_Sgain, nTargetHD_MAX_Sgain, nTargetHD_MIN_Sgain;

    userfilelog("EXP_BIEND");
    if(MAX_HEAD != 2)
    {
        userfilelog("MissSET");
        str_exp_setdectype(1, 0);
        poststatus("OOB");
        stoptest();
        return;
    }
    if(engfw[7] == POSTFIX_main || engfw[7] == POSTFIX_MAIN)
	{
        nRet = DOWNLOAD_EXPBICODE(50);
        if(nRet != 0)
        {
            return;
        }
    }

    if((CAPA == 32) && (MAX_HEAD == 2) && (MES_EXPERT_INFO.MES_BI_COUNT <= 1))
    {
        CHK_MESHistory(3,10);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if((CAPA == 32) && (MAX_HEAD == 2) && (MES_EXPERT_INFO.MES_BI_COUNT > 1))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if((CAPA == 25) && (MAX_HEAD == 2) && (MES_EXPERT_INFO.MES_BI_COUNT <= 2))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }

//IsServoGainFail
    nTTLHD_MAX_Sgain = 0;
    nTTLHD_MIN_Sgain = 1000;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        IsServoGainFail(62,nHd,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
        
        if(MAX_SGAIN[nHd] > nTTLHD_MAX_Sgain)
        {
            nTargetHD_MAX_Sgain = nHd;
            nTTLHD_MAX_Sgain = MAX_SGAIN[nHd];
        }
        if(MIN_SGAIN[nHd] < nTTLHD_MIN_Sgain)
        {
            nTargetHD_MIN_Sgain = nHd;
            nTTLHD_MIN_Sgain = MIN_SGAIN[nHd];
        }
    }
    if (nTTLHD_MAX_Sgain > 160)
    {
        SetDecisionHead(nTargetHD_MAX_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",nTargetHD_MAX_Sgain);
        userfilelog(EXPLOG);
    }
    if ((nTTLHD_MIN_Sgain != 0) && (nTTLHD_MIN_Sgain < 5))
    {
        SetDecisionHead(nTargetHD_MIN_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",nTargetHD_MIN_Sgain);
        userfilelog(EXPLOG);
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//IsBERFail
    nRet = 0;
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        Set_TestCondition(1,20,10,10,10);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        nRet = IsBERFail(62,nHd,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
        if (nRet != 0)
        {
            SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
            sputd(EXPLOG,"IsBERFail Hd:%d Fail",nHd);
            userfilelog(EXPLOG);
        }
    }
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nRet = GetDecisionHead(nHd);
        if(nRet != 0)	return;
    }

//SRVCONTINOUS & DEFCONTINOUS
//CHK_DList, CHK_ELogList
    if(MASS == 0)
    {
        MakeMassData();     //PP CMD
        CHK_DList();		// int TBL_DLIST[MAX_HEAD][MAX_ZONE]
        CHK_ELogList();		// int TBL_ELOG[MAX_HEAD][MAX_ZONE]
    }
    else
    {
        CHK_HTDList(500);		// int TBL_DLIST[MAX_HEAD][MAX_ZONE]
        CHK_HTELogList();	// int TBL_ELOG[MAX_HEAD][MAX_ZONE]
    }
    CHK_DefTargetHDandMaxRatio();	    //Get Data : DEF_MAXHDRATE, DEF_MAXDEFTARGETHD
                                        //ContinousCheck(nHd,nCyl,nSkew,50,5,300)
    CHK_TrkSrvTargetHDandMaxRatio();	//Get Data : TRKSRV_MAXHDRATE, TRKSRV_MAXDEFTARGETHD
                                        //ContinousCheck(nHd,nCyl,nSkew,50,5,50)
	sprintf(EXPLOG,"DEF_TTLDEFCNT : %d", DEF_TTLDEFCNT);
    userfilelog(EXPLOG);
	sprintf(EXPLOG,"TRKSRV_TTLDEFCNT : %d", TRKSRV_TTLDEFCNT);
    userfilelog(EXPLOG);
    if(DEF_TTLDEFCNT > 5000)
    {
        MAXHDRATE = DEF_MAXHDRATE;
        MAXDEFTARGETHD = DEF_MAXDEFTARGETHD;    
    }
    if(TRKSRV_TTLDEFCNT > 500)
    {
        MAXHDRATE = TRKSRV_MAXHDRATE;
        MAXDEFTARGETHD = TRKSRV_MAXDEFTARGETHD;
    }
    CHK_DiskDetect(300);    // DEFCONTINOUS
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		ntemp=SRVCONTINOUS[nHd];
		if(ntemp != 0)
		{
			SetDecisionDisk(nHd);
	    	sprintf(EXPLOG,"SrvCont nDK:%d Fail [ContThres:%d >= 50]",nHd,ntemp);
            userfilelog(EXPLOG);
		}
		ntemp1=DEFCONTINOUS[nHd];
		if(ntemp1 != 0)
		{
			SetDecisionDisk(nHd);
	    	sprintf(EXPLOG,"DefCont nDK:%d Fail [ContThres:%d >= 300]",nHd,ntemp1);
            userfilelog(EXPLOG);
		}
	}
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionDisk(nHd);
		if(nRet != 0)	return;
	}
    /*
    CHK_DiskDefect_L3B(70);
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		if(BAD_DISK_3B[nHd] == CHAR_X)
		{
            str_exp_setdectype(1, 0);
            poststatus("DSK_GoExp");
            stoptest();
		}
	}
    */
    CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
    return;
}

void EXP_BURN()
{
	int nHd, nZn, nRet, ntemp;
    int nTTLHD_MAX_Sgain, nTTLHD_MIN_Sgain, nTargetHD_MAX_Sgain, nTargetHD_MIN_Sgain;

    userfilelog("EXP_BURN");
    if((CAPA == 32) && (MAX_HEAD == 2))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if((CAPA == 25) && (MAX_HEAD == 2) && (MES_EXPERT_INFO.MES_BI_COUNT < 2))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if(MAX_HEAD != 2)
    {
        str_exp_setdectype(1, 0);
        poststatus("OOB");
        userfilelog("EXP_BURN MissSET");
        stoptest();
        return;
    }

//IsSeekFail
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,8,1,1,1);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
		nRet = IsSeekFail(62,62,nHd,nHd,0,25,5);  //0 is OK, argument : int ntargetCyl,int ntargetHd,int nMode,int nLoop,float nthres
		if (nRet != 0)
		{
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
	    	sputd(EXPLOG,"IsSeekFail Hd:%d Fail]",nHd);
            userfilelog(EXPLOG);
		}
	}
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//CHK_ParameterMonitoring	
	//CHK_ParameterMonitoring();	//Paramtermonitoring : //AGC/ASC/MRR/CSM/ALARM/FODR/FODW/BER/SGAIN/QM/FODRES/TMPR [72]12*6
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        //ntemp = ParaMonLast[nHd * 12 + 2];	//2 = MRR
        if((ntemp != 0) && (ntemp < 0x100))        
        {
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
            sprintf("Hd:%d ParaMonLastMRR Fail:%d < 0x100]",nHd,ntemp);
            userfilelog(EXPLOG);
        }
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//IsServoGainFail
    nTTLHD_MAX_Sgain = 0;
    nTTLHD_MIN_Sgain = 1000;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        IsServoGainFail(62,nHd,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
        
        if(MAX_SGAIN[nHd] > nTTLHD_MAX_Sgain)
        {
            nTargetHD_MAX_Sgain = nHd;
            nTTLHD_MAX_Sgain = MAX_SGAIN[nHd];
        }
        if(MIN_SGAIN[nHd] < nTTLHD_MIN_Sgain)
        {
            nTargetHD_MIN_Sgain = nHd;
            nTTLHD_MIN_Sgain = MIN_SGAIN[nHd];
        }
    }
    if (nTTLHD_MAX_Sgain > 160)
    {
        SetDecisionHead(nTargetHD_MAX_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",nTargetHD_MAX_Sgain);
        userfilelog(EXPLOG);
    }
    if ((nTTLHD_MIN_Sgain != 0) && (nTTLHD_MIN_Sgain < 5))
    {
        SetDecisionHead(nTargetHD_MIN_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",nTargetHD_MIN_Sgain);
        userfilelog(EXPLOG);
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//IsBERFail
    nRet = 0;
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        Set_TestCondition(1,20,10,10,10);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        nRet = IsBERFail(62,nHd,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
        if (nRet != 0)
        {
            SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
            sputd(EXPLOG,"IsBERFail Hd:%d Fail",nHd);
            userfilelog(EXPLOG);
        }
    }
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nRet = GetDecisionHead(nHd);
        if(nRet != 0)	return;
    }

// BURN Rule Pass -> Re-STW
    CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
    return;
}


void EXP_CONT()
{
	int nHd, nZn, nRet, ntemp;
    int nTTLHD_MAX_Sgain, nTTLHD_MIN_Sgain, nTargetHD_MAX_Sgain, nTargetHD_MIN_Sgain;

    userfilelog("EXP_CONT");
    if(MAX_HEAD != 2)
    {
        userfilelog("EXP_CONT MissSET");
        str_exp_setdectype(1, 0);
        poststatus("OOB");
        stoptest();
        return;
    }

    if(BI_LASTSTEP == 0)
    {
        CHK_MESHistory(3,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if(BI_LASTSTEP < 3)
    {
        userfilelog("PCBA Engine : PAC003");
        str_exp_setdectype(8, 1);
        poststatus("PCBA");
        return;
    }
    if(BI_LASTSTEP < 33)
    {
        EXP_HTFail();
        ExpertDecision(0);        
        return;
    }

//IsSeekFail
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,8,1,1,1);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
		nRet = IsSeekFail(62,62,nHd,nHd,0,25,5);  //0 is OK, argument : int ntargetCyl,int ntargetHd,int nMode,int nLoop,float nthres
		if (nRet != 0)
		{
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
	    	sputd(EXPLOG,"IsSeekFail Hd:%d Fail] ",nHd);
            userfilelog(EXPLOG);
		}
	}
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//CHK_ParameterMonitoring	
	//CHK_ParameterMonitoring();	//Paramtermonitoring : //AGC/ASC/MRR/CSM/ALARM/FODR/FODW/BER/SGAIN/QM/FODRES/TMPR [72]12*6
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        //ntemp = ParaMonLast[nHd * 12 + 2];	//2 = MRR
        if((ntemp != 0) && (ntemp < 0x100))        
        {
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
            sprintf(EXPLOG,"Hd:%d ParaMonLastMRR Fail:%d < 0x100]",nHd,ntemp);
            userfilelog(EXPLOG);
        }
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//IsServoGainFail
    nTTLHD_MAX_Sgain = 0;
    nTTLHD_MIN_Sgain = 1000;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        IsServoGainFail(62,nHd,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
        
        if(MAX_SGAIN[nHd] > nTTLHD_MAX_Sgain)
        {
            nTargetHD_MAX_Sgain = nHd;
            nTTLHD_MAX_Sgain = MAX_SGAIN[nHd];
        }
        if(MIN_SGAIN[nHd] < nTTLHD_MIN_Sgain)
        {
            nTargetHD_MIN_Sgain = nHd;
            nTTLHD_MIN_Sgain = MIN_SGAIN[nHd];
        }
    }
    if (nTTLHD_MAX_Sgain > 160)
    {
        SetDecisionHead(nTargetHD_MAX_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",nTargetHD_MAX_Sgain);
        userfilelog(EXPLOG);
    }
    if ((nTTLHD_MIN_Sgain != 0) && (nTTLHD_MIN_Sgain < 5))
    {
        SetDecisionHead(nTargetHD_MIN_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",nTargetHD_MIN_Sgain);
        userfilelog(EXPLOG);
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//Downsizing
    sprintf(EXPLOG,"CAPA:%d MAX_HEAD:%d BI_LASTSTEP:%d MES_EXPERT_INFO.MES_BI_COUNT:%d",CAPA,MAX_HEAD,BI_LASTSTEP,MES_EXPERT_INFO.MES_BI_COUNT);
    userfilelog(EXPLOG);
    if((CAPA == 32) && (MAX_HEAD == 2) && (BI_LASTSTEP > 120) && (MES_EXPERT_INFO.MES_BI_COUNT <= 1))
    {
        CHK_MESHistory(3,9);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if((CAPA == 32) && (MAX_HEAD == 2) && (BI_LASTSTEP > 120) && (MES_EXPERT_INFO.MES_BI_COUNT > 1))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if((CAPA == 32) && (MAX_HEAD == 2) && (BI_LASTSTEP < 120))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if((CAPA == 25) && (MAX_HEAD == 2) && (MES_EXPERT_INFO.MES_BI_COUNT < 2))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }

//IsBERFail
    nRet = 0;
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        Set_TestCondition(1,20,2,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        nRet = IsBERFail(62,nHd,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
        if (nRet != 0)
        {
            SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
            sputd(EXPLOG,"IsBERFail Hd:%d Fail ",nHd);
            userfilelog(EXPLOG);
        }
    }
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nRet = GetDecisionHead(nHd);
        if(nRet != 0)	return;
    }
// CONT Rule Pass -> 28XX or D/S
    if(BI_LASTSTEP > 100)
    {
        EXP_L28XX();
        ExpertDecision(0);
        return;
    }
    else
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
}

void EXP_FTFail()
{
    int nRet, ret;
    char model[16];
    memset(model,0,0,16);

    userfilelog("EXP_FTFail");
    if(MES_EXPERT_INFO.MES_BI_COUNT <= 1)
    {
        if(CAPA == 32)
        {
            CHK_MESHistory(3,10);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            return;
        }
        if(CAPA == 25)
        {
            CHK_MESHistory(3,10);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            return;
        }
    }
    if((CAPA == 32) && (MAX_HEAD == 2) && (MES_EXPERT_INFO.MES_BI_COUNT <= 1))
    {
        CHK_MESHistory(3,10);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if((CAPA == 32) && (MAX_HEAD == 2) && (MES_EXPERT_INFO.MES_BI_COUNT > 1))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if((CAPA == 25) && (MAX_HEAD == 2) && (MES_EXPERT_INFO.MES_BI_COUNT <= 2))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    if((CAPA == 25) && (MAX_HEAD == 2) && (MES_EXPERT_INFO.MES_BI_COUNT > 2))
    {
        nRet = DOWNLOAD_EXPBICODE(50);
        if(nRet != 0)
        {
            return;
        }        
        EXP_BIEND();
        return;
    }
    else
	{
        str_exp_setdectype(1, 0);
		poststatus("UnDef Model");
    	stoptest();    	
    }
}

void EXP_BIFail()
{
    int led, icyl, ihd;
    userfilelog("EXP_BIFail");
    sputd(EXPLOG,"LED : %x",BI_ERRORCODE);
    userfilelog(EXPLOG);
	
	switch(BI_ERRORCODE)
	{
		// LATCH or Ramp Error
		case 0x1107:	//latch force too low
		case 0x1108:	//latch force too high
            str_exp_setdectype(6, 7);
            poststatus("VCM Fail");
            strcpy(REPAIRCODE,"V00000");	//VCM Fail
            Send_Debug_info();        
            stoptest();
            break;

		// SPSTW (offline) Error		
		case 0x1314:	//Difference of bias calibration value is big
        if(MES_EXPERT_INFO.MES_STW_COUNT <= 1)
            {
                CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                break;
            }
            else
            {
                sputd(EXPLOG,"0x%d Fail : Head Test",BI_ERRORCODE);
                userfilelog(EXPLOG);
                SetDecisionHeadTest();
                stoptest();
                break;
            }
	
		// Servo_Calibration Error
		case 0x1601:	//bias calibration error
		case 0x1602:	//track zero not found error
		case 0x1603:	//NP-offset test result was bad.
		case 0x1604:	//AB-slope test result was bad.
		case 0x1606:	//loop calibration  was bad.
		case 0x1607:	//head skew calbration result was bad.
		case 0x1608:	//head gap calibration is failed.
		case 0x1702:	//seek error
	        EXP_HTFail();
            ExpertDecision(0);
			break;
	
		case 0x1704:	//too many parking during process
            EXP_L1704();
            ExpertDecision(0);
            break;
            
        case 0x1705:	//Spin up too long
            userfilelog("1705 Fail : PCBA");
            str_exp_setdectype(8, 1);
            poststatus("PCBA");
            strcpy(REPAIRCODE,"PAA002");
            Send_Debug_info();        
            stoptest();
            break;
	
		// MC Access Error
		case 0x1A03:	//MC WRITE ERROR
		case 0x1A04:	//MC READ ERROR
		    EXP_L1AXX();
            ExpertDecision(0);
			break;
        // H/M Screen
        case 0x1F01:    //Head/Media interference fail
        case 0x1F02:    //Interfere test fail
        case 0x1F03:    //Failed Amplitude Spike Test
        case 0x1F04:    //WII TEST FAIL
        case 0x1F05:    //Thermal Decay
        case 0x1F06:    //Head Degradation screen
        case 0x1F07:    //BIG diff AGC between tracks
        case 0x1F08:    //hdi problem
		case 0x1F09:	//LED_BAD_CSM_DEFECT_OD
		case 0x1F0A:	//LED_BAD_CSM
            EXP_L28XX();
            ExpertDecision(0);
			break;

		// BER Error(0x1B01) , AZL Error(0x1C01)
		case 0x1B01:	//error rate too high
		case 0x1C01:	//error rate too high
		// timeout Error
        case 0x2201:    //SLOW_RUN_ERR
        case 0x2202:    //CONT_TEST
        case 0x2203:    //timeout              
        case 0x2204:    //                      
        case 0x2205:    //IPC related error    
        case 0x2206:    //function test timeout
            poststatus("LED 4X");
            EXP_L1B01();
            ExpertDecision(0);
			break;
	
		// FOD Error
        case 0x2101:    //Write Error when HSC test                     
        case 0x2102:    //Read Error when HSC test                      
        case 0x2103:    //FOD resistance test before FOD touch down test
        case 0x2104:    //FOD TD fail: channel table verify failure     
        case 0x2105:    //HSC FH Error when screen                      
        case 0x2106:    //HSC TCC Error when screen                     
        case 0x2107:                                                   
        case 0x2108:    //FOD TD fail: too high fluxion                 
        case 0x2109:    //Out of TD test range                          
        case 0x210A:    //fod resistance bad                            
        case 0x210B:    //MC read error during FOD test                 
        case 0x210C:    //MC write error during FOD test                
        case 0x210D:    //FH one step difference is too big             
        case 0x210E:    //No good zone                                  
        case 0x210F:    //FOD TCC make fail                             
		//Process_Misc
		case 0x2301:	//channel calibration fail
		case 0x2303:	//O Pattern write fail after rco
		case 0x2304:	//general error / Read Screen Error 
		case 0x2307:	//script error
			EXP_L2105();
            ExpertDecision(0);
			break;
			
		case 0x2305:	//Failed compiling
		case 0x2306:	//Unrecognized Opcode (opcode cmd function not available)
		case 0x2308:	//Firmware Assert
		case 0x2309:	//2nd Downsize Error
            CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            break;

		case 0x230A:	//5400Rpm DownSpeed Criteria : Zone Cutting bug
		case 0x2353:	//5400Rpm DownSpeed Criteria : Zone Cutting bug
            EXP_L2105();
            ExpertDecision(0);
            break;

        //Version Mismatch
		case 0x2501:	//Overlay version incorrect
		case 0x2502:	//wrong code download
		case 0x2503:	//Serial Number checksum error
		case 0x2504:	//OVLY_ERR
		case 0x2505:	//NEW_ID
		case 0x2506:	//overlay checksum
		case 0x2507:	//Can't find MRR look up table
            CHK_MESHistory(3,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            break;

		// SERVO DEFECT Error
		case 0x1901:	//Drive per servo vitual defect over
		case 0x1902:	//Drive per servo defect over
		case 0x1903:	//Drive per servo real defect over
		case 0x1904:	//Drive per track defect over
		case 0x1905:	//zone per track defect over
		case 0x1F09:	//LED_BAD_CSM_DEFECT_OD
		case 0x1F0A:	//LED_BAD_CSM
		//Defect Overflow
		case 0x2801:	//Error log in full
		case 0x2807:	//linear scratch
		case 0x2809:	//2MANY_DLIST_IN_BUF
		case 0x2810:	//2MANY_WR_ERR
		case 0x2811:	//2MANY_SEQ_TRK_ERR
		case 0x2812:	//2MANY_BAD_TRACKS
		case 0x2813:	//DEFECT_IN_CYL0
		case 0x2814:	//CAPACITY_CHK_ERR
		case 0x2815:	//LED_VLIST_SF_OVFLOW 
		case 0x2816:	//TNU_PER_HEAD_OVER
		case 0x2818:	//LOGIN_FULL_MC
		case 0x2819:	//2MANY_SLIST
		case 0x0000:	//Undef Fail
            EXP_L28XX();
            ExpertDecision(0);
			break;
	
		//IC(preamp, channel, engine, FFS)
		case 0x2A01:	//preamp is out of order
		case 0x2A02:	//preamp voltage regulator err
            str_exp_setdectype(6, 8);
	    	poststatus("HSA PreAmp");
            strcpy(REPAIRCODE,"H11012");	//HSA PreAmp
            Send_Debug_info();        
	    	stoptest();
			break;
	
		case 0x2A03:	//chip rev error
		case 0x2A04:	//NLS Register error
		case 0x2A05:	//Linked List Register error
		case 0x2A06:	//Free Fall Sensor is out of order
            str_exp_setdectype(8, 1);
	    	poststatus("PCBA");
            strcpy(REPAIRCODE,"PAA018");	//HSA PreAmp
            Send_Debug_info();        
	    	stoptest();
			break;
	
		//Ram/buffer
		case 0x2B01:	//CPURAM_ERR
		case 0x2B02:	//SEQRAM_ERR
		case 0x2B03:	//BUFRAM_ERR
		case 0x2B04:	//FLASH_ERR
		case 0x2B06:	//Servo SPIRAM/YRAM/XRAM error
		case 0x2B07:	//ECCRAM_ERR
		case 0x2B08:	//Hybrid ONENAND flash error
		case 0x2B09:	//BufferAlloc reuturned NULL
            str_exp_setdectype(8, 1);
	    	poststatus("PCBA");
            strcpy(REPAIRCODE,"PAA002");
            Send_Debug_info();        
	    	stoptest();
			break;
	
		// OEM screen
		case 0x2C04:	//Drive per track defect over
		case 0x2C05:	//too many parking during process
		case 0x2C07:	//TPI test fail
		case 0x2C0C:	//LED_SCREEN_MOST_OD
            CHK_MESHistory(4,1);
            break;

        default:
            userfilelog("No Define Failure");
            str_exp_setdectype(1, 0);
            poststatus("OOB");
            stoptest();
            break;
	}
}

void EXP_L1AXX()
{
	int nHd, nZn, nCnt, nCnt, nRet;
	int ntemp, ntemp0, ntemp1, ntemp2, ntemp3;
    int nTTLHD_MAX_Sgain, nTTLHD_MIN_Sgain, nTargetHD_MAX_Sgain, nTargetHD_MIN_Sgain;

    userfilelog("EXP_L1AXX");
    str_exp_setdectype(1, 0);
    poststatus("GoWinstar");
    stoptest();

/*    
    //poststatus("EXP_L1AXX");
//IsSeekFail
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,8,1,1,1);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
		nRet = IsSeekFail(62,62,nHd,nHd,0,25,5);  //0 is OK, argument : int ntargetCyl,int ntargetHd,int nMode,int nLoop,float nthres
		if (nRet != 0)
		{
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
	    	sputd(EXPLOG,"IsSeekFail Hd:%d Fail]",nHd);
            userfilelog(EXPLOG);
		}
        Set_TestCondition(1,8,1,1,1);    //(int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry)
		nRet = IsSeekFail(62,62,nHd,nHd,1,25,5);  //0 is OK, argument : int ntargetCyl,int ntargetHd,int nMode,int nLoop,float nthres
		if (nRet != 0)
		{
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
	    	sputd(EXPLOG,"IsSeekFail Hd:%d Fail]",nHd);
            userfilelog(EXPLOG);
		}
	}
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//CHK_ParameterMonitoring	
	//CHK_ParameterMonitoring();	//Paramtermonitoring : //AGC/ASC/MRR/CSM/ALARM/FODR/FODW/BER/SGAIN/QM/FODRES/TMPR [72]12*6
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        //ntemp = ParaMonLast[nHd*12+2];	//2 = MRR
        if((ntemp != 0) && (ntemp < 0x100))
        {
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
            sprintf(EXPLOG,"Hd:%d ParaMonLastMRR Fail:%d < 0x100]",nHd,ntemp);
            userfilelog(EXPLOG);
        }
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//CHK_TrkSrvTargetHDandMaxRatio , CHK_DefTargetHDandMaxRatio
//SRVCONTINOUS & DEFCONTINOUS
//CHK_DList, CHK_ELogList
    if(MASS == 0)
    {
        MakeMassData();     //PP CMD
        CHK_DList();		// int TBL_DLIST[MAX_HEAD][MAX_ZONE]
        CHK_ELogList();		// int TBL_ELOG[MAX_HEAD][MAX_ZONE]
    }
    else
    {
        CHK_HTDList(500);		// int TBL_DLIST[MAX_HEAD][MAX_ZONE]
        CHK_HTELogList();	// int TBL_ELOG[MAX_HEAD][MAX_ZONE]
    }
    CHK_DefTargetHDandMaxRatio();	    //Get Data : DEF_MAXHDRATE, DEF_MAXDEFTARGETHD
                                        //ContinousCheck(nHd,nCyl,nSkew,50,5,300)
    CHK_TrkSrvTargetHDandMaxRatio();	//Get Data : TRKSRV_MAXHDRATE, TRKSRV_MAXDEFTARGETHD
                                        //ContinousCheck(nHd,nCyl,nSkew,50,5,50)
	sputd(EXPLOG,"DEF_TTLDEFCNT : %d", DEF_TTLDEFCNT);
    userfilelog(EXPLOG);
	sputd(EXPLOG,"TRKSRV_TTLDEFCNT : %d", TRKSRV_TTLDEFCNT);
    userfilelog(EXPLOG);
    if(DEF_TTLDEFCNT > 5000)
    {
        MAXHDRATE = DEF_MAXHDRATE;
        MAXDEFTARGETHD = DEF_MAXDEFTARGETHD;    
    }
    else
    {
        if(TRKSRV_TTLDEFCNT > 500)
        {
            MAXHDRATE = TRKSRV_MAXHDRATE;
            MAXDEFTARGETHD = TRKSRV_MAXDEFTARGETHD;
        }
        else
        {
            MAXHDRATE = 0;
            MAXDEFTARGETHD = 0xff;
        }
    }
    CHK_DiskDetect(300);    // DEFCONTINOUS
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		ntemp=SRVCONTINOUS[nHd];
		if(ntemp != 0)
		{
			SetDecisionDisk(nHd);
	    	sprintf(EXPLOG,"SrvCont nDK:%d Fail [ContThres:%d >= 50]",nHd,ntemp);
            userfilelog(EXPLOG);
		}
        printf(EXPLOG,"SrvCont nDK:%d Pass [ContThres:%d >= 50]",nHd,ntemp);
        userfilelog(EXPLOG);
		ntemp1=DEFCONTINOUS[nHd];
		if(ntemp1 != 0)
		{
			SetDecisionDisk(nHd);
	    	sprintf(EXPLOG,"DefCont nDK:%d Fail [ContThres:%d >= 300]",nHd,ntemp1);
            userfilelog(EXPLOG);
		}
		sprintf(EXPLOG,"DefCont nDK:%d Pass [ContThres:%d >= 300]",nHd,ntemp);
        userfilelog(EXPLOG);
	}
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionDisk(nHd);
		sputd(EXPLOG,"DefCont nRet:%d",nRet);
        userfilelog(EXPLOG);
		if(nRet != 0)	return;
	}

    CHK_DiskDefect_L3B(70);
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		if(BAD_DISK_3B[nHd] == CHAR_X)
		{
            str_exp_setdectype(1, 0);
            poststatus("DSK_GoExp");
            stoptest();
		}
		sprintf(EXPLOG,"DefCont nDK:%d Pass [ContThres:%d >= 300]",nHd,ntemp);
        userfilelog(EXPLOG);
	}
//MAXDEFTARGETHD
	sprintf(EXPLOG,"EXP_L1AXX TargetHd:%d Rate:%d ",MAXDEFTARGETHD,MAXHDRATE);
    userfilelog(EXPLOG);
    if(MAXDEFTARGETHD != 0xff)
    {
        CHK_TPI74710SData(MAXDEFTARGETHD);
        if(MAXHDRATE > 600)	//MAXHDRATE 60.0%
        {
            //RefBER
            if(MAXREFBER < 5.8)	//RefCnt 128102 = RefBER : 5.8
            {
                SetDecisionHead(MAXREFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"MAXREFBER nHd:%d Fail [RefBER:%5.2f < 5.8]",MAXDEFTARGETHD,MAXREFBER);
                userfilelog(EXPLOG);
                return;
            }
            //747BER
            if(MAX747BER < 4.6)	//747Cnt 128716 = 747BER : 4.6
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"MAX747BER nHd:%d Fail [747BER:%5.2f < 4.6]",MAXDEFTARGETHD,MAX747BER);
                userfilelog(EXPLOG);
                return;
            }
            //10SBER
            if(MAX10SBER > 5.2)	//ErrCnt 15154 = n10Stemp : 5.20 (read Cnt 600000)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"MAX10SBER nHd:%d Fail [10SBER:%5.2f < 5.2]",MAXDEFTARGETHD,MAX10SBER);
                userfilelog(EXPLOG);
                return;
            }
            //Marginal Head Fail : Check 10SBER, RefBER, 747BER
            if((MAX10SBER > 5.5) && (MAXREFBER > 5.9))	//ErrCnt7770 = 10sBER : 5.5 (read Cnt 600000)
                                                        //RefCnt134092 = RefBER : 5.9
            {
                if(MAX10STARGETHD == MAXREFTARGETHD)
                {
                    SetDecisionHead(MAX10STARGETHD,HEAD_FAIL);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 600 HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
                else
                {
                    SetDecisionHead(MAXREFTARGETHD,HEAD_SWAP);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 600 HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
            }
            if((MAX10SBER > 5.8) || (MAXREFBER > 5.9))  //MAXREFBER 128051 : 5.9
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                sprintf(EXPLOG,"Target nHd:%d Swap [MAX10SBER:%5.2f > 5.8] || [MAXREFBER:%5.2f > 5.9]",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                userfilelog(EXPLOG);
                return;
            }
            Set_TestCondition(0,4,0,2,1);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);		//type 0:Good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                sputd(EXPLOG,"EXP_L1AXX Rate(>600) Hd:%d IsBERFail : Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
            else
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);		//type 0:Good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                sputd(EXPLOG,"EXP_L1AXX Rate(>600) Hd:%d IsBERFail : Pass",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
        }
        else if(MAXHDRATE > 400)	//MAXHDRATE 40.0%
        {
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                sprintf(EXPLOG,"EXP_L1AXX Rate(%d>400) Hd:%d IsBER1Fail",MAXHDRATE,MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
            Set_TestCondition(0,4,2,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                sprintf(EXPLOG,"EXP_L1AXX Rate(%d>400) Hd:%d IsBER2Fail",MAXHDRATE,MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
            else
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                sprintf(EXPLOG,"EXP_L1AXX Rate(%d>400) Hd:%d All Pass",MAXHDRATE,MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
        }
        else if(MAXHDRATE > 300)	//MAXHDRATE 30.0%
        {
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                sputd(EXPLOG,"IsBERFail1 Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
                return;
            }
            Set_TestCondition(0,4,2,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                sputd(EXPLOG,"IsBERFail2 Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
                return;
            }
        }
        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
    else
    {
//CHK_GetTrackandServoDefect
    	CHK_GetTrackandServoDefect(0);	//Summary TrkSrvDef & Check Servo Defect Continous
    									//ContinousCheck(nHd,nCyl,nSkew,50,5,50);
    	for(nHd=0; nHd<MAX_HEAD; nHd++)
    	{
    		ntemp=SRVCONTINOUS[nHd];
    		if(ntemp != 0)
    		{
	        	SetDecisionDisk(nHd);
    	    	sprintf(EXPLOG,"SrvCont nDK:%d Fail [ContThres:%d >= 50]",nHd,ntemp);
                userfilelog(EXPLOG);
    		}
    	}
//CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MAXFODTARGETHD
        CHK_FODMaxandMinValue(0xff);	//0xff -> All Head Fail
        nCnt = 0;
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            ntemp0 = HDMAXFODFH[nHd];
            ntemp1 = HDMINFODFH[nHd];
            ntemp2 = HDMAX2NDFODFH[nHd];
            ntemp3 = HDMIN2NDFODFH[nHd];
            if( (ntemp0 >= 70866) && (ntemp2 >= 70000) )
            {
                SetDecisionHead(nHd,HEAD_FAIL);
                sprintf(EXPLOG,"FODFHMax_Hd:%d Fail [HDMAXFODFH:%d >= 70866][HDMAX2NDFODFH:%d >= 70866]",nHd,ntemp0,ntemp2);
                userfilelog(EXPLOG);
                nCnt++;
            }
            if((ntemp1 != 0) && (ntemp3 != 0) && (ntemp1 <= 19685) && (ntemp3 <= 20000))
            {
                SetDecisionHead(nHd,HEAD_FAIL);
                sprintf(EXPLOG,"FODFHMin_Hd:%d Fail [HDMINFODFH:%d <= 19685][HDMIN2NDFODFH:%d <= 19685]",nHd,ntemp1,ntemp3);
                userfilelog(EXPLOG);
                nCnt++;
            }
        }
        if(nCnt != 0)   return;
//IsServoGainFail
        nTTLHD_MAX_Sgain = 0;
        nTTLHD_MIN_Sgain = 1000;
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            IsServoGainFail(62,nHd,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
            
            if(MAX_SGAIN[nHd] > nTTLHD_MAX_Sgain)
            {
                nTargetHD_MAX_Sgain = nHd;
                nTTLHD_MAX_Sgain = MAX_SGAIN[nHd];
            }
            if(MIN_SGAIN[nHd] < nTTLHD_MIN_Sgain)
            {
                nTargetHD_MIN_Sgain = nHd;
                nTTLHD_MIN_Sgain = MIN_SGAIN[nHd];
            }
        }
        if (nTTLHD_MAX_Sgain > 160)
        {
            SetDecisionHead(nTargetHD_MAX_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
            sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",nTargetHD_MAX_Sgain);
            userfilelog(EXPLOG);
        }
        if ((nTTLHD_MIN_Sgain != 0) && (nTTLHD_MIN_Sgain < 5))
        {
            SetDecisionHead(nTargetHD_MIN_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
            sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",nTargetHD_MIN_Sgain);
            userfilelog(EXPLOG);
        }
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            nRet = GetDecisionHead(nHd);
            if(nRet != 0)	return;
        }

//IsBERFail 1
    	for(nHd=0; nHd<MAX_HEAD; nHd++)
    	{
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,nHd,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
    		if (nRet != 0)
    		{
    	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
    	    	sputd(EXPLOG,"IsBERFail Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
    		}
    	}
    	for(nHd=0; nHd<MAX_HEAD; nHd++)
		{
			nRet = GetDecisionHead(nHd);
			if(nRet != 0)	return;
		}
        //////////////////////////////////////////
        //		1AXX No Matrial Fail -> STW
        //////////////////////////////////////////
        if((MAX_HEAD == 2) && (MES_EXPERT_INFO.MES_BI_COUNT <= 1))
        {
            CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            return;
        }
        else
        {
            CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            return;
        }
    }
*/
}

void EXP_L1704()
{
    int led, icyl, ihd;
    
    if((INVALID_LED == 0x1702) && (INVALID_LED == 0x1704) && (MES_EXPERT_INFO.MES_HEAD_DMR != CHAR_D))
    {
        SetDecisionHeadTest();
        userfilelog("SC fail HSA Test");
        stoptest();
    }
    else
    {
        /*
        ONERROR(dummyerror);
        str_usercmd("RF 1", 4, 5000, 3584, 1, RDBUF, 512);
        ONERRORCLEAR();
        
        peek(RDBUF,0x100,4,icyl);
        peek(RDBUF,0x105,1,ihd);
        peek(RDBUF,0xDE,2,led);
        sprintf(EXPLOG,"L%x icyl:%x ihd:%x",led,icyl,ihd);
        userfilelog(EXPLOG);
        
        if((led != 0x00) && (icyl != 0xFFFF) && (ihd != 0xFF) && (ihd >= MAX_HEAD))
        {
            str_exp_setdectype(6, 1);
            SetDecisionHead(ihd, HEAD_FAIL);	//type 1 : Head Fail
            sputd(EXPLOG,"HD:%d Fail",ihd);
            userfilelog(EXPLOG);
            poststatus(EXPLOG);
            ExpertDecision(0);
            stoptest();
        }
        else
        {
            str_exp_setdectype(6, 5);
            poststatus("HeadTest");
            userfilelog("L1704 Head Test");
            strcpy(REPAIRCODE,"H11000");	//HSA Test
            Send_Debug_info();
            stoptest();
        }
        */
        SetDecisionHeadTest();
        userfilelog("L0x1704 HSA Test");
        stoptest();
    }
}

void EXP_L1B01()
{
	int nHd,nZn,nCnt,nValue,nTargetHD;
	int nTPI747CntTTL, nTPIRefCntTTL;
	int ntemp,ntemp0,ntemp1,ntemp2,ntemp3;
	int nReftemp, n747temp, n10Stemp;
	
	float fBER;

    userfilelog("EXP_L1B01");

    if((CAPA == 32) && (MAX_HEAD == 2))   //MAX_HEAD : ch 0,1,2,3,4,5
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }

    str_exp_setdectype(1, 0);
    poststatus("GoWinstar");
    stoptest();
    
/*
//CHK_GetTrackandServoDefect
	CHK_GetTrackandServoDefect(0);	//Summary TrkSrvDef & Check Servo Defect Continous
									//ContinousCheck(nHd,nCyl,nSkew,50,5,50);
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		ntemp=SRVCONTINOUS[nHd];
		if(ntemp != 0)
		{
	        SetDecisionDisk(nHd);
	    	sprintf(EXPLOG,"SrvCont nDK:%d Fail [ContThres:%d >= 50]",nHd,ntemp);
            userfilelog(EXPLOG);
		}
	}
//CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MAXFODTARGETHD
	CHK_FODMaxandMinValue(0xff);	//0xff -> All Head Check

    sprintf(EXPLOG,"CHK_FOD MAXFODFH:%d, MAXFODTARGETHD:%d",MAXFODFH,MAXFODTARGETHD);
    userfilelog(EXPLOG);
    sprintf(EXPLOG,"CHK_FOD MINFODFH:%d, MINFODTARGETHD:%d",MINFODFH,MINFODTARGETHD);
    userfilelog(EXPLOG);
//CHK_GetTPIand747 : Get Data MAXREFBER,MAXREFTARGETHD, MAX747BER,MAX747TARGETHD, MAX10SBER,MAX10STARGETHD
    CHK_TPI74710SData(0xff);	        //0xff -> All Head Check

    sprintf(EXPLOG,"TPI74710S MAXREFBER:%5.2f, MAXREFTARGETHD:%d",MAXREFBER,MAXREFTARGETHD);
    userfilelog(EXPLOG);
    sprintf(EXPLOG,"TPI74710S MAX747BER:%5.2f, MAX747TARGETHD:%d",MAX747BER,MAX747TARGETHD);
    userfilelog(EXPLOG);
    sprintf(EXPLOG,"TPI74710S MAX10SBER:%5.2f, MAX10STARGETHD:%d",MAX10SBER,MAX10STARGETHD);
    userfilelog(EXPLOG);
    //RefBER
    nCnt = 0;
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        ntemp0 = HDMAXFODFH[nHd];
        ntemp1 = HDMINFODFH[nHd];
        ntemp2 = HDMAX2NDFODFH[nHd];
        ntemp3 = HDMIN2NDFODFH[nHd];
        if( (ntemp0 >= 70000) && (ntemp2 >= 70000) )
        {
            SetDecisionHead(nHd,HEAD_FAIL);
            sprintf(EXPLOG,"FODFHMax_Hd:%d Fail [HDMAXFODFH:%d >= 70866][HDMAX2NDFODFH:%d >= 70866]",nHd,ntemp0,ntemp2);
            userfilelog(EXPLOG);
            nCnt++;
        }
        if((ntemp1 != 0) && (ntemp3 != 0) && (ntemp1 <= 20000) && (ntemp3 <= 20000))
        {
            SetDecisionHead(nHd,HEAD_FAIL);
            sprintf(EXPLOG,"FODFHMin_Hd:%d Fail [HDMINFODFH:%d <= 19685][HDMIN2NDFODFH:%d <= 19685]",nHd,ntemp1,ntemp3);
            userfilelog(EXPLOG);
            nCnt++;
        }
    }
    if(nCnt != 0)   return;

    if(MAXREFBER < 5.5)	//RefCnt133888 = RefBER : 5.50
    {
    	SetDecisionHead(MAXREFTARGETHD,HEAD_FAIL);
        sprintf(EXPLOG,"HD:%d MAXREFBER:%5.2f",MAXREFTARGETHD,MAXREFBER);
        userfilelog(EXPLOG);
    	return;
    }
	//747BER
    if(MAX747BER < 4.5)	//747Cnt133376 = 747BER : 4.50
    {
    	SetDecisionHead(MAX747TARGETHD,HEAD_FAIL);
        sprintf(EXPLOG,"HD:%d MAX747BER:%5.2f",MAX747TARGETHD,MAX747BER);
        userfilelog(EXPLOG);
    	return;
    }
	//10SBER
    if(MAX10SBER < 5.5)	//ErrCnt7770 = RefBER : 5.50 (read Cnt 600000)
    {
    	SetDecisionHead(MAX10STARGETHD,HEAD_FAIL);
        sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f",MAX10STARGETHD,MAX10SBER);
        userfilelog(EXPLOG);
    	return;
    }
    //Marginal Head Fail : Check 10SBER, RefBER, 747BER
    if((MAX10SBER < 5.4) && (MAXREFBER < 5.9))	//ErrCnt2457 = 10sBER : 5.4 (read Cnt 600000)
                                                //RefCnt134092 = RefBER : 5.9
    {
        if(MAX10STARGETHD == MAXREFTARGETHD)
        {
            SetDecisionHead(MAX10STARGETHD,HEAD_FAIL);
            sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f MAXREFBER:%d",MAX10STARGETHD,MAX10SBER,MAXREFBER);
            userfilelog(EXPLOG);
            return;
        }
        else
        {
            SetDecisionHead(MAXREFTARGETHD,HEAD_FAIL);
            sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f MAXREFBER:%d",MAXREFTARGETHD,MAX10SBER,MAXREFBER);
            userfilelog(EXPLOG);
            return;
        }
    }
    if(MAX10SBER < 6.0)
        //ErrCnt2457 = RefBER : 6.0 (read Cnt 600000) , RefCnt2816 = RefBER : 5.9
    {
        CHK_FODMaxandMinValue(MAX10STARGETHD);
        if((MINFODFH != 0) && ((MAXFODFH >= 60000) || (MINFODFH <= 25000)))
        {
            if( (MAX10STARGETHD == MAXFODTARGETHD) || (MAX10STARGETHD == MINFODTARGETHD) )
            {
                SetDecisionHead(MAX10STARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f MAXFODFH:%d MINFODFH:%d",MAX10STARGETHD,MAX10SBER,MAXFODFH,MINFODFH);
                userfilelog(EXPLOG);
                return;
            }
            else
            {
                SetDecisionHead(MAX10STARGETHD,HEAD_SWAP);
                sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f MAXFODFH:%d MINFODFH:%d",MAX10STARGETHD,MAX10SBER,MAXFODFH,MINFODFH);
                userfilelog(EXPLOG);
                return;
            }
        }
    }
    if((MAXREFBER > 5.7))
    {
        CHK_FODMaxandMinValue(MAXREFTARGETHD);
        if((MINFODFH != 0) && ((MAXFODFH >= 60000) || (MINFODFH <= 25000)))
        {
            if( (MAXREFTARGETHD == MAXFODTARGETHD)  || (MAXREFTARGETHD == MINFODTARGETHD) )
            {
                SetDecisionHead(MAXFODTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f MAXFODFH:%d MINFODFH:%d",MAXREFTARGETHD,MAX10SBER,MAXFODFH,MINFODFH);
                userfilelog(EXPLOG);
                return;
            }
            else
            {
                SetDecisionHead(MAXREFTARGETHD,HEAD_SWAP);
                printf("HD:%d MAX10SBER:%5.2f MAXFODFH:%d MINFODFH:%d",MAXREFTARGETHD,MAX10SBER,MAXFODFH,MINFODFH);
                userfilelog(EXPLOG);
                return;
            }            
        }
    }
///////////////////////////////////////////////
//		1B01 No Matrial Fail -> OOB          //
///////////////////////////////////////////////
    userfilelog("1B01 No Matrial Fail -> OOB");
    str_exp_setdectype(1, 0);
    poststatus("OOB");
    return;
*/
}

void EXP_L2105()
{
	int nRet,nHd,nZn,nCnt,nValue,nTargetHD;
	int nTPI747CntTTL, nTPIRefCntTTL;
	int ivalue,ntemp,ntemp0,ntemp1,ntemp2,ntemp3;
	int nReftemp, n747temp, n10Stemp;
    int nTTLHD_MAX_Sgain, nTTLHD_MIN_Sgain, nTargetHD_MAX_Sgain, nTargetHD_MIN_Sgain;
	
	float fBER;

    userfilelog("EXP_L2105");

    str_exp_setdectype(1, 0);
    poststatus("GoWinstar");
    stoptest();
    
/*
//IsServoGainFail
    nTTLHD_MAX_Sgain = 0;
    nTTLHD_MIN_Sgain = 1000;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        IsServoGainFail(62,nHd,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
        
        if(MAX_SGAIN[nHd] > nTTLHD_MAX_Sgain)
        {
            nTargetHD_MAX_Sgain = nHd;
            nTTLHD_MAX_Sgain = MAX_SGAIN[nHd];
        }
        if(MIN_SGAIN[nHd] < nTTLHD_MIN_Sgain)
        {
            nTargetHD_MIN_Sgain = nHd;
            nTTLHD_MIN_Sgain = MIN_SGAIN[nHd];
        }
    }
    sprintf(EXPLOG,"nTTLHD_MAX_Sgain:%d",nTTLHD_MAX_Sgain);
    userfilelog(EXPLOG);
    sprintf(EXPLOG,"nTTLHD_MIN_Sgain:%d",nTTLHD_MIN_Sgain);
    userfilelog(EXPLOG);

    ivalue = RDY_MRR[nTargetHD_MAX_Sgain];
    sprintf(EXPLOG,"Head[%d] RDY_MRR:%x",nTargetHD_MAX_Sgain,ivalue);
    userfilelog(EXPLOG);

    if((ivalue != 0) && (ivalue < 256))
    {
        if (nTTLHD_MAX_Sgain > 200)
        {
            SetDecisionHead(nTargetHD_MAX_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
            sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",nTargetHD_MAX_Sgain);
            userfilelog(EXPLOG);
        }
    }
    if(ivalue > 700)
    {
        if ((nTTLHD_MIN_Sgain != 0) && (nTTLHD_MIN_Sgain < 5))
        {
            SetDecisionHead(nTargetHD_MIN_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
            sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",nTargetHD_MIN_Sgain);
            userfilelog(EXPLOG);
        }
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//320G -> Downsizing
    if((CAPA == 32) && (MAX_HEAD == 2))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }
	
//CHK_GetTrackandServoDefect
    userfilelog("CHK_GetTrackandServoDefect start");
	CHK_GetTrackandServoDefect(0);	//Summary TrkSrvDef & Check Servo Defect Continous
									//ContinousCheck(nHd,nCyl,nSkew,50,5,50);
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		ntemp=SRVCONTINOUS[nHd];
		if(ntemp != 0)
		{
			SetDecisionDisk(nHd);
	    	sprintf(EXPLOG,"SrvCont nDK:%d Fail [ContThres:%d >= 50]",nHd,ntemp);
            userfilelog(EXPLOG);
		}
	}

//CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MAXFODTARGETHD
    userfilelog("CHK_FODMaxandMinValue start");
    nCnt = 0;
	CHK_FODMaxandMinValue(0xff);	//0xff -> All Head Fail
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        ntemp0 = HDMAXFODFH[nHd];
        ntemp1 = HDMINFODFH[nHd];
        ntemp2 = HDMAX2NDFODFH[nHd];
        ntemp3 = HDMIN2NDFODFH[nHd];
        if( (ntemp0 >= 70866) && (ntemp2 >= 70000) )
        {
            sprintf(EXPLOG,"FODFHMax_Hd:%d Fail [HDMAXFODFH:%d >= 70866][HDMAX2NDFODFH:%d >= 70866]",nHd,ntemp0,ntemp2);
            userfilelog(EXPLOG);
            SetDecisionHead(nHd,HEAD_FAIL);
            nCnt++;
        }
        if((ntemp1 != 0) && (ntemp3 != 0) && (ntemp1 <= 19685) && (ntemp3 <= 20000))
        {
            sprintf(EXPLOG,"FODFHMin_Hd:%d Fail [HDMINFODFH:%d <= 19685][HDMIN2NDFODFH:%d <= 19685]",nHd,ntemp1,ntemp3);
            userfilelog(EXPLOG);
            SetDecisionHead(nHd,HEAD_FAIL);
            nCnt++;
        }
    }
    if(nCnt != 0)
    {
        userfilelog("FODFail");
        return;
    }

//CHK_GetTPIand747 : Get Data //MAXREFBER,MAXREFTARGETHD, MAX747BER,MAX747TARGETHD, MAX10SBER,MAX10STARGETHD
    userfilelog("CHK_TPI74710SData start");
    CHK_TPI74710SData(0xff);
    //RefBER
    if(MAXREFBER < 5.5)	//RefCnt133888 = RefBER : 5.50
    {
    	SetDecisionHead(MAXREFTARGETHD,HEAD_FAIL);
    	sprintf(EXPLOG,"HD:%d MAXREFBER:%5.2f",MAXREFTARGETHD,MAXREFBER);
        userfilelog(EXPLOG);
    	return;
    }
	//747BER
    if(MAX747BER < 4.5)	//747Cnt133376 = 747BER : 4.50
    {
    	SetDecisionHead(MAX747TARGETHD,HEAD_FAIL);
    	sprintf(EXPLOG,"HD:%d MAX747BER:%5.2f",MAX747TARGETHD,MAX747BER);
        userfilelog(EXPLOG);
    	return;
    }
	//10SBER
    if(MAX10SBER < 5.5)	//ErrCnt7770 = RefBER : 5.50 (read Cnt 600000)
    {
    	SetDecisionHead(MAX10STARGETHD,HEAD_FAIL);
    	sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f",MAX10STARGETHD,MAX10SBER);
        userfilelog(EXPLOG);
    	return;
    }
//Marginal Head Fail : Check 10SBER, RefBER, 747BER
    if(MAX10SBER < 6.0 && MAXREFBER < 5.9)	//ErrCnt2457 = RefBER : 6.0 (read Cnt 600000)
									    		//RefCnt134092 = RefBER : 5.9
    {
        if(MAX10STARGETHD == MAXREFBER)
        {
            SetDecisionHead(MAX10STARGETHD,HEAD_FAIL);
            sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",MAX10STARGETHD,MAX10SBER,MAXREFBER);
            userfilelog(EXPLOG);
            return;
        }
        else
        {
            SetDecisionHead(MAXREFTARGETHD,HEAD_FAIL);
            sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXREFTARGETHD,MAX10SBER,MAXREFBER);
            userfilelog(EXPLOG);
            return;
        }
    }
//////////////////////////////////////////////
//		L2105 No Matrial Fail -> OOB        //
//////////////////////////////////////////////
    userfilelog("2105 No Matrial Fail -> D/S");
    CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
    str_exp_setdectype(1, 0);
    poststatus("OOB");
    return;
*/
}

void EXP_L28XX()
{
	int nHd,nZn,nCnt,nValue,nTargetHD,nRet,nfile;
	int nTPI747CntTTL, nTPIRefCntTTL;
	int ntemp,ntemp1;
	int nReftemp, n747temp, n10Stemp;
    int nTTLHD_MAX_Sgain, nTTLHD_MIN_Sgain, nTargetHD_MAX_Sgain, nTargetHD_MIN_Sgain;

	float ftemp0, ftemp1, ftemp2;	

    userfilelog("EXP_L28XX");
    poststatus(EXPLOG);

    if((CAPA == 32) && (MAX_HEAD == 2))
    {
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }

    str_exp_setdectype(1, 0);
    poststatus("GoWinstar");
    stoptest();

/*    
//IsSeekFail
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,8,1,1,1);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
		nRet = IsSeekFail(62,62,nHd,nHd,0,25,5);  //0 is OK, argument : int ntargetCyl,int ntargetHd,int nMode,int nLoop,float nthres
		if (nRet != 0)
		{
	    	SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
	    	sputd(EXPLOG,"IsSeekFail Hd:%d Fail",nHd);
            userfilelog(EXPLOG);
		}
	}
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}
	
//IsServoGainFail
    nTTLHD_MAX_Sgain = 0;
    nTTLHD_MIN_Sgain = 1000;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
        IsServoGainFail(62,nHd,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
        
        if(MAX_SGAIN[nHd] > nTTLHD_MAX_Sgain)
        {
            nTargetHD_MAX_Sgain = nHd;
            nTTLHD_MAX_Sgain = MAX_SGAIN[nHd];
        }
        if(MIN_SGAIN[nHd] < nTTLHD_MIN_Sgain)
        {
            nTargetHD_MIN_Sgain = nHd;
            nTTLHD_MIN_Sgain = MIN_SGAIN[nHd];
        }
    }
    if (nTTLHD_MAX_Sgain > 160)
    {
        SetDecisionHead(nTargetHD_MAX_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",nTargetHD_MAX_Sgain);
        userfilelog(EXPLOG);
    }
    if ((nTTLHD_MIN_Sgain != 0) && (nTTLHD_MIN_Sgain < 5))
    {
        SetDecisionHead(nTargetHD_MIN_Sgain,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
        sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",nTargetHD_MIN_Sgain);
        userfilelog(EXPLOG);
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionHead(nHd);
		if(nRet != 0)	return;
	}

//CHK_TrkSrvTargetHDandMaxRatio , CHK_DefTargetHDandMaxRatio
//SrvContinous & DefContinous
	if((BI_ERRORCODE != 0x2812) && (BI_ERRORCODE != 0x2811))
	{
        userfilelog("Not 0x2812, 0x2811 Drive");
        if(MASS == 0)
        {
            userfilelog("28XX MASS Enable");
            MakeMassData();     //PP CMD
            CHK_DList();		// int TBL_DLIST[MAX_HEAD][MAX_ZONE]
            CHK_ELogList();		// int TBL_ELOG[MAX_HEAD][MAX_ZONE]
        }
        else
        {
            userfilelog("28XX MASS Disable");
            CHK_HTDList(500);		// int TBL_DLIST[MAX_HEAD][MAX_ZONE]
            CHK_HTELogList();	// int TBL_ELOG[MAX_HEAD][MAX_ZONE]
        }
        CHK_DefTargetHDandMaxRatio();	    //Get Data : DEF_MAXHDRATE, DEF_MAXDEFTARGETHD
                                            //ContinousCheck(nHd,nCyl,nSkew,50,5,300)
	}
    //Get Data : DEF_MAXHDRATE, DEF_MAXDEFTARGETHD , ContinousCheck(nHd,nCyl,nSkew,50,5,300)
    CHK_TrkSrvTargetHDandMaxRatio();
    //Get Data : TRKSRV_MAXHDRATE, TRKSRV_MAXDEFTARGETHD , //ContinousCheck(nHd,nCyl,nSkew,50,5,50)
	sputd(EXPLOG,"DEF_TTLDEFCNT : %d", DEF_TTLDEFCNT);
    userfilelog(EXPLOG);
	sputd(EXPLOG,"TRKSRV_TTLDEFCNT : %d", TRKSRV_TTLDEFCNT);
    userfilelog(EXPLOG);
    if(DEF_TTLDEFCNT > 5000)
    {
        MAXHDRATE = DEF_MAXHDRATE;
        MAXDEFTARGETHD = DEF_MAXDEFTARGETHD;    
        sprintf(EXPLOG,"DEF -> MAXDEFTARGETHD[%d] MAXHDRATE:%d",MAXDEFTARGETHD,MAXHDRATE);
        userfilelog(EXPLOG);
    }
    else
    {
        if(TRKSRV_TTLDEFCNT > 500)
        {
            MAXHDRATE = TRKSRV_MAXHDRATE;
            MAXDEFTARGETHD = TRKSRV_MAXDEFTARGETHD;
            sprintf(EXPLOG,"TRKSRV -> MAXDEFTARGETHD[%d] MAXHDRATE:%d",MAXDEFTARGETHD,MAXHDRATE);
            userfilelog(EXPLOG);
        }
        else
        {
            MAXHDRATE = 0;
            MAXDEFTARGETHD = 0xff;
            sprintf(EXPLOG,"TRKSRV -> MAXDEFTARGETHD[%d] MAXHDRATE:%d",MAXDEFTARGETHD,MAXHDRATE);
            userfilelog(EXPLOG);
        }
    }
    CHK_DiskDetect(300);    // DEFCONTINOUS
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		ntemp=SRVCONTINOUS[nHd];
		if(ntemp != 0)
		{
			SetDecisionDisk(nHd);
	    	sprintf(EXPLOG,"SrvCont nDK:%d Fail [ContThres:%d >= 50]",nHd,ntemp);
            userfilelog(EXPLOG);
		}
		ntemp1=DEFCONTINOUS[nHd];
		if(ntemp1 != 0)
		{
			SetDecisionDisk(nHd);
	    	sprintf(EXPLOG,"DefCont nDK:%d Fail [ContThres:%d >= 300]",nHd,ntemp1);
            userfilelog(EXPLOG);
		}
	}
    nRet = 0; ntemp= 0;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		nRet = GetDecisionDisk(nHd);
		if(nRet != 0)
        {
            ntemp++;
        //CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MINFODTARGETHD
            CHK_FODMaxandMinValue(nHd);	//0xff -> All Head Fail
            if(MAXFODFH >= 64000)
            {
                SetDecisionHead(nHd,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                sprintf(EXPLOG,"FODFHMax_Hd:%d Fail [MAXFODFH:%d < 64000]",MAXDEFTARGETHD,MAXFODFH);
                userfilelog(EXPLOG);
            }
            if((MINFODFH != 0) && (MINFODFH <= 23000))
            {
                SetDecisionHead(nHd,HEAD_FAIL);
                sprintf(EXPLOG,"FODFHMin_Hd:%d Fail [MINFODFH:%d < 23000]",MAXDEFTARGETHD,MINFODFH);
                userfilelog(EXPLOG);
            }
        }
	}
    if(ntemp != 0) return;

    CHK_DiskDefect_L3B(70);
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		if(BAD_DISK_3B[nHd] == CHAR_X)
		{
            str_exp_setdectype(1, 0);
            poststatus("DSK_GoExp");
            stoptest();
		}
	}
//0x2801, 0x2809 Rule
    if(((CAPA == 75) || (CAPA == 64)) && (MAX_HEAD == 6) && (MES_EXPERT_INFO.MES_BI_COUNT <= 2) &&
       ((BI_ERRORCODE == 0x2801) || (BI_ERRORCODE == 0x2807) || (BI_ERRORCODE == 0x2809) ||
        (BI_ERRORCODE == 0x2816) || ((BI_ERRORCODE & 0xFF00) == 0x1F00) ) )
        //special screen 0x2801, 0x2807, 0x2809, 0x2816, 0x1FXX
    {
        userfilelog("0x2801, 0x2807, 0x2809, 0x2816, 0x1F09, 0x1F0A Drive");
        if(DEF_TTLDEFCNT > 5000)
        {
            MAXHDRATE = DEF_MAXHDRATE;
            MAXDEFTARGETHD = DEF_MAXDEFTARGETHD;    
            if((MAXHDRATE > 800) && (MAX_HEAD == 2))	//MAXHDRATE 45.0%
            {
                sprintf(EXPLOG,"TTLDef target Hd:%d -> Rate:%d>45.0]",MAXDEFTARGETHD,MAXHDRATE);
                userfilelog(EXPLOG);
                CHK_FODMaxandMinValue(MAXDEFTARGETHD);	//0xff -> All Head Fail
                if(MAXFODFH >= 64000)
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                    sprintf(EXPLOG,"FODFHMax_Hd:%d Fail [MAXFODFH:%d < 68000]",MAXDEFTARGETHD,MAXFODFH);
                    userfilelog(EXPLOG);
                    return;
                }
                CHK_FODMaxandMinValue(MAXDEFTARGETHD);
                if((MINFODFH != 0) && (MINFODFH <= 21000))
                {
                    SetDecisionHead(MINFODTARGETHD,HEAD_FAIL);
                    sprintf(EXPLOG,"FODFHMin_Hd:%d Fail [MINFODFH:%d < 21000]",MINFODTARGETHD,MINFODFH);
                    userfilelog(EXPLOG);
                    return;
                }
                CHK_TPI74710SData(MAXDEFTARGETHD);
                //RefBER
                if(MAXREFBER < 5.0)
                {
                    SetDecisionHead(MAXREFTARGETHD,HEAD_FAIL);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 800 HD:%d Fail MAXREFBER:%5.2f",MAXDEFTARGETHD,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
                //747BER
                if(MAX747BER < 4.0)
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 800 HD:%d Fail MAX747BER:%5.2f",MAXDEFTARGETHD,MAX747BER);
                    userfilelog(EXPLOG);
                    return;
                }
                else
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 800 HD:%d Fail MAX747BER:%5.2f",MAXDEFTARGETHD,MAX747BER);
                    userfilelog(EXPLOG);
                    return;
                }
            }
            if((MAXHDRATE > 450) && (MAX_HEAD == 2))	//MAXHDRATE 45.0%
            {
                sprintf(EXPLOG,"TTLDef target Hd:%d -> Rate:%d>45.0]",MAXDEFTARGETHD,MAXHDRATE);
                userfilelog(EXPLOG);
                CHK_FODMaxandMinValue(MAXDEFTARGETHD);	//0xff -> All Head Fail
                if(MAXFODFH >= 64000)
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                    sprintf(EXPLOG,"FODFHMax_Hd:%d Fail [MAXFODFH:%d < 68000]",MAXDEFTARGETHD,MAXFODFH);
                    userfilelog(EXPLOG);
                    return;
                }
                CHK_FODMaxandMinValue(MAXDEFTARGETHD);
                if((MINFODFH != 0) && (MINFODFH <= 21000))
                {
                    SetDecisionHead(MINFODTARGETHD,HEAD_FAIL);
                    sprintf(EXPLOG,"FODFHMin_Hd:%d Fail [MINFODFH:%d < 21000]",MINFODTARGETHD,MINFODFH);
                    userfilelog(EXPLOG);
                    return;
                }
                CHK_TPI74710SData(MAXDEFTARGETHD);
                //RefBER
                if(MAXREFBER < 5.0)
                {
                    SetDecisionHead(MAXREFTARGETHD,HEAD_FAIL);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 800 HD:%d Fail MAXREFBER:%5.2f",MAXDEFTARGETHD,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
                //747BER
                if(MAX747BER < 4.0)
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 800 HD:%d Fail MAX747BER:%5.2f",MAXDEFTARGETHD,MAX747BER);
                    userfilelog(EXPLOG);
                    return;
                }
                if((CAPA == 75) && (MAX_HEAD == 6) && (MES_EXPERT_INFO.MES_BI_COUNT <= 2))
                {
                    CHK_MESHistory(4,1);
                    userfilelog("EXP_L28XX MAXHDRATE>450");
                    return;
                }
                else
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 800 HD:%d Fail MAX747BER:%5.2f",MAXDEFTARGETHD,MAX747BER);
                    userfilelog(EXPLOG);
                    return;
                }
            }
        }
        CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        return;
    }

//MAXDEFTARGETHD
   	sprintf(EXPLOG,"TTLDef MaxHd:%d -> Rate:%d]",MAXDEFTARGETHD,MAXHDRATE);
    userfilelog(EXPLOG);
    if(MAXDEFTARGETHD != 0xff)
    {
        //Spec tighten DefContinous
        if((BI_ERRORCODE != 0x2812) && (BI_ERRORCODE != 0x2811))
        {
            ntemp=0;
            CHK_DList();
            ntemp1=DEFCONTINOUS[MAXDEFTARGETHD];
            if(ntemp1 != 0)
            {
                SetDecisionDisk(MAXDEFTARGETHD);
                sprintf(EXPLOG,"DefCont nDK:%d Fail [ContThres:%d >= 250]",MAXDEFTARGETHD,ntemp1);
                userfilelog(EXPLOG);
            }
            if(BAD_DISK_3B[nHd] == CHAR_X)
            {
                str_exp_setdectype(1, 0);
                poststatus("DSK_GoExp");
                stoptest();
            }
        }
        if((MAXHDRATE > 450) && (MAX_HEAD != 2))	//MAXHDRATE 45.0%
        {
        //CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MINFODTARGETHD
            CHK_FODMaxandMinValue(MAXDEFTARGETHD);	//0xff -> All Head Fail
            if(MAXFODFH >= 64000)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 450 HD:%d Fail [MAXFODFH:%d < 68000]",MAXDEFTARGETHD,MAXFODFH);
                userfilelog(EXPLOG);
                return;
            }
            if((MINFODFH != 0) && (MINFODFH <= 21000))
            {
                sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 450 HD:%d Fail [MINFODFH:%d < 21000]",MINFODTARGETHD,MINFODFH);
                userfilelog(EXPLOG);
                SetDecisionHead(MINFODTARGETHD,HEAD_FAIL);
                return;
            }
            
        //CHK_GetTPIand747 : Get Data //MAXREFBER,MAXREFTARGETHD, MAX747BER,MAX747TARGETHD, MAX10SBER,MAX10STARGETHD
            CHK_TPI74710SData(MAXDEFTARGETHD);
            //RefBER
            if(MAXREFBER < 6.2)	//RefCnt127897 = RefBER : 6.2
            {
                SetDecisionHead(MAXREFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 450 HD:%d Fail MAXREFBER:%5.2f",MAXDEFTARGETHD,MAXREFBER);
                userfilelog(EXPLOG);
                return;
            }
            //747BER
            if(MAX747BER < 5.1)	//747Cnt128460 = 747BER : 5.1
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 450 HD:%d Fail MAX747BER:%5.2f",MAXDEFTARGETHD,MAX747BER);
                userfilelog(EXPLOG);
                return;
            }
            //10SBER
            if((MAX_HEAD <= 1) && (MAX10SBER < 5.2) && (MAXREFBER < 5.9)) 	//MAX10SBER15154 = n10Stemp : 5.20 (read Cnt 600000)
                                                                            //MAXREFBER127744 = 5.9
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 450 HD:%d Fail MAX10SBER:%5.2f, MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                userfilelog(EXPLOG);
                return;
            }
            if((MAX_HEAD >= 2) && (MAX10SBER < 5.2)) 	//ErrCnt15154 = n10Stemp : 5.20 (read Cnt 600000)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 450 HD:%d Fail MAX10SBER:%5.2f",MAXDEFTARGETHD,MAX10SBER);
                userfilelog(EXPLOG);
                return;
            }
            //Marginal Head Fail : Check 10SBER, RefBER, 747BER
            if((MAX10SBER < 5.5) && (MAXREFBER < 5.9))	//ErrCnt7770 = 10sBER : 5.5 (read Cnt 600000)
                                                        //RefCnt134092 = RefBER : 5.9
            {
                if(MAX10STARGETHD == MAXREFTARGETHD)
                {
                    SetDecisionHead(MAX10STARGETHD,HEAD_FAIL);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 450 HD:%d Fail MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
                else
                {
                    SetDecisionHead(MAXREFTARGETHD,HEAD_SWAP);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 450 HD:%d Swap MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
            }
            if((MAX10SBER < 5.7) || (MAXREFBER < 6.0))  //RefCnt134092 = RefBER : 6.0
            {
                sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 450 nHd:%d Swap MAX10SBER:%5.2f, MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                //printf("NEXT -> return");
                return;
            }
        //IsServoGainFail
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            IsServoGainFail(62,MAXDEFTARGETHD,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
            if (MAX_SGAIN[MAXDEFTARGETHD] > 160)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
            }
            if ((MIN_SGAIN[MAXDEFTARGETHD] != 0) && (MIN_SGAIN[MAXDEFTARGETHD] < 5))
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
            }
            nRet = GetDecisionHead(nHd);
            if(nRet != 0)	return;
        //IsBERFail
            Set_TestCondition(0,8,3,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sputd(EXPLOG,"EXP_L28XX MAXHDRATE 450 HD:%d IsBERFail1",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
            Set_TestCondition(0,2,1,1,1);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sputd(EXPLOG,"EXP_L28XX MAXHDRATE 550 HD:%d IsBERFail2",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //		MAXHDRATE > 450 But, No Head Fail -> Head Swap
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            else
            {
                nRet = 0;
				for(nHd=0; nHd<MAX_HEAD; nHd++)
				{
					nRet = GetDecisionHead(nHd);
					if(nRet != 0)	return;
				}
                
                CHK_TrkSrvTargetHDandMaxRatio();    //Servo Defect
                if(TRKSRV_TTLDEFCNT > 5000)
                {
                    MAXHDRATE = TRKSRV_MAXHDRATE;
                    MAXDEFTARGETHD = TRKSRV_MAXDEFTARGETHD;
                }                
                if(MAXHDRATE > 950)
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                    sputd(EXPLOG,"IMAXHDRATE > 950 Hd:%d -> Head_Fail",MAXDEFTARGETHD);
                    userfilelog(EXPLOG);
                    return;
                }
                
                if(BI_ERRORCODE != 0x2813 && BI_STATUS != 3)
                {
                    if(MAX_HEAD == 6)    //F1-3D
                    {
                        SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                        sputd(EXPLOG,"F1-3D IMAXHDRATE > 450 Hd:%d -> Head_Swap",MAXDEFTARGETHD);
                        userfilelog(EXPLOG);
                        return;
                    }
                    if(MAX_HEAD == 4)    //F1-2D
                    {
                        sputd(EXPLOG,"F1-1D IMAXHDRATE > 450 Hd:%d -> STW",MAXDEFTARGETHD);
                        userfilelog(EXPLOG);
                        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                        return;
                    }
                    else                //F1-1D
                    {
                        sputd(EXPLOG,"F1-1D IMAXHDRATE > 450 Hd:%d -> STW",MAXDEFTARGETHD);
                        userfilelog(EXPLOG);
                        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                        return;
                    }
                }
                if(BI_STATUS == 3)
                {
                    userfilelog("CONT Fail Re-Test");
                    CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
                else
                {
                    sputd(EXPLOG,"0x2813 IMAXHDRATE > 450 Hd:%d -> STW",MAXDEFTARGETHD);
                    userfilelog(EXPLOG);
                    CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
            }
        }
        if((MAXHDRATE > 800) && (MAX_HEAD == 2))	//MAXHDRATE 45.0%
        {
            sprintf(EXPLOG,"F1-1D TTLDef target Hd:%d -> Rate:%d>80.0]",MAXDEFTARGETHD,MAXHDRATE);
            userfilelog(EXPLOG);
        //CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MINFODTARGETHD
            CHK_FODMaxandMinValue(MAXDEFTARGETHD);	//0xff -> All Head Fail
            if(MAXFODFH >= 64000)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);		//type 0:good, 1:HEAD_DMR, 2:HEAD_FAIL, 3:HEAD_SWAP
                sprintf(EXPLOG,"FODFHMax_Hd:%d Fail [MAXFODFH:%d < 68000]",MAXDEFTARGETHD,MAXFODFH);
                userfilelog(EXPLOG);
                return;
            }
            if((MINFODFH != 0) && (MINFODFH <= 23000))
            {
                SetDecisionHead(MINFODTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"FODFHMin_Hd:%d Fail [MINFODFH:%d < 23000]",MINFODTARGETHD,MINFODFH);
                userfilelog(EXPLOG);
                return;
            }
            
        //CHK_GetTPIand747 : Get Data //MAXREFBER,MAXREFTARGETHD, MAX747BER,MAX747TARGETHD, MAX10SBER,MAX10STARGETHD
            CHK_TPI74710SData(MAXDEFTARGETHD);
            //RefBER
            //printf("EXP_L28XX MAXHDRATE 450 HD:%d MAXREFBER:%d",MAXREFTARGETHD,MAXREFBER);
            if(MAXREFBER < 6.2)	//RefCnt127897 = RefBER : 6.2
            {
                SetDecisionHead(MAXREFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"Ref Target nHd:%d Fail RefBER:%5.2f",MAXDEFTARGETHD,MAXREFBER);
                userfilelog(EXPLOG);
                return;
            }
            //747BER
            //printf("EXP_L28XX MAXHDRATE 450 HD:%d MAX747BER:%d",MAXDEFTARGETHD,MAX747BER);
            if(MAX747BER < 5.1)	//747Cnt128460 = 747BER : 5.1
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"747 Target nHd:%d Fail 747BER:%5.2f",MAXDEFTARGETHD,MAX747BER);
                userfilelog(EXPLOG);
                return;
            }
            //10SBER
            if((MAX_HEAD <= 1) && (MAX10SBER < 5.2) && (MAXREFBER < 5.9)) 	//MAX10SBER15154 = n10Stemp : 5.20 (read Cnt 600000)
                                                                            //MAXREFBER127744 = 5.9
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"10S Target nHd:%d Fail 10SBER:%5.2f",MAXDEFTARGETHD,MAX10SBER);
                userfilelog(EXPLOG);
                return;
            }
            if((MAX_HEAD >= 2) && (MAX10SBER < 5.2)) 	//ErrCnt15154 = n10Stemp : 5.20 (read Cnt 600000)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"10S Target nHd:%d Fail 10SBER:%5.2f",MAXDEFTARGETHD,MAX10SBER);
                userfilelog(EXPLOG);
                return;
            }
            //Marginal Head Fail : Check 10SBER, RefBER, 747BER
            if((MAX10SBER < 5.5) && (MAXREFBER < 5.9))	//ErrCnt7770 = 10sBER : 5.5 (read Cnt 600000)
                                                            //RefCnt134092 = RefBER : 5.9
            {
                if(MAX10STARGETHD == MAXREFTARGETHD)
                {
                    SetDecisionHead(MAX10STARGETHD,HEAD_FAIL);
                    sprintf(EXPLOG,"EXP_L28XX Head Fail MAXHDRATE 450 HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
                else
                {
                    SetDecisionHead(MAXREFTARGETHD,HEAD_SWAP);
                    sprintf(EXPLOG,"EXP_L28XX Head Swap MAXHDRATE 450 HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
            }
            if((MAX10SBER > 5.7) || (MAXREFBER > 6.0))
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                sprintf(EXPLOG,"10S Target nHd:%d Fail 10SBER:%5.2f",MAXDEFTARGETHD,MAX10SBER);
                userfilelog(EXPLOG);
                return;
            }
        //IsServoGainFail
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            IsServoGainFail(62,MAXDEFTARGETHD,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
            if (MAX_SGAIN[MAXDEFTARGETHD] > 160)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
            }
            if ((MIN_SGAIN[MAXDEFTARGETHD] != 0) && (MIN_SGAIN[MAXDEFTARGETHD] < 5))
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
            }
            nRet = GetDecisionHead(nHd);
            if(nRet != 0)	return;
        //IsBERFail
            Set_TestCondition(0,8,3,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sputd(EXPLOG,"EXP_L28XX MAXHDRATE 450 HD:%d IsBERFail1",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
            Set_TestCondition(0,2,1,1,1);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sputd(EXPLOG,"EXP_L28XX MAXHDRATE 550 HD:%d IsBERFail2",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //		MAXHDRATE > 450 But, No Head Fail -> Head Swap
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            else
            {
                nRet = 0;
				for(nHd=0; nHd<MAX_HEAD; nHd++)
				{
					nRet = GetDecisionHead(nHd);
					if(nRet != 0)	return;
				}
                
                CHK_TrkSrvTargetHDandMaxRatio();    //Servo Defect
                if(TRKSRV_TTLDEFCNT > 5000)
                {
                    MAXHDRATE = TRKSRV_MAXHDRATE;
                    MAXDEFTARGETHD = TRKSRV_MAXDEFTARGETHD;
                }                
                if(MAXHDRATE > 950)
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                    sputd(EXPLOG,"IMAXHDRATE > 950 Hd:%d -> Head_Fail",MAXDEFTARGETHD);
                    userfilelog(EXPLOG);
                    return;
                }
                
                if(BI_ERRORCODE != 0x2813 && BI_STATUS != 3)
                {
                    sputd(EXPLOG,"F1-1D IMAXHDRATE > 450 Hd:%d -> STW",MAXDEFTARGETHD);
                    userfilelog(EXPLOG);
                    CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
                if(BI_STATUS == 3)
                {
                    userfilelog("CONT Fail Re-Test");
                    CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
                else
                {
                    sputd(EXPLOG,"0x2813 IMAXHDRATE > 450 Hd:%d -> STW",MAXDEFTARGETHD);
                    userfilelog(EXPLOG);
                    CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
            }
        }
        if(MAXHDRATE > 300)	//MAXHDRATE 30.0%
        {
            //printf("TTLDef Hd:%d Fail -> Rate:%d>30.0",MAXDEFTARGETHD,MAXHDRATE);
        //CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MAXFODTARGETHD
            CHK_FODMaxandMinValue(MAXDEFTARGETHD);	//0xff -> All Head Fail	
            if(MAXFODFH >= 68000)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"FODFHMax_Hd:%d Fail [MAXFODFH:%d < 68000]",MAXFODTARGETHD,MAXFODFH);
                userfilelog(EXPLOG);
                return;
            }
            if((MINFODFH != 0) && (MINFODFH <= 20000))
            {
                SetDecisionHead(MINFODTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"FODFHMin_Hd:%d Fail [MINFODFH:%d < 20000]",MINFODTARGETHD,MINFODFH);
                userfilelog(EXPLOG);
                return;
            }
        //CHK_GetTPIand747 : Get Data //MAXREFBER,MAXREFTARGETHD, MAX747BER,MAX747TARGETHD, MAX10SBER,MAX10STARGETHD
            CHK_TPI74710SData(MAXDEFTARGETHD);
        //RefBER
            if(MAXREFBER < 5.5)	//RefCnt128256 = RefBER : 5.5
            {
                sprintf(EXPLOG,"Ref Target nHd:%d Fail RefBER:%5.2f",MAXDEFTARGETHD,MAXREFBER);
                userfilelog(EXPLOG);
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                return;
            }
        //747BER
            if(MAX747BER < 4.5)	//747Cnt128768) = 747BER : 4.50
            {
                sprintf(EXPLOG,"747 Target nHd:%d Fail 747BER:%5.2f",MAXDEFTARGETHD,MAXREFBER);
                userfilelog(EXPLOG);
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                return;
            }
        //10SBER
            if((MAX_HEAD <= 1) && (MAX10SBER < 5.15) && (MAXREFBER > 5.9)) 	//ErrCnt17395 = n10Stemp : 5.15 (read Cnt 600000)
                                                                            //MAXREFBER127744 = 5.9
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"10S Target nHd:%d Fail 10SBER:%5.2f",MAXDEFTARGETHD,MAX10SBER);
                userfilelog(EXPLOG);
                return;
            }
            if((MAX_HEAD >= 2) && (MAX10SBER < 5.2)) 	//ErrCnt15154 = n10Stemp : 5.20 (read Cnt 600000)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"10S Target nHd:%d Fail 10SBER:%5.2f",MAXDEFTARGETHD,MAX10SBER);
                userfilelog(EXPLOG);
                return;
            }
        //Marginal Head Fail : Check 10SBER, RefBER, 747BER
            if((MAX10SBER < 5.1) && (MAXREFBER < 5.9))	//ErrCnt19521 = 10sBER : 5.1 (read Cnt 600000)
                                                        //RefCnt128051 = RefBER : 5.9
            {
                if(MAX10STARGETHD == MAXREFTARGETHD)
                {
                    SetDecisionHead(MAX10STARGETHD,HEAD_FAIL);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 300 HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
                else
                {
                    SetDecisionHead(MAXREFTARGETHD,HEAD_SWAP);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 300 HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
            }
        //IsServoGainFail
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            IsServoGainFail(62,MAXDEFTARGETHD,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
            if (MAX_SGAIN[MAXDEFTARGETHD] > 160)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
            }
            if ((MIN_SGAIN[MAXDEFTARGETHD] != 0) && (MIN_SGAIN[MAXDEFTARGETHD] < 5))
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
            }
            nRet = GetDecisionHead(nHd);
            if(nRet != 0)	return;
        //IsBERFail
            Set_TestCondition(0,8,3,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sputd(EXPLOG,"EXP_L28XX MAXHDRATE 300 HD:%d IsBERFail1",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
            Set_TestCondition(0,4,2,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                sputd(EXPLOG,"EXP_L28XX MAXHDRATE 300 HD:%d IsBERFail2",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //		MAXHDRATE > 300 But, No Head Fail -> Head Swap
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            else
            {
                nRet = 0;
				for(nHd=0; nHd<MAX_HEAD; nHd++)
				{
					nRet = GetDecisionHead(nHd);
					if(nRet != 0)	return;
				}
                if(BI_ERRORCODE != 0x2813 && BI_STATUS != 3)
                {
                    if(MAX_HEAD == 6)    //F1-3D
                    {
                        SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                        sputd(EXPLOG,"F1-3D IMAXHDRATE > 450 Hd:%d -> Head_Swap",MAXDEFTARGETHD);
                        userfilelog(EXPLOG);
                        return;
                    }
                    if(MAX_HEAD == 4)    //F1-2D
                    {
                        sputd(EXPLOG,"F1-1D IMAXHDRATE > 450 Hd:%d -> STW",MAXDEFTARGETHD);
                        userfilelog(EXPLOG);
                        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                        return;
                    }
                    else                //F1-1D
                    {
                        sputd(EXPLOG,"F1-1D IMAXHDRATE > 450 Hd:%d -> STW",MAXDEFTARGETHD);
                        userfilelog(EXPLOG);
                        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                        return;
                    }
                }
                if(BI_STATUS == 3)
                {
                    userfilelog("CONT Fail Re-Test");
                    CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
                else
                {
                    sputd(EXPLOG,"0x2813 IMAXHDRATE > 450 Hd:%d -> STW",MAXDEFTARGETHD);
                    userfilelog(EXPLOG);
                    CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
            }
        }
        
        if(MAXHDRATE > 270)	//MAXHDRATE 27.0%
        {
            //printf("TTLDef Hd:%d Fail -> Rate:%d>27.0%] ",MAXDEFTARGETHD,MAXHDRATE);
        //CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MAXFODTARGETHD
            CHK_FODMaxandMinValue(MAXDEFTARGETHD);	//0xff -> All Head Fail
            if(MAXFODFH >= 70000)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"FODFHMax_Hd:%d Fail [MAXFODFH:%d >= 70000]",MAXDEFTARGETHD,MAXFODFH);
                userfilelog(EXPLOG);
                return;
            }
            if((MINFODFH != 0) && (MINFODFH <= 19900))
            {
                SetDecisionHead(MINFODTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"FODFHMin_Hd:%d Fail [MINFODFH:%d < 19900]",MINFODTARGETHD,MINFODFH);
                userfilelog(EXPLOG);
                return;
            }
            
        //CHK_GetTPIand747 : Get Data //MAXREFBER,MAXREFTARGETHD, MAX747BER,MAX747TARGETHD, MAX10SBER,MAX10STARGETHD
            CHK_TPI74710SData(MAXDEFTARGETHD);
        //RefBER
            if(MAXREFBER < 5.0)	//RefCnt128512 = RefBER : 5.0
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                sprintf(EXPLOG,"Ref Target nHd:%d Fail RefBER:%5.2f",MAXDEFTARGETHD,MAXREFBER);
                userfilelog(EXPLOG);
                return;
            }
        //747BER
            if(MAX747BER < 3.9)	//747Cnt129071 = 747BER : 3.90
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                sprintf(EXPLOG,"747 Target nHd:%d Swap 747BER:%5.2f",MAXDEFTARGETHD,MAX747BER);
                userfilelog(EXPLOG);
                return;
            }
        //10SBER
            if(MAX10SBER < 5.2)	//ErrCnt128399 = n10Stemp : 5.20 (read Cnt 600000)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                sprintf(EXPLOG,"10S Target nHd:%d Fail 10SBER:%5.2f",MAXDEFTARGETHD,MAX10SBER);
                userfilelog(EXPLOG);
                return;
            }
        //Marginal Head Fail : Check 10SBER, RefBER, 747BER
            if((MAX10SBER < 5.1) && (MAXREFBER < 5.9))	//ErrCnt19521 = 10sBER : 5.1 (read Cnt 600000)
                                                        //RefCnt128051 = RefBER : 5.9
            {
                if(MAX10STARGETHD == MAXREFTARGETHD)
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 270 HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
                else
                {
                    SetDecisionHead(MAXDEFTARGETHD,HEAD_SWAP);
                    sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 270 HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",MAXDEFTARGETHD,MAX10SBER,MAXREFBER);
                    userfilelog(EXPLOG);
                    return;
                }
            }
        //IsServoGainFail
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            IsServoGainFail(62,MAXDEFTARGETHD,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
            if (MAX_SGAIN[nHd] > 160)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
            }
            if ((MIN_SGAIN[MAXDEFTARGETHD] != 0) && (MIN_SGAIN[MAXDEFTARGETHD] < 5))
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
            }
            nRet = GetDecisionHead(nHd);
            if(nRet != 0)	return;
        //IsBERFail
            Set_TestCondition(0,8,3,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            nRet = IsBERFail(62,MAXDEFTARGETHD,64,5.5);    //argument : int ntargetCyl,int ntargetHd,int nLoop,float nthres
            if (nRet != 0)
            {
                SetDecisionHead(MAXDEFTARGETHD,HEAD_FAIL);
                sprintf(EXPLOG,"EXP_L28XX MAXHDRATE 270 HD:%d IsBERFail",MAXDEFTARGETHD);
                userfilelog(EXPLOG);
                return;
            }
	        else
	        {
				for(nHd=0; nHd<MAX_HEAD; nHd++)
				{
					nRet = GetDecisionHead(nHd);
					if(nRet != 0)	return;
				}
                if(BI_STATUS == 3)
                {
                    userfilelog("CONT Fail Re-Test");
                    CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
                else
                {
                    userfilelog("MAXHDRATE > 270 : STW");
                    CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }
	        }
        }
        else
        {
            nRet = 0;
			for(nHd=0; nHd<MAX_HEAD; nHd++)
			{
				nRet = GetDecisionHead(nHd);
				if(nRet != 0)	return;
			}
            if(BI_STATUS == 3)
            {
                userfilelog("CONT Fail Re-Test");
                CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                return;
            }
            else
            {
                userfilelog("MAXHDRATE < 270 : STW");
                CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                return;
            }
        }
    }
	else
	{
        nRet = 0;
		for(nHd=0; nHd<MAX_HEAD; nHd++)
		{
			nRet = GetDecisionHead(nHd);
			if(nRet != 0)	return;
		}
    //CHK_ParameterMonitoring	
        //CHK_ParameterMonitoring();	//Paramtermonitoring : //AGC/ASC/MRR/CSM/ALARM/FODR/FODW/BER/SGAIN/QM/FODRES/TMPR
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            //ntemp = ParaMonLast[nHd*12+3];
            if((ntemp != 0) && (ntemp < 0x100))
            {
                SetDecisionHead(nHd,HEAD_FAIL);
            }
        }
    //CHK_FODMaxandMinValue(nHd)	//nHd :0xff -> All HD Check, get Data : MAXFODFH, MAXFODTARGETHD, MINFODFH, MAXFODTARGETHD
        CHK_FODMaxandMinValue(0xff);	//0xff -> All Head Fail
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            CHK_FODMaxandMinValue(nHd);	//0xff -> All Head Fail
            if(MAXFODFH >= 70500)
            {
                SetDecisionHead(nHd,HEAD_FAIL);
                sprintf(EXPLOG,"EXP_L28XX HD:%d MAXFODFH:%d",nHd,MAXFODFH);
                userfilelog(EXPLOG);
            }
            if((MINFODFH != 0) && (MINFODFH <= 19990))
            {
                SetDecisionHead(nHd,HEAD_FAIL);
                sprintf(EXPLOG,"EXP_L28XX HD:%d MINFODFH:%d",nHd,MINFODFH);
                userfilelog(EXPLOG);
            }
        }
    //CHK_GetTPIand747 : Get Data //MAXREFBER,MAXREFTARGETHD, MAX747BER,MAX747TARGETHD, MAX10SBER,MAX10STARGETHD
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            CHK_TPI74710SData(nHd);
        //RefBER
            if((MAXREFBER != 0) && (MAXREFBER < 5.0))	//RefCnt133632 = RefBER : 5.00
            {
                SetDecisionHead(nHd,HEAD_FAIL);
                sprintf(EXPLOG,"EXP_L28XX HD:%d MAXREFBER:%5.2f",nHd,MAXREFBER);
                userfilelog(EXPLOG);
                return;
            }
        //747BER
            if(MAX747BER < 4.0)	//747Cnt129024 = 747BER : 4.00
            {
                SetDecisionHead(nHd,HEAD_FAIL);
                sprintf(EXPLOG,"EXP_L28XX HD:%d MAX747BER:%5.2f",nHd,MAX747BER);
                userfilelog(EXPLOG);
                return;
            }
        //10SBER
            if((MAX_HEAD <= 1) && (MAX10SBER < 5.15) && (MAXREFBER < 5.9)) 	//ErrCnt17395 = n10Stemp : 5.15 (read Cnt 600000)
                                                                            //MAXREFBER127744 = 5.9
            {
                SetDecisionHead(nHd,HEAD_FAIL);
                sprintf(EXPLOG,"10S Target nHd:%d Fail 10SBER:%5.2f",nHd,n10Stemp);
                userfilelog(EXPLOG);
                return;
            }
            if((MAX_HEAD >= 2) && (MAX10SBER < 5.2)) 	//ErrCnt15154 = n10Stemp : 5.20 (read Cnt 600000)
            {
                SetDecisionHead(nHd,HEAD_FAIL);
                sprintf(EXPLOG,"10S Target nHd:%d Fail 10SBER:%5.2f",nHd,n10Stemp);
                userfilelog(EXPLOG);
                return;
            }
        //Marginal Head Fail : Check 10SBER, RefBER, 747BER
            if((MAX10SBER < 3.95) && (MAXREFBER < 5.5))	//ErrCnt2457 = 10SBER : 3.95 (read Cnt 600000)
                                                        //RefCnt2816 = RefBER : 5.5
            {
                SetDecisionHead(nHd,HEAD_FAIL);
                sprintf(EXPLOG,"EXP_L28XX HD:%d MAX10SBER:%5.2f MAXREFBER:%5.2f",nHd,MAX10SBER,MAXREFBER);
                userfilelog(EXPLOG);
                return;
            }
        //IsServoGainFail
            Set_TestCondition(1,20,5,2,2);    //int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry
            IsServoGainFail(62,nHd,0,64,3,0); // ntargetCyl,ntargetHd,nMode,nLoop,nMinthres,nMaxthres,nAvgthres
            if (MAX_SGAIN[nHd] > 160)
            {
                SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsServoGain MAX Fail Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
            }
            if ((MIN_SGAIN[nHd] != 0) && (MIN_SGAIN[nHd] < 5))
            {
                SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sputd(EXPLOG,"IsServoGain MIN Fail Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
            }
            nRet = GetDecisionHead(nHd);
            if(nRet != 0)	return;
        }

        if(BI_STATUS == 3)
        {
            userfilelog("CONT Fail Re-Test");
            CHK_MESHistory(4,1);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            return;
        }
        else
        {
            userfilelog("CHK_MESHistory");
            CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
            return;
        }        //L28XX No Matrial Fail -> STW
    }
*/
}

void CHK_BISTATUS(int type)
{
    char bufdisp[32];
    char temp[32];
    char cmd[32];
    char rdbuf[768];
    int offset,cmdlen,ret,nUsedZn,nOffs,nHd;   

    userfilelog("CHK_BISTATUS");
    if(type == 0)
    {
        sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
        strlen(cmd,cmdlen);
        str_sendserialcmd(cmd,cmdlen);
        delays(1);
    }
    else
    {
        ONERROR(dummyerror);
        reset();
        ONERRORCLEAR();
    }
    
    ONERROR(dummyerror);
    str_usercmd("LB",2,10000,768,0,rdbuf,768);
    ONERRORCLEAR();
    
    memset(BI_FWREV,0,0,16);
    strfind(rdbuf,"FWREV",offset);
    if(offset != 0) memnmov(BI_FWREV, rdbuf,0,offset+10,12,0);
    sputs(EXPLOG,"BI_REV : %s",BI_FWREV);
    userfilelog(EXPLOG);

    strfind(rdbuf,"BIStat",offset);
    if(offset != 0) atoi(rdbuf,offset+12,16,BI_STATUS);
    sputd(EXPLOG,"BI_STATUS : %x",BI_STATUS);
    userfilelog(EXPLOG);
    
    strfind(rdbuf,"StopCond",offset);
    if(offset != 0) atoi(rdbuf,offset+12,16,BI_ERRORCODE);
    sputd(EXPLOG,"BI_ERRORCODE : %x",BI_ERRORCODE);
    userfilelog(EXPLOG);
    
    strfind(rdbuf,"LastCmd",offset);
    if(offset != 0) atoi(rdbuf,offset+12,16,BI_LASTCMD);
    sputd(EXPLOG,"BI_LASTCMD : %x",BI_LASTCMD);
    userfilelog(EXPLOG);
    
    strfind(rdbuf,"LastStp",offset);
    if(offset != 0) atoi(rdbuf,offset+12,16,BI_LASTSTEP);
    sputd(EXPLOG,"BI_LASTSTEP : %x",BI_LASTSTEP);
    userfilelog(EXPLOG);

    strfind(rdbuf,"AvailZone0",offset);
    if(offset != 0) atoi(rdbuf,offset+13,16,BI_AVILZONE0);
    ZONESKIP[0] = BI_AVILZONE0;
    sputd(EXPLOG,"BI_AVILZONE0 : %x",BI_AVILZONE0);
    userfilelog(EXPLOG);
    
    strfind(rdbuf,"AvailZone1",offset);
    if(offset != 0) atoi(rdbuf,offset+13,16,BI_AVILZONE1);
    ZONESKIP[1] = BI_AVILZONE1;
    sputd(EXPLOG,"BI_AVILZONE1 : %x",BI_AVILZONE1);
    userfilelog(EXPLOG);

    strfind(rdbuf,"AvailZone2",offset);
    if(offset != 0) atoi(rdbuf,offset+13,16,BI_AVILZONE2);
    ZONESKIP[2] = BI_AVILZONE2;
    sputd(EXPLOG,"BI_AVILZONE2 : %x",BI_AVILZONE2);
    userfilelog(EXPLOG);
    
    strfind(rdbuf,"AvailZone3",offset);
    if(offset != 0) atoi(rdbuf,offset+13,16,BI_AVILZONE3);    
    ZONESKIP[3] = BI_AVILZONE3;
    sputd(EXPLOG,"BI_AVILZONE3 : %x",BI_AVILZONE3);
    userfilelog(EXPLOG);
    
    strfind(rdbuf,"AvailZone4",offset);
    if(offset != 0) atoi(rdbuf,offset+13,16,BI_AVILZONE4);
    ZONESKIP[4] = BI_AVILZONE4;
    sputd(EXPLOG,"BI_AVILZONE4 : %x",BI_AVILZONE4);
    userfilelog(EXPLOG);
    
    strfind(rdbuf,"AvailZone5",offset);
    if(offset != 0) atoi(rdbuf,offset+13,16,BI_AVILZONE5);
    ZONESKIP[5] = BI_AVILZONE5;
    sputd(EXPLOG,"BI_AVILZONE5 : %x",BI_AVILZONE5);
    userfilelog(EXPLOG);

    if(BI_FWREV[6] == CHAR_U || BI_FWREV[6] == CHAR_u || BI_FWREV[5] == CHAR_Q ||
       BI_FWREV[5] == CHAR_q || BI_FWREV[5] == CHAR_N || BI_FWREV[5] == CHAR_n      )
    {
        MASS = 1;
        sputd(EXPLOG,"MASS : %d -> Disable MASS",MASS);
        userfilelog(EXPLOG);
    }
    else
    {
        MASS = 0;
        sputd(EXPLOG,"MASS : %d -> Enable MASS",MASS);
        userfilelog(EXPLOG);        
    }

    if(BI_ERRORCODE==0xFFFF)
    {
        strcmp(MES_EXPERT_INFO.MES_PREV_ERROR,"NONE",7,ret);
        sputd(EXPLOG,"nRet:%d",ret);
        userfilelog(EXPLOG);
        if(ret != 0)
        {
            atoi(MES_EXPERT_INFO.MES_PREV_ERROR,2,10,BI_ERRORCODE);
            sputd(EXPLOG,"Change BI_ERRORCODE:%d",BI_ERRORCODE);
            userfilelog(EXPLOG);
        }
    }
    
    memset(temp,0,0,32);
    temp[0]=BI_FWREV[8];
    temp[1]=BI_FWREV[9];
    temp[2]=BI_FWREV[10];    
    strcmp(temp,"a11",2,ret);
    if(ret == 0)    {RPM = 72; CAPA = 32;}
    strcmp(temp,"a12",2,ret);
    if(ret == 0)    {RPM = 72; CAPA = 25;}
    strcmp(temp,"d11",2,ret);
    if(ret == 0)    {RPM = 72; CAPA = 32;}
    strcmp(temp,"d12",2,ret);
    if(ret == 0)    {RPM = 72; CAPA = 25;}
    sprintf(EXPLOG,"BI_STATUS -> CAPA : %d , RPM : %d",CAPA,RPM);
    userfilelog(EXPLOG);
    
    if(type == 0)
    {
        sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
        strlen(cmd,cmdlen);
        str_sendserialcmd(cmd,cmdlen);
        delays(1);
        reset();
        
        // Used Zone Check
        if( MGET(9,0) !=0 )
        {
            userfilelog("BI_RESULTS");
            delays(10);
            return;
            //stoptest();
        }
        nOffs = 0xCC;
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            nUsedZn = (RDBUF[nOffs] & 0xff)<<0;
            nUsedZn = nUsedZn | ((RDBUF[nOffs+1] & 0xff) << 8);
            nUsedZn = nUsedZn | ((RDBUF[nOffs+2] & 0xff) <<16);
            nUsedZn = nUsedZn | ((RDBUF[nOffs+3] & 0xff) <<24);
            
            ZONEUSED[nHd] = nUsedZn;
            sprintf(EXPLOG,"ZONEUSED[%d] : %x",nHd,nUsedZn);
            userfilelog(EXPLOG);
            nOffs=nOffs+4;
        }
    }
    if(MAX_HEAD != 2)
    {
        userfilelog("CHK_BISTATUS MissSET");
        str_exp_setdectype(1, 0);
        poststatus("OOB");
        stoptest();
    }    
    return;
}

void Check_Kill_HeadNum()
{
    char cmd[32];
    int  cmdlen,nValue;

    str_setbyteswap(0);
    strcpy(cmd,"DS FFF3F020 100");
    strlen(cmd,cmdlen);
    str_usercmd(cmd,cmdlen,2000,1716,1,RDBUF,512);
    str_setbyteswap(1);    
    
    nValue = RDBUF[2] & 0xff;

    sprintf(EXPLOG,"KilledHd:%x",nValue);
    userfilelog(EXPLOG);
    poststatus(EXPLOG);

    if((nValue != 0x00) && (nValue != 0xc0))
    {
        if((nValue == 0x01) || (nValue == 0xc1))
        {
            userfilelog("OOB-HD0 Killed");
            poststatus("OOB-HD0 Killed");
            str_exp_setdectype(1, 0);
            stoptest();
        }
        if((nValue == 0x02) || (nValue == 0xc2))
        {
            userfilelog("OOB-HD1 Killed");
            poststatus("OOB-HD1 Killed");
            str_exp_setdectype(1, 0);
            stoptest();
        }
        if((nValue == 0x04) || (nValue == 0xc4))
        {
            userfilelog("OOB-HD2 Killed");
            poststatus("OOB-HD2 Killed");
            str_exp_setdectype(1, 0);
            stoptest();
        }
        if((nValue == 0x08) || (nValue == 0xc8))
        {
            userfilelog("OOB-HD3 Killed");
            poststatus("OOB-HD3 Killed");
            str_exp_setdectype(1, 0);
            stoptest();
        }
        if(nValue == 0x10)
        {
            userfilelog("OOB-HD4 Killed");
            poststatus("OOB-HD4 Killed");
            str_exp_setdectype(1, 0);
            stoptest();
        }
        if(nValue == 0x20)
        {
            userfilelog("OOB-HD5 Killed");
            poststatus("OOB-HD5 Killed");
            str_exp_setdectype(1, 0);
            stoptest();
        }
        else
        {
            userfilelog("OOB-HD? Killed");
            poststatus("OOB-HD? Killed");
            str_exp_setdectype(1, 0);
            stoptest();
        }
    }
    return;
}

int MGET(int mcid, int offset) //F1 MCFS
{
    char cmd[32];
    char addr[16];
    int  cmdlen,i,naddr,nOffSn;
    
    nOffSn = offset/512;
    sprintf(cmd,"TF %x 0 0 1 %x",mcid, nOffSn);
    strlen(cmd,cmdlen);
    
    ONERROR(dummyerror);
    str_usercmd(cmd,cmdlen,5000,512,0,RDBUF,512);
    ONERRORCLEAR();
    
    strstr(RDBUF,"OK",i);
    if(i == 0)
        return 1;

    strfind(RDBUF,"=",cmdlen);
    cmdlen = cmdlen+2;  //space
    
    for(i = 0; i < 8; i++)
        addr[i]=RDBUF[cmdlen+i];
    
    atoi(addr,0,16,naddr); 
    if(addr == 0)
        return 1;
	str_setbyteswap(0);
    sputd(cmd,"DS %x 100",naddr+(offset%512));
    strlen(cmd,cmdlen);
    str_usercmd(cmd,cmdlen,2000,1716,1,RDBUF,512);
	str_setbyteswap(1);
    return 0;
}

void ReadZoneInfo()   //F1¿ë Serial CMDÇÊ¿ä
{
    int nHd,nZn,nOffs,nValue,cmdlen;
    char cmd[32];
    
    poststatus("ReadZoneInfo");
    sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    delays(1);
        
    for( nHd=0; nHd < MAX_HEAD; nHd++)
    {
        //Read Zone
        {
            char cmd[32];
            char addr[16];
            int  cmdlen,i,naddr,offset;
            
            sputd(cmd,"GR %d",nHd);
            strlen(cmd,cmdlen);
            ONERROR(dummyerror);
            str_usercmd(cmd,cmdlen,2000,cmdlen+34,0,RDBUF,512);
            ONERRORCLEAR();
            
            strfind(RDBUF,"=",cmdlen);
            cmdlen = cmdlen+2;  //space
            
            for(i = 0; i < 8; i++)
                addr[i]=RDBUF[cmdlen+i];
            
            atoi(addr,0,16,naddr); 
            if(addr == 0)
            {
                userfilelog("ReadZoneInfo addr Fail");
                return;
            }
                
            str_setbyteswap(0);
            sputd(cmd,"DS %x 100",naddr+offset);
            strlen(cmd,cmdlen);
            ONERROR(dummyerror);
            str_usercmd(cmd,cmdlen,2000,1716,1,RDBUF,512);
            ONERRORCLEAR();
            str_setbyteswap(1);
        }
        
        for(nZn = 0; nZn < DEF_MAX_ZN; nZn++)
        {
            nOffs = nZn*16 + 8;    
            nValue = (RDBUF[nOffs+0] & 0xff)<<16;
			nValue = nValue | ((RDBUF[nOffs+1] & 0xff) << 24);
			nValue = nValue | ((RDBUF[nOffs+2] & 0xff) <<0);
			nValue = nValue | ((RDBUF[nOffs+3] & 0xff) <<8);
			TBL_ZONE[nHd][nZn] = nValue;
            
            if(nValue == 0 )
            {
                MAX_ZONE = nZn-1;
                break;
            }
            //sprintf(EXPLOG,"HD:%d ZN:%d CYL:%d", nHd, nZn, TBL_ZONE[nHd][nZn]);
            //userfilelog(EXPLOG);
        }
    }
    return;
}

void CHK_DList()    //¼öÁ¤ ÇÊ¿ä -> Chart¸¸ ¸¸µé¸é µÊ -> TBL_DLIST[MAX_HEAD][MAX_ZONE]
{
    int nHd,nZn,nValue,nOffs,nRet,cmdlen;
    char cmd[32];
    
    poststatus("CHK_DList");
    
    sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    delays(1);
        
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nOffs=0;
        str_getmassdata(RDBUF,22+nHd*23,1,nRet);
        
        if(nRet != 512)
        {
            //poststatus("CHK_DList");
            userfilelog("CHK_DList Fail");
            return;
            //stoptest();
        }
        for(nZn=1; nZn<MAX_ZONE; nZn++) //MASS Data Zn1 ~ 24
        {
            nValue = (RDBUF[nOffs+0] & 0xff)<<24;
			nValue = nValue | ((RDBUF[nOffs+1] & 0xff) << 16);
			nValue = nValue | ((RDBUF[nOffs+2] & 0xff) <<8);
			nValue = nValue | ((RDBUF[nOffs+3] & 0xff) <<0);
            TBL_DLIST[nHd][nZn]=nValue;
            nOffs = nOffs + 4;
        }
    }
    
    for(nHd=0;nHd<MAX_HEAD;nHd++)
    {
        for(nZn=0;nZn<MAX_ZONE;nZn++)
        {
            sprintf(EXPLOG,"DList Hd:%d Zn:%d Def:%d",nHd,nZn,TBL_DLIST[nHd][nZn]);
            userfilelog(EXPLOG);
        }
    }
    
    return;
}

void CHK_ELogList()    //¼öÁ¤ ÇÊ¿ä -> TBL_ELOG[MAX_HEAD][MAX_ZONE]
{
    int nHd,nZn,nValue,nOffs,nRet,cmdlen;
    char cmd[32];
    
    poststatus("CHK_ELogList");
    
    sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    delays(1);
        
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nOffs=0;
        str_getmassdata(RDBUF,35+nHd*23,1,nRet);
        if(nRet != 512)
        {
            //poststatus("CHK_ELogList");
            userfilelog("CHK_ELogList Fail");
            return;
            //stoptest();
        }
        for(nZn=1; nZn<MAX_ZONE; nZn++) //MASS Data Zn1 ~ 24
        {
            nValue = (RDBUF[nOffs+0] & 0xff)<<24;
			nValue = nValue | ((RDBUF[nOffs+1] & 0xff) << 16);
			nValue = nValue | ((RDBUF[nOffs+2] & 0xff) <<8);
			nValue = nValue | ((RDBUF[nOffs+3] & 0xff) <<0);
            TBL_ELOG[nHd][nZn]=nValue;
            nOffs = nOffs + 4;
        }
    }
    
    for(nHd=0;nHd<MAX_HEAD;nHd++)
    {
        for(nZn=0;nZn<MAX_ZONE;nZn++)
        {
            sprintf(EXPLOG,"ELog Hd:%d Zn:%d Def:%d",nHd,nZn,TBL_ELOG[nHd][nZn]);
            userfilelog(EXPLOG);
        }
    }
    return;
}

void CHK_HTDList(int nThres)    //¼öÁ¤ ÇÊ¿ä -> Chart¸¸ ¸¸µé¸é µÊ -> TBL_DLIST[MAX_HEAD][MAX_ZONE]
{
    
    int nHd,nZn,nValue,nOffs,nRet,cmdlen,nNode,nCyl,nSkew,nType,nEcode,nCnt,nBufOffs,nTemp;
    char cmd[32];
    
    poststatus("CHK_HTDList");
    
    sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    delays(1);
        
    if( MGET(162,0) !=0 )
    {
        //poststatus("HTDLIST");
        userfilelog("HTDList Fail");
        return;
        //stoptest();
    }
    nNode = (RDBUF[0x10+0] & 0xff);
    nNode = nNode | ((RDBUF[0x10+1] & 0xff) <<8);
    nNode = nNode | ((RDBUF[0x10+2] & 0xff) <<16);
    nNode = nNode | ((RDBUF[0x10+3] & 0xff) <<24);
    if(nNode == 0) return;
	if(nNode > 5000) 
    {
        str_exp_setdectype(1, 0);
        poststatus("GoWinstar");
        stoptest();
    }
    
    nOffs    = 0x10;
    for(nCnt=0; nCnt<nNode; nCnt++)
	{
		//read
        if(nOffs%512 == 0)
        {
            if( MGET(162,nOffs) !=0 )
            {
                poststatus("DLIST");
                return;
            }
            if(nOffs == 0x10) nBufOffs = 0x10;
            else nBufOffs = 0;
        }
        
        nCyl  = (RDBUF[nBufOffs+4] & 0xff);
		nCyl  = nCyl | ((RDBUF[nBufOffs+5] & 0xff) << 8);
		nCyl  = nCyl | ((RDBUF[nBufOffs+6] & 0xff) <<16);
		nCyl  = nCyl | ((RDBUF[nBufOffs+7] & 0xff) <<24);
        
    	nHd   = RDBUF[nBufOffs+8] & 0xff;
        
		nSkew = RDBUF[nBufOffs+10] & 0xff;
		nSkew = nSkew | ((RDBUF[nBufOffs+11] & 0xff) << 8);
        
		//nType = RDBUF[nBufOffs+8] & 0xff;
		//nType = nType | ((RDBUF[nBufOffs+9] & 0xff) << 8);
        
		//nEcode= RDBUF[nBufOffs+10] & 0xff;
		//nEcode= nEcode | ((RDBUF[nBufOffs+11] & 0xff) << 8);		
        
        nZn = GetNumOfZone(nCyl,nHd);
        TBL_DLIST[nHd][nZn] = TBL_DLIST[nHd][nZn] + 1;	
        
        //int ContinousCheck(int nHd,int nCyl,int nSN,int DCyl,int DSN,int nThres);
        nTemp=DEFCONTINOUS[nHd];
        if(nTemp==0 && nSkew>5)
        {
            nTemp=ContinousCheck(nHd,nCyl,nSkew,2,3,nThres);
            
            if(nTemp!=0)
            {
                DEFCONTINOUS[nHd] = 1;			
                //printf("DefCont Hd:%d Cyl:%d\n",nHd,nTemp);
                //delays(3);
            }
        }
        
        nOffs=nOffs+16;
        nBufOffs = nBufOffs + 16;
	}
    
    for(nHd=0;nHd<MAX_HEAD;nHd++)
    {
        for(nZn=0;nZn<MAX_ZONE;nZn++)
        {
            sprintf(EXPLOG,"HTDList Hd:%d Zn:%d Def:%d",nHd,nZn,TBL_DLIST[nHd][nZn]);
            userfilelog(EXPLOG);
        }
    }
    
    return;
}

void CHK_HTELogList()    //¼öÁ¤ ÇÊ¿ä -> TBL_ELOG[MAX_HEAD][MAX_ZONE]
{
    int nHd,nZn,nValue,nOffs,nRet,cmdlen,nNode,nCyl,nCnt,nBufOffs;
    char cmd[32];
    
    poststatus("CHK_ELogList");
    
    sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    delays(1);
        
    if( MGET(61,0) !=0 )
    {
        //poststatus("HTELog");
        userfilelog("HTELog Fail");
        return;
        //stoptest();
    }
    nNode = (RDBUF[0x10+0] & 0xff);
    nNode = nNode | ((RDBUF[0x10+1] & 0xff) <<8);
    nNode = nNode | ((RDBUF[0x10+2] & 0xff) <<16);
    nNode = nNode | ((RDBUF[0x10+3] & 0xff) <<24);
    if(nNode == 0) return;
	if(nNode > 5000) 
    {
        str_exp_setdectype(1, 0);
        poststatus("GoWinstar");
        stoptest();
    }
        
    nOffs = 0x20;
    for(nCnt=0; nCnt<nNode; nCnt++)
	{
		//read
        if(nOffs%512 == 0)
        {
            if( MGET(61,nOffs) !=0 )
            {
                poststatus("HTELog");
                return;
            }
            if(nOffs == 0x20) nBufOffs = 0x20;
            else nBufOffs = 0;
        }
        nValue = 0;
        nValue = RDBUF[nBufOffs+10] & 0xff;
		nValue = nValue | ((RDBUF[nBufOffs+11] & 0xff) << 8);

		if((nValue != 0x7F04) && (nValue != 0))
		{
			nCyl = (RDBUF[nBufOffs] & 0xff)<<0;
			nCyl = nCyl | ((RDBUF[nBufOffs+1] & 0xff) << 8);
			nCyl = nCyl | ((RDBUF[nBufOffs+2] & 0xff) <<16);
			nCyl = nCyl | ((RDBUF[nBufOffs+3] & 0xff) <<24);
            nHd  = RDBUF[nBufOffs+4] & 0x0f;
            
            //sprintf(EXPLOG,"nCyl:%d,nHd:%d",nCyl,nHd);
            //userfilelog(EXPLOG);
            
			nZn  = GetNumOfZone(nCyl,nHd);
			TBL_ELOG[nHd][nZn] = TBL_ELOG[nHd][nZn] + 1;
		}
		nOffs=nOffs+16;
        nBufOffs = nBufOffs + 16;
	}
    
    for(nHd=0;nHd<MAX_HEAD;nHd++)
    {
        for(nZn=0;nZn<MAX_ZONE;nZn++)
        {
            sprintf(EXPLOG,"HTELog Hd:%d Zn:%d Def:%d",nHd,nZn,TBL_ELOG[nHd][nZn]);
            userfilelog(EXPLOG);
        }
    }
    return;
}

void CHK_DefTargetHDandMaxRatio()   //int DEF_MAXHDRATE, DEF_MAXDEFTARGETHD, DEF_TTLDEFCNT
{
	int nHd,nZn,nCnt,ntemp,ntemp0,ntemp1,nMaxDef,nSkipHd;
   
	DEF_TTLDEFCNT = 1;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		ntemp=0;ntemp0=0;ntemp1=0;
		for(nZn=0; nZn<MAX_ZONE; nZn++)
		{
			ntemp0 = TBL_DLIST[nHd][nZn];
			ntemp1 = TBL_ELOG[nHd][nZn];
			ntemp = ntemp + ntemp0 + ntemp1;
			TTL_DLISTandELOG[nHd] = ntemp;
		}
		DEF_TTLDEFCNT = DEF_TTLDEFCNT + ntemp;
		sprintf(EXPLOG,"HD:%d TTL_DLISTandELOG:%d",nHd,ntemp);
        userfilelog(EXPLOG);
		sprintf(EXPLOG,"DEF_TTLDEFCNT:%d",DEF_TTLDEFCNT);			
        userfilelog(EXPLOG);
	}

	nMaxDef=0;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		ntemp0 = TTL_DLISTandELOG[nHd];
		if((ntemp0 != 0) && (ntemp0 > nMaxDef))
		{
			nMaxDef = TTL_DLISTandELOG[nHd];
			DEF_MAXDEFTARGETHD = nHd;
			sprintf(EXPLOG,"HD:%d nMaxDef:%d",DEF_MAXDEFTARGETHD,nMaxDef);
            userfilelog(EXPLOG);
		}
	}
	////// Check ZONESKIP
    ntemp = 0;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		if(ZONESKIP[nHd] == 0)
		{
            if(ZONEUSED[nHd] != 0)
            {
                sprintf(EXPLOG,"Def ZONESKIP %x and ZONEUSED %x deteced -> HD%d Zone Skip",ZONESKIP[nHd],ZONEUSED[nHd],nHd);
                userfilelog(EXPLOG);
                DEF_TTLDEFCNT = DEF_TTLDEFCNT + nMaxDef;
                nSkipHd = nHd;
                ntemp++;
            }
		}
		else if(ZONESKIP[nHd] != 0x1FFFFFE)
		{
            sprintf(EXPLOG,"Def ZONESKIP %x and ZONEUSED %x deteced -> HD%d Zone Skip",ZONESKIP[nHd],ZONEUSED[nHd],nHd);
            userfilelog(EXPLOG);
            DEF_TTLDEFCNT = DEF_TTLDEFCNT + nMaxDef;
            nSkipHd = nHd;
            ntemp++;
		}		
	}
	//////
    sputd(EXPLOG,"ntemp : %d",ntemp);
    userfilelog(EXPLOG);
    
	DEF_MAXHDRATE = (nMaxDef * 1000 / DEF_TTLDEFCNT);
    if(ntemp == 1)
    {
        DEF_MAXDEFTARGETHD = nSkipHd;
        DEF_MAXHDRATE = 500;
        /*
        if(ZONESKIP[5] != 0)
        {
            DEF_MAXDEFTARGETHD = 5;
            DEF_MAXHDRATE = 500;
        }
        */
    }
    if(ntemp > 1)
    {
        DEF_MAXDEFTARGETHD = 5;
        DEF_MAXHDRATE = 10;
    }
	sprintf(EXPLOG,"TargetHD:%d MAXHDRATE:%d",DEF_MAXDEFTARGETHD,DEF_MAXHDRATE);
    userfilelog(EXPLOG);
    return;
}

void CHK_GetBPIandTPI()
{
//FINAL_CAPA[6], ORG_CAPA[6], FINAL_TPI[6], ORG_TPI[6];
//ORG_BPI[150], ORG_BPIAVG[6], ORG_BPIBER[150], ORG_BPIAVGBER[6]
//FINAL_BPI[150], FINAL_BPIAVG[6], FINAL_BPIBER[150], FINAL_BPIAVGBER[6]

	float fber0, fber1, ftemp0, ftemp1;
	int nCnt,nHd,nZn,nOffs,nOffsCNT,nValue,ntemp0,ntemp1,ntemp2,ntemp3,ntemp4,ntemp5,nSum;

    poststatus("GetBPI&747");

    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        FINAL_CAPA[nHd]=0;
        ORG_CAPA[nHd]=0;
        FINAL_TPI[nHd]=0;
        ORG_TPI[nHd]=0;
        ORG_BPIAVG[nHd]=0;
        ORG_BPIAVGBER[nHd]=0;
        FINAL_BPIAVG[nHd]=0;
        FINAL_BPIAVGBER[nHd]=0;
    }

    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        for(nZn=0; nZn<=MAX_ZONE; nZn++)
        {
            FINAL_BPIBER[nHd][nZn]=0;
            FINAL_BPI[nHd][nZn]=0;
            ORG_BPIBER[nHd][nZn]=0;
            ORG_BPI[nHd][nZn]=0;
        }
    }

//ORG_CAPA, FINAL_CAPA
    nOffs=0xF30;
    nOffsCNT=0;
    if( MGET(191,nOffs) !=0 )
    {
        poststatus("GoWinstar");
        userfilelog("MGET CHK_GetBPIandTPI");
        userfilelog("1C04 GoWinstar");
        str_exp_setdectype(1, 0);
        poststatus("GoWinstar");
        return;
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        ntemp0 = RDBUF[nOffsCNT+0x8] & 0xff;
        ntemp0 = ntemp0 | ((RDBUF[nOffsCNT+0x8+1] & 0xff) << 8);
        ntemp0 = ntemp0 | ((RDBUF[nOffsCNT+0x8+2] & 0xff) << 16);
        ntemp0 = ntemp0 | ((RDBUF[nOffsCNT+0x8+3] & 0xff) << 24);
        ORG_CAPA[nHd] = ntemp0;
        
        ntemp1 = RDBUF[nOffsCNT+0x28] & 0xff;
        ntemp1 = ntemp1 | ((RDBUF[nOffsCNT+0x28+1] & 0xff) << 8);
        ntemp1 = ntemp1 | ((RDBUF[nOffsCNT+0x28+1] & 0xff) << 16);
        ntemp1 = ntemp1 | ((RDBUF[nOffsCNT+0x28+1] & 0xff) << 24);
        FINAL_CAPA[nHd] = ntemp1;
        
        nOffsCNT = nOffsCNT+4;
        
        //sprintf(EXPLOG,"Hd:%d ORG_CAPA:%d FINAL_CAPA:%d",nHd,ntemp0,ntemp1);
        //userfilelog(EXPLOG);        
	}
//FINAL_TPI
    nValue=0;
    nOffs=0x310;
    nOffsCNT=0;
    if( MGET(191,nOffs) !=0 )
    {
        poststatus("GoWinstar");
        userfilelog("MGET FINAL_TPI");
        userfilelog("1C04 GoWinstar");
        str_exp_setdectype(1, 0);
        poststatus("GoWinstar");
        return;
    }        
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        nValue = RDBUF[nOffsCNT] & 0xff;
        nValue = nValue | ((RDBUF[nOffsCNT+1] & 0xff) << 8);
        if(nValue > 32768)  nValue = nValue - 65536;
        FINAL_TPI[nHd] = nValue;
        nOffsCNT = nOffsCNT+2;
        
        //sprintf(EXPLOG,"Hd:%d FINAL_TPI:%d",nHd,nValue);
        //userfilelog(EXPLOG);        
	}
//ORG_TPI
    nValue=0;
    nOffs=0x320;
    nOffsCNT=0;
    if( MGET(191,nOffs) !=0 )
    {
        poststatus("GoWinstar");
        userfilelog("MGET ORG_TPI");
        userfilelog("1C04 GoWinstar");
        str_exp_setdectype(1, 0);
        poststatus("GoWinstar");
        return;
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        nValue = RDBUF[nOffsCNT] & 0xff;
        nValue = nValue | ((RDBUF[nOffsCNT+1] & 0xff) << 8);
        if(nValue > 32768)  nValue = nValue - 65536;
        ORG_TPI[nHd] = nValue;
        nOffsCNT = nOffsCNT+2;
        
        //sprintf(EXPLOG,"Hd:%d FINAL_TPI:%d",nHd,nValue);
        //userfilelog(EXPLOG);        
	}
//ORG_BPI
    nSum=0; nValue=0;
    nOffs=0x190;
    nOffsCNT=0;
    if( MGET(191,nOffs) !=0 )
    {
        poststatus("GoWinstar");
        userfilelog("MGET ORG_BPI");
        userfilelog("1C04 GoWinstar");
        str_exp_setdectype(1, 0);
        poststatus("GoWinstar");
        return;
    }    
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		for(nZn=0; nZn<24; nZn++)
		{
			nValue = RDBUF[nOffsCNT] & 0xff;
			nValue = nValue | ((RDBUF[nOffsCNT+1] & 0xff) << 8);
            if(nValue > 32768)  nValue = nValue - 65536;
			ORG_BPI[nHd][nZn] = nValue;
            nSum = nSum + nValue;
			nOffsCNT = nOffsCNT+2;
            
            //sprintf(EXPLOG,"Hd:%d ORG_BPI:%d",nHd,nValue);
            //userfilelog(EXPLOG);        
		}
        if(nSum<0)
        {
            nSum=(nSum * -1)/24;
            ORG_BPIAVG[nHd] = nSum * -1;
        }
        else
        {
            ORG_BPIAVG[nHd] = nSum/24;
        }
        nSum=0;
	}
//FINAL_BPI
    nSum=0; nValue=0;
    nOffs=0x10;
    nOffsCNT=0;
    if( MGET(191,nOffs) !=0 )
    {
        poststatus("GoWinstar");
        userfilelog("MGET FINAL_BPI");
        userfilelog("1C04 GoWinstar");
        str_exp_setdectype(1, 0);
        poststatus("GoWinstar");
        return;
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		for(nZn=0; nZn<24; nZn++)
		{
			nValue = RDBUF[nOffsCNT] & 0xff;
			nValue = nValue | ((RDBUF[nOffsCNT+1] & 0xff) << 8);
            if(nValue > 32768)  nValue = nValue - 65536;
			FINAL_BPI[nHd][nZn] = nValue;
            nSum = nSum + nValue;
			nOffsCNT = nOffsCNT+2;
            
            //sprintf(EXPLOG,"Hd:%d FINAL_BPI:%d",nHd,nValue);
            //userfilelog(EXPLOG);        
		}
        if(nSum<0)
        {
            nSum=(nSum * -1)/24;
            FINAL_BPIAVG[nHd] = nSum * -1;
        }
        else
        {
            FINAL_BPIAVG[nHd] = nSum/24;
        }
        nSum=0;
	}
//ORG_BPIBER
    nValue=0;
    nOffs=0x330;
    nOffsCNT=0;
    if( MGET(191,nOffs) !=0 )
    {
        poststatus("GoWinstar");
        userfilelog("MGET ORG_BPIBER");
        userfilelog("1C04 GoWinstar");
        str_exp_setdectype(1, 0);
        poststatus("GoWinstar");
        return;
    }
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		for(nZn=0; nZn<24; nZn++)
		{
			nValue = RDBUF[nOffsCNT] & 0xff;
			nValue = nValue | ((RDBUF[nOffsCNT+1] & 0xff) << 8);
			nValue = nValue | ((RDBUF[nOffsCNT+2] & 0xff) << 16);
			nValue = nValue | ((RDBUF[nOffsCNT+3] & 0xff) << 24);
            
			ORG_BPIBER[nHd][nZn] = nValue;
			nOffsCNT = nOffsCNT+4;
            
            //sprintf(EXPLOG,"Hd:%d ORG_BPIBER:%d",nHd,nValue);
            //userfilelog(EXPLOG);        
		}
	}
//FINAL_BPIBER
    nValue=0;
    nOffs=0x630;
    nOffsCNT=0;
    if( MGET(191,nOffs) !=0 )
    {
        poststatus("GoWinstar");
        userfilelog("MGET FINAL_BPIBER");
        userfilelog("1C04 GoWinstar");
        str_exp_setdectype(1, 0);
        poststatus("GoWinstar");
        return;
    }    
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		for(nZn=0; nZn<24; nZn++)
		{
			nValue = RDBUF[nOffsCNT] & 0xff;
			nValue = nValue | ((RDBUF[nOffsCNT+1] & 0xff) << 8);
			nValue = nValue | ((RDBUF[nOffsCNT+2] & 0xff) << 16);
			nValue = nValue | ((RDBUF[nOffsCNT+3] & 0xff) << 24);
            
			FINAL_BPIBER[nHd][nZn] = nValue;
			nOffsCNT = nOffsCNT+4;
            
            //sprintf(EXPLOG,"Hd:%d FINAL_BPIBER:%d",nHd,nValue);
            //userfilelog(EXPLOG);        
		}
	}

//debug
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        ntemp0 = FINAL_TPI[nHd];
        ntemp1 = ORG_TPI[nHd];
        ntemp2 = FINAL_BPIAVG[nHd];
        ntemp3 = ORG_BPIAVG[nHd];
        ntemp4 = ORG_CAPA[nHd];
        ntemp5 = FINAL_CAPA[nHd];
        sprintf(EXPLOG,"Hd:%d FINAL_TPI:%d ORG_TPI:%d FINAL_BPIAVG:%d ORG_BPIAVG:%d",nHd,ntemp0,ntemp1,ntemp2,ntemp3);
        userfilelog(EXPLOG);
        sprintf(EXPLOG,"ORG_CAPA[%d]:%d FINAL_CAPA[%d]:%d",nHd,ntemp4,nHd,ntemp5);
        userfilelog(EXPLOG);
    }
    sprintf(EXPLOG,"==================================================================================\n");
    userfilelog(EXPLOG);
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		for(nZn=0; nZn<24; nZn++)
		{
			ntemp0 = FINAL_BPI[nHd][nZn];
            ntemp1 = ORG_BPI[nHd][nZn];
            ntemp2 = FINAL_BPIBER[nHd][nZn];
            ntemp3 = ORG_BPIBER[nHd][nZn];
            
            if(ntemp2!=0)
            {
                ntemp2=ntemp2/10*4096;
                log10(ntemp2,fber0);
            }
            else
            {
                ntemp2=(ntemp2+1)/10*4096;
                log10(ntemp2,fber0);
            }
            if(ntemp3!=0)
            {
                ntemp3=ntemp3/10*4096;
                log10(ntemp3,fber1);
            }
            else
            {
                ntemp2=(ntemp2+1)/10*4096;
                log10(ntemp2,fber1);
            }
			sprintf(EXPLOG,"Hd:%d Zn:%d FINAL_BPI:%d ORG_BPI:%d FINAL_BPIBER:%5.2f ORG_BPIBER:%5.2f",nHd,nZn,ntemp0,ntemp1,fber0,fber1);
            userfilelog(EXPLOG);
		}
	}
}

void CHK_GetREFandATWCSM()	            // REFCSM and ATWCSM Results
{
//int REFCSMCnt[4][DEF_MAX_HD][21];     //6*16*4
//int ATWCSMCnt[4][DEF_MAX_HD][21];     //6*16*4
//int HIGHREFCSM[DEF_MAX_HD];
//int HIGHATWCSM[DEF_MAX_HD];
//int HIGHESTREFCSM[DEF_MAX_HD];
//int HIGHESTATWCSM[DEF_MAX_HD];

	int i,nCnt,nHd,nTest,nOffs,nOffsCnt,nValue,nTemp;
    
    poststatus("CHK_GetREF&ATWCSM");

    for(nHd=0; nHd<=MAX_HEAD; nHd++)
    {
        HIGHREFCSM[nHd]=0;
        HIGHATWCSM[nHd]=0;
        HIGHESTREFCSM[nHd]=0;
        HIGHESTATWCSM[nHd]=0;
    }
    for(nCnt=0; nCnt<4; nCnt++)
    {
        for(nHd=0; nHd<=MAX_HEAD; nHd++)
        {
            for(i=0; i<16; i++)
            {
                REFCSMCnt[nCnt][nHd][i]=0;     //6*16*4
                ATWCSMCnt[nCnt][nHd][i]=0;     //6*16*4
            }
        }
    }

    //nTest
    nOffs = 0x00;
    if( MGET(127,nOffs) !=0 )
    {
        poststatus("MGET CHK_GetREF&ATWCSM");
        return;
    }

    nTest = RDBUF[0x12] & 0xff;
    sprintf(EXPLOG,"nTest: %d",nTest);
    userfilelog(EXPLOG);
    
    for(nCnt=0; nCnt<4; nCnt++)
    {
        if(MAX_HEAD==1)
        {
            if(nTest==0x13)
            {
                if(nCnt==0) nOffs = 0x20;       //Zn1
                if(nCnt==1) nOffs = 0x1320;     //Zn7
                if(nCnt==2) nOffs = 0x4C20;     //Zn19
                if(nCnt==3) nOffs = 0x7220;     //Zn24
            }
            else if(nTest==0x15)
            {
                if(nCnt==0) nOffs = 0x20;       //Zn1
                if(nCnt==1) nOffs = 0x1520;     //Zn7
                if(nCnt==2) nOffs = 0x5420;     //Zn19
                if(nCnt==3) nOffs = 0x7E20;     //Zn24
            }
            else
            {
                if(nCnt==0) nOffs = 0x20;       //Zn1
                if(nCnt==1) nOffs = 0x2020;     //Zn7
                if(nCnt==2) nOffs = 0x8020;     //Zn19
                if(nCnt==3) nOffs = 0xC020;     //Zn24
            }
        }
        else if(MAX_HEAD==4)
        {
            if(nCnt==0) nOffs = 0x20;       //Zn1
            if(nCnt==1) nOffs = 0x1520;     //Zn7
            if(nCnt==2) nOffs = 0x5420;     //Zn19
            if(nCnt==3) nOffs = 0x7E20;     //Zn24
        }
        else
        {
            if(nCnt==0) nOffs = 0x20;       //Zn1
            if(nCnt==1) nOffs = 0x2020;     //Zn7
            if(nCnt==2) nOffs = 0x8020;     //Zn19
            if(nCnt==3) nOffs = 0xC020;     //Zn24
        }
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            //sprintf(EXPLOG,"nOffs:%x\n",nOffs);
            //userfilelog(EXPLOG);
            MGET(127,nOffs);
            nOffsCnt=0;
            for(i=0; i<nTest; i++)
            {
                nValue = RDBUF[nOffsCnt+0x8] & 0xff;
                nValue = nValue | ((RDBUF[nOffsCnt+0x8+1] & 0xff) << 8);
                nValue = nValue | ((RDBUF[nOffsCnt+0x8+2] & 0xff) << 16);
                nValue = nValue | ((RDBUF[nOffsCnt+0x8+2] & 0xff) << 24);
                REFCSMCnt[nCnt][nHd][i] = nValue;
                //sprintf(EXPLOG,"[nCnt:%d] [nHd:%d] RefCSM[%d]= %d",nCnt,nHd,i,REFCSMCnt[nCnt][nHd][i]);
                //userfilelog(EXPLOG);
                if(nValue >= 100000)
                {
                    //sprintf(EXPLOG,"HIGHEST [nCnt:%d] [nHd:%d] RefCSM[%d]= %d \n",nCnt,nHd,i,nValue);
                    //userfilelog(EXPLOG);
                    HIGHESTREFCSM[nHd]=HIGHESTREFCSM[nHd]+1;
                }
                if(nValue >= 40000)
                {
                    //sprintf(EXPLOG,"HIGH [nCnt:%d] [nHd:%d] RefCSM[%d]= %d \n",nCnt,nHd,i,nValue);
                    //userfilelog(EXPLOG);
                    HIGHREFCSM[nHd]=HIGHREFCSM[nHd]+1;
                }
                    
                nTemp = RDBUF[nOffsCnt+0xC] & 0xff;
                nTemp = nTemp | ((RDBUF[nOffsCnt+0xC+1] & 0xff) << 8);
                nTemp = nTemp | ((RDBUF[nOffsCnt+0xC+2] & 0xff) << 16);
                nTemp = nTemp | ((RDBUF[nOffsCnt+0xC+2] & 0xff) << 24);
                ATWCSMCnt[nCnt][nHd][i] = nTemp;
                //sprintf(EXPLOG,"[nCnt:%d] [nHd:%d] ATWCSM[%d]= %d",nCnt,nHd,i,ATWCSMCnt[nCnt][nHd][i]);
                //userfilelog(EXPLOG);
                if(nValue >= 100000)
                {
                    //sprintf(EXPLOG,"HIGHEST [nCnt:%d] [nHd:%d] RefCSM[%d]= %d \n",nCnt,nHd,i,nValue);
                    //userfilelog(EXPLOG);
                    HIGHESTATWCSM[nHd]=HIGHESTATWCSM[nHd]+1;
                }
                if(nTemp >= 40000)
                {
                    //sprintf(EXPLOG,"HIGH [nCnt:%d] [nHd:%d] ATWCSM[%d]= %d \n",nCnt,nHd,i,nTemp);
                    //userfilelog(EXPLOG);
                    HIGHATWCSM[nHd]=HIGHATWCSM[nHd]+1;
                }
                nOffsCnt = nOffsCnt+0x10;
            }
            if(MAX_HEAD==4) nOffs = nOffs+nOffsCnt+0x150;
            else            nOffs = nOffs+nOffsCnt+0x300;
            nValue=0; nTemp=0;
        }
	}
    
    for(nCnt=0; nCnt<4; nCnt++)
    {
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            for(i=0; i<nTest; i++)
            {
                nValue = REFCSMCnt[nCnt][nHd][i];
                nTemp  = ATWCSMCnt[nCnt][nHd][i];
                sprintf(EXPLOG,"Cnt:%d Hd:%d Test:%d REFCSM:%d ATWCSM:%d",nCnt,nHd,i,nValue,nTemp);
                userfilelog(EXPLOG);
            }
        }
    }
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nValue = HIGHREFCSM[nHd];
        nTemp  = HIGHATWCSM[nHd];
        sprintf(EXPLOG,"HIGHREFCSM[%d] = %d HIGHATWCSM[%d] = %d",nHd,nValue,nHd,nTemp);
        userfilelog(EXPLOG);
    }
}

void CHK_GetBER()
{
	float fBER, ftemp;
	int nHd,nZn,nOffs,nValue,nTTLReadCnt,nTTLErrCnt,itemp,cmdlen;
    char cmd[32];

    poststatus("CHK_GetBER");
    sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    delays(1);
    
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        for(nZn=0; nZn<MAX_ZONE; nZn++)
        {
            READCNT10S[nHd][nZn] = 0;
            ERRCNT10S[nHd][nZn] = 0;
        }
	}

	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        nTTLReadCnt = 0;
        nTTLErrCnt = 0;
        
        //ReadCnt10s
		nOffs = 0x24 + (nHd*0x68);
        if( MGET(114,nOffs) !=0 )
        {
            poststatus("MGET BER");
            return;
        }
        nOffs=0; nValue = 0;
		for(nZn=0; nZn<MAX_ZONE; nZn++)
		{
			nValue = RDBUF[nOffs] & 0xff;
			nValue = nValue | ((RDBUF[nOffs+1] & 0xff) << 8);
			nValue = nValue | ((RDBUF[nOffs+2] & 0xff) <<16);
			nValue = nValue | ((RDBUF[nOffs+3] & 0xff) <<24);
			READCNT10S[nHd][nZn] = nValue;
            nOffs = nOffs+4;
            nTTLReadCnt = nValue + nTTLReadCnt;
			//sprintf(EXPLOG,"Hd:%d Zn:%d ReadCnt10s:%d nTTLReadCnt:%d",nHd,nZn,nValue,nTTLReadCnt);
            //userfilelog(EXPLOG);
        }
        //ErrCnt10s
        nOffs = 0x364 + (nHd*0x68);
        if( MGET(114,nOffs) !=0 )
        {
            poststatus("MGET BER");
            return;
        }
        nOffs=0; nValue = 0;
        for(nZn=0; nZn<MAX_ZONE; nZn++)
        {
        	nValue = RDBUF[nOffs] & 0xff;
			nValue = nValue | ((RDBUF[nOffs+1] & 0xff) << 8);
			nValue = nValue | ((RDBUF[nOffs+2] & 0xff) <<16);
			nValue = nValue | ((RDBUF[nOffs+3] & 0xff) <<24);
            ERRCNT10S[nHd][nZn] = nValue;
            nOffs = nOffs+4;
            nTTLErrCnt = nValue + nTTLErrCnt;
			//sprintf(EXPLOG,"Hd:%d Zn:%d ErrCnt10s:%d nTTLErrCnt:%d",nHd,nZn,nValue,nTTLErrCnt);
            //userfilelog(EXPLOG);
        }
        //f1stAVGBER[nHd]
        itemp = (nTTLReadCnt*8*512)/nTTLErrCnt;
        log10(itemp,ftemp);
        fstAVGBER[nHd]= itemp;
        sprintf(EXPLOG,"f1stAVGBER[%d] : %5.2f",nHd,ftemp);
        userfilelog(EXPLOG);
	}
}

void CHK_GetTPIand747()
{
	float fBER, fTemp;
	int nCnt,nHd,nOffs,nValue;

    poststatus("CHK_GetTPIand747");
    delays(1);
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
        if( MGET(39,nHd*0x140) !=0 )
        {
            poststatus("MGET TPI");
            return;
        }
            
		nOffs = 0xA4;
		for(nCnt=0; nCnt<15; nCnt++)
		{
			nValue = RDBUF[nOffs] & 0xff;
			nValue = nValue | ((RDBUF[nOffs+1] & 0xff) << 8);
			nOffs = nOffs + 4;
            
            //fTemp = 0x10000 - nValue;
			//fBER = fTemp / 256;
            TBL_TPI747[nHd*15+nCnt] = nValue;
			//sprintf(EXPLOG,"Hd:%d nValue:%d fTemp:%5.2f fTBL_TPI747:%5.2f",nHd,nValue,fTemp,fTBL_TPI747[nHd][nCnt]);
            //userfilelog(EXPLOG);
		}
	}
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		for(nCnt=0; nCnt<15; nCnt++)
		{
            fTemp = 0x10000 - TBL_TPI747[nHd*15+nCnt];
            if(fTemp == 0x10000) fBER = 0;
			else fBER = fTemp / 256;
            sprintf(EXPLOG,"Hd:%d Cnt:%d fTBL_TPI747:%5.2f",nHd,nCnt,fBER);
            userfilelog(EXPLOG);
		}
	}
}

void CHK_TPI74710SData(int nHd)	//MAX747BER, MAX747TARGETHD, TPIREFBER, MAXREFTARGETHD, MAX10SBER, MAX10STARGETHD
{
	float f747temp, fReftemp, f1stAvgtemp, ftemp, ftemp0, ftemp1;
    int nHd, nZn;
  
	CHK_GetTPIand747();	// TPI747 Results
	CHK_GetBER();		// 10SBER Results
        
    poststatus("CHK_TPI74710SData");
    
	if(nHd != 0xff)
	{
		//747Ber
        ftemp0 = (0x10000 - TBL_TPI747[nHd*15+0])/256;
        ftemp1 = (0x10000 - TBL_TPI747[nHd*15+7])/256;
        if(ftemp0 == 0 || ftemp1 == 0)  MAX747BER = 0.0;
        else MAX747BER = (ftemp0+ftemp1)/2;
        MAX747TARGETHD = nHd;
        
		//RefBer
        ftemp0 = (0x10000 - TBL_TPI747[nHd*15+12])/256;
        ftemp1 = (0x10000 - TBL_TPI747[nHd*15+13])/256;
        if(ftemp0 == 0 || ftemp1 == 0)  MAX747BER = 0.0;
        else MAXREFBER = (ftemp0+ftemp1)/2;
        MAXREFTARGETHD = nHd;
        
		//10SBER
        log10(fstAVGBER[nHd],MAX10SBER);
		MAX10STARGETHD = nHd;
		
		sprintf(EXPLOG,"HD:%d MAX747BER:%5.2f",MAX747TARGETHD,MAX747BER);
        userfilelog(EXPLOG);
		sprintf(EXPLOG,"HD:%d MAXREFBER:%5.2f",MAXREFTARGETHD,MAXREFBER);
        userfilelog(EXPLOG);
		sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f",MAX10STARGETHD,MAX10SBER);
        userfilelog(EXPLOG);
	}
	else
	{
        MAX747BER = 10.0;
        MAXREFBER = 10.0;
        MAX10SBER = 10.0;
        
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
			//747Ber
            ftemp0 = (0x10000 - TBL_TPI747[nHd*15+0])/256;
            ftemp1 = (0x10000 - TBL_TPI747[nHd*15+7])/256;
            if(ftemp0 == 0 || ftemp1 == 0)  f747temp = 0.0;
			else f747temp = (ftemp0+ftemp1)/2;
            
			//RefBer
            ftemp0 = (0x10000 - TBL_TPI747[nHd*15+12])/256;
            ftemp1 = (0x10000 - TBL_TPI747[nHd*15+13])/256;
            if(ftemp0 == 0 || ftemp1 == 0)  fReftemp = 0.0;
			fReftemp = (ftemp0+ftemp1)/2;
			
            //10sBER
            log10(fstAVGBER[nHd],f1stAvgtemp);
			
            sprintf(EXPLOG,"HD:%d 747BER:%5.2f",nHd,f747temp);
            userfilelog(EXPLOG);
            sprintf(EXPLOG,"HD:%d RefBER:%5.2f",nHd,fReftemp);
            userfilelog(EXPLOG);  
            sprintf(EXPLOG,"HD:%d 10sBER:%5.2f",nHd,f1stAvgtemp);
            userfilelog(EXPLOG);  
            
            //747BER : MAX747BER, MAXREFTARGETHD
            if((f747temp != 0) && (f747temp < MAX747BER))
			{
				//747BER Data
				MAX747BER = f747temp;
				MAX747TARGETHD = nHd;
			}
            
            //RefBER : MAXREFBER, MAXREFTARGETHD
            if((fReftemp != 0) && (fReftemp < MAXREFBER))
			{
				//RefBER Data
				MAXREFBER = fReftemp;
				MAXREFTARGETHD = nHd;
			}
            
            //10sBER : 		MAX10SBER, MAXREFTARGETHD
            log10(fstAVGBER[nHd],f1stAvgtemp);
            if (f1stAvgtemp < MAX10SBER)
		    {
		    	//10SBER Data
		        MAX10SBER = f1stAvgtemp;
		        MAX10STARGETHD = nHd;
		    }
        }
            
		sprintf(EXPLOG,"HD:%d MAX747BER:%5.2f",MAX747TARGETHD,MAX747BER);
        userfilelog(EXPLOG);
		sprintf(EXPLOG,"HD:%d MAXREFBER:%5.2f",MAXREFTARGETHD,MAXREFBER);
        userfilelog(EXPLOG);
		sprintf(EXPLOG,"HD:%d MAX10SBER:%5.2f",MAX10STARGETHD,MAX10SBER);
        userfilelog(EXPLOG);
	}
}

void CHK_GetFOD()   //F1 complet
{
	int nCnt,nHd,nZn,nOffs,nValue,cmdlen;
    char cmd[32];

    poststatus("CHK_GetFOD");
    sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    delays(1);

    for(nHd=0; nHd<MAX_HEAD;nHd++)
	{
        if( MGET(119,nHd*25*4+0x150) != 0)
        {
            poststatus("MGET FOD");
            return;
        }
        nOffs = 0;
        
		for(nZn=0; nZn<MAX_ZONE; nZn++)
		{
			nValue = RDBUF[nOffs] & 0xff;
			nValue = nValue | ((RDBUF[nOffs+1] & 0xff) << 8);
			nValue = nValue | ((RDBUF[nOffs+2] & 0xff) <<16);
			nValue = nValue | ((RDBUF[nOffs+3] & 0xff) <<24);
			FOD_FH[nHd][nZn] = nValue;
			nOffs = nOffs+4;
		}
	}

	for(nHd=0; nHd<MAX_HEAD;nHd++)
	{
		for(nZn=0; nZn<MAX_ZONE;nZn++)
		{
			nValue = FOD_FH[nHd][nZn];
            sprintf(EXPLOG,"Hd:%d Zn:%d FOD_FH:%d",nHd,nZn,nValue);
            userfilelog(EXPLOG);
		}
	}
}

void CHK_FODMaxandMinValue(int ntargetHd)  //0xff -> All Head Fail
{
    //int MAXFODFH, MINFODFH, MAXFODTARGETHD, MINFODTARGETHD;
    //int HDMAXFODFH[6], HDMINFODFH[6], HDMAX2NDFODFH[6], HDMIN2NDFODFH[6];
    
	int nZn,nHd,ntemp0,ntemp1;
    int nmaxtemp,nmaxhdtemp,nmintemp,nminhdtemp;
    int nmax2ndtemp,nmin2ndtemp;

//CHK_GetFOD();		int FOD_FH[nHd][nZn]; //6*25
	CHK_GetFOD();
    if(ntargetHd != 0xff)
    {
    	sprintf(EXPLOG,"ntargetHd : %d",ntargetHd);
        userfilelog(EXPLOG);
		nmaxtemp=0; nmintemp=999999;
		for(nZn=0; nZn<MAX_ZONE; nZn++)
		{
			ntemp0 = FOD_FH[ntargetHd][nZn];
			ntemp1 = FOD_FH[ntargetHd][nZn];
			if((ntemp0 != 0) && (ntemp0 > nmaxtemp))
			{
				nmaxtemp = FOD_FH[ntargetHd][nZn];
				nmaxhdtemp = ntargetHd;
			}
			if((ntemp1 != 0) && (ntemp1 < nmintemp))
			{
				nmintemp = FOD_FH[ntargetHd][nZn];
				nminhdtemp = ntargetHd;
			}
		}
        MAXFODFH = nmaxtemp;
        MAXFODTARGETHD = nmaxhdtemp;
        MINFODFH = nmintemp;
        MINFODTARGETHD = nminhdtemp;
        
    	sprintf(EXPLOG,"Results HD:%d MAXFODFH:%d",MAXFODTARGETHD,MAXFODFH);
        userfilelog(EXPLOG);
    	sprintf(EXPLOG,"Results HD:%d MINFODFH:%d",MINFODTARGETHD,MINFODFH);
        userfilelog(EXPLOG);
    }
    else
    {
    	for(nHd=0; nHd<MAX_HEAD; nHd++)
    	{
            nmaxtemp=0; nmintemp=999999;
            nmax2ndtemp=0; nmin2ndtemp=999999;
    		for(nZn=0; nZn<MAX_ZONE; nZn++)
    		{
    			ntemp0 = FOD_FH[nHd][nZn];
    			ntemp1 = FOD_FH[nHd][nZn];
    			if((ntemp0 != 0) && (ntemp0 > nmaxtemp))
    			{
    				nmax2ndtemp = nmaxtemp;
    				nmaxtemp = FOD_FH[nHd][nZn];
    			}
    			if((ntemp1 != 0) && (ntemp1 < nmintemp))
    			{
    				nmin2ndtemp = nmintemp;
    				nmintemp = FOD_FH[nHd][nZn];
    			}
                if((ntemp0 != 0) && (nmax2ndtemp == 0) && (ntemp0 != nmaxtemp))
    			{
    				nmax2ndtemp = FOD_FH[nHd][nZn];
    			}                    
                if((ntemp1 != 0) && (nmin2ndtemp == 999999) && (ntemp1 != nmintemp))
    			{
    				nmin2ndtemp = FOD_FH[nHd][nZn];
    			}
    			if((ntemp0 != 0) && (ntemp0 > nmax2ndtemp) && (ntemp0 != nmaxtemp))
    			{
    				nmax2ndtemp = FOD_FH[nHd][nZn];
    			}
    			if((ntemp1 != 0) && (ntemp1 < nmin2ndtemp) && (ntemp1 != nmintemp))
    			{
    				nmin2ndtemp = FOD_FH[nHd][nZn];
    			}
    		}
            HDMAXFODFH[nHd] = nmaxtemp;
            HDMINFODFH[nHd] = nmintemp;
            HDMAX2NDFODFH[nHd] = nmax2ndtemp;
            HDMIN2NDFODFH[nHd] = nmin2ndtemp;
            
            sprintf(EXPLOG,"Results HD:%d MAXFODFH   : %d",nHd,nmaxtemp);
            userfilelog(EXPLOG);
            sprintf(EXPLOG,"Results HD:%d MINFODFH   : %d",nHd,nmintemp);
            userfilelog(EXPLOG);
            sprintf(EXPLOG,"Results HD:%d MAX2NDFODFH: %d",nHd,nmax2ndtemp);
            userfilelog(EXPLOG);
            sprintf(EXPLOG,"Results HD:%d MIN2NDFODFH: %d",nHd,nmin2ndtemp);
            userfilelog(EXPLOG);
    	}
    }
}

void CHK_DiskDefect_L3B(int pct)
{
    int nHd, nZn, def_max_tmp, def_max_zn, elog_max_tmp, elog_max_zn, dsum;
    float ftemp, ftemp1, ftemp2;
    char debug[64];
    memset(debug,0,0,64);
    
    def_max_tmp=0; def_max_zn=0; elog_max_tmp=0; elog_max_zn=0; dsum = 0;
    
    poststatus("DEC_Disk_L3B");
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        for(nZn=0; nZn<MAX_ZONE; nZn++)
        {
            if( def_max_tmp < TBL_DLIST[nHd][nZn] ) 
            {
                def_max_tmp = TBL_DLIST[nHd][nZn];
                def_max_zn = nZn;
            }
        }
        sprintf(EXPLOG,"DEC_Disk_L3B: HD[%d] MAX ZONE %d",nHd,def_max_zn);
        userfilelog(EXPLOG);
        for(nZn=0; nZn<MAX_ZONE; nZn++)
        {
            if( elog_max_tmp < TBL_ELOG[nHd][nZn] ) 
            {
                elog_max_tmp = TBL_ELOG[nHd][nZn];
                elog_max_zn = nZn;
            }
        }
        sputd(EXPLOG,"Elog_Disk_L3B: MAX ZONE %d",def_max_zn);
        userfilelog(EXPLOG);
        
        if(nZn == 0)
        { 
            dsum = dsum + TBL_DLIST[nHd][0];
            dsum = dsum + TBL_DLIST[nHd][1];
            dsum = dsum + TBL_DLIST[nHd][2];
            dsum = dsum + TBL_ELOG[nHd][0];
            dsum = dsum + TBL_ELOG[nHd][1];
            dsum = dsum + TBL_ELOG[nHd][2];
        }
        else if(nZn == MAX_ZONE-1)
        {
            dsum = dsum + TBL_DLIST[nHd][MAX_ZONE-3];
            dsum = dsum + TBL_DLIST[nHd][MAX_ZONE-2];
            dsum = dsum + TBL_DLIST[nHd][MAX_ZONE-1];
            dsum = dsum + TBL_ELOG[nHd][MAX_ZONE-3];
            dsum = dsum + TBL_ELOG[nHd][MAX_ZONE-2];
            dsum = dsum + TBL_ELOG[nHd][MAX_ZONE-1];
        }
        else
        {
            dsum = dsum + TBL_DLIST[nHd][def_max_zn-1];
            dsum = dsum + TBL_DLIST[nHd][def_max_zn];
            dsum = dsum + TBL_DLIST[nHd][def_max_zn+1];
            dsum = dsum + TBL_ELOG[nHd][elog_max_zn-1];
            dsum = dsum + TBL_ELOG[nHd][elog_max_zn];
            dsum = dsum + TBL_ELOG[nHd][elog_max_zn+1];
        }
        sprintf(EXPLOG,"DEC_Disk_L3B: HD[%d] TTL_DLISTandELOG %d",nHd,TTL_DLISTandELOG[nHd]);
        userfilelog(EXPLOG);
        sprintf(EXPLOG,"DEC_Disk_L3B: HD[%d] ElogDlis DFT SUM %d",nHd,dsum);
        userfilelog(EXPLOG);
        
        ftemp1 = TTL_DLISTandELOG[nHd];
    sprintf(EXPLOG,"DEC_Disk_L3B: ftemp1 %5.2f",ftemp1);
    userfilelog(EXPLOG);
        ftemp2 = dsum;
    sprintf(EXPLOG,"DEC_Disk_L3B: ftemp2 %5.2f",ftemp2);
    userfilelog(EXPLOG);
        if((ftemp1 != 0) && (ftemp2 != 0))
        {
            ftemp = ftemp2/ftemp1;
            ftemp = ftemp * 100;
            sprintf(EXPLOG,"DEC_Disk_L3B: ftemp %5.2f",ftemp);
            userfilelog(EXPLOG);
        }
        else
        {
            ftemp = 0;
        }
        if(dsum > 700)
        {
            if( ftemp > pct)
            {
                sprintf(EXPLOG,"DEC_Disk_L3B: DFT SUM %f > pct %d",ftemp, pct);
                userfilelog(EXPLOG);
                
                sputd(debug,"DEC_Disk_L3B: DISK %d",nHd);
                userfilelog(EXPLOG);
                BAD_DISK_3B[nHd]=CHAR_X; 
            }
        }
        def_max_tmp=0; def_max_zn=0; elog_max_tmp=0; elog_max_zn=0; dsum = 0;
    }
}

void CHK_DiskDetect(int thres)
{
    char cmd[32];
    char tbuf[512];
    int  cmdlen, offset;
    char head;
    int  i, j, headnum;
    
    offset=0; i=0; j=0;
    
    poststatus("CHK_DiskDetect");
    
    sprintf(cmd,"XK 7 %x",thres);
    strlen(cmd,cmdlen);

    ONERROR(dummyerror);
    str_usercmd(cmd, cmdlen, 30000, 256, 0, RDBUF, 128); 
    ONERRORCLEAR();

    while(RDBUF[i] != 0 && i<512)
    {
        if( RDBUF[i] == 0x0A)
        {
            strlen(tbuf,cmdlen);
            if(cmdlen > 18)
            {
                strfind(tbuf,"fail",offset);
                if(offset > 0)
                {
                    head = tbuf[offset+10];
                    headnum = head-0x30;
                    DEFCONTINOUS[headnum] = 1;
                    sprintf(EXPLOG,"Disk Defect : %d ",headnum);
                    userfilelog(EXPLOG);
                }
            }
            j=0;
        }
        else
        {
            tbuf[j] = RDBUF[i];
            j++;
        }
        i++;
    }
}

int CHK_HeadDetect(int type)
{
    int head, temp, ivalue, ntemp;
    char debug[64];
    memset(debug,0,0,64);
    
    ntemp=0;
    CHK_Head(type);
    
    if(MAX_HEAD != 2)
    {
        if(MAX_HEAD == 1)
        {
            for( head = 0; head < MAX_HEAD; head++)
            {
                temp = 0x00000001<<head;
                if( (HEAD_INFO & temp) == 0 )
                {
                    HDDETECT = 1;
                    sputd(EXPLOG,"CHK_Head : HD%d Fail",head);
                    userfilelog(EXPLOG);
                    sputd(debug,"HD%dFail",head);
                    poststatus(debug);
                    SetDecisionHead(head,HEAD_FAIL);
                    break;
                }
            }
        }
        else
        {
            poststatus("CHK_HeadDetect");
            for( head = 0; head < MAX_HEAD; head++)
            {
                temp = 0x00000001<<head;
                if( (HEAD_INFO & temp) == 0 )
                {
                    sputd(EXPLOG,"CHK_Head : HD%d Fail",head);        
                    userfilelog(EXPLOG);
                    sputd(debug,"HD%dFail",head);
                    poststatus(debug);
                    SetDecisionHead(head,HEAD_FAIL);
                }
                ivalue = RDY_MRR[head];
                if((ivalue != 0) && (ivalue < 100))
                {
                    sprintf(EXPLOG,"HD %d MRR:%d Wrong",head,ivalue);
                    poststatus(EXPLOG);
                    sputd(debug,"HD%dFail",head);
                    poststatus(debug);
                    SetDecisionHead(head,HEAD_FAIL);
                }
            }
        }
    }
    else
    {
        poststatus("CHK_HeadDetect");
        for( head = 0; head < MAX_HEAD; head++)
        {
            temp = 0x00000001<<head;
            if((HEAD_INFO & temp) == 0)
            {
                ntemp++;
                sputd(EXPLOG,"CHK_Head : HD%d Fail",head);        
                userfilelog(EXPLOG);
                sputd(debug,"HD%dFail",head);
                poststatus(debug);
                SetDecisionHead(head,HEAD_FAIL);
            }
            ivalue = RDY_MRR[head];
            if((ivalue != 0) && (ivalue < 100))
            {
                sprintf(EXPLOG,"HD %d MRR:%d Wrong",head,ivalue);
                poststatus(EXPLOG);
                sputd(debug,"HD%dFail",head);
                poststatus(debug);
                SetDecisionHead(head,HEAD_FAIL);
            }
        }
    }
    for(head=0; head<MAX_HEAD; head++)
    {
        temp = GetDecisionHead(head);
        if(temp != 0)
        {
            return 1;
        }
    }
    if(ntemp==MAX_HEAD)
    {
        sprintf(EXPLOG,"ntemp : %d",ntemp);
        userfilelog(EXPLOG);
        userfilelog("OOB[all_MRRFail]");
        
        str_exp_setdectype(1, 0);
        poststatus("OOB[all_MRRFail]");
        stoptest();
    }
    userfilelog("CHK_Head : HD All Pass");
    poststatus("HD All PASS");
    return 0;
}

void CHK_Head(int flag)
{
    int  find, nHd, cmdlen;
    char hdstatus;
    int  ivalue;
    char head[4];
    char mrr[8];
    char fodres[8];
    char wrres[8];
    char phd[8];
    char cmd[32];
    
    if(flag!=3)
    {
        poststatus("CHK_Head");
        userfilelog("park head");
        ONERROR(reseterror);
        reset();
        ONERRORCLEAR();
        str_checkprompt("ENG>");
        str_sendserialcmd("PK",2);
        delays(2);
        ONERROR(pkerror);
        str_checkprompt("ENG>");
        ONERRORCLEAR();
    }
    else //DBG mode
    {
        poststatus("CHK_Head");
        userfilelog("park head");
        ONERROR(reseterror);
        reset();
        ONERRORCLEAR();
        str_checkprompt("DBG>");
        str_sendserialcmd("PK",2);
        delays(2);
        ONERROR(pkerror);
        str_checkprompt("DBG>");
        ONERRORCLEAR();
    }
    /*
    sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    */
    ONERROR(dummyerror);
    //str_usercmd("XH", 2, 3000, 512, 0, RDBUF, 512);     //PHD
    str_usercmd("XH", 2, 10000, 100, 0, RDBUF, 512);
    ONERRORCLEAR();

    strcpy(phd,"PHD=");
    strfind(RDBUF,phd,find);
    if(find>0)
    {
        memset(phd,0,0,4);
        memnmov(phd,RDBUF,0,find+4,1,0);
        atoi(phd,0,10,ivalue);
        MAX_HEAD = ivalue;
        sputd(EXPLOG,"MAX_HEAD:%d",MAX_HEAD);
        poststatus(EXPLOG);
        userfilelog(EXPLOG);
    }
    else
    {
        //poststatus("CHK HD_Fail");
        poststatus("OOB");
        userfilelog("CHK HD_Fail");
        stoptest();
    }
    
    if(DEF_MAX_HD < MAX_HEAD)
    {
        poststatus("MODL");
        userfilelog("Model Error");
        stoptest();
    }
    reset();    //buffer clear
    HEAD_INFO = 0x00000000;
    ONERROR(dummyerror);
    //str_usercmd("XH", 2, 3000, 4+76*MAX_HEAD, 0, RDBUF, 512);    //456 : XH + CR + LF + (ÇÑÁÙ±æÀÌ+CR+LF) * HDnum
    str_usercmd("XH", 2, 3000, 139, 0, RDBUF, 512);
    ONERRORCLEAR();
    
    if((flag==0) || (flag==3))      //SC Fail
    {
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            sputd(head,"H%d:",nHd);
            strfind(RDBUF,head,find);
            if(find>0)
            {
                //sputd(EXPLOG,"H%d find",nHd);
                //userfilelog(EXPLOG);
                memset(mrr,0,0,8);
                memnmov(mrr,RDBUF,0,find+33,7,0);
                atoi(mrr,0,16,ivalue);
                RDY_MRR[nHd]=ivalue;
                //sputs(EXPLOG,"RDBUF[find+32]=%s",RDBUF[find+32]);
                //userfilelog(EXPLOG);
                if(RDBUF[find+27] == CHAR_G)    //find Good
                {
                    HEAD_INFO = (HEAD_INFO|(0x00000001<<nHd));
                    sprintf(EXPLOG,"CHK_HEAD FIND HD%d MRR:%x",nHd, ivalue);
                    userfilelog(EXPLOG);
                }
                if(RDBUF[find+27] == CHAR_B)    //find Fail
                {
                    sprintf(EXPLOG,"CHK_HEAD NOT FIND HD%d MRR:%x",nHd, ivalue);
                    userfilelog(EXPLOG);
                }
                else    //No find character
                {
                    HEAD_INFO = (HEAD_INFO|(0x00000001<<nHd));
                    sputd(EXPLOG,"CHK_HEAD nHd[%d] Character not found",nHd);
                    userfilelog(EXPLOG);
                }
            }
            else
            {
                //poststatus("X");
                break;
            }
        }
        reset();
        return;
    }
    else        //BI Fail
    {
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            sputd(head,"H%d:",nHd);
            strfind(RDBUF,head,find);
            if(find>0)
            {
                sputd(EXPLOG,"H%d find",nHd);
                userfilelog(EXPLOG);
                memset(mrr,0,0,8);
                memset(fodres,0,0,8);
                memset(wrres,0,0,8);
                //MRR
                memnmov(mrr,RDBUF,0,find+40,8,0);
                //sputs(EXPLOG,"mrr:%s",mrr);
                //userfilelog(EXPLOG);
                atoi(mrr,0,16,ivalue);
                RDY_MRR[nHd]=ivalue;
                //FODRES
                memnmov(fodres,RDBUF,0,find+11,4,0);
                //sputs(EXPLOG,"fodres:%s",fodres);
                //userfilelog(EXPLOG);
                atoi(fodres,0,16,ivalue);
                RDY_FODRES[nHd]=ivalue;
                //WRRES
                memnmov(wrres,RDBUF,0,find+30,4,0);
                //sputs(EXPLOG,"wrres:%s",wrres);
                //userfilelog(EXPLOG);
                atoi(wrres,0,16,ivalue);
                RDY_WRRES[nHd]=ivalue;
                
                sprintf(EXPLOG,"mrr:%x, fodres:%x, wrres:%x",RDY_MRR[nHd],RDY_FODRES[nHd],RDY_WRRES[nHd]);
                userfilelog(EXPLOG);
                if(RDBUF[find+35] == CHAR_G)    //find Good
                {
                    HEAD_INFO = (HEAD_INFO|(0x00000001<<nHd));
                    sprintf(EXPLOG,"CHK_HEAD FIND HD%d MRR:%x",nHd, ivalue);
                    userfilelog(EXPLOG);
                }
                if(RDBUF[find+35] == CHAR_B)    //find Fail
                {
                    sprintf(EXPLOG,"CHK_HEAD NOT FIND HD%d MRR:%x",nHd, ivalue);
                    userfilelog(EXPLOG);
                }
                else    //No find character
                {
                    HEAD_INFO = (HEAD_INFO|(0x00000001<<nHd));
                    sputd(EXPLOG,"CHK_HEAD nHd[%d] Character not found",nHd);
                    userfilelog(EXPLOG);
                }
            }
            else
            {
                //poststatus("X");
                break;
            }
        }
        reset();
        return;
    }
    return;
}

void CHK_TrkSrvTargetHDandMaxRatio()    //int TRKSRV_MAXHDRATE, TRKSRV_MAXDEFTARGETHD, TRKSRV_TTLDEFCNT
{
	int nHd,nZn,nCnt,ntemp,ntemp0,ntype,nSkipHd;
	int nMaxTrkSrvDef;
	
	int TTLTrkSrvDef[6];

	for(nCnt=0; nCnt<6; nCnt++)
	{
		TTLTrkSrvDef[nCnt] = 0;
	}

	ntype = 0;
	if((BI_ERRORCODE == 0x2812) || (BI_ERRORCODE == 0x2811))
	{
		ntype = 1;
	}
	CHK_GetTrackandServoDefect(ntype);	//int TRKSRVCNT[150]; // 6*25
	
	TRKSRV_TTLDEFCNT = 1;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		ntemp=0; ntemp0=0;
		for(nZn=0; nZn<MAX_ZONE; nZn++)
		{
			ntemp0 = TRKSRVCNT[nHd][nZn];
			ntemp = ntemp + ntemp0;
			TTLTrkSrvDef[nHd] = ntemp;
		}
		TRKSRV_TTLDEFCNT = TRKSRV_TTLDEFCNT + ntemp;
		sprintf(EXPLOG,"HD:%d TRKSRV_TTLDEFCNT:%d",nHd,ntemp);
        userfilelog(EXPLOG);
		sprintf(EXPLOG,"TRKSRV_TTLDEFCNT:%d",TRKSRV_TTLDEFCNT);
        userfilelog(EXPLOG);
	}

	nMaxTrkSrvDef=0;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		ntemp0 = TTLTrkSrvDef[nHd];
		if((ntemp0 != 0) && (ntemp0 > nMaxTrkSrvDef))
		{
			nMaxTrkSrvDef = TTLTrkSrvDef[nHd];
			TRKSRV_MAXDEFTARGETHD = nHd;
			sprintf(EXPLOG,"HD:%d nMaxTrkSrvDef:%d",TRKSRV_MAXDEFTARGETHD,nMaxTrkSrvDef);
            userfilelog(EXPLOG);
		}
	}
	////// Check ZONESKIP
    ntemp = 0;
	for(nHd=0; nHd<MAX_HEAD; nHd++)
	{
		if(ZONESKIP[nHd] == 0)
		{
            if(ZONEUSED[nHd] != 0)
            {
                sprintf(EXPLOG,"TrkSrv ZONESKIP %x and ZONEUSED %x deteced -> HD%d Zone Skip",ZONESKIP[nHd],ZONEUSED[nHd],nHd);
                userfilelog(EXPLOG);
                TRKSRV_TTLDEFCNT=TRKSRV_TTLDEFCNT+nMaxTrkSrvDef;
                nSkipHd = nHd;
                ntemp++;
            }
		}
		else if(ZONESKIP[nHd] != 0x1FFFFFE)
		{
            sprintf(EXPLOG,"TrkSrv ZONESKIP %x and ZONEUSED %x deteced -> HD%d Zone Skip",ZONESKIP[nHd],ZONEUSED[nHd],nHd);
            userfilelog(EXPLOG);
            TRKSRV_TTLDEFCNT=TRKSRV_TTLDEFCNT+nMaxTrkSrvDef;
            nSkipHd = nHd;
            ntemp++;
		}		
	}
	//////
    sprintf(EXPLOG,"ZONESKIP : %d",ntemp);
    userfilelog(EXPLOG);
	TRKSRV_MAXHDRATE = (nMaxTrkSrvDef * 1000 / TRKSRV_TTLDEFCNT);
    if(ntemp == 1)
    {
        TRKSRV_MAXDEFTARGETHD = nSkipHd;
        TRKSRV_MAXHDRATE = 500;
        /*
        if(ZONESKIP[5] != 0)
        {
            TRKSRV_MAXDEFTARGETHD = 5;
            TRKSRV_MAXHDRATE = 500;
        }
        */
    }
    if(ntemp > 1)
    {
        TRKSRV_MAXDEFTARGETHD = 5;
        TRKSRV_MAXHDRATE = 10;
    }
	sprintf(EXPLOG,"HD:%d nMaxTrkSrvDef:%d * 1000 / TRKSRV_TTLDEFCNT:%d",TRKSRV_MAXDEFTARGETHD,nMaxTrkSrvDef,TRKSRV_TTLDEFCNT);
    userfilelog(EXPLOG);
	sprintf(EXPLOG,"HD:%d TRKSRV_MAXHDRATE:%d",TRKSRV_MAXDEFTARGETHD,TRKSRV_MAXHDRATE);
    userfilelog(EXPLOG);
    return;
}

void CHK_GetTrackandServoDefect(int ntype)
{
	int nCnt,nNode,nOffs,nNum,nBufOffs;
	int nCyl,nHd,nZn,nSkew,nFlag,ntemp,ntemp0,ntemp2;

    poststatus("CHK_GetTrackandServoDefect");
	ntemp = 0; ntemp0 = 0; ntemp2 = 0;
    
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        for(nZn=0; nZn<MAX_ZONE; nZn++)
        {
            TRKSRVCNT[nHd][nZn]=0;
        }
        SRVCONTINOUS[nHd]=0; 
    }
		
    //memset(CIRCLE,0,0,MAX_HEAD*2000*4);

    if( MGET(15,0) !=0 )
    {
        poststatus("VLIST");
        return;
    }
    nNode = RDBUF[0x24]&0xff;
    nNode = nNode | ((RDBUF[0x24+1] & 0xff) << 8);
    nNode = nNode | ((RDBUF[0x24+2] & 0xff) <<16);
    nNode = nNode | ((RDBUF[0x24+3] & 0xff) <<24);
    
    if(nNode!=0)
    {
        nOffs = 0;
        for(nCnt=0; nCnt<nNode; nCnt++)
        {
            //read
            if(nOffs%512 == 0)
            {
                if( MGET(15,nOffs+0x368) !=0 )
                {
                    poststatus("VLIST");
                    return;
                }
                nBufOffs = 0;
            }
            
            nCyl = RDBUF[nBufOffs] & 0xff;
            nCyl = nCyl | ((RDBUF[nBufOffs+1] & 0xff) << 8);
            nCyl = nCyl | ((RDBUF[nBufOffs+2] & 0xff) <<16);
            nCyl = nCyl | ((RDBUF[nBufOffs+3] & 0xff) <<24);
            nHd   = RDBUF[nBufOffs+4] & 0xff;
            nSkew = RDBUF[nBufOffs+5] & 0xff;
            nFlag = RDBUF[nBufOffs+6] & 0xff;
            
            nZn = GetNumOfZone(nCyl,nHd);
            
            if(nFlag != 0x0C)
            {
                TRKSRVCNT[nHd][nZn] = TRKSRVCNT[nHd][nZn] + 1;
                
                //ContinousCheck(int nHd,int nCyl,int nSN,int DCyl,int DSN,int nThres);
                ntemp0 = SRVCONTINOUS[nHd];
                if(ntemp0==0 && nSkew > 5)
                {
                    //printf("nHd:%d nCyl:%d nSkew:%d",nHd,nCyl,nSkew);
                    ntemp = ContinousCheck(nHd,nCyl,nSkew,5,2,50);
                    if(ntemp != 0)
                    {
                        SRVCONTINOUS[nHd] = ntemp;	
                        sprintf(EXPLOG,"ContinousCheck detect : Hd:%d Cyl:%d",nHd,ntemp);
                        userfilelog(EXPLOG);
                    }
                }
                ntemp2=TRKSRVCNT[nHd][nZn];
                //sprintf(EXPLOG,"nHd:%d Cyl:%d nZn:%d ntemp:%d Vlist%d",nHd,nCyl,nZn,ntemp,ntemp2);
                //userfilelog(EXPLOG);
            }
            nBufOffs=nBufOffs+8;
            nOffs=nOffs+8;
        }
    }
    if(ntype != 0)		//0x2812 Fail is only TLIST
    {
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            for(nZn=0; nZn < MAX_ZONE; nZn++)
                TRKSRVCNT[nHd][nZn] = 0;
        }
    }
// read TList
	if( MGET(18,0) != 0 )
    {
        poststatus("TLIST");
        return;
    }

    nNode = RDBUF[0x20] & 0xff;
    nNode = nNode | ((RDBUF[0x20+1] & 0xff) << 8);
    nNode = nNode | ((RDBUF[0x20+2] & 0xff) <<16);
    nNode = nNode | ((RDBUF[0x20+3] & 0xff) <<24);
    //printf("TLIST nNode:%d",nNode);
    if(nNode == 0) return;

    nOffs = 0;
    for(nCnt=0; nCnt<nNode; nCnt++)
    {
        //read
        if(nOffs % 512 == 0)
        {
            if( MGET(18,nOffs+0x30) !=0 )
            {
                poststatus("TLIST");
                return;
            }
            nBufOffs = 0;
        }
        
        nCyl = RDBUF[nBufOffs] & 0xff;
        nCyl = nCyl | ((RDBUF[nBufOffs+1] & 0xff) << 8);
        nCyl = nCyl | ((RDBUF[nBufOffs+2] & 0xff) <<16);
        nCyl = nCyl | ((RDBUF[nBufOffs+3] & 0xff) <<24);
        nHd = RDBUF[nBufOffs+5] & 0xff;
        
        nZn = GetNumOfZone(nCyl,nHd);
        TRKSRVCNT[nHd][nZn] = TRKSRVCNT[nHd][nZn] + 1;
        
        ntemp2=TRKSRVCNT[nHd][nZn];
        //sprintf(EXPLOG,"nHd:%d Cyl:%d nZn:%d Tlist%d",nHd,nCyl,nZn,ntemp2);
        //userfilelog(EXPLOG);
        
        nBufOffs=nBufOffs+8;
        nOffs=nOffs+8;
    }
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        for(nZn=0; nZn<MAX_ZONE; nZn++)
        {
            sprintf(EXPLOG,"TRKSRVCNT[%d][%d] Tlist:%d",nHd,nZn,TRKSRVCNT[nHd][nZn]);
            userfilelog(EXPLOG);
        }
    }
    return;
}

int ContinousCheck(int nHd, int nCyl, int nSN, int DCyl, int DSN, int nThres)
{
	//ContCyl=0; ContSec=0; ContCnt=0;
	int i,temp,temp1,nCont;
	int MaxCyl,SSN,ESN;

	MaxCyl=0;nCont=0;
	if(nSN<DSN)	{ SSN=0; }
	else {	SSN=nSN-DSN; }	
	if((DSN+nSN)>2000) { ESN=2000; }
	else { ESN=nSN+DSN; }

	for(i=SSN;i<=ESN;i++)
	{
		temp=GetCylMark(nHd,i);
		
		if(MaxCyl<=temp && temp!=nCyl) 
		{
			temp1=GetCntMark(nHd,i)+1;
			if(temp1 > nCont) nCont=temp1;
			MaxCyl=temp;
		}
	}

	if((nCyl-MaxCyl)<=(DCyl+1) && MaxCyl!=0)
	{
		if(temp1>=nThres) return nCyl;
		SetCircleMark(nHd,nSN,nCyl,nCont);		
	}
	else
	{		
		SetCircleMark(nHd,nSN,nCyl,1);
	}
	return 0;
}

int GetCylMark(int nHd,int nNum)
{
	int temp;
	temp=CIRCLE[nHd][nNum] & 0xFFFFF;
	return temp;
}
int GetCntMark(int nHd,int nNum)
{
	int temp;
	temp=CIRCLE[nHd][nNum]>> 20;
	return temp;
}
void SetCircleMark(int nHd,int nNum,int nCyl,int nCnt)
{
	int temp;
	temp=nCyl&0xFFFFF;
	temp=temp |(nCnt<<20);
	CIRCLE[nHd][nNum]=temp;
}

int CHK_MESHistory(int ntype, int module)
{
    int  nRet;
    char cmd[32];
    
    //str_mesgetexpertinfo(MES_EXPERT_INFO);
    //MES History
    /*
    printf("MES_STW_COUNT: %d"  , MES_STW_COUNT   );  atoi
    printf("MES_BI_COUNT: %d"   , MES_BI_COUNT    );  atoi
    printf("MES_PCBA_USED_COUNT: %d", MES_PCBA_USED_COUNT );  atoi
    printf("MES_PREV_ERROR: %d" , MES_PREV_ERROR  );  4byet char
    printf("MES_RWK_FLAG: %d", MES_RWK_FLAG );  P or D
    printf("MES_REASSY_COUNT: %d", MES_REASSY_COUNT );  atoi
    printf("MES_DISK_DMR: %d" , MES_DISK_DMR  );  P or D
    printf("MES_DISK_DMR: %d" , MES_HEAD_DMR  );  P or D

    MES_EXPERT_INFO.MES_STW_COUNT;
    MES_EXPERT_INFO.MES_PCBA_USED_COUNT
    MES_EXPERT_INFO.MES_BI_COUNT
    MES_EXPERT_INFO.MES_REASSY_COUNT
    MES_EXPERT_INFO.MES_DISK_DMR
    MES_EXPERT_INFO.MES_HEAD_DMR
    MES_EXPERT_INFO.MES_DEBUG
    MES_EXPERT_INFO.MES_RWK_FLAG
    MES_EXPERT_INFO.MES_MCODE
    MES_EXPERT_INFO.MES_PREV_ERROR
    
    str_exp_setdectype(int group, int subgroup);
    OOB         1
    Kill        2
        HD_KILL     1
        HS_KILL     2
        DK_KILL     3
        HD_DK_KILL  4
        HS_DK_KILL  5
        ETC         6
    B/I         3
        ReSC        1
        1stB/I      2
        2ndB/I      3
        3rdB/I      4
        ETC         5
    Down        4
        1stD/S      1
        2ndD/S      2
        3rdD/S      3
        ETC         4
    RPMDROP     5
        1stR/D      1
        2ndR/D      2
        3rdR/D      3
        ETC         4
    REWORK      6
        Head        1
        Disk        2
        HdSwap      3
        DskSwap     4
        HdTest      5
        Cover       6
        VCM         7
        ETC         8
    STW         7
        ErsrSTW     1
        DownSTW     2
        STRSTW      3
        PSTW        4
        STW         5
        ETC         6
    ETC         8
        PCBA        1
        PSwap       2
        ETC         3


//1st Main Script 
* Main script 
    {
        str_starttest();
        ONTESTERROR(Mass);
        str_monitorbi(10000);
        ONTESTERRORCLEAR();
    }
    Mass()
    {
        //if test is fail, stop the test
        if( passfail == CHAR_F )
        {
            str_exp_runexp();
            stoptest();
        }
    }

//EXPERT    -retest
    #define ESPECTYPE_SBT 0x01 //!<SC->BI
    #define ESPECTYPE_HBT 0x02 //!<HT->BI
    #define ESPECTYPE_SCR 0x04 //!<SCR(FT)
    #define ESPECTYPE_DCO 0x08 //!<DCO Setting(include S/N Write)
    #define ESPECTYPE_EXP 0x10 //!<EXPERT
    #define ESPECTYPE_SB1 0x20 //!<SC->BI(SC-> partial BI room temp)
    #define ESPECTYPE_HB1 0x40 //!<HT->BI(HT-> partial BI room temp)
    #define ESPECTYPE_BF1 0x80 //!<HT->BI(partial BI high temp)
    str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);
    

//XP command : XP espec_type
    - espec_type
    #define GLOBAL_EXP_BI_TYPE_HT_1             1   // IN-LINE EXPERT : HT Servo cal
    #define GLOBAL_EXP_BI_TYPE_HT_2             2   // IN-LINE EXPERT : HT W/R
    #define GLOBAL_EXP_BI_TYPE_BI_1             3   // IN-LINE EXPERT : BI Servo Cal
    #define GLOBAL_EXP_BI_TYPE_BI_2             4   // IN-LINE EXPERT : BI OSC WII
    #define GLOBAL_EXP_BI_TYPE_BI_3             5   // IN-LINE EXPERT : BI MR TURN
    #define GLOBAL_EXP_BI_TYPE_BI_4             6   // IN-LINE EXPERT : BI TPI
    #define GLOBAL_EXP_BI_TYPE_BI_5             7   // IN-LINE EXPERT : BI BER Measurement
    #define GLOBAL_EXP_BI_TYPE_BI_6             8   // IN-LINE EXPERT : BI Scan gray
    #define GLOBAL_EXP_BI_TYPE_BI_7             9   // IN-LINE EXPERT : BI W/R1
    #define GLOBAL_EXP_BI_TYPE_BI_8             10  // IN-LINE EXPERT : BI W/R2
    #define GLOBAL_EXP_BI_TYPE_BI_9             11  // IN-LINE EXPERT : BI ToneFill
    #define GLOBAL_EXP_BI_TYPE_BI_10            12  // IN-LINE EXPERT : BI FOD & W/R3
    
//str_exp_startcmd(char startcmd)    //Main Script¿¡¼­ StartÇÒ Serial CMD¸¦ °áÁ¤
    
*/

    int nCnt;
    
    sputd(EXPLOG,"CHK_MESHistory %d",ntype);
    userfilelog(EXPLOG);

    sputd(EXPLOG,"CHK_MESHistory MES_STW_COUNT:%d",MES_EXPERT_INFO.MES_STW_COUNT);
    userfilelog(EXPLOG);
    sputd(EXPLOG,"CHK_MESHistory MES_BI_COUNT:%d",MES_EXPERT_INFO.MES_BI_COUNT);
    userfilelog(EXPLOG);
    

    if(ntype == 1)  //MES_STW_COUNT
    {
        /*
        str_exp_setdectype(7, 6);
        poststatus("Port Screen");
        strcpy(REPAIRCODE,"T10005");	//Screen
        Send_Debug_info();
        stoptest();
        */
        if(MES_EXPERT_INFO.MES_STW_COUNT <= 2)
        {
            str_exp_setdectype(7, 3);
            poststatus("STW:DSTW");
            strcpy(REPAIRCODE,"T10002");    //"STRSTW" + DECISION_STW
            Send_Debug_info();        
            stoptest();
        }
        else
        {
            SetDecisionHeadSwap();
            userfilelog("L1208 Misalign : HeadSwap");
            stoptest();
        }
    }
    
    if(ntype == 2)  //MES_BI_COUNT -> ReS/C
    {
        if(MES_EXPERT_INFO.MES_BI_COUNT <= 2)
        {
            nRet = DOWNLOAD_EXPSCCODE(60,1);
            /*
            if(nRet != 0)
            {
                str_exp_setdectype(1, 0);
                poststatus("SC D/LFail");
                userfilelog("SC Code D/L Fail");
                stoptest();
            }
            */
            str_exp_setdectype(3, 1);
            poststatus("320G SBT");
            strcpy(REPAIRCODE,"T00004");    //"ReSC" + DECISION_SC
            Send_Debug_info();
            Restore_MESmodel();
            MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],0);     //0:ORG, 1:DS, 2:DDS
            //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x01);    //#define ESPECTYPE_SBT 0x01 //!<SC->BI
            //sputd(cmd,"XP %d",module);
            //poststatus(cmd);
            //str_exp_startcmd(cmd); 
            stoptest();
        }
        else
        {
            /*
            str_exp_setdectype(7, 6);
            poststatus("Port Screen");
            strcpy(REPAIRCODE,"T10005");	//Screen
            Send_Debug_info();
            stoptest();
            */
            if(MES_EXPERT_INFO.MES_STW_COUNT <= 2)
            {
                str_exp_setdectype(7, 3);
                poststatus("STW:DSTW");
                strcpy(REPAIRCODE,"T10002");    //"STRSTW" + DECISION_STW
                Send_Debug_info();        
                stoptest();
            }
            else
            {
                SetDecisionHeadSwap();
                userfilelog("L1208 Misalign : HeadSwap");
                stoptest();
            }
        }
    }
    if(ntype == 3)  //MES_BI_COUNT -> ReB/I
    {
        nRet = DOWNLOAD_EXPSCCODE(60,1);
        /*
        if(nRet != 0)
        {
            str_exp_setdectype(1, 0);
            poststatus("SC D/LFail");
            userfilelog("SC Code D/L Fail");
            stoptest();
        }
        */
        if(MES_EXPERT_INFO.MES_BI_COUNT <= 2)
        {
            if( (MAX_HEAD == 2) && (CAPA == 32) )
            {
                str_exp_setdectype(3, 3);
                poststatus("ReBI_320G");
                strcpy(REPAIRCODE,"T00005");    //"2ndB/I" + DECISION_BI
                Send_Debug_info();
                Restore_MESmodel();
                MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],0);     //0:ORG, 1:DS, 2:DDS
                //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);    //#define ESPECTYPE_HBT 0x02 //!<HT->BI
                //sputd(cmd,"XP %d",module);
                //poststatus(cmd);
                //str_exp_startcmd(cmd); 
                stoptest();
            }
            if( (MAX_HEAD == 2) && (CAPA == 25) )
            {
                str_exp_setdectype(3, 3);
                poststatus("ReBI_500G");
                strcpy(REPAIRCODE,"T00005");    //"2ndB/I" + DECISION_BI
                Send_Debug_info();
                Restore_MESmodel();
                MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],1);     //0:ORG, 1:DS, 2:DDS
                //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);    //#define ESPECTYPE_HBT 0x02 //!<HT->BI
                //sputd(cmd,"XP %d",module);
                //poststatus(cmd);
                //str_exp_startcmd(cmd);
                stoptest();
            }
            if( (MAX_HEAD == 6) && (CAPA == 64) )
            {
                str_exp_setdectype(3, 4);
                poststatus("ReBI_640G");
                strcpy(REPAIRCODE,"T00005");    //"3rdB/I" + DECISION_BI
                Send_Debug_info();
                Restore_MESmodel();
                MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],2);     //0:ORG, 1:DS, 2:DDS
                //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);    //#define ESPECTYPE_HBT 0x02 //!<HT->BI
                //sputd(cmd,"XP %d",module);
                //poststatus(cmd);
                //str_exp_startcmd(cmd); 
                stoptest();
            }
            else
            {
                str_exp_setdectype(1, 0);
                poststatus("OOB");
                userfilelog("CHK_MESHistory Model Error");
                stoptest();
            }
        }
        else
        {
            if( (MAX_HEAD == 2) && (CAPA == 32) )
            {
                str_exp_setdectype(3, 3);
                poststatus("ReBI_250G");
                strcpy(REPAIRCODE,"T00005");    //"2ndB/I" + DECISION_BI
                Send_Debug_info();
                Restore_MESmodel();
                MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],1);     //0:ORG, 1:DS, 2:DDS
                //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);    //#define ESPECTYPE_HBT 0x02 //!<HT->BI
                //sputd(cmd,"XP %d",module);
                //poststatus(cmd);
                //str_exp_startcmd(cmd); 
                stoptest();
            }
            if( (MAX_HEAD == 6) && (CAPA == 75) )
            {
                str_exp_setdectype(3, 3);
                poststatus("ReBI_640G");
                strcpy(REPAIRCODE,"T00005");    //"2ndB/I" + DECISION_BI
                Send_Debug_info();
                Restore_MESmodel();
                MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],2);     //0:ORG, 1:DS, 2:DDS
                //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);    //#define ESPECTYPE_HBT 0x02 //!<HT->BI
                //sputd(cmd,"XP %d",module);
                //poststatus(cmd);
                //str_exp_startcmd(cmd); 
                stoptest();
            }
            /*
            if(MES_EXPERT_INFO.MES_STW_COUNT <= 2)
            {
                str_exp_setdectype(7, 3);
                poststatus("STW:DSTW");
                strcpy(REPAIRCODE,"T10002");    //"STRSTW" + DECISION_STW
                Send_Debug_info();        
                stoptest();
            }
            */
            else
            {
            SetDecisionHeadSwap();
            stoptest();
            }
        }
    }
    if(ntype == 4)  //MES_BI_COUNT -> ReD/S
    {
        nRet = DOWNLOAD_EXPSCCODE(60,1);
        /*
        if(nRet != 0)
        {
            str_exp_setdectype(1, 0);
            poststatus("SC D/LFail");
            userfilelog("SC Code D/L Fail");
            stoptest();
        }
        */
        if(MES_EXPERT_INFO.MES_BI_COUNT <= 1)
        {
            if( (MAX_HEAD == 2) && (CAPA == 32) )
            {
                str_exp_setdectype(3, 3);
                poststatus("250G HBT");
                strcpy(REPAIRCODE,"T00005");    //"2ndB/I" + DECISION_BI
                Send_Debug_info();
                Restore_MESmodel();
                MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],1);     //0:ORG, 1:DS, 2:DDS
                //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);    //#define ESPECTYPE_HBT 0x02 //!<HT->BI
                //sputd(cmd,"XP %d",module);
                //poststatus(cmd);
                //str_exp_startcmd(cmd); 
                stoptest();
            }
            if( (MAX_HEAD == 2) && (CAPA == 25) )
            {
                str_exp_setdectype(3, 3);
                poststatus("250G HBT");
                strcpy(REPAIRCODE,"T00005");    //"3rdB/I" + DECISION_BI
                Send_Debug_info();
                Restore_MESmodel();
                MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],2);     //0:ORG, 1:DS, 2:DDS
                //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);    //#define ESPECTYPE_HBT 0x02 //!<HT->BI
                //sputd(cmd,"XP %d",module);
                //poststatus(cmd);
                //str_exp_startcmd(cmd); 
                stoptest();
            }
            else
            {
                str_exp_setdectype(1, 0);
                poststatus("MODL");
                stoptest();
            }
        }
        else
        {
            if( (MAX_HEAD == 2) && (CAPA == 32) )
            {
                str_exp_setdectype(3, 3);
                poststatus("ReBI_250G");
                strcpy(REPAIRCODE,"T00005");    //"2ndB/I" + DECISION_BI
                Send_Debug_info();
                Restore_MESmodel();
                MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],1);     //0:ORG, 1:DS, 2:DDS
                //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);    //#define ESPECTYPE_HBT 0x02 //!<HT->BI
                //sputd(cmd,"XP %d",module);
                //poststatus(cmd);
                //str_exp_startcmd(cmd); 
                stoptest();
            }
            if( (MAX_HEAD == 2) && (CAPA == 25) )
            {
                str_exp_setdectype(3, 3);
                poststatus("250G HBT");
                strcpy(REPAIRCODE,"T00005");    //"3rdB/I" + DECISION_BI
                Send_Debug_info();
                Restore_MESmodel();
                MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],2);     //0:ORG, 1:DS, 2:DDS
                //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);    //#define ESPECTYPE_HBT 0x02 //!<HT->BI
                //sputd(cmd,"XP %d",module);
                //poststatus(cmd);
                //str_exp_startcmd(cmd); 
                stoptest();
            }
            /*
            if(MES_EXPERT_INFO.MES_STW_COUNT <= 2)
            {
                str_exp_setdectype(7, 3);
                poststatus("STW:DSTW");
                strcpy(REPAIRCODE,"T10002");    //"STRSTW" + DECISION_STW
                Send_Debug_info();        
                stoptest();
 
            }
            */
            else
            {
                SetDecisionHeadSwap();
                userfilelog("L1208 Misalign : HeadSwap");
                stoptest();
            }
        }
    }
    if(ntype == 5 || ntype ==6) //5:DDMR, 6:HDMR, "P", "D"
    {
        if(MES_EXPERT_INFO.MES_DISK_DMR == CHAR_D || MES_EXPERT_INFO.MES_HEAD_DMR == CHAR_D)
        {
            return 1;
        }
    }
    return 0;
}

void ExpertDecision(int type)
{
	int  nCnt,nRet,nHd,nDsk;
	int  nFailCnt,nType,nKill; 
	int  nHdFailCnt,nDskFailCnt;
    char debugcode[16];
    char cmd[32];
    char debug[64];
    
    memset(debugcode,0,0,16);
    memset(cmd,0,0,32);
    memset(debug,0,0,64);

    poststatus("ExpertDecision");
	userfilelog("Start ExpertDecision");
    
	nFailCnt = nDskFailCnt = nHdFailCnt = 0;
	for(nCnt=0; nCnt<MAX_HEAD; nCnt++)
	{
		nRet=GetDecisionHead(nCnt);
		if(nRet != 0)
		{
		    nHdFailCnt++;
		    nHd = nCnt;
		}
		nRet=GetDecisionDisk(nCnt);
		if(nRet != 0)
		{
		    nDskFailCnt++;
		    nDsk = nCnt;
		}
	}
	nFailCnt = nDskFailCnt + nHdFailCnt;
	nKill = 1;
    if(type==0)
    {
        if((nHdFailCnt > 3) && (MAX_HEAD == 6))
        {
            SetDecisionHeadTest();
            stoptest();
        }
        if((nHdFailCnt > 3) && (MAX_HEAD == 4))
        {
            SetDecisionHeadTest();
            stoptest();
        }
        if((nHdFailCnt > 2) && (MAX_HEAD == 2))
        {
            SetDecisionHeadTest();
            stoptest();
        }
    }
    if(type==1)
    {
        if((nHdFailCnt >= 4) && (MAX_HEAD == 6))
        {
            SetDecisionHeadTest();
            stoptest();
        }
        if((nHdFailCnt >= 4) && (MAX_HEAD == 5))
        {
            SetDecisionHeadTest();
            stoptest();
        }
        if((nHdFailCnt >= 3) && (MAX_HEAD == 4))
        {
            SetDecisionHeadTest();
            stoptest();
        }
        if((nHdFailCnt >= 2) && (MAX_HEAD == 2))
        {
            SetDecisionHeadTest();
            stoptest();
        }
    }
    sprintf(EXPLOG,"killing cnt:%d nHd:%d nDsk:%d",nFailCnt,nHd,nDsk);
    userfilelog(EXPLOG);
    
    if((nHdFailCnt != 0) && (nFailCnt == 1))
    {
        //if(HDDETECT == 0)   nKill = EXPF1KILL(nHd);
        //else                nKill = 0;
        nKill = 1;
        if(nKill==0)    // Hd kill ok
        {
            userfilelog("Killing OK");
            
            nRet = GetDecisionHead(nHd);
            if(nRet == HEAD_FAIL || nRet == HEAD_DMR)
            {
                str_exp_setdectype(2, 1);
                sputd(EXPLOG,"Kill_HD:%d",nHd);
                poststatus(EXPLOG);
                sputd(debugcode,"H0%d100",nHd);
                strcpy(EXPLOG,debugcode);
                userfilelog(EXPLOG);
                strcpy(REPAIRCODE,debugcode);    //Head Killing
                Send_Debug_info();        
            }
            else
            {
                str_exp_setdectype(2, 2);
                sputd(EXPLOG,"Kill_HS:%d",nHd);
                poststatus(EXPLOG);
                if(nHd == 0)
                {
                    strcpy(REPAIRCODE,"H0000A");    //HS 0 Killing
                    strcpy(EXPLOG,"H0000A");
                    userfilelog(EXPLOG);
                    Send_Debug_info();        
                }
                else
                {					   //0123456
                    sputd(debugcode,"H010%d0",nHd);
                    strcpy(EXPLOG,debugcode);
                    userfilelog(EXPLOG);
                    strcpy(REPAIRCODE,debugcode);    //HS Killing
                    Send_Debug_info();        
                }
            }
        }
    }
    else if((nDskFailCnt != 0) && (nFailCnt == 1))
    {
        nKill = 1;
        //if(nHdFailCnt==0)	{ nKill = EXPF1KILL(nDsk);}
        if(nKill == 0) // Hd kill ok
        {
            str_exp_setdectype(2, 3);
            sputd(EXPLOG,"Kill_DK:%d",nDsk);
            poststatus(EXPLOG);
            sputd(debugcode,"D0%d010",nDsk);
            strcpy(EXPLOG,debugcode);
            userfilelog(EXPLOG);
            strcpy(REPAIRCODE,debugcode);    //Disk Killing
            Send_Debug_info();        
        }
    }
    else if((nFailCnt == 2) && (nHd == nDsk))
    {
        nKill = 1;
        //nKill = EXPF1KILL(nHd);
        if(nKill == 0) // Hd kill ok
        {
            nRet=GetDecisionHead(nHd);				
            nCnt=nHd+0x30;				
            if(nRet==HEAD_FAIL||nRet==HEAD_DMR)
            {					
                str_exp_setdectype(2, 4);
                sputd(EXPLOG,"Kill_HD:%d",nHd);
                poststatus(EXPLOG);
                
                sputd(debugcode,"D0%d010",nHd);
                strcpy(REPAIRCODE,debugcode);    //Disk Killing
                strcpy(EXPLOG,debugcode);
                userfilelog(EXPLOG);
                Send_Debug_info();        
                
                sputd(debugcode,"H0%d100",nHd);
                strcpy(EXPLOG,debugcode);
                userfilelog(EXPLOG);
                strcpy(REPAIRCODE,debugcode);    //Head Killing
                Send_Debug_info();        
            }
            else
            {
                str_exp_setdectype(2, 5);
                sputd(EXPLOG,"Kill_HS:%d",nHd);
                poststatus(EXPLOG);
                if(nHd == 0)
                {
                    strcpy(REPAIRCODE,"H0000A");    //Head Killing
                    Send_Debug_info();        
                    strcpy(REPAIRCODE,"D00010");    //Disk Killing
                    Send_Debug_info();        
                    stoptest();
                }
                else
                {					   //0123456
                    sputd(debugcode,"D0%d010",nHd);
                    strcpy(REPAIRCODE,debugcode);    //Disk Killing
                    Send_Debug_info();        
                    sputd(debugcode,"H010%d0",nHd);
                    strcpy(REPAIRCODE,debugcode);    //HS Killing
                    Send_Debug_info();        
                }
            }
        }
    }
    if(nKill==0)
    {
        userfilelog("KILL OK");
        Restore_MESmodel();
        MES_EXPERT_INFO.MES_MCODE[3] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[3],1);     //0:ORG, 1:DS, 2:DDS
        MES_EXPERT_INFO.MES_MCODE[4] = Get3rdCode(MES_EXPERT_INFO.MES_MCODE[4],1);     //0:ORG, 1:DS, 2:DDS
        //str_exp_runtest(MES_EXPERT_INFO.MES_MCODE,0x02);    //#define ESPECTYPE_HBT 0x02 //!<HT->BI
        //sputd(cmd,"XP %d",1);
        //poststatus(cmd);
        //str_exp_startcmd(cmd); 
        stoptest();
    }
    // kill End

    // Rework
    nFailCnt=0;
    for(nCnt=0; nCnt<MAX_HEAD; nCnt++)
    {
    	nRet=GetDecisionDisk(nCnt);
    	if(nRet!=0)
    	{
    		nDskFailCnt++;
    	}  
    	nRet=GetDecisionHead(nCnt);
    	if(nRet!= HEAD_SWAP && nRet!=0)
    	{
    		nFailCnt++;
    	}
    }   
    if(nFailCnt != 0)
    {
       	for(nCnt=0; nCnt<MAX_HEAD; nCnt++)
       	{
       		nRet=GetDecisionHead(nCnt);
    		if(nRet == HEAD_SWAP && nRet != 0)
    		{
    			SetDecisionHead(nCnt,0);
    			nRet = GetDecisionHead(nCnt);
    			sprintf(EXPLOG,"Delete HeadSwap Decision HD:%d %d",nCnt,nRet);
                userfilelog(EXPLOG);
    		}
    	}
    }
    nDskFailCnt=0;
    //strcpy(debug,"D:");
    for(nCnt=0; nCnt<MAX_HEAD; nCnt++)
    {
    	if(nDskFailCnt > 6) break;
    	nRet = GetDecisionDisk(nCnt);
        //printf("GetDecisionDisk nRet : %d",nRet);    	
    	if(nRet != 0)
    	{
    		nDskFailCnt++;
            str_exp_setdectype(6, 2);
            sputd(EXPLOG,"DK:%d",nCnt);
            poststatus(EXPLOG);
            sputd(debugcode,"D0%d00B",nCnt);
            strcpy(REPAIRCODE,debugcode);    //Disk Fail
            Send_Debug_info();        
    	}
    }
    nFailCnt=0;
    //strcpy(debug,"H:");
    for(nCnt=0; nCnt<MAX_HEAD; nCnt++)
    {
    	if(nFailCnt > 6) break;
    	nRet = GetDecisionHead(nCnt);
        //printf("GetDecisionHead[%d] : %d",nCnt,nRet);
    	if(nRet != 0)
    	{
    		nFailCnt++;    		
			if(nRet==HEAD_FAIL || nRet==HEAD_DMR)
			{
				nType = nRet;
                str_exp_setdectype(6, 1);
                sputd(EXPLOG,"HD:%d",nCnt);
                poststatus(EXPLOG);
                sputd(debugcode,"H0%d000",nCnt);
                strcpy(REPAIRCODE,debugcode);    //Head Fail
                Send_Debug_info();
				//break;
            }
			else
			{
                if(MES_EXPERT_INFO.MES_STW_COUNT < 2)
                {
                    //poststatus("HeadSwap");
                    str_exp_setdectype(6, 3);
                    CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
                    return;
                }       
				nType = nRet;
                str_exp_setdectype(6, 5);
                sputd(EXPLOG,"HS:%d",nCnt);
                poststatus(EXPLOG);
                if(nHd == 0)
                {
                    strcpy(REPAIRCODE,"H0000A");    //HS 0
                    Send_Debug_info();        
                }
                else
                {					   //0123456
                    sputd(debugcode,"H010%d0",nCnt);
                    strcpy(REPAIRCODE,debugcode);    //HS
                    Send_Debug_info();        
                }
				break;
			}
    	}
    }
//printf("nDskFailCnt : %d",nDskFailCnt);      
	if (nDskFailCnt != 0)
	{
        strcpy(debug,"DK:");
        for(nCnt=0; nCnt<MAX_HEAD; nCnt++)
        {
            nRet = GetDecisionDisk(nCnt);
            //printf("GetDecisionDisk nRet : %d",nRet);    	
            if(nRet != 0)
            {
                sputd(EXPLOG,"%d ",nCnt);
                strcat(debug,EXPLOG);
            }
        }
        userfilelog("DISK Fail");
        strcpy(EXPLOG," Fail");
        strcat(debug,EXPLOG);
        str_exp_setdectype(6, 2);
        poststatus(debug);
        //stoptest();     
	}
    if(nFailCnt != 0)
    {
        if (nType == HEAD_FAIL)
        {
            strcpy(debug,"HD:");
            for(nCnt=0; nCnt<MAX_HEAD; nCnt++)
            {
                nRet = GetDecisionHead(nCnt);
                //printf("GetDecisionDisk nRet : %d",nRet);    	
                if(nRet != 0)
                {
                    sputd(EXPLOG,"%d ",nCnt);
                    strcat(debug,EXPLOG);
                }
            }
            userfilelog("Head Fail");
            str_exp_setdectype(6, 1);
            strcpy(EXPLOG," Fail");
            strcat(debug,EXPLOG);
            poststatus(debug);
            stoptest();     
        }
        if (nType == HEAD_DMR)
        {
            strcpy(debug,"HD:");
            for(nCnt=0; nCnt<MAX_HEAD; nCnt++)
            {
                nRet = GetDecisionHead(nCnt);
                //printf("GetDecisionDisk nRet : %d",nRet);    	
                if(nRet != 0)
                {
                    sputd(EXPLOG,"%d ",nCnt);
                    strcat(debug,EXPLOG);
                }
            }        
            userfilelog("Head DMR");
            str_exp_setdectype(6, 1);
            strcpy(EXPLOG," Fail");
            strcat(debug,EXPLOG);
            poststatus(debug);
            stoptest();     
        }
    }
    else
    {
        SetDecisionHeadSwap();
        userfilelog("Expert_Decision HeadSwap");
        stoptest();
    }
    return;
}

int  EXPF1KILL(int hd)
{
	char killhd[16];
    char model[16];
    char debug[16];
    
	int ivalue, xHdNum;
    int nRet;
    
    if(KILLFLAG != 0)   return 1;

	xHdNum = 0;
	//Target HD	
    xHdNum =  xHdNum | (0x0000001<<hd);	//s=0x01, 0x02, 0x04, 0x08, 0x10, 0x20 (HD 0,1,2,3,4,5) 

    // HK CMD
	sputd(killhd,"KL %x",xHdNum);
	strlen(killhd,ivalue);
	str_sendserialcmd(killhd,ivalue);

    //BI CODE D/L	
    sputs(EXPLOG,"ORGF11D72:%s",ORGF11D72);
    userfilelog(EXPLOG);
    dw(ORGF11D72);      //F1-3D 1TB Code
    delays(40);
    power(0);
    delays(3);
    power(1);
    delays(80);
    reset();
    id(0);
    str_getinvalidled(INVALID_LED);
    
    if(INVALID_LED != 0x0000)
    {
        str_exp_setdectype(1, 0);        
        poststatus("D/L Fail");
        return 1;
    }
    return 0;
}

void SetDecisionHeadSwap()
{
    if(STR_PORT_SCRN ==0)
    {
        str_exp_setdectype(6, 5);
        poststatus("HeadSwap");
        userfilelog("HSA Test");
        strcpy(REPAIRCODE,"H11000");	//HSA Test
        Send_Debug_info();
        return;
    }
    else
    {
        str_exp_setdectype(7, 6);
        poststatus("Port Screen");
        strcpy(REPAIRCODE,"T10005");	//HSA Test
        Send_Debug_info();
        return;    
    }
}

void SetDecisionHeadTest()
{
    if(STR_PORT_SCRN ==0)
    {
        str_exp_setdectype(6, 5);
        poststatus("HeadTest");
        strcpy(REPAIRCODE,"H11000");	//HSA Test
        Send_Debug_info();
        return;    
    }
    else
    {
        str_exp_setdectype(7, 6);
        poststatus("Port Screen");
        strcpy(REPAIRCODE,"T10005");	//HSA Test
        Send_Debug_info();
        return;    
    }
}

void SetDecisionHead(int nNum,int Type)
{	
	int shift,temp;

    //sprintf(EXPLOG,"nNum:%x Type:%x",nNum,Type);
    //poststatus(EXPLOG);
	
	shift=nNum*2;
	temp=0x03;
	temp=temp<<shift;
	temp = 0xFFFFFFFF^temp;//Not
	//sprintf(EXPLOG,"SetDecisionHead Num:%d Type:%d mask:%x",nNum,Type,temp);
    //userfilelog(EXPLOG);
	gD_HD = gD_HD & temp;  //Clear	 
	gD_HD = gD_HD | (Type << shift); //Set
    return;
}

void SetDecisionDisk(int nNum)
{	
	gD_DISK = gD_DISK | (1 << nNum);
    return;
}

void SetDecisionSTW(int nNum)
{	
	gD_STW = gD_STW | (1 << nNum);
    return;
}

int GetDecisionHead(int nNum)
{
	int Value,shift;
	shift=nNum*2;
	Value=gD_HD & ( 0x03 << shift);	
	Value=(Value>>shift);
	return Value;
}

int GetDecisionDisk(int nNum)
{	
	int Value;
	Value=gD_DISK & ( 1 << nNum);	
	return Value;
}

int GetDecisionSTW(int nNum)
{	
	int Value;
	Value=gD_STW & ( 1 << nNum);	
	return Value;
}

int GetNumOfZone(int nCyl,int nHd)
{
	int nLastCyl,nZn;

    for(nZn=0;nZn<MAX_ZONE;nZn++)
	{
        nLastCyl = TBL_ZONE[nHd][nZn];
		
		if(nCyl <= nLastCyl)
		{
			return nZn;
		}
	}
	return 0;
}

void Set_TestCondition(int ECC, int ECCSymbol, int UserRetry, int HidWRetry, int HidRRetry)
{
    int  cmdlen;
    char cmd[32], buff[32];

//ECC ON,OFF
    sprintf(cmd,"XK 2 %x",ECC);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);  //XK 2 val : ECC OFF

//ECC Symbol    
    sprintf(cmd,"XK 1 %x",ECCSymbol);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);  //XK 1 val : ECC Symbol setting

//User Retry
    sprintf(cmd,"XK 4 %x",UserRetry);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);  //XK 1 val : ECC Symbol setting

//Hidden Write retry
    sprintf(cmd,"XK 3 0 %x",UserRetry);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);  //XK 3 0 val : Hidden write retry setting
    
//Hidden Read retry
    sprintf(cmd,"XK 3 1 %x",UserRetry);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);  //XK 3 1 val : Hidden read retry setting
    return;
}

int IsSeekFail(int nstartCyl, int ntargetCyl,int nstartHd,int ntargetHd,int nMode,int nLoop,float nthres)
{
//##################################################################################################
//# 1. XK 8 sCyl tCly sHd tHd 0 loop
//#  - sCyl sHd ¿¡¼­ tCyl tHd·Î read seek¸¦ loop ¸¸Å­ ¹Ýº¹ÇÏ¸é¼­ error count¸¦ º¸¿©ÁÜ
//# 2. XK 8 sCyl tCly sHd tHd 1 loop
//#  - sCyl sHd ¿¡¼­ tCyl tHd·Î write seek¸¦ loop ¸¸Å­ ¹Ýº¹ÇÏ¸é¼­ error count¸¦ º¸¿©ÁÜ
//##################################################################################################        
    
    int  nCnt,nCnt1, cmdlen, nSeekloop, nErrCnt;
    char cmd[32], buff[32];

    sputd(EXPLOG,"IsSeektest HD[%d]",ntargetHd);
    //poststatus(EXPLOG);
    userfilelog(EXPLOG);

    if (ntargetHd > MAX_HEAD)
        return 0;

    sprintf(cmd,"XK 8 %x %x %x %x %x %x",nstartCyl,ntargetCyl,nstartHd,ntargetHd,nMode,nLoop);
    //sCyl sHd ¿¡¼­ tCyl tHd·Î read seek¸¦ loop ¸¸Å­ ¹Ýº¹ÇÏ¸é¼­ error count¸¦ º¸¿©ÁÜ. 0 write mode
    //sCyl sHd ¿¡¼­ tCyl tHd·Î write seek¸¦ loop ¸¸Å­ ¹Ýº¹ÇÏ¸é¼­ error count¸¦ º¸¿©ÁÜ. 1 read mode
    strlen(cmd,cmdlen);
    
    ONERROR(dummyerror);
    str_usercmd(cmd,cmdlen,20000,55,0,RDBUF,512);
    ONERRORCLEAR();
    /*
    nCnt=0; nCnt1=0;
    strfind(RDBUF,"SeekLoop: ",nCnt);
    //find CR
    nCnt1=nCnt+10;
    if(nCnt != 0)
    {
        while(RDBUF[nCnt1] != 0)
        {
            if(RDBUF[nCnt1] == 0x53)
            {
                RDBUF[nCnt1] = 0;
                memnmov(buff,RDBUF,0,nCnt+10,nCnt1-nCnt-10,0);
                atoi(buff,0,10,nSeekloop);
                //poststatus(buff); delays(3);
                break;
            }
            nCnt1++;
        }
    }
    RDBUF[nCnt1] = 0x20; //fill space
    */
    
    nCnt=0; nCnt1=0;
    //userfilelog(RDBUF);
    strfind(RDBUF,"SeekError: ",nCnt);
    //find CR
    nCnt1=nCnt+11;
    if(nCnt != 0)
    {
        //userfilelog("find SeekError:");
        while(RDBUF[nCnt1] != 0)
        {
            if(RDBUF[nCnt1] == 0x0d)
            {
                RDBUF[nCnt1] = 0;
                memnmov(buff,RDBUF,0,nCnt+11,nCnt1-nCnt-11,0);
                atoi(buff,0,10,nErrCnt);
                //poststatus(buff); delays(3);
                break;
            }
            nCnt1++;
        }
    }
    
    sprintf(EXPLOG,"ntargetHd:%d, SeekLoop:%d, SeekError:%d",ntargetHd,nLoop,nErrCnt);
    userfilelog(EXPLOG);
    
    if(nErrCnt > nthres)
        return 1;
    
    return 0;
}

int IsBERFail(int ntargetCyl,int ntargetHd,int nLoop,float nthres)   //OUTPUT Data¸¦ BER·Î ÇÏ´øÁö Readcount¸¦ ´Ù½Ã Ç¥½ÃÇÏ´Â °ÍÀ¸·Î º¯°æ
{
//##################################################################################################
//# 1. XK 6 cyl head loop
//#  - cyl, head À§Ä¡¿¡¼­ 256¼½ÅÍ write ÈÄ loop ¸¸Å­ 256 ¼½ÅÍ read
//#  - loop count ¿Í error count º¸¿©ÁÜ.
//##################################################################################################    

    int  itemp,nCnt,nCnt1,cmdlen,nErrCnt,nReadCnt,tled,flag,find,addr,nAddress,nOffs,nRet;
    float fIsBerRes;
    char cmd[32], buff[32];
    
    sputd(EXPLOG,"IsBERFail HD[%d]",ntargetHd);
    poststatus(EXPLOG);

    sprintf(cmd,"XK 8 %x %x %x %x %x %x",60,62,0,0,0,4);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    delays(1);

    if (ntargetHd > MAX_HEAD)
        return 0;
    /*
    sprintf(cmd,"XK 6 %x %x %x",ntargetCyl,ntargetHd,nLoop);
    strlen(cmd,cmdlen);
    
    ONERROR(dummyerror);
    str_usercmd(cmd,cmdlen,10000,512,0,RDBUF,512);
    ONERRORCLEAR();
    
    strfind(RDBUF,"repeat count =",nCnt);
    //find CR
    nCnt1=nCnt+14;
    if(nCnt != 0)
    {
        while(RDBUF[nCnt1] != 0)
        {
            if( RDBUF[nCnt1] == 0x0d )
            {
                RDBUF[nCnt1] = 0;
                memnmov(buff,RDBUF,0,nCnt+14,nCnt1-nCnt-14,0);
                atoi(buff,0,10,nReadCnt);
                //poststatus(buff); delays(3);
                break;
            }
            nCnt1++;
        }
    }

    strfind(RDBUF,"error count =",nCnt);
    //find CR
    nCnt1=nCnt+13;
    if(nCnt != 0)
    {
        while(RDBUF[nCnt1] != 0)
        {
            if( RDBUF[nCnt1] == 0x0d )
            {
                RDBUF[nCnt1] = 0;
                memnmov(buff,RDBUF,0,nCnt+13,nCnt1-nCnt-13,0);
                atoi(buff,0,10,nErrCnt);
                //poststatus(buff); delays(3);
                break;
            }
            nCnt1++;
        }
    }
    nReadCnt = nReadCnt * nLoop;
    itemp = (nReadCnt*8*512)/nErrCnt;
    log10(itemp,fIsBerRes);
    sprintf(EXPLOG,"BER ntargetHd:%d nReadCnt:%d ErrCnt:%d BER:%5.2f",ntargetHd,nReadCnt,nErrCnt,fIsBerRes);
    userfilelog(EXPLOG);
    
    if(fIsBerRes < nthres)
    {
        return 1;
    }
    */
    //Special serial CMD
    sprintf(cmd,"XK 6 %x %x %x",ntargetCyl,ntargetHd,nLoop);
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    userfilelog(cmd);
    str_monitoring("XK 6",10,tled,flag);
    sputd(EXPLOG,"XK 6 Results : LED%x",tled);
    userfilelog(EXPLOG);
    poststatus("XK 6 END");
    str_debugmode();

    //Get Memory Address
    ONERROR(dummyerror);
    str_usercmd("XK A",4, 3000, 60, 0, RDBUF, 512);
    ONERRORCLEAR();
    //sputs(EXPLOG,"RDBUF:%s",RDBUF);
    //userfilelog(EXPLOG);
    
    strfind(RDBUF,"Buffer Start :",find);
    if(find>0)
    {
        memset(addr,0,0,8);
        memnmov(addr,RDBUF,0,find+15,8,0);
        atoi(addr,0,16,nAddress);
        sputd(EXPLOG,"XK A addr:%x",nAddress);
        userfilelog(EXPLOG);
    }
    
    if(nAddress != 0)
    {
        sputd(cmd,"DS %x 100",nAddress);
        strlen(cmd,cmdlen);
        ONERROR(dummyerror);
        str_usercmd(cmd,cmdlen, 3000, 1687, 1, RDBUF, 512);
        ONERRORCLEAR();
        
        nOffs=0x00;
        peek(RDBUF,nOffs+4,2,nErrCnt);
        
        nReadCnt = 256 * nLoop;  //100Sector*nloop
        itemp = (nReadCnt*8*512)/nErrCnt;
        log10(itemp,fIsBerRes);
        sprintf(EXPLOG,"BER ntargetHd:%d nReadCnt:%d ErrCnt:%d BER:%5.2f",ntargetHd,nReadCnt,nErrCnt,fIsBerRes);
        userfilelog(EXPLOG);
    }
    else
    {
        fIsBerRes=8.00;
        sprintf(EXPLOG,"BER Test Address Fail ntargetHd:%d BER:%5.2f",ntargetHd,fIsBerRes);
        userfilelog(EXPLOG);
    }
    //Power Off/ON
    power(0);
    delays(3);
    power(1);
    delays(40);
    ONERROR(reseterror);
    reset();
    ONERRORCLEAR();
    //str_sendserialcmd("XQ",2);      //loading Servo table
    //delays(90);
    //str_checkprompt("ENG>");
    
    if(fIsBerRes < nthres)
    {
        return 1;
    }
    return 0;
}

void IsServoGainFail(int ntargetCyl,int ntargetHd,int nMode,int nLoop,int Pgain,int mode)
{
//##################################################################################################
//# 1. XK 5 cyl head 0 loop
//#  - cyl, head À§Ä¡·Î read seek ÇÏ¸é¼­ loop ¸¸Å­ ¼­º¸ gainÀ» ¹Þ¾Æ ÃÖ´ë, ÃÖ¼Ò, Æò±Õ°ªÀ» º¸¿©ÁÜ
//# 2. XK 5 cyl head 1 loop
//#  - cyl, head À§Ä¡·Î write seek ÇÏ¸é¼­ loop ¸¸Å­ ¼­º¸ gainÀ» ¹Þ¾Æ ÃÖ´ë, ÃÖ¼Ò, Æò±Õ°ªÀ» º¸¿©ÁÜ
//##################################################################################################

    int  nCnt,nCnt1, cmdlen, min_sgain, max_sgain, avg_sgain;
    char cmd[32], buff[32];

    sputd(EXPLOG,"IsServoGainFail HD[%d]",ntargetHd);
    poststatus(EXPLOG);    

    if (ntargetHd > MAX_HEAD)
    {
        MIN_SGAIN[ntargetHd] = 0x50;
        MAX_SGAIN[ntargetHd] = 0x50;
        return;
    }

    sprintf(cmd,"XK 5 %x %x %x %x %x",ntargetCyl,ntargetHd,nMode,nLoop,Pgain);
    strlen(cmd,cmdlen);
    
    ONERROR(dummyerror);
    str_usercmd(cmd,cmdlen,3000,70,0,RDBUF,512);
    ONERRORCLEAR();

    if(mode == 0)
    {
        //find min sgain
        strfind(RDBUF,"min SGain:",nCnt);
        nCnt1=nCnt+10; //+find length
        if(nCnt != 0)
        {
            while(RDBUF[nCnt1] != 0)
            {
                if( RDBUF[nCnt1] == 0x6d )
                {
                    memset(buff,0,0,32);
                    memnmov(buff,RDBUF,0,nCnt+10,nCnt1-nCnt-10,0);
                    atoi(buff,0,10,min_sgain);
                    //poststatus(buff); delays(3);
                    MIN_SGAIN[ntargetHd] = min_sgain;
                    break;
                }
                nCnt1++;
            }
        }
        
        //find max sgain
        strfind(RDBUF,"max SGain:",nCnt);
        nCnt1=nCnt+10;
        if(nCnt != 0)
        {
            while(RDBUF[nCnt1] != 0)
            {
                if( RDBUF[nCnt1] == 0x61 )
                {
                    memset(buff,0,0,32);
                    memnmov(buff,RDBUF,0,nCnt+10,nCnt1-nCnt-10,0);
                    atoi(buff,0,10,max_sgain);
                    //poststatus(buff); delays(3);
                    MAX_SGAIN[ntargetHd] = max_sgain;
                    break;
                }
                nCnt1++;
            }
        }
        
        //find avg sgain
        strfind(RDBUF,"avg SGain:",nCnt);
        nCnt1=nCnt+10;
        if(nCnt != 0)
        {
            while(RDBUF[nCnt1] != 0)
            {
                if( RDBUF[nCnt1] == 0x0d )
                {
                    memset(buff,0,0,32);
                    memnmov(buff,RDBUF,0,nCnt+10,nCnt1-nCnt-10,0);
                    atoi(buff,0,10,avg_sgain);
                    //poststatus(buff); delays(3);
                    AVG_SGAIN[ntargetHd] = avg_sgain;
                    break;
                }
                nCnt1++;
            }
        }
        sprintf(EXPLOG,"ntargetHd:%d, min SGain:%d, max SGain: %d, avg SGain:%d",ntargetHd,MIN_SGAIN[ntargetHd],MAX_SGAIN[ntargetHd],AVG_SGAIN[ntargetHd]);
        userfilelog(EXPLOG);
        return;
    }    
    else if(mode == 1)
    {
        //find min sgain
        strfind(RDBUF,"n:",nCnt);
        nCnt1=nCnt+3; //+find length
        if(nCnt != 0)
        {
            while(RDBUF[nCnt1] != 0)
            {
                if(RDBUF[nCnt1] == 0x4d)
                {
                    memset(buff,0,0,32);
                    memnmov(buff,RDBUF,0,nCnt+3,nCnt1-nCnt-3,0);
                    atoi(buff,0,10,min_sgain);
                    //poststatus(buff); delays(3);
                    MIN_SGAIN[ntargetHd] = min_sgain;
                    break;
                }
                nCnt1++;
            }
        }
        
        //find max sgain
        strfind(RDBUF,"x:",nCnt);
        nCnt1=nCnt+3;
        if(nCnt != 0)
        {
            while(RDBUF[nCnt1] != 0)
            {
                if(RDBUF[nCnt1] == 0x41)
                {
                    memset(buff,0,0,32);
                    memnmov(buff,RDBUF,0,nCnt+3,nCnt1-nCnt-3,0);
                    atoi(buff,0,10,max_sgain);
                    //poststatus(buff); delays(3);
                    MAX_SGAIN[ntargetHd] = max_sgain;
                    break;
                }
                nCnt1++;
            }
        }
        
        //find avg sgain
        strfind(RDBUF,"g:",nCnt);
        nCnt1=nCnt+3;
        if(nCnt != 0)
        {
            while(RDBUF[nCnt1] != 0)
            {
                if(RDBUF[nCnt1] == 0x0d)
                {
                    memset(buff,0,0,32);
                    memnmov(buff,RDBUF,0,nCnt+3,nCnt1-nCnt-3,0);
                    atoi(buff,0,10,avg_sgain);
                    //poststatus(buff); delays(3);
                    AVG_SGAIN[ntargetHd] = avg_sgain;
                    break;
                }
                nCnt1++;
            }
        }
        sprintf(EXPLOG,"ntargetHd:%d, min SGain:%d, max SGain: %d, avg SGain:%d",ntargetHd,MIN_SGAIN[ntargetHd],MAX_SGAIN[ntargetHd],AVG_SGAIN[ntargetHd]);
        userfilelog(EXPLOG);
        return;
    }
}

void xqerror()
{
    int tled, flag, find, cmdlen, nAddress, addr, nOffs, nRet, nHd;
    int nODSGC, nMDSGC, nIDSGC, nDiff1, nDiff2, nDiff3;
    int ntemp0, icyl, ihd;
    int nSlimTDvlaue[8];
    int cmd[32];
    
    //Power Off/ON
    power(0);
    delays(3);
    power(1);
    delays(40);
    ONERROR(reseterror);
    reset();
    ONERRORCLEAR();
    
    //Head detect
    nRet = CHK_HeadDetect(1);
    if(nRet != 0)
    {
        ExpertDecision(0);
        stoptest();
    }
    
    //////////////////////////////
    //Slim TD Test (XK 9)
    //////////////////////////////
    tled=0; flag=0; find=0; cmdlen=0; nAddress=0;
    userfilelog("Slim TD Test Start");
    userfilelog("Special serial CMD : XK 9");
    str_checkprompt("ENG>");
    str_sendserialcmd("XK 9",4);
    str_monitoring("XK 9",30,tled,flag);
    sputd(EXPLOG,"XK 9 Results : LED%x",tled);
    userfilelog(EXPLOG);
    poststatus("XK 9 END");
    str_debugmode();    //F1 can not Reset()
    
    //Get Memory Address
    ONERROR(dummyerror);
    str_usercmd("XK A",4, 3000, 60, 0, RDBUF, 512);
    ONERRORCLEAR();
    //sputs(EXPLOG,"RDBUF:%s",RDBUF);
    //userfilelog(EXPLOG);
    
    strfind(RDBUF,"Buffer Start :",find);
    if(find>0)
    {
        memset(addr,0,0,8);
        memnmov(addr,RDBUF,0,find+15,8,0);
        atoi(addr,0,16,nAddress);
        sputd(EXPLOG,"XK A addr:%x",nAddress);
        userfilelog(EXPLOG);
    }
    if(nAddress != 0)
    {
        sputd(cmd,"DS %x 100",nAddress);
        strlen(cmd,cmdlen);
        ONERROR(dummyerror);
        str_usercmd(cmd,cmdlen, 3000, 1687, 1, RDBUF, 512);
        ONERRORCLEAR();
        
        nOffs=0x08; nRet=0;
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            peek(RDBUF,nOffs+nHd*4,2,nRet);
            nSlimTDvlaue[nHd]=nRet;
            sprintf(EXPLOG,"HD[%d] nSlimTDvlaue=%x",nHd,nSlimTDvlaue[nHd]);
            userfilelog(EXPLOG);
            
            if((nSlimTDvlaue[nHd] != 0) && (nSlimTDvlaue[nHd] < 170) && (nSlimTDvlaue[nHd] > 240))
            {
                SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sprintf(EXPLOG,"HD[%d] Slim TD Fail : 170 <= %d <= 240",nSlimTDvlaue);
                poststatus(EXPLOG);
            }
        }
    }
    else
    {
        userfilelog("Slim TD Test Address Fail");
    }
    //Power Off/ON
    power(0);
    delays(3);
    power(1);
    delays(40);
    ONERROR(reseterror);
    reset();
    ONERRORCLEAR();
    
    //////////////////////////////
    //OD-MD-ID Servo Gain Test
    //////////////////////////////
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nRet = IsSeekFail(62,63,nHd,nHd,1,2,1);
        if(nRet == 0)
        {
            IsServoGainFail(1000,nHd,1,240,3,0);
            nODSGC = AVG_SGAIN[nHd];
            
            IsServoGainFail(80000,nHd,1,240,3,0);
            nMDSGC = AVG_SGAIN[nHd];
            
            IsServoGainFail(170000,nHd,1,240,3,0);
            nIDSGC = AVG_SGAIN[nHd];
            
            ntemp0=nODSGC-nMDSGC;
            abs(ntemp0,nDiff1);
            ntemp0=nODSGC-nIDSGC;
            abs(ntemp0,nDiff2);
            ntemp0=nMDSGC-nIDSGC;
            abs(ntemp0,nDiff3);
            if((nDiff1 >= 80) || (nDiff2 >= 80) || (nDiff3 >= 80))
            {
                SetDecisionHead(nHd,HEAD_FAIL);	//type0:good,1:HEAD_DMR,2:HEAD_FAIL,3:HEAD_SWAP
                sprintf(EXPLOG,"nDiff1:%d nDiff2:%d nDiff3:%d",nDiff1,nDiff2,nDiff3);
                userfilelog(EXPLOG);
                sprintf(EXPLOG,"ServoGain difference Fail Hd:%d Fail",nHd);
                userfilelog(EXPLOG);
            }
        }
    }
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        nRet = GetDecisionHead(nHd);
        if(nRet != 0)
        {
            ExpertDecision(1);
            stoptest();
        }
    }
    str_sendserialcmd("QT 0018",7); //QT 4018 : cal. vlaue save mc
    userfilelog("QT CMD 0018");
    str_monitoring("QT 0x0018",300,tled,flag);
    sputd(EXPLOG,"QT 0x0018 RES:L%x",tled);
    userfilelog(EXPLOG);
    poststatus(EXPLOG);
    str_debugmode();
    
    if(tled == 0x00)
    {
    // HTRule Pass -> Re-STW
        sprintf(EXPLOG,"xqerror skew,gap cal. Pass -> Re-STW");
        userfilelog(EXPLOG);    
        CHK_MESHistory(1,0);     //1:ReSTW, 2:ReSC, 3:ReBI, 4:ReDS, 5:DMR
        stoptest();
    }
    else
    {
        icyl=0; ihd=0xff;
        str_usercmd("RI", 2, 3000, 1716, 1, RDBUF, 512);
        peek(RDBUF,0x00,4,icyl);
        peek(RDBUF,0x05,1,ihd);
        sprintf(EXPLOG,"L%x ihd %x",tled,ihd);
        userfilelog(EXPLOG);
        if(icyl != 0)
        {
            if(ihd != 0xFF)
            {
                sputd(EXPLOG,"HD:%d Fail",ihd);
                userfilelog(EXPLOG);
                SetDecisionHead(ihd, HEAD_FAIL);	//type 1 : Head Fail
                ExpertDecision(1);
                stoptest();
            }
            else
            {
                for(nHd=0; nHd<MAX_HEAD; nHd++)
                {
                    nRet = GetDecisionHead(nHd);
                    if(nRet != 0)
                    {
                        ExpertDecision(1);
                        stoptest();
                    }
                }
                sputd(EXPLOG,"LED%x Head Swap",tled);
                userfilelog(EXPLOG);
                SetDecisionHeadTest();
                stoptest();
            }
        }
        else
        {
            for(nHd=0; nHd<MAX_HEAD; nHd++)
            {
                nRet = GetDecisionHead(nHd);
                if(nRet != 0)
                {
                    ExpertDecision(1);
                    stoptest();
                }
            }
            sputd(EXPLOG,"LED%x Head Swap",tled);
            userfilelog(EXPLOG);
            SetDecisionHeadTest();
            stoptest();
        }
    }
}

void pkerror()
{
    str_exp_setdectype(1, 0);        
    poststatus("PK OOB");
    stoptest();
}

void MakeMassData()
{
    poststatus("Make Mass");
    str_sendserialcmd("PP",2);
    delays(300);
    ONERROR(OnMassError);
    reset();
    ONERRORCLEAR();
    return;
}

void reset_1st()
{
    ONERRORCLEAR();
    cmdtimeout(0);
    userfilelog("reset_1st");
    str_debugmode();
    ONERRORCLEAR();
    str_checkprompt("DBG>");
    
    cmdtimeout(5000);
	id(0);
    cmdtimeout(0);
    str_getengfw(engfw);
    sputs(EXPLOG,"engfw:%s",engfw);
    userfilelog(EXPLOG);
    if((engfw[7]==POSTFIX_sc) || (engfw[7]==POSTFIX_SC))
    {
        //SC Code Download
        poststatus("CODE D/L");
        str_debugmode();
        sputs(EXPLOG,"EXSCCODE:%s",EXSCCODE);
        userfilelog(EXPLOG);
        dw(EXSCCODE);
        str_checkprompt("BOOT>");
        delays(60);
        power(0);
        delays(3);
        power(1);
        delays(10);
        str_debugmode();
        
        CHK_HeadDetect(3);
    }
    else if(engfw[7] == POSTFIX_bi || engfw[7] == POSTFIX_BI)
    {
        InitReadyError(3);
    }
    return;
}

void reseterror()
{
    ONERRORCLEAR();
    userfilelog("reseterror");
    str_debugmode();
    str_checkprompt("DBG>");
    return;
}

void dummyerror()
{
	//poststatus("DummyErr");
	return;
}

void OnMassError()
{
    //poststatus("Mass Err");
    //stoptest();
    str_exp_setdectype(1, 0);
    poststatus("GoWinstar");
    stoptest();
}

void InitReadyError(int flag)
{
    int  find, nHd, cmdlen, temp, head;
    char hdstatus;
    int  ivalue;
    char head[4];
    char mrr[8];
    char fodres[8];
    char wrres[8];
    char phd[8];
    char cmd[32];
    char debug[64];
    memset(head,0,0,4);
    memset(mrr,0,0,8);
    memset(fodres,0,0,8);
    memset(wrres,0,0,8);
    memset(phd,0,0,8);
    memset(cmd,0,0,32);
    memset(debug,0,0,64);
    
    userfilelog("InitReadyError");
    
    if((flag!=0) && (flag!=3))      //SC Fail
    {
        //BI Code Download
        poststatus("CODE D/L");
        str_debugmode();
        sputs(EXPLOG,"ORGF11D72:%s",ORGF11D72);
        userfilelog(EXPLOG);
        dw(ORGF11D72);
        delays(40);
        power(0);
        delays(3);
        power(1);
        delays(10);
        str_debugmode();
    }
    else
    {
        //SC Code Download
        poststatus("CODE D/L");
        str_debugmode();
        sputs(EXPLOG,"EXSCCODE:%s",EXSCCODE);
        userfilelog(EXPLOG);
        dw(EXSCCODE);
        delays(40);
        power(0);
        delays(3);
        power(1);
        delays(10);
        str_debugmode();
    }
    
    ONERROR(dummyerror);
    reset();
    ONERRORCLEAR();
    
    //Check MRR
    poststatus("CHK_Head");
    ONERROR(dummyerror);
    //str_usercmd("XH", 2, 3000, 512, 0, RDBUF, 512);     //PHD
    str_usercmd("XH", 2, 10000, 128, 0, RDBUF, 512);
    ONERRORCLEAR();
    
    strcpy(phd,"PHD=");
    strfind(RDBUF,phd,find);
    if(find>0)
    {
        memset(phd,0,0,4);
        memnmov(phd,RDBUF,0,find+4,1,0);
        atoi(phd,0,10,ivalue);
        MAX_HEAD = ivalue;
        sputd(EXPLOG,"MAX_HEAD:%d",MAX_HEAD);
        poststatus(EXPLOG);
    }
    else
    {
        poststatus("CHK HD_Fail");
    }
    
    ONERROR(dummyerror);
    reset();        //buffer clear
    ONERRORCLEAR();

    HEAD_INFO = 0x00000000;
    ONERROR(dummyerror);
    //str_usercmd("XH", 2, 3000, 4+76*MAX_HEAD, 0, RDBUF, 512);    //456 : XH + CR + LF + (ÇÑÁÙ±æÀÌ+CR+LF) * HDnum
    str_usercmd("XH", 2, 3000, 311, 0, RDBUF, 512);
    ONERRORCLEAR();
    
    if((flag==0) || (flag==3))      //SC Fail
    {
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            sputd(head,"H%d:",nHd);
            strfind(RDBUF,head,find);
            if(find>0)
            {
                //sputd(EXPLOG,"H%d find",nHd);
                //userfilelog(EXPLOG);
                memset(mrr,0,0,8);
                memnmov(mrr,RDBUF,0,find+33,7,0);
                atoi(mrr,0,16,ivalue);
                RDY_MRR[nHd]=ivalue;
                //sputs(EXPLOG,"RDBUF[find+32]=%s",RDBUF[find+32]);
                //userfilelog(EXPLOG);
                if(RDBUF[find+27] == CHAR_G)    //find Good
                {
                    HEAD_INFO = (HEAD_INFO|(0x00000001<<nHd));
                    sprintf(EXPLOG,"CHK_HEAD FIND HD%d MRR:%x",nHd, ivalue);
                    userfilelog(EXPLOG);
                }
                if(RDBUF[find+27] == CHAR_B)    //find Fail
                {
                    sprintf(EXPLOG,"CHK_HEAD NOT FIND HD%d MRR:%x",nHd, ivalue);
                    userfilelog(EXPLOG);
                }
                else    //No find character
                {
                    HEAD_INFO = (HEAD_INFO|(0x00000001<<nHd));
                    sputd(EXPLOG,"CHK_HEAD nHd[%d] Character not found",nHd);
                    userfilelog(EXPLOG);
                }
            }
            else
            {
                //poststatus("X");
                break;
            }
        }
        reset();
        return;
    }
    else        //BI Fail
    {
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            sputd(head,"H%d:",nHd);
            strfind(RDBUF,head,find);
            if(find>0)
            {
                sputd(EXPLOG,"H%d find",nHd);
                userfilelog(EXPLOG);
                memset(mrr,0,0,8);
                memset(fodres,0,0,8);
                memset(wrres,0,0,8);
                //MRR
                memnmov(mrr,RDBUF,0,find+40,8,0);
                //sputs(EXPLOG,"mrr:%s",mrr);
                //userfilelog(EXPLOG);
                atoi(mrr,0,16,ivalue);
                RDY_MRR[nHd]=ivalue;
                //FODRES
                memnmov(fodres,RDBUF,0,find+11,4,0);
                //sputs(EXPLOG,"fodres:%s",fodres);
                //userfilelog(EXPLOG);
                atoi(fodres,0,16,ivalue);
                RDY_FODRES[nHd]=ivalue;
                //WRRES
                memnmov(wrres,RDBUF,0,find+30,4,0);
                //sputs(EXPLOG,"wrres:%s",wrres);
                //userfilelog(EXPLOG);
                atoi(wrres,0,16,ivalue);
                RDY_WRRES[nHd]=ivalue;
                
                sprintf(EXPLOG,"mrr:%x, fodres:%x, wrres:%x",RDY_MRR[nHd],RDY_FODRES[nHd],RDY_WRRES[nHd]);
                userfilelog(EXPLOG);
                if(RDBUF[find+35] == CHAR_G)    //find Good
                {
                    HEAD_INFO = (HEAD_INFO|(0x00000001<<nHd));
                    sprintf(EXPLOG,"CHK_HEAD FIND HD%d MRR:%x",nHd, ivalue);
                    userfilelog(EXPLOG);
                }
                if(RDBUF[find+35] == CHAR_B)    //find Fail
                {
                    sprintf(EXPLOG,"CHK_HEAD NOT FIND HD%d MRR:%x",nHd, ivalue);
                    userfilelog(EXPLOG);
                }
                else    //No find character
                {
                    HEAD_INFO = (HEAD_INFO|(0x00000001<<nHd));
                    sputd(EXPLOG,"CHK_HEAD nHd[%d] Character not found",nHd);
                    userfilelog(EXPLOG);
                }
            }
            else
            {
                //poststatus("X");
                break;
            }
        }
    }
    ONERROR(dummyerror);
    reset();
    ONERRORCLEAR();

    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        temp = 0x00000001<<nHd;
        if( (HEAD_INFO & temp) == 0 )
        {
            HDDETECT = 1;
            sputd(EXPLOG,"CHK_Head : HD%d Fail",nHd);
            userfilelog(EXPLOG);
            sputd(debug,"HD%dFail",nHd);
            poststatus(debug);
            SetDecisionHead(nHd,HEAD_FAIL);
        }
    }
    for(nHd=0; nHd<MAX_HEAD; nHd++)
    {
        temp = GetDecisionHead(nHd);
        if(temp != 0)
        {
            ExpertDecision(1);
            return;
        }
    }
}

void NotReady()
{
    int led,icyl,ihd;
    
    led=0xFFFF;
    icyl=0xFFFF;
    ihd=0xFF;
    
    poststatus("NotReady");
    userfilelog("NotReady");
    cmdtimeout(0);
    str_debugmode();
    ONERRORCLEAR();
    //reset();
    
    reset_1st();
    //InitReadyError();
    
    str_getengfw(engfw);
    if(engfw[7] == POSTFIX_sc || engfw[7] == POSTFIX_SC)
    {
        ONERROR(dummyerror);
        str_usercmd("RF 1", 4, 5000, 3584, 1, RDBUF, 512);
        ONERRORCLEAR();
        
        peek(RDBUF,0x100,4,icyl);
        peek(RDBUF,0x105,1,ihd);
        peek(RDBUF,0xDE,2,led);
        sprintf(EXPLOG,"SCL%x icyl:%x ihd:%x",led,icyl,ihd);
        userfilelog(EXPLOG);
        
        INVALID_LED = led;
        
        if((led != 0xFFFF) && (ihd != 0xFF) && (ihd >= MAX_HEAD))
        {
            SetDecisionHead(ihd, HEAD_FAIL);	//type 1 : Head Fail
            sputd(EXPLOG,"HD:%d Fail",ihd);
            userfilelog(EXPLOG);
            poststatus(EXPLOG);
            ExpertDecision(0);
            stoptest();
        }
        else if((led == 0x1704) && (icyl == 0xFFFF) && (ihd == 0xFF))
        {
            str_exp_setdectype(6, 5);
            userfilelog("L0x1704 HeadTest");
            SetDecisionHeadTest();
            stoptest();
        }
    }
}

int DOWNLOAD_EXPSCCODE(int delay, int type)
{
    int nHd, temp;
    poststatus("SCCODE D/L");
    dw(SCCODE);
    str_checkprompt("BOOT>");
    delays(50);
    power(0);
    delays(3);
    power(1);
    delays(delay);
    str_getinvalidled(INVALID_LED);
    
    if(INVALID_LED != 0x0000)
    {
        if(type == 0)
        {
            str_debugmode();
            InitReadyError(1);
            for(nHd=0; nHd<MAX_HEAD; nHd++)
            {
                temp = GetDecisionHead(nHd);
                if(temp != 0)	ExpertDecision(1);
            }
        }
        str_exp_setdectype(1, 0);        
        poststatus("SC D/LFail");
        return 1;
    }
    reset();
    id(0);
    return 0;
}

int DOWNLOAD_EXPBICODE(int delay)
{
    int nHd, temp;
    poststatus("BICODE D/L");
    if(CAPA == 32 && RPM == 72 && MAX_HEAD == 2)
    {
        userfilelog("F1-1D 7200rpm 320G CODE D/L");
        sputs(EXPLOG,"ORGF11D72:%s",ORGF11D72);
        userfilelog(EXPLOG);
        dw(ORGF11D72);                        //F1-1D 320G
    }
    else
    {
        str_exp_setdectype(1, 0);        
        poststatus("OOB");
        userfilelog("CODE XXXX : Download Fail");
        return 1;
    }
    delays(40);
    power(0);
    delays(3);
    power(1);
    delays(delay);
    str_getinvalidled(INVALID_LED);
    
    if(INVALID_LED != 0x0000)
    {
        str_debugmode();
        InitReadyError(3);
        for(nHd=0; nHd<MAX_HEAD; nHd++)
        {
            temp = GetDecisionHead(nHd);
            if(temp != 0)	ExpertDecision(1);
        }        
        str_exp_setdectype(1, 0);        
        poststatus("D/L Fail");
        return 1;
    }
    reset();
    id(0);
    return 0;
}

void Send_Debug_info()
{
    userfilelog("Send_Debug_info");
    str_meson();
    str_mesrepairinfo(REPAIRCODE);
    str_mesoff();
}

char Get3rdCode(char ch, int type)
{
    char orgchar;

    //initialize Character 
    if( ch >= 0x30 && ch <= 0x39 )
        orgchar = ch;
    else if( ch >= CHAR_A && ch <= CHAR_J)
        orgchar = ch-0x10;
    else if( ch >= CHAR_K && ch <= CHAR_T)
        orgchar = ch-0x20;
    else
        orgchar = ch;

    switch(type)
    {
        case 0:
        {
            return orgchar;
            break;
        }
        case 1:
        {
            orgchar = orgchar+0x10;
            return orgchar;
            break;
        }
        case 2:
        {
            orgchar = orgchar+0x20;
            return orgchar;
            break;
        }
        default:
            break;
    }
    return orgchar;
}

void Restore_MESmodel()
{
    int ret;
    
    strcmp(MES_EXPERT_INFO.MES_MCODE,"0000",4,ret);
    if(ret != 0)
    {
        //initialize Character 
        if( MES_EXPERT_INFO.MES_MCODE[3] >= CHAR_A && MES_EXPERT_INFO.MES_MCODE[3] <= CHAR_J )
        {
            MES_EXPERT_INFO.MES_MCODE[3]=MES_EXPERT_INFO.MES_MCODE[3]-0x10;
        }
        if( MES_EXPERT_INFO.MES_MCODE[3] >= CHAR_K && MES_EXPERT_INFO.MES_MCODE[3] <= CHAR_T )
        {
            MES_EXPERT_INFO.MES_MCODE[3]=MES_EXPERT_INFO.MES_MCODE[3]-0x20;
        }
        if( MES_EXPERT_INFO.MES_MCODE[4] >= CHAR_A && MES_EXPERT_INFO.MES_MCODE[4] <= CHAR_J )
        {
            MES_EXPERT_INFO.MES_MCODE[4]=MES_EXPERT_INFO.MES_MCODE[3]-0x10;
        }
        if( MES_EXPERT_INFO.MES_MCODE[4] >= CHAR_K && MES_EXPERT_INFO.MES_MCODE[4] <= CHAR_T )
        {
            MES_EXPERT_INFO.MES_MCODE[4]=MES_EXPERT_INFO.MES_MCODE[3]-0x20;
        }
        str_changemesmodel(MES_EXPERT_INFO.MES_MCODE);
    }
}

void exe_BI(int minute, int type)
{
    int cmdlen;
    char cmd[32];
/*
    XP espec_type
    - espec_type
    #define GLOBAL_EXP_BI_TYPE_HT_1             1   // IN-LINE EXPERT : HT Servo cal
    #define GLOBAL_EXP_BI_TYPE_HT_2             2   // IN-LINE EXPERT : HT W/R
    #define GLOBAL_EXP_BI_TYPE_BI_1             3   // IN-LINE EXPERT : BI Servo Cal
    #define GLOBAL_EXP_BI_TYPE_BI_2             4   // IN-LINE EXPERT : BI OSC WII
    #define GLOBAL_EXP_BI_TYPE_BI_3             5   // IN-LINE EXPERT : BI MR TURN
    #define GLOBAL_EXP_BI_TYPE_BI_4             6   // IN-LINE EXPERT : BI TPI
    #define GLOBAL_EXP_BI_TYPE_BI_5             7   // IN-LINE EXPERT : BI BER Measurement
    #define GLOBAL_EXP_BI_TYPE_BI_6             8   // IN-LINE EXPERT : BI Scan gray
    #define GLOBAL_EXP_BI_TYPE_BI_7             9   // IN-LINE EXPERT : BI W/R1
    #define GLOBAL_EXP_BI_TYPE_BI_8             10  // IN-LINE EXPERT : BI W/R2
    #define GLOBAL_EXP_BI_TYPE_BI_9             11  // IN-LINE EXPERT : BI ToneFill
    #define GLOBAL_EXP_BI_TYPE_BI_10            12  // IN-LINE EXPERT : BI FOD & W/R3
*/
	poststatus("BISPT D/L");
	//str_downloadbiscript(BISPT);
	poststatus("CHK ENG BISPT");
	delays(20);
	str_checkprompt("eng>");

    poststatus("exe_BI");
    sprintf(cmd,"XP %d",type);	// 'XP' Command <- for Expert B/I start
    strlen(cmd,cmdlen);
    str_sendserialcmd(cmd,cmdlen);
    delays(1);
    
	str_sendserialcmd("XP",2);	// 'DZ' Command <- for downsizing zonemap
	delays(60);
}


  