#include "record.h"
#include "Runtime.h"
#include "MainCtl.h"

extern "C"{
	#include "user_norflash.h"
	#include "user_eeprom.h"
	#include "uart_key_drv.h"
	#include "task.h"
	#include "HW_config.h"
}

//쭯 512 Kbit (64 Kbytes) of EEPROM
//쭯 Page size: 128 bytes
//쭯 Byte Write within 5 ms
//쭯 Page Write within 5 ms

//쭯 1 Mbit (128 Kbyte) of EEPROM
//쭯 Page size: 256 byte

//Write Status Register Time		10	15	ms
//Page Program Time			0.7	3	ms
//Sector Erase Time (4KB)		45	400	ms
//Block Erase Time (32KB)		120	1,600	ms
//Block Erase Time (64KB)		150	2,000	ms

#define dWrite2FlashMaxCount			(2560)
#define dAddrSdWrite2FlashBufBase		(0xC3D00000)
#define dAddrSdWrite2FlashBufFire		(0xC3D00000)
#define dAddrSdWrite2FlashBufLinkage	(0xC3D30000)
#define dAddrSdWrite2FlashBufSv		(0xC3D60000)
#define dAddrSdWrite2FlashBufFault		(0xC3D90000)
#define dAddrSdWrite2FlashBufOperate	(0xC3DB0000)

#define dFlashFireChip				(0)
#define dFlashLinkageChip			(0)
#define dFlashFireBaseAddr 			(0x00010000)
#define dFlashLinkageBaseAddr 		(0x00180000)

#define dFlashSvChip				(1)
#define dFlashFaultChip				(1)
#define dFlashOperateChip			(1)
#define dFlashFaultBaseAddr			(0x00010000)
#define dFlashSvBaseAddr			(0x00180000)
#define dFlashOperateBaseAddr			(0x009C0000)

#define dFlashEntryCountPerPage	(4)
#define dFlashPageCountPerBlock	(256)
#define dFlashEntrySize			(0x40)
#define dFlashPageSize			(0x00000100)
#define dFlashBlockSize			(0x00010000)

#define deOperateEepRomCount	(static_cast<unsigned int>(4096))

#define EraseBlock64K_TryRusumeTime		20

#define dPRIMARY_KEY_WARNING_NUM		(0x6FFF'FFFF'FFFF'FFFF)

const unsigned char NwDescpUsingChip[68] = {
1,    
	0,0,0,0, 0,0,0,0,		0,0,0,0, 0,0,0,0,
	1,1,1,1, 1,1,1,1,		1,1,1,1, 1,1,1,1,
	1,1,1,1, 1,1,1,1,		1,1,1,1, 1,1,1,1,
	1,1,1,1, 1,1,1,1,		1,1,1,1, 1,1,1,1,		
1,1,1,	
};

const unsigned int NwDescpUsingFlashAddr[68] = {
0x900000,    	
			0x2F0000,0x350000,0x3B0000,0x410000, 0x470000,0x4D0000,0x530000,0x590000,	0x5F0000,0x650000,0x6B0000,0x710000, 0x770000,0x7D0000,0x830000,0x890000,
			0x2F0000,0x350000,0x3B0000,0x410000, 0x470000,0x4D0000,0x530000,0x590000,	0x5F0000,0x650000,0x6B0000,0x710000, 0x770000,0x7D0000,0x830000,0x890000,
			0x2F0000,0x350000,0x3B0000,0x410000, 0x470000,0x4D0000,0x530000,0x590000,	0x5F0000,0x650000,0x6B0000,0x710000, 0x770000,0x7D0000,0x830000,0x890000,
			0x2F0000,0x350000,0x3B0000,0x410000, 0x470000,0x4D0000,0x530000,0x590000,	0x5F0000,0x650000,0x6B0000,0x710000, 0x770000,0x7D0000,0x830000,0x890000,
	
0x900000,	0x900000,	0x900000
};

void TRecord::Init()
{
	unsigned int i, ar,x;
	iMsgRx.D32[0] = 0;
	iMsgRx.D32[1] = 0;
	iMsgRx.D32[2] = 0;
	iMsgRx.D32[3] = 0;
	
	ar = dAddrSdWrite2FlashBufBase;
	for(i=0; i<(dWrite2FlashMaxCount); i++){
		for(x=0; x<6; x++)*(volatile unsigned int*)(ar + (i*24 + x*4)) = 0x00000000;
	}
	ar = dAddrSdWrite2FlashBufLinkage;
	for(i=0; i<(dWrite2FlashMaxCount); i++){
		for(x=0; x<6; x++)*(volatile unsigned int*)(ar + (i*24 + x*4)) = 0x00000000;
	}
	ar = dAddrSdWrite2FlashBufSv;
	for(i=0; i<(dWrite2FlashMaxCount); i++){
		for(x=0; x<6; x++)*(volatile unsigned int*)(ar + (i*24 + x*4)) = 0x00000000;
	}
	ar = dAddrSdWrite2FlashBufFault;
	for(i=0; i<(dWrite2FlashMaxCount); i++){
		for(x=0; x<6; x++)*(volatile unsigned int*)(ar + (i*24 + x*4)) = 0x00000000;
	}
	ar = dAddrSdWrite2FlashBufOperate;
	for(i=0; i<(dWrite2FlashMaxCount); i++){
		for(x=0; x<6; x++)*(volatile unsigned int*)(ar + (i*24 + x*4)) = 0x00000000;
	}
}

void TRecord::DelayMs(unsigned int aMs)
{
	volatile unsigned int mTick;
	volatile unsigned int Tick;
	for(Tick=0; Tick<aMs; Tick++){
		for(mTick=0; mTick<160000; mTick++);
		sys_error_led_task();
	}
}

int TRecord::CheckHistoryCSum(unsigned char *p)
{
	int i; 

	aCsum = 0;
	for(i=0; i<60; i++){
		aCsum += p[i];
	}
	Actu.D8[0] = p[60];
	Actu.D8[1] = p[61];
	Actu.D8[2] = p[62];
	Actu.D8[3] = p[63];

	if(aCsum == Actu.D32)return 1;
	else return 0;
}

void TRecord::WaitChipIdle(unsigned int ChipX)
{
	volatile unsigned int Sta;
	do{
		DelayMs(1);
		Sta = nor_get_busy_state(ChipX);
	}while(Sta);
}
			
int TRecord::InnerMsgGetIfReadOk(unsigned int aIndex)
{
	if(iMsgRx.Splite.iByte0 == 0xFF){
		return 0;
	}else{
		return 1;
	}
}	

unsigned char TRecord::InnerMsgGetYear(unsigned int aIndex) 	{ return (iMsgRx.Splite.Year);}
unsigned char TRecord::InnerMsgGetMonth(unsigned int aIndex)	{ return (iMsgRx.Splite.Month);}
unsigned char TRecord::InnerMsgGetDay(unsigned int aIndex)		{ return (iMsgRx.Splite.Day);} 	
unsigned char TRecord::InnerMsgGetHour(unsigned int aIndex) 	{ return (iMsgRx.Splite.Hour);}
unsigned char TRecord::InnerMsgGetMinute(unsigned int aIndex)	{ return (iMsgRx.Splite.Minute);}
unsigned char TRecord::InnerMsgGetSecond(unsigned int aIndex)	{ return (iMsgRx.Splite.Second);}	
unsigned char TRecord::InnerMsgGet_mType(unsigned int aIndex)	{ return (iMsgRx.Splite.mType);}
unsigned char TRecord::InnerMsgGet_dType(unsigned int aIndex)	{ return (iMsgRx.Splite.dType);}	
unsigned char TRecord::InnerMsgGet_CtlNum(unsigned int aIndex)	{ return (iMsgRx.Splite.PCtlNum);}	
unsigned char TRecord::InnerMsgGet_P0(unsigned int aIndex)		{ return (iMsgRx.Splite.P0);}
unsigned char TRecord::InnerMsgGet_P1(unsigned int aIndex)		{ return (iMsgRx.Splite.P1);}
unsigned char TRecord::InnerMsgGet_P2(unsigned int aIndex)		{ return (iMsgRx.Splite.P2);}
unsigned char TRecord::InnerMsgGet_ValueHi(unsigned int aIndex)	{ return (iMsgRx.Splite.Data0);}
unsigned char TRecord::InnerMsgGet_ValueLow(unsigned int aIndex) 	{ return (iMsgRx.Splite.Data1);}
			
void TRecord::InnerMsgSetYear		(unsigned int aIndex, unsigned char aYear)    	{ iMsgRx.Splite.Year = aYear;}
void TRecord::InnerMsgSetMonth	(unsigned int aIndex, unsigned char aMonth)   	{ iMsgRx.Splite.Month = aMonth;}
void TRecord::InnerMsgSetDay		(unsigned int aIndex, unsigned char aDay)     	{ iMsgRx.Splite.Day = aDay;}
void TRecord::InnerMsgSetHour		(unsigned int aIndex, unsigned char aHour)	{ iMsgRx.Splite.Hour = aHour;}
void TRecord::InnerMsgSetMinute	(unsigned int aIndex, unsigned char aMinute)	{ iMsgRx.Splite.Minute = aMinute;}
void TRecord::InnerMsgSetSecond	(unsigned int aIndex, unsigned char aSecond)	{ iMsgRx.Splite.Second = aSecond;}	
void TRecord::InnerMsgSet_mType	(unsigned int aIndex, unsigned char aMtype) 	{ iMsgRx.Splite.mType = aMtype;}	
void TRecord::InnerMsgSet_dType	(unsigned int aIndex, unsigned char aDtype)	{ iMsgRx.Splite.dType = aDtype;}	
void TRecord::InnerMsgSet_CtlNum	(unsigned int aIndex, unsigned char aCtlNum)	{ iMsgRx.Splite.PCtlNum = aCtlNum;}	
void TRecord::InnerMsgSet_P0		(unsigned int aIndex, unsigned char aP0)		{ iMsgRx.Splite.P0 = aP0;}	
void TRecord::InnerMsgSet_P1		(unsigned int aIndex, unsigned char aP1)		{ iMsgRx.Splite.P1 = aP1;}	
void TRecord::InnerMsgSet_P2		(unsigned int aIndex, unsigned char aP2)		{ iMsgRx.Splite.P2 = aP2;}	
void TRecord::InnerMsgSet_Data0	(unsigned int aIndex, unsigned char aVh)		{ iMsgRx.Splite.Data0 = aVh;}	
void TRecord::InnerMsgSet_Data1	(unsigned int aIndex, unsigned char aVl)		{ iMsgRx.Splite.Data1 = aVl;}	
void TRecord::InnerMsgSet_iByte0	(unsigned int aIndex, unsigned char aB)		{ iMsgRx.Splite.iByte0 = aB;}	
void TRecord::InnerMsgSet_iByte1	(unsigned int aIndex, unsigned char aB)		{ iMsgRx.Splite.iByte1 = aB;}	


void TRecord::FlushToWriteBuf()
{
	int i;
	for(i=0; i<4; i++){
		ToWriteBuf[i].D32[ 0]=0xFFFFFFFF;	ToWriteBuf[i].D32[ 1]=0xFFFFFFFF;	ToWriteBuf[i].D32[ 2]=0xFFFFFFFF;	ToWriteBuf[i].D32[ 3]=0xFFFFFFFF;
		ToWriteBuf[i].D32[ 4]=0xFFFFFFFF;	ToWriteBuf[i].D32[ 5]=0xFFFFFFFF;	ToWriteBuf[i].D32[ 6]=0xFFFFFFFF;	ToWriteBuf[i].D32[ 7]=0xFFFFFFFF;
		ToWriteBuf[i].D32[ 8]=0xFFFFFFFF;	ToWriteBuf[i].D32[ 9]=0xFFFFFFFF;	ToWriteBuf[i].D32[10]=0xFFFFFFFF;	ToWriteBuf[i].D32[11]=0xFFFFFFFF;
		ToWriteBuf[i].D32[12]=0xFFFFFFFF;	ToWriteBuf[i].D32[13]=0xFFFFFFFF;	ToWriteBuf[i].D32[14]=0xFFFFFFFF;	ToWriteBuf[i].D32[15]=0xFFFFFFFF;
	}
}

//______________________________________________________________________________>>>>>
void TRecord::FireAdd(TRecordCommon aRC)
{
	unsigned int SdramAddr;
	Buf4Fire.WriteIndex++;
	if(Buf4Fire.WriteIndex >= dWrite2FlashMaxCount){
		Buf4Fire.WriteIndex = 0;
	}
	SdramAddr = dAddrSdWrite2FlashBufFire + (Buf4Fire.WriteIndex * 24 );
	*(volatile unsigned int*)(SdramAddr +  0) = aRC.D32[0];
	*(volatile unsigned int*)(SdramAddr +  4) = aRC.D32[1];
	*(volatile unsigned int*)(SdramAddr +  8) = aRC.D32[2];
	*(volatile unsigned int*)(SdramAddr + 12) = aRC.D32[3];
	*(volatile unsigned int*)(SdramAddr + 16) = aRC.D32[4];
	*(volatile unsigned int*)(SdramAddr + 20) = aRC.D32[5];

	DoPrint.FillFireText(aRC);
	ExRecord.iExRecordFireInsert(aRC);
	FecBusCRT.iFecBusCRTFireInsert(aRC);
	
	//Conv2Utf8Text(aRC);
}

void TRecord::LinkageAdd(TRecordCommon aRC)
{
	unsigned int SdramAddr;
	Buf4Linkage.WriteIndex++;
	if(Buf4Linkage.WriteIndex >= dWrite2FlashMaxCount){
		Buf4Linkage.WriteIndex = 0;
	}
	SdramAddr = dAddrSdWrite2FlashBufLinkage + (Buf4Linkage.WriteIndex * 24 );
	*(volatile unsigned int*)(SdramAddr +  0) = aRC.D32[0];
	*(volatile unsigned int*)(SdramAddr +  4) = aRC.D32[1];
	*(volatile unsigned int*)(SdramAddr +  8) = aRC.D32[2];
	*(volatile unsigned int*)(SdramAddr + 12) = aRC.D32[3];
	*(volatile unsigned int*)(SdramAddr + 16) = aRC.D32[4];
	*(volatile unsigned int*)(SdramAddr + 20) = aRC.D32[5];
	
	DoPrint.FillLinkageText(aRC);
	ExRecord.iExRecordLinkageInsert(aRC);
	FecBusCRT.iFecBusCRTLinkageInsert(aRC);
}

void TRecord::SvAdd(TRecordCommon aRC)
{
	unsigned int SdramAddr;
	Buf4Sv.WriteIndex++;
	if(Buf4Sv.WriteIndex >= dWrite2FlashMaxCount){
		Buf4Sv.WriteIndex = 0;
	}
	SdramAddr = dAddrSdWrite2FlashBufSv + (Buf4Sv.WriteIndex * 24 );
	*(volatile unsigned int*)(SdramAddr +  0) = aRC.D32[0];
	*(volatile unsigned int*)(SdramAddr +  4) = aRC.D32[1];
	*(volatile unsigned int*)(SdramAddr +  8) = aRC.D32[2];
	*(volatile unsigned int*)(SdramAddr + 12) = aRC.D32[3];
	*(volatile unsigned int*)(SdramAddr + 16) = aRC.D32[4];
	*(volatile unsigned int*)(SdramAddr + 20) = aRC.D32[5];	
	
	DoPrint.FillSvText(aRC);
	ExRecord.iExRecordSvInsert(aRC);
	FecBusCRT.iFecBusCRTSvInsert(aRC);
}

void TRecord::FaultAdd(TRecordCommon aRC)
{
	unsigned int SdramAddr;
	Buf4Fault.WriteIndex++;
	if(Buf4Fault.WriteIndex >= dWrite2FlashMaxCount){
		Buf4Fault.WriteIndex = 0;
	}
	SdramAddr = dAddrSdWrite2FlashBufFault + (Buf4Fault.WriteIndex * 24 );
	*(volatile unsigned int*)(SdramAddr +  0) = aRC.D32[0];
	*(volatile unsigned int*)(SdramAddr +  4) = aRC.D32[1];
	*(volatile unsigned int*)(SdramAddr +  8) = aRC.D32[2];
	*(volatile unsigned int*)(SdramAddr + 12) = aRC.D32[3];
	*(volatile unsigned int*)(SdramAddr + 16) = aRC.D32[4];
	*(volatile unsigned int*)(SdramAddr + 20) = aRC.D32[5];
	
	DoPrint.FillFaultText(aRC);
	ExRecord.iExRecordFaultInsert(aRC);
	FecBusCRT.iFecBusCRTFaultInsert(aRC);
}

void TRecord::OperateAdd(TRecordCommon aRC)
{
	unsigned int addr;
	Buf4Operate.WriteIndex++;
	if(Buf4Operate.WriteIndex >= dWrite2FlashMaxCount){
		Buf4Operate.WriteIndex = 0;
	}
	addr = dAddrSdWrite2FlashBufOperate + (Buf4Operate.WriteIndex * 24);
	*(volatile unsigned int*)(addr +  0) = aRC.D32[0];
	*(volatile unsigned int*)(addr +  4) = aRC.D32[1];
	*(volatile unsigned int*)(addr +  8) = aRC.D32[2];
	*(volatile unsigned int*)(addr + 12) = aRC.D32[3];
	*(volatile unsigned int*)(addr + 16) = aRC.D32[4];
	*(volatile unsigned int*)(addr + 20) = aRC.D32[5];
	
	DoPrint.FillOperateText(aRC);
	ExRecord.iExRecordOperateInsert(aRC);
	FecBusCRT.iFecBusCRTOperateInsert(aRC);
}

void TRecord::OperateAddFromNw(TRecordCommon aRC)
{
	unsigned int addr;
	Buf4Operate.WriteIndex++;
	if(Buf4Operate.WriteIndex >= dWrite2FlashMaxCount){
		Buf4Operate.WriteIndex = 0;
	}
	addr = dAddrSdWrite2FlashBufOperate + (Buf4Operate.WriteIndex * 24);
	*(volatile unsigned int*)(addr +  0) = aRC.D32[0];
	*(volatile unsigned int*)(addr +  4) = aRC.D32[1];
	*(volatile unsigned int*)(addr +  8) = aRC.D32[2];
	*(volatile unsigned int*)(addr + 12) = aRC.D32[3];
	*(volatile unsigned int*)(addr + 16) = aRC.D32[4];
	*(volatile unsigned int*)(addr + 20) = aRC.D32[5];
	
	DoPrint.FillOperateText(aRC);
	ExRecord.iExRecordOperateInsert(aRC);
	FecBusCRT.iFecBusCRTOperateInsert(aRC);
}

void TRecord::TryFireWriteToFlash()
{
/*	unsigned int SdramAddr;
	unsigned int NewFound =0;
	unsigned int aWI = 0;
	unsigned int aPage;
	unsigned int aSector;
	unsigned int aState;
	unsigned int FlashAddr;
	unsigned int ToWriteBytesCount;
	unsigned int aIndex;
	
	aState = nor_get_busy_state(0); 
	if(aState){
		//Check Not Done, then 
		WriteTickChipF1 = 5;
		return;
	}	

	aState = CheckFireNextSector();
	if(aState){
		aIndex = BufFire.Writing_64KBlockIndex +1;
		if(aIndex > (dFrashUsingBlockCount-1)){
			aIndex = 0;
		}
		//Erase Next Sector Here, and Set Chip0 State to Erasing 64K Sector
	}
	
	if(BufFire.WritePageChanged){
		aState = CheckChipF1Idle();
		if(aState){
			//Not Idle return
			return;
		}
		aPage = BufFire.NowPage + 1;
		if(aPage > 15){
			aPage = 0;
			aSector = BufFire.NowSector + 1;
			if(aSector > (dfFireSectorCount - 1)){
				aSector = 0;
			}
			
			//Read and Check Next Sector if Must Be Erase
			FlashAddr= (aSector * 4096) + dfFire;
			aState = CheckChipF1_SectorEmpty(FlashAddr);
			
			if(aState){
				//Not Need To Erase
				BufFire.NowPage = aPage;
				BufFire.NowSector = aSector;
			}else{
				//Erase Here 
				//Do Erase
				BufFire.NowPage = aPage;
				BufFire.NowSector = aSector;
				BufFire.Erasing = 1;
				WriteTickChipF1 = 50;
			}
		}else{
			BufFire.NowPage = aPage;
		}
		BufFire.WritePageChanged = 0;
		return;
	}
	
	if(BufFire.IndexWriteFlash != BufFire.IndexAddOn){
		FlushToWriteBuf();
	}
	aWI = BufFire.IndexWriteFlash;
	if(aWI != BufFire.IndexAddOn){
		aIndex = BufFire.Writing_ItemIndex;
		for(; aIndex <16; aIndex++){
			
			
			
			
			
			aWI++;
			if(aWI > dfOperateItemsCount)aWI = 0;
			
			SdramAddr = aWI * 16 + daSdramFire;
			ToWriteBuf[aIndex][0] = *(unsigned int*)(SdramAddr + 0x00);
			ToWriteBuf[aIndex][1] = *(unsigned int*)(SdramAddr + 0x04);
			ToWriteBuf[aIndex][2] = *(unsigned int*)(SdramAddr + 0x08);
			ToWriteBuf[aIndex][3] = *(unsigned int*)(SdramAddr + 0x0C);
			NewFound++;
			aIndex++;
			if(aIndex > 15){
				BufFire.WritePageChanged =1;
				aIndex = 0;
				break;
			}
		}
		//
		
	}
	
	if(NewFound){
		//get flash addr and to write Length
		ToWriteBytesCount = NewFound * 16;
		BufFire.FlashPageAddr = (BufFire.NowSector * 4096) + (BufFire.NowPage * 256) + (BufFire.IndexInPage * 16) + dfFire;
		//Write Flash Here
		if(1){
			//write succes
			BufFire.IndexWriteFlash = aWI;
			BufFire.IndexInPage = aIndex;
			WriteTickChipE1 = 5;
		}
	}*/
}

void TRecord::LoadOperateEventDateTimeFromCharPointer(unsigned char *aDt)
{
	eOperateEvent.Splite.Year 	= *aDt++;
	eOperateEvent.Splite.Month 	= *aDt++;
	eOperateEvent.Splite.Day 	= *aDt++;
	eOperateEvent.Splite.Hour 	= *aDt++;
	eOperateEvent.Splite.Minute 	= *aDt++;
	eOperateEvent.Splite.Second 	= *aDt++;	
}

