// File: gvaumour[P]cache-simulator/src/DBAMBPredictor.cc
// Function: DBAMBPredictor::evictPolicy(int, bool)
int
DBAMBPredictor::evictPolicy(int set, bool inNVM)
{	
	int assoc_victim = -1;
	assert(m_replacementPolicyNVM_ptr != NULL);
	assert(m_replacementPolicySRAM_ptr != NULL);

	CacheEntry* current = NULL;
	if(inNVM)
	{	
		assoc_victim = m_replacementPolicyNVM_ptr->evictPolicy(set);
		current = m_tableNVM[set][assoc_victim];
	}
	else
	{
		assoc_victim = m_replacementPolicySRAM_ptr->evictPolicy(set);
		current = m_tableSRAM[set][assoc_victim];		
	}
		
	//DPRINTF("DBAMBPredictor::evictPolicy set %d n assoc_victim %d \n" , set , assoc_victim);
	DHPEntry* rap_current = lookup(current->signature);
	//We didn't create an entry for this dataset, probably a good reason =) (instruction mainly) 
	if(rap_current != NULL)
	{
		if(rap_current->des != BYPASS_CACHE)
		{
			// A learning cache line on dead dataset goes here
			if( current->nbWrite == 0 && current->nbRead == 0)
				rap_current->dead_counter--;
			else
				rap_current->dead_counter++;
		
			if(rap_current->dead_counter > simu_parameters.deadSaturationCouter)
				rap_current->dead_counter = simu_parameters.deadSaturationCouter;
			else if(rap_current->dead_counter < -simu_parameters.deadSaturationCouter)
				rap_current->dead_counter = -simu_parameters.deadSaturationCouter;


			string a =  current->nbWrite == 0 && current->nbRead == 0 ? "DEAD" : "LIVELY";
			dataset_file << "Dataset n°" << rap_current->id << ": Eviction of a " << a << " CL, Address :" << \
				std::hex << current->address << std::dec << " DeadCounter=" << rap_current->dead_counter << endl;
				
			if(rap_current->dead_counter == -simu_parameters.deadSaturationCouter && simu_parameters.enableBP)
			{
				/* Switch the state of the dataset to dead */ 
				rap_current->des = BYPASS_CACHE;			
				dataset_file << "Dataset n°" << rap_current->id << ": Going into BYPASS mode " << endl;
				// Reset the window 
				RW_TYPE old_rw = rap_current->state_rw;
				RD_TYPE old_rd = rap_current->state_rd;

				rap_current->state_rd = RD_NOT_ACCURATE;
				rap_current->state_rw = DEAD;
				rap_current->rd_history.clear();
				rap_current->rw_history.clear();
				rap_current->cptWindow = 0;	
				rap_current->dead_counter = 0;

				// Monitor state switching 
				if(!m_isWarmup)
					stats_ClassErrors[old_rd + NUM_RD_TYPE*old_rw][rap_current->state_rd + NUM_RD_TYPE*rap_current->state_rw]++;
			}
		}
	}
	
	evictRecording(set , assoc_victim , inNVM);	
	return assoc_victim;
}

