// File: CMU-SAFARI[P]MQSim/src/ssd/Data_Cache_Manager_Flash_Simple.cpp
// Function: SSD_Components::Data_Cache_Manager_Flash_Simple::Execute_simulator_event(MQSimEngine::Sim_Event*)
void Data_Cache_Manager_Flash_Simple::Execute_simulator_event(MQSimEngine::Sim_Event* ev)
	{
		Data_Cache_Simulation_Event_Type eventType = (Data_Cache_Simulation_Event_Type)ev->Type;
		Memory_Transfer_Info* transfer_inf = (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_inf)->Related_request)->Sectors_serviced_from_cache -= transfer_inf->Size_in_bytes / SECTOR_SIZE_IN_BYTE;
				if (is_user_request_finished((User_Request*)(transfer_inf)->Related_request)) {
					broadcast_user_request_serviced_signal(((User_Request*)(transfer_inf)->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_inf->Related_request)));
				delete (std::list<NVM_Transaction*>*)transfer_inf->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;
		}

		dram_execution_queue[transfer_inf->Stream_id].pop();
		if (dram_execution_queue[transfer_inf->Stream_id].size() > 0) {
			Memory_Transfer_Info* new_transfer_info = dram_execution_queue[transfer_inf->Stream_id].front();
			Simulator->Register_sim_event(Simulator->Time() + estimate_dram_access_time(new_transfer_info->Size_in_bytes, dram_row_size, dram_busrt_size,
				dram_burst_transfer_time_ddr, dram_tRCD, dram_tCL, dram_tRP),
				this, new_transfer_info, static_cast<int>(new_transfer_info->next_event_type));
		}

		delete transfer_inf;
	}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x48(%rsp), %rax
movl 0x18(%rax), %eax
movl %eax, 0x44(%rsp)
movq 0x48(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x38(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x2c(%rsp)
testl %eax, %eax
je 0x7086a
jmp 0x70800
movl 0x2c(%rsp), %eax
subl $0x1, %eax
je 0x708b0
jmp 0x7080f
movl 0x2c(%rsp), %eax
addl $-0x2, %eax
subl $0x1, %eax
ja 0x708b2
jmp 0x70821
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rax
movl (%rax), %edx
shrl $0x9, %edx
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x58(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x58(%rax)
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rsi
callq 0x6c3c0
testb $0x1, %al
jne 0x70855
jmp 0x70868
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rsi
callq 0x67bf0
jmp 0x708b2
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %rdi
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rsi
movq (%rdi), %rax
callq *0x60(%rax)
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x708ae
movq 0x18(%rsp), %rdi
callq 0x6c2b0
movq 0x18(%rsp), %rdi
callq 0x8410
jmp 0x708b2
jmp 0x708b2
movq 0x20(%rsp), %rax
movq 0xc8(%rax), %rdi
movq 0x38(%rsp), %rax
movzwl 0x14(%rax), %eax
imulq $0x50, %rax, %rax
addq %rax, %rdi
callq 0x6bfc0
movq 0x20(%rsp), %rax
movq 0xc8(%rax), %rdi
movq 0x38(%rsp), %rax
movzwl 0x14(%rax), %eax
imulq $0x50, %rax, %rax
addq %rax, %rdi
callq 0x6bfa0
cmpq $0x0, %rax
jbe 0x70993
movq 0x20(%rsp), %rax
movq 0xc8(%rax), %rdi
movq 0x38(%rsp), %rax
movzwl 0x14(%rax), %eax
imulq $0x50, %rax, %rax
addq %rax, %rdi
callq 0x6bfb0
movq (%rax), %rax
movq %rax, 0x30(%rsp)
callq 0x46500
movq %rax, 0x10(%rsp)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x8(%rsp)
movq 0x30(%rsp), %rcx
movl (%rcx), %edi
movl 0x40(%rax), %esi
movl 0x48(%rax), %edx
movsd 0x50(%rax), %xmm0
movq 0x58(%rax), %rcx
movq 0x60(%rax), %r8
movq 0x68(%rax), %r9
callq 0x6c4e0
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rdx
addq %rax, %rsi
movq 0x30(%rsp), %rcx
movq 0x30(%rsp), %rax
movl 0x10(%rax), %r8d
callq 0x46c60
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x709ab
movq (%rsp), %rdi
callq 0x8410
addq $0x58, %rsp
retq