#include "InterLink.h"
#include "Runtime.h"

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

void TInterLink::Init(void)
{
	int i;
	AllowOld = 0;
	LinkIndex = 0;
	LinkCount =0;
	for(i=0; i<dLINKAGE_MAX_COUNT; i++){
		PrcFireInx[i] =0;
		PrcFireOutputInx[i] =0;
		CrcCheckInx[i] =0;
		LinkHasStartTabel[i] = 0;
		LinkHasRdyGoTabel[i] = 0;
		LinkForceGoTabel[i] =0;
	}
	
	ForceInx =0;
	Forcing =0;
	for(int i=0; i<dLINKAGE_MAX_COUNT; i++ )CrcFlag[i] =0;
}

int TInterLink::CheckEpHwTypeCanOutput(unsigned char aType)
{
	if((aType==dgTYPE_OUT_MODULE) || (aType==dgTYPE_INOUT_MODULE_NonSource) || (aType==dgTYPE_INOUT_MODULE_Source) || (aType==dgType_VAALRAM)) return 1;
	else return 0;
}

void TInterLink::ResetByClearKey()
{
	int i;
	for(i=0; i<dLINKAGE_MAX_COUNT; i++){
		PrcFireInx[i] =0;
		PrcFireOutputInx[i] =0;
		CrcCheckInx[i] =0;
		LinkHasRdyGoTabel[i] = 0;
		LinkHasStartTabel[i] = 0;
		LinkForceGoTabel[i] =0;
	}	
	
	ForceInx =0;
	Forcing =0;
}

void TInterLink::ClearState()
{
	eInput.IsMask = 0;
}

void TInterLink::ClearRuntime()
{

}

void TInterLink::StartAnaly(char *p)
{

}

unsigned int TInterLink::GetNwLinkInput(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	return ProtoC2C.GetNwFire(aCtlNum, aP0, aP1);
}

void TInterLink::SendOutputBuf()
{
	TCanHeaderID aIde;
	int aInx;
	unsigned char Path0, Path1;
	if(oBuf.ReadIndex != oBuf.WriteIndex){
		aInx = oBuf.ReadIndex;
		aInx++;
		if(aInx >= dLinkageOutputBufMaxCOunt)aInx = 0;
		if(oBuf.Body[aInx].Spl.Action == dLNKSTART){
			oBuf.Body[aInx].Spl.CtlNum = MainCtl.fData.Split.MyNum;
			Path0 = oBuf.Body[aInx].Spl.P0;
			Path1 = oBuf.Body[aInx].Spl.P1;
			if( (Path0) && (Path0 <= dPORT_MAX_COUNT) && (Path1) ){
				Path0--; Path1--;
				
				dType = Port[Path0].dTypeTable[Path1];
				Port[Path0].StateTable[Path1] &= ~dEpStateBitDelayStart;;
		
				aIm.Splite.Year = DateTime.Date.Split.Year;
				aIm.Splite.Month = DateTime.Date.Split.Month;
				aIm.Splite.Day = DateTime.Date.Split.Day;
				aIm.Splite.Hour = DateTime.Time.Split.Hour;
				aIm.Splite.Minute = DateTime.Time.Split.Minute;
				aIm.Splite.Second = DateTime.Time.Split.Second;
				aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum;;
				aIm.Splite.P0 = oBuf.Body[aInx].Spl.P0;
				aIm.Splite.P1 = oBuf.Body[aInx].Spl.P1;
				aIm.Splite.P2 = 0;

				aIm.Splite.mType = etLinkageStart;
				aIm.Splite.dType = dType;
				aIm.Splite.iByte0 = 0x01;
				aIm.Splite.Data1 = 0;

				//Record.LinkageAdd(aIm);
				//CtlCenter.ieManualPushOneMsgLinkageStart(aIm);
				
				Data.D8[0] = 0;
				Data.D8[1] = MainCtl.fData.Split.MyNum;
				Data.D8[2] = oBuf.Body[aInx].Spl.P0;
				Data.D8[3] = oBuf.Body[aInx].Spl.P1;
				Data.D8[4] = 0;
				Data.D8[5] = dType;
				Data.D8[6] = 0;
				
				aIde.BitF.Cmd = 18;
				aIde.BitF.Pri = 1;
				aIde.BitF.sLvl = 1;
				aIde.BitF.dLvl = 7;
				aIde.BitF.sAddr = 0;
				aIde.BitF.dAddr = 127;
				
				InnerCan.CmdSendCustom(aIde, Data.D8, 6);
				//if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.SlaveDoSend2MasterMachine(aIm, 18);
			}else
			if(  (Path0 >= dADDR_DIRECTPAD_START) && ( Path0 <= dADDR_DIRECTPAD_END) && (Path1)){
				Path1--;
				if(Path1 < dDIRECTCTL_KEY_MAX_COUNT){
					Path0 = Path0 - dADDR_DIRECTPAD_START;
					if(DirectCtlBoard[Path0].IsRegisted[Path1]){
						aIm.Splite.Year = DateTime.Date.Split.Year;
						aIm.Splite.Month = DateTime.Date.Split.Month;
						aIm.Splite.Day = DateTime.Date.Split.Day;
						aIm.Splite.Hour = DateTime.Time.Split.Hour;
						aIm.Splite.Minute = DateTime.Time.Split.Minute;
						aIm.Splite.Second = DateTime.Time.Split.Second;
						aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum;;
						aIm.Splite.P0 = oBuf.Body[aInx].Spl.P0;
						aIm.Splite.P1 = oBuf.Body[aInx].Spl.P1;
						aIm.Splite.P2 = 0;

						aIm.Splite.mType = etLinkageStart;
						aIm.Splite.iByte0 = 0x01;
						aIm.Splite.Data1 = 0;

						//Record.LinkageAdd(aIm);
						//CtlCenter.ieManualPushOneMsgLinkageStart(aIm);
						
						Data.D8[0] = 0;
						Data.D8[1] = MainCtl.fData.Split.MyNum;
						Data.D8[2] = oBuf.Body[aInx].Spl.P0;
						Data.D8[3] = oBuf.Body[aInx].Spl.P1;
						Data.D8[4] = 2;
						Data.D8[5] = 0;
						
						aIde.BitF.Cmd = cmDirectCtlPadLinkageState;
						aIde.BitF.Pri = 1;
						aIde.BitF.sLvl = 1;
						aIde.BitF.dLvl = 7;
						aIde.BitF.sAddr = 0;
						aIde.BitF.dAddr = 127;
						
						InnerCan.CmdSendCustom(aIde, Data.D8, 6);
						
						Data.D8[4] = 0;
						Data.D8[5] = 1;
						//if(MainCtl.fData.Split.NetworkMode == 0)ProtoC2C.SlaveDoSend2MasterMachine(aIm, 18);
					}
				}
			}
			oBuf.Body[aInx].Spl.Action = dLNKNONE;
		}else
		if(oBuf.Body[aInx].Spl.Action == dLNKSTOP){
			oBuf.Body[aInx].Spl.CtlNum = MainCtl.fData.Split.MyNum;
			Path0 = oBuf.Body[aInx].Spl.P0;
			Path1 = oBuf.Body[aInx].Spl.P1;
			if( (Path0) && (Path0 <= dPORT_MAX_COUNT) && (Path1) ){
				Path0--; Path1--;
				
				dType = Port[Path0].dTypeTable[Path1];
				Port[Path0].StateTable[Path1] &= ~dEpStateBitDelayStart;;
		
				aIm.Splite.Year = DateTime.Date.Split.Year;
				aIm.Splite.Month = DateTime.Date.Split.Month;
				aIm.Splite.Day = DateTime.Date.Split.Day;
				aIm.Splite.Hour = DateTime.Time.Split.Hour;
				aIm.Splite.Minute = DateTime.Time.Split.Minute;
				aIm.Splite.Second = DateTime.Time.Split.Second;
				aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum;;
				aIm.Splite.P0 = oBuf.Body[aInx].Spl.P0;
				aIm.Splite.P1 = oBuf.Body[aInx].Spl.P1;
				aIm.Splite.P2 = 0;

				aIm.Splite.mType = etLinkageStart;
				aIm.Splite.dType = dType;
				aIm.Splite.iByte0 = 0x01;
				aIm.Splite.Data1 = 0;

				//Record.LinkageAdd(aIm);
				//CtlCenter.ieManualPushOneMsgLinkageStart(aIm);
				
				Data.D8[0] = 0;
				Data.D8[1] = MainCtl.fData.Split.MyNum;
				Data.D8[2] = oBuf.Body[aInx].Spl.P0;
				Data.D8[3] = oBuf.Body[aInx].Spl.P1;
				Data.D8[4] = 0;
				Data.D8[5] = dType;
				Data.D8[6] = 0;
				
				aIde.BitF.Cmd = cmAutoStop;
				aIde.BitF.Pri = 1;
				aIde.BitF.sLvl = 1;
				aIde.BitF.dLvl = 7;
				aIde.BitF.sAddr = 0;
				aIde.BitF.dAddr = 127;
				
				InnerCan.CmdSendCustom(aIde, Data.D8, 6);
				//if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.SlaveDoSend2MasterMachine(aIm, 18);
			}else
			if(  (Path0 >= dADDR_DIRECTPAD_START) && ( Path0 <= dADDR_DIRECTPAD_END) && (Path1)){
				Path1--;
				if(Path1 < dDIRECTCTL_KEY_MAX_COUNT){
					Path0 = Path0 - dADDR_DIRECTPAD_START;
					if(DirectCtlBoard[Path0].IsRegisted[Path1]){
						aIm.Splite.Year = DateTime.Date.Split.Year;
						aIm.Splite.Month = DateTime.Date.Split.Month;
						aIm.Splite.Day = DateTime.Date.Split.Day;
						aIm.Splite.Hour = DateTime.Time.Split.Hour;
						aIm.Splite.Minute = DateTime.Time.Split.Minute;
						aIm.Splite.Second = DateTime.Time.Split.Second;
						aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum;;
						aIm.Splite.P0 = oBuf.Body[aInx].Spl.P0;
						aIm.Splite.P1 = oBuf.Body[aInx].Spl.P1;
						aIm.Splite.P2 = 0;

						aIm.Splite.mType = etLinkageStart;
						aIm.Splite.iByte0 = 0x01;
						aIm.Splite.Data1 = 0;

						//Record.LinkageAdd(aIm);
						//CtlCenter.ieManualPushOneMsgLinkageStart(aIm);
						
						Data.D8[0] = 0;
						Data.D8[1] = MainCtl.fData.Split.MyNum;
						Data.D8[2] = oBuf.Body[aInx].Spl.P0;
						Data.D8[3] = oBuf.Body[aInx].Spl.P1;
						Data.D8[4] = 3;
						Data.D8[5] = 0;
						
						aIde.BitF.Cmd = cmDirectCtlPadLinkageState;
						aIde.BitF.Pri = 1;
						aIde.BitF.sLvl = 1;
						aIde.BitF.dLvl = 7;
						aIde.BitF.sAddr = 0;
						aIde.BitF.dAddr = 127;
						
						InnerCan.CmdSendCustom(aIde, Data.D8, 6);
						
						Data.D8[4] = 0;
						Data.D8[5] = 1;
						//if(MainCtl.fData.Split.NetworkMode == 0)ProtoC2C.SlaveDoSend2MasterMachine(aIm, 18);
					}
				}
			}
			oBuf.Body[aInx].Spl.Action = dLNKNONE;
		}else
		if( (oBuf.Body[aInx].Spl.Action == dLNKMATCH) || (oBuf.Body[aInx].Spl.Action == dLNKDISMATCH) ){
			oBuf.Body[aInx].Spl.CtlNum = MainCtl.fData.Split.MyNum;
			Path0 = oBuf.Body[aInx].Spl.P0;
			Path1 = oBuf.Body[aInx].Spl.P1;
			if(  (Path0 >= dADDR_DIRECTPAD_START) && ( Path0 <= dADDR_DIRECTPAD_END) && (Path1)){
				Path1-=1;
				if(Path1 < dDIRECTCTL_KEY_MAX_COUNT){
					Path0 = Path0 - dADDR_DIRECTPAD_START;
					if(DirectCtlBoard[Path0].IsRegisted[Path1]){
						Data.D8[0] = 0;
						Data.D8[1] = MainCtl.fData.Split.MyNum;
						Data.D8[2] = oBuf.Body[aInx].Spl.P0;
						Data.D8[3] = oBuf.Body[aInx].Spl.P1;
						if(oBuf.Body[aInx].Spl.Action == dLNKMATCH)
							Data.D8[4] = 1;
						else
							Data.D8[4] = 0;
						Data.D8[5] = 0;
						
						aIde.BitF.Cmd = cmDirectCtlPadLinkageState;
						aIde.BitF.Pri = 1;
						aIde.BitF.sLvl = 1;
						aIde.BitF.dLvl = 7;
						aIde.BitF.sAddr = 0;
						aIde.BitF.dAddr = 127;
						
						InnerCan.CmdSendCustom(aIde, Data.D8, 6);
					}
				}
			}
			oBuf.Body[aInx].Spl.Action = dLNKNONE;
		}else{
			//aIde.BitF.Cmd = 19;
			//oBuf.Body[aInx].Spl.CtlNum = MainCtl.fData.Split.MyNum;
			//oBuf.Body[aInx].Spl.Action = dLNKNONE;
			//Path0 = oBuf.Body[aInx].Spl.P0;
			//Path1 = oBuf.Body[aInx].Spl.P1;
			//Path0--; Path1--;
			//if(Path0 < dPORT_MAX_COUNT){
			//	dType = Port[Path0].dTypeTable[Path1];
			//	RtManualPushStartMsg(dType,1,oBuf.Body[aInx].D32);
			//	Record.OperateAdd_ManualStartCancel(oBuf.Body[aInx].D32, etLinkageStartCancel, 0x01);
			//}else{
			//	return;
			//}
			oBuf.Body[aInx].Spl.Action = dLNKNONE;
		}
			
		oBuf.ReadIndex = aInx;
	}	
}


void TInterLink::WaitChipIdle(unsigned int aCnt)
{
	volatile unsigned int aWait, Sta;
	do{
		for(aWait=0; aWait<aCnt;aWait++);
		Sta = nor_get_busy_state(dFlashLinkageExpChipX);
	}while(Sta);
}

void TInterLink::LoadExpCount()
{
	unsigned int fAddr4Index;
	int i, ava;;
	LinkCount = 0;
	WaitChipIdle(100000);
	for(i=0; i<dLINKAGE_MAX_COUNT; i++){
		WaitChipIdle(1000);
		fAddr4Index = dFlashAddrLinkageExp + (i * 1024);
		nor_read_byte(dFlashLinkageExpChipX, fAddr4Index, ReadWriteData.D8, 4);
		ava = 0;
		if( (ReadWriteData.D8[0] == 'T') && (ReadWriteData.D8[1] == 'R') && (ReadWriteData.D8[2] == 'U') && (ReadWriteData.D8[3] == 'E') ){
			ava = 1;
			LinkNumList[LinkCount] = i;
			LinkCount++;
			//if(ReadWriteData.D8[4] == 'M') then this Exp was Masked
		}else{
			
		}	
	}
}

void TInterLink::ReScanExpViaRam()
{
	unsigned int i,x;
	char Cz;
	unsigned int SysLinkAddr;
	TDMix Idf;
	LinkCount = 0;
	for(i=0; i<dLINKAGE_MAX_COUNT; i++){
		SysLinkAddr = dSdAddrLinkageExp + (i*1024);
		Idf.D32 = *(volatile unsigned int *)(SysLinkAddr);
		if(  (Idf.D8[0] == 'T') && (Idf.D8[1] == 'R') && (Idf.D8[2] == 'U') && (Idf.D8[3] == 'E') ){
			LinkNumList[LinkCount] = i;
			LinkCount++;
		}
	}	
}

void TInterLink::CheckRamCrc(unsigned short aInx)
{
	unsigned int x;
	unsigned int rAddr4Index;
	unsigned int i, ord, ava;
	unsigned int Pst;
	unsigned char tInx;
	unsigned int tCrc;
	
	if(aInx==0)return;
	if(aInx>dLinkageOutputBufMaxCOunt)return;
	
	i=aInx-1;
	Crc32Reset();
	SysLoopTick = 0;
	rAddr4Index = dSdAddrLinkageExp + (i*1024);
	for(ord=0; ord<4; ord++){
		if(ord<3)Pst=64; else Pst=63;
		for(x=0;x<Pst;x++)Crc32Feed(*(volatile unsigned int *)(rAddr4Index + x *4));
		if(ord==3)this->Crc[i]=*(volatile unsigned int *)(rAddr4Index + 63 *4);
		rAddr4Index += 256;
	}
	tCrc=Crc32GetResult();
	if(tCrc == this->Crc[i]){
		if(CrcFlag[i]){
			CrcFlag[i] =0;
			CtlCenter.ieAddBrdFaultCancelLinkageStorage(aInx);
		}
	}else{
		CrcFlag[i] =1;
		CtlCenter.ieAddBrdFaultLinkageStorage(aInx);
	}
}

void TInterLink::CheckStorageCrc(unsigned short aInx)
{
	unsigned int x;
	unsigned int fAddr4Index;
	unsigned int i, ord, ava;
	unsigned int Pst;
	unsigned char tInx;
	unsigned int tCrc;
	
	if(aInx==0)return;
	if(aInx>dLinkageOutputBufMaxCOunt)return;
	
	i=aInx-1;
	Crc32Reset();
	SysLoopTick = 0;
	fAddr4Index = dFlashAddrLinkageExp + (i * 1024);
	for(ord=0; ord<4; ord++){
		WaitChipIdle(1000);
		nor_read_page(dFlashLinkageExpChipX, fAddr4Index, ReadWriteData.D8);
		if(ord<3)Pst=64; else Pst=63;
		for(x=0;x<Pst;x++)Crc32Feed(ReadWriteData.D32[x]);
		if(ord==3)this->Crc[i]=ReadWriteData.D32[63];
		fAddr4Index += 256;
	}
	tCrc=Crc32GetResult();
	if(tCrc == this->Crc[i]){
		if(this->CrcFlag[i]){
			this->CrcFlag[i] =0;
			CtlCenter.ieAddBrdFaultCancelLinkageStorage(aInx);
		}
	}else{
		this->CrcFlag[i] =1;
		CtlCenter.ieAddBrdFaultLinkageStorage(aInx);
	}
}

void TInterLink::CheckStorageCrc()
{
	WaitChipIdle(100000);
	for(int i=0; i<dLINKAGE_MAX_COUNT; i++){
		CheckStorageCrc(i+1);
	}	
}

int TInterLink::RuntimeReloadExpression2Ram(unsigned int aInx)
{
	unsigned int i,x;
	volatile unsigned int fAddr4Index;
	volatile unsigned int SysLinkAddr;
	TDMix Idf;
	
	fAddr4Index = dFlashAddrLinkageExp + (aInx * 1024);
	SysLinkAddr = dSdAddrLinkageExp + (aInx * 1024);
	WaitChipIdle(100000);
	//read flash 2 Sdram
	for(i=0; i<4; i++){
		nor_read_page(dFlashLinkageExpChipX, fAddr4Index, ReadWriteData.D8);
		for(x=0; x<64; x++){
			*(volatile unsigned int *)(SysLinkAddr 	+ x * 4) = ReadWriteData.D32[x];
		}
		fAddr4Index += 256;
		SysLinkAddr += 256;
		WaitChipIdle(1000);
	}
	//crc
	Crc32Reset();
	SysLinkAddr = dSdAddrLinkageExp + (aInx * 1024);
	for(i=0; i<255; i++){
		Exp.D32[i] = *(volatile unsigned int *)(SysLinkAddr + (i * 4));
		Crc32Feed(Exp.D32[i]);
	}
	this->Crc[aInx] = ReadWriteData.D32[63];
	if(this->Crc[aInx] == Crc32GetResult()){
		if(this->CrcFlag[aInx]){
			this->CrcFlag[aInx] =0;
			CtlCenter.ieAddBrdFaultCancelLinkageStorage(aInx+1);
		}
	}else{
		this->CrcFlag[aInx] =1;
		CtlCenter.ieAddBrdFaultLinkageStorage(aInx+1);
		return -2;
	}
	//check ava
	Idf.D32 = Exp.D32[0];
	if(  (Idf.D8[0] != 'T') or (Idf.D8[1] != 'R') or (Idf.D8[2] != 'U') or (Idf.D8[3] != 'E') ){
		return -1;
	}
	return 0;
}

