// File: /workspace/github_cpp_makefile_star2_O1/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 0x18474(%rip), %r14     # 0x3c4d8
cmpl $0x0, (%r14)
jle 0x24121
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 0x284bc
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 0x240ee
movl $0x0, 0x14(%r13)
movq 0x1f0(%rbx), %rsi
cmpq 0x1f8(%rbx), %rsi
je 0x2410b
movq %r13, (%rsi)
addq $0x8, 0x1f0(%rbx)
jmp 0x24116
movq %r15, %rdi
movq %r12, %rdx
callq 0x1ac5a
incl %ebp
cmpl (%r14), %ebp
jl 0x2407c
movl $0x0, 0x238(%rbx)
movq 0x1d0(%rbx), %rdx
movq 0x1d8(%rbx), %rax
cmpq %rdx, %rax
je 0x2419a
leaq 0x220(%rbx), %r15
xorl %r14d, %r14d
xorl %r12d, %r12d
addq %r14, %rdx
movq 0x228(%rbx), %rsi
cmpq 0x230(%rbx), %rsi
je 0x2416e
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x228(%rbx)
jmp 0x24176
movq %r15, %rdi
callq 0x1ac5a
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 0x2414b
cmpq %rdx, %rax
je 0x241a6
movq %rdx, 0x1d8(%rbx)
movl 0x25c(%rbx), %ebp
movq 0x270(%rbx), %rax
cmpq $0x0, (%rax,%rbp,8)
je 0x2445e
cmpl 0x260(%rbx), %ebp
je 0x244c0
leaq 0x1d0(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x182df(%rip), %r14     # 0x3c4bc
xorl %r15d, %r15d
movq $0x0, 0x10(%rsp)
movl %ebp, %ebp
movq 0x270(%rbx), %r12
cmpq $0x0, (%r12,%rbp,8)
je 0x2425a
movl $0x468, %edi            # imm = 0x468
callq 0x42f0
movq %rax, %r13
movq (%r12,%rbp,8), %rsi
movq %rax, %rdi
callq 0x2879e
movq %r13, 0x10(%rsp)
movzbl 0x1cc(%rbx), %eax
xorl $0x1, %eax
movl %eax, 0x14(%r13)
cmpl $0x2, 0x10(%r13)
je 0x24233
addl 0x430(%r13), %r15d
movq 0x270(%rbx), %rax
movq (%rax,%rbp,8), %rdi
testq %rdi, %rdi
je 0x24249
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x270(%rbx), %rax
movq $0x0, (%rax,%rbp,8)
jmp 0x24263
movq $0x0, 0x10(%rsp)
movq 0x1d8(%rbx), %rsi
cmpq 0x1e0(%rbx), %rsi
je 0x24285
movq 0x10(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x1d8(%rbx)
jmp 0x24294
movq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x1ac5a
incl %ebp
movl (%r14), %eax
cmpl %eax, %ebp
movl $0x0, %ecx
cmovbl %ecx, %eax
subl %eax, %ebp
cmpl 0x260(%rbx), %ebp
jne 0x241e0
addl %r15d, 0x208(%rbx)
movl 0x25c(%rbx), %eax
movl %eax, 0x260(%rbx)
movl $0xb, 0x200(%rbx)
leaq 0x20(%rbx), %r15
leaq 0x10188(%rip), %rsi     # 0x34461
movl $0x1, %ecx
movq %r15, %rdi
xorl %edx, %edx
callq 0x4510
movq 0x28(%rbx), %rcx
cmpq %rax, %rcx
jb 0x24552
movq %rax, 0x28(%rbx)
movq 0x20(%rbx), %rcx
movb $0x0, (%rcx,%rax)
leaq 0xf6db(%rip), %rsi      # 0x339e3
movq %r15, %rdi
callq 0x4580
leaq 0x18171(%rip), %rax     # 0x3c488
cmpb $0x0, (%rax)
je 0x2444f
leaq 0x18162(%rip), %rax     # 0x3c489
cmpb $0x0, (%rax)
jne 0x2444f
movq 0x17c99(%rip), %rax     # 0x3bfd0
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 0x24375
movq %r15, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r15)
movb $0x1, 0xe1(%r15)
movb $0x20, 0xe0(%r15)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq 0x17c44(%rip), %rdi     # 0x3bfd0
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 0x243d5
movq %r12, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r12)
movb $0x1, 0xe1(%r12)
movb $0x20, 0xe0(%r12)
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x28bbe
movq %rax, %r14
leaq 0xf2fb(%rip), %rax      # 0x336f0
leaq 0xf2fb(%rip), %rsi      # 0x336f7
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rax, %rsi
movl $0x6, %edx
movq %r14, %rdi
callq 0x4340
leaq 0xf635(%rip), %rsi      # 0x33a50
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 0x17b8b(%rip), %rdi     # 0x3bff0
leaq 0xcbb6(%rip), %rsi      # 0x31022
callq 0x42e0
leaq 0xf3fb(%rip), %rsi      # 0x33873
movq %rax, %rdi
callq 0x42e0
leaq 0xd3b0(%rip), %rsi      # 0x31837
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
movl $0xe4, %esi
callq 0x4500
leaq 0xcb9e(%rip), %rsi      # 0x31041
movq %rax, %rdi
callq 0x42e0
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x4290
leaq 0xf4bf(%rip), %rsi      # 0x3397d
jmp 0x24520
movq 0x17b29(%rip), %rdi     # 0x3bff0
leaq 0xcb54(%rip), %rsi      # 0x31022
callq 0x42e0
leaq 0xf399(%rip), %rsi      # 0x33873
movq %rax, %rdi
callq 0x42e0
leaq 0xd34e(%rip), %rsi      # 0x31837
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
movl $0x102, %esi            # imm = 0x102
callq 0x4500
leaq 0xcb3c(%rip), %rsi      # 0x31041
movq %rax, %rdi
callq 0x42e0
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x4290
leaq 0xf4cb(%rip), %rsi      # 0x339eb
movq %rax, %rdi
callq 0x42e0
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x40c0
leaq 0xff26(%rip), %rsi      # 0x34461
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
callq 0x4170
xorl %edi, %edi
callq 0x4360
leaq 0xe386(%rip), %rdi      # 0x328df
leaq 0xe3c6(%rip), %rsi      # 0x32926
movq %rax, %rdx
xorl %eax, %eax
callq 0x4410
jmp 0x2456c
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %r13, %rdi
callq 0x4300
movq %rbx, %rdi
callq 0x4540