//______________________________________________________________________________>>>>>
void TRecord::OperateAdd_PowerOff(unsigned char aCn, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otPowerOff;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_PowerUp(unsigned char aCn, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otPowerOn;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_Reset(unsigned char aCn, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otReset;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_Mute(unsigned char aCn, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otMute;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_Mask(TDMix aFullPath, unsigned char aDType, unsigned char *aDt, unsigned int aUserCode, unsigned short aAreaNum)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otMask;
	eOperateEvent.Splite.dType = aDType;
	
	eOperateEvent.Splite.UserCode = aUserCode;
	eOperateEvent.Splite.AreaNum = aAreaNum;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_UnMask(TDMix aFullPath, unsigned char aDType, unsigned char *aDt, unsigned int aUserCode, unsigned short aAreaNum)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otUnMask;
	eOperateEvent.Splite.dType = aDType;
	
	eOperateEvent.Splite.UserCode = aUserCode;
	eOperateEvent.Splite.AreaNum = aAreaNum;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_Registe(TDMix aFullPath, unsigned char aDType, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otRegiste;
	eOperateEvent.Splite.dType = aDType;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_UnRegiste(TDMix aFullPath, unsigned char aDType, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otUnRegiste;
	eOperateEvent.Splite.dType = aDType;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_SetCanNetwork(unsigned char aCn, unsigned char sAddr, unsigned char IsMaster, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = sAddr;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otSetCanNetwork;
	eOperateEvent.Splite.Data1 = IsMaster;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_CfgBoard(TDMix aFullPath, unsigned char aDType, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgBoard;
	eOperateEvent.Splite.dType = aDType;

	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_CfgCrt(unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgCrt;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_CfgPriter(unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgPriter;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_CfgPritEventType(unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgPriter;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_CfgDevice(TDMix aFullPath, unsigned char aDType, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgDevice;
	eOperateEvent.Splite.dType = aDType;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_CfgDateTime(u8 aY, u8 aM, u8 aD, u8 aH, u8 aMin, u8 aSec, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.Splite.PCtlNum = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgDateTime;
	
	eOperateEvent.Splite.P0 = aY;
	eOperateEvent.Splite.P1 = aM;
	eOperateEvent.Splite.P2 = aD;
	eOperateEvent.Splite.Data1 = aH;
	eOperateEvent.Splite.iByte0 = aMin;
	eOperateEvent.Splite.iByte1 = aSec;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_CfgPermissionAllow(unsigned char aPa, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgPermissionAllow;
	eOperateEvent.Splite.Data1 = aPa;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_AllowState(unsigned char aCn, unsigned char aAllowAuto, unsigned char aAllowManual, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otAllowSet;
	eOperateEvent.Splite.Data1 = 0;
	if(aAllowAuto)eOperateEvent.Splite.Data1 |= 0x01;
	if(aAllowManual)eOperateEvent.Splite.Data1 |= 0x02;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_AlramSoundOn(unsigned char aCn, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otAlramSetOnOff;
	eOperateEvent.Splite.Data1 = 1;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_AlramSoundOff(unsigned char aCn, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otAlramSetOnOff;
	eOperateEvent.Splite.Data1 = 0;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_BusPadOn(unsigned char aCn, unsigned char aP0, unsigned char aP1, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otBusPadOn;
	eOperateEvent.Splite.Data1 = 0;
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_BusPadOff(unsigned char aCn, unsigned char aP0, unsigned char aP1, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otBusPadOff;
	eOperateEvent.Splite.Data1 = 0;
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_DirectPadManualAllowOn(unsigned char aCn, unsigned char aP0, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otDirectPadManualAllowOn;
	eOperateEvent.Splite.Data1 = 0;
	
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_DirectPadManualAllowOff(unsigned char aCn, unsigned char aP0, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otDirectPadManualAllowOff;
	eOperateEvent.Splite.Data1 = 0;
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_DirectPadNodeOn(unsigned char aCn, unsigned char aP0, unsigned char aP1, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otDirectPadNodeOn;
	eOperateEvent.Splite.Data1 = 0;
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_DirectPadNodeOff(unsigned char aCn, unsigned char aP0, unsigned char aP1, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otDirectPadNodeOff;
	eOperateEvent.Splite.Data1 = 0;
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_UserCheck(unsigned char aCn, unsigned char OnOff, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otUserCheck;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = OnOff;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_LinkageGo(unsigned char aCn, unsigned char *aDt)
{
	LoadOperateEventDateTimeFromCharPointer(aDt);
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otLinkageGo;
	eOperateEvent.Splite.Data1 = 3;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
}
//____________________________<>

void TRecord::OperateAdd_PowerOff(unsigned char aCn)
{
	unsigned char Data[6];
	unsigned char cVal, cValOnes;
	DelayMs(10);
	eeprom_read_byte(0,dEepAddrCtlShutDownTime,Data,6);
	eOperateEvent.Splite.Year 	= Data[5];
	eOperateEvent.Splite.Month 	= Data[4];
	eOperateEvent.Splite.Day 	= Data[3];
	eOperateEvent.Splite.Hour 	= Data[2];
	eOperateEvent.Splite.Minute 	= Data[1];
	eOperateEvent.Splite.Second 	= Data[0];
	
	cVal = eOperateEvent.Splite.Year / 16 * 10;
	cValOnes = eOperateEvent.Splite.Year % 16;
	if(cValOnes >9)return;
	cVal += cValOnes;
	if( cVal > 99 )return;
	
	cVal = eOperateEvent.Splite.Month / 16 * 10;
	cValOnes = eOperateEvent.Splite.Month % 16;
	if(cValOnes >9)return;
	cVal += cValOnes;
	if( (cVal==0) || (cVal>12) )return;
	
	cVal = eOperateEvent.Splite.Day / 16 * 10;
	cValOnes = eOperateEvent.Splite.Day % 16;
	if(cValOnes > 9)return;
	cVal += cValOnes;
	if( (cVal==0) || (cVal>31) )return;
	
	cVal = eOperateEvent.Splite.Hour / 16 * 10;
	cValOnes = eOperateEvent.Splite.Hour % 16;
	if(cValOnes > 9)return;	
	cVal += cValOnes;
	if(cVal>23)return;
	
	cVal = eOperateEvent.Splite.Minute / 16 * 10;
	cValOnes = eOperateEvent.Splite.Minute % 16;
	if(cValOnes >9)return;
	cVal += cValOnes;
	if(cVal>59)return;
	
	
	cVal = eOperateEvent.Splite.Second / 16 * 10;
	cValOnes = eOperateEvent.Splite.Second % 16;
	if(cValOnes > 9)return;
	cVal += cValOnes;
	if(cVal>59)return;
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otPowerOff;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::LoadOperateEventDateTimeFromCurrent()
{
	eOperateEvent.Splite.Year 	= DateTime.Date.Split.Year;
	eOperateEvent.Splite.Month 	= DateTime.Date.Split.Month;
	eOperateEvent.Splite.Day 	= DateTime.Date.Split.Day;
	eOperateEvent.Splite.Hour 	= DateTime.Time.Split.Hour;
	eOperateEvent.Splite.Minute 	= DateTime.Time.Split.Minute;
	eOperateEvent.Splite.Second 	= DateTime.Time.Split.Second;	
}

void TRecord::OperateAdd_PowerUp(unsigned char aCn)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otPowerOn;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_Reset(unsigned char aCn)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otReset;
	eOperateEvent.Splite.Data1 = 0;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_Mute(unsigned char aCn)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otMute;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)
		ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_Mask(TDMix aFullPath, unsigned char aDT, unsigned int aUc)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otMask;
	eOperateEvent.Splite.dType = aDT;
	
	eOperateEvent.Splite.UserCode = aUc;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_UnMask(TDMix aFullPath, unsigned char aDType, unsigned int aUc)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otUnMask;
	eOperateEvent.Splite.dType = aDType;
	
	eOperateEvent.Splite.UserCode = aUc;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_Registe(unsigned char Sta)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otRegiste;
	eOperateEvent.Splite.Data1 = Sta;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_UnRegiste(TDMix aFullPath, unsigned char aDType)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otUnRegiste;
	eOperateEvent.Splite.dType = aDType;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_SetCanNetwork(unsigned char NwType, unsigned char aCn)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otSetCanNetwork;
	eOperateEvent.Splite.Data1 = NwType;
	
	OperateAdd(eOperateEvent);
	//if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_CfgBoard(TDMix aFullPath, unsigned char aDType)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgBoard;
	eOperateEvent.Splite.dType = aDType;

	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_CfgCrt()
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgCrt;
	
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_CfgPriter(unsigned char aOnOff, unsigned int aEvevtInc)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgPriter;
	eOperateEvent.Splite.Data1 = aOnOff;
	eOperateEvent.Splite.P0 = aEvevtInc & 0x000000FF;
	
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_CfgPritEventType()
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgPriter;
	eOperateEvent.Splite.Data1 = 1;
	eOperateEvent.Splite.P0 = 0;
	
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_CfgDevice(TDMix aFullPath, unsigned char aDType)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = aFullPath.D32;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgDevice;
	eOperateEvent.Splite.dType = aDType;
	
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_CfgCircuitSmokeSensGrade(unsigned int aFullPath, unsigned char Grade)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = aFullPath;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otSetSmokeSensViaCircuit;
	eOperateEvent.Splite.Data1 = Grade;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_CfgAddrSmokeSensGrade(unsigned int aFullPath, unsigned char Grade)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = aFullPath;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otSetSmokeSensViaAddr;
	eOperateEvent.Splite.Data1 = Grade;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}


void TRecord::OperateAdd_CfgDateTime(u8 aY, u8 aM, u8 aD, u8 aH, u8 aMin, u8 aSec)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgDateTime;
	
	eOperateEvent.Splite.P0 = aY;
	eOperateEvent.Splite.P1 = aM;
	eOperateEvent.Splite.P2 = aD;
	eOperateEvent.Splite.Data1 = aH;
	eOperateEvent.Splite.iByte0 = aMin;
	eOperateEvent.Splite.iByte1 = aSec;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_GetDateTime(u8 aY, u8 aM, u8 aD, u8 aH, u8 aMin, u8 aSec)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgDateTime;
	
	eOperateEvent.Splite.P0 = aY;
	eOperateEvent.Splite.P1 = aM;
	eOperateEvent.Splite.P2 = aD;
	eOperateEvent.Splite.Data1 = aH;
	eOperateEvent.Splite.iByte0 = aMin;
	eOperateEvent.Splite.iByte1 = aSec;
	
	OperateAdd(eOperateEvent);
	//ProtoC2C.PublishOperate(eOperateEvent);
}


void TRecord::OperateAdd_SetNwDateTime(u8 aY, u8 aM, u8 aD, u8 aH, u8 aMin, u8 aSec)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otSetNwTime;
	
	eOperateEvent.Splite.P0 = aY;
	eOperateEvent.Splite.P1 = aM;
	eOperateEvent.Splite.P2 = aD;
	eOperateEvent.Splite.Data1 = aH;
	eOperateEvent.Splite.iByte0 = aMin;
	eOperateEvent.Splite.iByte1 = aSec;
	
	OperateAdd(eOperateEvent);
	
	ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_ReqNwDateTime()
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otReqNwTime;
	
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_CfgPermissionAllow(unsigned char aPa)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCfgPermissionAllow;
	eOperateEvent.Splite.Data1 = aPa;
	
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_ManualStart(unsigned int aPath, unsigned char aType, unsigned char aSubType)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = aPath;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = aType;
	eOperateEvent.Splite.Data1 = aSubType;
	
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_ManualStartCancel(unsigned int aPath, unsigned char aType, unsigned char aSubType)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = aPath;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = aType;
	eOperateEvent.Splite.Data1 = aSubType;
	
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_ManualFb(unsigned int aPath, unsigned char aType, unsigned char aSubType)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = aPath;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = aType;
	eOperateEvent.Splite.Data1 = aSubType;
	
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_ManualAllowSet(unsigned char aCn, unsigned char aAllow)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otSetManualAllowState;
	eOperateEvent.Splite.Data1 = aAllow;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
	if(MainCtl.fData.Split.NetworkMode == 1){
		eOperateEvent.Splite.Data0 = otRemoteSetManualAllowState;
		ProtoC2C.PublishOperate(eOperateEvent);
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		eOperateEvent.Splite.Data0 = otReportManualAllowState;
		ProtoC2C.PublishOperate(eOperateEvent);
	}
}

void TRecord::OperateAdd_AutoAllowSet(unsigned char aCn, unsigned char aAllow)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otSetAutoAllowState;
	eOperateEvent.Splite.Data1 = aAllow;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
	if(MainCtl.fData.Split.NetworkMode == 1){
		eOperateEvent.Splite.Data0 = otRemoteSetAutoAllowState;
		ProtoC2C.PublishOperate(eOperateEvent);
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		eOperateEvent.Splite.Data0 = otReportAutoAllowState;
		ProtoC2C.PublishOperate(eOperateEvent);
	}
}

void TRecord::OperateAdd_ManualAllowRemoteSet(unsigned char aCn, unsigned char aAllow)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otRemoteSetManualAllowState;
	eOperateEvent.Splite.Data1 = aAllow;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_AllowSet(unsigned char aCn, unsigned char aAllow)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otSetAllowState;
	eOperateEvent.Splite.Data1 = aAllow;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
	if(MainCtl.fData.Split.NetworkMode == 1){
		eOperateEvent.Splite.Data0 = otRemoteSetAllowState;
		ProtoC2C.PublishOperate(eOperateEvent);
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		eOperateEvent.Splite.Data0 = otReportAllowState;
		ProtoC2C.PublishOperate(eOperateEvent);
	}
}

void TRecord::OperateAdd_AutoAllowRemoteSet(unsigned char aCn, unsigned char aAllow)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otRemoteSetAutoAllowState;
	eOperateEvent.Splite.Data1 = aAllow;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_AllowRemoteSet(unsigned char aCn, unsigned char aAllow)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otRemoteSetAllowState;
	eOperateEvent.Splite.Data1 = aAllow;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_ManualAllowReport(unsigned char aCn, unsigned char aAllow)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otReportManualAllowState;
	eOperateEvent.Splite.Data1 = aAllow;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_AutoAllowReport(unsigned char aCn, unsigned char aAllow)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otReportAutoAllowState;
	eOperateEvent.Splite.Data1 = aAllow;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_AllowReport(unsigned char aCn, unsigned char aAllow)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otReportAllowState;
	eOperateEvent.Splite.Data1 = aAllow;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
}

void TRecord::OperateAdd_AlramSoundOn(unsigned char aCn)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otAlramSetOnOff;
	eOperateEvent.Splite.Data1 = 1;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
	//ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_AlramSoundOff(unsigned char aCn)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otAlramSetOnOff;
	eOperateEvent.Splite.Data1 = 0;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);	
	//ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_BusPadOn(unsigned char aCn, unsigned char aP0, unsigned char aP1)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otBusPadOn;
	eOperateEvent.Splite.Data1 = 0;
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_BusPadOff(unsigned char aCn, unsigned char aP0, unsigned char aP1)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otBusPadOff;
	eOperateEvent.Splite.Data1 = 0;
	OperateAdd(eOperateEvent);
	ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_DirectPadManualAllowOn(unsigned char aCn, unsigned char aP0)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otDirectPadManualAllowOn;
	eOperateEvent.Splite.Data1 = 0;
	
	
	OperateAdd(eOperateEvent);	
	ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_DirectPadManualAllowOff(unsigned char aCn, unsigned char aP0)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otDirectPadManualAllowOff;
	eOperateEvent.Splite.Data1 = 0;
	OperateAdd(eOperateEvent);	
	ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_DirectPadNodeOn(unsigned char aCn, unsigned char aP0, unsigned char aP1)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otDirectPadNodeOn;
	eOperateEvent.Splite.Data1 = 0;
	OperateAdd(eOperateEvent);	
	ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_DirectPadNodeOff(unsigned char aCn, unsigned char aP0, unsigned char aP1)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 0;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otDirectPadNodeOff;
	eOperateEvent.Splite.Data1 = 0;
	OperateAdd(eOperateEvent);	
	ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_UserCheck(unsigned char aCn, unsigned char OnOff)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otUserCheck;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = OnOff;
	
	if(OnOff)OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_SelfCheck(unsigned char aCn)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otSelfCheck;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_MasterLinkageGo(unsigned char aCn)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otLinkageGo;
	eOperateEvent.Splite.Data1 = 1;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	OperateAdd(eOperateEvent);
	
	eOperateEvent.Splite.Data1 = 3;
	ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_SlaveLinkageGo(unsigned char aCn)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otLinkageGo;
	eOperateEvent.Splite.Data1 = 1;
	
	eOperateEvent.Splite.PCtlNum = aCn;
	
	OperateAdd(eOperateEvent);
	
	eOperateEvent.Splite.Data1 = 0;
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);	
}

void TRecord::OperateAdd_FireConfirm(unsigned char aCn, unsigned char aP0, unsigned char aP1, unsigned char aType, unsigned int aUc, unsigned char IsSim)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.P2 = 0;	//prm
	eOperateEvent.Splite.dType = aType;
	
	eOperateEvent.Splite.mType = etOperate;
	if(IsSim == TControlCenter::LinkInputTypeFireSimulate){
		eOperateEvent.Splite.Data0 = otFireSimConfirm;
	}else{
		eOperateEvent.Splite.Data0 = otFireConfirm;
	}
	
	eOperateEvent.Splite.UserCode = aUc;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_FaultConfirm(unsigned char aCn, unsigned char aP0, unsigned char aP1, unsigned char dType, unsigned char fType, unsigned char Data1, unsigned int aUc)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = aCn;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.P2 = fType;
	eOperateEvent.Splite.dType = dType;
	eOperateEvent.Splite.Data1 = Data1;	//Fault Type
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otFaultConfirm;
	
	eOperateEvent.Splite.UserCode = aUc;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperatePublic_FireRemoteConfirm(unsigned char aCn, unsigned char aP0, unsigned char aP1, unsigned char aType, unsigned char IsSim)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.P2 = 0;
	eOperateEvent.Splite.dType = aType;	//prm
	eOperateEvent.Splite.Data1 = aCn;
	
	eOperateEvent.Splite.mType = etOperate;
	if(IsSim == TControlCenter::LinkInputTypeFireSimulate){
		eOperateEvent.Splite.Data0 = otFireSimRemoteConfirm;
	}else{
		eOperateEvent.Splite.Data0 = otFireRemoteConfirm;
	}
	
	ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperatePublic_FaultRemoteConfirm(unsigned char aCn, unsigned char aP0, unsigned char aP1, unsigned char dType, unsigned char fType, unsigned char Data1)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.P2 = fType;
	eOperateEvent.Splite.dType = dType;	//prm
	eOperateEvent.Splite.Data1 = Data1;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otFaultRemoteConfirm;
	
	eOperateEvent.Splite.iByte0 = aCn;
	
	ProtoC2C.PublishOperate(eOperateEvent);
}


void TRecord::OperateAdd_CleanUp(unsigned char cType)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.Data1 = cType;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otCleanUp;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_LinkageExpsModify(unsigned char IsAdd, unsigned int eInx)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.Data1 = IsAdd;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otLinkageExpsSaveRemove;
	
	eOperateEvent.Splite.P0 = eInx / 256;
	eOperateEvent.Splite.P1 = eInx % 256;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_BusPadModifyModify(unsigned char aP0, unsigned int aP1)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otBusPadModify;
	
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_DirectPadModify(unsigned char aP0)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otDirectPadModify;
	
	eOperateEvent.Splite.P0 = aP0;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_DescpModify(unsigned char aP0, unsigned char aP1)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otDescpModify;
	
	
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_PassWdModify(unsigned char aLvl)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otPassWdModify;
	
	eOperateEvent.Splite.Data1 = aLvl;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_RegisterByManual(unsigned char aP0, unsigned char sAddr, unsigned char eAddr, unsigned char aType, unsigned char IsAdd)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otRegisterByManual;
	eOperateEvent.Splite.Data1 = IsAdd;
	
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = sAddr;
	eOperateEvent.Splite.P2 = eAddr;
	eOperateEvent.Splite.dType = aType;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);
}

void TRecord::OperateAdd_AddRemoveNwCtl(unsigned char Act, unsigned char aCtlNum)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otAddRemoveNwCtl;
	
	eOperateEvent.Splite.Data1 = Act;
	eOperateEvent.Splite.P0 = aCtlNum;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_RegCanNetwork(unsigned char NwType, unsigned char aCtlNum, unsigned char SlaveCount)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otRegCanNetwork;
	
	eOperateEvent.Splite.Data1 = NwType;
	eOperateEvent.Splite.P0 = aCtlNum;
	eOperateEvent.Splite.P1 = SlaveCount;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_StartStop(unsigned char aCn, unsigned char aP0, unsigned char aP1, unsigned char aType, unsigned char IsSatrt)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otStartStop;
	
	eOperateEvent.Splite.Data1 = IsSatrt;
	eOperateEvent.Splite.P0 = aP0;
	eOperateEvent.Splite.P1 = aP1;
	eOperateEvent.Splite.P2 = aCn;
	eOperateEvent.Splite.dType = aType;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_ClearNetworkCfg()
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otClearNetworkCfg;
	
	eOperateEvent.Splite.Data1 = MainCtl.fData.Split.NetworkMode;
	
	OperateAdd(eOperateEvent);
}

void TRecord::OperateAdd_BroadcastVa(unsigned char aPadNum, unsigned char aAddr, unsigned char aMode)
{
	LoadOperateEventDateTimeFromCurrent();
	
	eOperateEvent.D32[2] = 1;
	eOperateEvent.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
	eOperateEvent.Splite.P0 = aPadNum;
	eOperateEvent.Splite.P1 = aAddr;
	
	eOperateEvent.Splite.mType = etOperate;
	eOperateEvent.Splite.Data0 = otBroadcastVa;
	
	eOperateEvent.Splite.Data1 = aMode;
	
	OperateAdd(eOperateEvent);
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.PublishOperate(eOperateEvent);
}



void TRecord::OperateAdd_ScreenKeyLock(unsigned char aScreenSave, unsigned char EnterSaveTime, unsigned char aLock, char *aCr)
{
	
}
//______________________________________________________________________________<<<<<

//______________________________________________________________________________>>>>>


//______________________________________________________________________________>>>>>

void TRecord::Task10Ms()
{
	Chip0Prc();
	Chip1Prc();
}

void TRecord::Task5000Ms()
{

}

void FireFlushRuntimeDataArray()
{
	
}
//__________________________________________________________________________________________________
void TRecord::GetFireEntry(int aInx)
{
	volatile unsigned int aAddr ,i;
	unsigned int aCn, aP0, aP1;
	aAddr = dAddrSdWrite2FlashBufFire + (aInx * 24);
	EntryRead.fData.Body.D32[0] = *(volatile unsigned int *)(aAddr + 0);
	EntryRead.fData.Body.D32[1] = *(volatile unsigned int *)(aAddr + 4);
	EntryRead.fData.Body.D32[2] = *(volatile unsigned int *)(aAddr + 8);
	EntryRead.fData.Body.D32[3] = *(volatile unsigned int *)(aAddr +12);
	EntryRead.fData.UserCode = *(volatile unsigned int *)(aAddr +16);
	if(EntryRead.fData.Body.Splite.PCtlNum == MainCtl.fData.Split.MyNum){
		aP0 = EntryRead.fData.Body.Splite.P0;
		aP1 = EntryRead.fData.Body.Splite.P1;
		if(aP0 && aP1){
			if(aP0 <= dPORT_MAX_COUNT){
				aP0--;
				aP1--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + (aP1 * 32);
			for(i=7; i<15; i++){
				EntryRead.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}else{
			for(i=7; i<15; i++){
				EntryRead.D32[i] = 0;
			}
		}
	}else
	if( (EntryRead.fData.Body.Splite.PCtlNum) && (EntryRead.fData.Body.Splite.PCtlNum <= dNETWORK_MACHINE_COUNT) ){
		aCn = EntryRead.fData.Body.Splite.PCtlNum -1;
		aP0 = EntryRead.fData.Body.Splite.P0;
		aP1 = EntryRead.fData.Body.Splite.P1;
		if(aP0 && aP1){
			if(aP0 <= dPORT_MAX_COUNT){
				aP0--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_DescpNw + (aCn*dONE_CTRL_DESCP_BYTES)+(aP0*8192) + (aP1*32);
			for(i=7; i<15; i++){
				EntryRead.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}else{
			for(i=7; i<15; i++){
				EntryRead.D32[i] = 0;
			}
		}				
	}else{
		for(i=7; i<15; i++){
			EntryRead.D32[i] = 0;
		}
	}
}

void TRecord::PrcFireWrite()
{
	int i, aReadInx ,OK, m;
	unsigned int aAddr, aState, GotNew;
	unsigned int aEntry, aPage, aBlock, aBlock2;
	int BlockCount;
	int EntryCount;

	
	//Read Chip0 State Here 
	
	if(RtChip0.Sta == csIdle){
		aState = nor_get_busy_state(dFlashFireChip);
		if(aState != 0)return;
		
		GotNew = 0;
		FlushToWriteBuf();
		aReadInx = Buf4Fire.ReadIndex;
		aEntry = Buf4Fire.EntryX;
		aPage = Buf4Fire.PageX;
		aBlock = Buf4Fire.BlockX;
		while(aReadInx != Buf4Fire.WriteIndex){
			aReadInx++;
			if(aReadInx >=  dWrite2FlashMaxCount){
				aReadInx = 0;
			}
			GetFireEntry(aReadInx);
			if(EntryRead.fData.Body.D32[2] != 0){
				if(aEntry < dFlashEntryCountPerPage){
					ToWriteBuf[aEntry].D32[ 0] = EntryRead.D32[ 0];
					ToWriteBuf[aEntry].D32[ 1] = EntryRead.D32[ 1];
					ToWriteBuf[aEntry].D32[ 2] = EntryRead.D32[ 2];
					ToWriteBuf[aEntry].D32[ 3] = EntryRead.D32[ 3];
					ToWriteBuf[aEntry].D32[ 4] = EntryRead.D32[ 4];
					ToWriteBuf[aEntry].D32[ 5] = EntryRead.D32[ 5];
					ToWriteBuf[aEntry].D32[ 6] = EntryRead.D32[ 6];
					ToWriteBuf[aEntry].D32[ 7] = EntryRead.D32[ 7];
					ToWriteBuf[aEntry].D32[ 8] = EntryRead.D32[ 8];
					ToWriteBuf[aEntry].D32[ 9] = EntryRead.D32[ 9];
					ToWriteBuf[aEntry].D32[10] = EntryRead.D32[10];
					ToWriteBuf[aEntry].D32[11] = EntryRead.D32[11];
					ToWriteBuf[aEntry].D32[12] = EntryRead.D32[12];
					ToWriteBuf[aEntry].D32[13] = EntryRead.D32[13];
					ToWriteBuf[aEntry].D32[14] = EntryRead.D32[14];
					ToWriteBuf[aEntry].D32[15] = EntryRead.D32[15];
					GotNew ++;
					aEntry ++;
				}
				if(aEntry >= dFlashEntryCountPerPage){
					aEntry =0;
					aPage++;
					if(aPage >= dFlashPageCountPerBlock){
						aPage = 0;
						aBlock++;
						if(aBlock >= dFrashFireUsingBlockCount)aBlock = 0;
						Buf4Fire.NeedCheckBlockEmpty = 1;
						Buf4Fire.BlockToBeErase = aBlock;
						aBlock2 = Buf4Fire.BlockToBeErase +1;
						if(aBlock2 >= dFrashFireUsingBlockCount)aBlock2 = 0;
						Buf4Fire.BlockToBeErase2 = aBlock2;
						Buf4Fire.NeedCheckBlockEmpty2 = 1;
					}
					break;
				}
				if(  (0 == Buf4Fire.EntryX ) && (0 == Buf4Fire.PageX) ){
					//Check Writing Block if Empty
					fAddr = dFlashFireBaseAddr + (dFlashBlockSize * Buf4Fire.BlockX);
					nor_read_byte(dFlashFireChip, fAddr, FlashPageReadData.D8, 8);
					if( (FlashPageReadData.D32[0] != 0xFFFFFFFF) || (FlashPageReadData.D32[1] != 0xFFFFFFFF) ){
						Buf4Fire.NeedCheckBlockEmpty = 1;
						Buf4Fire.BlockToBeErase = Buf4Fire.BlockX;
						GotNew = 0;
					}
					//Check Next Block if Need Erase
					aBlock2 = Buf4Fire.BlockX +1;
					if(aBlock2 >= dFrashFireUsingBlockCount)aBlock2 = 0;
					fAddr = dFlashFireBaseAddr + (dFlashBlockSize * aBlock2);
					nor_read_byte(dFlashFireChip, fAddr, FlashPageReadData.D8, 8);
					if( (FlashPageReadData.D32[0] != 0xFFFFFFFF) || (FlashPageReadData.D32[1] != 0xFFFFFFFF) ){
						Buf4Fire.NeedCheckBlockEmpty2 = 1;
						Buf4Fire.BlockToBeErase2 = aBlock2;
						GotNew = 0;
					}
					
					Buf4Fire.wBlockWriteToEep = Buf4Fire.BlockX;
					Buf4Fire.NeedMarkWriteBlcokToEep = 1;
				}
			}else{
				continue;
			}
		}
		if(GotNew){
			//write Here and Check Ok
			aAddr = dFlashFireBaseAddr + (dFlashBlockSize * Buf4Fire.BlockX) + (dFlashPageSize * Buf4Fire.PageX) + (dFlashEntrySize * Buf4Fire.EntryX);
			OK = 0;
			for(i=Buf4Fire.EntryX; i<dFlashEntryCountPerPage; i++){
				sysIndex.fIndex++;
				ToWriteBuf[i].D32[0] = sysIndex.D32.Inx0;
				ToWriteBuf[i].D32[1] = sysIndex.D32.Inx1;
				aCsum = 0;
				for(m=0; m<60; m++){
					aCsum += ToWriteBuf[i].D8[m];
				}
				ToWriteBuf[i].D32[15] = aCsum;
				OK += 1;
				if(OK >= GotNew){
					break;
				}
			}
			nor_write_byte_via_page(dFlashFireChip, aAddr, ToWriteBuf[Buf4Fire.EntryX].D8, GotNew * dFlashEntrySize);
			
			OK = 0;
			aAddr = dSdramFireHistoryContentBaseAddr + (Buf4Fire.BlockX * SizeOf64K) + (Buf4Fire.PageX * SizeOfPage) + (Buf4Fire.EntryX * dFlashEntrySize);
			for(i=Buf4Fire.EntryX; i<dFlashEntryCountPerPage; i++){
				for(BlockCount=0; BlockCount<16; BlockCount++){
					*(volatile unsigned int *) (aAddr + BlockCount * 4) = ToWriteBuf[i].D32[BlockCount];
				}
				OK += 1;
				if(OK >= GotNew){
					break;
				}
				aAddr += dFlashEntrySize;
			}
			
			//read Back To ToWriteBuf
			if(1){
				OK = 1;
			}else{
				OK = 0;
			}
			
			//OK
			if(OK){
				Buf4Fire.ReadIndex = aReadInx;
				Buf4Fire.BlockX = aBlock;
				Buf4Fire.PageX = aPage;
				Buf4Fire.EntryX = aEntry;
				
				BlockCount = ((Buf4Fire.BlockX + dFrashFireUsingBlockCount) - Buf4Fire.sBlock ) % dFrashFireUsingBlockCount +1;
				if(BlockCount > (dFrashFireUsingBlockCount -2)){
					Buf4Fire.sBlock = Buf4Fire.BlockX - (dFrashFireUsingBlockCount -3);
					if(Buf4Fire.sBlock <0)Buf4Fire.sBlock += dFrashFireUsingBlockCount;

					BlockCount = dFrashFireUsingBlockCount -2;
				}
				EntryCount = (BlockCount-1) * (dFlashBlockSize / dFlashEntrySize) + (Buf4Fire.PageX * (dFlashPageSize / dFlashEntrySize)) + Buf4Fire.EntryX ;
				if(EntryCount > dSdramHistoryEntryCount){
					Buf4Fire.Count = dSdramHistoryEntryCount;
				}else{
					Buf4Fire.Count = EntryCount;
				}
				Buf4Fire.SdramAddrCurrent = dSdramFireHistoryContentBaseAddr + (Buf4Fire.BlockX * SizeOf64K) + (Buf4Fire.PageX * dFlashPageSize) + (Buf4Fire.EntryX * dFlashEntrySize);
				Buf4Fire.SdramAddrStart = Buf4Fire.SdramAddrCurrent - (Buf4Fire.Count * 64);
				if(Buf4Fire.SdramAddrStart < dSdramFireHistoryContentBaseAddr){
					Buf4Fire.SdramAddrStart = Buf4Fire.SdramAddrStart + dSdramHistoryBytesLength;
				}
			}
		}
	}
}
//_____________________________________________________________________________________________________
void TRecord::GetLinkageEntry(int aInx)
{
	volatile unsigned int aAddr ,i;
	unsigned int aCn, aP0, aP1;
	aAddr = dAddrSdWrite2FlashBufLinkage + (aInx * 24);
	EntryRead.fData.Body.D32[0] = *(volatile unsigned int *)(aAddr + 0);
	EntryRead.fData.Body.D32[1] = *(volatile unsigned int *)(aAddr + 4);
	EntryRead.fData.Body.D32[2] = *(volatile unsigned int *)(aAddr + 8);
	EntryRead.fData.Body.D32[3] = *(volatile unsigned int *)(aAddr +12);
	EntryRead.fData.UserCode = *(volatile unsigned int *)(aAddr +16);
	if(EntryRead.fData.Body.Splite.PCtlNum == MainCtl.fData.Split.MyNum){
		aP0 = EntryRead.fData.Body.Splite.P0;
		aP1 = EntryRead.fData.Body.Splite.P1;
		if(aP0 && aP1){
			if(aP0 <= dPORT_MAX_COUNT){
				aP0--;
				aP1--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + (aP1 * 32);
			for(i=7; i<15; i++){
				EntryRead.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}else{
			for(i=7; i<15; i++){
				EntryRead.D32[i] = 0;
			}
		}
	}else
	if( (EntryRead.fData.Body.Splite.PCtlNum) && (EntryRead.fData.Body.Splite.PCtlNum <= dNETWORK_MACHINE_COUNT) ){
		aCn = EntryRead.fData.Body.Splite.PCtlNum -1;
		aP0 = EntryRead.fData.Body.Splite.P0;
		aP1 = EntryRead.fData.Body.Splite.P1;
		if(aP0 && aP1){
			if(aP0 <= dPORT_MAX_COUNT){
				aP0--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_DescpNw + (aCn*dONE_CTRL_DESCP_BYTES)+(aP0*8192) + (aP1*32);
			for(i=7; i<15; i++){
				EntryRead.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}else{
			for(i=7; i<15; i++){
				EntryRead.D32[i] = 0;
			}
		}
	}else{
		for(i=7; i<15; i++){
			EntryRead.D32[i] = 0;
		}
	}
}

void TRecord::PrcLinkageWrite()
{
	int i, aReadInx ,OK, m;
	unsigned int aAddr, aState, GotNew;
	unsigned int aEntry, aPage, aBlock, aBlock2;
	int BlockCount;
	int EntryCount;

	
	//Read Chip0 State Here 
	
	if(RtChip0.Sta == csIdle){
		aState = nor_get_busy_state(0);
		if(aState != 0)return;
		
		GotNew = 0;
		FlushToWriteBuf();
		aReadInx = Buf4Linkage.ReadIndex;
		aEntry = Buf4Linkage.EntryX;
		aPage = Buf4Linkage.PageX;
		aBlock = Buf4Linkage.BlockX;
		while(aReadInx != Buf4Linkage.WriteIndex){
			aReadInx++;
			if(aReadInx >=  dWrite2FlashMaxCount){
				aReadInx = 0;
			}
			GetLinkageEntry(aReadInx);
			if(EntryRead.fData.Body.D32[2] != 0){
				if(aEntry < dFlashEntryCountPerPage){
					ToWriteBuf[aEntry].D32[ 0] = EntryRead.D32[ 0];
					ToWriteBuf[aEntry].D32[ 1] = EntryRead.D32[ 1];
					ToWriteBuf[aEntry].D32[ 2] = EntryRead.D32[ 2];
					ToWriteBuf[aEntry].D32[ 3] = EntryRead.D32[ 3];
					ToWriteBuf[aEntry].D32[ 4] = EntryRead.D32[ 4];
					ToWriteBuf[aEntry].D32[ 5] = EntryRead.D32[ 5];
					ToWriteBuf[aEntry].D32[ 6] = EntryRead.D32[ 6];
					ToWriteBuf[aEntry].D32[ 7] = EntryRead.D32[ 7];
					ToWriteBuf[aEntry].D32[ 8] = EntryRead.D32[ 8];
					ToWriteBuf[aEntry].D32[ 9] = EntryRead.D32[ 9];
					ToWriteBuf[aEntry].D32[10] = EntryRead.D32[10];
					ToWriteBuf[aEntry].D32[11] = EntryRead.D32[11];
					ToWriteBuf[aEntry].D32[12] = EntryRead.D32[12];
					ToWriteBuf[aEntry].D32[13] = EntryRead.D32[13];
					ToWriteBuf[aEntry].D32[14] = EntryRead.D32[14];
					ToWriteBuf[aEntry].D32[15] = EntryRead.D32[15];
					GotNew ++;
					aEntry ++;
				}
				if(aEntry >= dFlashEntryCountPerPage){
					aEntry =0;
					aPage++;
					if(aPage >= dFlashPageCountPerBlock){
						aPage = 0;
						aBlock++;
						if(aBlock >= dFrashLinkageUsingBlockCount)aBlock = 0;
						Buf4Linkage.NeedCheckBlockEmpty = 1;
						Buf4Linkage.BlockToBeErase = aBlock;
						aBlock2 = Buf4Linkage.BlockToBeErase +1;
						if(aBlock2 >= dFrashLinkageUsingBlockCount)aBlock2 = 0;
						Buf4Linkage.BlockToBeErase2 = aBlock2;
						Buf4Linkage.NeedCheckBlockEmpty2 = 1;
					}
					break;
				}
				if(  (0 == Buf4Linkage.EntryX ) && (0 == Buf4Linkage.PageX) ){
					//Check Writing Block if Empty
					fAddr = dFlashLinkageBaseAddr + (dFlashBlockSize * Buf4Linkage.BlockX);
					nor_read_byte(0, fAddr, FlashPageReadData.D8, 8);
					if( (FlashPageReadData.D32[0] != 0xFFFFFFFF) || (FlashPageReadData.D32[1] != 0xFFFFFFFF) ){
						Buf4Linkage.NeedCheckBlockEmpty = 1;
						Buf4Linkage.BlockToBeErase = Buf4Linkage.BlockX;
						GotNew = 0;
					}
					//Check Next Block if Need Erase
					aBlock2 = Buf4Linkage.BlockX +1;
					if(aBlock2 >= dFrashLinkageUsingBlockCount)aBlock2 = 0;
					fAddr = dFlashLinkageBaseAddr + (dFlashBlockSize * aBlock2);
					nor_read_byte(0, fAddr, FlashPageReadData.D8, 8);
					if( (FlashPageReadData.D32[0] != 0xFFFFFFFF) || (FlashPageReadData.D32[1] != 0xFFFFFFFF) ){
						Buf4Linkage.NeedCheckBlockEmpty2 = 1;
						Buf4Linkage.BlockToBeErase2 = aBlock2;
						GotNew = 0;
					}
					
					Buf4Linkage.wBlockWriteToEep = Buf4Linkage.BlockX;
					Buf4Linkage.NeedMarkWriteBlcokToEep = 1;
				}
			}else{
				continue;
			}
		}
		if(GotNew){
			//write Here and Check Ok
			aAddr = dFlashLinkageBaseAddr + (dFlashBlockSize * Buf4Linkage.BlockX) + (dFlashPageSize * Buf4Linkage.PageX) + (dFlashEntrySize * Buf4Linkage.EntryX);
			OK = 0;
			for(i=Buf4Linkage.EntryX; i<dFlashEntryCountPerPage; i++){
				sysIndex.fIndex++;
				ToWriteBuf[i].D32[0] = sysIndex.D32.Inx0;
				ToWriteBuf[i].D32[1] = sysIndex.D32.Inx1;
				aCsum = 0;
				for(m=0; m<60; m++){
					aCsum += ToWriteBuf[i].D8[m];
				}
				ToWriteBuf[i].D32[15] = aCsum;
				OK += 1;
				if(OK >= GotNew){
					break;
				}
			}
			nor_write_byte_via_page(0, aAddr, ToWriteBuf[Buf4Linkage.EntryX].D8, GotNew * dFlashEntrySize);
			
			OK = 0;
			aAddr = dSdramLinkageHistoryContentBaseAddr + (Buf4Linkage.BlockX * SizeOf64K) + (Buf4Linkage.PageX * SizeOfPage) + (Buf4Linkage.EntryX * dFlashEntrySize);
			for(i=Buf4Linkage.EntryX; i<dFlashEntryCountPerPage; i++){
				for(BlockCount=0; BlockCount<16; BlockCount++){
					*(volatile unsigned int *) (aAddr + BlockCount * 4) = ToWriteBuf[i].D32[BlockCount];
				}
				OK += 1;
				if(OK >= GotNew){
					break;
				}
				aAddr += dFlashEntrySize;
			}
			
			//read Back To ToWriteBuf
			if(1){
				OK = 1;
			}else{
				OK = 0;
			}
			
			//OK
			if(OK){
				Buf4Linkage.ReadIndex = aReadInx;
				Buf4Linkage.BlockX = aBlock;
				Buf4Linkage.PageX = aPage;
				Buf4Linkage.EntryX = aEntry;
				
				BlockCount = ((Buf4Linkage.BlockX + dFrashLinkageUsingBlockCount) - Buf4Linkage.sBlock ) % dFrashLinkageUsingBlockCount +1;
				if(BlockCount > (dFrashLinkageUsingBlockCount -2)){
					Buf4Linkage.sBlock = Buf4Linkage.BlockX - (dFrashLinkageUsingBlockCount -3);
					if(Buf4Linkage.sBlock <0)Buf4Linkage.sBlock += dFrashLinkageUsingBlockCount;

					BlockCount = dFrashLinkageUsingBlockCount -2;
				}
				EntryCount = (BlockCount-1) * (dFlashBlockSize / dFlashEntrySize) + (Buf4Linkage.PageX * (dFlashPageSize / dFlashEntrySize)) + Buf4Linkage.EntryX ;
				if(EntryCount > dSdramHistoryEntryCount){
					Buf4Linkage.Count = dSdramHistoryEntryCount;
				}else{
					Buf4Linkage.Count = EntryCount;
				}
				Buf4Linkage.SdramAddrCurrent = dSdramLinkageHistoryContentBaseAddr + (Buf4Linkage.BlockX * SizeOf64K) + (Buf4Linkage.PageX * dFlashPageSize) + (Buf4Linkage.EntryX * dFlashEntrySize);
				Buf4Linkage.SdramAddrStart = Buf4Linkage.SdramAddrCurrent - (Buf4Linkage.Count * 64);
				if(Buf4Linkage.SdramAddrStart < dSdramLinkageHistoryContentBaseAddr){
					Buf4Linkage.SdramAddrStart = Buf4Linkage.SdramAddrStart + dSdramHistoryBytesLength;
				}
			}
		}
	}
}
//_____________________________________________________________________________________________________________________________________________________________________________
void TRecord::Chip0Prc()
{
	unsigned int aAddr, aState;
	if(RtChip0.Sta == csIdle){
			if(RtChip0.FireOrSv){
				//Process Fire Here
				RtChip0.FireOrSv = 0;
				if(Buf4Fire.NeedCheckBlockEmpty){
					aState = nor_get_busy_state(0);
					if(aState == 0){
						aAddr = dFlashFireBaseAddr + (dFlashBlockSize * Buf4Fire.BlockToBeErase);
						nor_erase_sector_64k(dFlashFireChip,aAddr);
						RtChip0.Sta = csErassing;
						Buf4Fire.NeedCheckBlockEmpty = 0;
					}
				}else
				if(Buf4Fire.NeedCheckBlockEmpty2){
					aState = nor_get_busy_state(0);
					if(aState == 0){
						aAddr = dFlashFireBaseAddr + (dFlashBlockSize * Buf4Fire.BlockToBeErase2);
						nor_erase_sector_64k(dFlashFireChip,aAddr);
						RtChip0.Sta = csErassing;
						Buf4Fire.NeedCheckBlockEmpty2 = 0;
					}
				}else{
					if(Buf4Fire.ReadIndex != Buf4Fire.WriteIndex){
						PrcFireWrite();
					}
				}
				if(Buf4Fire.NeedMarkWriteBlcokToEep){
					Wait10Ms();
					eeprom_write_byte_via_page(0, dEepAddrFireWritingBlock, &Buf4Fire.wBlockWriteToEep, 1);
					Wait10Ms();
					Buf4Fire.NeedMarkWriteBlcokToEep = 0;
				}
			}else{
				//Process Start Here
				RtChip0.FireOrSv = 1;
				if(Buf4Linkage.NeedCheckBlockEmpty){
					aState = nor_get_busy_state(0);
					if(aState == 0){
						aAddr = dFlashLinkageBaseAddr + (dFlashBlockSize * Buf4Linkage.BlockToBeErase);
						nor_erase_sector_64k(dFlashLinkageChip,aAddr);
						RtChip0.Sta = csErassing;
						Buf4Linkage.NeedCheckBlockEmpty = 0;
					}
				}else
				if(Buf4Linkage.NeedCheckBlockEmpty2){
					aState = nor_get_busy_state(0);
					if(aState == 0){
						aAddr = dFlashLinkageBaseAddr + (dFlashBlockSize * Buf4Linkage.BlockToBeErase2);
						nor_erase_sector_64k(dFlashLinkageChip,aAddr);
						RtChip0.Sta = csErassing;
						Buf4Linkage.NeedCheckBlockEmpty2 = 0;
					}
				}else{
					if(Buf4Linkage.ReadIndex != Buf4Linkage.WriteIndex){
						PrcLinkageWrite();
					}
				}
				if(Buf4Linkage.NeedMarkWriteBlcokToEep){
					Wait10Ms();
					eeprom_write_byte_via_page(0, dEepAddrLinkageWritingBlock, &Buf4Linkage.wBlockWriteToEep, 1);
					Wait10Ms();
					Buf4Linkage.NeedMarkWriteBlcokToEep = 0;
				}
			}
	}else
	if(RtChip0.Sta == csCanTryResume){
		if(0 == nor_get_busy_state(0)){
			RtChip0.Sta = csIdle;
		}else{
			RtChip0.Tick4_10Ms = EraseBlock64K_TryRusumeTime /2;
			RtChip0.Sta = csBusy;
		}
	}else{
		RtChip0.Tick4_10Ms ++;
		if(RtChip0.Tick4_10Ms > EraseBlock64K_TryRusumeTime){
			RtChip0.Sta = csCanTryResume;
		}
	}
}
//______________________________________________________________________________________________________________________________________________

void TRecord::GetSvEntry(int aInx)
{
	volatile unsigned int aAddr,i;
	unsigned int aCn, aP0, aP1;
	aAddr = dAddrSdWrite2FlashBufSv + (aInx * 24);
	EntryRead.fData.Body.D32[0] = *(volatile unsigned int *)(aAddr + 0);
	EntryRead.fData.Body.D32[1] = *(volatile unsigned int *)(aAddr + 4);
	EntryRead.fData.Body.D32[2] = *(volatile unsigned int *)(aAddr + 8);
	EntryRead.fData.Body.D32[3] = *(volatile unsigned int *)(aAddr +12);
	EntryRead.fData.UserCode = *(volatile unsigned int *)(aAddr +16);
	if(EntryRead.fData.Body.Splite.PCtlNum == MainCtl.fData.Split.MyNum){
		aP0 = EntryRead.fData.Body.Splite.P0;
		aP1 = EntryRead.fData.Body.Splite.P1;
		if(aP0 && aP1){
			if(aP0 <= dPORT_MAX_COUNT){
				aP0--;
				aP1--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + (aP1 * 32);
			for(i=7; i<15; i++){
				EntryRead.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}else{
			for(i=7; i<15; i++){
				EntryRead.D32[i] = 0;
			}
		}
	}else
	if( (EntryRead.fData.Body.Splite.PCtlNum) && (EntryRead.fData.Body.Splite.PCtlNum <= dNETWORK_MACHINE_COUNT) ){
		aCn = EntryRead.fData.Body.Splite.PCtlNum -1;
		aP0 = EntryRead.fData.Body.Splite.P0;
		aP1 = EntryRead.fData.Body.Splite.P1;
		if(aP0 && aP1){
			if(aP0 <= dPORT_MAX_COUNT){
				aP0--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_DescpNw + (aCn*dONE_CTRL_DESCP_BYTES)+(aP0*8192) + (aP1*32);
			for(i=7; i<15; i++){
				EntryRead.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}else{
			for(i=7; i<15; i++){
				EntryRead.D32[i] = 0;
			}
		}
	}else{
		for(i=7; i<15; i++){
			EntryRead.D32[i] = 0;
		}
	}
}

void TRecord::PrcSvWrite()
{
	int i, aReadInx ,OK, m;
	unsigned int aAddr, aState, GotNew;
	unsigned int aEntry, aPage, aBlock, aBlock2;
	int BlockCount, EntryCount;

	
	//Read Chip1 State Here 
	
	if(RtChip1.Sta == csIdle){
		aState = nor_get_busy_state(dFlashSvChip);
		if(aState != 0)return;
		
		GotNew = 0;
		FlushToWriteBuf();
		aReadInx = Buf4Sv.ReadIndex;
		aEntry = Buf4Sv.EntryX;
		aPage = Buf4Sv.PageX;
		aBlock = Buf4Sv.BlockX;
		while(aReadInx != Buf4Sv.WriteIndex){
			aReadInx++;
			if(aReadInx >=  dWrite2FlashMaxCount){
				aReadInx = 0;
			}
			GetSvEntry(aReadInx);
			if(EntryRead.fData.Body.D32[2] != 0){
				if(aEntry < dFlashEntryCountPerPage){
					ToWriteBuf[aEntry].D32[ 0] = EntryRead.D32[ 0];
					ToWriteBuf[aEntry].D32[ 1] = EntryRead.D32[ 1];
					ToWriteBuf[aEntry].D32[ 2] = EntryRead.D32[ 2];
					ToWriteBuf[aEntry].D32[ 3] = EntryRead.D32[ 3];
					ToWriteBuf[aEntry].D32[ 4] = EntryRead.D32[ 4];
					ToWriteBuf[aEntry].D32[ 5] = EntryRead.D32[ 5];
					ToWriteBuf[aEntry].D32[ 6] = EntryRead.D32[ 6];
					ToWriteBuf[aEntry].D32[ 7] = EntryRead.D32[ 7];
					ToWriteBuf[aEntry].D32[ 8] = EntryRead.D32[ 8];
					ToWriteBuf[aEntry].D32[ 9] = EntryRead.D32[ 9];
					ToWriteBuf[aEntry].D32[10] = EntryRead.D32[10];
					ToWriteBuf[aEntry].D32[11] = EntryRead.D32[11];
					ToWriteBuf[aEntry].D32[12] = EntryRead.D32[12];
					ToWriteBuf[aEntry].D32[13] = EntryRead.D32[13];
					ToWriteBuf[aEntry].D32[14] = EntryRead.D32[14];
					ToWriteBuf[aEntry].D32[15] = EntryRead.D32[15];
					GotNew ++;
					aEntry ++;
				}
				if(aEntry >= dFlashEntryCountPerPage){
					aEntry =0;
					aPage++;
					if(aPage >= dFlashPageCountPerBlock){
						aPage = 0;
						aBlock++;
						if(aBlock >= dFrashSvUsingBlockCount)aBlock = 0;
						Buf4Sv.NeedCheckBlockEmpty = 1;
						Buf4Sv.BlockToBeErase = aBlock;
						aBlock2 = Buf4Sv.BlockToBeErase +1;
						if(aBlock2 >= dFrashSvUsingBlockCount)aBlock2 = 0;
						Buf4Sv.BlockToBeErase2 = aBlock2;
						Buf4Sv.NeedCheckBlockEmpty2 = 1;
					}
					break;
				}
				if(  (0 == Buf4Sv.EntryX ) && (0 == Buf4Sv.PageX) ){
					//Check Writing Block if Empty
					fAddr = dFlashSvBaseAddr + (dFlashBlockSize * Buf4Sv.BlockX);
					nor_read_byte(dFlashSvChip, fAddr, FlashPageReadData.D8, 8);
					if( (FlashPageReadData.D32[0] != 0xFFFFFFFF) || (FlashPageReadData.D32[1] != 0xFFFFFFFF) ){
						Buf4Sv.NeedCheckBlockEmpty = 1;
						Buf4Sv.BlockToBeErase = Buf4Sv.BlockX;
						GotNew = 0;
					}
					//Check Next Block if Need Erase
					aBlock2 = Buf4Sv.BlockX +1;
					if(aBlock2 >= dFrashSvUsingBlockCount)aBlock2 = 0;
					fAddr = dFlashSvBaseAddr + (dFlashBlockSize * aBlock2);
					nor_read_byte(dFlashSvChip, fAddr, FlashPageReadData.D8, 8);
					if( (FlashPageReadData.D32[0] != 0xFFFFFFFF) || (FlashPageReadData.D32[1] != 0xFFFFFFFF) ){
						Buf4Sv.NeedCheckBlockEmpty2 = 1;
						Buf4Sv.BlockToBeErase2 = aBlock2;
						GotNew = 0;
					}
					
					Buf4Sv.wBlockWriteToEep = Buf4Sv.BlockX;
					Buf4Sv.NeedMarkWriteBlcokToEep = 1;
				}
			}else{
				continue;
			}
		}
		if(GotNew){
			//write Here and Check Ok
			aAddr = dFlashSvBaseAddr + (dFlashBlockSize * Buf4Sv.BlockX) + (dFlashPageSize * Buf4Sv.PageX) + (dFlashEntrySize * Buf4Sv.EntryX);
			OK = 0;
			for(i=Buf4Sv.EntryX; i<dFlashEntryCountPerPage; i++){
				sysIndex.fIndex++;
				ToWriteBuf[i].D32[0] = sysIndex.D32.Inx0;
				ToWriteBuf[i].D32[1] = sysIndex.D32.Inx1;
				aCsum = 0;
				for(m=0; m<60; m++){
					aCsum += ToWriteBuf[i].D8[m];
				}
				ToWriteBuf[i].D32[15] = aCsum;
				OK += 1;
				if(OK >= GotNew){
					break;
				}
			}
			nor_write_byte_via_page(dFlashSvChip, aAddr, ToWriteBuf[Buf4Sv.EntryX].D8, GotNew * dFlashEntrySize);
			
			OK = 0;
			aAddr = dSdramSvHistoryContentBaseAddr + (Buf4Sv.BlockX * SizeOf64K) + (Buf4Sv.PageX * SizeOfPage) + (Buf4Sv.EntryX * dFlashEntrySize);
			for(i=Buf4Sv.EntryX; i<dFlashEntryCountPerPage; i++){
				for(BlockCount=0; BlockCount<16; BlockCount++){
					*(volatile unsigned int *) (aAddr + BlockCount * 4) = ToWriteBuf[i].D32[BlockCount];
				}
				OK += 1;
				if(OK >= GotNew){
					break;
				}
				aAddr += dFlashEntrySize;
			}
			
			//read Back To ToWriteBuf
			if(1){
				OK = 1;
			}else{
				OK = 0;
			}
			
			//OK
			if(OK){
				Buf4Sv.ReadIndex = aReadInx;
				Buf4Sv.BlockX = aBlock;
				Buf4Sv.PageX = aPage;
				Buf4Sv.EntryX = aEntry;
				
				BlockCount = ((Buf4Sv.BlockX + dFrashSvUsingBlockCount) - Buf4Sv.sBlock ) % dFrashSvUsingBlockCount +1;
				if(BlockCount > (dFrashSvUsingBlockCount -2)){
					Buf4Sv.sBlock = Buf4Sv.BlockX - (dFrashSvUsingBlockCount -3);
					if(Buf4Sv.sBlock <0)Buf4Sv.sBlock += dFrashSvUsingBlockCount;

					BlockCount = dFrashSvUsingBlockCount -2;
				}
				EntryCount = (BlockCount-1) * (dFlashBlockSize / dFlashEntrySize) + (Buf4Sv.PageX * (dFlashPageSize / dFlashEntrySize)) + Buf4Sv.EntryX ;
				if(EntryCount > dSdramHistoryEntryCount){
					Buf4Sv.Count = dSdramHistoryEntryCount;
				}else{
					Buf4Sv.Count = EntryCount;
				}
				Buf4Sv.SdramAddrCurrent = dSdramSvHistoryContentBaseAddr + (Buf4Sv.BlockX * SizeOf64K) + (Buf4Sv.PageX * dFlashPageSize) + (Buf4Sv.EntryX * dFlashEntrySize);
				Buf4Sv.SdramAddrStart = Buf4Sv.SdramAddrCurrent - (Buf4Sv.Count * 64);
				if(Buf4Sv.SdramAddrStart < dSdramSvHistoryContentBaseAddr){
					Buf4Sv.SdramAddrStart = Buf4Sv.SdramAddrStart + dSdramHistoryBytesLength;
				}
			}
		}
	}
}

void TRecord::GetFaultEntry(int aInx)
{
	volatile unsigned int aAddr,i;
	unsigned int aCn, aP0, aP1;
	aAddr = dAddrSdWrite2FlashBufFault + (aInx * 24);
	EntryRead.fData.Body.D32[0] = *(volatile unsigned int *)(aAddr + 0);
	EntryRead.fData.Body.D32[1] = *(volatile unsigned int *)(aAddr + 4);
	EntryRead.fData.Body.D32[2] = *(volatile unsigned int *)(aAddr + 8);
	EntryRead.fData.Body.D32[3] = *(volatile unsigned int *)(aAddr +12);
	EntryRead.fData.UserCode = *(volatile unsigned int *)(aAddr +16);
	if(EntryRead.fData.Body.Splite.PCtlNum == MainCtl.fData.Split.MyNum){
		aP0 = EntryRead.fData.Body.Splite.P0;
		aP1 = EntryRead.fData.Body.Splite.P1;
		if(aP0 && aP1){
			if(aP0 < dPORT_MAX_COUNT){
				aP0--;
				aP1--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + (aP1 * 32);
			for(i=7; i<15; i++){
				EntryRead.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}else{
			for(i=7; i<15; i++){
				EntryRead.D32[i] = 0;
			}
		}
	}else
	if( (EntryRead.fData.Body.Splite.PCtlNum) && (EntryRead.fData.Body.Splite.PCtlNum <= dNETWORK_MACHINE_COUNT) ){
		aCn = EntryRead.fData.Body.Splite.PCtlNum -1;
		aP0 = EntryRead.fData.Body.Splite.P0;
		aP1 = EntryRead.fData.Body.Splite.P1;
		if(aP0 && aP1){
			if(aP0 < dPORT_MAX_COUNT){
				aP0--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_DescpNw + (aCn*dONE_CTRL_DESCP_BYTES)+(aP0*8192) + (aP1*32);
			for(i=7; i<15; i++){
				EntryRead.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}else{
			for(i=7; i<15; i++){
				EntryRead.D32[i] = 0;
			}
		}
	}else{
		for(i=7; i<15; i++){
			EntryRead.D32[i] = 0;
		}
	}
}

void TRecord::PrcFaultWrite()
{
	int i, aReadInx ,OK, m;
	unsigned int aAddr, aState, GotNew;
	unsigned int aEntry, aPage, aBlock, aBlock2;
	int BlockCount, EntryCount;

	
	//Read Chip1 State Here 
	
	if(RtChip1.Sta == csIdle){
		aState = nor_get_busy_state(dFlashFaultChip);
		if(aState != 0)return;
		
		GotNew = 0;
		FlushToWriteBuf();
		aReadInx = Buf4Fault.ReadIndex;
		aEntry = Buf4Fault.EntryX;
		aPage = Buf4Fault.PageX;
		aBlock = Buf4Fault.BlockX;
		while(aReadInx != Buf4Fault.WriteIndex){
			aReadInx++;
			if(aReadInx >=  dWrite2FlashMaxCount){
				aReadInx = 0;
			}
			GetFaultEntry(aReadInx);
			if(EntryRead.fData.Body.D32[2] != 0){
				if(aEntry < dFlashEntryCountPerPage){
					ToWriteBuf[aEntry].D32[ 0] = EntryRead.D32[ 0];
					ToWriteBuf[aEntry].D32[ 1] = EntryRead.D32[ 1];
					ToWriteBuf[aEntry].D32[ 2] = EntryRead.D32[ 2];
					ToWriteBuf[aEntry].D32[ 3] = EntryRead.D32[ 3];
					ToWriteBuf[aEntry].D32[ 4] = EntryRead.D32[ 4];
					ToWriteBuf[aEntry].D32[ 5] = EntryRead.D32[ 5];
					ToWriteBuf[aEntry].D32[ 6] = EntryRead.D32[ 6];
					ToWriteBuf[aEntry].D32[ 7] = EntryRead.D32[ 7];
					ToWriteBuf[aEntry].D32[ 8] = EntryRead.D32[ 8];
					ToWriteBuf[aEntry].D32[ 9] = EntryRead.D32[ 9];
					ToWriteBuf[aEntry].D32[10] = EntryRead.D32[10];
					ToWriteBuf[aEntry].D32[11] = EntryRead.D32[11];
					ToWriteBuf[aEntry].D32[12] = EntryRead.D32[12];
					ToWriteBuf[aEntry].D32[13] = EntryRead.D32[13];
					ToWriteBuf[aEntry].D32[14] = EntryRead.D32[14];
					ToWriteBuf[aEntry].D32[15] = EntryRead.D32[15];
					GotNew ++;
					aEntry ++;
				}
				if(aEntry >= dFlashEntryCountPerPage){
					aEntry =0;
					aPage++;
					if(aPage >= dFlashPageCountPerBlock){
						aPage = 0;
						aBlock++;
						if(aBlock >= dFrashFaultUsingBlockCount)aBlock = 0;
						Buf4Fault.NeedCheckBlockEmpty = 1;
						Buf4Fault.BlockToBeErase = aBlock;
						aBlock2 = Buf4Fault.BlockToBeErase +1;
						if(aBlock2 >= dFrashFaultUsingBlockCount)aBlock2 = 0;
						Buf4Fault.BlockToBeErase2 = aBlock2;
						Buf4Fault.NeedCheckBlockEmpty2 = 1;
					}
					break;
				}
				if(  (0 == Buf4Fault.EntryX ) && (0 == Buf4Fault.PageX) ){
					//Check Writing Block if Empty
					fAddr = dFlashFaultBaseAddr + (dFlashBlockSize * Buf4Fault.BlockX);
					nor_read_byte(dFlashFaultChip, fAddr, FlashPageReadData.D8, 8);
					if( (FlashPageReadData.D32[0] != 0xFFFFFFFF) || (FlashPageReadData.D32[1] != 0xFFFFFFFF) ){
						Buf4Fault.NeedCheckBlockEmpty = 1;
						Buf4Fault.BlockToBeErase = Buf4Fault.BlockX;
						GotNew = 0;
					}
					//Check Next Block if Need Erase
					aBlock2 = Buf4Fault.BlockX +1;
					if(aBlock2 >= dFrashFaultUsingBlockCount)aBlock2 = 0;
					fAddr = dFlashFaultBaseAddr + (dFlashBlockSize * aBlock2);
					nor_read_byte(dFlashFaultChip, fAddr, FlashPageReadData.D8, 8);
					if( (FlashPageReadData.D32[0] != 0xFFFFFFFF) || (FlashPageReadData.D32[1] != 0xFFFFFFFF) ){
						Buf4Fault.NeedCheckBlockEmpty2 = 1;
						Buf4Fault.BlockToBeErase2 = aBlock2;
						GotNew = 0;
					}
					
					Buf4Fault.wBlockWriteToEep = Buf4Fault.BlockX;
					Buf4Fault.NeedMarkWriteBlcokToEep = 1;
				}
			}else{
				continue;
			}
		}
		if(GotNew){
			//write Here and Check Ok
			aAddr = dFlashFaultBaseAddr + (dFlashBlockSize * Buf4Fault.BlockX) + (dFlashPageSize * Buf4Fault.PageX) + (dFlashEntrySize * Buf4Fault.EntryX);
			OK = 0;
			for(i=Buf4Fault.EntryX; i<dFlashEntryCountPerPage; i++){
				sysIndex.fIndex++;
				ToWriteBuf[i].D32[0] = sysIndex.D32.Inx0;
				ToWriteBuf[i].D32[1] = sysIndex.D32.Inx1;
				aCsum = 0;
				for(m=0; m<60; m++){
					aCsum += ToWriteBuf[i].D8[m];
				}
				ToWriteBuf[i].D32[15] = aCsum;
				OK += 1;
				if(OK >= GotNew){
					break;
				}
			}
			nor_write_byte_via_page(dFlashFaultChip, aAddr, ToWriteBuf[Buf4Fault.EntryX].D8, GotNew * dFlashEntrySize);
			
			OK = 0;
			aAddr = dSdramFaultHistoryContentBaseAddr + (Buf4Fault.BlockX * SizeOf64K) + (Buf4Fault.PageX * SizeOfPage) + (Buf4Fault.EntryX * dFlashEntrySize);
			for(i=Buf4Fault.EntryX; i<dFlashEntryCountPerPage; i++){
				for(BlockCount=0; BlockCount<16; BlockCount++){
					*(volatile unsigned int *) (aAddr + BlockCount * 4) = ToWriteBuf[i].D32[BlockCount];
				}
				OK += 1;
				if(OK >= GotNew){
					break;
				}
				aAddr += dFlashEntrySize;
			}
			
			//read Back To ToWriteBuf
			if(1){
				OK = 1;
			}else{
				OK = 0;
			}
			
			//OK
			if(OK){
				Buf4Fault.ReadIndex = aReadInx;
				Buf4Fault.BlockX = aBlock;
				Buf4Fault.PageX = aPage;
				Buf4Fault.EntryX = aEntry;
				
				BlockCount = ((Buf4Fault.BlockX + dFrashFaultUsingBlockCount) - Buf4Fault.sBlock ) % dFrashFaultUsingBlockCount +1;
				if(BlockCount > (dFrashFaultUsingBlockCount -2)){
					Buf4Fault.sBlock = Buf4Fault.BlockX - (dFrashFaultUsingBlockCount -3);
					if(Buf4Fault.sBlock <0)Buf4Fault.sBlock += dFrashFaultUsingBlockCount;

					BlockCount = dFrashFaultUsingBlockCount -2;
				}
				EntryCount = (BlockCount-1) * (dFlashBlockSize / dFlashEntrySize) + (Buf4Fault.PageX * (dFlashPageSize / dFlashEntrySize)) + Buf4Fault.EntryX ;
				if(EntryCount > dSdramHistoryEntryCount){
					Buf4Fault.Count = dSdramHistoryEntryCount;
				}else{
					Buf4Fault.Count = EntryCount;
				}
				Buf4Fault.SdramAddrCurrent = dSdramFaultHistoryContentBaseAddr + (Buf4Fault.BlockX * SizeOf64K) + (Buf4Fault.PageX * dFlashPageSize) + (Buf4Fault.EntryX * dFlashEntrySize);
				Buf4Fault.SdramAddrStart = Buf4Fault.SdramAddrCurrent - (Buf4Fault.Count * 64);
				if(Buf4Fault.SdramAddrStart < dSdramFaultHistoryContentBaseAddr){
					Buf4Fault.SdramAddrStart = Buf4Fault.SdramAddrStart + dSdramHistoryBytesLength;
				}
			}
		}
	}
}

void TRecord::GetOperateEntry(int aInx)
{
	volatile unsigned int aAddr ,i, IsNeedFill;
	unsigned int aCn, aP0, aP1;
	unsigned char aOtType;
	aAddr = dAddrSdWrite2FlashBufOperate + (aInx * 24);
	EntryRead.fData.Body.D32[0] = *(volatile unsigned int *)(aAddr + 0);
	EntryRead.fData.Body.D32[1] = *(volatile unsigned int *)(aAddr + 4);
	EntryRead.fData.Body.D32[2] = *(volatile unsigned int *)(aAddr + 8);
	EntryRead.fData.Body.D32[3] = *(volatile unsigned int *)(aAddr +12);
	EntryRead.fData.UserCode = *(volatile unsigned int *)(aAddr +16);
	aOtType = EntryRead.fData.Body.Splite.Data0;
	IsNeedFill = 0;
	if(aOtType == otMask)IsNeedFill =2;
	else if(aOtType == otUnMask)IsNeedFill =2;
	else if(aOtType == otCfgDevice)IsNeedFill =2;
	else if(aOtType == otFireConfirm)IsNeedFill =2;
	else if(aOtType == otFireRemoteConfirm)IsNeedFill =2;
	else if(aOtType == otDescpModify)IsNeedFill =2;
	else if(aOtType == otFaultConfirm){
		if( (EntryRead.fData.Body.Splite.Data1 & 0xF0) == 0)IsNeedFill =2;
	}
	else if(aOtType == otFaultRemoteConfirm){
		if( (EntryRead.fData.Body.Splite.Data1 & 0xF0) == 0)IsNeedFill =2;
	}
	
	if(IsNeedFill == 0){
		for(i=7; i<15; i++){
			EntryRead.D32[i] = 0;
		}
		return;
	}
	
	if(EntryRead.fData.Body.Splite.PCtlNum == MainCtl.fData.Split.MyNum){
		aP0 = EntryRead.fData.Body.Splite.P0;
		aP1 = EntryRead.fData.Body.Splite.P1;
		if(aP0 && aP1){
			if(aP0 < dPORT_MAX_COUNT){
				aP0--;
				aP1--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + (aP1 * 32);
			for(i=7; i<15; i++){
				EntryRead.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}else{
			for(i=7; i<15; i++){
				EntryRead.D32[i] = 0;
			}
		}
	}else
	if( (EntryRead.fData.Body.Splite.PCtlNum) && (EntryRead.fData.Body.Splite.PCtlNum <= dNETWORK_MACHINE_COUNT) ){
		aCn = EntryRead.fData.Body.Splite.PCtlNum -1;
		aP0 = EntryRead.fData.Body.Splite.P0;
		aP1 = EntryRead.fData.Body.Splite.P1;
		if(aP0 && aP1){
			if(aP0 < dPORT_MAX_COUNT){
				aP0--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_DescpNw + (aCn*dONE_CTRL_DESCP_BYTES)+(aP0*8192) + (aP1*32);
			for(i=7; i<15; i++){
				EntryRead.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}else{
			for(i=7; i<15; i++){
				EntryRead.D32[i] = 0;
			}
		}
	}else{
		for(i=7; i<15; i++){
			EntryRead.D32[i] = 0;
		}
	}
}

void TRecord::PrcOperateWrite()
{
	int i, m, aReadInx ,OK;
	unsigned int aAddr, aState, GotNew;
	unsigned int aEntry, aPage, aBlock, aBlock2;
	int BlockCount;
	int EntryCount;

	//Read Chip1 State Here 
	if(RtChip1.Sta == csIdle){
		aState = nor_get_busy_state(dFlashOperateChip);
		if(aState != 0)return;
		
		GotNew = 0;
		FlushToWriteBuf();
		aReadInx = Buf4Operate.ReadIndex;
		aEntry = Buf4Operate.EntryX;
		aPage = Buf4Operate.PageX;
		aBlock = Buf4Operate.BlockX;
		while(aReadInx != Buf4Operate.WriteIndex){
			aReadInx++;
			if(aReadInx >=  dWrite2FlashMaxCount){
				aReadInx = 0;
			}
			GetOperateEntry(aReadInx);
			if(EntryRead.fData.Body.D32[2] != 0){
				if(aEntry < dFlashEntryCountPerPage){
					ToWriteBuf[aEntry].D32[ 0] = EntryRead.D32[ 0];
					ToWriteBuf[aEntry].D32[ 1] = EntryRead.D32[ 1];
					ToWriteBuf[aEntry].D32[ 2] = EntryRead.D32[ 2];
					ToWriteBuf[aEntry].D32[ 3] = EntryRead.D32[ 3];
					ToWriteBuf[aEntry].D32[ 4] = EntryRead.D32[ 4];
					ToWriteBuf[aEntry].D32[ 5] = EntryRead.D32[ 5];
					ToWriteBuf[aEntry].D32[ 6] = EntryRead.D32[ 6];
					ToWriteBuf[aEntry].D32[ 7] = EntryRead.D32[ 7];
					ToWriteBuf[aEntry].D32[ 8] = EntryRead.D32[ 8];
					ToWriteBuf[aEntry].D32[ 9] = EntryRead.D32[ 9];
					ToWriteBuf[aEntry].D32[10] = EntryRead.D32[10];
					ToWriteBuf[aEntry].D32[11] = EntryRead.D32[11];
					ToWriteBuf[aEntry].D32[12] = EntryRead.D32[12];
					ToWriteBuf[aEntry].D32[13] = EntryRead.D32[13];
					ToWriteBuf[aEntry].D32[14] = EntryRead.D32[14];
					ToWriteBuf[aEntry].D32[15] = EntryRead.D32[15];
					GotNew ++;
					aEntry ++;
				}
				if(aEntry >= dFlashEntryCountPerPage){
					aEntry =0;
					aPage++;
					if(aPage >= dFlashPageCountPerBlock){
						aPage = 0;
						aBlock++;
						if(aBlock >= dFrashOperateUsingBlockCount)aBlock = 0;
						Buf4Operate.NeedCheckBlockEmpty = 1;
						Buf4Operate.BlockToBeErase = aBlock;
						aBlock2 = Buf4Operate.BlockToBeErase +1;
						if(aBlock2 >= dFrashOperateUsingBlockCount)aBlock2 = 0;
						Buf4Operate.BlockToBeErase2 = aBlock2;
						Buf4Operate.NeedCheckBlockEmpty2 = 1;
					}
					break;
				}
				if(  (0 == Buf4Operate.EntryX ) && (0 == Buf4Operate.PageX) ){
					//Check Writing Block if Empty
					fAddr = dFlashOperateBaseAddr + (dFlashBlockSize * Buf4Operate.BlockX);
					nor_read_byte(dFlashOperateChip, fAddr, FlashPageReadData.D8, 8);
					if( (FlashPageReadData.D32[0] != 0xFFFFFFFF) || (FlashPageReadData.D32[1] != 0xFFFFFFFF) ){
						Buf4Operate.NeedCheckBlockEmpty = 1;
						Buf4Operate.BlockToBeErase = Buf4Operate.BlockX;
						GotNew = 0;
					}
					//Check Next Block if Need Erase
					aBlock2 = Buf4Operate.BlockX +1;
					if(aBlock2 >= dFrashOperateUsingBlockCount)aBlock2 = 0;
					fAddr = dFlashOperateBaseAddr + (dFlashBlockSize * aBlock2);
					nor_read_byte(dFlashOperateChip, fAddr, FlashPageReadData.D8, 8);
					if( (FlashPageReadData.D32[0] != 0xFFFFFFFF) || (FlashPageReadData.D32[1] != 0xFFFFFFFF) ){
						Buf4Operate.NeedCheckBlockEmpty2 = 1;
						Buf4Operate.BlockToBeErase2 = aBlock2;
						GotNew = 0;
					}
					
					Buf4Operate.wBlockWriteToEep = Buf4Operate.BlockX;
					Buf4Operate.NeedMarkWriteBlcokToEep = 1;
				}
			}else{
				continue;
			}
		}
		if(GotNew){
			//write Here and Check Ok
			aAddr = dFlashOperateBaseAddr + (dFlashBlockSize * Buf4Operate.BlockX) + (dFlashPageSize * Buf4Operate.PageX) + (dFlashEntrySize * Buf4Operate.EntryX);
			OK = 0;
			for(i=Buf4Operate.EntryX; i<dFlashEntryCountPerPage; i++){
				sysIndex.fIndex++;
				ToWriteBuf[i].D32[0] = sysIndex.D32.Inx0;
				ToWriteBuf[i].D32[1] = sysIndex.D32.Inx1;
				aCsum = 0;
				for(m=0; m<60; m++){
					aCsum += ToWriteBuf[i].D8[m];
				}
				ToWriteBuf[i].D32[15] = aCsum;
				OK += 1;
				if(OK >= GotNew){
					break;
				}
			}
			nor_write_byte_via_page(dFlashOperateChip, aAddr, ToWriteBuf[Buf4Operate.EntryX].D8, GotNew * dFlashEntrySize);
			
			OK = 0;
			aAddr = dSdramOperateHistoryContentBaseAddr + (Buf4Operate.BlockX * SizeOf64K) + (Buf4Operate.PageX * SizeOfPage) + (Buf4Operate.EntryX * dFlashEntrySize);
			for(i=Buf4Operate.EntryX; i<dFlashEntryCountPerPage; i++){
				for(BlockCount=0; BlockCount<16; BlockCount++){
					*(volatile unsigned int *) (aAddr + BlockCount * 4) = ToWriteBuf[i].D32[BlockCount];
				}
				OK += 1;
				if(OK >= GotNew){
					break;
				}
				aAddr += dFlashEntrySize;
			}
			
			//read Back To ToWriteBuf
			if(1){
				OK = 1;
			}else{
				OK = 0;
			}
			
			//OK
			if(OK){
				Buf4Operate.ReadIndex = aReadInx;
				Buf4Operate.BlockX = aBlock;
				Buf4Operate.PageX = aPage;
				Buf4Operate.EntryX = aEntry;
				
				BlockCount = ((Buf4Operate.BlockX + dFrashOperateUsingBlockCount) - Buf4Operate.sBlock ) % dFrashOperateUsingBlockCount +1;
				if(BlockCount > (dFrashOperateUsingBlockCount -2)){
					Buf4Operate.sBlock = Buf4Operate.BlockX - (dFrashOperateUsingBlockCount -3);
					if(Buf4Operate.sBlock <0)Buf4Operate.sBlock += dFrashOperateUsingBlockCount;

					BlockCount = dFrashOperateUsingBlockCount -2;
				}
				EntryCount = (BlockCount-1) * (dFlashBlockSize / dFlashEntrySize) + (Buf4Operate.PageX * (dFlashPageSize / dFlashEntrySize)) + Buf4Operate.EntryX ;
				if(EntryCount > dSdramHistoryEntryCount){
					Buf4Operate.Count = dSdramHistoryEntryCount;
				}else{
					Buf4Operate.Count = EntryCount;
				}
				Buf4Operate.SdramAddrCurrent = dSdramOperateHistoryContentBaseAddr + (Buf4Operate.BlockX * SizeOf64K) + (Buf4Operate.PageX * dFlashPageSize) + (Buf4Operate.EntryX * dFlashEntrySize);
				Buf4Operate.SdramAddrStart = Buf4Operate.SdramAddrCurrent - (Buf4Operate.Count * 64);
				if(Buf4Operate.SdramAddrStart < dSdramOperateHistoryContentBaseAddr){
					Buf4Operate.SdramAddrStart = Buf4Operate.SdramAddrStart + dSdramHistoryBytesLength;
				}
			}
		}
	}
}

void TRecord::Chip1Prc()
{
	unsigned int aAddr, aState;
	if(RtChip1.Sta == csIdle){
			RtChip1.SvFaultOpa++;
			if(RtChip1.SvFaultOpa >2)RtChip1.SvFaultOpa = 0;
			if(RtChip1.SvFaultOpa <0)RtChip1.SvFaultOpa = 0;
			if(RtChip1.SvFaultOpa == 0){
				//Process Sv Here
				if(Buf4Sv.NeedCheckBlockEmpty){
					aState = nor_get_busy_state(dFlashSvChip);
					if(aState == 0){
						aAddr = dFlashSvBaseAddr + (dFlashBlockSize * Buf4Sv.BlockToBeErase);
						nor_erase_sector_64k(dFlashSvChip,aAddr);
						RtChip1.Sta = csErassing;
						Buf4Sv.NeedCheckBlockEmpty = 0;
					}
				}else
				if(Buf4Sv.NeedCheckBlockEmpty2){
					aState = nor_get_busy_state(dFlashSvChip);
					if(aState == 0){
						aAddr = dFlashSvBaseAddr + (dFlashBlockSize * Buf4Sv.BlockToBeErase2);
						nor_erase_sector_64k(dFlashSvChip,aAddr);
						RtChip1.Sta = csErassing;
						Buf4Sv.NeedCheckBlockEmpty2 = 0;
					}
				}else{
					if(Buf4Sv.ReadIndex != Buf4Sv.WriteIndex){
						PrcSvWrite();
					}
				}
				if(Buf4Sv.NeedMarkWriteBlcokToEep){
					Wait10Ms();
					eeprom_write_byte_via_page(0, dEepAddrSvWritingBlock, &Buf4Sv.wBlockWriteToEep, 1);
					Wait10Ms();
					Buf4Sv.NeedMarkWriteBlcokToEep = 0;
				}
			}else
			if(RtChip1.SvFaultOpa == 1){
				//Process Fault Here
				if(Buf4Fault.NeedCheckBlockEmpty){
					aState = nor_get_busy_state(dFlashFaultChip);
					if(aState == 0){
						aAddr = dFlashFaultBaseAddr + (dFlashBlockSize * Buf4Fault.BlockToBeErase);
						nor_erase_sector_64k(dFlashFaultChip,aAddr);
						RtChip1.Sta = csErassing;
						Buf4Fault.NeedCheckBlockEmpty = 0;
					}
				}else
				if(Buf4Fault.NeedCheckBlockEmpty2){
					aState = nor_get_busy_state(dFlashFaultChip);
					if(aState == 0){
						aAddr = dFlashFaultBaseAddr + (dFlashBlockSize * Buf4Fault.BlockToBeErase2);
						nor_erase_sector_64k(dFlashFaultChip,aAddr);
						RtChip1.Sta = csErassing;
						Buf4Fault.NeedCheckBlockEmpty2 = 0;
					}
				}else{
					if(Buf4Fault.ReadIndex != Buf4Fault.WriteIndex){
						PrcFaultWrite();
					}
				}
				if(Buf4Fault.NeedMarkWriteBlcokToEep){
					Wait10Ms();
					eeprom_write_byte_via_page(0, dEepAddrFaultWritingBlock, &Buf4Fault.wBlockWriteToEep, 1);
					Wait10Ms();
					Buf4Fault.NeedMarkWriteBlcokToEep = 0;
				}
			}else
			if(RtChip1.SvFaultOpa == 2){
				//Process Oprate Here
				if(Buf4Operate.NeedCheckBlockEmpty){
					aState = nor_get_busy_state(dFlashOperateChip);
					if(aState == 0){
						aAddr = dFlashOperateBaseAddr + (dFlashBlockSize * Buf4Operate.BlockToBeErase);
						nor_erase_sector_64k(dFlashOperateChip,aAddr);
						RtChip1.Sta = csErassing;
						Buf4Operate.NeedCheckBlockEmpty = 0;
					}
				}else
				if(Buf4Operate.NeedCheckBlockEmpty2){
					aState = nor_get_busy_state(dFlashOperateChip);
					if(aState == 0){
						aAddr = dFlashOperateBaseAddr + (dFlashBlockSize * Buf4Operate.BlockToBeErase2);
						nor_erase_sector_64k(dFlashOperateChip,aAddr);
						RtChip1.Sta = csErassing;
						Buf4Operate.NeedCheckBlockEmpty2 = 0;
					}
				}else{
					if(Buf4Operate.ReadIndex != Buf4Operate.WriteIndex){
						PrcOperateWrite();
					}
				}
				if(Buf4Operate.NeedMarkWriteBlcokToEep){
					Wait10Ms();
					eeprom_write_byte_via_page(0, dEepAddrOperateWritingBlock, &Buf4Operate.wBlockWriteToEep, 1);
					Wait10Ms();
					Buf4Operate.NeedMarkWriteBlcokToEep = 0;
				}
			}
	}else
	if(RtChip1.Sta == csCanTryResume){
		if(0 == nor_get_busy_state(1)){
			RtChip1.Sta = csIdle;
		}else{
			RtChip1.Tick4_10Ms = EraseBlock64K_TryRusumeTime /2;
			RtChip1.Sta = csBusy;
		}
	}else{
		RtChip1.Tick4_10Ms ++;
		if(RtChip1.Tick4_10Ms > EraseBlock64K_TryRusumeTime){
			RtChip1.Sta = csCanTryResume;
		}
	}	
}

/*
void TRecord::MsgReadInit(unsigned int aChip, TBufToFlash *aBuf, unsigned int aFlashBaseAddr, int aPstY)
{
	unsigned int BaseAddr, aAddr, sAddr, nAddr, IsBlockFound, IsPageFound, IsEntryFound;
	int StartBlock, NextBlock;
	int StartPage, NextPage; 
	int StartEntry, NextEntry;
	unsigned char Data[4]; 
	int b, p, e;
	
	if(aChip > 1)return;
	
	StartBlock = 0;
	for(b=0; b<dFrashFireBlockCount; b++){
		aAddr = aFlashBaseAddr + (dFlashBlockSize * b);
		nor_read_byte(aChip,aAddr,Data,4);
		aAddr = dAddrSdRam_RuntimeBase + b*4;
		*(volatile unsigned char *)(aAddr +0) = Data[0];
		*(volatile unsigned char *)(aAddr +1) = Data[1];
		*(volatile unsigned char *)(aAddr +2) = Data[2];
		*(volatile unsigned char *)(aAddr +3) = Data[3];
	}
	IsBlockFound = 0;
	for(b=0; b<dFrashFireBlockCount; b++){
		sAddr = dAddrSdRam_RuntimeBase + b * 4;
		if( *(volatile unsigned int *)sAddr !=0xFFFFFFFF ){
			aBuf->BlockDataAva[b] = 1;
		}else{
			aBuf->BlockDataAva[b] = 0;
		}
	}
	for(b=0; b<dFrashFireBlockCount; b++){
		NextBlock = b +1;
		if(NextBlock >= dFrashFireBlockCount){
			NextBlock = 0;
		}
		nAddr = dAddrSdRam_RuntimeBase + NextBlock * 4;
		if( aBuf->BlockDataAva[b]  && (*(volatile unsigned int *)nAddr == 0xFFFFFFFF )  ){
			IsBlockFound =1;
			break;
		}
	}
	
	if(IsBlockFound){
		aBuf->BlockX = StartBlock;
		IsPageFound = 0;
		//Find Page
		for(p=0; p<dFlashPageCountPerBlock; p++){
			aAddr = aFlashBaseAddr + (dFlashBlockSize * StartBlock) + (dFlashBlockSize * p);
			nor_read_byte(aChip,aAddr,Data,4);
			aAddr = dAddrSdRam_RuntimeBase + p*4;
			*(volatile unsigned char *)(aAddr +0) = Data[0];
			*(volatile unsigned char *)(aAddr +1) = Data[1];
			*(volatile unsigned char *)(aAddr +2) = Data[2];
			*(volatile unsigned char *)(aAddr +3) = Data[3];
		}
		for(p=0; p<(dFlashPageCountPerBlock-1); p++){
			StartPage = p;
			NextPage = StartPage+1;
			sAddr = dAddrSdRam_RuntimeBase + p * 4;
			nAddr = dAddrSdRam_RuntimeBase + NextPage * 4;
			if(  (*(volatile unsigned int *)sAddr !=0xFFFFFFFF ) && (*(volatile unsigned int *)nAddr == 0xFFFFFFFF )  ){
				IsPageFound =1;
				break;
			}
		}
		if(IsPageFound){
			aBuf->PageX = StartPage;
		}else{
			aBuf->PageX = dFlashPageCountPerBlock -1;
		}
		aBuf->PageX = StartPage;
		IsEntryFound = 0;
		//Check Entry
		for(e=0; e<dFlashEntryCountPerPage; e++){
			StartEntry = e;
			aAddr = aFlashBaseAddr + (dFlashBlockSize * StartBlock) + (dFlashBlockSize * StartPage) + (e * dFlashEntrySize);
			nor_read_byte(aChip,aAddr,Data,4);
			aAddr = dAddrSdRam_RuntimeBase;
			*(volatile unsigned char *)(aAddr +0) = Data[0];
			*(volatile unsigned char *)(aAddr +1) = Data[1];
			*(volatile unsigned char *)(aAddr +2) = Data[2];
			*(volatile unsigned char *)(aAddr +3) = Data[3];
			if(  *(volatile unsigned int *)aAddr == 0xFFFFFFFF   ){
				IsEntryFound =1;
				break;
			}
		}
		if(IsEntryFound){
			aBuf->EntryX = StartEntry;
		}else{
			//this Page is Full
			aBuf->EntryX = 0;
			aBuf->PageX++;
			if(aBuf->PageX >= dFlashPageCountPerBlock){
				aBuf->PageX = 0;
				aBuf->BlockX++;
				if(aBuf->BlockX >= dFrashFireBlockCount)
					aBuf->BlockX =0;
			}
		}
	}else{
		//Start Block Not Found
		StartBlock = 0;
		for(b=0; b<dFrashFireBlockCount; b++){
			if(aBuf->BlockDataAva[b] == 1){
				StartBlock = 1;
			}
		}
		aBuf->BlockX = 0;
		aBuf->PageX = 0;
		aBuf->EntryX = 0;
	}
	if( (IsBlockFound == 0) && (StartBlock) ){
		//Flash Error Show it
		if(aBuf == &Buf4Fire)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fir:");
		else if(aBuf == &Buf4Linkage)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Lnk:");
		if(aBuf == &Buf4Sv)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Spv:");
		if(aBuf == &Buf4Fault)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fau:");
		if(aBuf == &Buf4Operate)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "OPA:");
		TextRender_string24(60, 	aPstY, clNearBlack, clNearWhite, "션쩌댄轎,헝헌뇜");
	}else{
		//Show
		if(aBuf == &Buf4Fire)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fir:");
		else if(aBuf == &Buf4Linkage)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Lnk:");
		if(aBuf == &Buf4Sv)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Spv:");
		if(aBuf == &Buf4Fault)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fau:");
		if(aBuf == &Buf4Operate)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "OPA:");
		TextRender_string24(60, 	aPstY, clNearBlack, clNearWhite, "BK");
		TextDigitRender3_24(90,		aPstY, clNearBlack, clNearWhite, aBuf->BlockX);
		TextRender_string24(130, 	aPstY, clNearBlack, clNearWhite, "PG");
		TextDigitRender3_24(160,	aPstY, clNearBlack, clNearWhite, aBuf->PageX);
		TextRender_string24(200, 	aPstY, clNearBlack, clNearWhite, "Ey");
		TextDigitRender3_24(230,	aPstY, clNearBlack, clNearWhite, aBuf->EntryX);
	}
}*/

/*
void TRecord::MsgReadInit2(unsigned int aChip, TBufToFlash *aBuf, unsigned int aFlashBaseAddr, int aPstY)
{
	unsigned int BaseAddr, aAddr, sAddr, nAddr, IsBlockFound, IsPageFound, IsEntryFound;
	int StartBlock, NextBlock;
	int StartPage, NextPage; 
	int StartEntry, NextEntry;
	int FoundStart, FoundEnd;
	int b, p, e;
	int Suce;
	
	if(aChip > 1)return;
	
	Suce =0;
	
	//Find Block_______________________________________________
	StartBlock = 0;
	IsBlockFound = 0;
	for(b=0; b<dFlashBlockCount; b++){
		FoundStart = 0;
		FoundEnd = 0;
		//Get First 
		aAddr = aFlashBaseAddr + (dFlashBlockSize * b);
		nor_read_byte(aChip,aAddr,EntryRead.D8,8);
		aAddr = dAddrSdRam_RuntimeBlockData + b*16;
		*(volatile unsigned int *)(aAddr + 0) = EntryRead.fData.sIndex.spl.sIndex0;
		*(volatile unsigned int *)(aAddr + 4) = EntryRead.fData.sIndex.spl.sIndex1;
		if(EntryRead.fData.sIndex.spl.sIndex0 != 0xFFFFFFFF){
			FoundStart = 1;
			aBuf->BlockDataFirstAva[b] = 1;
		}else{
			aBuf->BlockDataFirstAva[b] = 0;
		}
		
		//Get Last
		aAddr = aFlashBaseAddr + (dFlashBlockSize * b) + dFlashBlockSize - dFlashEntrySize;
		nor_read_byte(aChip,aAddr,EntryRead.D8,8);
		aAddr = dAddrSdRam_RuntimeBlockData + b*16;
		*(volatile unsigned int *)(aAddr + 8) = EntryRead.fData.sIndex.spl.sIndex0;
		*(volatile unsigned int *)(aAddr +12) = EntryRead.fData.sIndex.spl.sIndex1;
		if(EntryRead.fData.sIndex.spl.sIndex0 != 0xFFFFFFFF){
			FoundEnd = 1;
			aBuf->BlockDataEndAva[b] = 1;
		}else{
			aBuf->BlockDataEndAva[b] = 0;
		}
		if( (FoundStart) && (FoundEnd == 0) ){
			IsBlockFound++;
			StartBlock = b;
		}
	}
	
	if(IsBlockFound == 1){
		aBuf->BlockX = StartBlock;
		IsPageFound = 0;
		//Find Page______________________________________________
		for(p=0; p<dFlashPageCountPerBlock; p++){
			FoundStart = 0;
			FoundEnd = 0;
			aAddr = aFlashBaseAddr + (dFlashBlockSize * StartBlock) + (dFlashBlockSize * p);
			nor_read_byte(aChip,aAddr,EntryRead.D8,4);
			aAddr = dAddrSdRam_RuntimePageData + p*16;
			*(volatile unsigned int *)(aAddr +0) = EntryRead.fData.sIndex.spl.sIndex0;
			*(volatile unsigned int *)(aAddr +4) = EntryRead.fData.sIndex.spl.sIndex1;
			if(EntryRead.fData.sIndex.spl.sIndex0 != 0xFFFFFFFF){
				FoundStart = 1;
			}
			
			aAddr = aFlashBaseAddr + (dFlashBlockSize * StartBlock) + (dFlashBlockSize * p) - dFlashEntrySize;
			nor_read_byte(aChip,aAddr,EntryRead.D8,4);
			aAddr = dAddrSdRam_RuntimePageData + p*16;
			*(volatile unsigned int *)(aAddr + 8) = EntryRead.fData.sIndex.spl.sIndex0;
			*(volatile unsigned int *)(aAddr +12) = EntryRead.fData.sIndex.spl.sIndex1;
			if(EntryRead.fData.sIndex.spl.sIndex0 != 0xFFFFFFFF){
				FoundEnd = 1;
			}
			if( (FoundStart) && (FoundEnd == 0) ){
				IsPageFound++;
				StartPage = p;
			}
		}
		if(IsPageFound == 1){
			//Find Entry___________________________________________________
			for(e=0; e<(dFlashEntryCountPerPage -1); e++){
				StartEntry = e;
				aAddr = aFlashBaseAddr + (dFlashBlockSize * StartBlock) + (dFlashBlockSize * StartPage) + (e * dFlashEntrySize);
				nor_read_byte(aChip,aAddr,EntryRead.D8,4);
				aAddr = dAddrSdRam_RuntimeEntryData + e * 8;
				*(volatile unsigned int *)(aAddr +0) = EntryRead.fData.sIndex.spl.sIndex0;
				*(volatile unsigned int *)(aAddr +4) = EntryRead.fData.sIndex.spl.sIndex0;
				if(  *(volatile unsigned int *)aAddr == 0xFFFFFFFF   ){
					IsEntryFound =1;
					break;
				}
			}
		}else{
			aBuf->PageX = 0;
			
		}
	}else{
		StartBlock = 0;
	}
	
	for(b=0; b<dFrashFireBlockCount; b++){
		sAddr = dAddrSdRam_RuntimeBase + b * 4;
		if( *(volatile unsigned int *)sAddr !=0xFFFFFFFF ){
			aBuf->BlockDataAva[b] = 1;
		}else{
			aBuf->BlockDataAva[b] = 0;
		}
	}
	for(b=0; b<dFrashFireBlockCount; b++){
		NextBlock = b +1;
		if(NextBlock >= dFrashFireBlockCount){
			NextBlock = 0;
		}
		nAddr = dAddrSdRam_RuntimeBase + NextBlock * 4;
		if( aBuf->BlockDataAva[b]  && (*(volatile unsigned int *)nAddr == 0xFFFFFFFF )  ){
			IsBlockFound =1;
			break;
		}
	}
	
	if(IsBlockFound){
		aBuf->BlockX = StartBlock;
		IsPageFound = 0;
		//Find Page
		for(p=0; p<dFlashPageCountPerBlock; p++){
			aAddr = aFlashBaseAddr + (dFlashBlockSize * StartBlock) + (dFlashBlockSize * p);
			nor_read_byte(aChip,aAddr,Data,4);
			aAddr = dAddrSdRam_RuntimeBase + p*4;
			*(volatile unsigned char *)(aAddr +0) = Data[0];
			*(volatile unsigned char *)(aAddr +1) = Data[1];
			*(volatile unsigned char *)(aAddr +2) = Data[2];
			*(volatile unsigned char *)(aAddr +3) = Data[3];
		}
		for(p=0; p<(dFlashPageCountPerBlock-1); p++){
			StartPage = p;
			NextPage = StartPage+1;
			sAddr = dAddrSdRam_RuntimeBase + p * 4;
			nAddr = dAddrSdRam_RuntimeBase + NextPage * 4;
			if(  (*(volatile unsigned int *)sAddr !=0xFFFFFFFF ) && (*(volatile unsigned int *)nAddr == 0xFFFFFFFF )  ){
				IsPageFound =1;
				break;
			}
		}
		if(IsPageFound){
			aBuf->PageX = StartPage;
		}else{
			aBuf->PageX = dFlashPageCountPerBlock -1;
		}
		aBuf->PageX = StartPage;
		IsEntryFound = 0;
		//Check Entry
		for(e=0; e<dFlashEntryCountPerPage; e++){
			StartEntry = e;
			aAddr = aFlashBaseAddr + (dFlashBlockSize * StartBlock) + (dFlashBlockSize * StartPage) + (e * dFlashEntrySize);
			nor_read_byte(aChip,aAddr,Data,4);
			aAddr = dAddrSdRam_RuntimeBase;
			*(volatile unsigned char *)(aAddr +0) = Data[0];
			*(volatile unsigned char *)(aAddr +1) = Data[1];
			*(volatile unsigned char *)(aAddr +2) = Data[2];
			*(volatile unsigned char *)(aAddr +3) = Data[3];
			if(  *(volatile unsigned int *)aAddr == 0xFFFFFFFF   ){
				IsEntryFound =1;
				break;
			}
		}
		if(IsEntryFound){
			aBuf->EntryX = StartEntry;
		}else{
			//this Page is Full
			aBuf->EntryX = 0;
			aBuf->PageX++;
			if(aBuf->PageX >= dFlashPageCountPerBlock){
				aBuf->PageX = 0;
				aBuf->BlockX++;
				if(aBuf->BlockX >= dFrashFireBlockCount)
					aBuf->BlockX =0;
			}
		}
	}else{
		//Start Block Not Found
		StartBlock = 0;
		for(b=0; b<dFrashFireBlockCount; b++){
			if(aBuf->BlockDataAva[b] == 1){
				StartBlock = 1;
			}
		}
		aBuf->BlockX = 0;
		aBuf->PageX = 0;
		aBuf->EntryX = 0;
	}
	if( (IsBlockFound == 0) && (StartBlock) ){
		//Flash Error Show it
		if(aBuf == &Buf4Fire)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fir:");
		else if(aBuf == &Buf4Linkage)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Lnk:");
		if(aBuf == &Buf4Sv)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Spv:");
		if(aBuf == &Buf4Fault)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fau:");
		if(aBuf == &Buf4Operate)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "OPA:");
		TextRender_string24(60, 	aPstY, clNearBlack, clNearWhite, "션쩌댄轎,헝헌뇜");
	}else{
		//Show
		if(aBuf == &Buf4Fire)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fir:");
		else if(aBuf == &Buf4Linkage)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Lnk:");
		if(aBuf == &Buf4Sv)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Spv:");
		if(aBuf == &Buf4Fault)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fau:");
		if(aBuf == &Buf4Operate)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "OPA:");
		TextRender_string24(60, 	aPstY, clNearBlack, clNearWhite, "BK");
		TextDigitRender3_24(90,		aPstY, clNearBlack, clNearWhite, aBuf->BlockX);
		TextRender_string24(130, 	aPstY, clNearBlack, clNearWhite, "PG");
		TextDigitRender3_24(160,	aPstY, clNearBlack, clNearWhite, aBuf->PageX);
		TextRender_string24(200, 	aPstY, clNearBlack, clNearWhite, "Ey");
		TextDigitRender3_24(230,	aPstY, clNearBlack, clNearWhite, aBuf->EntryX);
	}
}*/


/*
void TRecord::MsgReadInit3(unsigned int aChip, TBufToFlash *aBuf, unsigned int aFlashBaseAddr, int aPstY)
{
	unsigned int BaseAddr, aAddr, sAddr, nAddr, IsBlockFound, IsPageFound, IsEntryFound;
	int StartBlock, NextBlock;
	int StartPage, NextPage; 
	int StartEntry, NextEntry;
	int FoundStart, FoundEnd;
	int EmptyFound;
	int b, p, e;
	int Suce;
	volatile unsigned int MaxBlockCount;
	unsigned long long aInx;
	
	if(aChip > 1)return;
	
	if(aBuf == &Buf4Fire){
		MaxBlockCount = dFrashFireUsingBlockCount;
		eeprom_read_byte(0, dEepAddrFireWritingBlock, &aBuf->WritingBlockReadFromEep, 1);
	}else if(aBuf == &Buf4Linkage){
		MaxBlockCount = dFrashLinkageUsingBlockCount;
		eeprom_read_byte(0, dEepAddrLinkageWritingBlock, &aBuf->WritingBlockReadFromEep, 1);
	}else if(aBuf == &Buf4Sv){
		MaxBlockCount = dFrashSvUsingBlockCount;
		eeprom_read_byte(0, dEepAddrSvWritingBlock, &aBuf->WritingBlockReadFromEep, 1);
	}else if(aBuf == &Buf4Fault){
		MaxBlockCount = dFrashFaultUsingBlockCount;
		eeprom_read_byte(0, dEepAddrFaultWritingBlock, &aBuf->WritingBlockReadFromEep, 1);
	}else if(aBuf == &Buf4Operate){
		MaxBlockCount = dFrashOperateUsingBlockCount;
		eeprom_read_byte(0, dEepAddrOpaWritingBlock, &aBuf->WritingBlockReadFromEep, 1);
	}else{ 
		return;
	}
	
	Suce =0;
	
	//Find Block_______________________________________________
	EmptyFound = 0;
	StartBlock = 0;
	IsBlockFound = 0;
	IsPageFound = 0;
	IsEntryFound = 0;
	//aBuf->eBlock = 255;
	aInx = 0;
	for(b=0; b<MaxBlockCount; b++){
		aAddr = aFlashBaseAddr + (dFlashBlockSize * b);
		nor_read_byte(aChip,aAddr,EntryRead.D8,8);
		aAddr = dAddrSdRam_RuntimeBlockData + b*8;
		*(volatile unsigned int *)(aAddr + 0) = EntryRead.fData.sIndex.spl.sIndex0;
		*(volatile unsigned int *)(aAddr + 4) = EntryRead.fData.sIndex.spl.sIndex1;
		if( (EntryRead.fData.sIndex.spl.sIndex0 == 0xFFFFFFFF) && (EntryRead.fData.sIndex.spl.sIndex1 == 0xFFFFFFFF) ){
			if(EmptyFound == 0)EmptyFound = 1;
		}else{
			if(EntryRead.fData.sIndex.fIndex > aInx ){
				aInx = EntryRead.fData.sIndex.fIndex;
				IsBlockFound = 1;
				StartBlock = b;
			}
			if(EmptyFound == 1){
				aBuf->sBlock = b;
				EmptyFound = 2;
			}
			if(EmptyFound == 0){
		//		aBuf->eBlock = b;
			}
		}
	}
	if(EmptyFound == 1){
		aBuf->sBlock = 0;
	}
	
	if(IsBlockFound){
		aInx = 0;
		for(p=0; p<dFlashPageCountPerBlock; p++){
			aAddr = aFlashBaseAddr + (dFlashBlockSize * StartBlock) + (dFlashPageSize * p);
			nor_read_byte(aChip,aAddr,EntryRead.D8,8);
			aAddr = dAddrSdRam_RuntimePageData + p*8;
			*(volatile unsigned int *)(aAddr +0) = EntryRead.fData.sIndex.spl.sIndex0;
			*(volatile unsigned int *)(aAddr +4) = EntryRead.fData.sIndex.spl.sIndex1;
			if( (EntryRead.fData.sIndex.spl.sIndex0 == 0xFFFFFFFF) && (EntryRead.fData.sIndex.spl.sIndex1 == 0xFFFFFFFF) ){
			}else{
				if(EntryRead.fData.sIndex.fIndex > aInx ){
					aInx = EntryRead.fData.sIndex.fIndex;
					IsPageFound = 1;
					StartPage = p;
				}
			}
		}
		
		if(IsPageFound){
			aInx = 0;
			for(e=0; e<(dFlashEntryCountPerPage -1); e++){
				aAddr = aFlashBaseAddr + (dFlashBlockSize * StartBlock) + (dFlashPageSize * StartPage) + (dFlashEntrySize * e);
				nor_read_byte(aChip,aAddr,EntryRead.D8,8);
				aAddr = dAddrSdRam_RuntimeEntryData + e * 8;
				*(volatile unsigned int *)(aAddr +0) = EntryRead.fData.sIndex.spl.sIndex0;
				*(volatile unsigned int *)(aAddr +4) = EntryRead.fData.sIndex.spl.sIndex0;
				if( (EntryRead.fData.sIndex.spl.sIndex0 == 0xFFFFFFFF) && (EntryRead.fData.sIndex.spl.sIndex1 == 0xFFFFFFFF) ){
				}else{
					if(EntryRead.fData.sIndex.fIndex > aInx ){
						aInx = EntryRead.fData.sIndex.fIndex;
						IsEntryFound = 1;
						StartEntry = e;
					}
				}
			}
			if(IsEntryFound){
				aBuf->fIndex.fIndex = aInx;
				StartEntry++;
				if(StartEntry >= (dFlashEntryCountPerPage) ){
					StartEntry = 0;
					StartPage++;
					if(StartPage >= dFlashPageCountPerBlock){
						StartPage = 0;
						StartBlock++;
						if(StartBlock >= MaxBlockCount ){
							StartBlock = 0;
						}
					}
				}
				aAddr = aFlashBaseAddr + (dFlashBlockSize * StartBlock) + (dFlashPageSize * StartPage) + (dFlashEntrySize * StartEntry);
				nor_read_byte(aChip,aAddr,EntryRead.D8,8);
				if( (EntryRead.fData.sIndex.spl.sIndex0 == 0xFFFFFFFF) && (EntryRead.fData.sIndex.spl.sIndex1 == 0xFFFFFFFF) ){
					aBuf->BlockX = StartBlock;
					aBuf->PageX = StartPage;
					aBuf->EntryX = StartEntry;
					Suce = 1;
				}
			}
		}else{
			
		}
	}else{
		aBuf->fIndex.fIndex = 1;
		aBuf->BlockX = 0;
		aBuf->PageX = 0;
		aBuf->EntryX = 0;
		Suce = 1;
	}
	
	
	if(Suce == 0){
		//Flash Error Show it
		if(aBuf == &Buf4Fire)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fir:");
		else if(aBuf == &Buf4Linkage)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Lnk:");
		if(aBuf == &Buf4Sv)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Spv:");
		if(aBuf == &Buf4Fault)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fau:");
		if(aBuf == &Buf4Operate)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "OPA:");
		TextRender_string24(60, 	aPstY, clNearBlack, clNearWhite, "션쩌댄轎,헝헌뇜");
		
		for(MaxBlockCount=0; MaxBlockCount<100000000; MaxBlockCount++);
	}else{
		//Show
		if(aBuf == &Buf4Fire)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fir:");
		else if(aBuf == &Buf4Linkage)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Lnk:");
		if(aBuf == &Buf4Sv)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Spv:");
		if(aBuf == &Buf4Fault)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "Fau:");
		if(aBuf == &Buf4Operate)
			TextRender_string24(2, 		aPstY, clNearBlack, clNearWhite, "OPA:");
		TextRender_string24(60, 	aPstY, clNearBlack, clNearWhite, "BK");
		TextDigitRender3_24(90,		aPstY, clNearBlack, clNearWhite, aBuf->BlockX);
		TextRender_string24(130, 	aPstY, clNearBlack, clNearWhite, "PG");
		TextDigitRender3_24(160,	aPstY, clNearBlack, clNearWhite, aBuf->PageX);
		TextRender_string24(200, 	aPstY, clNearBlack, clNearWhite, "Ey");
		TextDigitRender3_24(230,	aPstY, clNearBlack, clNearWhite, aBuf->EntryX);
	}
}*/
	


void TRecord::LoadFireHistory()
{
	volatile unsigned int i, apg, sAddr, m, ReFindMaxInx, AvaCount, CanGoFindPageAndEntry, Found, BlockCount;
	int FirstEmptyBlock =10000;
	int FirstAvaBlock =10000;
	int LastAvaBlock = 10000;
	int EntryCount;
	int x;
	THisIndex rInx, tInx;
	unsigned long long aInx;

	AvaCount = 0;
	CanGoFindPageAndEntry = 0;
	for(i=0; i<dFrashFireUsingBlockCount; i++){
		rInx.fIndex = 0;
		for(apg=0; apg<256; apg++){
			SysLoopTick = 0;
			fAddr = dFlashFireBaseAddr + (i * SizeOf64K) + (apg*256);
			nor_read_byte(dFlashFireChip,fAddr,FlashPageReadData.D8,256);
			
			sAddr = dSdramFireHistoryContentBaseAddr + (i * SizeOf64K) + (apg*256);
			for(m=0; m<64; m++){
				*(volatile unsigned int *)(sAddr + m *4) = FlashPageReadData.D32[m];
			}
			
			for(x=0; x<4; x++){
				if(CheckHistoryCSum(&FlashPageReadData.D8[dFlashEntrySize * x])){
					tInx.spl.sIndex0 = FlashPageReadData.D32[dFlashEntrySize / 4 * x + 0];
					tInx.spl.sIndex1 = FlashPageReadData.D32[dFlashEntrySize / 4 * x + 1];
					if(tInx.fIndex < 0x7FFFFFFF'FFFFFFFF){
						if(tInx.fIndex > rInx.fIndex){
							rInx.fIndex =tInx.fIndex; 
						}
					}
				}
			}
			/*if(apg == 0){
				if(  (FlashPageReadData.D32[0] == 0xFFFFFFFF) && (FlashPageReadData.D32[1] == 0xFFFFFFFF)  ){
					TmpIsAva[i] =0;
					TmpllIndex[i].fIndex = 0;
				}else{
					TmpIsAva[i] =1;
					AvaCount++;
					TmpllIndex[i].spl.sIndex0 = FlashPageReadData.D32[0];
					TmpllIndex[i].spl.sIndex1 = FlashPageReadData.D32[1];
				}
			}*/
			sys_error_led_task();
		}
		if(rInx.fIndex > 0){
			TmpIsAva[i] =1;
			AvaCount++;
			TmpllIndex[i].fIndex = rInx.fIndex;
		}else{
			TmpIsAva[i] =0;
			TmpllIndex[i].fIndex = 0;
		}
	}
	
	SysLoopTick = 0;
	if(AvaCount == 0){
		Buf4Fire.BlockX = 0;
		Buf4Fire.PageX = 0;
		Buf4Fire.EntryX = 0;
		Buf4Fire.sBlock = 0;
		Buf4Fire.Count =0;
		Buf4Fire.fIndex.fIndex = 2;
		Buf4Fire.SdramAddrStart = dSdramFireHistoryContentBaseAddr + (Buf4Fire.sBlock * SizeOf64K);
	}else
	if(AvaCount >= dFrashFireUsingBlockCount){
		//Find Max 
		aInx = TmpllIndex[0].fIndex;
		m =0;
		for(i=0; i<dFrashFireUsingBlockCount; i++){
			if(TmpllIndex[i].fIndex > aInx){
				aInx = TmpllIndex[i].fIndex;
				m = i;
			}
		}
		Buf4Fire.fIndex.fIndex = aInx;
		LastAvaBlock = m;
		Buf4Fire.BlockX = LastAvaBlock;
		m++;
		if(m >= dFrashFireUsingBlockCount)m = 0;
		FirstAvaBlock = m;
		Buf4Fire.sBlock = FirstAvaBlock;
		Buf4Fire.SdramAddrStart = dSdramFireHistoryContentBaseAddr + (FirstAvaBlock * SizeOf64K);
		
		CanGoFindPageAndEntry =1;
		sys_error_led_task();
	}else{
		if(TmpIsAva[0])FirstAvaBlock = 0;
		for(i=0; i<dFrashFireUsingBlockCount; i++){
			if(FirstEmptyBlock > 256){
				if(TmpIsAva[i] == 0){
					FirstEmptyBlock = i;
				}
			}else{
				if(TmpIsAva[i]){
					if( (FirstAvaBlock == 0) || (FirstAvaBlock >256))FirstAvaBlock = i;
				}
			}
			if(i<(dFrashFireUsingBlockCount-1)){
				if( (TmpIsAva[i]) && (TmpIsAva[i+1] ==0) ){
					LastAvaBlock = i;
				}
			}
		}
		if(LastAvaBlock > 256){
			LastAvaBlock = dFrashFireUsingBlockCount -1;
		}
		DelayMs(10);
		eeprom_read_byte(0, dEepAddrFireWritingBlock, &Buf4Fire.WritingBlockReadFromEep, 1);
		if(Buf4Fire.WritingBlockReadFromEep == LastAvaBlock){
			for(i=0; i<dFrashFireUsingBlockCount; i++){
				if(i != LastAvaBlock){
					if(TmpllIndex[i].fIndex >= TmpllIndex[LastAvaBlock].fIndex)ReFindMaxInx =1;
				}
			}
		}
		Buf4Fire.sBlock = FirstAvaBlock;
		Buf4Fire.BlockX = LastAvaBlock;
		
		CanGoFindPageAndEntry =1;
		sys_error_led_task();
	}
	
	//Find Out PageX And EntryX
	SysLoopTick = 0;
	if(CanGoFindPageAndEntry){
		Found = 0;
		sAddr = dSdramFireHistoryContentBaseAddr + (LastAvaBlock * SizeOf64K);
		for(i=0; i<((dFlashBlockSize / dFlashEntrySize) -1); i++ ){
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr + i*dFlashEntrySize +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr + i*dFlashEntrySize +4);
			CmpllIndex2.spl.sIndex0 = *(volatile unsigned int *)(sAddr + (i+1)*dFlashEntrySize +0);
			CmpllIndex2.spl.sIndex1 = *(volatile unsigned int *)(sAddr + (i+1)*dFlashEntrySize +4);
			if(  (CmpllIndex1.fIndex != 0xFFFFFFFFFFFFFFFF) && (CmpllIndex2.fIndex == 0xFFFFFFFFFFFFFFFF) ){
				Buf4Fire.PageX = (i+1) / dFlashEntryCountPerPage;
				Buf4Fire.EntryX = (i+1) % dFlashEntryCountPerPage;
				Buf4Fire.fIndex.fIndex = CmpllIndex1.fIndex;
				Found = 1;
				BlockCount = ((LastAvaBlock + dFrashFireUsingBlockCount) - FirstAvaBlock ) % dFrashFireUsingBlockCount;
				Buf4Fire.Count = BlockCount * (dFlashBlockSize / dFlashEntrySize) + i -1;
				if(Buf4Fire.Count <0)Buf4Fire.Count =0;
				break;
			}
		}
		if(Found == 0){
			Buf4Fire.BlockX++;
			if(Buf4Fire.BlockX >= dFrashFireUsingBlockCount){
				Buf4Fire.BlockX = 0;
			}
			Buf4Fire.PageX = 0;
			Buf4Fire.EntryX = 0;
			i = (dFlashBlockSize / dFlashEntrySize) -1;
			sAddr = dSdramFireHistoryContentBaseAddr + (LastAvaBlock * SizeOf64K);
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr + (i)*dFlashEntrySize +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr + (i)*dFlashEntrySize +4);
			Buf4Fire.fIndex.fIndex = CmpllIndex1.fIndex;
			BlockCount = ((LastAvaBlock + dFrashFireUsingBlockCount) - FirstAvaBlock ) % dFrashFireUsingBlockCount +1;
			Buf4Fire.Count = BlockCount * (dFlashBlockSize / dFlashEntrySize) -1;
			if(Buf4Fire.Count <0)Buf4Fire.Count =0;
		}
		
		BlockCount = ((Buf4Fire.BlockX + dFrashFireUsingBlockCount) - Buf4Fire.sBlock ) % dFrashFireUsingBlockCount +1;
		if(BlockCount > (dFrashFireUsingBlockCount -2)){
			Buf4Fire.sBlock = Buf4Fire.BlockX - (dFrashFireUsingBlockCount -3);
			if(Buf4Fire.sBlock <0)Buf4Fire.sBlock += dFrashFireUsingBlockCount;

			BlockCount = dFrashFireUsingBlockCount -2;
		}
		EntryCount = (BlockCount-1) * (dFlashBlockSize / dFlashEntrySize) + (Buf4Fire.PageX * (dFlashPageSize / dFlashEntrySize)) + Buf4Fire.EntryX ;
		if(EntryCount > dSdramHistoryEntryCount){
			Buf4Fire.Count = dSdramHistoryEntryCount;
		}else{
			Buf4Fire.Count = EntryCount;
		}
		Buf4Fire.SdramAddrCurrent = dSdramFireHistoryContentBaseAddr + (Buf4Fire.BlockX * SizeOf64K) + (Buf4Fire.PageX * dFlashPageSize) + (Buf4Fire.EntryX * dFlashEntrySize);
		Buf4Fire.SdramAddrStart = Buf4Fire.SdramAddrCurrent - (Buf4Fire.Count * 64);
		if(Buf4Fire.SdramAddrStart < dSdramFireHistoryContentBaseAddr){
			Buf4Fire.SdramAddrStart = Buf4Fire.SdramAddrStart + dSdramHistoryBytesLength;
		}
	}
	
	m=0;
	Buf4Fire.fIndex.fIndex = 0;
	EntryCount = Buf4Fire.Count -1;
	do{
		if(EntryCount < 0)break;
		sAddr = Buf4Fire.SdramAddrStart + (EntryCount * 64);
		if(sAddr >= dSdramFireHistoryContentBaseAddr + dSdramHistoryBytesLength)
			sAddr = sAddr - dSdramHistoryBytesLength;
		for(i=0; i<16; i++){
			FlashPageReadData.D32[i] = *(volatile unsigned int *)(sAddr + i *4);
		}
		AvaCount = CheckHistoryCSum(FlashPageReadData.D8);
		if(AvaCount){
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr +4);
			Buf4Fire.fIndex.fIndex = CmpllIndex1.fIndex;
			break;
		}
		EntryCount--;;
		m++;
	}while(m<20);
	
	if(1){
		TextRender_string24(2, 		80, clNearBlack, clNearWhite, "Fir:");
		TextRender_string24(60, 	80, clNearBlack, clNearWhite, "BK");
		TextDigitRender3_24(90,		80, clNearBlack, clNearWhite, Buf4Fire.BlockX);
		TextRender_string24(130, 	80, clNearBlack, clNearWhite, "PG");
		TextDigitRender3_24(160,	80, clNearBlack, clNearWhite, Buf4Fire.PageX);
		TextRender_string24(200, 	80, clNearBlack, clNearWhite, "Ey");
		TextDigitRender3_24(230,	80, clNearBlack, clNearWhite, Buf4Fire.EntryX);
	}
}

void TRecord::LoadLinkageHistory()
{
	volatile unsigned int i, apg, sAddr, m, ReFindMaxInx, AvaCount, CanGoFindPageAndEntry, Found, BlockCount;
	int FirstEmptyBlock =10000;
	int FirstAvaBlock =10000;
	int LastAvaBlock = 10000;
	int EntryCount;
	int x;
	THisIndex rInx, tInx;
	unsigned long long aInx;

	AvaCount = 0;
	CanGoFindPageAndEntry = 0;
	for(i=0; i<dFrashLinkageUsingBlockCount; i++){
		rInx.fIndex = 0;
		for(apg=0; apg<256; apg++){
			SysLoopTick = 0;
			fAddr = dFlashLinkageBaseAddr + (i * SizeOf64K) + (apg*256);
			nor_read_byte(dFlashLinkageChip,fAddr,FlashPageReadData.D8,256);
			
			sAddr = dSdramLinkageHistoryContentBaseAddr + (i * SizeOf64K) + (apg*256);
			for(m=0; m<64; m++){
				*(volatile unsigned int *)(sAddr + m *4) = FlashPageReadData.D32[m];
			}
			for(x=0; x<4; x++){
				if(CheckHistoryCSum(&FlashPageReadData.D8[dFlashEntrySize * x])){
					tInx.spl.sIndex0 = FlashPageReadData.D32[dFlashEntrySize / 4 * x + 0];
					tInx.spl.sIndex1 = FlashPageReadData.D32[dFlashEntrySize / 4 * x + 1];
					if(tInx.fIndex < 0x7FFFFFFF'FFFFFFFF){
						if(tInx.fIndex > rInx.fIndex){
							rInx.fIndex =tInx.fIndex; 
						}
					}
				}
			}
			/*if(apg == 0){
				if(  (FlashPageReadData.D32[0] == 0xFFFFFFFF) && (FlashPageReadData.D32[1] == 0xFFFFFFFF)  ){
					TmpIsAva[i] =0;
					TmpllIndex[i].fIndex = 0;
				}else{
					TmpIsAva[i] =1;
					AvaCount++;
					TmpllIndex[i].spl.sIndex0 = FlashPageReadData.D32[0];
					TmpllIndex[i].spl.sIndex1 = FlashPageReadData.D32[1];
				}
			}*/
			sys_error_led_task();
		}
		if(rInx.fIndex > 0){
			TmpIsAva[i] =1;
			AvaCount++;
			TmpllIndex[i].fIndex = rInx.fIndex;
		}else{
			TmpIsAva[i] =0;
			TmpllIndex[i].fIndex = 0;
		}
	}
	
	SysLoopTick = 0;
	if(AvaCount == 0){
		Buf4Linkage.BlockX = 0;
		Buf4Linkage.PageX = 0;
		Buf4Linkage.EntryX = 0;
		Buf4Linkage.sBlock = 0;
		Buf4Linkage.Count =0;
		Buf4Linkage.fIndex.fIndex = 2;
		Buf4Linkage.SdramAddrStart = dSdramLinkageHistoryContentBaseAddr + (Buf4Linkage.sBlock * SizeOf64K);
	}else
	if(AvaCount >= dFrashLinkageUsingBlockCount){
		//Find Max 
		aInx = TmpllIndex[0].fIndex;
		m =0;
		for(i=0; i<dFrashLinkageUsingBlockCount; i++){
			if(TmpllIndex[i].fIndex > aInx){
				aInx = TmpllIndex[i].fIndex;
				m = i;
			}
		}
		Buf4Linkage.fIndex.fIndex = aInx;
		LastAvaBlock = m;
		Buf4Linkage.BlockX = LastAvaBlock;
		m++;
		if(m >= dFrashLinkageUsingBlockCount)m = 0;
		FirstAvaBlock = m;
		Buf4Linkage.sBlock = FirstAvaBlock;
		Buf4Linkage.SdramAddrStart = dSdramLinkageHistoryContentBaseAddr + (FirstAvaBlock * SizeOf64K);
		
		CanGoFindPageAndEntry =1;
	}else{
		if(TmpIsAva[0])FirstAvaBlock = 0;
		for(i=0; i<dFrashLinkageUsingBlockCount; i++){
			if(FirstEmptyBlock > 256){
				if(TmpIsAva[i] == 0){
					FirstEmptyBlock = i;
				}
			}else{
				if(TmpIsAva[i]){
					if( (FirstAvaBlock == 0) || (FirstAvaBlock >256))FirstAvaBlock = i;
				}
			}
			if(i<(dFrashLinkageUsingBlockCount-1)){
				if( (TmpIsAva[i]) && (TmpIsAva[i+1] ==0) ){
					LastAvaBlock = i;
				}
			}
		}
		if(LastAvaBlock > 256){
			LastAvaBlock = dFrashLinkageUsingBlockCount -1;
		}
		DelayMs(10);
		eeprom_read_byte(0, dEepAddrLinkageWritingBlock, &Buf4Linkage.WritingBlockReadFromEep, 1);
		if(Buf4Linkage.WritingBlockReadFromEep == LastAvaBlock){
			for(i=0; i<dFrashLinkageUsingBlockCount; i++){
				if(i != LastAvaBlock){
					if(TmpllIndex[i].fIndex >= TmpllIndex[LastAvaBlock].fIndex)ReFindMaxInx =1;
				}
			}
		}
		Buf4Linkage.sBlock = FirstAvaBlock;
		Buf4Linkage.BlockX = LastAvaBlock;
		
		CanGoFindPageAndEntry =1;
		
		sys_error_led_task();
	}
	
	//Find Out PageX And EntryX
	
	SysLoopTick = 0;
	if(CanGoFindPageAndEntry){
		Found = 0;
		sAddr = dSdramLinkageHistoryContentBaseAddr + (LastAvaBlock * SizeOf64K);
		for(i=0; i<((dFlashBlockSize / dFlashEntrySize) -1); i++ ){
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr + i*dFlashEntrySize +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr + i*dFlashEntrySize +4);
			CmpllIndex2.spl.sIndex0 = *(volatile unsigned int *)(sAddr + (i+1)*dFlashEntrySize +0);
			CmpllIndex2.spl.sIndex1 = *(volatile unsigned int *)(sAddr + (i+1)*dFlashEntrySize +4);
			if(  (CmpllIndex1.fIndex != 0xFFFFFFFFFFFFFFFF) && (CmpllIndex2.fIndex == 0xFFFFFFFFFFFFFFFF) ){
				Buf4Linkage.PageX = (i+1) / dFlashEntryCountPerPage;
				Buf4Linkage.EntryX = (i+1) % dFlashEntryCountPerPage;
				Buf4Linkage.fIndex.fIndex = CmpllIndex1.fIndex;
				Found = 1;
				BlockCount = ((LastAvaBlock + dFrashLinkageUsingBlockCount) - FirstAvaBlock ) % dFrashLinkageUsingBlockCount;
				Buf4Linkage.Count = BlockCount * (dFlashBlockSize / dFlashEntrySize) + i -1;
				if(Buf4Linkage.Count <0)Buf4Linkage.Count =0;
				break;
			}
		}
		if(Found == 0){
			Buf4Linkage.BlockX++;
			if(Buf4Linkage.BlockX >= dFrashLinkageUsingBlockCount){
				Buf4Linkage.BlockX = 0;
			}
			Buf4Linkage.PageX = 0;
			Buf4Linkage.EntryX = 0;
			i = (dFlashBlockSize / dFlashEntrySize) -1;
			sAddr = dSdramLinkageHistoryContentBaseAddr + (LastAvaBlock * SizeOf64K);
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr + (i)*dFlashEntrySize +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr + (i)*dFlashEntrySize +4);
			Buf4Linkage.fIndex.fIndex = CmpllIndex1.fIndex;
			BlockCount = ((LastAvaBlock + dFrashLinkageUsingBlockCount) - FirstAvaBlock ) % dFrashLinkageUsingBlockCount +1;
			Buf4Linkage.Count = BlockCount * (dFlashBlockSize / dFlashEntrySize) -1;
			if(Buf4Linkage.Count <0)Buf4Linkage.Count =0;
		}
		
		BlockCount = ((Buf4Linkage.BlockX + dFrashLinkageUsingBlockCount) - Buf4Linkage.sBlock ) % dFrashLinkageUsingBlockCount +1;
		if(BlockCount > (dFrashLinkageUsingBlockCount -2)){
			Buf4Linkage.sBlock = Buf4Linkage.BlockX - (dFrashLinkageUsingBlockCount -3);
			if(Buf4Linkage.sBlock <0)Buf4Linkage.sBlock += dFrashLinkageUsingBlockCount;

			BlockCount = dFrashLinkageUsingBlockCount -2;
		}
		EntryCount = (BlockCount-1) * (dFlashBlockSize / dFlashEntrySize) + (Buf4Linkage.PageX * (dFlashPageSize / dFlashEntrySize)) + Buf4Linkage.EntryX ;
		if(EntryCount > dSdramHistoryEntryCount){
			Buf4Linkage.Count = dSdramHistoryEntryCount;
		}else{
			Buf4Linkage.Count = EntryCount;
		}
		Buf4Linkage.SdramAddrCurrent = dSdramLinkageHistoryContentBaseAddr + (Buf4Linkage.BlockX * SizeOf64K) + (Buf4Linkage.PageX * dFlashPageSize) + (Buf4Linkage.EntryX * dFlashEntrySize);
		Buf4Linkage.SdramAddrStart = Buf4Linkage.SdramAddrCurrent - (Buf4Linkage.Count * 64);
		if(Buf4Linkage.SdramAddrStart < dSdramLinkageHistoryContentBaseAddr){
			Buf4Linkage.SdramAddrStart = Buf4Linkage.SdramAddrStart + dSdramHistoryBytesLength;
		}
	}
	
	SysLoopTick = 0;
	m=0;
	Buf4Linkage.fIndex.fIndex = 0;
	EntryCount = Buf4Linkage.Count -1;
	do{
		if(EntryCount < 0)break;
		sAddr = Buf4Linkage.SdramAddrStart + (EntryCount * 64);
		if(sAddr >= dSdramLinkageHistoryContentBaseAddr + dSdramHistoryBytesLength)
			sAddr = sAddr - dSdramHistoryBytesLength;
		for(i=0; i<16; i++){
			FlashPageReadData.D32[i] = *(volatile unsigned int *)(sAddr + i *4);
		}
		AvaCount = CheckHistoryCSum(FlashPageReadData.D8);
		if(AvaCount){
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr +4);
			Buf4Linkage.fIndex.fIndex = CmpllIndex1.fIndex;
			break;
		}
		EntryCount--;;
		m++;
	}while(m<20);
	
	if(1){
		TextRender_string24(2, 		110, clNearBlack, clNearWhite, "LNK:");
		TextRender_string24(60, 	110, clNearBlack, clNearWhite, "BK");
		TextDigitRender3_24(90,		110, clNearBlack, clNearWhite, Buf4Linkage.BlockX);
		TextRender_string24(130, 	110, clNearBlack, clNearWhite, "PG");
		TextDigitRender3_24(160,	110, clNearBlack, clNearWhite, Buf4Linkage.PageX);
		TextRender_string24(200, 	110, clNearBlack, clNearWhite, "Ey");
		TextDigitRender3_24(230,	110, clNearBlack, clNearWhite, Buf4Linkage.EntryX);
	}	
}

void TRecord::LoadSvHistory()
{
	volatile unsigned int i, apg, sAddr, m, ReFindMaxInx, AvaCount, CanGoFindPageAndEntry, Found, BlockCount;
	int FirstEmptyBlock =10000;
	int FirstAvaBlock =10000;
	int LastAvaBlock = 10000;
	int EntryCount;
	int x;
	THisIndex rInx, tInx;
	unsigned long long aInx;

	AvaCount = 0;
	CanGoFindPageAndEntry = 0;
	for(i=0; i<dFrashSvUsingBlockCount; i++){
		rInx.fIndex = 0;
		for(apg=0; apg<256; apg++){
			SysLoopTick = 0;
			fAddr = dFlashSvBaseAddr + (i * SizeOf64K) + (apg*256);
			nor_read_byte(dFlashSvChip,fAddr,FlashPageReadData.D8,256);
			
			sAddr = dSdramSvHistoryContentBaseAddr + (i * SizeOf64K) + (apg*256);
			for(m=0; m<64; m++){
				*(volatile unsigned int *)(sAddr + m *4) = FlashPageReadData.D32[m];
			}
			for(x=0; x<4; x++){
				if(CheckHistoryCSum(&FlashPageReadData.D8[dFlashEntrySize * x])){
					tInx.spl.sIndex0 = FlashPageReadData.D32[dFlashEntrySize / 4 * x + 0];
					tInx.spl.sIndex1 = FlashPageReadData.D32[dFlashEntrySize / 4 * x + 1];
					if(tInx.fIndex < 0x7FFFFFFF'FFFFFFFF){
						if(tInx.fIndex > rInx.fIndex){
							rInx.fIndex =tInx.fIndex; 
						}
					}
				}
			}
			/*if(apg == 0){
				if(  (FlashPageReadData.D32[0] == 0xFFFFFFFF) && (FlashPageReadData.D32[1] == 0xFFFFFFFF)  ){
					TmpIsAva[i] =0;
					TmpllIndex[i].fIndex = 0;
				}else{
					TmpIsAva[i] =1;
					AvaCount++;
					TmpllIndex[i].spl.sIndex0 = FlashPageReadData.D32[0];
					TmpllIndex[i].spl.sIndex1 = FlashPageReadData.D32[1];
				}
			}*/
			sys_error_led_task();
		}
		if(rInx.fIndex > 0){
			TmpIsAva[i] =1;
			AvaCount++;
			TmpllIndex[i].fIndex = rInx.fIndex;
		}else{
			TmpIsAva[i] =0;
			TmpllIndex[i].fIndex = 0;
		}
	}
	
	SysLoopTick = 0;
	if(AvaCount == 0){
		Buf4Sv.BlockX = 0;
		Buf4Sv.PageX = 0;
		Buf4Sv.EntryX = 0;
		Buf4Sv.sBlock = 0;
		Buf4Sv.Count =0;
		Buf4Sv.fIndex.fIndex = 2;
		Buf4Sv.SdramAddrStart = dSdramSvHistoryContentBaseAddr + (Buf4Sv.sBlock * SizeOf64K);
	}else
	if(AvaCount >= dFrashSvUsingBlockCount){
		//Find Max 
		aInx = TmpllIndex[0].fIndex;
		m =0;
		for(i=0; i<dFrashSvUsingBlockCount; i++){
			if(TmpllIndex[i].fIndex > aInx){
				aInx = TmpllIndex[i].fIndex;
				m = i;
			}
		}
		Buf4Sv.fIndex.fIndex = aInx;
		LastAvaBlock = m;
		Buf4Sv.BlockX = LastAvaBlock;
		m++;
		if(m >= dFrashSvUsingBlockCount)m = 0;
		FirstAvaBlock = m;
		Buf4Sv.sBlock = FirstAvaBlock;
		Buf4Sv.SdramAddrStart = dSdramSvHistoryContentBaseAddr + (FirstAvaBlock * SizeOf64K);
		
		CanGoFindPageAndEntry =1;
		sys_error_led_task();
	}else{
		if(TmpIsAva[0])FirstAvaBlock = 0;
		for(i=0; i<dFrashSvUsingBlockCount; i++){
			if(FirstEmptyBlock > 256){
				if(TmpIsAva[i] == 0){
					FirstEmptyBlock = i;
				}
			}else{
				if(TmpIsAva[i]){
					if( (FirstAvaBlock == 0) || (FirstAvaBlock >256))FirstAvaBlock = i;
				}
			}
			if(i<(dFrashSvUsingBlockCount-1)){
				if( (TmpIsAva[i]) && (TmpIsAva[i+1] ==0) ){
					LastAvaBlock = i;
				}
			}
		}
		if(LastAvaBlock > 256){
			LastAvaBlock = dFrashSvUsingBlockCount -1;
		}
		DelayMs(10);
		eeprom_read_byte(0, dEepAddrSvWritingBlock, &Buf4Sv.WritingBlockReadFromEep, 1);
		if(Buf4Sv.WritingBlockReadFromEep == LastAvaBlock){
			for(i=0; i<dFrashSvUsingBlockCount; i++){
				if(i != LastAvaBlock){
					if(TmpllIndex[i].fIndex >= TmpllIndex[LastAvaBlock].fIndex)ReFindMaxInx =1;
				}
			}
		}
		Buf4Sv.sBlock = FirstAvaBlock;
		Buf4Sv.BlockX = LastAvaBlock;
		
		CanGoFindPageAndEntry =1;
		sys_error_led_task();
	}
	
	//Find Out PageX And EntryX
	SysLoopTick = 0;
	if(CanGoFindPageAndEntry){
		Found = 0;
		sAddr = dSdramSvHistoryContentBaseAddr + (LastAvaBlock * SizeOf64K);
		for(i=0; i<((dFlashBlockSize / dFlashEntrySize) -1); i++ ){
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr + i*dFlashEntrySize +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr + i*dFlashEntrySize +4);
			CmpllIndex2.spl.sIndex0 = *(volatile unsigned int *)(sAddr + (i+1)*dFlashEntrySize +0);
			CmpllIndex2.spl.sIndex1 = *(volatile unsigned int *)(sAddr + (i+1)*dFlashEntrySize +4);
			if(  (CmpllIndex1.fIndex != 0xFFFFFFFFFFFFFFFF) && (CmpllIndex2.fIndex == 0xFFFFFFFFFFFFFFFF) ){
				Buf4Sv.PageX = (i+1) / dFlashEntryCountPerPage;
				Buf4Sv.EntryX = (i+1) % dFlashEntryCountPerPage;
				Buf4Sv.fIndex.fIndex = CmpllIndex1.fIndex;
				Found = 1;
				BlockCount = ((LastAvaBlock + dFrashSvUsingBlockCount) - FirstAvaBlock ) % dFrashSvUsingBlockCount;
				Buf4Sv.Count = BlockCount * (dFlashBlockSize / dFlashEntrySize) + i -1;
				if(Buf4Sv.Count <0)Buf4Sv.Count =0;
				break;
			}
		}
		if(Found == 0){
			Buf4Sv.BlockX++;
			if(Buf4Sv.BlockX >= dFrashSvUsingBlockCount){
				Buf4Sv.BlockX = 0;
			}
			Buf4Sv.PageX = 0;
			Buf4Sv.EntryX = 0;
			i = (dFlashBlockSize / dFlashEntrySize) -1;
			sAddr = dSdramSvHistoryContentBaseAddr + (LastAvaBlock * SizeOf64K);
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr + (i)*dFlashEntrySize +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr + (i)*dFlashEntrySize +4);
			Buf4Sv.fIndex.fIndex = CmpllIndex1.fIndex;
			BlockCount = ((LastAvaBlock + dFrashSvUsingBlockCount) - FirstAvaBlock ) % dFrashSvUsingBlockCount +1;
			Buf4Sv.Count = BlockCount * (dFlashBlockSize / dFlashEntrySize) -1;
			if(Buf4Sv.Count <0)Buf4Sv.Count =0;
		}
		
		BlockCount = ((Buf4Sv.BlockX + dFrashSvUsingBlockCount) - Buf4Sv.sBlock ) % dFrashSvUsingBlockCount +1;
		if(BlockCount > (dFrashSvUsingBlockCount -2)){
			Buf4Sv.sBlock = Buf4Sv.BlockX - (dFrashSvUsingBlockCount -3);
			if(Buf4Sv.sBlock <0)Buf4Sv.sBlock += dFrashSvUsingBlockCount;

			BlockCount = dFrashSvUsingBlockCount -2;
		}
		EntryCount = (BlockCount-1) * (dFlashBlockSize / dFlashEntrySize) + (Buf4Sv.PageX * (dFlashPageSize / dFlashEntrySize)) + Buf4Sv.EntryX ;
		if(EntryCount > dSdramHistoryEntryCount){
			Buf4Sv.Count = dSdramHistoryEntryCount;
		}else{
			Buf4Sv.Count = EntryCount;
		}
		Buf4Sv.SdramAddrCurrent = dSdramSvHistoryContentBaseAddr + (Buf4Sv.BlockX * SizeOf64K) + (Buf4Sv.PageX * dFlashPageSize) + (Buf4Sv.EntryX * dFlashEntrySize);
		Buf4Sv.SdramAddrStart = Buf4Sv.SdramAddrCurrent - (Buf4Sv.Count * 64);
		if(Buf4Sv.SdramAddrStart < dSdramSvHistoryContentBaseAddr){
			Buf4Sv.SdramAddrStart = Buf4Sv.SdramAddrStart + dSdramHistoryBytesLength;
		}
		sys_error_led_task();
	}
	
	SysLoopTick = 0;
	m=0;
	Buf4Sv.fIndex.fIndex = 0;
	EntryCount = Buf4Sv.Count -1;
	do{
		if(EntryCount < 0)break;
		sAddr = Buf4Sv.SdramAddrStart + (EntryCount * 64);
		if(sAddr >= dSdramSvHistoryContentBaseAddr + dSdramHistoryBytesLength)
			sAddr = sAddr - dSdramHistoryBytesLength;
		for(i=0; i<16; i++){
			FlashPageReadData.D32[i] = *(volatile unsigned int *)(sAddr + i *4);
		}
		AvaCount = CheckHistoryCSum(FlashPageReadData.D8);
		if(AvaCount){
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr +4);
			Buf4Sv.fIndex.fIndex = CmpllIndex1.fIndex;
			break;
		}
		EntryCount--;;
		m++;
	}while(m<20);
	
	if(1){
		TextRender_string24(2, 		140, clNearBlack, clNearWhite, "Spv:");
		TextRender_string24(60, 	140, clNearBlack, clNearWhite, "BK");
		TextDigitRender3_24(90,		140, clNearBlack, clNearWhite, Buf4Sv.BlockX);
		TextRender_string24(130, 	140, clNearBlack, clNearWhite, "PG");
		TextDigitRender3_24(160,	140, clNearBlack, clNearWhite, Buf4Sv.PageX);
		TextRender_string24(200, 	140, clNearBlack, clNearWhite, "Ey");
		TextDigitRender3_24(230,	140, clNearBlack, clNearWhite, Buf4Sv.EntryX);
	}	
}

void TRecord::LoadFaultHistory()
{
	volatile unsigned int i, apg, sAddr, m, ReFindMaxInx, AvaCount, CanGoFindPageAndEntry, Found, BlockCount;
	int FirstEmptyBlock =10000;
	int FirstAvaBlock =10000;
	int LastAvaBlock = 10000;
	int EntryCount;
	int x;
	THisIndex rInx, tInx;
	unsigned long long aInx;

	AvaCount = 0;
	CanGoFindPageAndEntry = 0;
	for(i=0; i<dFrashFaultUsingBlockCount; i++){
		rInx.fIndex = 0;
		for(apg=0; apg<256; apg++){
			SysLoopTick = 0;
			fAddr = dFlashFaultBaseAddr + (i * SizeOf64K) + (apg*256);
			nor_read_byte(dFlashFaultChip,fAddr,FlashPageReadData.D8,256);
			
			sAddr = dSdramFaultHistoryContentBaseAddr + (i * SizeOf64K) + (apg*256);
			for(m=0; m<64; m++){
				*(volatile unsigned int *)(sAddr + m *4) = FlashPageReadData.D32[m];
			}
			for(x=0; x<4; x++){
				if(CheckHistoryCSum(&FlashPageReadData.D8[dFlashEntrySize * x])){
					tInx.spl.sIndex0 = FlashPageReadData.D32[dFlashEntrySize / 4 * x + 0];
					tInx.spl.sIndex1 = FlashPageReadData.D32[dFlashEntrySize / 4 * x + 1];
					if(tInx.fIndex < 0x7FFFFFFF'FFFFFFFF){
						if(tInx.fIndex > rInx.fIndex){
							rInx.fIndex =tInx.fIndex; 
						}
					}
				}
			}
			/*if(apg == 0){
				if(  (FlashPageReadData.D32[0] == 0xFFFFFFFF) && (FlashPageReadData.D32[1] == 0xFFFFFFFF)  ){
					TmpIsAva[i] =0;
					TmpllIndex[i].fIndex = 0;
				}else{
					TmpIsAva[i] =1;
					AvaCount++;
					TmpllIndex[i].spl.sIndex0 = FlashPageReadData.D32[0];
					TmpllIndex[i].spl.sIndex1 = FlashPageReadData.D32[1];
				}
			}*/
			sys_error_led_task();
		}
		if(rInx.fIndex > 0){
			TmpIsAva[i] =1;
			AvaCount++;
			TmpllIndex[i].fIndex = rInx.fIndex;
		}else{
			TmpIsAva[i] =0;
			TmpllIndex[i].fIndex = 0;
		}
	}
	
	SysLoopTick = 0;
	if(AvaCount == 0){
		Buf4Fault.BlockX = 0;
		Buf4Fault.PageX = 0;
		Buf4Fault.EntryX = 0;
		Buf4Fault.sBlock = 0;
		Buf4Fault.Count =0;
		Buf4Fault.fIndex.fIndex = 2;
		Buf4Fault.SdramAddrStart = dSdramFaultHistoryContentBaseAddr + (Buf4Fault.sBlock * SizeOf64K);
	}else
	if(AvaCount >= dFrashFaultUsingBlockCount){
		//Find Max 
		aInx = TmpllIndex[0].fIndex;
		m =0;
		for(i=0; i<dFrashFaultUsingBlockCount; i++){
			if(TmpllIndex[i].fIndex > aInx){
				aInx = TmpllIndex[i].fIndex;
				m = i;
			}
		}
		Buf4Fault.fIndex.fIndex = aInx;
		LastAvaBlock = m;
		Buf4Fault.BlockX = LastAvaBlock;
		m++;
		if(m >= dFrashFaultUsingBlockCount)m = 0;
		FirstAvaBlock = m;
		Buf4Fault.sBlock = FirstAvaBlock;
		Buf4Fault.SdramAddrStart = dSdramFaultHistoryContentBaseAddr + (FirstAvaBlock * SizeOf64K);
		
		CanGoFindPageAndEntry =1;
		sys_error_led_task();
	}else{
		if(TmpIsAva[0])FirstAvaBlock = 0;
		for(i=0; i<dFrashFaultUsingBlockCount; i++){
			if(FirstEmptyBlock > 256){
				if(TmpIsAva[i] == 0){
					FirstEmptyBlock = i;
				}
			}else{
				if(TmpIsAva[i]){
					if( (FirstAvaBlock == 0) || (FirstAvaBlock >256))FirstAvaBlock = i;
				}
			}
			if(i<(dFrashFaultUsingBlockCount-1)){
				if( (TmpIsAva[i]) && (TmpIsAva[i+1] ==0) ){
					LastAvaBlock = i;
				}
			}
		}
		if(LastAvaBlock > 256){
			LastAvaBlock = dFrashFaultUsingBlockCount -1;
		}
		DelayMs(10);
		eeprom_read_byte(0, dEepAddrFaultWritingBlock, &Buf4Fault.WritingBlockReadFromEep, 1);
		if(Buf4Fault.WritingBlockReadFromEep == LastAvaBlock){
			for(i=0; i<dFrashFaultUsingBlockCount; i++){
				if(i != LastAvaBlock){
					if(TmpllIndex[i].fIndex >= TmpllIndex[LastAvaBlock].fIndex)ReFindMaxInx =1;
				}
			}
		}
		Buf4Fault.sBlock = FirstAvaBlock;
		Buf4Fault.BlockX = LastAvaBlock;
		
		CanGoFindPageAndEntry =1;
		sys_error_led_task();
	}
	
	//Find Out PageX And EntryX
	
	SysLoopTick = 0;
	if(CanGoFindPageAndEntry){
		Found = 0;
		sAddr = dSdramFaultHistoryContentBaseAddr + (LastAvaBlock * SizeOf64K);
		for(i=0; i<((dFlashBlockSize / dFlashEntrySize) -1); i++ ){
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr + i*dFlashEntrySize +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr + i*dFlashEntrySize +4);
			CmpllIndex2.spl.sIndex0 = *(volatile unsigned int *)(sAddr + (i+1)*dFlashEntrySize +0);
			CmpllIndex2.spl.sIndex1 = *(volatile unsigned int *)(sAddr + (i+1)*dFlashEntrySize +4);
			if(  (CmpllIndex1.fIndex != 0xFFFFFFFFFFFFFFFF) && (CmpllIndex2.fIndex == 0xFFFFFFFFFFFFFFFF) ){
				Buf4Fault.PageX = (i+1) / dFlashEntryCountPerPage;
				Buf4Fault.EntryX = (i+1) % dFlashEntryCountPerPage;
				Buf4Fault.fIndex.fIndex = CmpllIndex1.fIndex;
				Found = 1;
				BlockCount = ((LastAvaBlock + dFrashFaultUsingBlockCount) - FirstAvaBlock ) % dFrashFaultUsingBlockCount;
				Buf4Fault.Count = BlockCount * (dFlashBlockSize / dFlashEntrySize) + i -1;
				if(Buf4Fault.Count <0)Buf4Fault.Count =0;
				break;
			}
		}
		if(Found == 0){
			Buf4Fault.BlockX++;
			if(Buf4Fault.BlockX >= dFrashFaultUsingBlockCount){
				Buf4Fault.BlockX = 0;
			}
			Buf4Fault.PageX = 0;
			Buf4Fault.EntryX = 0;
			i = (dFlashBlockSize / dFlashEntrySize) -1;
			sAddr = dSdramFaultHistoryContentBaseAddr + (LastAvaBlock * SizeOf64K);
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr + (i)*dFlashEntrySize +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr + (i)*dFlashEntrySize +4);
			Buf4Fault.fIndex.fIndex = CmpllIndex1.fIndex;
			BlockCount = ((LastAvaBlock + dFrashFaultUsingBlockCount) - FirstAvaBlock ) % dFrashFaultUsingBlockCount +1;
			Buf4Fault.Count = BlockCount * (dFlashBlockSize / dFlashEntrySize) -1;
			if(Buf4Fault.Count <0)Buf4Fault.Count =0;
		}
		
		BlockCount = ((Buf4Fault.BlockX + dFrashFaultUsingBlockCount) - Buf4Fault.sBlock ) % dFrashFaultUsingBlockCount +1;
		if(BlockCount > (dFrashFaultUsingBlockCount -2)){
			Buf4Fault.sBlock = Buf4Fault.BlockX - (dFrashFaultUsingBlockCount -3);
			if(Buf4Fault.sBlock <0)Buf4Fault.sBlock += dFrashFaultUsingBlockCount;

			BlockCount = dFrashFaultUsingBlockCount -2;
		}
		EntryCount = (BlockCount-1) * (dFlashBlockSize / dFlashEntrySize) + (Buf4Fault.PageX * (dFlashPageSize / dFlashEntrySize)) + Buf4Fault.EntryX ;
		if(EntryCount > dSdramHistoryEntryCount){
			Buf4Fault.Count = dSdramHistoryEntryCount;
		}else{
			Buf4Fault.Count = EntryCount;
		}
		Buf4Fault.SdramAddrCurrent = dSdramFaultHistoryContentBaseAddr + (Buf4Fault.BlockX * SizeOf64K) + (Buf4Fault.PageX * dFlashPageSize) + (Buf4Fault.EntryX * dFlashEntrySize);
		Buf4Fault.SdramAddrStart = Buf4Fault.SdramAddrCurrent - (Buf4Fault.Count * 64);
		if(Buf4Fault.SdramAddrStart < dSdramFaultHistoryContentBaseAddr){
			Buf4Fault.SdramAddrStart = Buf4Fault.SdramAddrStart + dSdramHistoryBytesLength;
		}
		sys_error_led_task();
	}
	
	SysLoopTick = 0;
	m=0;
	Buf4Fault.fIndex.fIndex = 0;
	EntryCount = Buf4Fault.Count -1;
	do{
		if(EntryCount < 0)break;
		sAddr = Buf4Fault.SdramAddrStart + (EntryCount * 64);
		if(sAddr >= dSdramFaultHistoryContentBaseAddr + dSdramHistoryBytesLength)
			sAddr = sAddr - dSdramHistoryBytesLength;
		for(i=0; i<16; i++){
			FlashPageReadData.D32[i] = *(volatile unsigned int *)(sAddr + i *4);
		}
		AvaCount = CheckHistoryCSum(FlashPageReadData.D8);
		if(AvaCount){
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr +4);
			Buf4Fault.fIndex.fIndex = CmpllIndex1.fIndex;
			break;
		}
		EntryCount--;;
		m++;
	}while(m<20);
	
	if(1){
		TextRender_string24(2, 		170, clNearBlack, clNearWhite, "FAT:");
		TextRender_string24(60, 	170, clNearBlack, clNearWhite, "BK");
		TextDigitRender3_24(90,		170, clNearBlack, clNearWhite, Buf4Fault.BlockX);
		TextRender_string24(130, 	170, clNearBlack, clNearWhite, "PG");
		TextDigitRender3_24(160,	170, clNearBlack, clNearWhite, Buf4Fault.PageX);
		TextRender_string24(200, 	170, clNearBlack, clNearWhite, "Ey");
		TextDigitRender3_24(230,	170, clNearBlack, clNearWhite, Buf4Fault.EntryX);
	}	
}


void TRecord::LoadOperateHistory()
{
	volatile unsigned int i, apg, sAddr, m, ReFindMaxInx, AvaCount, CanGoFindPageAndEntry, Found, BlockCount;
	int FirstEmptyBlock =10000;
	int FirstAvaBlock =10000;
	int LastAvaBlock = 10000;
	int EntryCount;
	int x;
	THisIndex rInx, tInx;
	unsigned long long aInx;

	AvaCount = 0;
	CanGoFindPageAndEntry = 0;
	for(i=0; i<dFrashOperateUsingBlockCount; i++){
		rInx.fIndex = 0;
		for(apg=0; apg<256; apg++){
			SysLoopTick = 0;
			fAddr = dFlashOperateBaseAddr + (i * SizeOf64K) + (apg*256);
			nor_read_byte(dFlashOperateChip,fAddr,FlashPageReadData.D8,256);
			
			sAddr = dSdramOperateHistoryContentBaseAddr + (i * SizeOf64K) + (apg*256);
			for(m=0; m<64; m++){
				*(volatile unsigned int *)(sAddr + m *4) = FlashPageReadData.D32[m];
			}
			for(x=0; x<4; x++){
				if(CheckHistoryCSum(&FlashPageReadData.D8[dFlashEntrySize * x])){
					tInx.spl.sIndex0 = FlashPageReadData.D32[dFlashEntrySize / 4 * x + 0];
					tInx.spl.sIndex1 = FlashPageReadData.D32[dFlashEntrySize / 4 * x + 1];
					if(tInx.fIndex < 0x7FFFFFFF'FFFFFFFF){
						if(tInx.fIndex > rInx.fIndex){
							rInx.fIndex =tInx.fIndex; 
						}
					}
				}
			}
			/*if(apg == 0){
				if(  (FlashPageReadData.D32[0] == 0xFFFFFFFF) && (FlashPageReadData.D32[1] == 0xFFFFFFFF)  ){
					TmpIsAva[i] =0;
					TmpllIndex[i].fIndex = 0;
				}else{
					TmpIsAva[i] =1;
					AvaCount++;
					TmpllIndex[i].spl.sIndex0 = FlashPageReadData.D32[0];
					TmpllIndex[i].spl.sIndex1 = FlashPageReadData.D32[1];
				}
			}*/
			sys_error_led_task();
		}
		if(rInx.fIndex > 0){
			TmpIsAva[i] =1;
			AvaCount++;
			TmpllIndex[i].fIndex = rInx.fIndex;
		}else{
			TmpIsAva[i] =0;
			TmpllIndex[i].fIndex = 0;
		}
	}
	
	SysLoopTick = 0;
	if(AvaCount == 0){
		Buf4Operate.BlockX = 0;
		Buf4Operate.PageX = 0;
		Buf4Operate.EntryX = 0;
		Buf4Operate.sBlock = 0;
		Buf4Operate.Count =0;
		Buf4Operate.fIndex.fIndex = 2;
		Buf4Operate.SdramAddrStart = dSdramOperateHistoryContentBaseAddr + (Buf4Operate.sBlock * SizeOf64K);
	}else
	if(AvaCount >= dFrashOperateUsingBlockCount){
		//Find Max 
		aInx = TmpllIndex[0].fIndex;
		m =0;
		for(i=0; i<dFrashOperateUsingBlockCount; i++){
			if(TmpllIndex[i].fIndex > aInx){
				aInx = TmpllIndex[i].fIndex;
				m = i;
			}
		}
		Buf4Operate.fIndex.fIndex = aInx;
		LastAvaBlock = m;
		Buf4Operate.BlockX = LastAvaBlock;
		m++;
		if(m >= dFrashOperateUsingBlockCount)m = 0;
		FirstAvaBlock = m;
		Buf4Operate.sBlock = FirstAvaBlock;
		Buf4Operate.SdramAddrStart = dSdramOperateHistoryContentBaseAddr + (FirstAvaBlock * SizeOf64K);
		
		CanGoFindPageAndEntry =1;
		sys_error_led_task();
	}else{
		if(TmpIsAva[0])FirstAvaBlock = 0;
		for(i=0; i<dFrashOperateUsingBlockCount; i++){
			if(FirstEmptyBlock > 256){
				if(TmpIsAva[i] == 0){
					FirstEmptyBlock = i;
				}
			}else{
				if(TmpIsAva[i]){
					if( (FirstAvaBlock == 0) || (FirstAvaBlock >256))FirstAvaBlock = i;
				}
			}
			if(i<(dFrashOperateUsingBlockCount-1)){
				if( (TmpIsAva[i]) && (TmpIsAva[i+1] ==0) ){
					LastAvaBlock = i;
				}
			}
		}
		if(LastAvaBlock > 256){
			LastAvaBlock = dFrashOperateUsingBlockCount -1;
		}
		DelayMs(10);
		eeprom_read_byte(0, dEepAddrOperateWritingBlock, &Buf4Operate.WritingBlockReadFromEep, 1);
		if(Buf4Operate.WritingBlockReadFromEep == LastAvaBlock){
			for(i=0; i<dFrashOperateUsingBlockCount; i++){
				if(i != LastAvaBlock){
					if(TmpllIndex[i].fIndex >= TmpllIndex[LastAvaBlock].fIndex)ReFindMaxInx =1;
				}
			}
		}
		Buf4Operate.sBlock = FirstAvaBlock;
		Buf4Operate.BlockX = LastAvaBlock;
		
		CanGoFindPageAndEntry =1;
		sys_error_led_task();
	}
	
	//Find Out PageX And EntryX
	SysLoopTick = 0;
	if(CanGoFindPageAndEntry){
		Found = 0;
		sAddr = dSdramOperateHistoryContentBaseAddr + (LastAvaBlock * SizeOf64K);
		for(i=0; i<((dFlashBlockSize / dFlashEntrySize) -1); i++ ){
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr + i*dFlashEntrySize +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr + i*dFlashEntrySize +4);
			CmpllIndex2.spl.sIndex0 = *(volatile unsigned int *)(sAddr + (i+1)*dFlashEntrySize +0);
			CmpllIndex2.spl.sIndex1 = *(volatile unsigned int *)(sAddr + (i+1)*dFlashEntrySize +4);
			if(  (CmpllIndex1.fIndex != 0xFFFFFFFFFFFFFFFF) && (CmpllIndex2.fIndex == 0xFFFFFFFFFFFFFFFF) ){
				Buf4Operate.PageX = (i+1) / dFlashEntryCountPerPage;
				Buf4Operate.EntryX = (i+1) % dFlashEntryCountPerPage;
				Buf4Operate.fIndex.fIndex = CmpllIndex1.fIndex;
				Found = 1;
				BlockCount = ((LastAvaBlock + dFrashOperateUsingBlockCount) - FirstAvaBlock ) % dFrashOperateUsingBlockCount;
				Buf4Operate.Count = BlockCount * (dFlashBlockSize / dFlashEntrySize) + i -1;
				if(Buf4Operate.Count <0)Buf4Operate.Count =0;
				break;
			}
		}
		if(Found == 0){
			Buf4Operate.BlockX++;
			if(Buf4Operate.BlockX >= dFrashOperateUsingBlockCount){
				Buf4Operate.BlockX = 0;
			}
			Buf4Operate.PageX = 0;
			Buf4Operate.EntryX = 0;
			i = (dFlashBlockSize / dFlashEntrySize) -1;
			sAddr = dSdramOperateHistoryContentBaseAddr + (LastAvaBlock * SizeOf64K);
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr + (i)*dFlashEntrySize +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr + (i)*dFlashEntrySize +4);
			Buf4Operate.fIndex.fIndex = CmpllIndex1.fIndex;
			BlockCount = ((LastAvaBlock + dFrashOperateUsingBlockCount) - FirstAvaBlock ) % dFrashOperateUsingBlockCount +1;
			Buf4Operate.Count = BlockCount * (dFlashBlockSize / dFlashEntrySize) -1;
			if(Buf4Operate.Count <0)Buf4Operate.Count =0;
		}
		
		BlockCount = ((Buf4Operate.BlockX + dFrashOperateUsingBlockCount) - Buf4Operate.sBlock ) % dFrashOperateUsingBlockCount +1;
		if(BlockCount > (dFrashOperateUsingBlockCount -2)){
			Buf4Operate.sBlock = Buf4Operate.BlockX - (dFrashOperateUsingBlockCount -3);
			if(Buf4Operate.sBlock <0)Buf4Operate.sBlock += dFrashOperateUsingBlockCount;

			BlockCount = dFrashOperateUsingBlockCount -2;
		}
		EntryCount = (BlockCount-1) * (dFlashBlockSize / dFlashEntrySize) + (Buf4Operate.PageX * (dFlashPageSize / dFlashEntrySize)) + Buf4Operate.EntryX ;
		if(EntryCount > dSdramHistoryEntryCount){
			Buf4Operate.Count = dSdramHistoryEntryCount;
		}else{
			Buf4Operate.Count = EntryCount;
		}
		Buf4Operate.SdramAddrCurrent = dSdramOperateHistoryContentBaseAddr + (Buf4Operate.BlockX * SizeOf64K) + (Buf4Operate.PageX * dFlashPageSize) + (Buf4Operate.EntryX * dFlashEntrySize);
		Buf4Operate.SdramAddrStart = Buf4Operate.SdramAddrCurrent - (Buf4Operate.Count * 64);
		if(Buf4Operate.SdramAddrStart < dSdramOperateHistoryContentBaseAddr){
			Buf4Operate.SdramAddrStart = Buf4Operate.SdramAddrStart + dSdramHistoryBytesLength;
		}
		sys_error_led_task();
	}

	SysLoopTick = 0;	
	m=0;
	Buf4Operate.fIndex.fIndex = 0;
	EntryCount = Buf4Operate.Count -1;
	do{
		if(EntryCount < 0)break;
		sAddr = Buf4Operate.SdramAddrStart + (EntryCount * 64);
		if(sAddr >= dSdramOperateHistoryContentBaseAddr + dSdramHistoryBytesLength)
			sAddr = sAddr - dSdramHistoryBytesLength;
		for(i=0; i<16; i++){
			FlashPageReadData.D32[i] = *(volatile unsigned int *)(sAddr + i *4);
		}
		AvaCount = CheckHistoryCSum(FlashPageReadData.D8);
		if(AvaCount){
			CmpllIndex1.spl.sIndex0 = *(volatile unsigned int *)(sAddr +0);
			CmpllIndex1.spl.sIndex1 = *(volatile unsigned int *)(sAddr +4);
			Buf4Operate.fIndex.fIndex = CmpllIndex1.fIndex;
			break;
		}
		EntryCount--;;
		m++;
	}while(m<20);
	
	if(1){
		TextRender_string24(2, 		200, clNearBlack, clNearWhite, "Opa:");
		TextRender_string24(60, 	200, clNearBlack, clNearWhite, "BK");
		TextDigitRender3_24(90,		200, clNearBlack, clNearWhite, Buf4Operate.BlockX);
		TextRender_string24(130, 	200, clNearBlack, clNearWhite, "PG");
		TextDigitRender3_24(160,	200, clNearBlack, clNearWhite, Buf4Operate.PageX);
		TextRender_string24(200, 	200, clNearBlack, clNearWhite, "Ey");
		TextDigitRender3_24(230,	200, clNearBlack, clNearWhite, Buf4Operate.EntryX);
	}	
}

void TRecord::AllMsgReadInit()
{
	LoadFireHistory();
	LoadLinkageHistory();
	LoadSvHistory();
	LoadFaultHistory();
	LoadOperateHistory();
	
	sysIndex.fIndex = 1;
	if(Buf4Fire.fIndex.fIndex > sysIndex.fIndex) sysIndex.fIndex = Buf4Fire.fIndex.fIndex;
	if(Buf4Linkage.fIndex.fIndex > sysIndex.fIndex) sysIndex.fIndex = Buf4Linkage.fIndex.fIndex;
	if(Buf4Sv.fIndex.fIndex > sysIndex.fIndex) sysIndex.fIndex = Buf4Sv.fIndex.fIndex;
	if(Buf4Fault.fIndex.fIndex > sysIndex.fIndex) sysIndex.fIndex = Buf4Fault.fIndex.fIndex;
	if(Buf4Operate.fIndex.fIndex > sysIndex.fIndex) sysIndex.fIndex = Buf4Operate.fIndex.fIndex;
	
	
	TMyString::sFromUInt(sysIndex.D32.Inx1, FlashPageReadData.D8);
	TextRender_string24(2, 		240, clNearWhite, FlashPageReadData.D8);
	TMyString::sFromUInt(sysIndex.D32.Inx0, FlashPageReadData.D8);
	TextRender_string24(200, 	240, clNearWhite, FlashPageReadData.D8);
	
	if(sysIndex.fIndex > dPRIMARY_KEY_WARNING_NUM){
		PrimaryKeyWarning = 1;
		CtlCenter.ieAddBrdFault(40,TBoardFaultType::RecordPrimaryKeyWarning);
	}else{
		PrimaryKeyWarning =0;
	}
	
	DelayMs(1000);
}

void TRecord::CheckPrimaryKeyWarning()
{
	if( (sysIndex.fIndex > dPRIMARY_KEY_WARNING_NUM) or (PrimaryKeyWarning) ){
		CtlCenter.ieAddBrdFault(40,TBoardFaultType::RecordPrimaryKeyWarning);
	}	
}

void TRecord::ClearFireHistory()
{
	volatile unsigned int sta, aWait;

	int b;

	//Find Block_______________________________________________
	for(b=0; b<dFrashFireUsingBlockCount; b++){
		fAddr = dFlashFireBaseAddr + (dFlashBlockSize * b);
		nor_erase_sector_64k(dFlashFireChip,fAddr);
		for(aWait=0; aWait<10000000;aWait++);
		sta =1;
		while(sta){
			for(aWait=0; aWait<100000;aWait++);
			sta = nor_get_busy_state(dFlashFireChip);
		}
	}
	Buf4Fire.BlockX = 0;
	Buf4Fire.PageX = 0;
	Buf4Fire.EntryX = 0;
	Buf4Fire.sBlock = 0;
	Buf4Fire.Count =0;
	Buf4Fire.fIndex.fIndex = 2;
	Buf4Fire.SdramAddrStart = dSdramFireHistoryContentBaseAddr + (Buf4Fire.sBlock * SizeOf64K);
}

void TRecord::ClearLinkageHistory()
{
	volatile unsigned int sta, aWait;

	int b;
	//Find Block_______________________________________________
	for(b=0; b<dFrashLinkageUsingBlockCount; b++){
		fAddr = dFlashLinkageBaseAddr + (dFlashBlockSize * b);
		nor_erase_sector_64k(dFlashLinkageChip,fAddr);
		for(aWait=0; aWait<10000000;aWait++);
		sta =1;
		while(sta){
			for(aWait=0; aWait<100000;aWait++);
			sta = nor_get_busy_state(dFlashLinkageChip);
		}
	}
	Buf4Linkage.BlockX = 0;
	Buf4Linkage.PageX = 0;
	Buf4Linkage.EntryX = 0;
	Buf4Linkage.sBlock = 0;
	Buf4Linkage.Count =0;
	Buf4Linkage.fIndex.fIndex = 3;
	Buf4Linkage.SdramAddrStart = dSdramLinkageHistoryContentBaseAddr + (Buf4Linkage.sBlock * SizeOf64K);	
}

void TRecord::ClearSvHistory()
{
	volatile unsigned int sta, aWait;
	int b;
	//Find Block_______________________________________________
	for(b=0; b<dFrashSvUsingBlockCount; b++){
		fAddr = dFlashSvBaseAddr + (dFlashBlockSize * b);
		nor_erase_sector_64k(dFlashSvChip,fAddr);
		for(aWait=0; aWait<10000000;aWait++);
		sta =1;
		while(sta){
			for(aWait=0; aWait<100000;aWait++);
			sta = nor_get_busy_state(dFlashSvChip);
		}
	}
	Buf4Sv.BlockX = 0;
	Buf4Sv.PageX = 0;
	Buf4Sv.EntryX = 0;
	Buf4Sv.sBlock = 0;
	Buf4Sv.Count =0;
	Buf4Sv.fIndex.fIndex = 4;
	Buf4Sv.SdramAddrStart = dSdramSvHistoryContentBaseAddr + (Buf4Sv.sBlock * SizeOf64K);	
}

void TRecord::ClearFaultHistory()
{
	volatile unsigned int sta, aWait;
	int b;
	//Find Block_______________________________________________
	for(b=0; b<dFrashFaultUsingBlockCount; b++){
		fAddr = dFlashFaultBaseAddr + (dFlashBlockSize * b);
		nor_erase_sector_64k(dFlashFaultChip,fAddr);
		for(aWait=0; aWait<10000000;aWait++);
		sta =1;
		while(sta){
			for(aWait=0; aWait<100000;aWait++);
			sta = nor_get_busy_state(dFlashFaultChip);
		}
	}
	Buf4Fault.BlockX = 0;
	Buf4Fault.PageX = 0;
	Buf4Fault.EntryX = 0;
	Buf4Fault.sBlock = 0;
	Buf4Fault.Count =0;
	Buf4Fault.fIndex.fIndex = 5;
	Buf4Fault.SdramAddrStart = dSdramFaultHistoryContentBaseAddr + (Buf4Fault.sBlock * SizeOf64K);	
}

void TRecord::ClearOperateHistory()
{
	volatile unsigned int sta, aWait;
	int b;
	//Find Block_______________________________________________
	for(b=0; b<dFrashOperateUsingBlockCount; b++){
		fAddr = dFlashOperateBaseAddr + (dFlashBlockSize * b);
		nor_erase_sector_64k(dFlashOperateChip,fAddr);
		for(aWait=0; aWait<10000000;aWait++);
		sta =1;
		while(sta){
			for(aWait=0; aWait<100000;aWait++);
			sta = nor_get_busy_state(dFlashOperateChip);
		}
	}
	Buf4Operate.BlockX = 0;
	Buf4Operate.PageX = 0;
	Buf4Operate.EntryX = 0;
	Buf4Operate.sBlock = 0;
	Buf4Operate.Count =0;
	Buf4Operate.fIndex.fIndex = 6;
	Buf4Operate.SdramAddrStart = dSdramOperateHistoryContentBaseAddr + (Buf4Operate.sBlock * SizeOf64K);		
}

void TRecord::ReadPortDisplayPadSet(unsigned char aPort)
{
	unsigned int tP,m,x;
	volatile unsigned int aFlashState;
	if(aPort==0)return;
	if(aPort>dPORT_MAX_COUNT)return;
	tP=aPort-1;
	
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	//Read DisplayPad Cfg Table
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * tP) + (256 * 2);
	nor_read_byte(dFlashRegisterDataChip,fAddr,FlashPageReadData.D8,256);
	for(m=0; m<16; m++){
		for(int Gg=0; Gg<16; Gg++){
			Port[tP].DisplayPadSet[m].MemBytes[Gg] = FlashPageReadData.D8[m*16 +Gg];
		}
		if(Port[tP].DisplayPadSet[m].MemBytes[0] ==0xFF){
			for(int Gg=0; Gg<16; Gg++){
				Port[tP].DisplayPadSet[m].MemBytes[Gg] = 0;
			}
		}
	}
	
	//Read DisplayPad Crc
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * tP) + (256 * 3);
	nor_read_byte(dFlashRegisterDataChip,fAddr,FlashPageReadData.D8,256);
	Port[tP].CrcDisplayPad = FlashPageReadData.D32[0];
	SysLoopTick = 0;
	Crc32Reset();
	for(m=0; m<16; m++){
		for(int x=0; x<4; x++)Crc32Feed(Port[tP].DisplayPadSet[m].MemD32[x]);
	}
	if(Crc32GetResult() == Port[tP].CrcDisplayPad){
		if(Port[tP].CrcFlag.CrcDisplayPad){
			Port[tP].CrcFlag.CrcDisplayPad =0;
			CtlCenter.ieAddBrdFaultCancel(tP+1,TBoardFaultType::StoragePortDpSet);
		}
	}else{	
		Port[tP].CrcFlag.CrcDisplayPad =1;
		CtlCenter.ieAddBrdFault(tP+1,TBoardFaultType::StoragePortDpSet);
	}	
}

void TRecord::ReadRegisterDataOnePort(unsigned char aPort)
{
	unsigned int i,m,x;
	volatile unsigned int aFlashState;
	volatile unsigned int tCrc; 
	TDMix tDm;
	
	if(aPort == 0)return;
	if(aPort>dPORT_MAX_COUNT)return;
	i=aPort-1;
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	SysLoopTick = 0;
	//Read ExistTable 
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * i) + (256 * 0);
	nor_read_byte(dFlashRegisterDataChip,fAddr,FlashPageReadData.D8,256);
	for(m=0; m<dEP_MAX_COUNT_PER_PORT; m++){
		 Port[i].ExistTable[m] = FlashPageReadData.D8[m];
	}
	for(m=250; m<256; m++){
		 Port[i].ExistTable[m] = 0;
	}
	
	//Read dType
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * i) + (256 * 1);
	nor_read_byte(dFlashRegisterDataChip,fAddr,FlashPageReadData.D8,256);
	for(m=0; m<dEP_MAX_COUNT_PER_PORT; m++){
		Port[i].dTypeTable[m] = FlashPageReadData.D8[m];
	}
	tDm.D8[0] = FlashPageReadData.D8[252];
	tDm.D8[1] = FlashPageReadData.D8[253];
	tDm.D8[2] = FlashPageReadData.D8[254];
	tDm.D8[3] = FlashPageReadData.D8[255];
	Port[i].CrcDevType = tDm.D32;
	Crc32Reset();
	for(x=0; x<dEP_MAX_COUNT_PER_PORT;x++){
		Crc32Feed(static_cast<unsigned int>(Port[i].ExistTable[x]));
	}
	for(x=0; x<dEP_MAX_COUNT_PER_PORT;x++){
		Crc32Feed(static_cast<unsigned int>(Port[i].dTypeTable[x]));
	}
	tCrc = Crc32GetResult();
	if(tCrc == Port[i].CrcDevType ){
		if(Port[i].CrcFlag.DevType){
			Port[i].CrcFlag.DevType =0;
			CtlCenter.ieAddBrdFaultCancel(i+1, TBoardFaultType::StorageEpDType);
		}
	}else{
		Port[i].CrcFlag.DevType =1;
		CtlCenter.ieAddBrdFault(i+1, TBoardFaultType::StorageEpDType);
	}
	
	Port[i].fData.Port.dType = dgTYPE_PORT;
	Port[i].fData.Port.Id = i+1;
}

void TRecord::ReadRegisterData()
{
	unsigned int i,m,x;
	volatile unsigned int aFlashState;
	volatile unsigned int tCrc; 
	TDMix tDm;
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	//_____________________________________Port
	for(i=0; i<dPORT_MAX_COUNT; i++){
		ReadRegisterDataOnePort(i+1);
		ReadPortDisplayPadSet(i+1);
	}
	
	SysLoopTick = 0;
	//MainCtl______________________________MainCtl
	//Read MainCtl Port Exist Table
	for(m=0; m<dPORT_MAX_COUNT; m++){
		MainCtl.fData.Split.ExistTablePort[m] = 0;
	}
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * dPORT_MAX_COUNT ) + (256 * 0);
	DelayMs(2);
	nor_read_page(0, fAddr, FlashPageReadData.D8);
	Crc32Reset();
	for(m=0;m<dPORT_MAX_COUNT;m++)Crc32Feed(static_cast<unsigned int>(FlashPageReadData.D8[m]));
	tDm.D8[0]= FlashPageReadData.D8[dPORT_MAX_COUNT + 0];
	tDm.D8[1]= FlashPageReadData.D8[dPORT_MAX_COUNT + 1];
	tDm.D8[2]= FlashPageReadData.D8[dPORT_MAX_COUNT + 2];
	tDm.D8[3]= FlashPageReadData.D8[dPORT_MAX_COUNT + 3];
	MainCtl.CrcPortList = tDm.D32;
	if(MainCtl.CrcPortList == Crc32GetResult()){
		if(MainCtl.CrcFlag.PortList){
			MainCtl.CrcFlag.PortList =0;
			CtlCenter.ieAddBrdFaultCancel(41,TBoardFaultType::StoragePortList);
		}
	}else{
		MainCtl.CrcFlag.PortList =1;
		CtlCenter.ieAddBrdFault      (41,TBoardFaultType::StoragePortList);
	}
	MainCtl.fData.Split.PortCount = 0;
	for(m=0; m<dPORT_MAX_COUNT; m++){
		if(FlashPageReadData.D8[m] == 0xA5){
			MainCtl.fData.Split.ExistTablePort[m] = 0xA5;
			MainCtl.fData.Split.PortCount++;
		}
	}
	
	//Mother Board
	MotherBoard.Exist[0] = 0;
	MotherBoard.Exist[1] = 0;
	MotherBoard.Exist[2] = 0;
	MotherBoard.Exist[3] = 0;
	for(i=0; i<8; i++){
		if(MainCtl.fData.Split.ExistTablePort[i]) {
			MotherBoard.Exist[0] = 1;
			break;
		}
	}
	for(i=8; i<16; i++){
		if(MainCtl.fData.Split.ExistTablePort[i]) {
			MotherBoard.Exist[1] = 1;
			break;
		}
	}
	for(i=16; i<24; i++){
		if(MainCtl.fData.Split.ExistTablePort[i]) {
			MotherBoard.Exist[2] = 1;
			break;
		}
	}
	SysLoopTick = 0;
	//____________________________________________________________________________________________________
	//Read HandCtl Port Exist Table
	for(m=0; m<dHANDCTL_MAX_COUNT; m++){
		MainCtl.fData.Split.ExistTableHandCtl[m] = 0;
	}
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * dPORT_MAX_COUNT ) + (256 * 1);
	DelayMs(2);
	nor_read_page(0, fAddr, FlashPageReadData.D8);
	
	
	//Read Direct Ctl Port Exist Table
	for(m=0; m<dDIRECTCTL_MAX_COUNT; m++){
		MainCtl.fData.Split.ExistTableDirectCtl[m] = 0;
	}
	fAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * dPORT_MAX_COUNT ) + (256 * 2);
	DelayMs(2);
	nor_read_page(0, fAddr, FlashPageReadData.D8);
	
	//calc Display Pad Count
	for(i=0; i<dPORT_MAX_COUNT; i++){
		Port[i].DisplayPadCount = 0;
		if(MainCtl.fData.Split.ExistTablePort[i]){
			for(m=0; m<256; m++){
				if(Port[i].dTypeTable[m] == dgTYPE_DISPLAY_PAD){
					Port[i].DisplayPadAddr[Port[i].DisplayPadCount] = m+1;
					Port[i].DisplayPadCount++;
					if(Port[i].DisplayPadCount >= dDISPLAYPAD_MAX_COUNT)break;
				}
			}
		}
		sys_error_led_task();
	}
	
	//calc NeedFeedBackCount
	for(i=0; i<dPORT_MAX_COUNT; i++){
		Port[i].NeedFeedBackCount = 0;
		if(MainCtl.fData.Split.ExistTablePort[i]){
			for(m=0; m<250; m++){
				if(Port[i].ExistTable[m]){
					if(  	(Port[i].dTypeTable[m] == dgTYPE_INOUT_MODULE_NonSource) 	|| (Port[i].dTypeTable[m] == dgTYPE_INOUT_MODULE_Source)	//||
						//(Port[i].dTypeTable[m] == dgType_VAALRAM)			||	
						//(Port[i].dTypeTable[m] == dgTYPE_FIRE_HydrantKp)				
																	){
							Port[i].NeedFeedBackAddr[Port[i].NeedFeedBackCount] = m;
							Port[i].NeedFeedBackCount++;
					}
				}
			}
		}
		sys_error_led_task();
	}	
}

void TRecord::ReadDevUserSet()
{
	unsigned int i,m,x;
	volatile unsigned int aFlashState;
	volatile unsigned int tAddr;
	TUserCodeStr tUserCodeStr;
	TDMix tCrc;
	unsigned char tType;

	SysLoopTick = 0;
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(dFlashAddrDevUserCodeChip);
	}while(aFlashState);
	
	//_____________________________________Port
	for(i=0; i<dPORT_MAX_COUNT; i++){
		//Read ExistTable 
		//Read anyway 
		//if(MainCtl.fData.Split.ExistTablePort[i] == 0)continue;
		SysLoopTick = 0;
		for(m=0; m<4; m++){
			fAddr = dFlashAddrDevUserCode + (4096 * i) + (256 * m);
			nor_read_byte(dFlashAddrDevUserCodeChip,fAddr,FlashPageReadData.D8,256);
			for(x=0; x<64; x++){
				Port[i].UcList[m*64+x].Full = FlashPageReadData.D32[x];
				if(Port[i].UcList[m*64+x].Full > 99999999)Port[i].UcList[m*64+x].Full =0;
				tAddr = dAddrSdRam_4UserCodeMatch + (i * 1024) + ((m*64+x) * 4);
				tUserCodeStr.Spl.BcdText_7 = Port[i].UcList[m*64+x].Full / 10000000 % 10; 
				tUserCodeStr.Spl.BcdText_6 = Port[i].UcList[m*64+x].Full / 1000000 % 10;
				tUserCodeStr.Spl.BcdText_5 = Port[i].UcList[m*64+x].Full / 100000 % 10;
				tUserCodeStr.Spl.BcdText_4 = Port[i].UcList[m*64+x].Full / 10000 % 10;
				tUserCodeStr.Spl.BcdText_3 = Port[i].UcList[m*64+x].Full / 1000 % 10;
				tUserCodeStr.Spl.BcdText_2 = Port[i].UcList[m*64+x].Full / 100 % 10;
				tUserCodeStr.Spl.BcdText_1 = Port[i].UcList[m*64+x].Full / 10 % 10;
				tUserCodeStr.Spl.BcdText_0 = Port[i].UcList[m*64+x].Full / 1 % 10;
				*(volatile unsigned int *)(tAddr) = tUserCodeStr.Full;
			}
			sys_error_led_task();
			if(m==3){
				Port[i].CrcAuxId = FlashPageReadData.D32[63];
			}
		}
		Crc32Reset();
		for(m=0; m<dEP_MAX_COUNT_PER_PORT; m++)Crc32Feed(Port[i].UcList[m].Full);
		tCrc.D32 = Crc32GetResult();
		if(tCrc.D32 == Port[i].CrcAuxId){
			if(Port[i].CrcFlag.AuxId){
				Port[i].CrcFlag.AuxId =0;
				CtlCenter.ieAddBrdFaultCancel(i+1, TBoardFaultType::StorageEpAuxId);	
			}
		}else{
			Port[i].CrcFlag.AuxId =1;
			CtlCenter.ieAddBrdFault(i+1, TBoardFaultType::StorageEpAuxId);
		}
		
		SysLoopTick = 0;
		fAddr = dFlashAddrDevUserCode + (4096 * i) + (256 * 4);
		nor_read_byte(dFlashAddrDevUserCodeChip,fAddr,FlashPageReadData.D8,256);
		Port[i].IsIncludePump=0;
		tCrc.D8[0] = FlashPageReadData.D8[252];
		tCrc.D8[1] = FlashPageReadData.D8[253];
		tCrc.D8[2] = FlashPageReadData.D8[254];
		tCrc.D8[3] = FlashPageReadData.D8[255];
		Port[i].CrcAssignedType = tCrc.D32;
		Crc32Reset();
		for(x=0; x<dEP_MAX_COUNT_PER_PORT; x++)Crc32Feed(static_cast<unsigned int>(FlashPageReadData.D8[x]));
		if(Crc32GetResult() == Port[i].CrcAssignedType){
			if(Port[i].CrcFlag.AssignedType){
				Port[i].CrcFlag.AssignedType =0;
				CtlCenter.ieAddBrdFaultCancel(i+1, TBoardFaultType::StorageEpAssignedType);	
			}
		}else{
			Port[i].CrcFlag.AssignedType =1;
			CtlCenter.ieAddBrdFault(i+1, TBoardFaultType::StorageEpAssignedType);
		}
		for(x=0; x<dEP_MAX_COUNT_PER_PORT; x++){
			Port[i].AssignType[x] = FlashPageReadData.D8[x];
			tType = Port[i].dTypeTable[x];
			if( (tType != dgTYPE_IN_MODULE) && (tType != dgTYPE_OUT_MODULE) && (tType != dgTYPE_INOUT_MODULE_NonSource) && (tType != dgTYPE_INOUT_MODULE_Source) && (tType != dgTYPE_NoType) ){
				Port[i].AssignType[x] =0;
			}
			if(Port[i].AssignType[x] == 255)Port[i].AssignType[x] =0;
			if(Port[i].AssignType[x] == dFIREPUMP_ASSIGN_TYPE)Port[i].IsIncludePump =0xA5;
		}
		Crc32Reset();
		for(x=0; x<dEP_MAX_COUNT_PER_PORT; x++)Crc32Feed(static_cast<unsigned int>(Port[i].AssignType[x]));
		Port[i].CrcRealAssignedType = Crc32GetResult();
		
		sys_error_led_task();
		SysLoopTick = 0;
		fAddr = dFlashAddrDevUserCode + (4096 * i) + (256 * 5);
		nor_read_byte(dFlashAddrDevUserCodeChip,fAddr,FlashPageReadData.D8,256);
		tCrc.D8[0] = FlashPageReadData.D8[252];
		tCrc.D8[1] = FlashPageReadData.D8[253];
		tCrc.D8[2] = FlashPageReadData.D8[254];
		tCrc.D8[3] = FlashPageReadData.D8[255];
		Port[i].CrcSignalType = tCrc.D32;
		Crc32Reset();
		for(x=0; x<dEP_MAX_COUNT_PER_PORT; x++)Crc32Feed(static_cast<unsigned int>(FlashPageReadData.D8[x]));
		if(Crc32GetResult() == Port[i].CrcSignalType){
			if(Port[i].CrcFlag.SignalType){
				Port[i].CrcFlag.SignalType =0;
				CtlCenter.ieAddBrdFaultCancel(i+1, TBoardFaultType::StorageEpSignalType);	
			}
		}else{
			Port[i].CrcFlag.SignalType =1;
			CtlCenter.ieAddBrdFault(i+1, TBoardFaultType::StorageEpSignalType);
		}
		for(x=0; x<dEP_MAX_COUNT_PER_PORT; x++){
			Port[i].FuncSetByte[x] = FlashPageReadData.D8[x];
			if(Port[i].FuncSetByte[x] == 255)Port[i].FuncSetByte[x] =0;
			if(Port[i].FuncSetByte[x] & 0xF0)Port[i].FuncSetByte[x] =0;
			if( ((Port[i].FuncSetByte[x] & 0x07) != 0x02) && ((Port[i].FuncSetByte[x] & 0x07) != 0x04)  && ((Port[i].FuncSetByte[x] & 0x07) != 0x01) ){
				Port[i].FuncSetByte[x] = 0;
			}
			tType = Port[i].dTypeTable[x];
			if( (tType != dgTYPE_IN_MODULE) && (tType != dgTYPE_OUT_MODULE) && (tType != dgTYPE_INOUT_MODULE_NonSource) && (tType != dgTYPE_INOUT_MODULE_Source) && (tType != dgTYPE_NoType) ){
				Port[i].FuncSetByte[x] =0;
			}
		}
		Crc32Reset();
		for(x=0; x<dEP_MAX_COUNT_PER_PORT; x++)Crc32Feed(static_cast<unsigned int>(Port[i].FuncSetByte[x]));
		Port[i].CrcRealSignalType = Crc32GetResult();

		sys_error_led_task();
		
		Port[i].UserCodeMin = 0xFFFFFFFF;
		Port[i].UserCodeMax = 0;
		
		for(x=0;x<256;x++){
			if(Port[i].UcList[x].Full < Port[i].UserCodeMin){
				Port[i].UserCodeMin = Port[i].UcList[x].Full;
			}
			if(Port[i].UcList[x].Full > Port[i].UserCodeMax){
				Port[i].UserCodeMax = Port[i].UcList[x].Full;
			}
		}
	}
}

void TRecord::ReadDirectPadUserSet(unsigned char aPath0)
{
	unsigned int i,m,x;
	volatile unsigned int aFlashState;
	TUserCodeStr tUserCodeStr;
	TDMix tCrc1, tCrc2;
	
	if(aPath0 == 0)return;
	if(aPath0>dDIRECTCTL_MAX_COUNT)return;
	i=aPath0-1;
	
	SysLoopTick = 0;
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(dFlashAddrDevUserCodeChip);
	}while(aFlashState);
	fAddr = dFlashAddrDirectPadUserCode + (4096 * i) + (256 * 0);
	nor_read_byte(dFlashAddrDevUserCodeChip,fAddr,FlashPageReadData.D8,256);
	tCrc1.D8[0] = FlashPageReadData.D8[32+8 +0];
	tCrc1.D8[1] = FlashPageReadData.D8[32+8 +1];
	tCrc1.D8[2] = FlashPageReadData.D8[32+8 +2];
	tCrc1.D8[3] = FlashPageReadData.D8[32+8 +3];
	tCrc2.D8[0] = FlashPageReadData.D8[32+8 +4];
	tCrc2.D8[1] = FlashPageReadData.D8[32+8 +5];
	tCrc2.D8[2] = FlashPageReadData.D8[32+8 +6];
	tCrc2.D8[3] = FlashPageReadData.D8[32+8 +7];
	DirectCtlBoard[i].CrcAuxId = tCrc1.D32;
	DirectCtlBoard[i].CrcAssignedType = tCrc2.D32;
	Crc32Reset();
	for(x=0; x<8; x++){
		DirectCtlBoard[i].UcList[x].Full = FlashPageReadData.D32[x];
		Crc32Feed(FlashPageReadData.D32[x]);
		if(DirectCtlBoard[i].UcList[x].Full >99999999)DirectCtlBoard[i].UcList[x].Full =0;
		tUserCodeStr.Spl.BcdText_7 = DirectCtlBoard[i].UcList[x].Full / 10000000 % 10; 
		tUserCodeStr.Spl.BcdText_6 = DirectCtlBoard[i].UcList[x].Full / 1000000 % 10;
		tUserCodeStr.Spl.BcdText_5 = DirectCtlBoard[i].UcList[x].Full / 100000 % 10;
		tUserCodeStr.Spl.BcdText_4 = DirectCtlBoard[i].UcList[x].Full / 10000 % 10;
		tUserCodeStr.Spl.BcdText_3 = DirectCtlBoard[i].UcList[x].Full / 1000 % 10;
		tUserCodeStr.Spl.BcdText_2 = DirectCtlBoard[i].UcList[x].Full / 100 % 10;
		tUserCodeStr.Spl.BcdText_1 = DirectCtlBoard[i].UcList[x].Full / 10 % 10;
		tUserCodeStr.Spl.BcdText_0 = DirectCtlBoard[i].UcList[x].Full / 1 % 10;
		DirectCtlBoard[i].UcFastMatch[x].Full = tUserCodeStr.Full;			
	}
	if(DirectCtlBoard[i].CrcAuxId == Crc32GetResult()){
		if(DirectCtlBoard[i].CrcFlag.AuxId){
			DirectCtlBoard[i].CrcFlag.AuxId = 0;
			CtlCenter.ieAddBrdFaultCancel(i+dADDR_DIRECTPAD_START, TBoardFaultType::StorageEpAuxId);	
		}
	}else{
		DirectCtlBoard[i].CrcFlag.AuxId =1;
		CtlCenter.ieAddBrdFault(i+dADDR_DIRECTPAD_START, TBoardFaultType::StorageEpAuxId);
	}
	
	DirectCtlBoard[i].IsIncludePump = 0;
	Crc32Reset();
	for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
		DirectCtlBoard[i].AssignType[x] = FlashPageReadData.D8[x + 32];
		Crc32Feed(FlashPageReadData.D8[x + 32]);
		if(DirectCtlBoard[i].AssignType[x] == 255) DirectCtlBoard[i].AssignType[x]=0;
		if(DirectCtlBoard[i].AssignType[x] == dFIREPUMP_ASSIGN_TYPE){
			DirectCtlBoard[i].IsPump[x] =0xA5;
			DirectCtlBoard[i].IsIncludePump = 0xA5;
		}else{
			DirectCtlBoard[i].IsPump[x] =0;
		}
	}
	if(DirectCtlBoard[i].CrcAssignedType == Crc32GetResult()){
		if(DirectCtlBoard[i].CrcFlag.AssignedType){
			DirectCtlBoard[i].CrcFlag.AssignedType = 0;
			CtlCenter.ieAddBrdFaultCancel(i+dADDR_DIRECTPAD_START, TBoardFaultType::StorageEpAssignedType);	
		}
	}else{
		DirectCtlBoard[i].CrcFlag.AssignedType =1;
		CtlCenter.ieAddBrdFault(i+dADDR_DIRECTPAD_START, TBoardFaultType::StorageEpAssignedType);
	}
	
	sys_error_led_task();	
}

void TRecord::ReadDirectPadUserSet()
{
	unsigned int i;
	for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
		ReadDirectPadUserSet(i+1);
	}
}

void TRecord::ReadDescp()
{
	unsigned int fAddr, sdAddr;
	unsigned int x,i,m;
	unsigned int wP1;
	volatile unsigned int aWait, aFlashState;
	
	SysLoopTick = 0;
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	for(m=0; m<(dPORT_MAX_COUNT +1); m++){
		for(x=0; x<32; x++){
			SysLoopTick = 0;
			fAddr = dFlashAddrDescpBase + (m * 8192) + (x * 256);
			nor_read_page(0, fAddr, FlashPageReadData.D8);
			
			if(FlashPageReadData.D32[0] == 0xFFFFFFFF)FlashPageReadData.D32[0] = 0;
			if(FlashPageReadData.D32[8] == 0xFFFFFFFF)FlashPageReadData.D32[8] = 0;
			if(FlashPageReadData.D32[16] == 0xFFFFFFFF)FlashPageReadData.D32[16] = 0;
			if(FlashPageReadData.D32[24] == 0xFFFFFFFF)FlashPageReadData.D32[24] = 0;
			if(FlashPageReadData.D32[32] == 0xFFFFFFFF)FlashPageReadData.D32[32] = 0;
			if(FlashPageReadData.D32[40] == 0xFFFFFFFF)FlashPageReadData.D32[40] = 0;
			if(FlashPageReadData.D32[48] == 0xFFFFFFFF)FlashPageReadData.D32[48] = 0;
			if(FlashPageReadData.D32[56] == 0xFFFFFFFF)FlashPageReadData.D32[56] = 0;
			
			sdAddr = dAddrSdRam_Descp + (m * 8192) + (x * 256);
			for(i=0; i<64;i++){
				*(volatile unsigned int *)(sdAddr + i *4) = FlashPageReadData.D32[i];
			}
			sys_error_led_task();
		}
	}
}

void TRecord::ReadDescpNw()
{
	int i, y;
	if(MainCtl.fData.Split.NetworkMode != 1)return;
	/*if(ProtoC2C.SlaveCount){
		for(i=0; i<ProtoC2C.SlaveCount; i++){
			aNum = ProtoC2C.SlaveList[i];
			for(y=1; y<=dPORT_MAX_COUNT; y++){
				fNetworkReg.ReadPortDescp(aNum, y);
			}
			fNetworkReg.ReadDirectPadDescp(aNum);
		}
	}*/
	for(i=1; i<=dNETWORK_MACHINE_COUNT; i++){
		for(y=1; y<=dPORT_MAX_COUNT; y++){
			fNetworkReg.ReadPortDescp(i, y);
		}
		fNetworkReg.ReadDirectPadDescp(i);
	}
}

void TRecord::FloorDescp()
{
	unsigned int fAddr, sdAddr;
	unsigned int x,i;
	unsigned int wP1;
	volatile unsigned int aWait;
	
	for(x=0; x<32; x++){
		SysLoopTick = 0;
		fAddr = dFlashAddrFloorDescp + (x * 256);
		DelayMs(2);
		nor_read_page(0, fAddr, FlashPageReadData.D8);
		
		if(FlashPageReadData.D32[0] == 0xFFFFFFFF)FlashPageReadData.D32[0] = 0;
		if(FlashPageReadData.D32[8] == 0xFFFFFFFF)FlashPageReadData.D32[8] = 0;
		if(FlashPageReadData.D32[16] == 0xFFFFFFFF)FlashPageReadData.D32[16] = 0;
		if(FlashPageReadData.D32[24] == 0xFFFFFFFF)FlashPageReadData.D32[24] = 0;
		if(FlashPageReadData.D32[32] == 0xFFFFFFFF)FlashPageReadData.D32[32] = 0;
		if(FlashPageReadData.D32[40] == 0xFFFFFFFF)FlashPageReadData.D32[40] = 0;
		if(FlashPageReadData.D32[48] == 0xFFFFFFFF)FlashPageReadData.D32[48] = 0;
		if(FlashPageReadData.D32[56] == 0xFFFFFFFF)FlashPageReadData.D32[56] = 0;
		
		sdAddr = dAddrSdRam_4Area + (x * 256);
		for(i=0; i<64;i++){
			*(volatile unsigned int *)(sdAddr + i*4) = FlashPageReadData.D32[i];
		}
	}
}

/*
void TRecord::ReadMaskFromFlash()
{
	volatile int i, x;
	volatile unsigned int fAddr, sAddr,  MaskListAddr, MaskCount;
	unsigned char MaskSta;
	unsigned int P0, P1;

	MaskCount = 0;
	MaskListAddr = dFlashAddrMask;	//+ 0x80000
	
	WaitChipIdle(dFlashMaskChip);
	for(P0=0; P0<dPORT_MAX_COUNT; P0++){
		fAddr = dFlashAddrMask + (P0 * SizeOf4K);
		sAddr = dAddrSdRam_RuntimeBuf + (P0 * SizeOf4K);
		for(i=0; i<8; i++){
			nor_read_page(dFlashMaskChip, fAddr, FlashPageReadData.D8);
			for(x=0; x<32;x++){
				P1 = i*32+x;
				MaskSta = FlashPageReadData.D8[x*8+7];
				if(MaskSta == 0xA5){
					if(MaskCount< ieMSG_MASK_MAX_COUNT){
						FlashPageReadData.D8[x*8+6] =0;
						FlashPageReadData.D8[x*8+7] =0;
						//Load Path
						*(volatile unsigned int *)(MaskListAddr + 0) =  P0;
						*(volatile unsigned int *)(MaskListAddr + 1) =  P1;
						//Load Time
						*(volatile unsigned int *)(MaskListAddr + 2) =  FlashPageReadData.D32[x*8+0];
						*(volatile unsigned int *)(MaskListAddr + 3) =  FlashPageReadData.D32[x*8+4];
						MaskListAddr += 16;
						MaskCount++;
					}
					
					Port[P0].StateTable[P1] |= dEpStateBitMask;
				}else{
					Port[P0].StateTable[P1] &= ~dEpStateBitMask;
				}
			}
			fAddr += SizeOfPage;
			sAddr += SizeOfPage;
		}	
	}
	
	CtlCenter.ieMsgMask.Count = MaskCount;
	for(i=0; i<MaskCount; i++){
		
	}
}*/


//void TRecord::Task1Ms()
//{
	//if(Eep0WriteTick < 100)Eep0WriteTick++;
	//if(Eep1WriteTick < 100)Eep1WriteTick++;
//}

void TRecord::Wait10Ms()
{
	DelayMs(15);
}

//_____________________________________________________EEP
void TRecord::ReadProjAndCtlDescp()
{
	unsigned int aAddr;
	int i;
	//Read ExistTable 
	aAddr = dEepAddrProjectName;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, FlashPageReadData.D8, 64);
	for(i=0; i<32; i++){
		PrjName[i] = FlashPageReadData.D8[i];
	}
	for(i=32; i<64; i++){
		CtlName[i-32] = FlashPageReadData.D8[i];
	}
	if( (PrjName[0] == 0xFF) && (PrjName[1] == 0xFF) ){
		PrjName[0] = '\0';
		PrjName[1] = '\0';
	}
	if( (CtlName[0] == 0xFF) && (CtlName[1] == 0xFF) ){
		CtlName[0] = '\0';
		CtlName[1] = '\0';
	}
	PrjName[32] = '\0';
	PrjName[33] = '\0';
	CtlName[32] = '\0';
	CtlName[33] = '\0';
}

void TRecord::ReadDesignPrm()
{

}

void TRecord::ReadDisplayPadSet()
{
	unsigned int i;
	
	SysLoopTick = 0;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTablePort[i]){
			ReadPortDisplayPadSet(i+1);
			sys_error_led_task();
		}
	}
}

void TRecord::LoadBusPadLnk(unsigned char aPad)
{
	unsigned int fAddr;
	unsigned int i, z, x;
	unsigned char aP0, aP1s, aP1e;
	volatile unsigned int aFlashState, Error;
	unsigned char wPad;
	if(aPad==0)return;
	if(aPad>dHANDCTL_MAX_COUNT)return;
	
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(dFlashBusPadLnkChipX);
	}while(aFlashState);
	Error = 0;
	i=aPad-1;
	SysLoopTick = 0;
	
	for(z=0; z<dHANDCTL_KEY_MAX_COUNT; z++){
		HandCtlBoard[i].KeyFbRequst[z] = 0;
	}
	Crc32Reset();
	for(z=0; z<2; z++){
		sys_error_led_task();
		fAddr = dFlashAddrBusPadLnk + (i * 4096) + (z * 256);
		nor_read_page(dFlashBusPadLnkChipX, fAddr, FlashPageReadData.D8);
		for(x=0; x<64; x++){
			HandCtlBoard[i].UcList[z* 64 + x].Full = FlashPageReadData.D32[x];
			Crc32Feed(FlashPageReadData.D32[x]);
			HandCtlBoard[i].UcListNotAnd[z* 64 + x].Full = 0;
			for(int Gz=0; Gz<8; Gz++){
				if( ((HandCtlBoard[i].UcList[z* 64 + x].Full >> (Gz *4)) & 0x0000000F) == 10){
					HandCtlBoard[i].UcListNotAnd[z* 64 + x].Full |= 0x0000000F << (Gz * 4);
				}else
				if( ((HandCtlBoard[i].UcList[z* 64 + x].Full >> (Gz *4)) & 0x0000000F) > 10 ){
					HandCtlBoard[i].UcList[z* 64 + x].Full = 0;
					Error =1;
					break;
				}
			}
		}
	}
	fAddr = dFlashAddrBusPadLnk + (i * 4096) + (2 * 256);
	nor_read_page(dFlashBusPadLnkChipX, fAddr, FlashPageReadData.D8);
	for(x=0; x<dHANDCTL_KEY_MAX_COUNT; x++){
		HandCtlBoard[i].AssignType[x] = FlashPageReadData.D8[x];
		Crc32Feed(static_cast<unsigned int>(HandCtlBoard[i].AssignType[x]));
	}
	fAddr = dFlashAddrBusPadLnk + (i * 4096) + (3 * 256);
	nor_read_page(dFlashBusPadLnkChipX, fAddr, FlashPageReadData.D8);
	for(x=0; x<dHANDCTL_KEY_MAX_COUNT; x++){
		HandCtlBoard[i].IsMatchAreaAssign[x] = FlashPageReadData.D8[x];
		Crc32Feed(static_cast<unsigned int>(HandCtlBoard[i].IsMatchAreaAssign[x]));
	}
	fAddr = dFlashAddrBusPadLnk + (i * 4096) + (4 * 256);
	nor_read_page(dFlashBusPadLnkChipX, fAddr, FlashPageReadData.D8);
	for(x=0; x<dHANDCTL_KEY_MAX_COUNT; x++){
		HandCtlBoard[i].Area[x] = FlashPageReadData.D16[x];
		Crc32Feed(static_cast<unsigned int>(HandCtlBoard[i].Area[x]));
		if(HandCtlBoard[i].Area[x] > dAreaCountMax){
			HandCtlBoard[i].Area[x] =0;
			Error =1;
		}
	}
	
	fAddr = dFlashAddrBusPadLnk + (i * 4096) + (5 * 256);
	nor_read_page(dFlashBusPadLnkChipX, fAddr, FlashPageReadData.D8);
	HandCtlBoard[i].PadIsProhibit = FlashPageReadData.D8[0];
	HandCtlBoard[i].KeyCount = FlashPageReadData.D8[1];
	HandCtlBoard[i].Crc = FlashPageReadData.D32[1];
	
	if(   (HandCtlBoard[i].KeyCount != 48) && (HandCtlBoard[i].KeyCount != 64) && (HandCtlBoard[i].KeyCount != 128)  ){
		HandCtlBoard[i].KeyCount = 0;
		Error =1;
	}
	
	Crc32Feed(static_cast<unsigned int>(HandCtlBoard[i].PadIsProhibit));
	Crc32Feed(static_cast<unsigned int>(HandCtlBoard[i].KeyCount));
	
	if( (HandCtlBoard[i].Crc==Crc32GetResult()) and (Error==0)){
		if(HandCtlBoard[i].CrcFlag){
			HandCtlBoard[i].CrcFlag = 0;
			CtlCenter.ieAddBrdFaultCancel(i+1,TBoardFaultType::StorageBusPadCfg);
		}
	}else{
		HandCtlBoard[i].CrcFlag = 1;
		CtlCenter.ieAddBrdFault(i+1,TBoardFaultType::StorageBusPadCfg);
	}
	
	SysLoopTick = 0;
	HandCtlBoard[i].PadHasConfig = 0;
	HandCtlBoard[i].PadFbRequst = 0;
	for(z=0; z<dHANDCTL_KEY_MAX_COUNT; z++){
		HandCtlBoard[i].KeyFbRequst[z] = 0;
		if( 	(HandCtlBoard[i].AssignType[z] == dgTYPE_IN_MODULE) ||
			(HandCtlBoard[i].AssignType[z] == dgTYPE_INOUT_MODULE_NonSource) ||
			(HandCtlBoard[i].AssignType[z] == dgTYPE_INOUT_MODULE_Source) /*||
			(HandCtlBoard[i].AssignType[z] == 0) */){
				HandCtlBoard[i].KeyFbRequst[z] = 1;
				HandCtlBoard[i].PadFbRequst = 1;
		}
		if(HandCtlBoard[i].IsMatchAreaAssign[z]){
			if(HandCtlBoard[i].Area[z] != 0){
				HandCtlBoard[i].PadHasConfig = 1;
				HandCtlBoard[i].KeyHasConfig[z] = 0xA5;
			}else{
				HandCtlBoard[i].KeyHasConfig[z] = 0;
			}
		}else{
			if(  (HandCtlBoard[i].UcList[z].Full != 0) && (HandCtlBoard[i].UcList[z].Full < 100000000) ){
				HandCtlBoard[i].PadHasConfig = 1;
				HandCtlBoard[i].KeyHasConfig[z] = 0xA5;
			}else{
				HandCtlBoard[i].KeyHasConfig[z] = 0;
			}
		}
	}
}

void TRecord::LoadBusPadLnk()
{
	unsigned int fAddr;
	unsigned int i, z, x;
	unsigned char aP0, aP1s, aP1e;
	volatile unsigned int aFlashState;
	
	SysLoopTick = 0;
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(dFlashBusPadLnkChipX);
	}while(aFlashState);
	
	DelayMs(2);
	for(i=0; i<dHANDCTL_MAX_COUNT; i++){
		SysLoopTick = 0;
		LoadBusPadLnk(i+1);
	}	
}

void TRecord::LoadDirectPadCfg(unsigned char aPath0)
{
	unsigned int fAddr;
	unsigned int i, x, z;
	volatile unsigned int GetSta;
	TDMix tCrc;
	
	if(aPath0==0)return;
	if(aPath0>dDIRECTCTL_MAX_COUNT)return;
	
	i=aPath0-1;
	
	DelayMs(2);
	
	SysLoopTick = 0;
	for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
		DirectCtlBoard[i].IsRegisted[x] = 0;
		DirectCtlBoard[i].IsCheckError[x] = 0;
		//DirectCtlBoard[i].IsPump[z] = 0;
		DirectCtlBoard[i].IsProhibitLinkage[x] = 0;
		DirectCtlBoard[i].IsOutputState[x] = 0;
	}
	
	DirectCtlBoard[i].PadHasConfig = 0;
	
	do{
		DelayMs(3);
		GetSta = nor_get_busy_state(dFlashDirectPadLnkChipX);
	}while(GetSta);
	
	fAddr = dFlashAddrDirectPadLnk + (i * 4096);
	nor_read_page(dFlashDirectPadLnkChipX, fAddr, FlashPageReadData.D8);
	
	Crc32Reset();
	for(x=0;x<dDIRECTCTL_KEY_MAX_COUNT;x++){
		DirectCtlBoard[i].IsRegisted[x]		= FlashPageReadData.D8[x*8 +0];
		DirectCtlBoard[i].IsCheckError[x]		= FlashPageReadData.D8[x*8 +1];
		//DirectCtlBoard[i].IsPump[x]			= 0;
		//DirectCtlBoard[i].IsProhibitLinkage[x]	= 0;
		
		//DirectCtlBoard[i].IsOutputState[x]		= 0;
		//
		//
		if(x==0)DirectCtlBoard[i].PadIsProhibit = FlashPageReadData.D8[x*8 +7];
		
		if(DirectCtlBoard[i].IsRegisted[x])DirectCtlBoard[i].PadHasConfig++;
		Crc32Feed(static_cast<unsigned int>(DirectCtlBoard[i].IsRegisted[x]));
		Crc32Feed(static_cast<unsigned int>(DirectCtlBoard[i].IsCheckError[x]));
		Crc32Feed(static_cast<unsigned int>(FlashPageReadData.D8[x*8 +7]));
	}
	DirectCtlBoard[i].CrcConfig = FlashPageReadData.D32[16]; 
	if(DirectCtlBoard[i].CrcConfig== Crc32GetResult()){
		if(DirectCtlBoard[i].CrcFlag.Config){
			DirectCtlBoard[i].CrcFlag.Config =0;
			CtlCenter.ieAddBrdFaultCancel(i+dADDR_DIRECTPAD_START, TBoardFaultType::StorageDirectPadCfg);
		}
	}else{
		DirectCtlBoard[i].CrcFlag.Config =1;
		CtlCenter.ieAddBrdFault(i+dADDR_DIRECTPAD_START, TBoardFaultType::StorageDirectPadCfg);
	}
	
	DirectCtlBoard[i].SendPadCfg2Pad(i);
	
	sys_error_led_task();	
}

