// File: /workspace/github_cpp_makefile_star2_O2/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 0x1b201
movq (%rax), %rdx
testq %rdx, %rdx
je 0x1b6e1
cmpl $0x0, 0x14(%rdx)
jne 0x1b201
movq 0x1f0(%rbx), %rax
cmpq 0x1e8(%rbx), %rax
je 0x1b324
cmpl $0xb, 0x200(%rbx)
jne 0x1b3e9
movq 0x1d0(%rbx), %rax
cmpq 0x1d8(%rbx), %rax
jne 0x1b3e9
movq 0x1e8(%rbx), %rax
cmpq 0x1f0(%rbx), %rax
jne 0x1b3e9
cmpb $0x1, 0x250(%rbx)
jne 0x1b367
leaq 0x1e8(%rbx), %r14
movb $0x0, 0x250(%rbx)
leaq 0x20(%rbx), %r15
xorl %r13d, %r13d
leaq 0xb72f(%rip), %rbp      # 0x2698e
leaq 0x16272(%rip), %rax     # 0x314d8
cmpl (%rax), %r13d
jge 0x1b39f
movl $0x468, %edi            # imm = 0x468
callq 0x4310
movq %rax, %r12
andq $0x0, (%rsp)
movq %rax, %rdi
pushq $0x2
popq %rsi
pushq $0x3
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
pushq $0x1
popq %r9
callq 0x1dd04
movq %r12, 0x10(%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 0x1b2db
andl $0x0, 0x14(%r12)
movq %r14, %rdi
leaq 0x10(%rsp), %rsi
callq 0x13442
movl $0xa, 0x200(%rbx)
movq %r15, %rdi
leaq 0xc1d5(%rip), %rsi      # 0x274d1
xorl %edx, %edx
callq 0x4580
movq %r15, %rdi
movq %rax, %rsi
pushq $-0x1
popq %rdx
callq 0x40b0
movq %r15, %rdi
movq %rbp, %rsi
callq 0x4550
incl %r13d
jmp 0x1b25f
cmpl $0x2, 0x10(%rdx)
jne 0x1b332
movl 0x430(%rdx), %esi
jmp 0x1b344
movl 0x430(%rdx), %esi
cmpl %esi, 0x208(%rbx)
jb 0x1b201
movl 0x25c(%rbx), %eax
movl 0x260(%rbx), %ecx
addl %ecx, %esi
cmpl %eax, %ecx
jae 0x1b6c1
cmpl %eax, %esi
jge 0x1b201
jmp 0x1b6d4
andl $0x0, 0x200(%rbx)
leaq 0x20(%rbx), %r14
leaq 0xc158(%rip), %r15      # 0x274d1
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4580
pushq $-0x1
popq %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x40b0
movq %r14, %rdi
movq %r15, %rsi
callq 0x4550
leaq 0x1d0(%rbx), %r14
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x220(%rbx), %rsi
movq 0x228(%rbx), %rax
movq %rax, %rcx
subq %rsi, %rcx
sarq $0x3, %rcx
cmpq %r12, %rcx
jbe 0x1b3dd
addq %r15, %rsi
movq %r14, %rdi
callq 0x13442
incq %r12
addq $0x8, %r15
jmp 0x1b3ac
cmpq %rsi, %rax
je 0x1b3e9
movq %rsi, 0x228(%rbx)
movl 0x254(%rbx), %eax
cmpb $0x1, 0x251(%rbx)
jne 0x1b400
incl %eax
movl %eax, 0x254(%rbx)
leaq 0x160b5(%rip), %rcx     # 0x314bc
movl (%rcx), %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jbe 0x1b4e6
leaq 0x1606d(%rip), %rax     # 0x31488
cmpb $0x1, (%rax)
jne 0x1b4b4
leaq 0x1605e(%rip), %rax     # 0x31489
cmpb $0x0, (%rax)
jne 0x1b4b4
movq 0x15b95(%rip), %rdi     # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x21, 0x10(%rdi,%rax)
pushq $0x20
popq %rsi
callq 0x4440
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x42b0
leaq 0xb302(%rip), %rcx      # 0x26768
leaq 0xb302(%rip), %rsi      # 0x2676f
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x4300
leaq 0xb6cb(%rip), %rsi      # 0x26b52
movq %rax, %rdi
callq 0x4300
movl 0x258(%rbx), %esi
movq %rax, %rdi
callq 0x4210
leaq 0xc02d(%rip), %rsi      # 0x274d1
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
movl 0x258(%rbx), %eax
leaq 0x1601b(%rip), %rcx     # 0x314dc
cmpl (%rcx), %eax
jne 0x1b4cf
movq %rbx, %rdi
callq 0x1b052
jmp 0x1b4e6
incl %eax
movl %eax, 0x258(%rbx)
movq 0x248(%rbx), %rsi
movq %rbx, %rdi
callq 0x1aa4c
movl 0x200(%rbx), %eax
cmpl $0x4, %eax
je 0x1b5cf
cmpl $0x3, %eax
jne 0x1b6aa
movl 0x264(%rbx), %eax
cmpq %rax, 0x8(%rbx)
jb 0x1b6aa
cmpb $0x1, 0x268(%rbx)
jne 0x1b6aa
movl $0x468, %edi            # imm = 0x468
callq 0x4310
movq %rax, %r14
andq $0x0, (%rsp)
pushq $0x2
popq %rsi
pushq $0x1
popq %r9
movq %rax, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1dd04
leaq 0x10(%rsp), %rsi
movq %r14, (%rsi)
leaq 0x1e8(%rbx), %rdi
callq 0x13442
leaq 0x15f2a(%rip), %rax     # 0x31488
cmpb $0x1, (%rax)
jne 0x1b6a3
leaq 0x15f1b(%rip), %rax     # 0x31489
cmpb $0x0, (%rax)
jne 0x1b6a3
movq 0x15a52(%rip), %rdi     # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x21, 0x10(%rdi,%rax)
pushq $0x20
popq %rsi
callq 0x4440
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x42b0
leaq 0xb1bf(%rip), %rcx      # 0x26768
leaq 0xb1bf(%rip), %rsi      # 0x2676f
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x4300
leaq 0xb5b0(%rip), %rsi      # 0x26b7a
jmp 0x1b693
movl 0x264(%rbx), %eax
cmpq %rax, 0x8(%rbx)
jb 0x1b6aa
cmpb $0x1, 0x268(%rbx)
jne 0x1b6aa
movl $0x468, %edi            # imm = 0x468
callq 0x4310
movq %rax, %r14
andq $0x0, (%rsp)
pushq $0x2
popq %rsi
pushq $0x1
popq %r9
movq %rax, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1dd04
leaq 0x10(%rsp), %rsi
movq %r14, (%rsi)
leaq 0x1e8(%rbx), %rdi
callq 0x13442
leaq 0x15e59(%rip), %rax     # 0x31488
cmpb $0x1, (%rax)
jne 0x1b6a3
leaq 0x15e4e(%rip), %rax     # 0x31489
cmpb $0x0, (%rax)
jne 0x1b6a3
movq 0x15989(%rip), %rdi     # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x21, 0x10(%rdi,%rax)
pushq $0x20
popq %rsi
callq 0x4440
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x42b0
leaq 0xb0f6(%rip), %rcx      # 0x26768
leaq 0xb0f6(%rip), %rsi      # 0x2676f
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x4300
leaq 0xb516(%rip), %rsi      # 0x26ba9
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
movb $0x0, 0x268(%rbx)
movq %rbx, %rdi
callq 0x1c69e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %esi, %ecx
subl %eax, %ecx
leaq 0x15df0(%rip), %rax     # 0x314bc
cmpl (%rax), %ecx
jge 0x1b201
movq %rbx, %rdi
callq 0x1b79a
jmp 0x1b201
movq 0x15908(%rip), %rdi     # 0x30ff0
leaq 0x8933(%rip), %rsi      # 0x24022
callq 0x4300
leaq 0xb1e4(%rip), %rsi      # 0x268df
movq %rax, %rdi
callq 0x4300
leaq 0x916d(%rip), %rsi      # 0x24877
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movl $0x128, %esi            # imm = 0x128
callq 0x44f0
leaq 0x891b(%rip), %rsi      # 0x24041
movq %rax, %rdi
callq 0x4300
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x42b0
leaq 0xb3b7(%rip), %rsi      # 0x26af8
movq %rax, %rdi
callq 0x4300
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x40d0
leaq 0xbd75(%rip), %rsi      # 0x274d1
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
xorl %edi, %edi
callq 0x4370
jmp 0x1b775
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %r14, %rdi
jmp 0x1b78d
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %r12, %rdi
callq 0x4320
movq %rbx, %rdi
callq 0x4520