// File: gvaumour[P]cache-simulator/src/Predictor.cc
// Function: Predictor::getHitPerDataArray(unsigned long, int, bool)
bool
Predictor::getHitPerDataArray(uint64_t block_addr, int set , bool inNVM)
{
	uint64_t ref_time = 0;
	bool find = false;
	for(unsigned i = 0 ; i < BP_missing_tags[set].size() && !find; i++)
	{
		if( block_addr == BP_missing_tags[set][i]->addr && BP_missing_tags[set][i]->isValid)
		{
			ref_time = BP_missing_tags[set][i]->last_time_touched;
			find = true;		
		}
	}
	
	if(!find)
		return false;
	
	/*string str_inNVM = inNVM ? "TRUE" : "FALSE";
	cout << "*** Call getHitPerDataArray with inNVM =" << str_inNVM << endl;
	cout << "Block is " << std::hex << block_addr << ", ref_time = " << ref_time << endl;
	for(unsigned i = 0 ; i < BP_missing_tags[set].size() ; i++)
	{
		if(BP_missing_tags[set][i]->isValid)
		{
			string alloc_site = BP_missing_tags[set][i]->allocSite ? "NVM" : "SRAM";
			cout << "Index " << i << " - Addr = " << std::hex << BP_missing_tags[set][i]->addr \
			<< std::dec << " - Time = " << BP_missing_tags[set][i]->last_time_touched << " - Allocation = " << alloc_site << endl;		
		}
	}
	*/
	int pos = 0;
	
	for(unsigned i = 0 ; i < BP_missing_tags[set].size() ; i++)
	{
		if( BP_missing_tags[set][i]->isValid && \
			BP_missing_tags[set][i]->allocSite == inNVM && \
			ref_time < BP_missing_tags[set][i]->last_time_touched)
		{
			pos++;
		}
	}

	//cout << "pos = " << pos << endl;
	if(inNVM)
		return pos < simu_parameters.nvm_assoc ? true : false;
	else
		return pos < simu_parameters.sram_assoc ? true : false;
}

// Assembly:
subq $0x68, %rsp
movb %cl, %al
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movl %edx, 0x4c(%rsp)
andb $0x1, %al
movb %al, 0x4b(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x40(%rsp)
movb $0x0, 0x3f(%rsp)
movl $0x0, 0x38(%rsp)
movq 0x28(%rsp), %rdi
movl 0x38(%rsp), %eax
movq %rax, 0x18(%rsp)
addq $0x140, %rdi            # imm = 0x140
movslq 0x4c(%rsp), %rsi
callq 0x58920
movq %rax, %rdi
callq 0x58940
movq 0x18(%rsp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, 0x27(%rsp)
jae 0x577be
movb 0x3f(%rsp), %al
xorb $-0x1, %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x577cb
jmp 0x5787d
movq 0x28(%rsp), %rdi
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
addq $0x140, %rdi            # imm = 0x140
movslq 0x4c(%rsp), %rsi
callq 0x58920
movq %rax, %rdi
movl 0x38(%rsp), %eax
movl %eax, %esi
callq 0x58860
movq %rax, %rcx
movq 0x10(%rsp), %rax
movq (%rcx), %rcx
cmpq (%rcx), %rax
jne 0x5786b
movq 0x28(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
movslq 0x4c(%rsp), %rsi
callq 0x58920
movq %rax, %rdi
movl 0x38(%rsp), %eax
movl %eax, %esi
callq 0x58860
movq (%rax), %rax
testb $0x1, 0x14(%rax)
je 0x5786b
movq 0x28(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
movslq 0x4c(%rsp), %rsi
callq 0x58920
movq %rax, %rdi
movl 0x38(%rsp), %eax
movl %eax, %esi
callq 0x58860
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, 0x40(%rsp)
movb $0x1, 0x3f(%rsp)
jmp 0x5786d
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x5777a
testb $0x1, 0x3f(%rsp)
jne 0x5788e
movb $0x0, 0x67(%rsp)
jmp 0x579dd
movl $0x0, 0x34(%rsp)
movl $0x0, 0x30(%rsp)
movq 0x28(%rsp), %rdi
movl 0x30(%rsp), %eax
movq %rax, 0x8(%rsp)
addq $0x140, %rdi            # imm = 0x140
movslq 0x4c(%rsp), %rsi
callq 0x58920
movq %rax, %rdi
callq 0x58940
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x5799e
movq 0x28(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
movslq 0x4c(%rsp), %rsi
callq 0x58920
movq %rax, %rdi
movl 0x30(%rsp), %eax
movl %eax, %esi
callq 0x58860
movq (%rax), %rax
testb $0x1, 0x14(%rax)
je 0x5798c
movq 0x28(%rsp), %rdi
addq $0x140, %rdi            # imm = 0x140
movslq 0x4c(%rsp), %rsi
callq 0x58920
movq %rax, %rdi
movl 0x30(%rsp), %eax
movl %eax, %esi
callq 0x58860
movq (%rax), %rax
movb 0x16(%rax), %al
andb $0x1, %al
movzbl %al, %eax
movb 0x4b(%rsp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x5798c
movq 0x28(%rsp), %rdi
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
addq $0x140, %rdi            # imm = 0x140
movslq 0x4c(%rsp), %rsi
callq 0x58920
movq %rax, %rdi
movl 0x30(%rsp), %eax
movl %eax, %esi
callq 0x58860
movq %rax, %rcx
movq (%rsp), %rax
movq (%rcx), %rcx
cmpq 0x8(%rcx), %rax
jae 0x5798c
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x5798e
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x5789e
testb $0x1, 0x4b(%rsp)
je 0x579c2
movl 0x34(%rsp), %eax
leaq 0x3c640(%rip), %rcx     # 0x93ff0
movl 0x40(%rcx), %ecx
subl %ecx, %eax
setl %al
negb %al
andb $0x1, %al
movb %al, 0x67(%rsp)
jmp 0x579dd
movl 0x34(%rsp), %eax
leaq 0x3c623(%rip), %rcx     # 0x93ff0
movl 0x38(%rcx), %ecx
subl %ecx, %eax
setl %al
negb %al
andb $0x1, %al
movb %al, 0x67(%rsp)
movb 0x67(%rsp), %al
andb $0x1, %al
addq $0x68, %rsp
retq
nopl (%rax,%rax)