// File: /workspace/github_cpp_makefile_star2_O3/estwings57[P]CasHMC/sources/LinkMaster.cpp
// Function: CasHMC::LinkMaster::Update()
void LinkMaster::Update()
{
	if(Buffers.size() > 0) {
		//Make sure that buffer[0] is not virtual tail packet.
		if(Buffers[0] == NULL) {
			ERROR(header<<"  == Error - Buffers[0] is NULL (It could be one of virtual tail packet  (CurrentClock : "<<currentClockCycle<<")");
			exit(0);
		}
		else if(Buffers[0]->bufPopDelay == 0) {
			//Token count register represents the available space in link slave input buffer
			if(linkRxTx.size() == 0 && !(Buffers[0]->packetType != FLOW && tokenCount < Buffers[0]->LNG)) {
				int tempWriteP = retBufWriteP + Buffers[0]->LNG;
				if(retBufWriteP	>= retBufReadP) {
					if(tempWriteP - (int)retBufReadP < MAX_RETRY_BUF) {
						CRCCountdown(tempWriteP, Buffers[0]);
					}
					else {
						//DEBUG(ALI(18)<<header<<ALI(15)<<*Buffers[0]<<(downstream ? "Down) " : "Up)   ")<<"Retry buffer is near-full condition");
					}
				}
				else {
					if((int)retBufReadP - tempWriteP > 0) {
						CRCCountdown(tempWriteP, Buffers[0]);
					}
					else {
						//DEBUG(ALI(18)<<header<<ALI(15)<<*Buffers[0]<<(downstream ? "Down) " : "Up)   ")<<"Retry buffer is near-full condition");
					}
				}
			}
			else {
				//DEBUG(ALI(18)<<header<<ALI(15)<<*Buffers[0]<<(downstream ? "Down) " : "Up)   ")<<"packet length ("<<Buffers[0]->LNG<<") is BIGGER than token count register ("<<tokenCount<<")");
			}
		}
	}

	//Finish LinkRetry state, and then backup packets return to buffers 
	if(currentState == LINK_RETRY && Buffers.empty() && linkRxTx.empty()) {
		//If StartRetry state is waiting, start StartRetry state
		if(readyStartRetry == true) {
			readyStartRetry = false;
			for(int i=0; i<NUM_OF_IRTRY; i++) {
				//packet, cmd, addr, cub, lng, *lat
				Packet *packetIRTRY = new Packet(FLOW, IRTRY, 0, 0, 1, NULL);
				packetIRTRY->TAG = retryStartPacket->TAG;
				packetIRTRY->RRP = lastestRRP;
				packetIRTRY->FRP = 1;		//StartRetry flag is set with FRP[0] = 1
				if(downstream)	packetIRTRY->bufPopDelay = 0;
				linkRxTx.push_back(packetIRTRY);
				currentState = START_RETRY;
				header.erase(header.find(")"));
				header += ") ST_RT";
			}
		}
		else {
			currentState = ACTIVE;
			header.erase(header.find(")"));
			header += ")";
		}
		//Restore backup packets to Buffers
		for(int i=0; i<backupBuffers.size(); i++) {
			Buffers.push_back(backupBuffers[i]);
		}
		backupBuffers.clear();
	}
	
	if(startRetryTimer)		retryTimer++;
	//Retry timer should be set to at least 3 times the retry buffer full period
	if(retryTimer > MAX_RETRY_BUF*4) {
		DEBUG(ALI(33)<<header<<(downstream ? "Down) " : "Up)   ")<<"Retry timer TIME-OUT  (retry attempt : "<<retryAttempts<<")");
		if(retryAttempts == RETRY_ATTEMPT_LIMIT) {
			FinishRetry();
		}
		else {
			retryAttempts++;
			StartRetry(retryStartPacket);
		}
	}
	
	//Requester/Responder issue continuous scrambled NULL packet and TS1 training sequence for link retraining
	if(currentState == RETRAIN1 && currentClockCycle >= retrainTransit && firstNull) {
		//Upon descrambler sync, the responder will begin to transmit scrambled NULL packet
		Packet *packetNULL = new Packet(FLOW, NULL_, 0, 0, 1, NULL);
		linkRxTx.push_back(packetNULL);
		DEBUG(ALI(33)<<header<<(downstream ? "Down) " : "Up)   ")<<"issue NULL packet for descrambler initializing");
		firstNull = false;
	}
	//Requester/Responder enters the TS1 state 
	// and issues the scrambled TS1 training sequence continuously to achieve FLIT synchrony
	else if(currentState == RETRAIN2 && currentClockCycle >= retrainTransit && firstNull) {
		//Because TS1 is 16-bit character, we just employ NULL packet instead of TS1
		Packet *packetTS1 = new Packet(FLOW, NULL_, 0, 0, 1, NULL);
		linkRxTx.push_back(packetTS1);
		DEBUG(ALI(33)<<header<<(downstream ? "Down) " : "Up)   ")<<"issue TS1 sequence to achieve FLIT synchrony");
		firstNull = false;
	}
	
	Step();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x1d0(%rdi), %rax
cmpq %rax, 0x1d8(%rdi)
je 0x24031
movq (%rax), %rdx
testq %rdx, %rdx
je 0x24719
cmpl $0x0, 0x14(%rdx)
jne 0x24031
movq 0x1f0(%rbx), %rax
cmpq 0x1e8(%rbx), %rax
je 0x241a7
cmpl $0xb, 0x200(%rbx)
jne 0x242b1
movq 0x1d0(%rbx), %rax
cmpq 0x1d8(%rbx), %rax
jne 0x242b1
movq 0x1e8(%rbx), %rax
cmpq 0x1f0(%rbx), %rax
jne 0x242b1
cmpb $0x1, 0x250(%rbx)
jne 0x241ea
movb $0x0, 0x250(%rbx)
leaq 0x17457(%rip), %rax     # 0x3b4d8
cmpl $0x0, (%rax)
jle 0x24236
leaq 0x1e8(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x20(%rbx), %r15
xorl %r14d, %r14d
leaq 0xf43d(%rip), %r13      # 0x334e1
leaq 0xe8f3(%rip), %rbp      # 0x3299e
movl $0x468, %edi            # imm = 0x468
callq 0x42f0
movq %rax, %r12
movq $0x0, (%rsp)
movq %rax, %rdi
movl $0x2, %esi
movl $0x3, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x27c44
movq %r12, 0x8(%rsp)
movq 0x248(%rbx), %rax
movl 0x42c(%rax), %eax
movl %eax, 0x42c(%r12)
movl 0x20c(%rbx), %eax
movl %eax, 0x44c(%r12)
movl $0x1, 0x448(%r12)
cmpb $0x1, 0x1cc(%rbx)
jne 0x24123
movl $0x0, 0x14(%r12)
movq 0x1f0(%rbx), %rsi
cmpq 0x1f8(%rbx), %rsi
je 0x24140
movq %r12, (%rsi)
addq $0x8, 0x1f0(%rbx)
jmp 0x2414f
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x1a462
movl $0xa, 0x200(%rbx)
movl $0x1, %ecx
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x4510
movq 0x28(%rbx), %rcx
cmpq %rax, %rcx
jb 0x24701
movq %rax, 0x28(%rbx)
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4580
incl %r14d
leaq 0x1733f(%rip), %rax     # 0x3b4d8
cmpl (%rax), %r14d
jl 0x240ab
jmp 0x24236
cmpl $0x2, 0x10(%rdx)
jne 0x241b5
movl 0x430(%rdx), %esi
jmp 0x241c7
movl 0x430(%rdx), %esi
cmpl %esi, 0x208(%rbx)
jb 0x24031
movl 0x25c(%rbx), %eax
movl 0x260(%rbx), %ecx
addl %ecx, %esi
cmpl %eax, %ecx
jae 0x2450f
cmpl %eax, %esi
jge 0x24031
jmp 0x24522
movl $0x0, 0x200(%rbx)
leaq 0x20(%rbx), %r14
leaq 0xf2e2(%rip), %rsi      # 0x334e1
movl $0x1, %ecx
movq %r14, %rdi
xorl %edx, %edx
callq 0x4510
movq 0x28(%rbx), %rcx
cmpq %rax, %rcx
jb 0x24701
movq %rax, 0x28(%rbx)
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
leaq 0xf2b3(%rip), %rsi      # 0x334e1
movq %r14, %rdi
callq 0x4580
movq 0x220(%rbx), %rdx
movq 0x228(%rbx), %rax
cmpq %rdx, %rax
je 0x242a5
leaq 0x1d0(%rbx), %r14
xorl %r15d, %r15d
xorl %r12d, %r12d
addq %r15, %rdx
movq 0x1d8(%rbx), %rsi
cmpq 0x1e0(%rbx), %rsi
je 0x24279
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x1d8(%rbx)
jmp 0x24281
movq %r14, %rdi
callq 0x1a462
incq %r12
movq 0x220(%rbx), %rdx
movq 0x228(%rbx), %rax
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x3, %rcx
addq $0x8, %r15
cmpq %r12, %rcx
ja 0x24256
cmpq %rdx, %rax
je 0x242b1
movq %rdx, 0x228(%rbx)
movl 0x254(%rbx), %eax
cmpb $0x1, 0x251(%rbx)
jne 0x242c8
incl %eax
movl %eax, 0x254(%rbx)
leaq 0x171ed(%rip), %rcx     # 0x3b4bc
movl (%rcx), %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jbe 0x24411
leaq 0x171a5(%rip), %rax     # 0x3b488
cmpb $0x1, (%rax)
jne 0x243df
leaq 0x17196(%rip), %rax     # 0x3b489
cmpb $0x0, (%rax)
jne 0x243df
movq 0x16ccd(%rip), %rax     # 0x3afd0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x21, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %r14
cmpb $0x0, 0xe1(%rax,%rcx)
jne 0x2433a
movq %r14, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%r14)
movb $0x20, 0xe0(%r14)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq 0x16c7f(%rip), %rdi     # 0x3afd0
callq 0x4340
movq %rax, %r14
leaq 0xe418(%rip), %rax      # 0x32778
leaq 0xe418(%rip), %rsi      # 0x3277f
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r14, %rdi
callq 0x4340
leaq 0xe7dc(%rip), %rsi      # 0x32b62
movl $0x27, %edx
movq %r14, %rdi
callq 0x4340
movl 0x258(%rbx), %esi
movq %r14, %rdi
callq 0x41e0
movq %rax, %r14
leaq 0xf136(%rip), %rsi      # 0x334e1
movl $0x1, %edx
movq %rax, %rdi
callq 0x4340
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x42d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41f0
movl 0x258(%rbx), %eax
leaq 0x170f0(%rip), %rcx     # 0x3b4dc
cmpl (%rcx), %eax
jne 0x243fa
movq %rbx, %rdi
callq 0x23ddc
jmp 0x24411
incl %eax
movl %eax, 0x258(%rbx)
movq 0x248(%rbx), %rsi
movq %rbx, %rdi
callq 0x234fc
movl 0x200(%rbx), %eax
cmpl $0x4, %eax
je 0x2449a
cmpl $0x3, %eax
jne 0x246ea
movl 0x264(%rbx), %eax
cmpq %rax, 0x8(%rbx)
jb 0x246ea
cmpb $0x1, 0x268(%rbx)
jne 0x246ea
movl $0x468, %edi            # imm = 0x468
callq 0x42f0
movq %rax, %r14
movq $0x0, (%rsp)
movq %rax, %rdi
movl $0x2, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x27c44
movq %r14, 0x8(%rsp)
movq 0x1f0(%rbx), %rsi
cmpq 0x1f8(%rbx), %rsi
je 0x2452f
movq %r14, (%rsi)
addq $0x8, 0x1f0(%rbx)
jmp 0x24540
movl 0x264(%rbx), %eax
cmpq %rax, 0x8(%rbx)
jb 0x246ea
cmpb $0x1, 0x268(%rbx)
jne 0x246ea
movl $0x468, %edi            # imm = 0x468
callq 0x42f0
movq %rax, %r14
movq $0x0, (%rsp)
movq %rax, %rdi
movl $0x2, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x27c44
movq %r14, 0x8(%rsp)
movq 0x1f0(%rbx), %rsi
cmpq 0x1f8(%rbx), %rsi
je 0x245f4
movq %r14, (%rsi)
addq $0x8, 0x1f0(%rbx)
jmp 0x24605
movl %esi, %ecx
subl %eax, %ecx
leaq 0x16fa2(%rip), %rax     # 0x3b4bc
cmpl (%rax), %ecx
jge 0x24031
movq %rbx, %rdi
callq 0x247d2
jmp 0x24031
leaq 0x1e8(%rbx), %rdi
leaq 0x8(%rsp), %rdx
callq 0x1a462
leaq 0x16f41(%rip), %rax     # 0x3b488
cmpb $0x1, (%rax)
jne 0x246e3
leaq 0x16f32(%rip), %rax     # 0x3b489
cmpb $0x0, (%rax)
jne 0x246e3
movq 0x16a69(%rip), %rax     # 0x3afd0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x21, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %r14
cmpb $0x0, 0xe1(%rax,%rcx)
jne 0x2459e
movq %r14, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%r14)
movb $0x20, 0xe0(%r14)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq 0x16a1b(%rip), %rdi     # 0x3afd0
callq 0x4340
movq %rax, %r14
leaq 0xe1b4(%rip), %rax      # 0x32778
leaq 0xe1b4(%rip), %rsi      # 0x3277f
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r14, %rdi
callq 0x4340
leaq 0xe5a0(%rip), %rsi      # 0x32b8a
movl $0x2e, %edx
jmp 0x246b4
leaq 0x1e8(%rbx), %rdi
leaq 0x8(%rsp), %rdx
callq 0x1a462
leaq 0x16e7c(%rip), %rax     # 0x3b488
cmpb $0x1, (%rax)
jne 0x246e3
leaq 0x16e6d(%rip), %rax     # 0x3b489
cmpb $0x0, (%rax)
jne 0x246e3
movq 0x169a4(%rip), %rax     # 0x3afd0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x21, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %r14
cmpb $0x0, 0xe1(%rax,%rcx)
jne 0x24663
movq %r14, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%r14)
movb $0x20, 0xe0(%r14)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq 0x16956(%rip), %rdi     # 0x3afd0
callq 0x4340
movq %rax, %r14
leaq 0xe0ef(%rip), %rax      # 0x32778
leaq 0xe0ef(%rip), %rsi      # 0x3277f
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r14, %rdi
callq 0x4340
leaq 0xe50a(%rip), %rsi      # 0x32bb9
movl $0x2c, %edx
movq %r14, %rdi
callq 0x4340
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x42d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41f0
movb $0x0, 0x268(%rbx)
movq %rbx, %rdi
callq 0x25df6
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd237(%rip), %rdi      # 0x3193f
leaq 0xd277(%rip), %rsi      # 0x31986
movq %rax, %rdx
xorl %eax, %eax
callq 0x4410
movq 0x168d0(%rip), %rdi     # 0x3aff0
leaq 0xb8fb(%rip), %rsi      # 0x30022
callq 0x42e0
leaq 0xe1bc(%rip), %rsi      # 0x328ef
movq %rax, %rdi
callq 0x42e0
leaq 0xc155(%rip), %rsi      # 0x30897
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
movl $0x128, %esi            # imm = 0x128
callq 0x4500
leaq 0xb8e3(%rip), %rsi      # 0x30041
movq %rax, %rdi
callq 0x42e0
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x4290
leaq 0xe38f(%rip), %rsi      # 0x32b08
movq %rax, %rdi
callq 0x42e0
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x40c0
leaq 0xed4d(%rip), %rsi      # 0x334e1
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
callq 0x4170
xorl %edi, %edi
callq 0x4360
jmp 0x247ad
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %r14, %rdi
jmp 0x247c5
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %r12, %rdi
callq 0x4300
movq %rbx, %rdi
callq 0x4540