// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movb $0x0, 0x240(%rdi)
leaq 0x17c02(%rip), %r14     # 0x3b4d8
cmpl $0x0, (%r14)
jle 0x23993
leaq 0x1e8(%rbx), %r15
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
movl $0x468, %edi            # imm = 0x468
callq 0x42f0
movq %rax, %r13
movq $0x0, (%rsp)
movq %rax, %rdi
movl $0x2, %esi
movl $0x3, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x27c44
movq %r13, 0x10(%rsp)
movq 0x18(%rsp), %rax
movl 0x42c(%rax), %eax
movl %eax, 0x42c(%r13)
movl 0x20c(%rbx), %eax
movl %eax, 0x44c(%r13)
movl $0x2, 0x448(%r13)
cmpb $0x1, 0x1cc(%rbx)
jne 0x23960
movl $0x0, 0x14(%r13)
movq 0x1f0(%rbx), %rsi
cmpq 0x1f8(%rbx), %rsi
je 0x2397d
movq %r13, (%rsi)
addq $0x8, 0x1f0(%rbx)
jmp 0x23988
movq %r15, %rdi
movq %r12, %rdx
callq 0x1a462
incl %ebp
cmpl (%r14), %ebp
jl 0x238ee
movl $0x0, 0x238(%rbx)
movq 0x1d0(%rbx), %rdx
movq 0x1d8(%rbx), %rax
cmpq %rdx, %rax
je 0x23a0c
leaq 0x220(%rbx), %r15
xorl %r14d, %r14d
xorl %r12d, %r12d
addq %r14, %rdx
movq 0x228(%rbx), %rsi
cmpq 0x230(%rbx), %rsi
je 0x239e0
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x228(%rbx)
jmp 0x239e8
movq %r15, %rdi
callq 0x1a462
incq %r12
movq 0x1d0(%rbx), %rdx
movq 0x1d8(%rbx), %rax
movq %rax, %rcx
subq %rdx, %rcx
sarq $0x3, %rcx
addq $0x8, %r14
cmpq %r12, %rcx
ja 0x239bd
cmpq %rdx, %rax
je 0x23a18
movq %rdx, 0x1d8(%rbx)
movl 0x25c(%rbx), %ebp
movq 0x270(%rbx), %rax
cmpq $0x0, (%rax,%rbp,8)
je 0x23cb6
cmpl 0x260(%rbx), %ebp
je 0x23d18
leaq 0x1d0(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x17a6d(%rip), %r14     # 0x3b4bc
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
movl %ebp, %ebp
movq 0x270(%rbx), %r12
cmpq $0x0, (%r12,%rbp,8)
je 0x23aca
movl $0x468, %edi            # imm = 0x468
callq 0x42f0
movq %rax, %r13
movq (%r12,%rbp,8), %rsi
movq %rax, %rdi
callq 0x27f1e
movq %r13, 0x10(%rsp)
movzbl 0x1cc(%rbx), %eax
xorl $0x1, %eax
movl %eax, 0x14(%r13)
cmpl $0x2, 0x10(%r13)
je 0x23aa5
addl 0x430(%r13), %r15d
movq 0x270(%rbx), %rax
movq (%rax,%rbp,8), %rdi
testq %rdi, %rdi
je 0x23ac2
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x270(%rbx), %rax
movq $0x0, (%rax,%rbp,8)
movq 0x1d8(%rbx), %rsi
cmpq 0x1e0(%rbx), %rsi
je 0x23aec
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x1d8(%rbx)
jmp 0x23afb
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x1a462
incl %ebp
movl (%r14), %eax
cmpl %eax, %ebp
movl $0x0, %ecx
cmovbl %ecx, %eax
subl %eax, %ebp
cmpl 0x260(%rbx), %ebp
jne 0x23a52
addl %r15d, 0x208(%rbx)
movl 0x25c(%rbx), %eax
movl %eax, 0x260(%rbx)
movl $0xb, 0x200(%rbx)
leaq 0x20(%rbx), %r15
leaq 0xf9a1(%rip), %rsi      # 0x334e1
movl $0x1, %ecx
movq %r15, %rdi
xorl %edx, %edx
callq 0x4510
movq 0x28(%rbx), %rcx
cmpq %rax, %rcx
jb 0x23daa
movq %rax, 0x28(%rbx)
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
leaq 0xeef0(%rip), %rsi      # 0x32a5f
movq %r15, %rdi
callq 0x4580
leaq 0x1790a(%rip), %rax     # 0x3b488
cmpb $0x0, (%rax)
je 0x23ca7
leaq 0x178fb(%rip), %rax     # 0x3b489
cmpb $0x0, (%rax)
jne 0x23ca7
movq 0x17432(%rip), %rax     # 0x3afd0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x12, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %r15
cmpb $0x0, 0xe1(%rax,%rcx)
jne 0x23bd5
movq %r15, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%r15)
movb $0x20, 0xe0(%r15)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq 0x173e4(%rip), %rdi     # 0x3afd0
callq 0x4340
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rcx
movq $0xf, 0x10(%r15,%rcx)
movq -0x18(%rax), %rax
leaq (%r15,%rax), %r12
cmpb $0x0, 0xe1(%r15,%rax)
jne 0x23c2d
movq %r12, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%r12)
movb $0x20, 0xe0(%r12)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x28270
movq %rax, %r14
leaq 0xeb2b(%rip), %rax      # 0x32778
leaq 0xeb2b(%rip), %rsi      # 0x3277f
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r14, %rdi
callq 0x4340
leaq 0xee59(%rip), %rsi      # 0x32acc
movl $0x20, %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
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17333(%rip), %rdi     # 0x3aff0
leaq 0xc35e(%rip), %rsi      # 0x30022
callq 0x42e0
leaq 0xec1f(%rip), %rsi      # 0x328ef
movq %rax, %rdi
callq 0x42e0
leaq 0xcbb8(%rip), %rsi      # 0x30897
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
movl $0xe4, %esi
callq 0x4500
leaq 0xc346(%rip), %rsi      # 0x30041
movq %rax, %rdi
callq 0x42e0
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x4290
leaq 0xece3(%rip), %rsi      # 0x329f9
jmp 0x23d78
movq 0x172d1(%rip), %rdi     # 0x3aff0
leaq 0xc2fc(%rip), %rsi      # 0x30022
callq 0x42e0
leaq 0xebbd(%rip), %rsi      # 0x328ef
movq %rax, %rdi
callq 0x42e0
leaq 0xcb56(%rip), %rsi      # 0x30897
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
movl $0x102, %esi            # imm = 0x102
callq 0x4500
leaq 0xc2e4(%rip), %rsi      # 0x30041
movq %rax, %rdi
callq 0x42e0
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x4290
leaq 0xecef(%rip), %rsi      # 0x32a67
movq %rax, %rdi
callq 0x42e0
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x40c0
leaq 0xf74e(%rip), %rsi      # 0x334e1
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
callq 0x4170
xorl %edi, %edi
callq 0x4360
leaq 0xdb8e(%rip), %rdi      # 0x3193f
leaq 0xdbce(%rip), %rsi      # 0x31986
movq %rax, %rdx
xorl %eax, %eax
callq 0x4410
jmp 0x23dc4
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %r13, %rdi
callq 0x4300
movq %rbx, %rdi
callq 0x4540