// File: /workspace/github_cpp_makefile_star2_O1/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 0x247df
movq (%rax), %rdx
testq %rdx, %rdx
je 0x24ed0
cmpl $0x0, 0x14(%rdx)
jne 0x247df
movq 0x1f0(%rbx), %rax
cmpq 0x1e8(%rbx), %rax
je 0x24955
cmpl $0xb, 0x200(%rbx)
jne 0x24a5d
movq 0x1d0(%rbx), %rax
cmpq 0x1d8(%rbx), %rax
jne 0x24a5d
movq 0x1e8(%rbx), %rax
cmpq 0x1f0(%rbx), %rax
jne 0x24a5d
cmpb $0x1, 0x250(%rbx)
jne 0x24996
movb $0x0, 0x250(%rbx)
leaq 0x17ca9(%rip), %rax     # 0x3c4d8
cmpl $0x0, (%rax)
jle 0x249e2
leaq 0x1e8(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0x20(%rbx), %r15
xorl %r14d, %r14d
leaq 0xfc0f(%rip), %r13      # 0x34461
leaq 0xf0c9(%rip), %rbp      # 0x33922
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 0x284bc
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 0x248d1
movl $0x0, 0x14(%r12)
movq 0x1f0(%rbx), %rsi
cmpq 0x1f8(%rbx), %rsi
je 0x248ee
movq %r12, (%rsi)
addq $0x8, 0x1f0(%rbx)
jmp 0x248fd
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x1ac5a
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 0x24eb8
movq %rax, 0x28(%rbx)
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4580
incl %r14d
leaq 0x17b91(%rip), %rax     # 0x3c4d8
cmpl (%rax), %r14d
jl 0x24859
jmp 0x249e2
cmpl $0x2, 0x10(%rdx)
je 0x2496d
movl 0x208(%rbx), %eax
cmpl 0x430(%rdx), %eax
jb 0x247df
movl 0x25c(%rbx), %eax
movl 0x260(%rbx), %ecx
movl 0x430(%rdx), %esi
addl %ecx, %esi
cmpl %eax, %ecx
jae 0x24d84
cmpl %eax, %esi
jge 0x247df
jmp 0x24d97
movl $0x0, 0x200(%rbx)
leaq 0x20(%rbx), %r14
leaq 0xfab6(%rip), %rsi      # 0x34461
movl $0x1, %ecx
movq %r14, %rdi
xorl %edx, %edx
callq 0x4510
movq 0x28(%rbx), %rcx
cmpq %rax, %rcx
jb 0x24eb8
movq %rax, 0x28(%rbx)
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
leaq 0xfa87(%rip), %rsi      # 0x34461
movq %r14, %rdi
callq 0x4580
movq 0x220(%rbx), %rdx
movq 0x228(%rbx), %rax
cmpq %rdx, %rax
je 0x24a51
leaq 0x1d0(%rbx), %r14
xorl %r15d, %r15d
xorl %r12d, %r12d
addq %r15, %rdx
movq 0x1d8(%rbx), %rsi
cmpq 0x1e0(%rbx), %rsi
je 0x24a25
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x1d8(%rbx)
jmp 0x24a2d
movq %r14, %rdi
callq 0x1ac5a
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 0x24a02
cmpq %rdx, %rax
je 0x24a5d
movq %rdx, 0x228(%rbx)
cmpb $0x1, 0x251(%rbx)
jne 0x24a6c
incl 0x254(%rbx)
leaq 0x17a49(%rip), %rax     # 0x3c4bc
movl (%rax), %eax
shll $0x2, %eax
cmpl %eax, 0x254(%rbx)
jbe 0x24bc0
leaq 0x179fd(%rip), %rax     # 0x3c488
cmpb $0x1, (%rax)
jne 0x24b8e
leaq 0x179ee(%rip), %rax     # 0x3c489
cmpb $0x0, (%rax)
jne 0x24b8e
movq 0x17525(%rip), %rax     # 0x3bfd0
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 0x24ae9
movq %r14, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x20, 0xe0(%r14)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq 0x174d0(%rip), %rdi     # 0x3bfd0
callq 0x4340
movq %rax, %r14
leaq 0xebe1(%rip), %rax      # 0x336f0
leaq 0xebe1(%rip), %rsi      # 0x336f7
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r14, %rdi
callq 0x4340
leaq 0xefb1(%rip), %rsi      # 0x33ae6
movl $0x27, %edx
movq %r14, %rdi
callq 0x4340
movl 0x258(%rbx), %esi
movq %r14, %rdi
callq 0x41e0
movq %rax, %r14
leaq 0xf907(%rip), %rsi      # 0x34461
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 0x17941(%rip), %rcx     # 0x3c4dc
cmpl (%rcx), %eax
jne 0x24ba9
movq %rbx, %rdi
callq 0x24584
jmp 0x24bc0
incl %eax
movl %eax, 0x258(%rbx)
movq 0x248(%rbx), %rsi
movq %rbx, %rdi
callq 0x23c76
movl 0x200(%rbx), %eax
cmpl $0x4, %eax
je 0x24c4a
cmpl $0x3, %eax
jne 0x24ea1
movl 0x264(%rbx), %ecx
cmpq %rcx, 0x8(%rbx)
jb 0x24c41
cmpb $0x1, 0x268(%rbx)
jne 0x24c41
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 0x284bc
movq %r14, 0x8(%rsp)
movq 0x1f0(%rbx), %rsi
cmpq 0x1f8(%rbx), %rsi
je 0x24da4
movq %r14, (%rsi)
addq $0x8, 0x1f0(%rbx)
jmp 0x24db5
cmpl $0x4, %eax
jne 0x24ea1
movl 0x264(%rbx), %eax
cmpq %rax, 0x8(%rbx)
jb 0x24ea1
cmpb $0x1, 0x268(%rbx)
jne 0x24ea1
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 0x284bc
movq %r14, 0x8(%rsp)
movq 0x1f0(%rbx), %rsi
cmpq 0x1f8(%rbx), %rsi
je 0x24cb8
movq %r14, (%rsi)
addq $0x8, 0x1f0(%rbx)
jmp 0x24cc9
leaq 0x1e8(%rbx), %rdi
leaq 0x8(%rsp), %rdx
callq 0x1ac5a
leaq 0x177b8(%rip), %rax     # 0x3c488
cmpb $0x1, (%rax)
jne 0x24e9a
leaq 0x177a9(%rip), %rax     # 0x3c489
cmpb $0x0, (%rax)
jne 0x24e9a
movq 0x172e0(%rip), %rax     # 0x3bfd0
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 0x24d2e
movq %r14, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x20, 0xe0(%r14)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq 0x1728b(%rip), %rdi     # 0x3bfd0
callq 0x4340
movq %rax, %r14
leaq 0xe99c(%rip), %rax      # 0x336f0
leaq 0xe99c(%rip), %rsi      # 0x336f7
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r14, %rdi
callq 0x4340
leaq 0xedc3(%rip), %rsi      # 0x33b3d
movl $0x2c, %edx
jmp 0x24e6b
movl %esi, %ecx
subl %eax, %ecx
leaq 0x1772d(%rip), %rax     # 0x3c4bc
cmpl (%rax), %ecx
jge 0x247df
movq %rbx, %rdi
callq 0x24f8a
jmp 0x247df
leaq 0x1e8(%rbx), %rdi
leaq 0x8(%rsp), %rdx
callq 0x1ac5a
leaq 0x176cc(%rip), %rax     # 0x3c488
cmpb $0x1, (%rax)
jne 0x24e9a
leaq 0x176bd(%rip), %rax     # 0x3c489
cmpb $0x0, (%rax)
jne 0x24e9a
movq 0x171f4(%rip), %rax     # 0x3bfd0
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 0x24e1a
movq %r14, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x20, 0xe0(%r14)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq 0x1719f(%rip), %rdi     # 0x3bfd0
callq 0x4340
movq %rax, %r14
leaq 0xe8b0(%rip), %rax      # 0x336f0
leaq 0xe8b0(%rip), %rsi      # 0x336f7
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r14, %rdi
callq 0x4340
leaq 0xeca8(%rip), %rsi      # 0x33b0e
movl $0x2e, %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 0x2653a
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xda20(%rip), %rdi      # 0x328df
leaq 0xda60(%rip), %rsi      # 0x32926
movq %rax, %rdx
xorl %eax, %eax
callq 0x4410
movq 0x17119(%rip), %rdi     # 0x3bff0
leaq 0xc144(%rip), %rsi      # 0x31022
callq 0x42e0
leaq 0xe989(%rip), %rsi      # 0x33873
movq %rax, %rdi
callq 0x42e0
leaq 0xc93e(%rip), %rsi      # 0x31837
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
movl $0x128, %esi            # imm = 0x128
callq 0x4500
leaq 0xc12c(%rip), %rsi      # 0x31041
movq %rax, %rdi
callq 0x42e0
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x4290
leaq 0xeb5c(%rip), %rsi      # 0x33a8c
movq %rax, %rdi
callq 0x42e0
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x40c0
leaq 0xf516(%rip), %rsi      # 0x34461
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
callq 0x4170
xorl %edi, %edi
callq 0x4360
jmp 0x24f64
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %r14, %rdi
jmp 0x24f7c
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %r12, %rdi
callq 0x4300
movq %rbx, %rdi
callq 0x4540
nop