// File: /workspace/github_cpp_makefile_star2_O2/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 $0x18, %rsp
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
movb $0x0, 0x240(%rdi)
leaq 0x1e8(%rdi), %r15
xorl %r12d, %r12d
pushq $0x1
popq %r13
leaq 0x8(%rsp), %r14
leaq 0x16845(%rip), %rax     # 0x314d8
cmpl (%rax), %r12d
jge 0x1ad09
movl $0x468, %edi            # imm = 0x468
callq 0x4310
movq %rax, %rbp
andq $0x0, (%rsp)
movq %rax, %rdi
pushq $0x2
popq %rsi
pushq $0x3
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r13d, %r9d
callq 0x1dd04
movq %rbp, 0x8(%rsp)
movq 0x10(%rsp), %rax
movl 0x42c(%rax), %eax
movl %eax, 0x42c(%rbp)
movl 0x20c(%rbx), %eax
movl %eax, 0x44c(%rbp)
movl $0x2, 0x448(%rbp)
cmpb $0x1, 0x1cc(%rbx)
jne 0x1acf9
andl $0x0, 0x14(%rbp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x13442
incl %r12d
jmp 0x1ac8c
andl $0x0, 0x238(%rbx)
leaq 0x1d0(%rbx), %r15
leaq 0x220(%rbx), %r14
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x1d0(%rbx), %rsi
movq 0x1d8(%rbx), %rax
movq %rax, %rcx
subq %rsi, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0x1ad55
addq %r12, %rsi
movq %r14, %rdi
callq 0x13442
incq %r13
addq $0x8, %r12
jmp 0x1ad24
cmpq %rsi, %rax
je 0x1ad61
movq %rsi, 0x1d8(%rbx)
movl 0x25c(%rbx), %r13d
movq 0x270(%rbx), %rax
cmpq $0x0, (%rax,%r13,8)
je 0x1af38
cmpl 0x260(%rbx), %r13d
je 0x1af9a
xorl %ebp, %ebp
andq $0x0, 0x8(%rsp)
movl %r13d, %r13d
movq 0x270(%rbx), %r12
cmpq $0x0, (%r12,%r13,8)
je 0x1adfc
movl $0x468, %edi            # imm = 0x468
callq 0x4310
movq %rax, %r14
movq (%r12,%r13,8), %rsi
movq %rax, %rdi
callq 0x1df98
movq %r14, 0x8(%rsp)
movzbl 0x1cc(%rbx), %eax
xorl $0x1, %eax
movl %eax, 0x14(%r14)
cmpl $0x2, 0x10(%r14)
je 0x1adda
addl 0x430(%r14), %ebp
movq 0x270(%rbx), %rax
movq (%rax,%r13,8), %rdi
testq %rdi, %rdi
je 0x1adf7
movq (%rdi), %rax
callq *0x8(%rax)
movq 0x270(%rbx), %rax
andq $0x0, (%rax,%r13,8)
movq %r15, %rdi
leaq 0x8(%rsp), %rsi
callq 0x13442
incl %r13d
leaq 0x166a9(%rip), %rax     # 0x314bc
movl (%rax), %eax
cmpl %eax, %r13d
movl $0x0, %ecx
cmovbl %ecx, %eax
subl %eax, %r13d
cmpl 0x260(%rbx), %r13d
jne 0x1ad89
addl %ebp, 0x208(%rbx)
movl 0x25c(%rbx), %eax
movl %eax, 0x260(%rbx)
movl $0xb, 0x200(%rbx)
leaq 0x20(%rbx), %r14
leaq 0xc67a(%rip), %rsi      # 0x274d1
movq %r14, %rdi
xorl %edx, %edx
callq 0x4580
pushq $-0x1
popq %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x40b0
leaq 0xbbd9(%rip), %rsi      # 0x26a4f
movq %r14, %rdi
callq 0x4550
leaq 0x16603(%rip), %rax     # 0x31488
cmpb $0x0, (%rax)
je 0x1af29
leaq 0x165f4(%rip), %rax     # 0x31489
cmpb $0x0, (%rax)
jne 0x1af29
movq 0x1612b(%rip), %rdi     # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x12, 0x10(%rdi,%rax)
pushq $0x20
popq %rbp
movl %ebp, %esi
callq 0x4440
movq %rax, %rdi
movq %r14, %rsi
callq 0x42b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xf, 0x10(%rax,%rcx)
movq %rax, %rdi
movl %ebp, %esi
callq 0x4440
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x1e2c8
leaq 0xb870(%rip), %rcx      # 0x26768
leaq 0xb870(%rip), %rsi      # 0x2676f
cmpb $0x0, 0x1cc(%rbx)
cmovneq %rcx, %rsi
movq %rax, %rdi
callq 0x4300
leaq 0xbba3(%rip), %rsi      # 0x26abc
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x160b1(%rip), %rdi     # 0x30ff0
leaq 0x90dc(%rip), %rsi      # 0x24022
callq 0x4300
leaq 0xb98d(%rip), %rsi      # 0x268df
movq %rax, %rdi
callq 0x4300
leaq 0x9916(%rip), %rsi      # 0x24877
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movl $0xe4, %esi
callq 0x44f0
leaq 0x90c4(%rip), %rsi      # 0x24041
movq %rax, %rdi
callq 0x4300
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x42b0
leaq 0xba51(%rip), %rsi      # 0x269e9
jmp 0x1affa
movq 0x1604f(%rip), %rdi     # 0x30ff0
leaq 0x907a(%rip), %rsi      # 0x24022
callq 0x4300
leaq 0xb92b(%rip), %rsi      # 0x268df
movq %rax, %rdi
callq 0x4300
leaq 0x98b4(%rip), %rsi      # 0x24877
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movl $0x102, %esi            # imm = 0x102
callq 0x44f0
leaq 0x9062(%rip), %rsi      # 0x24041
movq %rax, %rdi
callq 0x4300
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x42b0
leaq 0xba5d(%rip), %rsi      # 0x26a57
movq %rax, %rdi
callq 0x4300
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x40d0
leaq 0xc4bc(%rip), %rsi      # 0x274d1
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
xorl %edi, %edi
callq 0x4370
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %r14, %rdi
jmp 0x1b044
movq %rax, %rbx
movl $0x468, %esi            # imm = 0x468
movq %rbp, %rdi
callq 0x4320
movq %rbx, %rdi
callq 0x4520
nop