// File: estwings57[P]CasHMC/sources/LinkMaster.cpp
// Function: CasHMC::LinkMaster::UpdateField(int, CasHMC::Packet*)
void LinkMaster::UpdateField(int nextWriteP, Packet *packet)
{
	//Sequence number updated
	packet->SEQ = (masterSEQ<7) ? masterSEQ++ : 0;
	//Update retry buffer pointer field
	packet->RRP = lastestRRP;
	packet->FRP = (nextWriteP < MAX_RETRY_BUF) ? nextWriteP : nextWriteP - MAX_RETRY_BUF;
	//CRC field updated
	startCRC = false;
	if(CRC_CHECK)	packet->CRC = packet->GetCRC();
	//Decrement the token count register by the number of packet in the transmitted packet
	if(packet->packetType != FLOW) {
		tokenCount -= packet->LNG;
		DEBUG(ALI(18)<<header<<ALI(15)<<*packet<<(downstream ? "Down) " : "Up)   ")
			<<"Decreased TOKEN : "<<packet->LNG<<" (remaining : "<<tokenCount<<"/"<<MAX_LINK_BUF<<")");
	}
	//Save packet in retry buffer
	Packet *retryPacket = new Packet(*packet);
	if(retryBuffers[retBufWriteP] == NULL) {
		retryBuffers[retBufWriteP] = retryPacket;
	}
	else {
		ERROR(header<<"  == Error - retryBuffers["<<retBufWriteP<<"] is not NULL  (CurrentClock : "<<currentClockCycle<<")");
		exit(0);
	}
	for(int i=1; i<packet->LNG; i++) {
		retryBuffers[(retBufWriteP+i<MAX_RETRY_BUF ? retBufWriteP+i : retBufWriteP+i-MAX_RETRY_BUF)] = NULL;
	}
	DEBUG(ALI(18)<<header<<ALI(15)<<*packet<<(downstream ? "Down) " : "Up)   ")
			<<"RETRY BUFFER["<<retBufWriteP<<"] (FRP : "<<packet->FRP<<")");
	retBufWriteP = packet->FRP;
	//Send packet to standby buffer where the packet is ready to be transmitted
	packet->bufPopDelay = 1;
	linkRxTx.push_back(packet);
	//DEBUG(ALI(18)<<header<<ALI(15)<<*Buffers[0]<<(downstream ? "Down) " : "Up)   ")
	//			<<"SENDING packet to link "<<linkMasterID<<" (LK_"<<(downstream ? "D" : "U")<<linkMasterID<<")");
	Buffers.erase(Buffers.begin(), Buffers.begin()+packet->LNG);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpl $0x7, 0x238(%rax)
jge 0x3efca
movq -0x88(%rbp), %rcx
movl 0x238(%rcx), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, 0x238(%rcx)
movl %eax, -0x8c(%rbp)
jmp 0x3efd4
xorl %eax, %eax
movl %eax, -0x8c(%rbp)
jmp 0x3efd4
movq -0x88(%rbp), %rax
movl -0x8c(%rbp), %edx
movq -0x18(%rbp), %rcx
movl %edx, 0x444(%rcx)
movl 0x20c(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x44c(%rax)
movl -0xc(%rbp), %eax
leaq 0x27557(%rip), %rcx     # 0x6655c
cmpl (%rcx), %eax
jge 0x3f014
movl -0xc(%rbp), %eax
movl %eax, -0x90(%rbp)
jmp 0x3f026
movl -0xc(%rbp), %eax
leaq 0x2753e(%rip), %rcx     # 0x6655c
subl (%rcx), %eax
movl %eax, -0x90(%rbp)
movq -0x88(%rbp), %rax
movl -0x90(%rbp), %edx
movq -0x18(%rbp), %rcx
movl %edx, 0x448(%rcx)
movb $0x0, 0x240(%rax)
leaq 0x27521(%rip), %rax     # 0x6656c
testb $0x1, (%rax)
je 0x3f065
movq -0x18(%rbp), %rdi
callq 0x44270
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x438(%rax)
movq -0x18(%rbp), %rax
cmpl $0x2, 0x10(%rax)
je 0x3f22a
movq -0x88(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x430(%rcx), %edx
movl 0x208(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x208(%rax)
leaq 0x2748f(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x3f228
leaq 0x27480(%rip), %rax     # 0x66529
testb $0x1, (%rax)
jne 0x3f228
movl $0x12, %edi
callq 0x177b0
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %esi
movq 0x26f07(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0xb0(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xb0(%rbp), %rdi
movb %al, -0x1d(%rbp)
movzbl -0x1d(%rbp), %esi
callq 0x4550
movq -0x88(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0xa8(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0xa8(%rbp), %rdi
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %esi
callq 0x4440
movq %rax, -0xa0(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xa0(%rbp), %rdi
movb %al, -0x25(%rbp)
movzbl -0x25(%rbp), %esi
callq 0x4550
movq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x44960
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, -0x98(%rbp)
testb $0x1, 0x1cc(%rax)
je 0x3f182
leaq 0x144cf(%rip), %rax     # 0x53648
movq %rax, -0xb8(%rbp)
jmp 0x3f192
leaq 0x144c6(%rip), %rax     # 0x5364f
movq %rax, -0xb8(%rbp)
jmp 0x3f192
movq -0x98(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x4380
movq %rax, %rdi
leaq 0x1490c(%rip), %rsi     # 0x53abb
callq 0x4380
movq %rax, %rdi
movq -0x18(%rbp), %rax
movl 0x430(%rax), %esi
callq 0x44c0
movq %rax, %rdi
leaq 0x148fe(%rip), %rsi     # 0x53ace
callq 0x4380
movq %rax, %rdi
movq -0x88(%rbp), %rax
movl 0x208(%rax), %esi
callq 0x44c0
movq %rax, %rdi
leaq 0x120a4(%rip), %rsi     # 0x51298
callq 0x4380
movq %rax, %rdi
leaq 0x27355(%rip), %rax     # 0x66558
movl (%rax), %esi
callq 0x4610
movq %rax, %rdi
leaq 0x15165(%rip), %rsi     # 0x54379
callq 0x4380
movq %rax, %rdi
movq 0x26d8d(%rip), %rsi     # 0x65fb0
callq 0x43c0
jmp 0x3f22a
movl $0x468, %edi            # imm = 0x468
callq 0x4390
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0xc0(%rbp)
movq -0x18(%rbp), %rsi
callq 0x43ff0
jmp 0x3f24c
movq -0x88(%rbp), %rax
movq -0xc0(%rbp), %rcx
movq %rcx, -0x30(%rbp)
movq %rax, %rdi
addq $0x270, %rdi            # imm = 0x270
movl 0x260(%rax), %eax
movl %eax, %esi
callq 0x29940
cmpq $0x0, (%rax)
jne 0x3f2d5
movq -0x88(%rbp), %rax
movq -0x30(%rbp), %rcx
movq %rcx, -0xc8(%rbp)
movq %rax, %rdi
addq $0x270, %rdi            # imm = 0x270
movl 0x260(%rax), %eax
movl %eax, %esi
callq 0x29940
movq -0xc8(%rbp), %rcx
movq %rcx, (%rax)
jmp 0x3f3a0
movq -0xc0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x468, %esi            # imm = 0x468
callq 0x43a0
jmp 0x3f682
movq 0x26d14(%rip), %rdi     # 0x65ff0
leaq 0x11d57(%rip), %rsi     # 0x5103a
callq 0x4380
movq %rax, %rdi
leaq 0x144b8(%rip), %rsi     # 0x537aa
callq 0x4380
movq %rax, %rdi
leaq 0x12534(%rip), %rsi     # 0x51835
callq 0x4380
movq %rax, %rdi
movl $0x1b5, %esi            # imm = 0x1B5
callq 0x4610
movq %rax, %rdi
leaq 0x11d3c(%rip), %rsi     # 0x51059
callq 0x4380
movq -0x88(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, %rdi
leaq 0x1479e(%rip), %rsi     # 0x53add
callq 0x4380
movq %rax, %rdi
movq -0x88(%rbp), %rax
movl 0x260(%rax), %esi
callq 0x44c0
movq %rax, %rdi
leaq 0x14795(%rip), %rsi     # 0x53af8
callq 0x4380
movq %rax, %rdi
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x40f0
movq %rax, %rdi
leaq 0x14ff4(%rip), %rsi     # 0x54379
callq 0x4380
movq %rax, %rdi
movq 0x26c1c(%rip), %rsi     # 0x65fb0
callq 0x43c0
xorl %edi, %edi
callq 0x4460
movl $0x1, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x430(%rcx), %eax
jae 0x3f448
movq -0x88(%rbp), %rax
movq %rax, %rcx
addq $0x270, %rcx            # imm = 0x270
movq %rcx, -0xd0(%rbp)
movl 0x260(%rax), %eax
addl -0x40(%rbp), %eax
leaq 0x2717a(%rip), %rcx     # 0x6655c
cmpl (%rcx), %eax
jae 0x3f3fe
movq -0x88(%rbp), %rax
movl 0x260(%rax), %eax
addl -0x40(%rbp), %eax
movl %eax, -0xd4(%rbp)
jmp 0x3f41d
movq -0x88(%rbp), %rax
movl 0x260(%rax), %eax
addl -0x40(%rbp), %eax
leaq 0x27147(%rip), %rcx     # 0x6655c
subl (%rcx), %eax
movl %eax, -0xd4(%rbp)
movq -0xd0(%rbp), %rdi
movl -0xd4(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
callq 0x29940
movq $0x0, (%rax)
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x3f3a7
leaq 0x270d9(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x3f5be
leaq 0x270ca(%rip), %rax     # 0x66529
testb $0x1, (%rax)
jne 0x3f5be
movl $0x12, %edi
callq 0x177b0
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %esi
movq 0x26b51(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0xf8(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xf8(%rbp), %rdi
movb %al, -0x45(%rbp)
movzbl -0x45(%rbp), %esi
callq 0x4550
movq -0x88(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0xf0(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0xf0(%rbp), %rdi
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %esi
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 %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x44960
movq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, -0xe0(%rbp)
testb $0x1, 0x1cc(%rax)
je 0x3f538
leaq 0x14119(%rip), %rax     # 0x53648
movq %rax, -0x100(%rbp)
jmp 0x3f548
leaq 0x14110(%rip), %rax     # 0x5364f
movq %rax, -0x100(%rbp)
jmp 0x3f548
movq -0xe0(%rbp), %rdi
movq -0x100(%rbp), %rsi
callq 0x4380
movq %rax, %rdi
leaq 0x145b3(%rip), %rsi     # 0x53b18
callq 0x4380
movq %rax, %rdi
movq -0x88(%rbp), %rax
movl 0x260(%rax), %esi
callq 0x44c0
movq %rax, %rdi
leaq 0x1459d(%rip), %rsi     # 0x53b26
callq 0x4380
movq %rax, %rdi
movq -0x18(%rbp), %rax
movl 0x448(%rax), %esi
callq 0x44c0
movq %rax, %rdi
leaq 0x14dcf(%rip), %rsi     # 0x54379
callq 0x4380
movq %rax, %rdi
movq 0x269f7(%rip), %rsi     # 0x65fb0
callq 0x43c0
movq -0x88(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x448(%rax), %eax
movl %eax, 0x260(%rdi)
movq -0x18(%rbp), %rax
movl $0x1, 0x14(%rax)
addq $0x1e8, %rdi            # imm = 0x1E8
leaq -0x18(%rbp), %rsi
callq 0x29ef0
movq -0x88(%rbp), %rdi
movq %rdi, %rax
addq $0x1d0, %rax            # imm = 0x1D0
movq %rax, -0x108(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
callq 0x299f0
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x29a60
movq -0x88(%rbp), %rdi
addq $0x1d0, %rdi            # imm = 0x1D0
callq 0x299f0
movq %rax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movl 0x430(%rax), %eax
movl %eax, %esi
leaq -0x78(%rbp), %rdi
callq 0x29a20
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x29a60
movq -0x108(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x29960
movq %rax, -0x80(%rbp)
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x4630
nopl (%rax,%rax)