//##############################################################################
//#
//#                  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
//# --------  	--- -- ---- ---- ------- ---------------------
//# M7EHSTD0   	04 12 2011  Fabre - M7E HD
//# M7EHEXZ0   	04 13 2011  Fabre - 4K external
//#
//##############################################################################
//#               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();
void Soft_Reset();
void NCQ_Error();
void NCQ_Soft_Reset();
void LBACut_verify_model();

void main()
{
	int nRet,nCnt,nRetry,nWVTemp;	
	int nFeatureFlag,nFeatureFlag2,nAtwhThres,nAtwhCnt,nAtwhMaxCnt;
	int nFeatureFlag3;
	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();
@MESSAGE_Step(NUM); 
	if(gszCODENEGREV[10]!=0x34 || gszCODENEGREV[11]!=0x35)
	{
		if(gszCODENEGREV[10]!=0x34 || gszCODENEGREV[11]!=0x36)
		{
			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,0x78,BY_TEXT|BY_FID);
	if(nRet!=0)
	{
		gErrorCode = 53; // final not run
		stoptest();
	}
*/
//#--------------------------------------------------------------------
//# Restore P/N
//#--------------------------------------------------------------------
/*
@MESSAGE_Step(NUM);
	strcpy(PATTERN,"0000");	
	nRet=verify_signature(7,0,1,0x10,BY_HEXA|BY_FID);
	if(nRet==0)
	{
		usercmd(rdbuf,0x2D,2,0,0,0,0,0x9A,3,2); 
		memnmov(PATTERN,rdbuf,0,0x32,14,0);	
		PATTERN[14]=0;
		@WINSTAR_modify_sector(7,0,2,2,0x10,BY_TEXT|BY_FID);
	}
*/

//#--------------------------------------------------------------------
//# 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);

//#---------------------------------------------------------------------
//# 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(PATTERN,"$EROM");
	nRet = Check_ENGREV(); //$EROM
	if(nRet==0)
	{
		strcpy(FILENAME,"$MCODE");		
		@WINSTAR_download_microcode(7);	
		@WINSTAR_power_off();                       
		delays(1);                                      
		LastPowerOnLimit = 17000;
		@WINSTAR_power_on(STOP_BURNIN_); // POWER ON, SPINUP, & CHECK READY.
		@WINSTAR_set_PIO (4);              
		@WINSTAR_init(); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)
	}
	strcpy(PATTERN,"$EROM");
	nRet = Check_ENGREV(); //$EROM
	if ( nRet == 0 )
	{
		strcpy(StringParam1,"D/L ");
		gErrorCode = 35;
		@MESSAGE_TEXT4();
		stoptest();
	}
	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);
	// 250G LBA cut
//	setUID(0x2260,0,0x1D1C5970); //$250G
	// 500G LBA cut
//	setUID(0x2260,0,0x3A386030); //$500G
@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); // SMART log for FA
	@WINSTAR_set_smart(0xD8); // SMART Enable
@MESSAGE_Step(NUM);
	@WINSTAR_set_features(0x00D7); // Transrate 3.0G
@MESSAGE_Step(NUM);
	@WINSTAR_reserved(0x97,0,0,1); // init & Enable  ATWH
	@WINSTAR_reserved(0x95,0,0,1); 
@MESSAGE_Step(NUM);
	setfeatures(0x56,0x01,0x05,0);// Enable Staggered Spin-up
@MESSAGE_Step(NUM);
	setfeatures(0x56,0x00,0x09,0);// for speed limit with SATA Power Pin 11
	@WINSTAR_reserved(0x99,1,1,8); // Restore Pin 11 setting
	@WINSTAR_reserved(0x9D,0,0x0F,1); //ActivityIndicatorProfile to Normal
@MESSAGE_Step(NUM);	
	setUID(0x3305,0,30); //UID_WV_COUNT_LUL
@MESSAGE_Step(NUM);
	nWVTemp=10;
	setUID(0x3301,0,nWVTemp); //UID_WV_LOW_TEMPERATURE
@MESSAGE_Step(NUM);
	@WINSTAR_init(); // INIT (RST,UNLK,IDFY,RCAL,SETPARMS)
	nRet = rdbuf[303] & 0x0F;
	if(nRet==0)
	{
		for(nCnt=0;nCnt<=15;)
		{
			delays(1);
			nCnt++;
		}
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
    ////////////////////////////////////////////////////////////////////////
    // setting ECC scan symbol criteria for WV realization (512B/4K) (091223)
    ////////////////////////////////////////////////////////////////////////
	setUID(0x330B,0,50); //UID_WV_FIRSTSECTOR_ECC 1stAvg
	setUID(0x330E,0,120); //UID_WV_FIRSTSECTOR_MAX 1stMax
	setUID(0x3324,0,120); //UID_WV_SECONDSECTOR_MAX 2ndMax

	setUID(0x332B,0,50); //UID_WV_FIRSTSECTOR_ECC_PWRON // 1stAvg for CSS
	setUID(0x332C,0,120); //UID_WV_FIRSTSECTOR_MAX_PWRON // 1stMax for CSS
	setUID(0x332D,0,120); //UID_WV_SECONDSECTOR_MAX_PWRON // 2stMax for CSS

	setUID(0x330A,0,5); //UID_WV_SCAN_MODE_RETRY_4_RESET reset

	setUID(0x3305,0,100); //UID_WV_COUNT_LUL
	setUID(0x3329,0,100); //UID_WV_WRITE_COUNT
@MESSAGE_Step(NUM);
	setUID(0x3209,0,10); // UID_HIDDEN_GLIST_COUNT
	setUID(0x1232,0,0x0037); //UID_PM_CONTROL_FLAG, enable MRB off(M7E S2P only)
	setUID(0x320A,0,16); // UID_GLIST_PROVISION_RETRY_INDEX
	setUID(0x320B,0,16); // UID_GLIST_VIRGIN_RETRY_INDEX
@MESSAGE_Step(NUM); // set ATWH condition
	setUID(0x3316,0,8); //UID_ATWH_MAX_ADJUST_COUNT_FOR_FIT
	setUID(0x3317,0,4); //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
// ATWH set
	nAtwhThres=5000;
	setUID(0x3311,0,nAtwhThres); //UID_ATWH_WRITE_THRESHOLD

	setUID(0x331D,0,10); // 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); //set feature flag
//OEM_WRITE_VERIFY           0x00040000      //b18   enabled write verify
//OEM_ATWHEALING_MODE        0x00004000     //b14   enabled ATW healing
//OEM_WRITE_REAS_VERIFY      0x01000000      //b24   write fault reassign verification
//OEM_ENABLE_REDUCED_LOW_VDD 0x20000000      //b29    
	nFeatureFlag=0;
	nFeatureFlag= nFeatureFlag | 0x08; //OEM_NEC
	nFeatureFlag= nFeatureFlag | 0x21044000; 
	setUID(0x1221,0,nFeatureFlag);//UID_FEATURE_OPTION_FLAG;
@MESSAGE_Step(NUM);
// OEM_DISABLE_MC_WRITE_ON_VIB         0x00008000      //b15
// OEM_DISABLE_READ_REASSIGN_ON_VIB    0x00010000      //b16
// OEM_ENABLE_ADAPTIVE_SHOCK_THRES 	0x08000000
// OEM_ENABLE_BUMP_LOOSED_4_VIB		0x10000000
	nFeatureFlag2=getUID(0x1222,0);
	nFeatureFlag2=nFeatureFlag2|0x18018000; 
	nFeatureFlag2=nFeatureFlag2|0x00080000;  //OEM_FORCE_512B_IDENTIFY             0x00080000
	nFeatureFlag2=nFeatureFlag2|0x00800000;  //OEM_RMW_ERROR                       0x00800000
	setUID(0x1222,0,nFeatureFlag2); //UID_FEATURE_OPTION_FLAG2
@MESSAGE_Step(NUM);
//OEM_DISABLE_LOW_VDD_LOGGING       0x00000001      //b0    disabled low Vdd logging
	nFeatureFlag3=getUID(0x1223,0);
	nFeatureFlag3=nFeatureFlag3|0x00000001; 
	setUID(0x1223,0,nFeatureFlag3); //UID_FEATURE_OPTION_FLAG3	
@MESSAGE_Step(NUM);
	setUID(0x1331,0,7000);// UID_APM_STAGE2_TIMER,Time to Low Power Idle
@MESSAGE_Step(NUM);
	setUID(0x3330,0,1);//UID_ECC_VOTING_CONTROL
	setUID(0x3207,0,3);//UID_MAX_WRITE_RETRY_ON_VIB
@MESSAGE_Step(NUM);
	setUID(0x3041,0,0); //UID_SUPER_SECTOR_SUPPORT
	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);
