// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]MQSim/src/ssd/Data_Cache_Manager_Flash_Advanced.cpp
// Function: SSD_Components::Data_Cache_Manager_Flash_Advanced::Execute_simulator_event(MQSimEngine::Sim_Event*)
void Data_Cache_Manager_Flash_Advanced::Execute_simulator_event(MQSimEngine::Sim_Event* ev)
	{
		Data_Cache_Simulation_Event_Type eventType = (Data_Cache_Simulation_Event_Type)ev->Type;
		Memory_Transfer_Info* transfer_info = (Memory_Transfer_Info*)ev->Parameters;

		switch (eventType)
		{
			case Data_Cache_Simulation_Event_Type::MEMORY_READ_FOR_USERIO_FINISHED://A user read is service from DRAM cache
			case Data_Cache_Simulation_Event_Type::MEMORY_WRITE_FOR_USERIO_FINISHED:
				((User_Request*)(transfer_info)->Related_request)->Sectors_serviced_from_cache -= transfer_info->Size_in_bytes / SECTOR_SIZE_IN_BYTE;
				if (is_user_request_finished((User_Request*)(transfer_info)->Related_request))
					broadcast_user_request_serviced_signal(((User_Request*)(transfer_info)->Related_request));
				break;
			case Data_Cache_Simulation_Event_Type::MEMORY_READ_FOR_CACHE_EVICTION_FINISHED://Reading data from DRAM and writing it back to the flash storage
				static_cast<FTL*>(nvm_firmware)->Address_Mapping_Unit->Translate_lpa_to_ppa_and_dispatch(*((std::list<NVM_Transaction*>*)(transfer_info->Related_request)));
				delete (std::list<NVM_Transaction*>*)transfer_info->Related_request;
				break;
			case Data_Cache_Simulation_Event_Type::MEMORY_WRITE_FOR_CACHE_FINISHED://The recently read data from flash is written back to memory to support future user read requests
				break;
		}
		delete transfer_info;

		memory_channel_is_busy = false;
		if (shared_dram_request_queue)	{
			if (dram_execution_queue[0].size() > 0)	{
				Memory_Transfer_Info* transfer_info = dram_execution_queue[0].front();
				dram_execution_queue[0].pop();
				Simulator->Register_sim_event(Simulator->Time() + estimate_dram_access_time(transfer_info->Size_in_bytes, dram_row_size, dram_busrt_size,
					dram_burst_transfer_time_ddr, dram_tRCD, dram_tCL, dram_tRP),
					this, transfer_info, static_cast<int>(transfer_info->next_event_type));
				memory_channel_is_busy = true;
			}
		} else {
			for (unsigned int i = 0; i < stream_count; i++) {
				dram_execution_list_turn++;
				dram_execution_list_turn %= stream_count;
				if (dram_execution_queue[dram_execution_list_turn].size() > 0) {
					Memory_Transfer_Info* transfer_info = dram_execution_queue[dram_execution_list_turn].front();
					dram_execution_queue[dram_execution_list_turn].pop();
					Simulator->Register_sim_event(Simulator->Time() + estimate_dram_access_time(transfer_info->Size_in_bytes, dram_row_size, dram_busrt_size,
						dram_burst_transfer_time_ddr, dram_tRCD, dram_tCL, dram_tRP),
						this, transfer_info, static_cast<int>(transfer_info->next_event_type));
					memory_channel_is_busy = true;
					break;
				}
			}
		}
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movq %rdi, %rbx
movl 0x18(%rsi), %eax
movq 0x10(%rsi), %r14
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jae 0x2a9d3
movl (%r14), %eax
shrl $0x9, %eax
movq 0x8(%r14), %rsi
movl 0x58(%rsi), %ecx
cmpq $0x0, 0x50(%rsi)
sete %dl
subl %eax, %ecx
movl %ecx, 0x58(%rsi)
sete %al
andb %dl, %al
cmpb $0x1, %al
jne 0x2aa02
movq %rbx, %rdi
callq 0x2956e
jmp 0x2aa02
testl %eax, %eax
jne 0x2aa02
movq 0x38(%rbx), %rax
movq 0x38(%rax), %rdi
movq 0x8(%r14), %rsi
movq (%rdi), %rax
callq *0x60(%rax)
movq 0x8(%r14), %r15
testq %r15, %r15
je 0x2a9fa
movq %r15, %rdi
callq 0x2adc4
movq %r15, %rdi
callq 0x7340
movq %r14, %rdi
callq 0x7340
movb $0x0, 0xc8(%rbx)
cmpb $0x0, 0xe0(%rbx)
je 0x2aa38
movq 0xd0(%rbx), %rdi
callq 0x2afbe
testq %rax, %rax
je 0x2aae6
movq 0xd0(%rbx), %rdi
jmp 0x2aa84
pushq $-0x1
popq %rbp
movl 0x74(%rbx), %ecx
incl %ebp
cmpl %ecx, %ebp
jae 0x2aae6
movl 0xe4(%rbx), %eax
incl %eax
xorl %edx, %edx
divl %ecx
movl %edx, 0xe4(%rbx)
movslq %edx, %rax
imulq $0x50, %rax, %rdi
addq 0xd0(%rbx), %rdi
callq 0x2afbe
testq %rax, %rax
je 0x2aa3b
movslq 0xe4(%rbx), %rax
imulq $0x50, %rax, %rdi
addq 0xd0(%rbx), %rdi
movq 0x10(%rdi), %rax
movq (%rax), %r14
callq 0x2b006
callq 0x1f784
movq %rax, %r15
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
movq %rax, %r12
movl (%r14), %edi
movl 0x40(%rbx), %esi
movl 0x48(%rbx), %edx
movsd 0x50(%rbx), %xmm0
movq 0x58(%rbx), %rcx
movq 0x60(%rbx), %r8
movq 0x68(%rbx), %r9
callq 0x2abb8
addq %rax, %r12
movl 0x10(%r14), %r8d
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r14, %rcx
callq 0x1fb00
movb $0x1, 0xc8(%rbx)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop