// File: estwings57[P]CasHMC/sources/Link.cpp
// Function: CasHMC::Link::Update(bool)
void Link::Update(bool lastUpdate)
{
	//Transmit packet from buffer
	if(linkMasterP->linkRxTx.size()>0 && linkMasterP->linkRxTx[0]->bufPopDelay==0 && inFlightPacket==NULL) {
	//	if(linkMasterP->currentState == ACTIVE
	//	|| linkMasterP->currentState == LINK_RETRY
	//	|| (linkMasterP->currentState == START_RETRY && linkMasterP->linkRxTx[0]->CMD == IRTRY && linkMasterP->linkRxTx[0]->FRP == 1)
	//	|| ((linkMasterP->currentState == WAIT || linkMasterP->currentState == SLEEP) && linkMasterP->linkRxTx[0]->CMD == QUIET)
	//	|| linkMasterP->linkRxTx[0]->CMD == NULL_) {
			inFlightPacket = linkMasterP->linkRxTx[0];
			UpdateStatistic(inFlightPacket);
			inFlightCountdown = (inFlightPacket->LNG * 128) / LINK_WIDTH;
			DEBUG(ALI(18)<<header<<ALI(15)<<*inFlightPacket<<(downstream ? "Down) " : "Up)   ")<<"START transmission packet");
			linkMasterP->linkRxTx.erase(linkMasterP->linkRxTx.begin());
	//	}
	}
	
	if(inFlightCountdown > 0) {
		inFlightCountdown--;
		//Packet transmission done
		if(inFlightCountdown == 0) {
			NoisePacket(inFlightPacket);
			DEBUG(ALI(18)<<header<<ALI(15)<<*inFlightPacket<<(downstream ? "Down) " : "Up)   ")<<"DONE transmission packet");
			inFlightPacket->bufPopDelay = 1;
			linkSlaveP->linkRxTx.push_back(inFlightPacket);
			if(inFlightPacket->packetType == RESPONSE)
				inFlightPacket->trace->linkFullLat = linkSlaveP->currentClockCycle - inFlightPacket->trace->linkTransmitTime;
			inFlightPacket = NULL;
		}
		else if(lastUpdate) {
			DEBUG(ALI(18)<<header<<ALI(15)<<*inFlightPacket<<(downstream ? "Down) " : "Up)   ")<<"Link transmiting countdown : "<<ALI(2)<<inFlightCountdown);
		}
	}
	
	if(lastUpdate && linkMasterP->linkRxTx.size()>0) {
		for(int i=0; i<linkMasterP->linkRxTx.size(); i++) {
			linkMasterP->linkRxTx[i]->bufPopDelay = (linkMasterP->linkRxTx[i]->bufPopDelay>0) ? linkMasterP->linkRxTx[i]->bufPopDelay-1 : 0;
		}
	}
	Step();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movb %sil, %al
movq %rdi, -0x8(%rbp)
andb $0x1, %al
movb %al, -0x9(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0x1d8(%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
callq 0x22d60
cmpq $0x0, %rax
jbe 0x3b68d
movq -0x70(%rbp), %rax
movq 0x1d8(%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
xorl %eax, %eax
movl %eax, %esi
callq 0x29940
movq (%rax), %rax
cmpl $0x0, 0x14(%rax)
jne 0x3b68d
movq -0x70(%rbp), %rax
cmpq $0x0, 0x1f0(%rax)
jne 0x3b68d
movq -0x70(%rbp), %rax
movq 0x1d8(%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
xorl %eax, %eax
movl %eax, %esi
callq 0x29940
movq -0x70(%rbp), %rdi
movq (%rax), %rax
movq %rax, 0x1f0(%rdi)
movq 0x1f0(%rdi), %rsi
callq 0x3bb30
movq -0x70(%rbp), %rax
movq 0x1f0(%rax), %rax
movl 0x430(%rax), %eax
shll $0x7, %eax
leaq 0x2b046(%rip), %rcx     # 0x6654c
xorl %edx, %edx
divl (%rcx)
movl %eax, %ecx
movq -0x70(%rbp), %rax
movl %ecx, 0x1f8(%rax)
leaq 0x2b00b(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x3b63c
leaq 0x2affc(%rip), %rax     # 0x66529
testb $0x1, (%rax)
jne 0x3b63c
movl $0x12, %edi
callq 0x177b0
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %esi
movq 0x2aa83(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0x90(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0x90(%rbp), %rdi
movb %al, -0x11(%rbp)
movzbl -0x11(%rbp), %esi
callq 0x4550
movq -0x70(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0x88(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0x88(%rbp), %rdi
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %esi
callq 0x4440
movq %rax, -0x80(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0x80(%rbp), %rdi
movb %al, -0x19(%rbp)
movzbl -0x19(%rbp), %esi
callq 0x4550
movq %rax, %rdi
movq -0x70(%rbp), %rax
movq 0x1f0(%rax), %rsi
callq 0x44960
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0x78(%rbp)
testb $0x1, 0x1cc(%rax)
je 0x3b5fe
leaq 0x18053(%rip), %rax     # 0x53648
movq %rax, -0x98(%rbp)
jmp 0x3b60e
leaq 0x1804a(%rip), %rax     # 0x5364f
movq %rax, -0x98(%rbp)
jmp 0x3b60e
movq -0x78(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x4380
movq %rax, %rdi
leaq 0x1802e(%rip), %rsi     # 0x53656
callq 0x4380
movq %rax, %rdi
movq 0x2a979(%rip), %rsi     # 0x65fb0
callq 0x43c0
movq -0x70(%rbp), %rax
movq 0x1d8(%rax), %rcx
addq $0x1e8, %rcx            # imm = 0x1E8
movq %rcx, -0xa0(%rbp)
movq 0x1d8(%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
callq 0x299f0
movq %rax, -0x30(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x29a60
movq -0xa0(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x29e80
movq %rax, -0x38(%rbp)
movq -0x70(%rbp), %rax
cmpl $0x0, 0x1f8(%rax)
jbe 0x3ba1a
movq -0x70(%rbp), %rax
movl 0x1f8(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x1f8(%rax)
cmpl $0x0, 0x1f8(%rax)
jne 0x3b87f
movq -0x70(%rbp), %rdi
movq 0x1f0(%rdi), %rsi
callq 0x3bf60
leaq 0x2ae53(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x3b800
leaq 0x2ae44(%rip), %rax     # 0x66529
testb $0x1, (%rax)
jne 0x3b800
movl $0x12, %edi
callq 0x177b0
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %esi
movq 0x2a8cb(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0xc0(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xc0(%rbp), %rdi
movb %al, -0x3d(%rbp)
movzbl -0x3d(%rbp), %esi
callq 0x4550
movq -0x70(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0xb8(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0xb8(%rbp), %rdi
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %esi
callq 0x4440
movq %rax, -0xb0(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xb0(%rbp), %rdi
movb %al, -0x45(%rbp)
movzbl -0x45(%rbp), %esi
callq 0x4550
movq %rax, %rdi
movq -0x70(%rbp), %rax
movq 0x1f0(%rax), %rsi
callq 0x44960
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0xa8(%rbp)
testb $0x1, 0x1cc(%rax)
je 0x3b7bf
leaq 0x17e92(%rip), %rax     # 0x53648
movq %rax, -0xc8(%rbp)
jmp 0x3b7cf
leaq 0x17e89(%rip), %rax     # 0x5364f
movq %rax, -0xc8(%rbp)
jmp 0x3b7cf
movq -0xa8(%rbp), %rdi
movq -0xc8(%rbp), %rsi
callq 0x4380
movq %rax, %rdi
leaq 0x17e84(%rip), %rsi     # 0x53670
callq 0x4380
movq %rax, %rdi
movq 0x2a7b5(%rip), %rsi     # 0x65fb0
callq 0x43c0
movq -0x70(%rbp), %rsi
movq 0x1f0(%rsi), %rax
movl $0x1, 0x14(%rax)
movq 0x1e8(%rsi), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
addq $0x1f0, %rsi            # imm = 0x1F0
callq 0x29ef0
movq -0x70(%rbp), %rax
movq 0x1f0(%rax), %rax
cmpl $0x1, 0x10(%rax)
jne 0x3b86b
movq -0x70(%rbp), %rax
movq 0x1e8(%rax), %rcx
movq 0x8(%rcx), %rcx
movq 0x1f0(%rax), %rdx
movq 0x8(%rdx), %rdx
movl 0x10(%rdx), %edx
subq %rdx, %rcx
movq 0x1f0(%rax), %rax
movq 0x8(%rax), %rax
movl %ecx, 0x14(%rax)
movq -0x70(%rbp), %rax
movq $0x0, 0x1f0(%rax)
jmp 0x3ba18
testb $0x1, -0x9(%rbp)
je 0x3ba16
leaq 0x2ac98(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x3ba14
leaq 0x2ac89(%rip), %rax     # 0x66529
testb $0x1, (%rax)
jne 0x3ba14
movl $0x12, %edi
callq 0x177b0
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %esi
movq 0x2a710(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0xe8(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xe8(%rbp), %rdi
movb %al, -0x4d(%rbp)
movzbl -0x4d(%rbp), %esi
callq 0x4550
movq -0x70(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0xe0(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0xe0(%rbp), %rdi
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %esi
callq 0x4440
movq %rax, -0xd8(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xd8(%rbp), %rdi
movb %al, -0x55(%rbp)
movzbl -0x55(%rbp), %esi
callq 0x4550
movq %rax, %rdi
movq -0x70(%rbp), %rax
movq 0x1f0(%rax), %rsi
callq 0x44960
movq %rax, %rcx
movq -0x70(%rbp), %rax
movq %rcx, -0xd0(%rbp)
testb $0x1, 0x1cc(%rax)
je 0x3b97a
leaq 0x17cd7(%rip), %rax     # 0x53648
movq %rax, -0xf0(%rbp)
jmp 0x3b98a
leaq 0x17cce(%rip), %rax     # 0x5364f
movq %rax, -0xf0(%rbp)
jmp 0x3b98a
movq -0xd0(%rbp), %rdi
movq -0xf0(%rbp), %rsi
callq 0x4380
movq %rax, %rdi
leaq 0x17ce2(%rip), %rsi     # 0x53689
callq 0x4380
movq %rax, -0x100(%rbp)
movl $0x2, %edi
callq 0x177b0
movq -0x100(%rbp), %rdi
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %esi
callq 0x4440
movq %rax, -0xf8(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xf8(%rbp), %rdi
movb %al, -0x5d(%rbp)
movzbl -0x5d(%rbp), %esi
callq 0x4550
movq %rax, %rdi
movq -0x70(%rbp), %rax
movl 0x1f8(%rax), %esi
callq 0x44c0
movq %rax, %rdi
movq 0x2a5a1(%rip), %rsi     # 0x65fb0
callq 0x43c0
jmp 0x3ba16
jmp 0x3ba18
jmp 0x3ba1a
testb $0x1, -0x9(%rbp)
je 0x3bb1e
movq -0x70(%rbp), %rax
movq 0x1d8(%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
callq 0x22d60
cmpq $0x0, %rax
jbe 0x3bb1e
movl $0x0, -0x64(%rbp)
movq -0x70(%rbp), %rax
movslq -0x64(%rbp), %rcx
movq %rcx, -0x108(%rbp)
movq 0x1d8(%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
callq 0x22d60
movq %rax, %rcx
movq -0x108(%rbp), %rax
cmpq %rcx, %rax
jae 0x3bb1c
movq -0x70(%rbp), %rax
movq 0x1d8(%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
movslq -0x64(%rbp), %rsi
callq 0x29940
movq (%rax), %rax
cmpl $0x0, 0x14(%rax)
jle 0x3bad1
movq -0x70(%rbp), %rax
movq 0x1d8(%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
movslq -0x64(%rbp), %rsi
callq 0x29940
movq (%rax), %rax
movl 0x14(%rax), %eax
subl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x3badb
xorl %eax, %eax
movl %eax, -0x10c(%rbp)
jmp 0x3badb
movq -0x70(%rbp), %rax
movl -0x10c(%rbp), %ecx
movl %ecx, -0x110(%rbp)
movq 0x1d8(%rax), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
movslq -0x64(%rbp), %rsi
callq 0x29940
movl -0x110(%rbp), %ecx
movq (%rax), %rax
movl %ecx, 0x14(%rax)
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x3ba4c
jmp 0x3bb1e
movq -0x70(%rbp), %rdi
callq 0x22dc0
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq