// File: estwings57[P]CasHMC/sources/LinkMaster.cpp
// Function: CasHMC::LinkMaster::LinkRetry(CasHMC::Packet*)
void LinkMaster::LinkRetry(Packet *packet)
{
	//linkRxTx and token count reset (packets in linkRxTx are already saved in retry buffer)
	startCRC = 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 = packet->TAG;
		packetIRTRY->RRP = lastestRRP;
		packetIRTRY->FRP = 2;		//ClearErrorAbort flag is set with FRP[1] = 1
		if(downstream)	packetIRTRY->bufPopDelay = 0;
		linkRxTx.push_back(packetIRTRY);
	}
	
	//Initialize sequence number and backup buffer packets
	masterSEQ = 0;
	for(int i=0; i<Buffers.size(); i++) {
		backupBuffers.push_back(Buffers[i]);
	}
	Buffers.clear();
	
	//Transmits the packets currently being saved for retransmission
	unsigned retryToken = 0;
	unsigned tempReadP = retBufReadP;
	if(retryBuffers[retBufReadP] == NULL) {
		ERROR(header<<"  == Error - The first retry packet is NULL (It could be one of virtual tail packet  (CurrentClock : "<<currentClockCycle<<")");
		exit(0);
	}
	if(retBufReadP != retBufWriteP) {
		do {
			Packet *retryPacket = NULL;
			if(retryBuffers[tempReadP] != NULL) {
				retryPacket = new Packet(*retryBuffers[tempReadP]);
				if(downstream)	retryPacket->bufPopDelay = 0;
				else			retryPacket->bufPopDelay = 1;
				if(retryPacket->packetType != FLOW) {
					retryToken += retryPacket->LNG;
				}
				delete retryBuffers[tempReadP];
				retryBuffers[tempReadP] = NULL;
			}
			else {
				retryPacket = NULL;
			}
			Buffers.push_back(retryPacket);
			tempReadP++;
			tempReadP = (tempReadP < MAX_RETRY_BUF) ? tempReadP : tempReadP - MAX_RETRY_BUF;
		} while(tempReadP != retBufWriteP);
		tokenCount += retryToken;
		retBufWriteP = retBufReadP;
		currentState = LINK_RETRY;
		header.erase(header.find(")"));
		header += ") LK_RT";
	}
	else {
		ERROR(header<<"  == Error - Retry buffer has no packets to be transmitted for LINK RETRY sequence  (CurrentClock : "<<currentClockCycle<<")");
		exit(0);
	}
	DEBUG(ALI(18)<<header<<ALI(15)<<*packet<<(downstream ? "Down) " : "Up)   ")<<"LINK RETRY sequence is initiated");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x58(%rbp)