// Assembly:
subq $0xf8, %rsp
movb %dl, %al
movq %rdi, 0xf0(%rsp)
movl %esi, 0xec(%rsp)
andb $0x1, %al
movb %al, 0xeb(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0xffffffff, 0xe4(%rsp) # imm = 0xFFFFFFFF
cmpq $0x0, 0x60(%rax)
je 0x1d4c5
jmp 0x1d4e4
leaq 0x53cd5(%rip), %rdi     # 0x711a1
leaq 0x54569(%rip), %rsi     # 0x71a3c
movl $0x13d, %edx            # imm = 0x13D
leaq 0x54573(%rip), %rcx     # 0x71a52
callq 0x5260
movq 0x88(%rsp), %rax
cmpq $0x0, 0x68(%rax)
je 0x1d4f5
jmp 0x1d514
leaq 0x53cfe(%rip), %rdi     # 0x711fa
leaq 0x54539(%rip), %rsi     # 0x71a3c
movl $0x13e, %edx            # imm = 0x13E
leaq 0x54543(%rip), %rcx     # 0x71a52
callq 0x5260
movq $0x0, 0xd8(%rsp)
testb $0x1, 0xeb(%rsp)
je 0x1d582
movq 0x88(%rsp), %rax
movq 0x60(%rax), %rdi
movl 0xec(%rsp), %esi
movq (%rdi), %rax
callq *0x20(%rax)
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0xe4(%rsp)
movq 0x10(%rax), %rdi
movslq 0xec(%rsp), %rsi
callq 0xf9a0
movq %rax, %rdi
movslq 0xe4(%rsp), %rsi
callq 0xf9c0
movq (%rax), %rax
movq %rax, 0xd8(%rsp)
jmp 0x1d5d8
movq 0x88(%rsp), %rax
movq 0x68(%rax), %rdi
movl 0xec(%rsp), %esi
movq (%rdi), %rax
callq *0x20(%rax)
movl %eax, %ecx
movq 0x88(%rsp), %rax
movl %ecx, 0xe4(%rsp)
movq 0x8(%rax), %rdi
movslq 0xec(%rsp), %rsi
callq 0xf9a0
movq %rax, %rdi
movslq 0xe4(%rsp), %rsi
callq 0xf9c0
movq (%rax), %rax
movq %rax, 0xd8(%rsp)
movq 0x88(%rsp), %rdi
movq 0xd8(%rsp), %rax
movq 0x18(%rax), %rsi
callq 0x1b150
movq %rax, 0xd0(%rsp)
cmpq $0x0, 0xd0(%rsp)
je 0x1da8c
movq 0xd0(%rsp), %rax
cmpl $0x2, 0x28(%rax)
je 0x1da8a
movq 0xd8(%rsp), %rax
cmpl $0x0, 0x11c(%rax)
jne 0x1d655
movq 0xd8(%rsp), %rax
cmpl $0x0, 0x120(%rax)
jne 0x1d655
movq 0xd0(%rsp), %rax
movl 0xb8(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0xb8(%rax)
jmp 0x1d66c
movq 0xd0(%rsp), %rax
movl 0xb8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0xb8(%rax)
movq 0xd0(%rsp), %rax
movl 0xb8(%rax), %eax
leaq 0x7696f(%rip), %rcx     # 0x93ff0
cmpl 0x9c(%rcx), %eax
jle 0x1d6a6
leaq 0x76960(%rip), %rax     # 0x93ff0
movl 0x9c(%rax), %ecx
movq 0xd0(%rsp), %rax
movl %ecx, 0xb8(%rax)
jmp 0x1d6e6
movq 0xd0(%rsp), %rax
movl 0xb8(%rax), %eax
xorl %ecx, %ecx
leaq 0x76933(%rip), %rdx     # 0x93ff0
subl 0x9c(%rdx), %ecx
cmpl %ecx, %eax
jge 0x1d6e4
xorl %ecx, %ecx
leaq 0x76920(%rip), %rax     # 0x93ff0
subl 0x9c(%rax), %ecx
movq 0xd0(%rsp), %rax
movl %ecx, 0xb8(%rax)
jmp 0x1d6e6
movq 0xd8(%rsp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x11c(%rcx)
movb %al, 0x87(%rsp)
jne 0x1d719
movq 0xd8(%rsp), %rax
cmpl $0x0, 0x120(%rax)
sete %al
movb %al, 0x87(%rsp)
movb 0x87(%rsp), %dl
leaq 0x5435e(%rip), %rcx     # 0x71a85
leaq 0x5435c(%rip), %rax     # 0x71a8a
testb $0x1, %dl
cmovneq %rcx, %rax
movq %rax, 0x70(%rsp)
leaq 0xaf(%rsp), %rdi
movq %rdi, 0x78(%rsp)
callq 0x56a0
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
leaq 0xb0(%rsp), %rdi
callq 0x5530
jmp 0x1d765
leaq 0xaf(%rsp), %rdi
callq 0x53e0
leaq 0x75f8f(%rip), %rdi     # 0x93708
leaq 0x54372(%rip), %rsi     # 0x71af2
callq 0x5380
movq %rax, 0x68(%rsp)
jmp 0x1d78c
movq 0x68(%rsp), %rdi
movq 0xd0(%rsp), %rax
movl 0x18(%rax), %esi
callq 0x5680
movq %rax, 0x60(%rsp)
jmp 0x1d7a8
movq 0x60(%rsp), %rdi
leaq 0x542dd(%rip), %rsi     # 0x71a91
callq 0x5380
movq %rax, 0x58(%rsp)
jmp 0x1d7c0
movq 0x58(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x5330
movq %rax, 0x50(%rsp)
jmp 0x1d7d9
movq 0x50(%rsp), %rdi
leaq 0x542bd(%rip), %rsi     # 0x71aa2
callq 0x5380
movq %rax, 0x48(%rsp)
jmp 0x1d7f1
movq 0x48(%rsp), %rdi
leaq -0xdffd(%rip), %rsi     # 0xf800
callq 0x5160
movq %rax, 0x40(%rsp)
jmp 0x1d809
movq 0x40(%rsp), %rdi
movq 0xd8(%rsp), %rax
movq 0x8(%rax), %rsi
callq 0x50f0
movq %rax, 0x38(%rsp)
jmp 0x1d826
movq 0x38(%rsp), %rdi
leaq -0xe012(%rip), %rsi     # 0xf820
callq 0x5160
movq %rax, 0x30(%rsp)
jmp 0x1d83e
movq 0x30(%rsp), %rdi
leaq 0x54267(%rip), %rsi     # 0x71ab1
callq 0x5380
movq %rax, 0x28(%rsp)
jmp 0x1d856
movq 0x28(%rsp), %rdi
movq 0xd0(%rsp), %rax
movl 0xb8(%rax), %esi
callq 0x5680
movq %rax, 0x20(%rsp)
jmp 0x1d875
movq 0x20(%rsp), %rdi
movq 0x75737(%rip), %rsi     # 0x92fb8
callq 0x53d0
jmp 0x1d888
movq 0xd0(%rsp), %rax
movl 0xb8(%rax), %eax
xorl %ecx, %ecx
leaq 0x76751(%rip), %rdx     # 0x93ff0
subl 0x9c(%rdx), %ecx
cmpl %ecx, %eax
jne 0x1da7d
leaq 0x7673c(%rip), %rax     # 0x93ff0
testb $0x1, 0xa8(%rax)
je 0x1da7d
movq 0xd0(%rsp), %rax
movl $0x2, 0x28(%rax)
leaq 0x75e31(%rip), %rdi     # 0x93708
leaq 0x54214(%rip), %rsi     # 0x71af2
callq 0x5380
movq %rax, 0x18(%rsp)
jmp 0x1d8ea
movq 0x18(%rsp), %rdi
movq 0xd0(%rsp), %rax
movl 0x18(%rax), %esi
callq 0x5680
movq %rax, 0x10(%rsp)
jmp 0x1d906
movq 0x10(%rsp), %rdi
leaq 0x541ad(%rip), %rsi     # 0x71abf
callq 0x5380
movq %rax, 0x8(%rsp)
jmp 0x1d91e
movq 0x8(%rsp), %rdi
movq 0x7568e(%rip), %rsi     # 0x92fb8
callq 0x53d0
jmp 0x1d931
movq 0xd0(%rsp), %rax
movl 0x98(%rax), %eax
movl %eax, 0x98(%rsp)
movq 0xd0(%rsp), %rax
movl 0x9c(%rax), %eax
movl %eax, 0x94(%rsp)
movq 0xd0(%rsp), %rax
movl $0x2, 0x9c(%rax)
movq 0xd0(%rsp), %rax
movl $0x0, 0x98(%rax)
movq 0xd0(%rsp), %rdi
addq $0x40, %rdi
callq 0x20c50
movq 0xd0(%rsp), %rdi
addq $0x58, %rdi
callq 0x20c70
movq 0x88(%rsp), %rax
movq 0xd0(%rsp), %rcx
movl $0x0, 0x84(%rcx)
movq 0xd0(%rsp), %rcx
movl $0x0, 0xb8(%rcx)
testb $0x1, 0x81(%rax)
jne 0x1da7b
movq 0x88(%rsp), %rdi
addq $0x3a0, %rdi            # imm = 0x3A0
movl 0x94(%rsp), %eax
imull $0x3, 0x98(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0xef70
movq %rax, %rdi
movq 0xd0(%rsp), %rax
movl 0x9c(%rax), %eax
movq 0xd0(%rsp), %rcx
imull $0x3, 0x98(%rcx), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0xef90
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x1da7b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0xaf(%rsp), %rdi
callq 0x53e0
jmp 0x1dac2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x5768
jmp 0x1dac2
jmp 0x1da7d
leaq 0xb0(%rsp), %rdi
callq 0x5768
jmp 0x1da8c
movq 0x88(%rsp), %rdi
movl 0xec(%rsp), %esi
movl 0xe4(%rsp), %edx
movb 0xeb(%rsp), %al
andb $0x1, %al
movzbl %al, %ecx
callq 0x20cd0
movl 0xe4(%rsp), %eax
addq $0xf8, %rsp
retq
movq 0xa0(%rsp), %rdi
callq 0x5690
nop