// 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::process_new_user_request(SSD_Components::User_Request*)
void Data_Cache_Manager_Flash_Advanced::process_new_user_request(User_Request* user_request)
	{
		//This condition shouldn't happen, but we check it
		if (user_request->Transaction_list.size() == 0) {
			return;
		}

		if (user_request->Type == UserRequestType::READ) {
			switch (caching_mode_per_input_stream[user_request->Stream_id]) {
				case Caching_Mode::TURNED_OFF:
					static_cast<FTL*>(nvm_firmware)->Address_Mapping_Unit->Translate_lpa_to_ppa_and_dispatch(user_request->Transaction_list);
					return;
				case Caching_Mode::WRITE_CACHE:
				case Caching_Mode::READ_CACHE:
				case Caching_Mode::WRITE_READ_CACHE:
				{
					std::list<NVM_Transaction*>::iterator it = user_request->Transaction_list.begin();
					while (it != user_request->Transaction_list.end()) {
						NVM_Transaction_Flash_RD* tr = (NVM_Transaction_Flash_RD*)(*it);
						if (per_stream_cache[tr->Stream_id]->Exists(tr->Stream_id, tr->LPA)) {
							page_status_type available_sectors_bitmap = per_stream_cache[tr->Stream_id]->Get_slot(tr->Stream_id, tr->LPA).State_bitmap_of_existing_sectors & tr->read_sectors_bitmap;
							if (available_sectors_bitmap == tr->read_sectors_bitmap) {
								user_request->Sectors_serviced_from_cache += count_sector_no_from_status_bitmap(tr->read_sectors_bitmap);
								user_request->Transaction_list.erase(it++);//the ++ operation should happen here, otherwise the iterator will be part of the list after erasing it from the list
							} else if (available_sectors_bitmap != 0) {
								user_request->Sectors_serviced_from_cache += count_sector_no_from_status_bitmap(available_sectors_bitmap);
								tr->read_sectors_bitmap = (tr->read_sectors_bitmap & ~available_sectors_bitmap);
								tr->Data_and_metadata_size_in_byte -= count_sector_no_from_status_bitmap(available_sectors_bitmap) * SECTOR_SIZE_IN_BYTE;
								it++;
							} else {
								it++;
							}
						} else {
							it++;
						}
					}

					if (user_request->Sectors_serviced_from_cache > 0) {
						Memory_Transfer_Info* transfer_info = new Memory_Transfer_Info;
						transfer_info->Size_in_bytes = user_request->Sectors_serviced_from_cache * SECTOR_SIZE_IN_BYTE;
						transfer_info->Related_request = user_request;
						transfer_info->next_event_type = Data_Cache_Simulation_Event_Type::MEMORY_READ_FOR_USERIO_FINISHED;
						transfer_info->Stream_id = user_request->Stream_id;
						service_dram_access_request(transfer_info);
					}
					if (user_request->Transaction_list.size() > 0) {
						static_cast<FTL*>(nvm_firmware)->Address_Mapping_Unit->Translate_lpa_to_ppa_and_dispatch(user_request->Transaction_list);
					}

					return;
				}
			}
		}
		else//This is a write request
		{
			switch (caching_mode_per_input_stream[user_request->Stream_id])
			{
				case Caching_Mode::TURNED_OFF:
				case Caching_Mode::READ_CACHE:
					static_cast<FTL*>(nvm_firmware)->Address_Mapping_Unit->Translate_lpa_to_ppa_and_dispatch(user_request->Transaction_list);
					return;
				case Caching_Mode::WRITE_CACHE://The data cache manger unit performs like a destage buffer
				case Caching_Mode::WRITE_READ_CACHE:
				{
					write_to_destage_buffer(user_request);

					int queue_id = user_request->Stream_id;
					if (shared_dram_request_queue) {
						queue_id = 0;
					}
					if (user_request->Transaction_list.size() > 0) {
						waiting_user_requests_queue_for_dram_free_slot[queue_id].push_back(user_request);
					}
					return;
				}
			}
		}
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, (%rsp)
cmpq $0x0, 0x50(%rsi)
je 0x2a492
movq %rsi, %r14
movq %rdi, %rbx
addq $0x40, %rsi
cmpl $0x0, 0x64(%r14)
leaq 0x3830b(%rip), %rax     # 0x625f8
movq (%rax), %rax
movzwl 0x68(%r14), %ecx
movl (%rax,%rcx,4), %eax
je 0x2a355
cmpl $0x3, %eax
ja 0x2a492
movl %eax, %eax
leaq 0x1cd6c(%rip), %rcx     # 0x47078
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a554
cmpq $0x0, 0x50(%r14)
je 0x2a492
movzwl 0x68(%r14), %eax
xorl %ecx, %ecx
cmpb $0x0, 0xe0(%rbx)
cmoveq %rax, %rcx
imulq $0x18, %rcx, %rdi
addq 0xd8(%rbx), %rdi
movq %rsp, %rsi
callq 0x2ab34
jmp 0x2a492
cmpl $0x3, %eax
jae 0x2a471
movq (%rsi), %r13
leaq 0x40(%r14), %r15
cmpq %r15, %r13
je 0x2a420
movq 0x10(%r13), %r12
movq 0xc0(%rbx), %rax
movzwl (%r12), %esi
movq (%rax,%rsi,8), %rdi
movq 0x58(%r12), %rdx
callq 0x287ce
testb %al, %al
je 0x2a3f3
movq 0xc0(%rbx), %rax
movzwl (%r12), %edx
movq (%rax,%rdx,8), %rsi
movq 0x58(%r12), %rcx
leaq 0x8(%rsp), %rdi
callq 0x28822
movq 0x90(%r12), %rdi
movq 0x8(%rsp), %rbp
andq %rdi, %rbp
cmpq %rdi, %rbp
je 0x2a3fc
testq %rbp, %rbp
je 0x2a3f3
movq %rbp, %rdi
callq 0x2661d
addl %eax, 0x58(%r14)
movq %rbp, %rax
notq %rax
andq %rax, 0x90(%r12)
movq %rbp, %rdi
callq 0x2661d
shll $0x9, %eax
subl %eax, 0x50(%r12)
movq (%r13), %r13
jmp 0x2a365
callq 0x2661d
addl %eax, 0x58(%r14)
movq (%r13), %r12
movq %r15, %rdi
movq %r13, %rsi
callq 0x2ab26
movq (%rsp), %r14
movq %r12, %r13
jmp 0x2a361
cmpl $0x0, 0x58(%r14)
je 0x2a457
pushq $0x18
popq %rdi
callq 0x7370
movl 0x58(%r14), %ecx
shll $0x9, %ecx
movl %ecx, (%rax)
movq %r14, 0x8(%rax)
movl $0x2, 0x10(%rax)
movzwl 0x68(%r14), %ecx
movw %cx, 0x14(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2a4a2
cmpq $0x0, 0x50(%r14)
je 0x2a492
movq 0x38(%rbx), %rax
movq 0x38(%rax), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x60(%rax)
jmp 0x2a492
jne 0x2a492
movq 0x38(%rbx), %rax
movq 0x38(%rax), %rdi
movq (%rdi), %rax
movq 0x60(%rax), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop