// File: CMU-SAFARI[P]MQSim/src/ssd/TSU_OutofOrder.cpp
// Function: SSD_Components::TSU_OutOfOrder::service_read_transaction(NVM::FlashMemory::Flash_Chip*)
bool TSU_OutOfOrder::service_read_transaction(NVM::FlashMemory::Flash_Chip *chip)
{
	Flash_Transaction_Queue *sourceQueue1 = NULL, *sourceQueue2 = NULL;

	//Flash transactions that are related to FTL mapping data have the highest priority
	if (MappingReadTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
	{
		sourceQueue1 = &MappingReadTRQueue[chip->ChannelID][chip->ChipID];
		if (ftl->GC_and_WL_Unit->GC_is_in_urgent_mode(chip) && GCReadTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
		{
			sourceQueue2 = &GCReadTRQueue[chip->ChannelID][chip->ChipID];
		}
		else if (UserReadTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
		{
			sourceQueue2 = &UserReadTRQueue[chip->ChannelID][chip->ChipID];
		}
	}
	else if (ftl->GC_and_WL_Unit->GC_is_in_urgent_mode(chip))
	{
		//If flash transactions related to GC are prioritzed (non-preemptive execution mode of GC), then GC queues are checked first

		if (GCReadTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
		{
			sourceQueue1 = &GCReadTRQueue[chip->ChannelID][chip->ChipID];
			if (UserReadTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
			{
				sourceQueue2 = &UserReadTRQueue[chip->ChannelID][chip->ChipID];
			}
		}
		else if (GCWriteTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
		{
			return false;
		}
		else if (GCEraseTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
		{
			return false;
		}
		else if (UserReadTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
		{
			sourceQueue1 = &UserReadTRQueue[chip->ChannelID][chip->ChipID];
		}
		else
		{
			return false;
		}
	}
	else
	{
		//If GC is currently executed in the preemptive mode, then user IO transaction queues are checked first

		if (UserReadTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
		{
			sourceQueue1 = &UserReadTRQueue[chip->ChannelID][chip->ChipID];
			if (GCReadTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
			{
				sourceQueue2 = &GCReadTRQueue[chip->ChannelID][chip->ChipID];
			}
		}
		else if (UserWriteTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
		{
			return false;
		}
		else if (GCReadTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
		{
			sourceQueue1 = &GCReadTRQueue[chip->ChannelID][chip->ChipID];
		}
		else
		{
			return false;
		}
	}

	bool suspensionRequired = false;
	ChipStatus cs = _NVMController->GetChipStatus(chip);
	switch (cs)
	{
	case ChipStatus::IDLE:
		break;
	case ChipStatus::WRITING:
		if (!programSuspensionEnabled || _NVMController->HasSuspendedCommand(chip))
		{
			return false;
		}
		if (_NVMController->Expected_finish_time(chip) - Simulator->Time() < writeReasonableSuspensionTimeForRead)
		{
			return false;
		}
		suspensionRequired = true;
	case ChipStatus::ERASING:
		if (!eraseSuspensionEnabled || _NVMController->HasSuspendedCommand(chip))
		{
			return false;
		}
		if (_NVMController->Expected_finish_time(chip) - Simulator->Time() < eraseReasonableSuspensionTimeForRead)
		{
			return false;
		}
		suspensionRequired = true;
	default:
		return false;
	}

	issue_command_to_chip(sourceQueue1, sourceQueue2, Transaction_Type::READ, suspensionRequired);

	return true;
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movq 0xd8(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9eb03
movq 0x20(%rsp), %rax
movq 0xd8(%rax), %rcx
movq 0x40(%rsp), %rdx
movl 0x2c(%rdx), %edx
movq (%rcx,%rdx,8), %rcx
movq 0x40(%rsp), %rdx
movl 0x30(%rdx), %edx
imulq $0x108, %rdx, %rdx      # imm = 0x108
addq %rdx, %rcx
movq %rcx, 0x38(%rsp)
movq 0x30(%rax), %rax
movq 0x48(%rax), %rdi
movq 0x40(%rsp), %rsi
movq (%rdi), %rax
callq *0x20(%rax)
testb $0x1, %al
jne 0x9ea2a
jmp 0x9ea94
movq 0x20(%rsp), %rax
movq 0xc0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9ea94
movq 0x20(%rsp), %rax
movq 0xc0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x40(%rsp), %rcx
movl 0x30(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x30(%rsp)
jmp 0x9eafe
movq 0x20(%rsp), %rax
movq 0xb0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9eafc
movq 0x20(%rsp), %rax
movq 0xb0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x40(%rsp), %rcx
movl 0x30(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x30(%rsp)
jmp 0x9eafe
jmp 0x9ee90
movq 0x20(%rsp), %rax
movq 0x30(%rax), %rax
movq 0x48(%rax), %rdi
movq 0x40(%rsp), %rsi
movq (%rdi), %rax
callq *0x20(%rax)
testb $0x1, %al
jne 0x9eb24
jmp 0x9ecfd
movq 0x20(%rsp), %rax
movq 0xc0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9ebf8
movq 0x20(%rsp), %rax
movq 0xc0(%rax), %rcx
movq 0x40(%rsp), %rdx
movl 0x2c(%rdx), %edx
movq (%rcx,%rdx,8), %rcx
movq 0x40(%rsp), %rdx
movl 0x30(%rdx), %edx
imulq $0x108, %rdx, %rdx      # imm = 0x108
addq %rdx, %rcx
movq %rcx, 0x38(%rsp)
movq 0xb0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9ebf3
movq 0x20(%rsp), %rax
movq 0xb0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x40(%rsp), %rcx
movl 0x30(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x30(%rsp)
jmp 0x9ecf8
movq 0x20(%rsp), %rax
movq 0xc8(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9ec3b
movb $0x0, 0x57(%rsp)
jmp 0x9effa
movq 0x20(%rsp), %rax
movq 0xd0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9ec7e
movb $0x0, 0x57(%rsp)
jmp 0x9effa
movq 0x20(%rsp), %rax
movq 0xb0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9ece8
movq 0x20(%rsp), %rax
movq 0xb0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x40(%rsp), %rcx
movl 0x30(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x38(%rsp)
jmp 0x9ecf2
movb $0x0, 0x57(%rsp)
jmp 0x9effa
jmp 0x9ecf4
jmp 0x9ecf6
jmp 0x9ecf8
jmp 0x9ee8e
movq 0x20(%rsp), %rax
movq 0xb0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9edd1
movq 0x20(%rsp), %rax
movq 0xb0(%rax), %rcx
movq 0x40(%rsp), %rdx
movl 0x2c(%rdx), %edx
movq (%rcx,%rdx,8), %rcx
movq 0x40(%rsp), %rdx
movl 0x30(%rdx), %edx
imulq $0x108, %rdx, %rdx      # imm = 0x108
addq %rdx, %rcx
movq %rcx, 0x38(%rsp)
movq 0xc0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9edcc
movq 0x20(%rsp), %rax
movq 0xc0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x40(%rsp), %rcx
movl 0x30(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x30(%rsp)
jmp 0x9ee8c
movq 0x20(%rsp), %rax
movq 0xb8(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9ee14
movb $0x0, 0x57(%rsp)
jmp 0x9effa
movq 0x20(%rsp), %rax
movq 0xc0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rdi
movq 0x40(%rsp), %rax
movl 0x30(%rax), %eax
imulq $0x108, %rax, %rax      # imm = 0x108
addq %rax, %rdi
addq $0x8, %rdi
callq 0x91c80
cmpq $0x0, %rax
jbe 0x9ee7e
movq 0x20(%rsp), %rax
movq 0xc0(%rax), %rax
movq 0x40(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x40(%rsp), %rcx
movl 0x30(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x38(%rsp)
jmp 0x9ee88
movb $0x0, 0x57(%rsp)
jmp 0x9effa
jmp 0x9ee8a
jmp 0x9ee8c
jmp 0x9ee8e
jmp 0x9ee90
movq 0x20(%rsp), %rax
movb $0x0, 0x2f(%rsp)
movq 0x38(%rax), %rdi
movq 0x40(%rsp), %rsi
movq (%rdi), %rax
movq 0x48(%rax), %rax
callq *%rax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x1c(%rsp)
testl %eax, %eax
je 0x9eedb
jmp 0x9eebe
movl 0x1c(%rsp), %eax
subl $0x5, %eax
je 0x9eee0
jmp 0x9eec9
movl 0x1c(%rsp), %eax
subl $0x6, %eax
je 0x9ef5a
jmp 0x9efce
jmp 0x9efd5
movq 0x20(%rsp), %rax
testb $0x1, 0x55(%rax)
je 0x9ef05
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x40(%rsp), %rsi
movq (%rdi), %rax
callq *0x40(%rax)
testb $0x1, %al
jne 0x9ef05
jmp 0x9ef0f
movb $0x0, 0x57(%rsp)
jmp 0x9effa
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x40(%rsp), %rsi
movq (%rdi), %rax
callq *0x50(%rax)
movq %rax, 0x10(%rsp)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq 0x20(%rsp), %rcx
movq %rax, %rdx
movq 0x10(%rsp), %rax
subq %rdx, %rax
cmpq 0x58(%rcx), %rax
jae 0x9ef55
movb $0x0, 0x57(%rsp)
jmp 0x9effa
movb $0x1, 0x2f(%rsp)
movq 0x20(%rsp), %rax
testb $0x1, 0x54(%rax)
je 0x9ef7f
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x40(%rsp), %rsi
movq (%rdi), %rax
callq *0x40(%rax)
testb $0x1, %al
jne 0x9ef7f
jmp 0x9ef86
movb $0x0, 0x57(%rsp)
jmp 0x9effa
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x40(%rsp), %rsi
movq (%rdi), %rax
callq *0x50(%rax)
movq %rax, 0x8(%rsp)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq 0x20(%rsp), %rcx
movq %rax, %rdx
movq 0x8(%rsp), %rax
subq %rdx, %rax
cmpq 0x60(%rcx), %rax
jae 0x9efc9
movb $0x0, 0x57(%rsp)
jmp 0x9effa
movb $0x1, 0x2f(%rsp)
movb $0x0, 0x57(%rsp)
jmp 0x9effa
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdx
movb 0x2f(%rsp), %al
xorl %ecx, %ecx
andb $0x1, %al
movzbl %al, %r8d
callq 0x9b2a0
movb $0x1, 0x57(%rsp)
movb 0x57(%rsp), %al
andb $0x1, %al
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)