void TRecord::LoadDirectPadCfg()
{
	int i;
	for(i=1; i<=dDIRECTCTL_MAX_COUNT; i++){
		LoadDirectPadCfg(i);
	}	
}

void TRecord::ReadMainSet()
{
	unsigned int aAddr;
	int i;
	//Read ExistTable 
	aAddr = dEepAddrMainSet;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, FlashPageReadData.D8, 32);
	
	MainCtl.fData.Split.MyNum = FlashPageReadData.D8[0];
	MainCtl.fData.Split.NetworkMode = FlashPageReadData.D8[1];
	PriterSetting.OnOff = FlashPageReadData.D8[2];
	PriterSetting.PrintSelected = FlashPageReadData.D8[3];
	AllowAuto = FlashPageReadData.D8[4];
	AllowManual = FlashPageReadData.D8[5];
	
	if( (AllowAuto == 0) && (AllowManual == 0) ){
		AllowAuto =1;
	}
	if(AllowAuto == 0)AllowManual =2;else AllowManual =0;
	
	ScreenAndKeyLock.SaveOn = FlashPageReadData.D8[6];
	ScreenAndKeyLock.SaveTime = FlashPageReadData.D8[7];
	ScreenAndKeyLock.IsKeyLock = FlashPageReadData.D8[8];
	ScreenAndKeyLock.UnLockText[0] = FlashPageReadData.D8[9];
	ScreenAndKeyLock.UnLockText[1] = FlashPageReadData.D8[10];
	ScreenAndKeyLock.UnLockText[2] = FlashPageReadData.D8[11];
	ScreenAndKeyLock.UnLockText[3] = FlashPageReadData.D8[12];
	if(ScreenAndKeyLock.SaveTime <1)ScreenAndKeyLock.SaveTime = 1;
	if(ScreenAndKeyLock.SaveTime >20)ScreenAndKeyLock.SaveTime = 20;
	//ScreenAndKeyLock.IsKeyLock = 0;
	
	i = FlashPageReadData.D8[17];
	i = i<<8;
	i |= FlashPageReadData.D8[16];
	i = i<<8;
	i |= FlashPageReadData.D8[15];
	i = i<<8;
	i |= FlashPageReadData.D8[14];
	
	//if(FlashPageReadData.D8[13] == 0xA5)GraphUsing = 1;
	//else GraphUsing = 0;
	//GraphDevBrd = i;
	//GraphDevFrameIntervalMs = FlashPageReadData.D8[18];
	//GraphDevHalfDuplex = FlashPageReadData.D8[19];
	//GraphDevReplyDelayUs = FlashPageReadData.D8[20];
	//GraphDevReplyRetainTimeMs = FlashPageReadData.D8[21];
	PowerPrmValue.MainCount = 1;
	PowerPrmValue.SubExist[0] = FlashPageReadData.D8[22];
	PowerPrmValue.SubExist[1] = FlashPageReadData.D8[23];
	if(PowerPrmValue.SubExist[0] != 1)PowerPrmValue.SubExist[0] =0;
	if(PowerPrmValue.SubExist[1] != 1)PowerPrmValue.SubExist[1] =0;
	
	LanguageEnCn = FlashPageReadData.D8[24];
	if(LanguageEnCn != 1)LanguageEnCn =0;
	LanguageEnCn = 0;
	
	tData[1] =0; tData[1] =0;
	tData[2] =0; tData[3] =0; tData[4] =0; tData[5] =0; tData[6] =0; tData[7] =0;
	if(AllowAuto)tData[1] |= 0x01;
	if(AllowManual)tData[1] |= 0x02;
	InnerCan.OBufInsert(1, 88, tData, 2);
	
	if( (MainCtl.fData.Split.MyNum == 0) || (MainCtl.fData.Split.MyNum > dNETWORK_MACHINE_COUNT) ){
		MainCtl.fData.Split.MyNum =1;
		MainCtl.fData.Split.NetworkMode = 0;
	}else{
		if(MainCtl.fData.Split.NetworkMode >2)MainCtl.fData.Split.NetworkMode =0;
	}
	if(PriterSetting.PrintSelected > 255)PriterSetting.PrintSelected = 255;
}

