//##############################################################################
//#
//#                  WUTT DOALL.UTS   -  Samsung DoAll Process
//#             ( Run ServoCopy, Functional/BurnIn, Final and AT Test )
//#
//#               For All Promise PATA/SATA , SATA II Host Adapters
//#       (PATA UDMA 33/66/100/100TX2/133TX2 and SATA 150TX4 and SATA II 300)
//#
//# CHANGE HISTORY:
//# FILE+VER  MMM-DD-YYYY  NAME H/T VER DESCRIPTION OF CHANGE
//# --------  --- -- ----  ---- ------- ---------------------
//# M7ESTD01   07 27 2009  Fabre - M7E draft
//# M7ESTD02	08 24 2009 Fabre - change ATWH condition
//#		09 10 2009 fabre - delete fmtu 0x98 about ATWH
//#		09 29 2009 Fabre - add M5S bit corruption test
//# 		09 29 2009 Fabre - change ATWH threshold as M/C 
//# M7ESTd03	10 16 2009 Fabre - S2P setting
//#		10 20 2009 Fabre - change ATWH threshold from 10K to 64K
//# M7EEXT02	10 22 2009 Fabre - External setting
//#		10 23 2009 Fabre - cahnge WV TEMP from 15 to 17
//# M7EPEXT1	11 09 2009 Fabre - block S2 model, SMART Enable//#
//#		11 18 2009 Jaewoo- SSC disable
//#		11 18 2009 Jaewoo- Restore default settings to be related FLASH//#
//#		11 19 2009 Jaewoo- del SSC disable
//#		11 21 2009 Fabre - only check Error code in verify BI step
//# M7EPEXT2	12 11 2009 Fabre - change to enhanced WV
//#		01 15 2010 Fabre - 63nq setting
//#		05 27 2010 Fabre - fix ATWH threshold 5K, ATW healing sector size 1024
//#		08 24 2010 Fabre - block M7E-S3M aluminum
//#		08 30 2010 Fabre - cheat for MES ROM version check
//# M7EEXT3	09 11 2010 Fabre - Ready time set
//#		04 26 2011 Fabre - block 90,91,92 flatform
//#
//##############################################################################
//#               Copyright (C) Samsung Information Systems, 2005
//##############################################################################
/*--------------------------------------------------------------------
  SET THE SCRIPT FILENAME (FOR RESULT DISPLAY/VERSION CONTROL).
  MUST UPDATE WITH EACH CHANGE/VERSION.
  CHECK CORRECT MODEL BEFORE BI DUE TO UNIVERSAL HEAD.
--------------------------------------------------------------------*/
void Trinity_Check_ALIST_Count(int nMCId,int nOffs);
void M5S_BitCorruptionTest();
int Soft_Reset();
void NCQ_Error();
int NCQ_Soft_Reset();

void main()
{
	int nRet,nCnt,nRetry;	
	int nGlistCnt,nFeatureFlag,nAtwhThres,nAtwhCnt;
	int nWVTemp;
	int nV1,nV2,nV3,nV4,nV5;

	@WINSTAR_set_timeout(25000); // SET COMMAND TIMEOUT                 
	strcpy(StringParam1,"TC_10");	
@MESSAGE_TESTPHASE();
	@WINSTAR_load_port();                                                        
	@WINSTAR_set_testoption(LOG_ACPT); // A/T Tester
@MESSAGE_Step(NUM); 
	LastPowerOnLimit = 80000;
	@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
	@WINSTAR_set_PIO (4);              
@MESSAGE_Step(NUM);
	@WINSTAR_init_smtcode();
	strcpy(PATTERN, "RE_SC");
	@WINSTAR_screen_processcode(0,0x50);
	@WINSTAR_screen_processcode(0,0x53);
@MESSAGE_Step(NUM); 
	@WINSTAR_init();
	@WINSTAR_set_PIO (4);              
	@ACPT_start();

//#--------------------------------------------------------------------
//# CHECK MODEL
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"SAMSUNG");
	@WINSTAR_verify_model(); // CHECK TO VERIFY IDFY DATA & ATA INTF      
	strcpy(PATTERN,"$MODEL");
	@WINSTAR_verify_model(); // M40-2 HEAD - VERIFY MODEL BEFORE
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"2AJ");
	@WINSTAR_verify_firmware(BY_MAIN);
	strstr(gszCODENEGREV, ".d24", nRet );
	if(nRet!=0)
	{
		gErrorCode = 59; //model error
		stoptest();
	}
	nRet=0;
	// Check M7E-S3M
	if(gszCODENEGREV[10]==0x34 && gszCODENEGREV[11]==0x33)
	{
		nRet=1;
	}
	// Check M7E-S3M Aluminum
	if(gszCODENEGREV[10]==0x34 && gszCODENEGREV[11]==0x34)
	{
		nRet=2;
	}
	// check other D/S M7E-S3M
	if(gszCODENEGREV[10]==0x39 && gszCODENEGREV[11]==0x31)
	{
		nRet=3;
	}	
	if(gszCODENEGREV[10]==0x39 && gszCODENEGREV[11]==0x32)
	{
		nRet=4;
	}	
	if(gszCODENEGREV[10]==0x39 && gszCODENEGREV[11]==0x30)
	{
		nRet=5;
	}
	if(nRet!=0)
	{
		@WINSTAR_set_serialnumber();
		strcpy(gszCODEREV,"$UROM"); // cheat for MES ROM version check
		@ACPT_MES_Check();
		for(nRet=0;nRet<20;)
		{
			delays(1);
			nRet++;
		}
		gErrorCode = 59; //model error
		stoptest();
	}
