// File: CMU-SAFARI[P]MQSim/src/ssd/TSU_Priority_OutOfOrder.cpp
// Function: SSD_Components::TSU_Priority_OutOfOrder::service_read_transaction(NVM::FlashMemory::Flash_Chip*)
bool TSU_Priority_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
        {
            sourceQueue2 = get_next_read_service_queue(chip);
        }
    }
    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];
            sourceQueue2 = get_next_read_service_queue(chip);
        }
        else if (GCWriteTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
        {
            return false;
        }
        else if (GCEraseTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
        {
            return false;
        }
        else
        {
            sourceQueue1 = get_next_read_service_queue(chip);
            if (sourceQueue1 == NULL)
            {
                return false;
            }
        }
    }
    else
    {
        //If GC is currently executed in the preemptive mode, then user IO transaction queues are checked first
        sourceQueue1 = get_next_read_service_queue(chip);
        if (sourceQueue1 != NULL)
        {
            if (GCReadTRQueue[chip->ChannelID][chip->ChipID].size() > 0)
            {
                sourceQueue2 = &GCReadTRQueue[chip->ChannelID][chip->ChipID];
            }
        }
        else if (get_next_write_service_queue(chip) != NULL)
        {
            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 0xa336d
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 0xa32ea
jmp 0xa3354
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 0xa3354
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 0xa3368
movq 0x20(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0xa2f30
movq %rax, 0x30(%rsp)
jmp 0xa35e5
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 0xa338e
jmp 0xa34c1
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 0xa340a
movq 0x20(%rsp), %rdi
movq 0xc0(%rdi), %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)
movq 0x40(%rsp), %rsi
callq 0xa2f30
movq %rax, 0x30(%rsp)
jmp 0xa34bc
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 0xa344d
movb $0x0, 0x57(%rsp)
jmp 0xa374f
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 0xa3490
movb $0x0, 0x57(%rsp)
jmp 0xa374f
movq 0x20(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0xa2f30
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
jne 0xa34b6
movb $0x0, 0x57(%rsp)
jmp 0xa374f
jmp 0xa34b8
jmp 0xa34ba
jmp 0xa34bc
jmp 0xa35e3
movq 0x20(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0xa2f30
movq %rax, 0x38(%rsp)
cmpq $0x0, 0x38(%rsp)
je 0xa354a
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 0xa3545
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 0xa35e1
movq 0x20(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0xa3760
cmpq $0x0, %rax
je 0xa3569
movb $0x0, 0x57(%rsp)
jmp 0xa374f
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 0xa35d3
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 0xa35dd
movb $0x0, 0x57(%rsp)
jmp 0xa374f
jmp 0xa35df
jmp 0xa35e1
jmp 0xa35e3
jmp 0xa35e5
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 0xa3630
jmp 0xa3613
movl 0x1c(%rsp), %eax
subl $0x5, %eax
je 0xa3635
jmp 0xa361e
movl 0x1c(%rsp), %eax
subl $0x6, %eax
je 0xa36af
jmp 0xa3723
jmp 0xa372a
movq 0x20(%rsp), %rax
testb $0x1, 0x55(%rax)
je 0xa365a
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x40(%rsp), %rsi
movq (%rdi), %rax
callq *0x40(%rax)
testb $0x1, %al
jne 0xa365a
jmp 0xa3664
movb $0x0, 0x57(%rsp)
jmp 0xa374f
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 0xa36aa
movb $0x0, 0x57(%rsp)
jmp 0xa374f
movb $0x1, 0x2f(%rsp)
movq 0x20(%rsp), %rax
testb $0x1, 0x54(%rax)
je 0xa36d4
movq 0x20(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x40(%rsp), %rsi
movq (%rdi), %rax
callq *0x40(%rax)
testb $0x1, %al
jne 0xa36d4
jmp 0xa36db
movb $0x0, 0x57(%rsp)
jmp 0xa374f
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 0xa371e
movb $0x0, 0x57(%rsp)
jmp 0xa374f
movb $0x1, 0x2f(%rsp)
movb $0x0, 0x57(%rsp)
jmp 0xa374f
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 (%rax,%rax)