@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); 
	strcpy(PATTERN,"$MODEL");
	@WINSTAR_verify_model();
@MESSAGE_Step(NUM); 
	@WINSTAR_set_timeout(10000); 
	@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, 128, 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);
	strcpy(PATTERN,"$MODEL");
	@WINSTAR_verify_model();	    
@MESSAGE_Step(NUM);
	nV1=getUID(0x3305,0); //UID_WV_COUNT_LUL
	nV2=getUID(0x3301,0); //UID_WV_LOW_TEMPERATURE
	nV3=getUID(0x330B,0); //UID_WV_FIRSTSECTOR_ECC 1stAvg
	nV4=getUID(0x330E,0); //UID_WV_FIRSTSECTOR_MAX 1stMax
	if(nV1!=100 || nV2!=nWVTemp || nV3!=50 || nV4!=120)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x3324,0); //UID_WV_SECONDSECTOR_MAX 2ndMax
	nV2=getUID(0x332B,0); //UID_WV_FIRSTSECTOR_ECC_PWRON // 1stAvg for CSS
	nV3=getUID(0x332C,0); //UID_WV_FIRSTSECTOR_MAX_PWRON // 1stMax for CSS
	nV4=getUID(0x332D,0); //UID_WV_SECONDSECTOR_MAX_PWRON // 2stMax for CSS
	nV5=getUID(0x330A,0); //UID_WV_SCAN_MODE_RETRY_4_RESET reset
	if(nV1!=120 || nV2!=50 || nV3!=120|| nV4!=120 || nV5!=5)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x3318,0); //UID_ATWH_SECTOR_COUNT
	nV2=getUID(0x3209,0); // UID_HIDDEN_GLIST_COUNT
	nV3=getUID(0x1232,0); //UID_PM_CONTROL_FLAG, enable MRB off(M7E S2P only)
	nV4=getUID(0x320A,0); // UID_GLIST_PROVISION_RETRY_INDEX
	nV5=getUID(0x320B,0); // UID_GLIST_VIRGIN_RETRY_INDEX
	if(nV1!=1024||nV2!=10 || nV3!=0x0037 || nV4!=16 || nV5!=16)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	//nV1=getUID(0x4015,0); //UID_CACHE_MAX_READ_PEND_COUNT
	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( nV2!=8 || nV3!=4 || nV4!=0 || nV5!=3)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x3311,0); //UID_ATWH_WRITE_THRESHOLD
	nV2=getUID(0x331D,0); // UID_ATWH_PENDING_LOW_TEMP
	nV3=getUID(0x331E,0); // UID_ATWH_PENDING_SPINON_TIME
	nV4=getUID(0x332A,0); //UID_WV_CONDTION_CHECK_MODE
	nV5=getUID(0x1221,0);//UID_FEATURE_OPTION_FLAG;
	if(nV1!=nAtwhThres || nV2!=10 || nV3!=180 || nV4!=1 || nV5!=nFeatureFlag)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x1331,0);// UID_APM_STAGE2_TIMER,Time to Low Power Idle
	nV2=getUID(0x3330,0);//UID_ECC_VOTING_CONTROL