//#--------------------------------------------------------------------
//# VERIFY BURNIN RESULTS.
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM);
	@WINSTAR_verify_burnin();
@MESSAGE_Step(NUM);
/*
	strcpy(PATTERN,"HSTR");	
	nRet=verify_signature(9,0,1,0x64,BY_TEXT|BY_FID);
	if(nRet!=0)
	{
		gErrorCode = 53; // final not run
		stoptest();
	}
*/
//#--------------------------------------------------------------------
//# MC modify before Maincode D/L
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); // clear 2nd S/N
	mcrd(rdbuf,7,0,2);		
	rdbuf[48]=0;rdbuf[49]=0;
	mcwr(rdbuf,7,0,2);
@MESSAGE_Step(NUM);
// change JIT FOD on time : 800us -> 1.6ms for BADW (091223)
	strcpy(PATTERN, "28");
	@WINSTAR_modify_sector(241,0,1,1,0x14,BY_HEXA|BY_FID);
//#---------------------------------------------------------------------
//# DOWNLOAD THE MAIN CODE BEFORE STARTING FINAL TEST.
//#---------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	strcpy(FILENAME,"$MCODE");		
	@WINSTAR_download_microcode(7);	
	@WINSTAR_power_off();                       
	delays(1);                                      

//#---------------------------------------------------------------------
//# POWER UP AND INITIALIZE DRIVE PARAMETERS.
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	LastPowerOnLimit = 17000;
	@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
@MESSAGE_Step(NUM); 
	@WINSTAR_set_PIO (4);              
	@WINSTAR_init(); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)                            
	strcpy(gszCODEREV,"$UROM"); // cheat for MES ROM version check
	@ACPT_MES_Check(); @WINSTAR_set_testoption(LOG_RESULT); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"$MODEL");
	@WINSTAR_verify_model();
@MESSAGE_Step(NUM);
	DEVICE_CONFIGURATION2(DCO_RESTORE,0);
@MESSAGE_Step(NUM); // clear profile page
	@WINSTAR_reserved(0x07,0,0,1); //UID clear
@MESSAGE_Step(NUM);
	// Restore default settings to be related FLASH
	// PUIS, Flash Flag, SATA Parameters
	// fmtu 0x2f1;
	@WINSTAR_reserved(0xF1,0,2,1); 
@MESSAGE_Step(NUM);
	@WINSTAR_set_smart(0xD8); // SMART Enable
@MESSAGE_Step(NUM);
	@WINSTAR_set_standby(0);
	@WINSTAR_power_off();                       
	delays(1);
@MESSAGE_Step(NUM); 
	LastPowerOnLimit = 17000;
	@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
@MESSAGE_Step(NUM); 
	@WINSTAR_set_PIO (4);              
	@WINSTAR_init(); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)
@MESSAGE_Step(NUM);
	@WINSTAR_set_features(0x00D7); // Transrate 3.0G
@MESSAGE_Step(NUM);
//	setfeatures(0x56,0x00,0x8D,0); //SSC disable
@MESSAGE_Step(NUM);
	setfeatures(0x56,0x01,0x05,0);// Init Staggered Spin-up
@MESSAGE_Step(NUM);
	setfeatures(0x56,0x00,0x09,0);// for speed limit with SATA Power Pin 11
@MESSAGE_Step(NUM);
	@WINSTAR_reserved(0x99,1,1,8); // Restore Pin 11 setting
	@WINSTAR_reserved(0x9D,0,0x0F,1); //ActivityIndicatorProfile to Normal
@MESSAGE_Step(NUM);
	@WINSTAR_reserved(0x97,0,0,1); // init & Enable  ATWH
	@WINSTAR_reserved(0x95,0,0,1); 
@MESSAGE_Step(NUM); 
	setUID(0x1232,0,0x0007); //UID_PM_CONTROL_FLAG, enable MRB control @ write mode(M7E S2P only)
@MESSAGE_Step(NUM);
	//block 4K sector
	strcpy(PATTERN,"0800"); 
	nRet=verify_signature(9,0,1,0x1BC,BY_HEXA|BY_FID);
	if(nRet==0)
	{
		for(nRet=0;nRet<20;)
		{
			delays(1);
			nRet++;
		}
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM); 
	setUID(0x3305,0,30); //UID_WV_COUNT_LUL	
@MESSAGE_Step(NUM);
	//monitor OW min value of per head	
	nWVTemp=17;
	mcrd(rdbuf,37,0,20);
	for(nCnt=0;nCnt<=MHD;)
	{
		nRet= rdbuf[0x400*nCnt+0x2FC] & 0xFF;
		nRet= nRet |((rdbuf[0x400*nCnt+0x2FD] & 0xFF)<<8);
		if(nRet<0x1FE && nRet!=0)     // oWT < 27dB
		{
			nWVTemp=20;
			break;
		}		
		nCnt++;
	}
	setUID(0x3301,0,nWVTemp); //UID_WV_LOW_TEMPERATURE
@MESSAGE_Step(NUM);
	//WV condition for 512 sector size
	setUID(0x330B,0,8); //UID_WV_FIRSTSECTOR_ECC 1stAvg
	setUID(0x330E,0,15); //UID_WV_FIRSTSECTOR_MAX 1stMax
	setUID(0x3324,0,18); //UID_WV_SECONDSECTOR_MAX 2ndMax
	setUID(0x330A,0,5); //UID_WV_SCAN_MODE_RETRY_4_RESET reset

	setUID(0x332B,0,7); //UID_WV_FIRSTSECTOR_ECC_PWRON  1stAvg for CSS, 0x332B
	setUID(0x332C,0,14); //UID_WV_FIRSTSECTOR_MAX_PWRON 1stAvg for CSS, 0x332B
	setUID(0x332D,0,18); //UID_WV_SECONDSECTOR_MAX_PWRON
@MESSAGE_Step(NUM);
	nGlistCnt=0x0A;
	setUID(0x3209,0,nGlistCnt); // UID_HIDDEN_GLIST_COUNT
	setUID(0x320A,0,16); // UID_GLIST_PROVISION_RETRY_INDEX
	setUID(0x320B,0,16); // UID_GLIST_VIRGIN_RETRY_INDEX
	setUID(0x4015,0,12); //UID_CACHE_MAX_READ_PEND_COUNT
	setUID(0x1331,0,0x1B58);// UID_APM_STAGE2_TIMER,Time to Low Power Idle : 8 sec->5 sec->7sec
	setUID(0x4021,0,6000); //UID_CACHE_MAX_POSTFETCH_AG, MAX_POSTFETCH_AG_4_EXT);
	setUID(0x4010,0,6000); //UID_CACHE_MAX_POST_FETCH, MAX_POSTFETCH_4_EXT);
// ATWH set
	nAtwhThres=5000;
/*
	mcrd(rdbuf,9,0,1);
	if(rdbuf[0x12A] == 18)
	{
		nAtwhThres=10000;   // atwh 10k
	}
	if(rdbuf[0x12A] == 21)
	{
        	nAtwhThres=5000;   // atwh 5k
	}   
	nRet=0;nAtwhCnt=9999;
	mcrd(rdbuf,11,0,2);
	for(nCnt=0;nCnt<=MHD;)
	{
		nRet=rdbuf[0x258+(nCnt*2)] & 0xFF;
		if(nAtwhCnt>nRet) nAtwhCnt=nRet;
		nCnt++;
	}
	if(nAtwhCnt>=3 && nAtwhCnt<5)
	{
		nAtwhThres=5000;   // atwh 5k	
	}
*/
	setUID(0x3311,0,nAtwhThres); //UID_ATWH_WRITE_THRESHOLD
	setUID(0x3316,0,8); //UID_ATWH_MAX_ADJUST_COUNT_FOR_FIT
	setUID(0x3317,0,3); //UID_ATWH_MIN_ADJUST_COUNT_FOR_FIT
	setUID(0x3310,0,0x00); //UID_ATWH_CONTROL_FLAG
	setUID(0x3312,0,3); // UID_ATWH_LBA_MARGIN
	setUID(0x3318,0,1024); //UID_ATWH_SECTOR_COUNT

	setUID(0x331D,0,17); // UID_ATWH_PENDING_LOW_TEMP
	setUID(0x331E,0,180); // UID_ATWH_PENDING_SPINON_TIME

	setUID(0x332A,0,1); // UID_WV_CONDTION_CHECK_MODE
