// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]MQSim/src/ssd/Host_Interface_NVMe.cpp
// Function: SSD_Components::Request_Fetch_Unit_NVMe::Process_pcie_read_message(unsigned long, void*, unsigned int)
void Request_Fetch_Unit_NVMe::Process_pcie_read_message(uint64_t address, void *payload, unsigned int payload_size)
{
	Host_Interface_NVMe *hi = (Host_Interface_NVMe *)host_interface;
	DMA_Req_Item *dma_req_item = dma_list.front();
	dma_list.pop_front();

	switch (dma_req_item->Type)
	{
	case DMA_Req_Type::REQUEST_INFO:
	{
		User_Request *new_request = new User_Request;
		new_request->IO_command_info = payload;
		new_request->Stream_id = (stream_id_type)((uint64_t)(dma_req_item->object));
		new_request->Priority_class = ((Input_Stream_Manager_NVMe *)host_interface->input_stream_manager)->Get_priority_class(new_request->Stream_id);
		new_request->STAT_InitiationTime = Simulator->Time();
		Submission_Queue_Entry *sqe = (Submission_Queue_Entry *)payload;
		switch (sqe->Opcode)
		{
		case NVME_READ_OPCODE:
			new_request->Type = UserRequestType::READ;
			new_request->Start_LBA = ((LHA_type)sqe->Command_specific[1]) << 31 | (LHA_type)sqe->Command_specific[0]; //Command Dword 10 and Command Dword 11
			new_request->SizeInSectors = sqe->Command_specific[2] & (LHA_type)(0x0000ffff);
			new_request->Size_in_byte = new_request->SizeInSectors * SECTOR_SIZE_IN_BYTE;
			break;
		case NVME_WRITE_OPCODE:
			new_request->Type = UserRequestType::WRITE;
			new_request->Start_LBA = ((LHA_type)sqe->Command_specific[1]) << 31 | (LHA_type)sqe->Command_specific[0]; //Command Dword 10 and Command Dword 11
			new_request->SizeInSectors = sqe->Command_specific[2] & (LHA_type)(0x0000ffff);
			new_request->Size_in_byte = new_request->SizeInSectors * SECTOR_SIZE_IN_BYTE;
			break;
		default:
			throw std::invalid_argument("NVMe command is not supported!");
		}
		((Input_Stream_Manager_NVMe *)(hi->input_stream_manager))->Handle_new_arrived_request(new_request);
		break;
	}
	case DMA_Req_Type::WRITE_DATA:
		COPYDATA(((User_Request *)dma_req_item->object)->Data, payload, payload_size);
		((Input_Stream_Manager_NVMe *)(hi->input_stream_manager))->Handle_arrived_write_data((User_Request *)dma_req_item->object);
		break;
	default:
		break;
	}
	delete dma_req_item;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %ecx, %ebp
movq %rdx, %r14
movq %rdi, %r12
movq 0x8(%rdi), %r13
movq 0x10(%rdi), %r15
movq 0x10(%r15), %rbx
decq 0x20(%rdi)
movq %r15, %rdi
callq 0x7580
movq %r15, %rdi
callq 0x7370
movl (%rbx), %eax
cmpl $0x1, %eax
je 0x42219
testl %eax, %eax
jne 0x4229e
movl $0x80, %edi
callq 0x73a0
movq %rax, %r15
movq %rax, %rdi
callq 0x5224c
movq %r14, 0x70(%r15)
movq 0x8(%rbx), %rax
movw %ax, 0x68(%r15)
movq 0x8(%r12), %rcx
movq 0x50(%rcx), %rcx
movzwl %ax, %eax
movq 0x10(%rcx), %rcx
movq (%rcx,%rax,8), %rax
movl 0x48(%rax), %eax
movl %eax, (%r15)
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
movq %rax, 0x30(%r15)
movzbl (%r14), %ecx
cmpl $0x1, %ecx
je 0x42265
xorl %eax, %eax
cmpl $0x2, %ecx
je 0x4226a
movl $0x10, %edi
callq 0x71d0
movq %rax, %rbx
leaq 0x17d7f(%rip), %rsi     # 0x59f7a
movq %rax, %rdi
callq 0x7680
movq 0x2cdbe(%rip), %rsi     # 0x6efc8
movq 0x2cd9f(%rip), %rdx     # 0x6efb0
movq %rbx, %rdi
callq 0x75e0
callq 0x281b4
movq %rax, %rdi
callq 0x28610
testb %al, %al
je 0x4224d
movl %ebp, %r15d
movq %r15, %rdi
callq 0x7030
movq 0x8(%rbx), %rcx
movq %rax, 0x78(%rcx)
movq %rax, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x7310
jmp 0x42255
movq 0x8(%rbx), %rax
movq %r14, 0x78(%rax)
movq 0x50(%r13), %rdi
movq 0x8(%rbx), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
jmp 0x4229e
movl $0x1, %eax
movl %eax, 0x64(%r15)
movl 0x30(%r14), %eax
movl 0x34(%r14), %ecx
shlq $0x1f, %rcx
orq %rcx, %rax
movq %rax, 0x28(%r15)
movzwl 0x38(%r14), %eax
movl %eax, 0x60(%r15)
shll $0x9, %eax
movl %eax, 0x5c(%r15)
movq 0x50(%r13), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x10(%rax)
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7370
movq %rax, %r14
movq %rbx, %rdi
callq 0x72c0
jmp 0x422cc
movq %rax, %r14
movq %r15, %rdi
callq 0x7370
movq %r14, %rdi
callq 0x7620