// File: /workspace/github_cpp_makefile_star2_O2/gvaumour[P]cache-simulator/src/Hierarchy.cc
// Function: Hierarchy::signalWB(Access, bool, int)
void
Hierarchy::signalWB(Access wb_request, bool isKept, int idcore)
{

	if(idcore == -1)
	{
		// It is a WB from the LLC to the Main Mem
		if(wb_request.m_type == MemCmd::DIRTY_WRITEBACK)
			stats_dirtyWB_MainMem++;		
		else if(wb_request.m_type == MemCmd::CLEAN_WRITEBACK)
			stats_cleanWB_MainMem++;
		else
			assert(false && "Error on the type of the write back request");

		string state = directory_state_str[m_directory->getState(wb_request.m_address)];
		//cout << "WB Request address = " << std::hex << wb_request.m_address << ", DirectoryState = " << state  << endl;

		uint64_t addr =  wb_request.m_address;
		DirectoryState dir_state = m_directory->getEntry(addr)->coherence_state;
		
		if( dir_state == CLEAN_LLC || dir_state == DIRTY_LLC )
			m_directory->setCoherenceState(addr , NOT_PRESENT);
		else if( dir_state == SHARED_L1)
			m_directory->setCoherenceState(addr , SHARED_L1_NOT_LLC);
		else if( dir_state == EXCLUSIVE_L1)
			m_directory->setCoherenceState(addr , EXCLUSIVE_L1_NOT_LLC);
		else if( dir_state == MODIFIED_L1)
			m_directory->setCoherenceState(addr , MODIFIED_L1_NOT_LLC);
		else{
			cout << "Error dir_state = " << directory_state_str[dir_state] << endl;
			assert(false);
		}
	
		state = directory_state_str[m_directory->getState(wb_request.m_address)];
		//cout << "WB Request address = " << std::hex << wb_request.m_address << ", DirectoryState = " << state  << endl;
			
//		m_directory->removeEntry(wb_request.m_address);
	}			
	else{
	
		string dir_state = string( directory_state_str[m_directory->getState(wb_request.m_address)]);
		DPRINTF( DebugHierarchy , "Write Back Block %#lx , Id Core = %d , Dir State = %s \n" , wb_request.m_address , idcore , dir_state.c_str()); 
		//Remove the idcore from the tracker list and update the state
		if(!isKept)
			m_directory->removeTracker(wb_request.m_address , idcore);
		
		if( m_LLC->handleAccess(wb_request) == BYPASS_CACHE)
		{
			if(m_directory->getTrackers(wb_request.m_address).size() == 0)
				m_directory->setCoherenceState(wb_request.m_address , NOT_PRESENT);
		
			if(wb_request.m_type == MemCmd::DIRTY_WRITEBACK)
				stats_dirtyWB_MainMem++;		
			else if(wb_request.m_type == MemCmd::CLEAN_WRITEBACK)
				stats_cleanWB_MainMem++;
			else
				assert(false && "Error on the type of the write back request");
		}
		else
		{
			
			if(m_directory->getTrackers(wb_request.m_address).size() == 0)
				m_directory->setCoherenceState(wb_request.m_address , CLEAN_LLC);
			else
				m_directory->setCoherenceState(wb_request.m_address , SHARED_L1);
		}
		dir_state = directory_state_str[m_directory->getState(wb_request.m_address)];
		DPRINTF( DebugHierarchy , "End Write Back Block %#lx Dir State is now %s \n" , wb_request.m_address , dir_state.c_str()); 
		//m_directory->updateEntry(wb_request.m_address);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
leaq 0xd0(%rsp), %rcx
cmpl $-0x1, %edx
je 0x12c6b
movl %edx, %ebp
movl %esi, %r12d
movq 0x20(%rbx), %rdi
movq (%rcx), %r14
movq %r14, %rsi
callq 0xfdba
movl %eax, %eax
leaq 0x2a852(%rip), %rcx     # 0x3d3b0
movq (%rcx,%rax,8), %rsi
leaq 0x78(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x5140
leaq 0x1778c(%rip), %rsi     # 0x2a304
leaq 0x48(%rsp), %rdi
leaq 0x47(%rsp), %rdx
callq 0x5140
leaq 0x2b3c2(%rip), %r15     # 0x3df50
addq $0x8, %r15
leaq 0x48(%rsp), %rsi
movq %r15, %rdi
callq 0x138f8
movq %rax, %r13
leaq 0x48(%rsp), %rdi
callq 0x5618
testq %r13, %r13
je 0x12bc9
movq 0x78(%rsp), %rcx
leaq 0x178d7(%rip), %rdi     # 0x2a494
movq %r14, %rsi
movl %ebp, %edx
xorl %eax, %eax
callq 0x5040
testb %r12b, %r12b
jne 0x12bdc
movq 0x20(%rbx), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0xfdea
movq 0x18(%rbx), %rdi
leaq 0xd0(%rsp), %rcx
movq 0x30(%rcx), %rax
movq %rax, 0x30(%rsp)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x19476
cmpl $0x2, %eax
jne 0x12c80
movq 0x20(%rbx), %rsi
leaq 0x48(%rsp), %rdi
movq %r14, %rdx
callq 0xfd76
leaq 0x48(%rsp), %rdi
movq 0x28(%rdi), %r12
callq 0x101d6
testq %r12, %r12
jne 0x12c47
movq 0x20(%rbx), %rdi
pushq $0x8
popq %rdx
movq %r14, %rsi
callq 0xfe58
leaq 0xd0(%rsp), %rax
movl 0x1c(%rax), %eax
cmpl $0x6, %eax
je 0x12d4e
cmpl $0x5, %eax
jne 0x12e44
pushq $0x70
jmp 0x12d50
movl 0x1c(%rcx), %eax
cmpl $0x6, %eax
je 0x12cbb
cmpl $0x5, %eax
jne 0x12ddf
pushq $0x70
jmp 0x12cbd
movq 0x20(%rbx), %rsi
leaq 0x48(%rsp), %rdi
movq %r14, %rdx
callq 0xfd76
xorl %eax, %eax
leaq 0x48(%rsp), %rdi
cmpq $0x0, 0x28(%rdi)
pushq $0x6
popq %rbp
cmovnel %eax, %ebp
callq 0x101d6
movq 0x20(%rbx), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0xfe58
jmp 0x12d55
pushq $0x78
popq %rax
incq (%rbx,%rax)
movq 0x20(%rbx), %rdi
movq (%rcx), %r14
movq %r14, %rsi
callq 0xfdba
movl %eax, %eax
leaq 0x2a6d6(%rip), %r12     # 0x3d3b0
movq (%r12,%rax,8), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x5140
movq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0xfc52
movl 0x38(%rax), %r15d
movl %r15d, %eax
andl $-0x2, %eax
cmpl $0x6, %eax
jne 0x12d0e
pushq $0x8
popq %r15
jmp 0x12d1c
cmpl $0x2, %r15d
ja 0x12dfe
addl $0x3, %r15d
movq 0x20(%rbx), %rdi
movq %r14, %rsi
movl %r15d, %edx
callq 0xfe58
movq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0xfdba
movl %eax, %eax
movq (%r12,%rax,8), %rsi
leaq 0x48(%rsp), %rdi
callq 0x55c0
leaq 0x48(%rsp), %rdi
jmp 0x12dc8
pushq $0x78
popq %rax
incq (%rbx,%rax)
movq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0xfdba
movl %eax, %eax
leaq 0x2a646(%rip), %rcx     # 0x3d3b0
movq (%rcx,%rax,8), %rsi
leaq 0x78(%rsp), %rdi
callq 0x55c0
leaq 0x17585(%rip), %rsi     # 0x2a304
leaq 0x48(%rsp), %rdi
leaq 0x47(%rsp), %rdx
callq 0x5140
leaq 0x48(%rsp), %rsi
movq %r15, %rdi
callq 0x138f8
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x5618
testq %rbx, %rbx
je 0x12dc3
movq 0x78(%rsp), %rdx
leaq 0x17713(%rip), %rdi     # 0x2a4cc
movq %r14, %rsi
xorl %eax, %eax
callq 0x5040
leaq 0x78(%rsp), %rdi
callq 0x5618
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17621(%rip), %rdi     # 0x2a407
leaq 0x17651(%rip), %rsi     # 0x2a43e
leaq 0x1765b(%rip), %rcx     # 0x2a44f
movl $0xa4, %edx
callq 0x51e0
movq 0x2a1cb(%rip), %rdi     # 0x3cfd0
leaq 0x1766f(%rip), %rsi     # 0x2a47b
callq 0x52c0
movq (%r12,%r15,8), %rsi
movq %rax, %rdi
callq 0x52c0
movq %rax, %rdi
callq 0x5190
leaq 0x17662(%rip), %rdi     # 0x2a48e
leaq 0x1760b(%rip), %rsi     # 0x2a43e
leaq 0x17615(%rip), %rcx     # 0x2a44f
movl $0xb6, %edx
callq 0x51e0
leaq 0x175bc(%rip), %rdi     # 0x2a407
leaq 0x175ec(%rip), %rsi     # 0x2a43e
leaq 0x175f6(%rip), %rcx     # 0x2a44f
movl $0xd0, %edx
callq 0x51e0
jmp 0x12e8d
jmp 0x12e8d
jmp 0x12e7e
jmp 0x12e6d
jmp 0x12e8d
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x5618
jmp 0x12e90
jmp 0x12e8d
movq %rax, %rbx
jmp 0x12e9a
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
jmp 0x12e95
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x5618
movq %rbx, %rdi
callq 0x5570