@MESSAGE_Step(NUM);
	nFeatureFlag=0;
	//OEM_WRITE_VERIFY|OEM_ATWHEALING_MODE|OEM_WRITE_REAS_VERIFY|OEM_ENABLE_REDUCED_LOW_VDD
	nFeatureFlag= nFeatureFlag | 0x21044000; 
	nFeatureFlag= nFeatureFlag | 0x08; //OEM_NEC
	setUID(0x1221,0,nFeatureFlag);//UID_FEATURE_OPTION_FLAG;
@MESSAGE_Step(NUM); 
	setUID(0x301F,0,4500000); // ready time
@MESSAGE_Step(NUM); 
	@WINSTAR_reserved(0x9A,0,0,1); // EXT mode	
	@WINSTAR_set_standby(0);
	@WINSTAR_power_off();                       
	delays(1);
//#--------------------------------------------------------------------
//# CHECK WR/RD MULTIPLE (C5/C4) WITH DIFFERENT MULTIPLES/BLOCK COUNTS.
//#--------------------------------------------------------------------
//#--------------------------------------------------------------------
//# DATA COMPARE TEST - ESMT TEST
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	LastPowerOnLimit = 17000;
	@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
@MESSAGE_Step(NUM); 
	@WINSTAR_set_PIO (4);              
	@WINSTAR_init(); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)
@MESSAGE_Step(NUM); 
	@WINSTAR_set_timeout(7000); 
	@WINSTAR_set_retry(30);
@MESSAGE_Step(NUM); 
	@WINSTAR_set_UDMA(5); // UDMA MODE 5
	Lba48Bit = 1;
	@WINSTAR_set_datacompare(1); // DATACOMPARE ON
@MESSAGE_Step(NUM); 
	@WINSTAR_ESMT_test( 700, 0, 200000 );
@MESSAGE_Step(NUM); 
	M5S_BitCorruptionTest();
	Lba48Bit = 0;
	@WINSTAR_set_datacompare(0); // DATACOMPARE OFF

//##############################################################################
//##############################################################################
//## NON 48 BIT MODE
//##############################################################################
//##############################################################################
@MESSAGE_Step(NUM); 
	@WINSTAR_set_UDMA(4);                                                               
	@WINSTAR_set_pattern(0x0000);                                              
	@WINSTAR_set_datacompare(1); // ENABLE DATACOMPARE
@MESSAGE_Step(NUM); 
	@WINSTAR_write ( BY_LBA|BY_DMA, 0, 97, 256, 0);                                    
@MESSAGE_Step(NUM); 
	@WINSTAR_write ( BY_LBA|BY_DMA, 1001, 1256, 256, 0);                                    
@MESSAGE_Step(NUM); 
	@WINSTAR_write ( BY_LBA|BY_DMA, 9645, 9645, 256, 0);   //$40G
	@WINSTAR_write ( BY_LBA|BY_DMA, 14411, 14411, 256, 0); //$60G
	@WINSTAR_write ( BY_LBA|BY_DMA, 19180, 19180, 256, 0); //$80G
@MESSAGE_Step(NUM); 
	@WINSTAR_write ( BY_LBA|BY_DMA, 19195, 19195, 256, 0);                                    
@MESSAGE_Step(NUM); 
	@WINSTAR_read ( BY_CHS|BY_DMA, 0,  200, 256, 0);               
@MESSAGE_Step(NUM); 
	@WINSTAR_read ( BY_CHS|BY_DMA, 1500,  1550, 256, 0);               
@MESSAGE_Step(NUM); 
	@WINSTAR_read ( BY_CHS|BY_DMA, 3000,  3050, 256, 0);               
@MESSAGE_Step(NUM); 
	@WINSTAR_read ( BY_CHS|BY_DMA, 5000,  5050, 256, 0);               
@MESSAGE_Step(NUM);     
	@WINSTAR_read ( BY_CHS|BY_DMA, 10000,  10050, 256, 0);               
@MESSAGE_Step(NUM);     
	@WINSTAR_read ( BY_CHS|BY_DMA, 16000,  16050, 256, 0);               
@MESSAGE_Step(NUM);     
	Lba48Bit = 0;                                           
	srand(0x5555);
	@WINSTAR_write (BY_CHS|BY_DMA|BY_RANDOM|BY_BIT_CHECK, 0, 100, 256, 200);
@MESSAGE_Step(NUM); 
	srand(0x5555);
	@WINSTAR_read (BY_CHS|BY_DMA|BY_RANDOM|BY_BIT_CHECK, 0, 100, 256, 200);
	@WINSTAR_set_pattern(0x0000);
@MESSAGE_Step(NUM); 
	@WINSTAR_write ( BY_CHS|BY_DMA, 0, 150, 256, 0); 
@MESSAGE_Step(NUM); 
	@WINSTAR_read ( BY_CHS|BY_DMA, 0, 150, 256, 0);
	@WINSTAR_set_datacompare(0); // DATA COMPARE OFF
@MESSAGE_Step(NUM); 
	@WINSTAR_set_PIO (4); // PIO 4              
	@WINSTAR_set_pattern(0x0000);
	@WINSTAR_set_datacompare(1); // ENABLE DATACOMPARE
	@WINSTAR_write (BY_CHS | BY_SINGLE , 500, 520, 32, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_read (BY_CHS | BY_SINGLE , 500, 520, 32, 0); 

//#--------------------------------------------------------------------
//# UDMA MODE TRANSFER CHECK.
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	@WINSTAR_set_pattern(0x0000);
	@WINSTAR_set_UDMA(0); // UDMA MODE 0
	@WINSTAR_write (BY_CHS | BY_DMA, 0, 10, 128, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_read (BY_CHS | BY_DMA, 0, 10, 128, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_set_UDMA(1); // UDMA MODE 1
	@WINSTAR_write (BY_CHS | BY_DMA, 500, 510, 64, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_read (BY_CHS | BY_DMA, 500, 510, 64, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_set_UDMA(2); // UDMA MODE 2
	@WINSTAR_write (BY_CHS | BY_DMA, 1000, 1010, 128, 0); 
@MESSAGE_Step(NUM); 
	@WINSTAR_read (BY_CHS | BY_DMA, 1000, 1010, 128, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_set_UDMA(3); // UDMA MODE 3
	@WINSTAR_write (BY_CHS | BY_DMA, 1500, 1510, 64, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_read (BY_CHS | BY_DMA, 1500, 1510, 64, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_set_UDMA(4); // UDMA MODE 4
	@WINSTAR_write (BY_CHS | BY_DMA, 2000, 2010, 128, 0); 
@MESSAGE_Step(NUM); 
	@WINSTAR_read (BY_CHS | BY_DMA, 2000, 2010, 128, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_set_UDMA(5); // UDMA MODE 5
	@WINSTAR_write (BY_CHS | BY_DMA, 2500, 2510, 64, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_read (BY_CHS | BY_DMA, 2500, 2510, 64, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_set_UDMA(6); // UDMA MODE 6
	@WINSTAR_write (BY_CHS | BY_DMA, 3000, 3050, 64, 0); 
@MESSAGE_Step(NUM); 
	@WINSTAR_read (BY_CHS | BY_DMA, 3000, 3050, 64, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_set_UDMA(7); // UDMA MODE 7
	@WINSTAR_write (BY_CHS | BY_DMA, 3500, 3550, 128, 0);
@MESSAGE_Step(NUM); 
	@WINSTAR_read (BY_CHS | BY_DMA, 3500, 3550, 128, 0);

//#--------------------------------------------------------------------
//# CHECK TO ENSURE SYS CYL READ OK.
//#--------------------------------------------------------------------
@MESSAGE_Step(NUM); 
	@WINSTAR_set_datacompare(0); // DATA COMPARE OFF
	Lba48Bit = 0;
    	@WINSTAR_read (BY_CHS | BY_VENDOR, 256, 1000, 64, 0);
@MESSAGE_Step(NUM);
	if(MHD>2) MHD=2;
	Lba48Bit = 1;
	@WINSTAR_set_direction(0); 
	@WINSTAR_set_pattern(0x0000);
	@WINSTAR_read(BY_CHS | BY_VENDOR | BY_MCYL | BY_TRACK, 1, 63, MAX_BLKCNT,0);
	@WINSTAR_set_standby(0);
	@WINSTAR_power_off(); 
	delays(1);

//############################################################################
//# NEW AT TEST                                                               
//############################################################################
@MESSAGE_Step(NUM); 
	LastPowerOnLimit = 17000;
	@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
	@WINSTAR_set_PIO (4);
	@WINSTAR_init();             
@MESSAGE_Step(NUM); 
	DEVICE_CONFIGURATION2(DCO_RESTORE,0);
	@WINSTAR_set_PIO (4);
@MESSAGE_Step(NUM);
	@WINSTAR_set_features(0xFE42); //AAM Fast
	Lba48Bit = 1; 
@MESSAGE_Step(NUM);
	@WINSTAR_set_serialnumber();
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"SAMSUNG");
	@WINSTAR_verify_model(); // CHECK TO VERIFY IDFY DATA & ATA INTF        
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"$MODEL");
	@WINSTAR_verify_model();
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"$UROM");
	@WINSTAR_verify_firmware(BY_MAIN);
@MESSAGE_Step(NUM); 
	strcpy(PATTERN,"$EROM");
	@WINSTAR_verify_firmware(BY_ENGINEERING); //$EROM
@MESSAGE_Step(NUM); 	                                          
	@WINSTAR_set_features(0x$AAMV); // AAM
@MESSAGE_Step(NUM); 
	@WINSTAR_set_retry(30); // SET RETRY LEVEL
	@WINSTAR_set_pattern(0x0000);                                              
	@WINSTAR_set_UDMA(7); // UDMA MODE 7
	@WINSTAR_set_datacompare(0); // DATA COMPARE OFF
	Lba48Bit = 0;
@MESSAGE_Step(NUM);
	@WINSTAR_write (BY_LBA|BY_DMA , 0, 2000, 256, 0);                                                             
@MESSAGE_Step(NUM); 
	@WINSTAR_read (BY_LBA|BY_DMA , 0, 2000, 256, 0);
@MESSAGE_Step(NUM);
	Trinity_Check_ALIST_Count(19,20);    
@MESSAGE_Step(NUM); 
	@WINSTAR_reserved(135,0,0,1); // SMART Initialize
	@WINSTAR_set_smart(0xDE); // SMART Initialize
	@WINSTAR_set_smart(0xD8); //$SMART_E
	@WINSTAR_set_smart(0xD9); //$SMART_D
	@WINSTAR_set_smart(0xD8); // $SMART_S
	@WINSTAR_set_smart(0xF1D2); // $SMART_S
@MESSAGE_Step(NUM); 
	@WINSTAR_set_standby(0);
	@WINSTAR_power_off();                       
	delays(7);                                      
@MESSAGE_Step(NUM);
	LastPowerOnLimit = 17000;
	@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
	@WINSTAR_set_PIO (4);
	@WINSTAR_init();
	    
@MESSAGE_Step(NUM);
	nV1=getUID(0x1232,0);
	nV2=getUID(0x3305,0);
	nV3=getUID(0x3209,0);
	nV4=getUID(0x1221,0);
	nV5=getUID(0x3301,0); //UID_WV_LOW_TEMPERATURE
	if(nV1!=7 || nV2!=30 || nV3!=nGlistCnt || nV4!=nFeatureFlag || nV5!=nWVTemp)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x320A,0); // UID_GLIST_PROVISION_RETRY_INDEX
	nV2=getUID(0x320B,0); // UID_GLIST_VIRGIN_RETRY_INDEX
	nV3=getUID(0x4015,0); //UID_CACHE_MAX_READ_PEND_COUNT
	nV4=getUID(0x1331,0); // UID_APM_STAGE2_TIMER
	nV5=getUID(0x3318,0); //UID_ATWH_SECTOR_COUNT
	if(nV1!=16 || nV2!=16 || nV3!=12 || nV4!=0x1B58 || nV5!=1024)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x330B,0); //UID_WV_FIRSTSECTOR_ECC 1stAvg
	nV2=getUID(0x330E,0); //UID_WV_FIRSTSECTOR_MAX 1stMax
	nV3=getUID(0x3324,0); //UID_WV_SECONDSECTOR_MAX 2ndMax
	nV4=getUID(0x330A,0); //UID_WV_SCAN_MODE_RETRY_4_RESET reset
	if(nV1!=8 || nV2!=15 || nV3!=18 || nV4!=5)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x332B,0); //UID_WV_FIRSTSECTOR_ECC_PWRON
	nV2=getUID(0x332C,0); //UID_WV_FIRSTSECTOR_MAX_PWRON
	nV3=getUID(0x332D,0); //UID_WV_SECONDSECTOR_MAX_PWRON
	if(nV1!=7 || nV2!=14 || nV3!=18)
	{
		gErrorCode = 137;	//sign error	
		stoptest();
	}	
@MESSAGE_Step(NUM);
	nV1=getUID(0x3311,0); //UID_ATWH_WRITE_THRESHOLD
	nV2=getUID(0x3316,0); //UID_ATWH_MAX_ADJUST_COUNT_FOR_FIT
	nV3=getUID(0x3317,0); //UID_ATWH_MIN_ADJUST_COUNT_FOR_FIT
	nV4=getUID(0x3310,0); //UID_ATWH_CONTROL_FLAG
	nV5=getUID(0x3312,0); //UID_ATWH_LBA_MARGIN
	if(nV1!=nAtwhThres || nV2!=8 || nV3!=3 || nV4!=0 || nV5!=3)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x331D,0); // UID_ATWH_PENDING_LOW_TEMP
	nV2=getUID(0x331E,0); // UID_ATWH_PENDING_SPINON_TIME
	nV3=getUID(0x301F,0); // ready time
	if(nV1!=17 || nV2!=180 || nV3!=4500000)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	strcpy(PATTERN, "B0F85009");             //$80G
	strcpy(PATTERN, "B04BF90D"); 		 //$120G
	strcpy(PATTERN, "B09EA112"); 		 //$160G
	strcpy(PATTERN, "70591C1D");                   //$250G
	strcpy(PATTERN, "B0EA4225");                   //$320G
	strcpy(PATTERN, "B090932E");                   //$400G
	strcpy(PATTERN, "3060383A");                   //$500G
	strcpy(PATTERN, "B082854A");              //$640G
	nRet=verify_signature(0,0,0,200,BY_HEXA|BY_IDFY);
	if(nRet!=0)
	{
		gErrorCode = 57; // Bad Capa Fail!!
		stoptest();
	}
@MESSAGE_Step(NUM);
	strcpy(PATTERN,"28"); 
	@WINSTAR_verify_signature(241,0,1,0x14,BY_HEXA|BY_FID);
@MESSAGE_Step(NUM);
	@WINSTAR_reserved(0x21,0,0,1); //screen temp sensor
	rtsk();
	TF_CL = TF_CL & 0xFF;
	if(TF_CL<=15)
	{	
		gErrorCode = 71; //Sens Error
		stoptest();		
	}

//############################################################################
//# CHECK TEST CONFIG - CHKUTS.TXT                                                               
//############################################################################

	//$INSERT_CHKSPT

@MESSAGE_Step(NUM); 
//$INSERT(INLINE=="YES","@ACPT_Pause();")
//$INSERT(INLINE=="YES"&&"S/N Write"!="D","@ACPT_Request_SN();")
//$INSERT(INLINE=="YES"&&"S/N Write"=="LO","@WINSTAR_change_pn2sn(4+2+1,0);")
//$INSERT(INLINE=="YES"&&"S/N Write"=="RO","@WINSTAR_change_pn2sn(4+2+1,1);")
//$INSERT(INLINE=="YES"&&"S/N Write"!="D","@WINSTAR_check_WWN();")
//$INSERT(INLINE=="YES"&&"PPID"=="E","@WINSTAR_Write_PPID();")

@MESSAGE_Step(NUM); 
//  s=0x8a;
//  setf(0x0156);    // disable loopback detection
	setfeatures(0x56,1,0x8A,0);
@MESSAGE_Step(NUM); 
	@WINSTAR_reserved(0x97,0,0,1); // init & Enable  ATWH
@MESSAGE_Step(NUM); 
	@WINSTAR_set_standby(0);
	delays(5);
	@MESSAGE_DrivePass(); // make report file
}
void Trinity_Check_ALIST_Count(int nMCId,int nOffs)
{
	int nReassignCnt;

	if(nMCId==0)
	{
		phrd(rdbuf,1,0,372,1);
	}
	else
	{
		mcrd(rdbuf,nMCId,0,1);
	}
	nReassignCnt=((rdbuf[nOffs+1]&0xFF)<<8)|(rdbuf[nOffs]&0xFF);
	if(nReassignCnt>1)
	{
		strcpy(StringParam1,"ALst");
		gErrorCode = 124;
		@MESSAGE_TEXT4();
		stoptest();
	}	
}
void M5S_BitCorruptionTest()
{
	int testLBA,testCount,testZone;
	int blockCount, distance;
	int pendCount;
	int nRetry,nRet;

	nRetry=0;

BIT_CORRUPTION_START:
	for (testZone=0;testZone<3;)
	{
		nRet=Soft_Reset();
		if(nRet!=0)
		{
			if(nRetry==0)
			{
				nRetry++;
				@WINSTAR_power_off();                       
				delays(1);
				LastPowerOnLimit = 17000;
				@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
				@WINSTAR_set_PIO (4);              
				@WINSTAR_init();		
				@WINSTAR_set_retry(30);
				@WINSTAR_set_UDMA(5); // UDMA MODE 5
				Lba48Bit = 1;
				goto BIT_CORRUPTION_START;
			}
			else
			{
				printf("\nSoft Reset Fail!!! (TF_ST=%02x)\n", TF_ST );
				gErrorCode = 211; // 78 TempErr3(Sreen)
				stoptest();
			}
			
		}
		testLBA = MAX_LBA/3*testZone;

		printf("Test Zone %02Xh \n", testZone);
		printf("Test SLBA %08Xh \n",testLBA);

		for (LBA=testLBA;LBA<testLBA+10000;)
		{
			fillsp(wdbuf,0,LBA,256);		
		        writedmaext(wdbuf,LBA,256);
        		LBA=LBA+256;
	        }
		for (LBA=testLBA;LBA<testLBA+10000;)
		{
			fillsp(wdbuf,0,LBA,256);
			readdmaext(rdbuf,LBA,256);
			LBA=LBA+256;
		}
		srand(0xFFFF);
		for (testCount=0;testCount<5;)
		{
			nRet=Soft_Reset();
			if(nRet!=0)
			{
				if(nRetry==0)
				{
					nRetry++;
					@WINSTAR_power_off();                       
					delays(1);
					LastPowerOnLimit = 17000;
					@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
					@WINSTAR_set_PIO (4);              
					@WINSTAR_init();		
					@WINSTAR_set_retry(30);
					@WINSTAR_set_UDMA(5); // UDMA MODE 5
					Lba48Bit = 1;
					goto BIT_CORRUPTION_START;
				}
				else
				{
					printf("\nSoft Reset Fail!!! (TF_ST=%02x)\n", TF_ST );
					gErrorCode = 211; // 78 TempErr3(Sreen)
					stoptest();
				}
			
			}
			rand(LBA,testLBA,testLBA+1000); 
			rand(blockCount,1,256);
			pendCount = 0;
			while(pendCount<10)
			{
				fillsp(wdbuf,0,LBA,blockCount);
	       		        writedmaext(wdbuf,LBA,blockCount);
                		rand(blockCount,1,10);
		                rand(distance,10,256);
		                LBA=LBA+distance;
        		        pendCount=pendCount+1;
            		}
			LBA=testLBA;
			while(LBA+256<=testLBA+1000)
			{
				fillsp(wdbuf,0,LBA,256);
				readdmaext(rdbuf,LBA,256);
				LBA=LBA+256;
			}
			testCount++;
        	}
		testZone++;
	}

	@WINSTAR_set_pattern(0x0000);
	for (testZone=0;testZone<3;)
	{
		testLBA = MAX_LBA/3*testZone;
		for (LBA=testLBA;LBA<testLBA+10000;)
		{	
			writedmaext(wdbuf,LBA,256);
			readdmaext(rdbuf,LBA,256);    
			LBA=LBA+256;
	        }
		srand(0xFFFF);
		for (testCount=0;testCount<5;)
		{
			rand(LBA,testLBA,testLBA+1000); 
			rand(blockCount,1,256);
			pendCount = 0;
			while(pendCount<10)
			{
				writedmaext(wdbuf,LBA,blockCount);
				readdmaext(wdbuf,LBA,blockCount);
				rand(blockCount,1,10);
				rand(distance,10,256);
				LBA=LBA+distance;
				pendCount=pendCount+1;
			}   
			testCount++;
		}
		testZone++;
	}

}
int Soft_Reset()
{
	int nCnt;

	softreset();
	for(nCnt=0; nCnt<30;)
	{
		rtsk();
		TF_ST = TF_ST & 0xFD;
		if ( TF_ST == 0x50 )
		{
			break;
		}
		printf("retry TF_ST=%02x\n", TF_ST );
        	delaym(100);
		nCnt++;
	}
	rtsk();
	TF_ST = TF_ST & 0xFD;
	if ( TF_ST != 0x50 )
	{
		return 1;		
	}
	return 0;
}
void NCQ_Error()
{
	gErrorCode = 210; // 77 screen(TempErr2)
	stoptest();	
}
int NCQ_Soft_Reset()
{
	int nCnt;

	for(nCnt=0; nCnt<3;)
	{
		softreset();
		rtsk();
		if(TF_ST != 0x40) break;
        	delaym(100);
		nCnt++;
	}

	for(nCnt=0; nCnt<30;)
	{
		TF_ST = TF_ST & 0xFD;
		if ( TF_ST == 0x50 )
		{
			break;
		}
		printf("retry TF_ST=%02x\n", TF_ST );
        	delaym(100);
		nCnt++;
	}
	rtsk();
	TF_ST = TF_ST & 0xFD;
	if ( TF_ST != 0x50 )
	{
		return 1;
	}
	return 0;
}

//################################# End of file ################################
