// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]MQSim/src/ssd/Data_Cache_Manager_Flash_Simple.cpp
// Function: SSD_Components::Data_Cache_Manager_Flash_Simple::process_new_user_request(SSD_Components::User_Request*)
void Data_Cache_Manager_Flash_Simple::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:
				{
					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 (data_cache->Exists(tr->Stream_id, tr->LPA)) {
							page_status_type available_sectors_bitmap = data_cache->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;
				}
				default:
					PRINT_ERROR("The specified caching mode is not not support in simple cache manager!")
			}
		} else {//This is a write request
			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://The data cache manger unit performs like a destage buffer
				{
					write_to_destage_buffer(user_request);

					if (user_request->Transaction_list.size() > 0) {
						waiting_user_requests_queue_for_dram_free_slot[user_request->Stream_id].push_back(user_request);
					}

					return;
				}
				default:
					PRINT_ERROR("The specified caching mode is not not support in simple cache manager!")
			}
		}
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, (%rsp)
cmpq $0x0, 0x50(%rsi)
je 0x2bbd0
movq %rsi, %r14
movq %rdi, %rbx
addq $0x40, %rsi
cmpl $0x0, 0x64(%r14)
leaq 0x36ba1(%rip), %rax     # 0x625f8
movq (%rax), %rax
movzwl 0x68(%r14), %ecx
movl (%rax,%rcx,4), %eax
je 0x2ba9b
testl %eax, %eax
jne 0x2ba9f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2bcc8
cmpq $0x0, 0x50(%r14)
je 0x2bbd0
movzwl 0x68(%r14), %eax
imulq $0x18, %rax, %rdi
addq 0xd0(%rbx), %rdi
movq %rsp, %rsi
callq 0x2ab34
jmp 0x2bbd0
testl %eax, %eax
je 0x2bac7
cmpl $0x3, %eax
jne 0x2bbdf
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
movq (%rsi), %r13
leaq 0x40(%r14), %r15
cmpq %r15, %r13
je 0x2bb81
movq 0x10(%r13), %r12
movq 0xc0(%rbx), %rdi
movzwl (%r12), %esi
movq 0x58(%r12), %rdx
callq 0x287ce
testb %al, %al
je 0x2bb54
movq 0xc0(%rbx), %rsi
movzwl (%r12), %edx
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 0x2bb5d
testq %rbp, %rbp
je 0x2bb54
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 0x2bace
callq 0x2661d
addl %eax, 0x58(%r14)
movq (%r13), %r12
movq %r15, %rdi
movq %r13, %rsi
callq 0x2ab26
movq (%rsp), %r14
movq %r12, %r13
jmp 0x2baca
cmpl $0x0, 0x58(%r14)
je 0x2bbb8
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 0x2bc20
cmpq $0x0, 0x50(%r14)
je 0x2bbd0
movq 0x38(%rbx), %rax
movq 0x38(%rax), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x60(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x3640a(%rip), %rbx     # 0x61ff0
leaq 0x1899b(%rip), %rsi     # 0x44588
movq %rbx, %rdi
callq 0x7360
leaq 0x1b4c2(%rip), %rsi     # 0x470be
movq %rbx, %rdi
callq 0x7360
movq %rax, %rdi
callq 0x71a0
movq 0x363bd(%rip), %rdi     # 0x61fd0
callq 0x73d0
pushq $0x1
popq %rdi
callq 0x73e0