//	nV3=getUID(0x3230,0);//UID_FORCED_VIRGIN_REASSIGN
	nV4=getUID(0x1223,0); //UID_FEATURE_OPTION_FLAG3	
	nV5=getUID(0x1222,0);//UID_FEATURE_OPTION_FLAG2;
	if(nV1!=7000 || nV2!=1  || nV4!=nFeatureFlag3 || nV5!=nFeatureFlag2)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x3207,0);//set max write retry conut 3 on vib for user data
	if(nV1!=3)
	{
		gErrorCode = 137;	//sign error	
		stoptest();		
	}
@MESSAGE_Step(NUM);
	nV1=getUID(0x3329,0); //UID_WV_WRITE_COUNT
	nV2=getUID(0x4021,0); //UID_CACHE_MAX_POSTFETCH_AG, MAX_POSTFETCH_AG_4_EXT);
	nV3=getUID(0x4010,0); //UID_CACHE_MAX_POST_FETCH, MAX_POSTFETCH_4_EXT);
//	nV4=getUID(0x3042,0); //UID_LSN_SEED4DATA
	nV5=getUID(0x3041,0); //UID_SUPER_SECTOR_SUPPORT
	if(nV1!=100 || nV2!=6000 || nV3!=6000 || nV5!=0)
	{
		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
	strcpy(PATTERN, "F0665457");                   //$750G
	strcpy(PATTERN, "B06D7074");                   //$1000G
	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,"23"); 
	@WINSTAR_verify_signature(241,0,1,0x14,BY_HEXA|BY_FID);
	strcpy(PATTERN,"9CFF"); 
	@WINSTAR_verify_signature(241,0,1,0x1E,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"&&"S/N Write"!="D"&&"PPID"=="E","@WINSTAR_Write_PPID();")
//$INSERT(INLINE=="YES"&&"S/N Write"!="D"&&"Component Write"=="E","@WINSTAR_Write_CCInfo();")

@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;

    @WINSTAR_set_UDMA(5); // UDMA MODE 5

    for (testZone=0;testZone<3;)
    {
	Soft_Reset();

        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;)
        {
		Soft_Reset();
		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++;
    }

}
void Soft_Reset()
{
	int nCnt;

	softreset();
	for(nCnt=0; nCnt<100;)
	{
		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 )
	{
		printf("\nSoft Reset Fail!!! (TF_ST=%02x)\n", TF_ST );
		strcpy(StringParam1,"Rst ");
		gErrorCode = 12;								// JJW 1.3g
		@MESSAGE_TEXT4();
		stoptest();
		
	}
	return;
}
void NCQ_Error()
{
	gErrorCode = 210; // 77 screen(TempErr2)
	stoptest();	
}
void NCQ_Soft_Reset()
{
	int nCnt;

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

	for(nCnt=0; nCnt<100;)
	{
		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 )
	{
		printf("\nSoft Reset Fail!!! (TF_ST=%02x)\n", TF_ST );
		NCQ_Error();	
	}
	return;
}
void LBACut_verify_model()
{
	int nRet,n250,n500;

	//strcpy(PATTERN,"SAMSUNG");
	strstr(PATTERN,"HM25",n250);
	strstr(PATTERN,"HM50",n500);

	if(n250!=0)
	{
		strstr(gszMODEL,"HM321HI",nRet);
		if(nRet!=0) return;
	}
	if(n500!=0)
	{
		strstr(gszMODEL,"HM641JI",nRet);
		if(nRet!=0) return;
	}
	@WINSTAR_verify_model();
}
//################################# End of file ################################
