// File: 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:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xb8(%rsp), %rdi
addq $0x40, %rdi
callq 0x59910
cmpq $0x0, %rax
jne 0x6add0
jmp 0x6b217
movq 0xb8(%rsp), %rax
cmpl $0x0, 0x64(%rax)
jne 0x6b141
leaq 0x6f8ff(%rip), %rax     # 0xda6e8
movq (%rax), %rax
movq 0xb8(%rsp), %rcx
movzwl 0x68(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0x1c(%rsp)
subl $0x3, %eax
jb 0x6ae39
jmp 0x6ae06
movl 0x1c(%rsp), %eax
subl $0x3, %eax
jne 0x6b13c
jmp 0x6ae15
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %rdi
movq 0xb8(%rsp), %rsi
addq $0x40, %rsi
movq (%rdi), %rax
callq *0x60(%rax)
jmp 0x6b217
movq 0xb8(%rsp), %rdi
addq $0x40, %rdi
callq 0x6c0d0
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rdi
addq $0x40, %rdi
callq 0x6c120
movq %rax, 0xa8(%rsp)
leaq 0xb0(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x6c100
testb $0x1, %al
jne 0x6ae89
jmp 0x6b08e
leaq 0xb0(%rsp), %rdi
callq 0x6c150
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq (%rcx), %rcx
movq %rcx, 0xa0(%rsp)
movq 0xc0(%rax), %rax
movq 0xa0(%rsp), %rcx
movzwl (%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0xa0(%rsp), %rax
movw (%rax), %ax
movq 0xa0(%rsp), %rcx
movq 0x58(%rcx), %rdx
movzwl %ax, %esi
callq 0x64390
testb $0x1, %al
jne 0x6aee7
jmp 0x6b075
movq 0x20(%rsp), %rax
movq 0xc0(%rax), %rax
movq 0xa0(%rsp), %rcx
movzwl (%rcx), %ecx
movq (%rax,%rcx,8), %rsi
movq 0xa0(%rsp), %rax
movw (%rax), %ax
movq 0xa0(%rsp), %rcx
movq 0x58(%rcx), %rcx
leaq 0x68(%rsp), %rdi
movzwl %ax, %edx
callq 0x644c0
movq 0x68(%rsp), %rax
movq 0xa0(%rsp), %rcx
andq 0x90(%rcx), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
movq 0xa0(%rsp), %rcx
cmpq 0x90(%rcx), %rax
jne 0x6afcc
movq 0xa0(%rsp), %rax
movq 0x90(%rax), %rdi
callq 0x5a170
movl %eax, %ecx
movq 0xb8(%rsp), %rax
addl 0x58(%rax), %ecx
movl %ecx, 0x58(%rax)
movq 0xb8(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x10(%rsp)
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x6c1c0
movq %rax, 0x58(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x6c1f0
movq 0x10(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x6c170
movq %rax, 0x50(%rsp)
jmp 0x6b073
cmpq $0x0, 0x98(%rsp)
je 0x6b05d
movq 0x98(%rsp), %rdi
callq 0x5a170
movl %eax, %ecx
movq 0xb8(%rsp), %rax
addl 0x58(%rax), %ecx
movl %ecx, 0x58(%rax)
movq 0xa0(%rsp), %rax
movq 0x90(%rax), %rcx
movq 0x98(%rsp), %rax
xorq $-0x1, %rax
andq %rax, %rcx
movq 0xa0(%rsp), %rax
movq %rcx, 0x90(%rax)
movq 0x98(%rsp), %rdi
callq 0x5a170
movl %eax, %edx
shll $0x9, %edx
movq 0xa0(%rsp), %rax
movl 0x50(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x50(%rax)
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x6c1c0
movq %rax, 0x48(%rsp)
jmp 0x6b071
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x6c1c0
movq %rax, 0x40(%rsp)
jmp 0x6b073
jmp 0x6b089
leaq 0xb0(%rsp), %rdi
xorl %esi, %esi
callq 0x6c1c0
movq %rax, 0x38(%rsp)
jmp 0x6ae52
movq 0xb8(%rsp), %rax
cmpl $0x0, 0x58(%rax)
jbe 0x6b101
movl $0x18, %edi
callq 0x8450
movq 0x20(%rsp), %rdi
movq %rax, 0x30(%rsp)
movq 0xb8(%rsp), %rax
movl 0x58(%rax), %ecx
shll $0x9, %ecx
movq 0x30(%rsp), %rax
movl %ecx, (%rax)
movq 0xb8(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x30(%rsp), %rax
movl $0x2, 0x10(%rax)
movq 0xb8(%rsp), %rax
movw 0x68(%rax), %cx
movq 0x30(%rsp), %rax
movw %cx, 0x14(%rax)
movq 0x30(%rsp), %rsi
callq 0x6b220
movq 0xb8(%rsp), %rdi
addq $0x40, %rdi
callq 0x59910
cmpq $0x0, %rax
jbe 0x6b137
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %rdi
movq 0xb8(%rsp), %rsi
addq $0x40, %rsi
movq (%rdi), %rax
callq *0x60(%rax)
jmp 0x6b217
jmp 0x6b217
leaq 0x6f5a0(%rip), %rax     # 0xda6e8
movq (%rax), %rax
movq 0xb8(%rsp), %rcx
movzwl 0x68(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movq %rax, 0x8(%rsp)
subq $0x3, %rax
ja 0x6b215
movq 0x8(%rsp), %rax
leaq 0x420c3(%rip), %rcx     # 0xad238
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %rdi
movq 0xb8(%rsp), %rsi
addq $0x40, %rsi
movq (%rdi), %rax
callq *0x60(%rax)
jmp 0x6b217
movq 0x20(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x6b320
movq 0x20(%rsp), %rax
movq 0xb8(%rsp), %rcx
movzwl 0x68(%rcx), %ecx
movl %ecx, 0x2c(%rsp)
testb $0x1, 0xe0(%rax)
je 0x6b1d7
movl $0x0, 0x2c(%rsp)
movq 0xb8(%rsp), %rdi
addq $0x40, %rdi
callq 0x59910
cmpq $0x0, %rax
jbe 0x6b213
movq 0x20(%rsp), %rax
movq 0xd8(%rax), %rdi
movslq 0x2c(%rsp), %rax
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x6c210
jmp 0x6b217
jmp 0x6b217
addq $0xc8, %rsp
retq
nop