void TInterLink::LoadExpression2Ram()
{
	unsigned int x,aInx;
	unsigned int fAddr4Index;
	unsigned int i, ord, ava;
	unsigned int SysLinkAddr;
	unsigned int Pst;
	unsigned char tInx;

	SysLoopTick = 0;
	LoadExpCount();
	WaitChipIdle(10000);
	for(i=0; i<dLINKAGE_MAX_COUNT; i++){
		SysLinkAddr = dSdAddrLinkageExp + (i*1024);
		*(volatile unsigned int *)(SysLinkAddr) = 0;
	}
	
	for(i=0; i<LinkCount; i++){
		aInx = InterLink.LinkNumList[i];
		if(aInx < dLINKAGE_MAX_COUNT){
			SysLoopTick = 0;
			fAddr4Index = dFlashAddrLinkageExp + (aInx * 1024);
			SysLinkAddr = dSdAddrLinkageExp + (aInx*1024);
			WaitChipIdle(1000);
			nor_read_page(dFlashLinkageExpChipX, fAddr4Index, ReadWriteData.D8);
			ava = 0;
			if(  (ReadWriteData.D8[0] == 'T') && (ReadWriteData.D8[1] == 'R') && (ReadWriteData.D8[2] == 'U') && (ReadWriteData.D8[3] == 'E')  ){
				ava = 1;
				for(x=0; x<64; x++){
					*(volatile unsigned int *)(SysLinkAddr + 	x * 4) = ReadWriteData.D32[x];
				}
			}else{
				ReadWriteData.D8[0] = '\0';
				*(volatile unsigned int *)(SysLinkAddr) = 0;
			}
			
			Crc32Reset();
			for(x=0;x<64;x++)Crc32Feed(ReadWriteData.D32[x]);
			if(ava){
				for(ord=0; ord<3; ord++){
					fAddr4Index += 256;
					SysLinkAddr += 256;
					WaitChipIdle(1000);
					nor_read_page(dFlashLinkageExpChipX, fAddr4Index, ReadWriteData.D8);
					for(x=0; x<64; x++){
						*(volatile unsigned int *)(SysLinkAddr + 	x * 4) = ReadWriteData.D32[x];
					}
					if(ord<2)Pst=64; else Pst=63;
					for(x=0;x<Pst;x++)Crc32Feed(ReadWriteData.D32[x]);
					if(ord==2)this->Crc[aInx]=ReadWriteData.D32[63];
					
				}
				if(this->Crc[aInx] == Crc32GetResult()){
					if(this->CrcFlag[aInx]){
						this->CrcFlag[aInx] =0;
						CtlCenter.ieAddBrdFaultCancelLinkageStorage(aInx+1);
					}
				}else{
					this->CrcFlag[aInx] =1;
					CtlCenter.ieAddBrdFaultLinkageStorage(aInx+1);
				}
			}
			
			
		}
	}
}

void TInterLink::ForceLoadLinkageExp()
{
	/*int i, Ava;
	unsigned int ActualIndex;
	volatile unsigned int aAddr;
	
	if(ForceInx < LinkCount){
		ActualIndex = LinkNumList[ForceInx];
		aAddr = dSdAddrLinkageExp + (ActualIndex * 1024);
		for(i=0; i<256; i++){
			Exp.D32[i] = *(volatile unsigned int *)(aAddr + (i * 4));
		}
		LinkCurrent = ActualIndex;
	}*/
}

int TInterLink::GetFireStateViaPath(unsigned int aCode, unsigned char aDType)
{
	unsigned int i, x, HadPrc, Result;
	
	Result = 0;
	HadPrc = 0;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		if(  (aCode >= Port[i].UserCodeMin) && (aCode <= Port[i].UserCodeMax)  ){
			for(x=0; x<256; x++){
				if(Port[i].UcList[x].Full == aCode){
					if(Port[i].dTypeTable[x] == aDType){
						if(Port[i].StateTable[x] & dEpStateBitFire){
							return 1;
						}
					}						
					HadPrc = 1;
					break;
				}
			}
		}
		if(HadPrc)break;
	}
	for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
		for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
			if(Port[i].UcList[x].Full == aCode){
				if(Port[i].dTypeTable[x] == aDType){
					if(Port[i].StateTable[x] & dEpStateBitFire){
						return 1;
					}
				}						
				HadPrc = 1;
				break;
			}
		}
		if(HadPrc)break;
	}
	return 0;
}

int TInterLink::GetFireStateViaMsg(TUserCodeStr aCode, unsigned char aDType, unsigned int aEty)
{
	unsigned int x, ActCnt, tType, tMatch;
	int i;
	TUserCodeStr MaskVal;
	ActCnt = 0;
	tMatch =0;
	if(CtlCenter.FiredUserCode.Count){
		for(i=CtlCenter.FiredUserCode.Count-1; i>=0; i--){
			if(CtlCenter.FiredUserCode.uCode[i] == 0) continue;
			if(RunState != TRunModeCfg::RunModeAdjust){
				if(CtlCenter.FiredUserCode.eType[i] > TControlCenter::LinkInputTypeAvaEdge)continue;
			}
			if(CtlCenter.FiredUserCode.AssignType[i]) tType = CtlCenter.FiredUserCode.AssignType[i];
			else tType = CtlCenter.FiredUserCode.dType[i];
			if(aDType == tType){
				if(eInput.Asterisk[0])MaskVal.Spl.BcdText_0 = 10; else MaskVal.Spl.BcdText_0 = CtlCenter.FiredUserCode.uCode[i] / 10000000 % 10; 
				if(eInput.Asterisk[1])MaskVal.Spl.BcdText_1 = 10; else MaskVal.Spl.BcdText_1 = CtlCenter.FiredUserCode.uCode[i] / 1000000 % 10;
				if(eInput.Asterisk[2])MaskVal.Spl.BcdText_2 = 10; else MaskVal.Spl.BcdText_2 = CtlCenter.FiredUserCode.uCode[i] / 100000 % 10;
				if(eInput.Asterisk[3])MaskVal.Spl.BcdText_3 = 10; else MaskVal.Spl.BcdText_3 = CtlCenter.FiredUserCode.uCode[i] / 10000 % 10;
				if(eInput.Asterisk[4])MaskVal.Spl.BcdText_4 = 10; else MaskVal.Spl.BcdText_4 = CtlCenter.FiredUserCode.uCode[i] / 1000 % 10;
				if(eInput.Asterisk[5])MaskVal.Spl.BcdText_5 = 10; else MaskVal.Spl.BcdText_5 = CtlCenter.FiredUserCode.uCode[i] / 100 % 10;
				if(eInput.Asterisk[6])MaskVal.Spl.BcdText_6 = 10; else MaskVal.Spl.BcdText_6 = CtlCenter.FiredUserCode.uCode[i] / 10 % 10;
				if(eInput.Asterisk[7])MaskVal.Spl.BcdText_7 = 10; else MaskVal.Spl.BcdText_7 = CtlCenter.FiredUserCode.uCode[i] / 1 % 10;
				if(aCode.Full == MaskVal.Full){
					ActCnt ++;
					if(i>=FireInxPrv)tMatch =1;
					if(ActCnt >= eInput.Entry[aEty].ThresholdCnt){
						eInput.Entry[aEty].Active = 1;
						eInput.HasInput = 1;
						if(tMatch)NewMatchFound =1;
						return 1;
					}
				}
			}
		}
	}
	return 0;
}

void TInterLink::GetFireStateByTypeViaMsg(unsigned char aDType, unsigned int aEty)
{
	unsigned int x, ActCnt, tMatch;
	int i;
	tMatch =0;
	ActCnt = 0;
	if(CtlCenter.FiredUserCode.Count){
		for(i=CtlCenter.FiredUserCode.Count-1; i>=0; i--){
			if(CtlCenter.FiredUserCode.uCode[i] == 0) continue;
			if(RunState != TRunModeCfg::RunModeAdjust){
				if(CtlCenter.FiredUserCode.eType[i] > TControlCenter::LinkInputTypeAvaEdge)continue;
			}
			
			if(aDType == 0){
				ActCnt ++;
				if(i>=FireInxPrv)tMatch =1;
				if(ActCnt >= eInput.Entry[aEty].ThresholdCnt){
					eInput.Entry[aEty].Active = 1;
					eInput.HasInput = 1;
					if(tMatch)NewMatchFound =1;
					return;
				}
			}else{
				if(aDType == CtlCenter.FiredUserCode.dType[i]){
					ActCnt ++;
					if(i>=FireInxPrv)tMatch =1;
					if(ActCnt >= eInput.Entry[aEty].ThresholdCnt){
						eInput.Entry[aEty].Active = 1;
						eInput.HasInput = 1;
						if(tMatch)NewMatchFound =1;
						return;
					}
				}
			}
		}
	}		
}

void TInterLink::CanNetMasterSendOutput(TUserCodeStr aCode, unsigned char aDType, unsigned int aDelaySec, unsigned int aDoStop)
{
	unsigned char aSta;
	if(aDoStop == 0){
		if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
			if(aDelaySec){
				aSta = 3;	//Delay Start
			}else{
				aSta = 2;	//Start
			}
		}else{
			aSta = 1;	//Match
		}
		ProtoC2C.MasterDoRemoteLnkStartStopViaUserCode(aCode.Full, aDType, aDelaySec, aSta);
	}else{
		aSta = 0;	//Stop
		ProtoC2C.MasterDoRemoteLnkStartStopViaUserCode(aCode.Full, aDType, aDelaySec, aSta);
	}		
}

void TInterLink::CanNetMasterSendOutputSimulate(TUserCodeStr aCode, unsigned char aDType, unsigned int aDelaySec, unsigned int aDoStop)
{
	unsigned char aSta;
	if(aDoStop == 0){
		if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
			if(aDelaySec){
				aSta = 3;	//Delay Start
			}else{
				aSta = 2;	//Start
			}
		}else{
			aSta = 1;	//Match
		}
		ProtoC2C.MasterDoRemoteLnkStartStopViaUserCodeSimulate(aCode.Full, aDType, aDelaySec, aSta);
	}else{
		aSta = 0;	//Stop
		ProtoC2C.MasterDoRemoteLnkStartStopViaUserCodeSimulate(aCode.Full, aDType, aDelaySec, aSta);
	}		
}

int TInterLink::SetOutputStateViaUserCode(TUserCodeStr aCode, unsigned char aDType, unsigned int aEty, unsigned int aDelaySec, unsigned int aDoStop)
{
	unsigned int i, x, HadPrc, Result, HasMatch, tType;
	TUserCodeStr DevUCode;
	
	Result = 0;
	HadPrc = 0;
	HasMatch = 0;
	if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
		LinkHasStartTabel[LinkCurrent] =0xAA;
	}
	if(MainCtl.fData.Split.NetworkMode == 1){
		CanNetMasterSendOutput(aCode,aDType,aDelaySec,aDoStop);
	}
	if(aDoStop == 0){
			for(i=0; i<dPORT_MAX_COUNT; i++){
				if(MainCtl.fData.Split.ExistTablePort[i]){
					for(x=0; x<256; x++){
						if(  (Port[i].ExistTable[x]) && (Port[i].UcList[x].Full != 0) ){
							if(CheckEpHwTypeCanOutput(Port[i].dTypeTable[x]) == 0)continue;
							if(Port[i].AssignType[x] != 0) tType = Port[i].AssignType[x];
							else tType = Port[i].dTypeTable[x];	
							//if(tType == aDType){
							if( (tType == aDType) || (aDType == 0) ){
								if(eOutput.Asterisk[0])DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = Port[i].UcList[x].Full / 10000000 % 10; 
								if(eOutput.Asterisk[1])DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = Port[i].UcList[x].Full / 1000000 % 10;
								if(eOutput.Asterisk[2])DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = Port[i].UcList[x].Full / 100000 % 10;
								if(eOutput.Asterisk[3])DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = Port[i].UcList[x].Full / 10000 % 10;
								if(eOutput.Asterisk[4])DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = Port[i].UcList[x].Full / 1000 % 10;
								if(eOutput.Asterisk[5])DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = Port[i].UcList[x].Full / 100 % 10;
								if(eOutput.Asterisk[6])DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = Port[i].UcList[x].Full / 10 % 10;
								if(eOutput.Asterisk[7])DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = Port[i].UcList[x].Full / 1 % 10;
								if(DevUCode.Full == aCode.Full){
									if((Port[i].StateTable[x] & (dEpStateBitStart + dEpStateBitMask) ) == 0){
										if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
											//LinkHasStartTabel[LinkCurrent] =0xAA;
											if(aDelaySec){
												if(Port[i].DelayTick[x] == 0){
													Port[i].DelayToDoEventType[x] =0;
													Port[i].DelayTick[x] = aDelaySec +2;
													Port[i].AddRemoveMsgDelayStart(1, i+1, x+1, tType);
												}
												Port[i].LnkRdyGo[x] = 0xA1;
											}else{
												oBuf.WriteIndex++;
												if(oBuf.WriteIndex >= dLinkageOutputBufMaxCOunt)oBuf.WriteIndex = 0;
												oBuf.Body[oBuf.WriteIndex].Spl.P0 = i+1;
												oBuf.Body[oBuf.WriteIndex].Spl.P1 = x+1;
												oBuf.Body[oBuf.WriteIndex].Spl.Action = dLNKSTART;
											}
										}else{
											Port[i].LnkRdyGo[x] = 0xA1;
										}
									}
								}
							}
						}
					}
				}
			}
			for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
				if(DirectCtlBoard[i].PadIsProhibit) continue;
				for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
					if(DirectCtlBoard[i].IsRegisted[x] == 0)continue;
					if(DirectCtlBoard[i].UcList[x].Full == 0)continue;
					//if(DirectCtlBoard[i].AssignType[x] == aDType){
					if( (DirectCtlBoard[i].AssignType[x] == aDType) || (aDType == 0) ){
						if(eOutput.Asterisk[0])DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = DirectCtlBoard[i].UcList[x].Full / 10000000 % 10; 
						if(eOutput.Asterisk[1])DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = DirectCtlBoard[i].UcList[x].Full / 1000000 % 10;
						if(eOutput.Asterisk[2])DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = DirectCtlBoard[i].UcList[x].Full / 100000 % 10;
						if(eOutput.Asterisk[3])DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = DirectCtlBoard[i].UcList[x].Full / 10000 % 10;
						if(eOutput.Asterisk[4])DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = DirectCtlBoard[i].UcList[x].Full / 1000 % 10;
						if(eOutput.Asterisk[5])DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = DirectCtlBoard[i].UcList[x].Full / 100 % 10;
						if(eOutput.Asterisk[6])DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = DirectCtlBoard[i].UcList[x].Full / 10 % 10;
						if(eOutput.Asterisk[7])DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = DirectCtlBoard[i].UcList[x].Full / 1 % 10;
						if(aCode.Full == DevUCode.Full){
							if( DirectCtlBoard[i].IsRegisted[x]){
								//cmDirectCtlPadLinkageState	4	机器号	多线盘号	符合联动启动标志	符合联动停止标志
								//Send State to Direct Pad
								//cmDirectCtlPadLinkageState	4	机器号	多线盘号	符合联动启动标志	符合联动停止标志
								
								if(DirectCtlBoard[i].OutputState[x] == 0){
									if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
										//LinkHasStartTabel[LinkCurrent] =0xAA;
										if(aDelaySec){
											if(DirectCtlBoard[i].DelayTick[x] == 0){
												DirectCtlBoard[i].DelayToDoEventType[x] =0;
												DirectCtlBoard[i].DelayTick[x] = aDelaySec +2;
												DirectCtlBoard[i].AddRemoveMsgDelayStart(1, i +dADDR_DIRECTPAD_START, x +1);
												
												DirectCtlBoard[i].LnkRdyGo[x] = 0xA1;
												if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
													DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
													oBuf.WriteIndex++;
													if(oBuf.WriteIndex >= dLinkageOutputBufMaxCOunt)oBuf.WriteIndex = 0;
													oBuf.Body[oBuf.WriteIndex].Spl.P0 = i +dADDR_DIRECTPAD_START;
													oBuf.Body[oBuf.WriteIndex].Spl.P1 = x +1;
													oBuf.Body[oBuf.WriteIndex].Spl.Action = dLNKMATCH;
													HasMatch = 1;
												}
											}
										}else{
											oBuf.WriteIndex++;
											if(oBuf.WriteIndex >= dLinkageOutputBufMaxCOunt)oBuf.WriteIndex = 0;
											oBuf.Body[oBuf.WriteIndex].Spl.P0 = i +dADDR_DIRECTPAD_START;
											oBuf.Body[oBuf.WriteIndex].Spl.P1 = x +1;
											oBuf.Body[oBuf.WriteIndex].Spl.Action = dLNKSTART;
										}
									}else{
										DirectCtlBoard[i].LnkRdyGo[x] = 0xA1;
									}
									if(HasMatch == 0){
										if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
											DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
											oBuf.WriteIndex++;
											if(oBuf.WriteIndex >= dLinkageOutputBufMaxCOunt)oBuf.WriteIndex = 0;
											oBuf.Body[oBuf.WriteIndex].Spl.P0 = i +dADDR_DIRECTPAD_START;
											oBuf.Body[oBuf.WriteIndex].Spl.P1 = x +1;
											oBuf.Body[oBuf.WriteIndex].Spl.Action = dLNKMATCH;
										}
									}
								}
							}
						}
					}
				}
			}
	}else{
			for(i=0; i<dPORT_MAX_COUNT; i++){
				if(MainCtl.fData.Split.ExistTablePort[i]){
					for(x=0; x<256; x++){
						if(  (Port[i].ExistTable[x]) && (Port[i].UcList[x].Full != 0) ){
							if(CheckEpHwTypeCanOutput(Port[i].dTypeTable[x]) == 0)continue;
							if(Port[i].AssignType[x] != 0) tType = Port[i].AssignType[x];
							else tType = Port[i].dTypeTable[x];	
							//if(tType == aDType){
							if( (tType == aDType) ||  (aDType == 0) ){
								if(eOutput.Asterisk[0])DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = Port[i].UcList[x].Full / 10000000 % 10; 
								if(eOutput.Asterisk[1])DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = Port[i].UcList[x].Full / 1000000 % 10;
								if(eOutput.Asterisk[2])DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = Port[i].UcList[x].Full / 100000 % 10;
								if(eOutput.Asterisk[3])DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = Port[i].UcList[x].Full / 10000 % 10;
								if(eOutput.Asterisk[4])DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = Port[i].UcList[x].Full / 1000 % 10;
								if(eOutput.Asterisk[5])DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = Port[i].UcList[x].Full / 100 % 10;
								if(eOutput.Asterisk[6])DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = Port[i].UcList[x].Full / 10 % 10;
								if(eOutput.Asterisk[7])DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = Port[i].UcList[x].Full / 1 % 10;
								if(DevUCode.Full == aCode.Full){
									if((Port[i].StateTable[x] & (dEpStateBitMask) ) == 0){
										if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
												//LinkHasStartTabel[LinkCurrent] =0xAA;
												oBuf.WriteIndex++;
												if(oBuf.WriteIndex >= dLinkageOutputBufMaxCOunt)oBuf.WriteIndex = 0;
												oBuf.Body[oBuf.WriteIndex].Spl.P0 = i+1;
												oBuf.Body[oBuf.WriteIndex].Spl.P1 = x+1;
												oBuf.Body[oBuf.WriteIndex].Spl.Action = dLNKSTOP;
										}else{
											//Port[i].LnkRdyGo[x] = 0;
										}
									}
								}
							}
						}
					}
				}
			}
									
			for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
				if(DirectCtlBoard[i].PadIsProhibit) continue;
				for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
					if(DirectCtlBoard[i].IsRegisted[x] == 0)continue;
					if(DirectCtlBoard[i].UcList[x].Full == 0)continue;
					//if(DirectCtlBoard[i].AssignType[x] == aDType){	
					if( (DirectCtlBoard[i].AssignType[x] == aDType) || (aDType == 0) ){
						if(eOutput.Asterisk[0])DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = DirectCtlBoard[i].UcList[x].Full / 10000000 % 10; 
						if(eOutput.Asterisk[1])DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = DirectCtlBoard[i].UcList[x].Full / 1000000 % 10;
						if(eOutput.Asterisk[2])DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = DirectCtlBoard[i].UcList[x].Full / 100000 % 10;
						if(eOutput.Asterisk[3])DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = DirectCtlBoard[i].UcList[x].Full / 10000 % 10;
						if(eOutput.Asterisk[4])DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = DirectCtlBoard[i].UcList[x].Full / 1000 % 10;
						if(eOutput.Asterisk[5])DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = DirectCtlBoard[i].UcList[x].Full / 100 % 10;
						if(eOutput.Asterisk[6])DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = DirectCtlBoard[i].UcList[x].Full / 10 % 10;
						if(eOutput.Asterisk[7])DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = DirectCtlBoard[i].UcList[x].Full / 1 % 10;
						if(aCode.Full == DevUCode.Full){
							if( DirectCtlBoard[i].IsRegisted[x]){
								//cmDirectCtlPadLinkageState	4	机器号	多线盘号	符合联动启动标志	符合联动停止标志
								//Send State to Direct Pad
								//cmDirectCtlPadLinkageState	4	机器号	多线盘号	符合联动启动标志	符合联动停止标志
								//if(DirectCtlBoard[i].OutputState[x] == 1){
									if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
											//LinkHasStartTabel[LinkCurrent] =0xAA;
											oBuf.WriteIndex++;
											if(oBuf.WriteIndex >= dLinkageOutputBufMaxCOunt)oBuf.WriteIndex = 0;
											oBuf.Body[oBuf.WriteIndex].Spl.P0 = i +dADDR_DIRECTPAD_START;
											oBuf.Body[oBuf.WriteIndex].Spl.P1 = x +1;
											oBuf.Body[oBuf.WriteIndex].Spl.Action = dLNKSTOP;
									}else{
										//DirectCtlBoard[i].LnkRdyGo[x] = 0;
									}
									//if(HasMatch == 0){
									//	if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
									//		DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
									//		oBuf.WriteIndex++;
									//		if(oBuf.WriteIndex >= dLinkageOutputBufMaxCOunt)oBuf.WriteIndex = 0;
									//		oBuf.Body[oBuf.WriteIndex].Spl.P0 = i +dADDR_DIRECTPAD_START;
									//		oBuf.Body[oBuf.WriteIndex].Spl.P1 = x +1;
									//		oBuf.Body[oBuf.WriteIndex].Spl.Action = dLNKDISMATCH;
									//	}
									//}
								//}
							}
						}
					}
				}
			}
	}
	return 0;
}

int TInterLink::SetOutputStateViaUserCodeSimulate(TUserCodeStr aCode, unsigned char aDType, unsigned int aEty, unsigned int aDelaySec, unsigned int aDoStop)
{
	unsigned int i, x, HadPrc, Result, HasMatch, tType, hType;
	TUserCodeStr DevUCode;
	
	Result = 0;
	HadPrc = 0;
	HasMatch = 0;
	if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
		LinkHasStartTabel[LinkCurrent] =0xAA;
	}
	if(MainCtl.fData.Split.NetworkMode == 1){
		CanNetMasterSendOutputSimulate(aCode,aDType,aDelaySec,aDoStop);
	}
	if(aDoStop == 0){
			for(i=0; i<dPORT_MAX_COUNT; i++){
				if(MainCtl.fData.Split.ExistTablePort[i]){
					for(x=0; x<256; x++){
						if(  (Port[i].ExistTable[x]) && (Port[i].UcList[x].Full != 0) ){
							if(CheckEpHwTypeCanOutput(Port[i].dTypeTable[x]) == 0)continue;
							if(Port[i].AssignType[x] != 0) tType = Port[i].AssignType[x];
							else tType = Port[i].dTypeTable[x];	
							//if(tType == aDType){
							if( (tType == aDType) || ( aDType == 0) ){
								if(eOutput.Asterisk[0])DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = Port[i].UcList[x].Full / 10000000 % 10; 
								if(eOutput.Asterisk[1])DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = Port[i].UcList[x].Full / 1000000 % 10;
								if(eOutput.Asterisk[2])DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = Port[i].UcList[x].Full / 100000 % 10;
								if(eOutput.Asterisk[3])DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = Port[i].UcList[x].Full / 10000 % 10;
								if(eOutput.Asterisk[4])DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = Port[i].UcList[x].Full / 1000 % 10;
								if(eOutput.Asterisk[5])DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = Port[i].UcList[x].Full / 100 % 10;
								if(eOutput.Asterisk[6])DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = Port[i].UcList[x].Full / 10 % 10;
								if(eOutput.Asterisk[7])DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = Port[i].UcList[x].Full / 1 % 10;
								if(DevUCode.Full == aCode.Full){
									if((Port[i].StateTable[x] & (dEpStateBitStart + dEpStateBitMask) ) == 0){
										if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
											//LinkHasStartTabel[LinkCurrent] =0xAA;
											if(aDelaySec){
												if(Port[i].DelayTick[x] == 0){
													Port[i].DelayToDoEventType[x] = 1;
													Port[i].DelayTick[x] = aDelaySec +2;
													Port[i].AddRemoveMsgDelayStartSimulate(1, i+1, x+1, tType);
												}
												//Port[i].LnkRdyGo[x] = 0xA1;
											}else{	
												aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
												aIm.Splite.P0 = i+1;
												aIm.Splite.P1 = x+1;
												aIm.Splite.P2 = 0;
												if(Port[i].AssignType[x] != 0){
													aIm.Splite.dType = Port[i].AssignType[x];
												}else{
													aIm.Splite.dType = Port[i].dTypeTable[x]; 
												}
												aIm.Splite.UserCode = Port[i].UcList[x].Full;
												TControlCenter::sSimStartGot(&aIm);
											}
										}else{
											//Port[i].LnkRdyGo[x] = 0xA1;
										}
									}
								}
							}
						}
					}
				}
			}
			for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
				if(DirectCtlBoard[i].PadIsProhibit) continue;
				for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
					if(DirectCtlBoard[i].IsRegisted[x] == 0)continue;
					if(DirectCtlBoard[i].UcList[x].Full == 0)continue;
					//if(DirectCtlBoard[i].AssignType[x] == aDType){
					if( (DirectCtlBoard[i].AssignType[x] == aDType) || (aDType == 0) ){
						if(eOutput.Asterisk[0])DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = DirectCtlBoard[i].UcList[x].Full / 10000000 % 10; 
						if(eOutput.Asterisk[1])DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = DirectCtlBoard[i].UcList[x].Full / 1000000 % 10;
						if(eOutput.Asterisk[2])DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = DirectCtlBoard[i].UcList[x].Full / 100000 % 10;
						if(eOutput.Asterisk[3])DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = DirectCtlBoard[i].UcList[x].Full / 10000 % 10;
						if(eOutput.Asterisk[4])DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = DirectCtlBoard[i].UcList[x].Full / 1000 % 10;
						if(eOutput.Asterisk[5])DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = DirectCtlBoard[i].UcList[x].Full / 100 % 10;
						if(eOutput.Asterisk[6])DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = DirectCtlBoard[i].UcList[x].Full / 10 % 10;
						if(eOutput.Asterisk[7])DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = DirectCtlBoard[i].UcList[x].Full / 1 % 10;
						if(aCode.Full == DevUCode.Full){
							if( DirectCtlBoard[i].IsRegisted[x] ){
								//cmDirectCtlPadLinkageState	4	机器号	多线盘号	符合联动启动标志	符合联动停止标志
								//Send State to Direct Pad
								//cmDirectCtlPadLinkageState	4	机器号	多线盘号	符合联动启动标志	符合联动停止标志
								
								if(DirectCtlBoard[i].OutputState[x] == 0){
									if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
										//LinkHasStartTabel[LinkCurrent] =0xAA;
										if(aDelaySec){
											if(DirectCtlBoard[i].DelayTick[x] == 0){
												DirectCtlBoard[i].DelayToDoEventType[x] =1;
												DirectCtlBoard[i].DelayTick[x] = aDelaySec +2;
												DirectCtlBoard[i].AddRemoveMsgDelayStartSimulate(1, i +dADDR_DIRECTPAD_START, x +1);
												
												//DirectCtlBoard[i].LnkRdyGo[x] = 0xA1;
												//if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
												//	DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
												//	oBuf.WriteIndex++;
												//	if(oBuf.WriteIndex >= dLinkageOutputBufMaxCOunt)oBuf.WriteIndex = 0;
												//	oBuf.Body[oBuf.WriteIndex].Spl.P0 = i +dADDR_DIRECTPAD_START;
												//	oBuf.Body[oBuf.WriteIndex].Spl.P1 = x +1;
												//	oBuf.Body[oBuf.WriteIndex].Spl.Action = dLNKMATCH;
												//	HasMatch = 1;
												//}
											}
										}else{
											DirectCtlBoard[i].AddStartStopSimulate(1, i+dADDR_DIRECTPAD_START, x+1);
										}
									}else{
										DirectCtlBoard[i].LnkRdyGo[x] = 0xA1;
									}
									//if(HasMatch == 0){
									//	if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
									//		DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
									//		oBuf.WriteIndex++;
									//		if(oBuf.WriteIndex >= dLinkageOutputBufMaxCOunt)oBuf.WriteIndex = 0;
									//		oBuf.Body[oBuf.WriteIndex].Spl.P0 = i +dADDR_DIRECTPAD_START;
									//		oBuf.Body[oBuf.WriteIndex].Spl.P1 = x +1;
									//		oBuf.Body[oBuf.WriteIndex].Spl.Action = dLNKMATCH;
									//	}
									//}
								}
							}
						}
					}
				}
			}
	}else{
			for(i=0; i<dPORT_MAX_COUNT; i++){
				if(MainCtl.fData.Split.ExistTablePort[i]){
					for(x=0; x<256; x++){
						if(  (Port[i].ExistTable[x]) && (Port[i].UcList[x].Full != 0) ){
							if(CheckEpHwTypeCanOutput(Port[i].dTypeTable[x]) == 0)continue;
							if(Port[i].AssignType[x] != 0) tType = Port[i].AssignType[x];
							else tType = Port[i].dTypeTable[x];	
							//if(tType == aDType){
							if( (tType == aDType) || (aDType == 0) ){
								if(eOutput.Asterisk[0])DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = Port[i].UcList[x].Full / 10000000 % 10; 
								if(eOutput.Asterisk[1])DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = Port[i].UcList[x].Full / 1000000 % 10;
								if(eOutput.Asterisk[2])DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = Port[i].UcList[x].Full / 100000 % 10;
								if(eOutput.Asterisk[3])DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = Port[i].UcList[x].Full / 10000 % 10;
								if(eOutput.Asterisk[4])DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = Port[i].UcList[x].Full / 1000 % 10;
								if(eOutput.Asterisk[5])DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = Port[i].UcList[x].Full / 100 % 10;
								if(eOutput.Asterisk[6])DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = Port[i].UcList[x].Full / 10 % 10;
								if(eOutput.Asterisk[7])DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = Port[i].UcList[x].Full / 1 % 10;
								if(DevUCode.Full == aCode.Full){
									if((Port[i].StateTable[x] & (dEpStateBitMask) ) == 0){
										if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
											//LinkHasStartTabel[LinkCurrent] =0xAA;
											aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum;
											aIm.Splite.P0 = i+1;
											aIm.Splite.P1 = x+1;
											aIm.Splite.P2 = 0;
											if(Port[i].AssignType[x] != 0){
												aIm.Splite.dType = Port[i].AssignType[x];
											}else{
												aIm.Splite.dType = Port[i].dTypeTable[x]; 
											}	
											aIm.Splite.UserCode = Port[i].UcList[x].Full;									
											TControlCenter::sSimStartCancelGot(&aIm);
										}else{
											//Port[i].LnkRdyGo[x] = 0;
										}
									}
								}
							}
						}
					}
				}
			}
									
			for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
				if(DirectCtlBoard[i].PadIsProhibit) continue;
				for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
					if(DirectCtlBoard[i].IsRegisted[x] == 0)continue;
					if(DirectCtlBoard[i].UcList[x].Full == 0)continue;
					//if(DirectCtlBoard[i].AssignType[x] == aDType){	
					if( (DirectCtlBoard[i].AssignType[x] == aDType) || (aDType == 0) ){
						if(eOutput.Asterisk[0])DevUCode.Spl.BcdText_0 = 10; else DevUCode.Spl.BcdText_0 = DirectCtlBoard[i].UcList[x].Full / 10000000 % 10; 
						if(eOutput.Asterisk[1])DevUCode.Spl.BcdText_1 = 10; else DevUCode.Spl.BcdText_1 = DirectCtlBoard[i].UcList[x].Full / 1000000 % 10;
						if(eOutput.Asterisk[2])DevUCode.Spl.BcdText_2 = 10; else DevUCode.Spl.BcdText_2 = DirectCtlBoard[i].UcList[x].Full / 100000 % 10;
						if(eOutput.Asterisk[3])DevUCode.Spl.BcdText_3 = 10; else DevUCode.Spl.BcdText_3 = DirectCtlBoard[i].UcList[x].Full / 10000 % 10;
						if(eOutput.Asterisk[4])DevUCode.Spl.BcdText_4 = 10; else DevUCode.Spl.BcdText_4 = DirectCtlBoard[i].UcList[x].Full / 1000 % 10;
						if(eOutput.Asterisk[5])DevUCode.Spl.BcdText_5 = 10; else DevUCode.Spl.BcdText_5 = DirectCtlBoard[i].UcList[x].Full / 100 % 10;
						if(eOutput.Asterisk[6])DevUCode.Spl.BcdText_6 = 10; else DevUCode.Spl.BcdText_6 = DirectCtlBoard[i].UcList[x].Full / 10 % 10;
						if(eOutput.Asterisk[7])DevUCode.Spl.BcdText_7 = 10; else DevUCode.Spl.BcdText_7 = DirectCtlBoard[i].UcList[x].Full / 1 % 10;
						if(aCode.Full == DevUCode.Full){
							if( DirectCtlBoard[i].IsRegisted[x] ){
								//cmDirectCtlPadLinkageState	4	机器号	多线盘号	符合联动启动标志	符合联动停止标志
								//Send State to Direct Pad
								//cmDirectCtlPadLinkageState	4	机器号	多线盘号	符合联动启动标志	符合联动停止标志
								if(DirectCtlBoard[i].OutputState[x] == 0){
									if( (ForceDoOutput) || (FirstDoOutput) || ( (NewMatchDoOutput) && (NewMatchFound) ) ){
										//LinkHasStartTabel[LinkCurrent] =0xAA;
										DirectCtlBoard[i].AddStartStopSimulate(0, i+dADDR_DIRECTPAD_START, x+1);
									}else{
										//DirectCtlBoard[i].LnkRdyGo[x] = 0;
									}
									//if(HasMatch == 0){
									//	if(DirectCtlBoard[i].LnkRdyGoOld[x] != DirectCtlBoard[i].LnkRdyGo[x]){
									//		DirectCtlBoard[i].LnkRdyGoOld[x] = DirectCtlBoard[i].LnkRdyGo[x];
									//		oBuf.WriteIndex++;
									//		if(oBuf.WriteIndex >= dLinkageOutputBufMaxCOunt)oBuf.WriteIndex = 0;
									//		oBuf.Body[oBuf.WriteIndex].Spl.P0 = i +dADDR_DIRECTPAD_START;
									//		oBuf.Body[oBuf.WriteIndex].Spl.P1 = x +1;
									//		oBuf.Body[oBuf.WriteIndex].Spl.Action = dLNKDISMATCH;
									//	}
									//}
								}
							}
						}
					}
				}
			}
	}
	return 0;
}

