// File: /workspace/github_cpp_makefile_star2_O1/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
cmpq $0x0, 0x50(%rsi)
je 0x35ea7
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x40(%rsi), %r14
cmpl $0x0, 0x64(%rsi)
leaq 0x39b32(%rip), %rax     # 0x6f628
movq (%rax), %rax
movzwl 0x68(%rsi), %ecx
movl (%rax,%rcx,4), %eax
je 0x35b52
testl %eax, %eax
jne 0x35b56
movq %rbx, %rdi
movq %r15, %rsi
callq 0x36196
cmpq $0x0, 0x50(%r15)
je 0x35ea7
movq 0xd0(%rbx), %rax
movzwl 0x68(%r15), %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rbx
movl $0x18, %edi
callq 0x73a0
movq %r15, 0x10(%rax)
movq %rax, %rdi
movq %rbx, %rsi
callq 0x7060
incq 0x10(%rbx)
jmp 0x35ea7
testl %eax, %eax
je 0x35b81
cmpl $0x3, %eax
jne 0x35eb6
movq 0x38(%rbx), %rax
movq 0x38(%rax), %rdi
movq (%rdi), %rax
movq 0x60(%rax), %rax
movq %r14, %rsi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax
movq (%r14), %r12
cmpq %r14, %r12
je 0x35e59
leaq 0x8(%rsp), %r13
movq 0x10(%r12), %rbp
movq 0xc0(%rbx), %rdi
movzwl (%rbp), %esi
movq 0x58(%rbp), %rdx
callq 0x31d3e
testb %al, %al
je 0x35e4c
movq 0xc0(%rbx), %rsi
movzwl (%rbp), %edx
movq 0x58(%rbp), %rcx
movq %r13, %rdi
callq 0x31dfc
movq 0x90(%rbp), %rax
movq 0x8(%rsp), %rcx
andq %rax, %rcx
cmpq %rax, %rcx
jne 0x35ccb
movq %rax, %xmm0
pshufd $0x44, %xmm0, %xmm1     # xmm1 = xmm0[0,1,0,1]
pxor %xmm0, %xmm0
movl $0x40, %eax
movdqa 0x22903(%rip), %xmm2    # 0x58500
movdqa 0x228eb(%rip), %xmm3    # 0x584f0
movdqa 0x22902(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x228ff(%rip), %xmm11   # 0x58520
movdqa %xmm8, %xmm4
psllq %xmm2, %xmm4
pshufd $0xee, %xmm2, %xmm5     # xmm5 = xmm2[2,3,2,3]
movdqa %xmm8, %xmm6
psllq %xmm5, %xmm6
movsd %xmm4, %xmm6            # xmm6 = xmm4[0],xmm6[1]
movdqa %xmm8, %xmm4
psllq %xmm3, %xmm4
pshufd $0xee, %xmm3, %xmm5     # xmm5 = xmm3[2,3,2,3]
movdqa %xmm8, %xmm7
psllq %xmm5, %xmm7
movsd %xmm4, %xmm7            # xmm7 = xmm4[0],xmm7[1]
andpd %xmm1, %xmm7
andpd %xmm1, %xmm6
pcmpeqd %xmm9, %xmm6
pcmpeqd %xmm9, %xmm7
movdqa %xmm6, %xmm4
shufps $0xdd, %xmm7, %xmm4     # xmm4 = xmm4[1,3],xmm7[1,3]
shufps $0x88, %xmm7, %xmm6     # xmm6 = xmm6[0,2],xmm7[0,2]
andps %xmm4, %xmm6
paddd %xmm6, %xmm0
psubd %xmm10, %xmm0
paddq %xmm11, %xmm2
paddq %xmm11, %xmm3
addq $-0x4, %rax
jne 0x35c21
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
paddd %xmm0, %xmm1
pshufd $0x55, %xmm1, %xmm0     # xmm0 = xmm1[1,1,1,1]
paddd %xmm1, %xmm0
movd %xmm0, %eax
addl %eax, 0x58(%r15)
movq (%r12), %rbp
decq 0x50(%r15)
movq %r12, %rdi
callq 0x7580
movq %r12, %rdi
callq 0x7370
movq %rbp, %r12
jmp 0x35e50
testq %rcx, %rcx
movdqa 0x22839(%rip), %xmm8    # 0x58510
pxor %xmm9, %xmm9
pcmpeqd %xmm10, %xmm10
movdqa 0x22836(%rip), %xmm11   # 0x58520
je 0x35e4c
movq %rcx, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
pxor %xmm1, %xmm1
movl $0x40, %edx
movdqa 0x227f5(%rip), %xmm2    # 0x58500
movdqa 0x227dd(%rip), %xmm3    # 0x584f0
movdqa %xmm8, %xmm4
psllq %xmm2, %xmm4
pshufd $0xee, %xmm2, %xmm5     # xmm5 = xmm2[2,3,2,3]
movdqa %xmm8, %xmm6
psllq %xmm5, %xmm6
movsd %xmm4, %xmm6            # xmm6 = xmm4[0],xmm6[1]
movdqa %xmm8, %xmm4
psllq %xmm3, %xmm4
pshufd $0xee, %xmm3, %xmm5     # xmm5 = xmm3[2,3,2,3]
movdqa %xmm8, %xmm7
psllq %xmm5, %xmm7
movsd %xmm4, %xmm7            # xmm7 = xmm4[0],xmm7[1]
andpd %xmm0, %xmm7
andpd %xmm0, %xmm6
pcmpeqd %xmm9, %xmm6
pcmpeqd %xmm9, %xmm7
movdqa %xmm6, %xmm4
shufps $0xdd, %xmm7, %xmm4     # xmm4 = xmm4[1,3],xmm7[1,3]
shufps $0x88, %xmm7, %xmm6     # xmm6 = xmm6[0,2],xmm7[0,2]
andps %xmm4, %xmm6
paddd %xmm6, %xmm1
psubd %xmm10, %xmm1
paddq %xmm11, %xmm2
paddq %xmm11, %xmm3
addq $-0x4, %rdx
jne 0x35d13
pshufd $0xee, %xmm1, %xmm2     # xmm2 = xmm1[2,3,2,3]
paddd %xmm1, %xmm2
pshufd $0x55, %xmm2, %xmm1     # xmm1 = xmm2[1,1,1,1]
paddd %xmm2, %xmm1
movd %xmm1, %edx
addl %edx, 0x58(%r15)
xorq %rax, %rcx
movq %rcx, 0x90(%rbp)
pxor %xmm1, %xmm1
movl $0x40, %eax
movdqa 0x22748(%rip), %xmm2    # 0x58500
movdqa 0x22730(%rip), %xmm3    # 0x584f0
movdqa %xmm8, %xmm4
psllq %xmm2, %xmm4
pshufd $0xee, %xmm2, %xmm5     # xmm5 = xmm2[2,3,2,3]
movdqa %xmm8, %xmm6
psllq %xmm5, %xmm6
movsd %xmm4, %xmm6            # xmm6 = xmm4[0],xmm6[1]
movdqa %xmm8, %xmm4
psllq %xmm3, %xmm4
pshufd $0xee, %xmm3, %xmm5     # xmm5 = xmm3[2,3,2,3]
movdqa %xmm8, %xmm7
psllq %xmm5, %xmm7
movsd %xmm4, %xmm7            # xmm7 = xmm4[0],xmm7[1]
andpd %xmm0, %xmm7
andpd %xmm0, %xmm6
pcmpeqd %xmm9, %xmm6
pcmpeqd %xmm9, %xmm7
movdqa %xmm6, %xmm4
shufps $0xdd, %xmm7, %xmm4     # xmm4 = xmm4[1,3],xmm7[1,3]
shufps $0x88, %xmm7, %xmm6     # xmm6 = xmm6[0,2],xmm7[0,2]
andps %xmm4, %xmm6
paddd %xmm6, %xmm1
psubd %xmm10, %xmm1
paddq %xmm11, %xmm2
paddq %xmm11, %xmm3
addq $-0x4, %rax
jne 0x35dc0
pshufd $0xee, %xmm1, %xmm0     # xmm0 = xmm1[2,3,2,3]
paddd %xmm1, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
paddd %xmm0, %xmm1
movd %xmm1, %eax
shll $0x9, %eax
subl %eax, 0x50(%rbp)
movq (%r12), %r12
cmpq %r14, %r12
jne 0x35b92
movl 0x58(%r15), %ebp
testl %ebp, %ebp
je 0x35e8f
movl $0x18, %edi
callq 0x73a0
shll $0x9, %ebp
movl %ebp, (%rax)
movq %r15, 0x8(%rax)
movl $0x2, 0x10(%rax)
movzwl 0x68(%r15), %ecx
movw %cx, 0x14(%rax)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x35efa
cmpq $0x0, 0x50(%r15)
je 0x35ea7
movq 0x38(%rbx), %rax
movq 0x38(%rax), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x60(%rax)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x39133(%rip), %rbx     # 0x6eff0
leaq 0x206e4(%rip), %rsi     # 0x565a8
movq %rbx, %rdi
callq 0x7390
leaq 0x2331b(%rip), %rsi     # 0x591ee
movq %rbx, %rdi
callq 0x7390
movq %rax, %rdi
callq 0x71c0
movq 0x390e6(%rip), %rdi     # 0x6efd0
callq 0x7430
movl $0x1, %edi
callq 0x7440
nop