void TRecord::ReadAllowSet()
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	int i;
	//Read ExistTable 
	aAddr = dEepAddrMainSet;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, FlashPageReadData.D8, 6);
	
	AllowAuto = FlashPageReadData.D8[4];
	AllowManual = FlashPageReadData.D8[5];
	if(AllowAuto == 0)AllowManual =2;else AllowManual =0;
}

void TRecord::ReadPrintSet()
{
	unsigned int aAddr;
	volatile unsigned int aWait;
	int i;
	//Read ExistTable 
	aAddr = dEepAddrMainSet;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, FlashPageReadData.D8, 4);
	
	PriterSetting.OnOff = FlashPageReadData.D8[2];
	PriterSetting.PrintSelected = FlashPageReadData.D8[3];
}

void TRecord::WriteMainSetMainCtl(unsigned char aIsMaster, unsigned char aCtlNum)
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	unsigned char aData[2];
	int i;
	//Read ExistTable 
	aData[0] = aCtlNum;
	aData[1] = aIsMaster;
	aAddr = dEepAddrMainSet + 0;
	DelayMs(10);
	eeprom_write_byte_via_page(0, aAddr, aData, 2);
	DelayMs(10);
}

void TRecord::WritePrinterState(unsigned char aSta)
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	unsigned char aData[2];
	//Read ExistTable 
	aData[0] = aSta;
	aData[1] = 0;
	aAddr = dEepAddrMainSet + 2;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, aData, 1);
}