void TInterLink::LoadOneEntryInput(unsigned int aEty)
{
	unsigned int i,x;
	unsigned int aAddr;
	unsigned int AsteriskCnt;
	unsigned int ParCntHasGot;
	
	ParCntHasGot = 0;
	
	EntryChar.EntryInt32[0] = Exp.D32[(aEty * 4) + 4 + 0];
	EntryChar.EntryInt32[1] = Exp.D32[(aEty * 4) + 4 + 1];
	EntryChar.EntryInt32[2] = Exp.D32[(aEty * 4) + 4 + 2];
	EntryChar.EntryInt32[3] = Exp.D32[(aEty * 4) + 4 + 3];
	EntryChar.EntryInt32[4] = 0;
	
	eInput.IsAllFire = 0;
	eInput.nWildcard = 0;
	eInput.Entry[aEty].Illegal = 0;
	eInput.Entry[aEty].Sym = 0;
	eInput.Entry[aEty].BaseNum.Full = 0;
	eInput.Entry[aEty].dType = 0;
	eInput.Entry[aEty].Active = 0;
	for(i=0; i<8; i++){
		eInput.Asterisk[i] = 0;
		eInput.Hashtag[i] = 0;
	}
	x = 0;
	AsteriskCnt = 0;
	
	if( (EntryChar.Str[0] >= '0') && (EntryChar.Str[0] <= '9') ) eInput.Entry[aEty].BaseNum.Spl.BcdText_0 = EntryChar.Str[0] -'0';
	else if (EntryChar.Str[0] == '*'){eInput.Entry[aEty].BaseNum.Spl.BcdText_0 = 10; eInput.Asterisk[0] = 1; eInput.Entry[aEty].Sym |= 1; AsteriskCnt++;}
	else if (EntryChar.Str[0] == '#'){eInput.Entry[aEty].BaseNum.Spl.BcdText_0 = 11; eInput.Hashtag[0] = 1; eInput.Entry[aEty].Sym |= 2;}
	else{eInput.Entry[aEty].Illegal = 1; return;}
	
	if( (EntryChar.Str[1] >= '0') && (EntryChar.Str[1] <= '9') ) eInput.Entry[aEty].BaseNum.Spl.BcdText_1 = EntryChar.Str[1] -'0';
	else if (EntryChar.Str[1] == '*'){eInput.Entry[aEty].BaseNum.Spl.BcdText_1 = 10; eInput.Asterisk[1] = 1; eInput.Entry[aEty].Sym |= 1; AsteriskCnt++;}
	else if (EntryChar.Str[1] == '#'){eInput.Entry[aEty].BaseNum.Spl.BcdText_1 = 11; eInput.Hashtag[1] = 1; eInput.Entry[aEty].Sym |= 2;}
	else{eInput.Entry[aEty].Illegal = 1; return;}
	
	if( (EntryChar.Str[2] >= '0') && (EntryChar.Str[2] <= '9') ) eInput.Entry[aEty].BaseNum.Spl.BcdText_2 = EntryChar.Str[2] -'0';
	else if (EntryChar.Str[2] == '*'){eInput.Entry[aEty].BaseNum.Spl.BcdText_2 = 10; eInput.Asterisk[2] = 1; eInput.Entry[aEty].Sym |= 1; AsteriskCnt++;}
	else if (EntryChar.Str[2] == '#'){eInput.Entry[aEty].BaseNum.Spl.BcdText_2 = 11; eInput.Hashtag[2] = 1; eInput.Entry[aEty].Sym |= 2;}
	else{eInput.Entry[aEty].Illegal = 1; return;}
	
	if( (EntryChar.Str[3] >= '0') && (EntryChar.Str[3] <= '9') ) eInput.Entry[aEty].BaseNum.Spl.BcdText_3 = EntryChar.Str[3] -'0';
	else if (EntryChar.Str[3] == '*'){eInput.Entry[aEty].BaseNum.Spl.BcdText_3 = 10; eInput.Asterisk[3] = 1; eInput.Entry[aEty].Sym |= 1; AsteriskCnt++;} 
	else if (EntryChar.Str[3] == '#'){eInput.Entry[aEty].BaseNum.Spl.BcdText_3 = 11; eInput.Hashtag[3] = 1; eInput.Entry[aEty].Sym |= 2;} 
	else{eInput.Entry[aEty].Illegal = 1; return;}
	
	if( (EntryChar.Str[4] >= '0') && (EntryChar.Str[4] <= '9') ) eInput.Entry[aEty].BaseNum.Spl.BcdText_4 = EntryChar.Str[4] -'0';
	else if (EntryChar.Str[4] == '*'){eInput.Entry[aEty].BaseNum.Spl.BcdText_4 = 10; eInput.Asterisk[4] = 1; eInput.Entry[aEty].Sym |= 1; AsteriskCnt++;}
	else if (EntryChar.Str[4] == '#'){eInput.Entry[aEty].BaseNum.Spl.BcdText_4 = 11; eInput.Hashtag[4] = 1; eInput.Entry[aEty].Sym |= 2;}
	else{eInput.Entry[aEty].Illegal = 1; return;}
	
	if( (EntryChar.Str[5] >= '0') && (EntryChar.Str[5] <= '9') ) eInput.Entry[aEty].BaseNum.Spl.BcdText_5 = EntryChar.Str[5] -'0';
	else if (EntryChar.Str[5] == '*'){eInput.Entry[aEty].BaseNum.Spl.BcdText_5 = 10; eInput.Asterisk[5] = 1; eInput.Entry[aEty].Sym |= 1; AsteriskCnt++;}
	else if (EntryChar.Str[5] == '#'){eInput.Entry[aEty].BaseNum.Spl.BcdText_5 = 11; eInput.Hashtag[5] = 1; eInput.Entry[aEty].Sym |= 2;}
	else{eInput.Entry[aEty].Illegal = 1; return;}
	
	if( (EntryChar.Str[6] >= '0') && (EntryChar.Str[6] <= '9') ) eInput.Entry[aEty].BaseNum.Spl.BcdText_6 = EntryChar.Str[6] -'0';
	else if (EntryChar.Str[6] == '*'){eInput.Entry[aEty].BaseNum.Spl.BcdText_6 = 10; eInput.Asterisk[6] = 1; eInput.Entry[aEty].Sym |= 1; AsteriskCnt++;}
	else if (EntryChar.Str[6] == '#'){eInput.Entry[aEty].BaseNum.Spl.BcdText_6 = 11; eInput.Hashtag[6] = 1; eInput.Entry[aEty].Sym |= 2;}
	else{eInput.Entry[aEty].Illegal = 1; return;}
	
	if( (EntryChar.Str[7] >= '0') && (EntryChar.Str[7] <= '9') ) eInput.Entry[aEty].BaseNum.Spl.BcdText_7 = EntryChar.Str[7] -'0';
	else if (EntryChar.Str[7] == '*'){eInput.Entry[aEty].BaseNum.Spl.BcdText_7 = 10; eInput.Asterisk[7] = 1; eInput.Entry[aEty].Sym |= 1; AsteriskCnt++;}
	else if (EntryChar.Str[7] == '#'){eInput.Entry[aEty].BaseNum.Spl.BcdText_7 = 11; eInput.Hashtag[7] = 1; eInput.Entry[aEty].Sym |= 2;}
	else{eInput.Entry[aEty].Illegal = 1; return;}
	
	
	if(eInput.Entry[aEty].Illegal == 0){
		if(  (EntryChar.Str[8] >= '0') && (EntryChar.Str[8] <= '9')  ){ 
			eInput.Entry[aEty].dType += static_cast<unsigned int>(EntryChar.Str[8] - '0') * 100;
		}else{
			eInput.Entry[aEty].Illegal = 1;
		}
		if(  (EntryChar.Str[9] >= '0') && (EntryChar.Str[9] <= '9')  ){ 
			eInput.Entry[aEty].dType += static_cast<unsigned int>(EntryChar.Str[9] - '0') * 10;
		}else{
			eInput.Entry[aEty].Illegal = 1;
		}
		if(  (EntryChar.Str[10] >= '0') && (EntryChar.Str[10] <= '9')  ){ 
			eInput.Entry[aEty].dType += static_cast<unsigned int>(EntryChar.Str[10] - '0') * 1;
		}else{
			eInput.Entry[aEty].Illegal = 1;
		}
		if(eInput.Entry[aEty].dType > 255)eInput.Entry[aEty].Illegal = 1;
	}
	
	if(eInput.Entry[aEty].Illegal == 0){
		if(  (EntryChar.Str[11] > '0') && (EntryChar.Str[11] <= '9') ){
			eInput.Entry[aEty].ThresholdCnt = EntryChar.Str[11] - '0';
		}else
		if(EntryChar.Str[11] == ' '){
			eInput.Entry[aEty].ThresholdCnt = 1;
		}else{
			eInput.Entry[aEty].Illegal = 1;
		}
	}
	
	if(eInput.Entry[aEty].Illegal == 0){
		if(EntryChar.Str[12] == 'X'){
			eInput.Entry[aEty].Logic = 'X';
		}else
		if(EntryChar.Str[12] == '+'){
			eInput.Entry[aEty].Logic = '+';
		}else{
			eInput.Entry[aEty].Illegal = 1;
		}
	}
	
	if(eInput.Entry[aEty].Illegal == 0){
		if(ParCntHasGot == 0){
			if(eInput.Entry[aEty].Logic == 'X'){
				if(  (EntryChar.Str[13] > '0') && (EntryChar.Str[13] <= '9') ){
					eInput.ParCnt = EntryChar.Str[13] - '0';
					ParCntHasGot = 1;
				}else
				if(EntryChar.Str[13] == ' '){
					eInput.ParCnt = 1;
					ParCntHasGot = 1;
				}else{
					eInput.Entry[aEty].Illegal = 1;
				}
			}
		}
	}
	
	if(eInput.Entry[aEty].Illegal == 0){
		if( AsteriskCnt > 7) {
			GetFireStateByTypeViaMsg(eInput.Entry[aEty].dType, aEty);
		}else{
			if(eInput.Entry[aEty].Sym == 0){
				//Direct
				GetFireStateViaMsg(eInput.Entry[aEty].BaseNum , eInput.Entry[aEty].dType, aEty);
			}else
			if(eInput.Entry[aEty].Sym & 1){
				//Wildcard
				GetFireStateViaMsg(eInput.Entry[aEty].BaseNum , eInput.Entry[aEty].dType, aEty);
			}else
			if(eInput.Entry[aEty].Sym == 2){
				//Foor
			}
		}
	}
}