movb $0x0, 0x240(%rax)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
leaq 0x28a25(%rip), %rcx     # 0x66578
cmpl (%rcx), %eax
jge 0x3dc2b
movl $0x468, %edi            # imm = 0x468
callq 0x4390
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x60(%rbp)
movq %rsp, %rax
movq $0x0, (%rax)
xorl %r8d, %r8d
movl %r8d, %ecx
movl $0x2, %esi
movl $0x3, %edx
movl $0x1, %r9d
callq 0x43a90
jmp 0x3db96
movq -0x58(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rcx
movl 0x42c(%rcx), %edx
movq -0x20(%rbp), %rcx
movl %edx, 0x42c(%rcx)
movl 0x20c(%rax), %edx
movq -0x20(%rbp), %rcx
movl %edx, 0x44c(%rcx)
movq -0x20(%rbp), %rcx
movl $0x2, 0x448(%rcx)
testb $0x1, 0x1cc(%rax)
je 0x3dc09
movq -0x20(%rbp), %rax
movl $0x0, 0x14(%rax)
jmp 0x3dc09
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x468, %esi            # imm = 0x468
callq 0x43a0
jmp 0x3e167
movq -0x58(%rbp), %rdi
addq $0x1e8, %rdi            # imm = 0x1E8
leaq -0x20(%rbp), %rsi
callq 0x29ef0
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3db49
movq -0x58(%rbp), %rax
movl $0x0, 0x238(%rax)
movl $0x0, -0x30(%rbp)
movq -0x58(%rbp), %rdi
movslq -0x30(%rbp), %rax
movq %rax, -0x68(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
callq 0x22d60
movq %rax, %rcx
movq -0x68(%rbp), %rax
cmpq %rcx, %rax
jae 0x3dc9c
movq -0x58(%rbp), %rdi
movq %rdi, %rax
addq $0x220, %rax            # imm = 0x220
movq %rax, -0x70(%rbp)
addq $0x1d0, %rdi            # imm = 0x1D0
movslq -0x30(%rbp), %rsi
callq 0x29940
movq -0x70(%rbp), %rdi
movq %rax, %rsi
callq 0x29ef0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x3dc40
movq -0x58(%rbp), %rdi
addq $0x1d0, %rdi            # imm = 0x1D0
callq 0x29920
movq -0x58(%rbp), %rax
movl $0x0, -0x34(%rbp)
movl 0x25c(%rax), %ecx
movl %ecx, -0x38(%rbp)
movq %rax, %rdi
addq $0x270, %rdi            # imm = 0x270
movl 0x25c(%rax), %eax
movl %eax, %esi
callq 0x29940
cmpq $0x0, (%rax)
jne 0x3dd82
movq 0x28308(%rip), %rdi     # 0x65ff0
leaq 0x1334b(%rip), %rsi     # 0x5103a
callq 0x4380
movq %rax, %rdi
leaq 0x15aac(%rip), %rsi     # 0x537aa
callq 0x4380
movq %rax, %rdi
leaq 0x13b28(%rip), %rsi     # 0x51835
callq 0x4380
movq %rax, %rdi
movl $0xe4, %esi
callq 0x4610
movq %rax, %rdi
leaq 0x13330(%rip), %rsi     # 0x51059
callq 0x4380
movq -0x58(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, %rdi
leaq 0x15b6c(%rip), %rsi     # 0x538b4
callq 0x4380
movq %rax, %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x40f0
movq %rax, %rdi
leaq 0x16612(%rip), %rsi     # 0x54379
callq 0x4380
movq %rax, %rdi
movq 0x2823a(%rip), %rsi     # 0x65fb0
callq 0x43c0
xorl %edi, %edi
callq 0x4460
movq -0x58(%rbp), %rcx
movl 0x25c(%rcx), %eax
cmpl 0x260(%rcx), %eax
je 0x3df92
jmp 0x3dd9a
movq -0x58(%rbp), %rdi
movq $0x0, -0x40(%rbp)
addq $0x270, %rdi            # imm = 0x270
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x29940
cmpq $0x0, (%rax)
je 0x3deac
movl $0x468, %edi            # imm = 0x468
callq 0x4390
movq -0x58(%rbp), %rdi
movq %rax, -0x78(%rbp)
movq %rax, -0x80(%rbp)
addq $0x270, %rdi            # imm = 0x270
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x29940
movq -0x78(%rbp), %rdi
movq (%rax), %rsi
callq 0x43ff0
jmp 0x3ddf6
movq -0x58(%rbp), %rax
movq -0x80(%rbp), %rcx
movq %rcx, -0x40(%rbp)
testb $0x1, 0x1cc(%rax)
je 0x3de37
movq -0x40(%rbp), %rax
movl $0x0, 0x14(%rax)
jmp 0x3de42
movq -0x80(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x468, %esi            # imm = 0x468
callq 0x43a0
jmp 0x3e167
movq -0x40(%rbp), %rax
movl $0x1, 0x14(%rax)
movq -0x40(%rbp), %rax
cmpl $0x2, 0x10(%rax)
je 0x3de5c
movq -0x40(%rbp), %rax
movl 0x430(%rax), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x58(%rbp), %rdi
addq $0x270, %rdi            # imm = 0x270
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x29940
movq (%rax), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x3de8e
movq -0x88(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq -0x58(%rbp), %rdi
addq $0x270, %rdi            # imm = 0x270
movl -0x38(%rbp), %eax
movl %eax, %esi
callq 0x29940
movq $0x0, (%rax)
jmp 0x3deb4
movq $0x0, -0x40(%rbp)
movq -0x58(%rbp), %rdi
addq $0x1d0, %rdi            # imm = 0x1D0
leaq -0x40(%rbp), %rsi
callq 0x29ef0
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %eax
leaq 0x28681(%rip), %rcx     # 0x6655c
cmpl (%rcx), %eax
jae 0x3deea
movl -0x38(%rbp), %eax
movl %eax, -0x8c(%rbp)
jmp 0x3defc
movl -0x38(%rbp), %eax
leaq 0x28668(%rip), %rcx     # 0x6655c
subl (%rcx), %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x58(%rbp), %rcx
movl -0x38(%rbp), %eax
cmpl 0x260(%rcx), %eax
jne 0x3dd9a
movq -0x58(%rbp), %rdi
movl -0x34(%rbp), %eax
addl 0x208(%rdi), %eax
movl %eax, 0x208(%rdi)
movl 0x25c(%rdi), %eax
movl %eax, 0x260(%rdi)
movl $0xb, 0x200(%rdi)
movq %rdi, %rax
addq $0x20, %rax
movq %rax, -0x98(%rbp)
addq $0x20, %rdi
leaq 0x1641f(%rip), %rsi     # 0x54379
xorl %eax, %eax
movl %eax, %edx
callq 0x46d0
movq -0x98(%rbp), %rdi
movq %rax, %rsi
movq $-0x1, %rdx
callq 0x40e0
movq -0x58(%rbp), %rdi
addq $0x20, %rdi
leaq 0x15992(%rip), %rsi     # 0x5391a
callq 0x4350
jmp 0x3e033
movq 0x28057(%rip), %rdi     # 0x65ff0
leaq 0x1309a(%rip), %rsi     # 0x5103a
callq 0x4380
movq %rax, %rdi
leaq 0x157fb(%rip), %rsi     # 0x537aa
callq 0x4380
movq %rax, %rdi
leaq 0x13877(%rip), %rsi     # 0x51835
callq 0x4380
movq %rax, %rdi
movl $0x102, %esi            # imm = 0x102
callq 0x4610
movq %rax, %rdi
leaq 0x1307f(%rip), %rsi     # 0x51059
callq 0x4380
movq -0x58(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, %rdi
leaq 0x15929(%rip), %rsi     # 0x53922
callq 0x4380
movq %rax, %rdi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x40f0
movq %rax, %rdi
leaq 0x16361(%rip), %rsi     # 0x54379
callq 0x4380
movq %rax, %rdi
movq 0x27f89(%rip), %rsi     # 0x65fb0
callq 0x43c0
xorl %edi, %edi
callq 0x4460
leaq 0x284ee(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x3e15e
leaq 0x284df(%rip), %rax     # 0x66529
testb $0x1, (%rax)
jne 0x3e15e
movl $0x12, %edi
callq 0x177b0
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %esi
movq 0x27f66(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0xb8(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xb8(%rbp), %rdi
movb %al, -0x45(%rbp)
movzbl -0x45(%rbp), %esi
callq 0x4550
movq -0x58(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0xb0(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0xb0(%rbp), %rdi
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %esi
callq 0x4440
movq %rax, -0xa8(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xa8(%rbp), %rdi
movb %al, -0x4d(%rbp)
movzbl -0x4d(%rbp), %esi
callq 0x4550
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x44960
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0xa0(%rbp)
testb $0x1, 0x1cc(%rax)
je 0x3e11d
leaq 0x15534(%rip), %rax     # 0x53648
movq %rax, -0xc0(%rbp)
jmp 0x3e12d
leaq 0x1552b(%rip), %rax     # 0x5364f
movq %rax, -0xc0(%rbp)
jmp 0x3e12d
movq -0xa0(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0x4380
movq %rax, %rdi
leaq 0x1583d(%rip), %rsi     # 0x53987
callq 0x4380
movq %rax, %rdi
movq 0x27e57(%rip), %rsi     # 0x65fb0
callq 0x43c0
addq $0xd0, %rsp
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x4630