void TRecord::WritePrinterEvent(unsigned char aEvent)
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	unsigned char aData[2];
	//Read ExistTable 
	aData[0] = aEvent;
	aData[1] = 0;
	aAddr = dEepAddrMainSet + 3;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, aData, 1);	
}

void TRecord::WriteAllow(unsigned char aAllowAuto, unsigned char aAllowManual)
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	unsigned char aData[2];
	//Read ExistTable 
	aData[0] = aAllowAuto;
	aData[1] = aAllowManual;
	aAddr = dEepAddrMainSet + 4;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, aData, 2);	
}

void TRecord::WriteScreenKeyLock(unsigned char aScreenSave, unsigned char EnterSaveTime, unsigned char aLock, char *aCr)
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	unsigned char aData[8];
	//Read ExistTable 
	aData[0] = aScreenSave;
	aData[1] = EnterSaveTime;
	aData[2] = aLock;
	aData[3] = *aCr++;
	aData[4] = *aCr++;
	aData[5] = *aCr++;
	aData[6] = *aCr++;
	
	aAddr = dEepAddrMainSet + 6;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, aData, 7);	
	
	DelayMs(20);
	eeprom_read_byte(0, aAddr, aData, 7);
	ScreenAndKeyLock.SaveOn = aData[0];
	ScreenAndKeyLock.SaveTime =  aData[1];
	ScreenAndKeyLock.IsKeyLock =  aData[2];
	ScreenAndKeyLock.UnLockText[0] =  aData[3];
	ScreenAndKeyLock.UnLockText[1] =  aData[4];
	ScreenAndKeyLock.UnLockText[2] =  aData[5];
	ScreenAndKeyLock.UnLockText[3] =  aData[6];
	if(ScreenAndKeyLock.SaveTime <1)ScreenAndKeyLock.SaveTime = 1;
	if(ScreenAndKeyLock.SaveTime >20)ScreenAndKeyLock.SaveTime = 20;
	//ScreenAndKeyLock.IsKeyLock = 0;
	DelayMs(20);
}