void TInterLink::ActiveEntryOutput(unsigned int aEty)
{
	unsigned int i,x;
	unsigned int aAddr;
	unsigned int AsteriskCnt;
	
	EntryChar.EntryInt32[0] = Exp.D32[(aEty * 4) + 4 + 64 + 0];
	EntryChar.EntryInt32[1] = Exp.D32[(aEty * 4) + 4 + 64 + 1];
	EntryChar.EntryInt32[2] = Exp.D32[(aEty * 4) + 4 + 64 + 2];
	EntryChar.EntryInt32[3] = Exp.D32[(aEty * 4) + 4 + 64 + 3];
	EntryChar.EntryInt32[4] = 0;
	
	eOutput.nWildcard = 0;
	eOutput.Entry[aEty].Illegal = 0;
	eOutput.Entry[aEty].Sym = 0;
	eOutput.Entry[aEty].BaseNum.Full = 0;
	eOutput.Entry[aEty].dType = 0;
	eOutput.Entry[aEty].Active = 0;
	for(i=0; i<8; i++){
		eOutput.Asterisk[i] = 0;
		eOutput.Hashtag[i] = 0;
	}
	x = 0;
	AsteriskCnt = 0;
	if( (EntryChar.Str[0] >= '0') && (EntryChar.Str[0] <= '9') ) eOutput.Entry[aEty].BaseNum.Spl.BcdText_0 = EntryChar.Str[0] -'0';
	else if (EntryChar.Str[0] == '*'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_0 = 10; eOutput.Asterisk[0] = 1; eOutput.Entry[aEty].Sym |= 1; AsteriskCnt ++;}
	else if (EntryChar.Str[0] == '#'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_0 = 11; eOutput.Hashtag[0] = 1; eOutput.Entry[aEty].Sym |= 2;}
	else{
		eOutput.Entry[aEty].Illegal = 1; 
		return;
	}
	
	if( (EntryChar.Str[1] >= '0') && (EntryChar.Str[1] <= '9') ) eOutput.Entry[aEty].BaseNum.Spl.BcdText_1 = EntryChar.Str[1] -'0';
	else if (EntryChar.Str[1] == '*'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_1 = 10; eOutput.Asterisk[1] = 1; eOutput.Entry[aEty].Sym |= 1; AsteriskCnt ++;}
	else if (EntryChar.Str[1] == '#'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_1 = 11; eOutput.Hashtag[1] = 1; eOutput.Entry[aEty].Sym |= 2;}
	else{
		eOutput.Entry[aEty].Illegal = 1; 
		return;
	}
	
	if( (EntryChar.Str[2] >= '0') && (EntryChar.Str[2] <= '9') ) eOutput.Entry[aEty].BaseNum.Spl.BcdText_2 = EntryChar.Str[2] -'0';
	else if (EntryChar.Str[2] == '*'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_2 = 10; eOutput.Asterisk[2] = 1; eOutput.Entry[aEty].Sym |= 1; AsteriskCnt ++;}
	else if (EntryChar.Str[2] == '#'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_2 = 11; eOutput.Hashtag[2] = 1; eOutput.Entry[aEty].Sym |= 2;}
	else{
		eOutput.Entry[aEty].Illegal = 1; 
		return;
	}
	
	if( (EntryChar.Str[3] >= '0') && (EntryChar.Str[3] <= '9') ) eOutput.Entry[aEty].BaseNum.Spl.BcdText_3 = EntryChar.Str[3] -'0';
	else if (EntryChar.Str[3] == '*'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_3 = 10; eOutput.Asterisk[3] = 1; eOutput.Entry[aEty].Sym |= 1; AsteriskCnt ++;} 
	else if (EntryChar.Str[3] == '#'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_3 = 11; eOutput.Hashtag[3] = 1; eOutput.Entry[aEty].Sym |= 2;} 
	else{
		eOutput.Entry[aEty].Illegal = 1; 
		return;
	}
	
	if( (EntryChar.Str[4] >= '0') && (EntryChar.Str[4] <= '9') ) eOutput.Entry[aEty].BaseNum.Spl.BcdText_4 = EntryChar.Str[4] -'0';
	else if (EntryChar.Str[4] == '*'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_4 = 10; eOutput.Asterisk[4] = 1; eOutput.Entry[aEty].Sym |= 1; AsteriskCnt ++;}
	else if (EntryChar.Str[4] == '#'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_4 = 11; eOutput.Hashtag[4] = 1; eOutput.Entry[aEty].Sym |= 2;}
	else{
		eOutput.Entry[aEty].Illegal = 1; 
		return;
	}
	
	if( (EntryChar.Str[5] >= '0') && (EntryChar.Str[5] <= '9') ) eOutput.Entry[aEty].BaseNum.Spl.BcdText_5 = EntryChar.Str[5] -'0';
	else if (EntryChar.Str[5] == '*'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_5 = 10; eOutput.Asterisk[5] = 1; eOutput.Entry[aEty].Sym |= 1; AsteriskCnt ++;}
	else if (EntryChar.Str[5] == '#'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_5 = 11; eOutput.Hashtag[5] = 1; eOutput.Entry[aEty].Sym |= 2;}
	else{
		eOutput.Entry[aEty].Illegal = 1; 
		return;
	}
	
	if( (EntryChar.Str[6] >= '0') && (EntryChar.Str[6] <= '9') ) eOutput.Entry[aEty].BaseNum.Spl.BcdText_6 = EntryChar.Str[6] -'0';
	else if (EntryChar.Str[6] == '*'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_6 = 10; eOutput.Asterisk[6] = 1; eOutput.Entry[aEty].Sym |= 1; AsteriskCnt ++;}
	else if (EntryChar.Str[6] == '#'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_6 = 11; eOutput.Hashtag[6] = 1; eOutput.Entry[aEty].Sym |= 2;}
	else{
		eOutput.Entry[aEty].Illegal = 1; 
		return;
	}
	
	if( (EntryChar.Str[7] >= '0') && (EntryChar.Str[7] <= '9') ) eOutput.Entry[aEty].BaseNum.Spl.BcdText_7 = EntryChar.Str[7] -'0';
	else if (EntryChar.Str[7] == '*'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_7 = 10; eOutput.Asterisk[7] = 1; eOutput.Entry[aEty].Sym |= 1; AsteriskCnt ++;}
	else if (EntryChar.Str[7] == '#'){eOutput.Entry[aEty].BaseNum.Spl.BcdText_7 = 11; eOutput.Hashtag[7] = 1; eOutput.Entry[aEty].Sym |= 2;}
	else{	
		eOutput.Entry[aEty].Illegal = 1; 
		return;
	}
	
	if(eOutput.Entry[aEty].Illegal == 0){
		if(EntryChar.Str[8]=='*' and EntryChar.Str[9]=='*' and EntryChar.Str[10]=='*'){
			eOutput.Entry[aEty].dType=0;
		}else
		if(EntryChar.Str[8]=='*' or  EntryChar.Str[9]=='*' or  EntryChar.Str[10]=='*'){
			eOutput.Entry[aEty].Illegal = 1;
		}else{
			if(  (EntryChar.Str[8] >= '0') && (EntryChar.Str[8] <= '9')  ){ 
				eOutput.Entry[aEty].dType += static_cast<unsigned int>(EntryChar.Str[8] - '0') * 100;
			}else{
				eOutput.Entry[aEty].Illegal = 1;
			}
			if(  (EntryChar.Str[9] >= '0') && (EntryChar.Str[9] <= '9')  ){ 
				eOutput.Entry[aEty].dType += static_cast<unsigned int>(EntryChar.Str[9] - '0') * 10;
			}else{
				eOutput.Entry[aEty].Illegal = 1;
			}
			if(  (EntryChar.Str[10] >= '0') && (EntryChar.Str[10] <= '9')  ){ 
				eOutput.Entry[aEty].dType += static_cast<unsigned int>(EntryChar.Str[10] - '0') * 1;
			}else{
				eOutput.Entry[aEty].Illegal = 1;
			}
			if(eOutput.Entry[aEty].dType==0 or eOutput.Entry[aEty].dType > 255)eOutput.Entry[aEty].Illegal = 1;
		}
	}
	
	if(eOutput.Entry[aEty].Illegal == 0){
		eOutput.Entry[aEty].DelaySec = 0;
		if(  (EntryChar.Str[11] >= '0') && (EntryChar.Str[11] <= '9')  ){ 
			eOutput.Entry[aEty].DelaySec += static_cast<unsigned int>(EntryChar.Str[11] - '0');
		}
		if(  (EntryChar.Str[12] >= '0') && (EntryChar.Str[12] <= '9')  ){
			eOutput.Entry[aEty].DelaySec  = eOutput.Entry[aEty].DelaySec * 10;
			eOutput.Entry[aEty].DelaySec += static_cast<unsigned int>(EntryChar.Str[12] - '0');
		}
		if(  (EntryChar.Str[13] >= '0') && (EntryChar.Str[13] <= '9')  ){ 
			eOutput.Entry[aEty].DelaySec  = eOutput.Entry[aEty].DelaySec * 10;
			eOutput.Entry[aEty].DelaySec += static_cast<unsigned int>(EntryChar.Str[13] - '0');
		}
		if(eOutput.Entry[aEty].DelaySec > 600)eOutput.Entry[aEty].Illegal = 1;
	}
	
	if(eOutput.Entry[aEty].Illegal == 0){
		//if( (AsteriskCnt > 7) && (eOutput.Entry[aEty].dType == dgType_VAALRAM) ){
			
		//}else{
			if(eOutput.Entry[aEty].Sym == 0){
				//Direct
				if(RunState != TRunModeCfg::RunModeAdjust){
					SetOutputStateViaUserCode(eOutput.Entry[aEty].BaseNum, eOutput.Entry[aEty].dType, aEty, eOutput.Entry[aEty].DelaySec, eOutput.DoStop);
				}else{
					SetOutputStateViaUserCodeSimulate(eOutput.Entry[aEty].BaseNum, eOutput.Entry[aEty].dType, aEty, eOutput.Entry[aEty].DelaySec, eOutput.DoStop);
				}
				//if(MainCtl.fData.Split.NetworkMode == 1){
				//	ProtoC2C.MasterDoRemoteLnkStartStopViaUserCode(eOutput.Entry[aEty].BaseNum.Full, eOutput.Entry[aEty].dType, eOutput.Entry[aEty].DelaySec, eOutput.DoStop);
				//}
			}else
			if(eOutput.Entry[aEty].Sym & 1){
				//Wildcard
				if(RunState != TRunModeCfg::RunModeAdjust){
					SetOutputStateViaUserCode(eOutput.Entry[aEty].BaseNum, eOutput.Entry[aEty].dType, aEty, eOutput.Entry[aEty].DelaySec, eOutput.DoStop);
				}else{
					SetOutputStateViaUserCodeSimulate(eOutput.Entry[aEty].BaseNum, eOutput.Entry[aEty].dType, aEty, eOutput.Entry[aEty].DelaySec, eOutput.DoStop);
				}
			}else
			if(eOutput.Entry[aEty].Sym == 2){
				//Foor
			}
		//}
	}
}

