// File: 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:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xa8(%rsp), %rdi
addq $0x40, %rdi
callq 0x59910
cmpq $0x0, %rax
jne 0x6fa10
jmp 0x6fe9c
movq 0xa8(%rsp), %rax
cmpl $0x0, 0x64(%rax)
jne 0x6fda8
leaq 0x6acbf(%rip), %rax     # 0xda6e8
movq (%rax), %rax
movq 0xa8(%rsp), %rcx
movzwl 0x68(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0x14(%rsp)
testl %eax, %eax
je 0x6fa78
jmp 0x6fa45
movl 0x14(%rsp), %eax
subl $0x3, %eax
jne 0x6fd5d
jmp 0x6fa54
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %rdi
movq 0xa8(%rsp), %rsi
addq $0x40, %rsi
movq (%rdi), %rax
callq *0x60(%rax)
jmp 0x6fe9c
movq 0xa8(%rsp), %rdi
addq $0x40, %rdi
callq 0x6c0d0
movq %rax, 0xa0(%rsp)
movq 0xa8(%rsp), %rdi
addq $0x40, %rdi
callq 0x6c120
movq %rax, 0x98(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x6c100
testb $0x1, %al
jne 0x6fac8
jmp 0x6fcaf
leaq 0xa0(%rsp), %rdi
callq 0x6c150
movq %rax, %rcx
movq 0x18(%rsp), %rax
movq (%rcx), %rcx
movq %rcx, 0x90(%rsp)
movq 0xc0(%rax), %rdi
movq 0x90(%rsp), %rax
movw (%rax), %ax
movq 0x90(%rsp), %rcx
movq 0x58(%rcx), %rdx
movzwl %ax, %esi
callq 0x64390
testb $0x1, %al
jne 0x6fb17
jmp 0x6fc96
movq 0x18(%rsp), %rax
movq 0xc0(%rax), %rsi
movq 0x90(%rsp), %rax
movw (%rax), %ax
movq 0x90(%rsp), %rcx
movq 0x58(%rcx), %rcx
leaq 0x58(%rsp), %rdi
movzwl %ax, %edx
callq 0x644c0
movq 0x58(%rsp), %rax
movq 0x90(%rsp), %rcx
andq 0x90(%rcx), %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rax
movq 0x90(%rsp), %rcx
cmpq 0x90(%rcx), %rax
jne 0x6fbed
movq 0x90(%rsp), %rax
movq 0x90(%rax), %rdi
callq 0x5a170
movl %eax, %ecx
movq 0xa8(%rsp), %rax
addl 0x58(%rax), %ecx
movl %ecx, 0x58(%rax)
movq 0xa8(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x8(%rsp)
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x6c1c0
movq %rax, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x6c1f0
movq 0x8(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x6c170
movq %rax, 0x40(%rsp)
jmp 0x6fc94
cmpq $0x0, 0x88(%rsp)
je 0x6fc7e
movq 0x88(%rsp), %rdi
callq 0x5a170
movl %eax, %ecx
movq 0xa8(%rsp), %rax
addl 0x58(%rax), %ecx
movl %ecx, 0x58(%rax)
movq 0x90(%rsp), %rax
movq 0x90(%rax), %rcx
movq 0x88(%rsp), %rax
xorq $-0x1, %rax
andq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x90(%rax)
movq 0x88(%rsp), %rdi
callq 0x5a170
movl %eax, %edx
shll $0x9, %edx
movq 0x90(%rsp), %rax
movl 0x50(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x50(%rax)
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x6c1c0
movq %rax, 0x38(%rsp)
jmp 0x6fc92
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x6c1c0
movq %rax, 0x30(%rsp)
jmp 0x6fc94
jmp 0x6fcaa
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
callq 0x6c1c0
movq %rax, 0x28(%rsp)
jmp 0x6fa91
movq 0xa8(%rsp), %rax
cmpl $0x0, 0x58(%rax)
jbe 0x6fd22
movl $0x18, %edi
callq 0x8450
movq 0x18(%rsp), %rdi
movq %rax, 0x20(%rsp)
movq 0xa8(%rsp), %rax
movl 0x58(%rax), %ecx
shll $0x9, %ecx
movq 0x20(%rsp), %rax
movl %ecx, (%rax)
movq 0xa8(%rsp), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x20(%rsp), %rax
movl $0x2, 0x10(%rax)
movq 0xa8(%rsp), %rax
movw 0x68(%rax), %cx
movq 0x20(%rsp), %rax
movw %cx, 0x14(%rax)
movq 0x20(%rsp), %rsi
callq 0x6feb0
movq 0xa8(%rsp), %rdi
addq $0x40, %rdi
callq 0x59910
cmpq $0x0, %rax
jbe 0x6fd58
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %rdi
movq 0xa8(%rsp), %rsi
addq $0x40, %rsi
movq (%rdi), %rax
callq *0x60(%rax)
jmp 0x6fe9c
movq 0x6a28c(%rip), %rdi     # 0xd9ff0
leaq 0x3ab7d(%rip), %rsi     # 0xaa8e8
callq 0x8440
movq 0x6a279(%rip), %rdi     # 0xd9ff0
leaq 0x3d500(%rip), %rsi     # 0xad27e
callq 0x8440
movq %rax, %rdi
movq 0x6a1cb(%rip), %rsi     # 0xd9f58
callq 0x8480
movq 0x6a237(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
leaq 0x6a939(%rip), %rax     # 0xda6e8
movq (%rax), %rax
movq 0xa8(%rsp), %rcx
movzwl 0x68(%rcx), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, 0x4(%rsp)
testl %eax, %eax
je 0x6fdfa
jmp 0x6fdcb
movl 0x4(%rsp), %eax
subl $0x3, %eax
jne 0x6fe51
jmp 0x6fdd6
movq 0x18(%rsp), %rax
movq 0x38(%rax), %rax
movq 0x38(%rax), %rdi
movq 0xa8(%rsp), %rsi
addq $0x40, %rsi
movq (%rdi), %rax
callq *0x60(%rax)
jmp 0x6fe9c
movq 0x18(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x6ff80
movq 0xa8(%rsp), %rdi
addq $0x40, %rdi
callq 0x59910
cmpq $0x0, %rax
jbe 0x6fe4f
movq 0x18(%rsp), %rax
movq 0xd0(%rax), %rdi
movq 0xa8(%rsp), %rax
movzwl 0x68(%rax), %eax
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x6c210
jmp 0x6fe9c
movq 0x6a198(%rip), %rdi     # 0xd9ff0
leaq 0x3aa89(%rip), %rsi     # 0xaa8e8
callq 0x8440
movq 0x6a185(%rip), %rdi     # 0xd9ff0
leaq 0x3d40c(%rip), %rsi     # 0xad27e
callq 0x8440
movq %rax, %rdi
movq 0x6a0d7(%rip), %rsi     # 0xd9f58
callq 0x8480
movq 0x6a143(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
addq $0xb8, %rsp
retq
nopw %cs:(%rax,%rax)