void TRecord::ReadScreenSave()
{
	unsigned int aAddr;
	int i;
	//Read ExistTable 
	aAddr = dEepAddrMainSet;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, FlashPageReadData.D8, 32);
	
	ScreenAndKeyLock.SaveOn = FlashPageReadData.D8[6];
	ScreenAndKeyLock.SaveTime = FlashPageReadData.D8[7];
	ScreenAndKeyLock.IsKeyLock = FlashPageReadData.D8[8];
	ScreenAndKeyLock.UnLockText[0] = FlashPageReadData.D8[9];
	ScreenAndKeyLock.UnLockText[1] = FlashPageReadData.D8[10];
	ScreenAndKeyLock.UnLockText[2] = FlashPageReadData.D8[11];
	ScreenAndKeyLock.UnLockText[3] = FlashPageReadData.D8[12];
	if(ScreenAndKeyLock.SaveTime <1)ScreenAndKeyLock.SaveTime = 1;
	if(ScreenAndKeyLock.SaveTime >20)ScreenAndKeyLock.SaveTime = 20;
	//ScreenAndKeyLock.IsKeyLock = 0;
}

void TRecord::WriteGraphSet(unsigned int aBrd, unsigned char aIntavalMs, unsigned char aIsHaft, unsigned char RpyDelayUs, unsigned char RpyTimeMs)
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	unsigned char aData[8];
	//Read ExistTable 
	aData[0] = static_cast<unsigned char>(aBrd & 0x000000FF);
	aData[1] = static_cast<unsigned char>((aBrd >>  8) & 0x000000FF);
	aData[2] = static_cast<unsigned char>((aBrd >> 16) & 0x000000FF);
	aData[3] = static_cast<unsigned char>((aBrd >> 24) & 0x000000FF);
	aData[4] = aIntavalMs;
	aData[5] = aIsHaft;
	aData[6] = RpyDelayUs;
	aData[7] = RpyTimeMs;
	aAddr = dEepAddrMainSet + 13;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, aData, 8);	
}

void TRecord::SaveNewTime2Eep()
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	unsigned char aData[6];
	
	aData[0] = DateTime.Time.Split.Second;
	aData[1] = DateTime.Time.Split.Minute;
	aData[2] = DateTime.Time.Split.Hour;
	
	aData[3] = DateTime.Date.Split.Day;
	aData[4] = DateTime.Date.Split.Month;
	aData[5] = DateTime.Date.Split.Year;
	
	aAddr = dEepAddrCtlShutDownTime;
	DelayMs(10);
	eeprom_write_byte_via_page(0, aAddr, aData, 6);
}

void TRecord::SaveNwCfg(unsigned char aMasterNum, unsigned char aSlaveCount, unsigned char *aList)
{
	volatile unsigned int i;
	unsigned int aAddr;
	
	FlashPageReadData.D8[ 0] = aMasterNum; 
	FlashPageReadData.D8[ 1] = aSlaveCount;
	
	for(i=0; (i<aSlaveCount) and (i<dNETWORK_MACHINE_COUNT); i++){
		FlashPageReadData.D8[i + 2] = aList[i];
	}
	
	aAddr = dEepAddrNetworkCfg;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, FlashPageReadData.D8, 2+dNETWORK_MACHINE_COUNT);
	DelayMs(20);
}