void TInterLink::LoadInputState()
{
	int x;
	eInput.HasInput = 0;
	for(x=0;x<16;x++)LoadOneEntryInput(x);
}

void TInterLink::SetOutputState()
{
	int x;
	for(x=0; x<16;x++)ActiveEntryOutput(x);
}

void TInterLink::CheckGoRunLogic()
{
	int i, AndCnt, AndRdyCnt;
	if(eInput.HasInput == 0){
		eOutput.Action = 0;
		return;
	}else{
		AndCnt = 0;
		AndRdyCnt = 0;
		for(i=0; i<16; i++){
			if( (eInput.Entry[i].Illegal == 0) && (eInput.Entry[i].Logic == 'X') ){
				AndCnt++;
				if(eInput.Entry[i].Active){
					AndRdyCnt++;
				}
			}
		}
		if(  (AndCnt >0) && (AndRdyCnt >= eInput.ParCnt)  ){
			eOutput.Action = 1;
			return;
		}
		for(i=0; i<16; i++){
			if(eInput.Entry[i].Active){
				if(eInput.Entry[i].Logic == '+'){	
					eOutput.Action = 1;
					return;
				}
			}
			
		}
	}
}


int TInterLink::LoadLinkageExp()
{
	int i, Ava;
	unsigned int ActualIndex;
	volatile unsigned int aAddr;
	
	if(LinkIndex >= LinkCount)LinkIndex = 0;
	ActualIndex = LinkNumList[LinkIndex];
	Ava = 0;
	if( (LinkHasStartTabel[ActualIndex] == 0xAA) && (Forcing == 0) ){
		Ava = 0;
	}else{
		aAddr = dSdAddrLinkageExp + (ActualIndex * 1024);
		Crc32Reset();
		for(i=0; i<255; i++){
			Exp.D32[i] = *(volatile unsigned int *)(aAddr + (i * 4));
			Crc32Feed(Exp.D32[i]);
		}
		Exp.D32[255]=0;
		if(this->Crc[ActualIndex] == Crc32GetResult()){
			LinkCurrent = ActualIndex;
			Ava = 1;
		}else{
			if(0==RuntimeReloadExpression2Ram(ActualIndex)){
				LinkCurrent = ActualIndex;
				Ava = 1;
			}
		}
	}
	LinkIndex++;
	if(LinkIndex >= LinkCount)LinkIndex = 0;
	return Ava;
}

void TInterLink::LoadExpSettingBytes(unsigned int aInx)
{
	volatile unsigned int aAddr = dSdAddrLinkageExp + (aInx * 1024);
	Exp.D32[1] = *(volatile unsigned int *)(aAddr + (1 * 4));	
}

int TInterLink::LoadExp(unsigned int aInx)
{
	volatile unsigned int aAddr;
	unsigned int i;
	aAddr = dSdAddrLinkageExp + (aInx * 1024);
	Crc32Reset();
	for(i=0; i<255; i++){
		Exp.D32[i] = *(volatile unsigned int *)(aAddr + (i * 4));
		Crc32Feed(Exp.D32[i]);
	}
	Exp.D32[255]=0;
	if(this->Crc[aInx] != Crc32GetResult()){
		if(0 != RuntimeReloadExpression2Ram(aInx)){
			return 0;
		}
	}
	return 1;
}

int TInterLink::QueueLoadExp()
{
	int i, Ava;
	volatile unsigned int aAddr;
	Ava =0;
	ForceDoOutput =0;
	FirstDoOutput =0;
	NewMatchDoOutput =0;
	NewMatchFound =0;
	if(LinkIndex >= LinkCount)LinkIndex = 0;
	LinkCurrent = LinkNumList[LinkIndex];
	LinkIndex++;
	if(LinkIndex >= LinkCount)LinkIndex = 0;
	
	FireInxPrv = PrcFireOutputInx[LinkCurrent];
	
	if( Forcing ){
		PrcFireOutputInx[LinkCurrent] = CtlCenter.FiredUserCode.Count;
		PrcFireInx[LinkCurrent] = PrcFireOutputInx[LinkCurrent];
		ForceDoOutput =1;
		Ava = LoadExp(LinkCurrent);
	}else{
		if(AllowAuto){
			if(LinkHasStartTabel[LinkCurrent] == 0xAA){
				if( PrcFireOutputInx[LinkCurrent] < (CtlCenter.FiredUserCode.Count) ) {
					PrcFireOutputInx[LinkCurrent] = CtlCenter.FiredUserCode.Count;
					PrcFireInx[LinkCurrent] = PrcFireOutputInx[LinkCurrent];
					NewMatchDoOutput =1;
					 Ava = LoadExp(LinkCurrent);
				}
			}else{
				if( PrcFireOutputInx[LinkCurrent] < (CtlCenter.FiredUserCode.Count) ) {
					PrcFireOutputInx[LinkCurrent] = CtlCenter.FiredUserCode.Count;
					PrcFireInx[LinkCurrent] = PrcFireOutputInx[LinkCurrent];
					FirstDoOutput =1;
					Ava = LoadExp(LinkCurrent);
				}
			}
		}else{
			if( ( PrcFireOutputInx[LinkCurrent] < CtlCenter.FiredUserCode.Count ) or ( PrcFireInx[LinkCurrent] < CtlCenter.FiredUserCode.Count ) ){
				Ava = LoadExp(LinkCurrent);
			}
			if(Ava){
				if(Exp.Exp[6] == 'F'){
					if(LinkHasStartTabel[LinkCurrent] == 0xAA){
						if( PrcFireOutputInx[LinkCurrent] < CtlCenter.FiredUserCode.Count ) {
							PrcFireOutputInx[LinkCurrent] = CtlCenter.FiredUserCode.Count;
							PrcFireInx[LinkCurrent] = PrcFireOutputInx[LinkCurrent];
							NewMatchDoOutput =1;
						}
					}else{
						if( PrcFireOutputInx[LinkCurrent] < (CtlCenter.FiredUserCode.Count) ) {
							PrcFireOutputInx[LinkCurrent] = CtlCenter.FiredUserCode.Count;
							PrcFireInx[LinkCurrent] = PrcFireOutputInx[LinkCurrent];
							FirstDoOutput =1;
						}
					}	
				}else{
					if( PrcFireInx[LinkCurrent] < CtlCenter.FiredUserCode.Count ) {
						PrcFireInx[LinkCurrent] = CtlCenter.FiredUserCode.Count;
					}
				}
			}
		}
	}	
	
	if(Ava ==0 )return 0;
	if(Exp.Exp[4] == 'M')return 0;
	if(Exp.Exp[5] == 'S')eOutput.DoStop =1;
	else eOutput.DoStop = 0;
	
	return 1;	
}

void TInterLink::GoAction()
{
	LoadInputState();
	eOutput.Action = 0;
	CheckGoRunLogic();

	if(eOutput.Action){
		SetOutputState();
	}else{
		if(AllowAuto == 0){
			if(eOutput.DoStop == 0){
				LinkHasRdyGoTabel[LinkCurrent] =0;
			}
		}
	}
}

void TInterLink::DoLnk()
{
	if(QueueLoadExp()){
		LoadInputState();
		eOutput.Action = 0;
		CheckGoRunLogic();
		if(eOutput.Action){
			SetOutputState();
		}else{
			if(AllowAuto == 0){
				if(eOutput.DoStop == 0){
					LinkHasRdyGoTabel[LinkCurrent] =0;
				}
			}
		}
	}
	
	if( LinkageManualGo ){
		LinkageManualGo = 0;
		ForceInx = 0;
		Forcing = 1;
	}
	if(Forcing){
		ForceInx++;
		if(ForceInx > LinkCount){
			Forcing = 0;
		}
	}
	
	return;
	
	/*if(Forcing){
		DoTick = 10;
		do{
			ForceLoadLinkageExp();
			GoAction();
			ForceInx++;
			if(ForceInx >= LinkCount){
				Forcing = 0;
				break;
			}
			DoTick--;
		}while(DoTick > 0);
	}else{
		if( (AllowManual) && (LinkageManualGo) ){
			LinkageManualGo = 0;
			Forcing = 1;
			ForceInx = 0;
		}else{
			DoTick = 10;
			if(ShouldGo){
				do{
					LinkAva = LoadLinkageExp();
					if(LinkAva){
						GoAction();
					}
					if(LinkIndex == 0){
						ShouldGo = 0;
						break;
					}
					DoTick--;
				}while(DoTick > 0);
			}
			
			if(ShouldGo == 0){
				DoEntryTickDiff = CtlCenter.SysFreeTick1Ms - DoEbtryLastTick;
				if(DoEntryTickDiff > 1000){
					DoEbtryLastTick = CtlCenter.SysFreeTick1Ms;
					ShouldGo = 1;
				}
			}
		}
	}*/	
}

void TInterLink::RecallCrcFaultMsg()
{
	for(int i=0; i< dLINKAGE_MAX_COUNT; i++){
		if(CrcFlag[i]){
			CtlCenter.ieAddBrdFaultLinkageStorage(i+1);
		}
	}
}

void TInterLink::Task10Ms()
{
	if(get_can_tx_complete()){
		if(oBuf.ReadIndex != oBuf.WriteIndex){
			SendOutputBuf();
		}
	}
}