void TRecord::ReadNwCfg()
{
	unsigned int aAddr;
	int i;
	//Read ExistTable 
	for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
		ProtoC2C.SlaveList[i] = 0;
	}
	
	aAddr = dEepAddrNetworkCfg;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, FlashPageReadData.D8, 2+dNETWORK_MACHINE_COUNT);
	
	ProtoC2C.MasterNum = FlashPageReadData.D8[ 0]; 
	ProtoC2C.SlaveCount = FlashPageReadData.D8[ 1];
	if(ProtoC2C.MasterNum > dNETWORK_MACHINE_COUNT)
		ProtoC2C.MasterNum  = 0;
	
	if(ProtoC2C.SlaveCount > dNETWORK_MACHINE_COUNT )
		ProtoC2C.SlaveCount  = 0;
	
	for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
		ProtoC2C.SlaveList[i] = FlashPageReadData.D8[i + 2];
		if(ProtoC2C.SlaveList[i] >dNETWORK_MACHINE_COUNT)ProtoC2C.SlaveList[i] =0;
	}
}

void TRecord::SaveNwM2MCfg(unsigned char aCnt, unsigned char *aList)
{
	volatile unsigned int i;
	unsigned int aAddr;
	
	for(i=0; i<dNETWORK_MACHINE_MASTER_COUNT; i++){
		FlashPageReadData.D8[i] = aList[i];
	}
	
	aAddr = dEepAddrNetworkCfg+80;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, FlashPageReadData.D8, dNETWORK_MACHINE_MASTER_COUNT);
	DelayMs(20);
}

void TRecord::ReadNwM2MCfg()
{
	unsigned int aAddr;
	int i;
	//Read ExistTable 
	
	aAddr = dEepAddrNetworkCfg+80;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, FlashPageReadData.D8, dNETWORK_MACHINE_MASTER_COUNT);
	
	ProtoC2C.M2MList.Cnt=0;
	for(i=0; i<dNETWORK_MACHINE_MASTER_COUNT; i++){
		if( (FlashPageReadData.D8[i]) && (FlashPageReadData.D8[i]<=dNETWORK_MACHINE_COUNT) ){
			ProtoC2C.M2MList.Items[i] = FlashPageReadData.D8[i];
			ProtoC2C.M2MList.Cnt++;
		}else{
			ProtoC2C.M2MList.Items[i] = 0;
		}
	}
}

void TRecord::WritePowerSuplyCount(unsigned char aSubExist0, unsigned char aSubExist1)
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	unsigned char aData[2];
	//Read ExistTable 
	aData[0] = aSubExist0;
	aData[1] = aSubExist1;
	aAddr = dEepAddrMainSet + 22;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, aData, 2);
}

void TRecord::ReadPowerSuplyCount()
{
	unsigned int aAddr;
	volatile unsigned int aWait;
	int i;
	//Read ExistTable 
	aAddr = dEepAddrMainSet +22;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, FlashPageReadData.D8, 2);
	
	PowerPrmValue.MainCount = 1;
	PowerPrmValue.SubExist[0] = FlashPageReadData.D8[0];
	PowerPrmValue.SubExist[1] = FlashPageReadData.D8[1];
	
	if(PowerPrmValue.SubExist[0] != 1)PowerPrmValue.SubExist[0] =0;
	if(PowerPrmValue.SubExist[1] != 1)PowerPrmValue.SubExist[1] =0;
}

void TRecord::ReadLanguage()
{
	unsigned int aAddr;
	volatile unsigned int aWait;
	int i;
	//Read ExistTable 
	aAddr = dEepAddrMainSet +24;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, FlashPageReadData.D8, 1);
	
	LanguageEnCn = FlashPageReadData.D8[0];	
	
	LanguageEnCn = 0;
	//LanguageEnCn = 0;
}

void TRecord::WriteLanguage(unsigned char aNew)
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	unsigned char aData[2];
	//Read ExistTable 
	aData[0] = aNew;
	aData[1] = 0;
	aAddr = dEepAddrMainSet + 24;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, aData, 1);
	DelayMs(20);
}

void TRecord::WriteMaintain(unsigned char *p)
{
	unsigned int aAddr;
	
	aAddr = dEepAddrMainTain;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, p, 16);		
	DelayMs(20);	
}

void TRecord::ReadMaintain()
{
	unsigned int aAddr;
	int i;
	//Read ExistTable 
	for(i=0; i<16; i++){
		ProtoC2C.SlaveList[i] = 0;
	}
	
	aAddr = dEepAddrMainTain;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, FlashPageReadData.D8, 20);
	
	for(i=0; i<16; i++)MaintainData.D8[i] = FlashPageReadData.D8[i];
	
	if( (MaintainData.fDa.Month0 ==0) || (MaintainData.fDa.Month0 >12) ) MaintainData.fDa.Month0 = 1;
	if( (MaintainData.fDa.Day0 == 0) || (MaintainData.fDa.Day0 >TDateTime::MonthDaysNotLeapYear[MaintainData.fDa.Month0-1]) ) MaintainData.fDa.Day0 = 1;
	
	if( (MaintainData.fDa.Month1 ==0) || (MaintainData.fDa.Month1 >12) ) MaintainData.fDa.Month1 = 1;
	if( (MaintainData.fDa.Day1 == 0) || (MaintainData.fDa.Day1 >TDateTime::MonthDaysNotLeapYear[MaintainData.fDa.Month1-1]) ) MaintainData.fDa.Day1 = 1;	
	
	if( (MaintainData.fDa.Month2 ==0) || (MaintainData.fDa.Month2 >12) ) MaintainData.fDa.Month2 = 1;
	if( (MaintainData.fDa.Day2 == 0) || (MaintainData.fDa.Day2 >TDateTime::MonthDaysNotLeapYear[MaintainData.fDa.Month2-1]) ) MaintainData.fDa.Day2 = 1;

	
	/*
	if(MaintainData.fDa.IsCustomTime == 0){
		if(MaintainData.fDa.IsHalfYear){
			//2 Point
			if( (MaintainData.fDa.Month0 ==0) || (MaintainData.fDa.Month0 >12) ) MaintainData.fDa.Month0 = 1;
			if( (MaintainData.fDa.Day0 == 0) || (MaintainData.fDa.Day0 >TDateTime::MonthDaysNotLeapYear[MaintainData.fDa.Month0-1]) ) MaintainData.fDa.Day0 = 1;
				
			if( (MaintainData.fDa.Month1 ==0) || (MaintainData.fDa.Month1 >12) ) MaintainData.fDa.Month1 = 1;
			if( (MaintainData.fDa.Day1 == 0) || (MaintainData.fDa.Day1 >TDateTime::MonthDaysNotLeapYear[MaintainData.fDa.Month1-1]) ) MaintainData.fDa.Day1 = 1;
		}else{
			//1 Point
			if( (MaintainData.fDa.Month0 ==0) || (MaintainData.fDa.Month0 >12) ) MaintainData.fDa.Month0 = 1;
			if( (MaintainData.fDa.Day0 == 0) || (MaintainData.fDa.Day0 >TDateTime::MonthDaysNotLeapYear[MaintainData.fDa.Month0-1]) ) MaintainData.fDa.Day0 = 1;
		}
	}else{
		//3 Point
		
		if( (MaintainData.fDa.Month0 ==0) || (MaintainData.fDa.Month0 >12) ) MaintainData.fDa.Month0 = 1;
		if( (MaintainData.fDa.Day0 == 0) || (MaintainData.fDa.Day0 >TDateTime::MonthDaysNotLeapYear[MaintainData.fDa.Month0-1]) ) MaintainData.fDa.Day0 = 1;
		
		if( (MaintainData.fDa.Month1 ==0) || (MaintainData.fDa.Month1 >12) ) MaintainData.fDa.Month1 = 1;
		if( (MaintainData.fDa.Day1 == 0) || (MaintainData.fDa.Day1 >TDateTime::MonthDaysNotLeapYear[MaintainData.fDa.Month1-1]) ) MaintainData.fDa.Day1 = 1;	
		
		if( (MaintainData.fDa.Month2 ==0) || (MaintainData.fDa.Month2 >12) ) MaintainData.fDa.Month2 = 1;
		if( (MaintainData.fDa.Day2 == 0) || (MaintainData.fDa.Day2 >TDateTime::MonthDaysNotLeapYear[MaintainData.fDa.Month2-1]) ) MaintainData.fDa.Day2 = 1;
	}*/
}

void TRecord::Read4Area()
{
	unsigned int fAddr, sdAddr;
	unsigned int x,i;
	unsigned int wP1;
	unsigned int PageCount;
	volatile unsigned int GetSta;
	do{
		DelayMs(3);
		GetSta = nor_get_busy_state(dFlash4AreaChip);
	}while(GetSta);	
	
	PageCount = dAreaCountMax / (256 / 64);
	
	for(x=0; x<PageCount; x++){
		SysLoopTick = 0;
		fAddr = dFlash4Area + (x * 256);
		DelayMs(2);
		nor_read_page(dFlash4AreaChip, fAddr, FlashPageReadData.D8);
		
		sdAddr = dAddrSdRam_4Area + (x * 256);
		for(i=0; i<64;i++){
			*(volatile unsigned int *)(sdAddr + i*4) = FlashPageReadData.D32[i];
		}
	}
	AreaSetting.Count =0;
	for(x=0; x<dAreaCountMax; x++){
		sdAddr = dAddrSdRam_4Area + (x * 64);
		wP1 = *(volatile unsigned int *)(sdAddr);
		if(wP1 == 0xA8A7A6A5)AreaSetting.Count++;
	}
}

void TRecord::ReadDevAreaNum()
{
	unsigned int fAddr;
	unsigned int i;
	unsigned int aUcFull;
	volatile unsigned int x, GetSta;
	unsigned char aP0, aP1, wP1;
	TDMix tCrc;
	
	SysLoopTick = 0;
	do{
		DelayMs(3);
		GetSta = nor_get_busy_state(dFlash4DevAreaNumChip);
	}while(GetSta);
		
	for(i=0; i<dPORT_MAX_COUNT; i++){
		Crc32Reset();
		for(int Gg=0; Gg<2; Gg++){
			SysLoopTick = 0;
			do{
				DelayMs(5);
				GetSta = nor_get_busy_state(dFlash4DevAreaNumChip);
			}while(GetSta);
			
			fAddr = dFlash4DevAreaNum + (i * 512) + (Gg * 256);
			DelayMs(2);
			nor_read_page(dFlash4AreaChip, fAddr, FlashPageReadData.D8);
			if(Gg ==0){
				for(x=0; x<128;x++){
					Port[i].Area[x] = FlashPageReadData.D16[x];
					Crc32Feed(static_cast<unsigned int>(Port[i].Area[x]));
					if(Port[i].Area[x] > dAreaCountMax)Port[i].Area[x] =0;
				}
			}else
			if(Gg == 1){
				for(x=0; x<128;x++){
					Port[i].Area[x + 128] = FlashPageReadData.D16[x];
					if(Port[i].Area[x + 128] > dAreaCountMax)Port[i].Area[x + 128] =0;
					if(x<(dEP_MAX_COUNT_PER_PORT-128))Crc32Feed(static_cast<unsigned int>(Port[i].Area[x+128]));	
				}
				tCrc.D16[0]=FlashPageReadData.D16[126];
				tCrc.D16[1]=FlashPageReadData.D16[127];
				Port[i].CrcArea = tCrc.D32;
				if(Port[i].CrcArea == Crc32GetResult()){
					if(Port[i].CrcFlag.Area){
						Port[i].CrcFlag.Area =0;
						CtlCenter.ieAddBrdFaultCancel(i+1,TBoardFaultType::StorageEpArea);
					}
				}else{
					Port[i].CrcFlag.Area =1;
					CtlCenter.ieAddBrdFault(i+1,TBoardFaultType::StorageEpArea);
				}
			}
		}
	}
}

void TRecord::ReadDirectPadAreaNum()
{
	unsigned int fAddr;
	volatile unsigned int x, GetSta;
	unsigned char aP0, aP1, wP1;
	TDMix tCrc;
	
	SysLoopTick = 0;
	do{
		DelayMs(3);
		GetSta = nor_get_busy_state(dFlash4DevAreaNumChip);
	}while(GetSta);
		
	for(int Gg=0; Gg<2; Gg++){
		SysLoopTick = 0;
		do{
			DelayMs(5);
			GetSta = nor_get_busy_state(dFlash4DevAreaNumChip);
		}while(GetSta);
		
		fAddr = dFlash4DevAreaNum + (40 * 512) + (Gg * 256);
		DelayMs(2);
		nor_read_page(dFlash4AreaChip, fAddr, FlashPageReadData.D8);
		
		if(Gg ==0){
			for(int pNum=0; pNum<10; pNum++){
				Crc32Reset();
				for(x=0; x<8; x++){
					DirectCtlBoard[pNum].Area[x] = FlashPageReadData.D16[pNum * (dDIRECTCTL_KEY_MAX_COUNT+2) + x];
					Crc32Feed(static_cast<unsigned int>(DirectCtlBoard[pNum].Area[x]));
					if(DirectCtlBoard[pNum].Area[x] > dAreaCountMax)DirectCtlBoard[pNum].Area[x] = 0;
				}
				tCrc.D16[0] = FlashPageReadData.D16[pNum * (dDIRECTCTL_KEY_MAX_COUNT+2) + 8];
				tCrc.D16[1] = FlashPageReadData.D16[pNum * (dDIRECTCTL_KEY_MAX_COUNT+2) + 9];
				DirectCtlBoard[pNum].CrcArea = tCrc.D32;
				if(DirectCtlBoard[pNum].CrcArea == Crc32GetResult()){
					if(DirectCtlBoard[pNum].CrcFlag.Area){
						DirectCtlBoard[pNum].CrcFlag.Area =0;
						CtlCenter.ieAddBrdFaultCancel(pNum+dADDR_DIRECTPAD_START,TBoardFaultType::StorageEpArea);
					}
				}else{
					DirectCtlBoard[pNum].CrcFlag.Area =1;
					CtlCenter.ieAddBrdFault(pNum+dADDR_DIRECTPAD_START,TBoardFaultType::StorageEpArea);
				}
			}
		}else
		if(Gg == 1){
			for(int pNum=10; pNum<20; pNum++){
				Crc32Reset();
				for(x=0; x<8; x++){
					DirectCtlBoard[pNum].Area[x] = FlashPageReadData.D16[(pNum-10) * (dDIRECTCTL_KEY_MAX_COUNT+2) + x];
					Crc32Feed(static_cast<unsigned int>(DirectCtlBoard[pNum].Area[x]));
					if(DirectCtlBoard[pNum].Area[x] > dAreaCountMax)DirectCtlBoard[pNum].Area[x] = 0;
				}
				tCrc.D16[0] = FlashPageReadData.D16[(pNum-10) * (dDIRECTCTL_KEY_MAX_COUNT+2) + 8];
				tCrc.D16[1] = FlashPageReadData.D16[(pNum-10) * (dDIRECTCTL_KEY_MAX_COUNT+2) + 9];
				DirectCtlBoard[pNum].CrcArea = tCrc.D32;
				if(DirectCtlBoard[pNum].CrcArea == Crc32GetResult()){
					if(DirectCtlBoard[pNum].CrcFlag.Area){
						DirectCtlBoard[pNum].CrcFlag.Area =0;
						CtlCenter.ieAddBrdFaultCancel(pNum+dADDR_DIRECTPAD_START,TBoardFaultType::StorageEpArea);
					}
				}else{
					DirectCtlBoard[pNum].CrcFlag.Area =1;
					CtlCenter.ieAddBrdFault(pNum+dADDR_DIRECTPAD_START,TBoardFaultType::StorageEpArea);
				}
			}
		}
	}
}

int TRecord::WriteLastMaintainTime()
{
	unsigned int aAddr, tVal;
	//dEepAddrMaintainLast	//Year Month Day
	aAddr = dEepAddrMaintainLast;
	LastMaintain.Cent = 0x20;
	tVal = LastMaintain.FullDate;
	DelayMs(20);
	eeprom_write_byte_via_page(0, aAddr, &LastMaintain.D8[0], 16);		
	DelayMs(20);
	
	ReadLastMaintainTime();
	if(tVal != LastMaintain.FullDate)return -1;
	return 0;
}

void TRecord::ReadLastMaintainTime()
{
	unsigned int aAddr;
	//dEepAddrMaintainLast	//Day  Month  Year
	aAddr = dEepAddrMaintainLast;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, &LastMaintain.D8[0], 4);
	
	if( (LastMaintain.Day == 0) || (LastMaintain.Day >0x31) ) LastMaintain.Day = 1;
	if( (LastMaintain.Month == 0) || (LastMaintain.Month >0x12) ) LastMaintain.Month = 1;
	if( LastMaintain.Year >0x99 ) LastMaintain.Year = 0;
	LastMaintain.Cent = 0x20;
}

void TRecord::Task1000Ms()
{
	static unsigned int aTick;
	if(aTick < 60){
		aTick++;
	}else{
		aTick = 0;
		SaveNewTime2Eep();
	}
}
			