// File: CMU-SAFARI[P]MQSim/src/ssd/TSU_OutofOrder.cpp
// Function: SSD_Components::TSU_OutOfOrder::Schedule()
void TSU_OutOfOrder::Schedule()
{
	opened_scheduling_reqs--;
	if (opened_scheduling_reqs > 0)
	{
		return;
	}

	if (opened_scheduling_reqs < 0)
	{
		PRINT_ERROR("TSU_OutOfOrder: Illegal status!");
	}

	if (transaction_receive_slots.size() == 0)
	{
		return;
	}

	for (std::list<NVM_Transaction_Flash *>::iterator it = transaction_receive_slots.begin(); it != transaction_receive_slots.end(); it++)
	{
		switch ((*it)->Type)
		{
		case Transaction_Type::READ:
			switch ((*it)->Source)
			{
			case Transaction_Source_Type::CACHE:
			case Transaction_Source_Type::USERIO:
				UserReadTRQueue[(*it)->Address.ChannelID][(*it)->Address.ChipID].push_back((*it));
				break;
			case Transaction_Source_Type::MAPPING:
				MappingReadTRQueue[(*it)->Address.ChannelID][(*it)->Address.ChipID].push_back((*it));
				break;
			case Transaction_Source_Type::GC_WL:
				GCReadTRQueue[(*it)->Address.ChannelID][(*it)->Address.ChipID].push_back((*it));
				break;
			default:
				PRINT_ERROR("TSU_OutOfOrder: unknown source type for a read transaction!")
			}
			break;
		case Transaction_Type::WRITE:
			switch ((*it)->Source)
			{
			case Transaction_Source_Type::CACHE:
			case Transaction_Source_Type::USERIO:
				UserWriteTRQueue[(*it)->Address.ChannelID][(*it)->Address.ChipID].push_back((*it));
				break;
			case Transaction_Source_Type::MAPPING:
				MappingWriteTRQueue[(*it)->Address.ChannelID][(*it)->Address.ChipID].push_back((*it));
				break;
			case Transaction_Source_Type::GC_WL:
				GCWriteTRQueue[(*it)->Address.ChannelID][(*it)->Address.ChipID].push_back((*it));
				break;
			default:
				PRINT_ERROR("TSU_OutOfOrder: unknown source type for a write transaction!")
			}
			break;
		case Transaction_Type::ERASE:
			GCEraseTRQueue[(*it)->Address.ChannelID][(*it)->Address.ChipID].push_back((*it));
			break;
		default:
			break;
		}
	}

	for (flash_channel_ID_type channelID = 0; channelID < channel_count; channelID++)
	{
		if (_NVMController->Get_channel_status(channelID) == BusChannelStatus::IDLE)
		{
			for (unsigned int i = 0; i < chip_no_per_channel; i++)
			{
				NVM::FlashMemory::Flash_Chip *chip = _NVMController->Get_chip(channelID, Round_robin_turn_of_channel[channelID]);
				//The TSU does not check if the chip is idle or not since it is possible to suspend a busy chip and issue a new command
				process_chip_requests(chip);
				Round_robin_turn_of_channel[channelID] = (flash_chip_ID_type)(Round_robin_turn_of_channel[channelID] + 1) % chip_no_per_channel;
				if (_NVMController->Get_channel_status(chip->ChannelID) != BusChannelStatus::IDLE)
				{
					break;
				}
			}
		}
	}
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0xc0(%rsp)
movl 0xa8(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0xa8(%rax)
cmpl $0x0, 0xa8(%rax)
jle 0x9e23c
jmp 0x9e96a
movq 0xc0(%rsp), %rax
cmpl $0x0, 0xa8(%rax)
jge 0x9e298
movq 0x3bd9c(%rip), %rdi     # 0xd9ff0
leaq 0xc68d(%rip), %rsi      # 0xaa8e8
callq 0x8440
movq 0x3bd89(%rip), %rdi     # 0xd9ff0
leaq 0x10709(%rip), %rsi     # 0xae977
callq 0x8440
movq %rax, %rdi
movq 0x3bcdb(%rip), %rsi     # 0xd9f58
callq 0x8480
movq 0x3bd47(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
movq 0xc0(%rsp), %rdi
addq $0x78, %rdi
callq 0x91c80
cmpq $0x0, %rax
jne 0x9e2b4
jmp 0x9e96a
movq 0xc0(%rsp), %rdi
addq $0x78, %rdi
callq 0x7ff20
movq %rax, 0xe8(%rsp)
movq 0xc0(%rsp), %rdi
addq $0x78, %rdi
callq 0x5d430
movq %rax, 0xe0(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x80030
testb $0x1, %al
jne 0x9e304
jmp 0x9e83a
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq (%rax), %rax
movl 0x8(%rax), %eax
movl %eax, 0xbc(%rsp)
testl %eax, %eax
je 0x9e34b
jmp 0x9e324
movl 0xbc(%rsp), %eax
subl $0x1, %eax
je 0x9e58d
jmp 0x9e336
movl 0xbc(%rsp), %eax
subl $0x2, %eax
je 0x9e79c
jmp 0x9e81a
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq (%rax), %rax
movl 0x4(%rax), %eax
movl %eax, 0xb8(%rsp)
subl $0x2, %eax
jb 0x9e393
jmp 0x9e36c
movl 0xb8(%rsp), %eax
subl $0x2, %eax
je 0x9e4b9
jmp 0x9e37e
movl 0xb8(%rsp), %eax
subl $0x3, %eax
je 0x9e426
jmp 0x9e53d
movq 0xc0(%rsp), %rax
movq 0xb0(%rax), %rax
movq %rax, 0xa0(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0xa0(%rsp), %rax
movq (%rcx), %rcx
movl 0x38(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0xa8(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0xa8(%rsp), %rax
movq (%rcx), %rcx
movl 0x3c(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0xb0(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq 0xb0(%rsp), %rdi
movq %rax, %rsi
callq 0x7ef00
jmp 0x9e588
movq 0xc0(%rsp), %rax
movq 0xd8(%rax), %rax
movq %rax, 0x88(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x88(%rsp), %rax
movq (%rcx), %rcx
movl 0x38(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x90(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq (%rcx), %rcx
movl 0x3c(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x98(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq 0x98(%rsp), %rdi
movq %rax, %rsi
callq 0x7ef00
jmp 0x9e588
movq 0xc0(%rsp), %rax
movq 0xc0(%rax), %rax
movq %rax, 0x70(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x70(%rsp), %rax
movq (%rcx), %rcx
movl 0x38(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x78(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x78(%rsp), %rax
movq (%rcx), %rcx
movl 0x3c(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x80(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq 0x80(%rsp), %rdi
movq %rax, %rsi
callq 0x7ef00
jmp 0x9e588
movq 0x3baac(%rip), %rdi     # 0xd9ff0
leaq 0xc39d(%rip), %rsi      # 0xaa8e8
callq 0x8440
movq 0x3ba99(%rip), %rdi     # 0xd9ff0
leaq 0x10439(%rip), %rsi     # 0xae997
callq 0x8440
movq %rax, %rdi
movq 0x3b9eb(%rip), %rsi     # 0xd9f58
callq 0x8480
movq 0x3ba57(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
jmp 0x9e81c
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq (%rax), %rax
movl 0x4(%rax), %eax
movl %eax, 0x6c(%rsp)
subl $0x2, %eax
jb 0x9e5cc
jmp 0x9e5ab
movl 0x6c(%rsp), %eax
subl $0x2, %eax
je 0x9e6ce
jmp 0x9e5ba
movl 0x6c(%rsp), %eax
subl $0x3, %eax
je 0x9e64d
jmp 0x9e74c
movq 0xc0(%rsp), %rax
movq 0xb8(%rax), %rax
movq %rax, 0x50(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x50(%rsp), %rax
movq (%rcx), %rcx
movl 0x38(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x58(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x58(%rsp), %rax
movq (%rcx), %rcx
movl 0x3c(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x60(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq 0x60(%rsp), %rdi
movq %rax, %rsi
callq 0x7ef00
jmp 0x9e797
movq 0xc0(%rsp), %rax
movq 0xe0(%rax), %rax
movq %rax, 0x38(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x38(%rsp), %rax
movq (%rcx), %rcx
movl 0x38(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x40(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x40(%rsp), %rax
movq (%rcx), %rcx
movl 0x3c(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x48(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x7ef00
jmp 0x9e797
movq 0xc0(%rsp), %rax
movq 0xc8(%rax), %rax
movq %rax, 0x20(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq (%rcx), %rcx
movl 0x38(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x28(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq (%rcx), %rcx
movl 0x3c(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x30(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x7ef00
jmp 0x9e797
movq 0x3b89d(%rip), %rdi     # 0xd9ff0
leaq 0xc18e(%rip), %rsi      # 0xaa8e8
callq 0x8440
movq 0x3b88a(%rip), %rdi     # 0xd9ff0
leaq 0x10266(%rip), %rsi     # 0xae9d3
callq 0x8440
movq %rax, %rdi
movq 0x3b7dc(%rip), %rsi     # 0xd9f58
callq 0x8480
movq 0x3b848(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
jmp 0x9e81c
movq 0xc0(%rsp), %rax
movq 0xd0(%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq (%rcx), %rcx
movl 0x38(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x10(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq %rax, %rcx
movq 0x10(%rsp), %rax
movq (%rcx), %rcx
movl 0x3c(%rcx), %ecx
imulq $0x108, %rcx, %rcx      # imm = 0x108
addq %rcx, %rax
movq %rax, 0x18(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7fda0
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x7ef00
jmp 0x9e81c
jmp 0x9e81c
jmp 0x9e81e
leaq 0xe8(%rsp), %rdi
xorl %esi, %esi
callq 0x91c50
movq %rax, 0xd8(%rsp)
jmp 0x9e2cd
movl $0x0, 0xd4(%rsp)
movq 0xc0(%rsp), %rcx
movl 0xd4(%rsp), %eax
cmpl 0x44(%rcx), %eax
jae 0x9e96a
movq 0xc0(%rsp), %rax
movq 0x38(%rax), %rdi
movl 0xd4(%rsp), %esi
movq (%rdi), %rax
callq *0x28(%rax)
cmpl $0x1, %eax
jne 0x9e952
movl $0x0, 0xd0(%rsp)
movq 0xc0(%rsp), %rcx
movl 0xd0(%rsp), %eax
cmpl 0x48(%rcx), %eax
jae 0x9e950
movq 0xc0(%rsp), %rax
movq 0x38(%rax), %rdi
movl 0xd4(%rsp), %esi
movq 0x70(%rax), %rax
movl 0xd4(%rsp), %ecx
movl (%rax,%rcx,4), %edx
movq (%rdi), %rax
callq *0x30(%rax)
movq 0xc0(%rsp), %rdi
movq %rax, 0xc8(%rsp)
movq 0xc8(%rsp), %rsi
callq 0x9b780
movq 0xc0(%rsp), %rcx
movq 0x70(%rcx), %rax
movl 0xd4(%rsp), %edx
movl (%rax,%rdx,4), %eax
addl $0x1, %eax
xorl %edx, %edx
divl 0x48(%rcx)
movq 0xc0(%rsp), %rax
movl %edx, %esi
movq 0x70(%rax), %rcx
movl 0xd4(%rsp), %edx
movl %esi, (%rcx,%rdx,4)
movq 0x38(%rax), %rdi
movq 0xc8(%rsp), %rax
movl 0x2c(%rax), %esi
movq (%rdi), %rax
callq *0x28(%rax)
cmpl $0x1, %eax
je 0x9e938
jmp 0x9e950
jmp 0x9e93a
movl 0xd0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd0(%rsp)
jmp 0x9e88a
jmp 0x9e952
jmp 0x9e954
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x9e845
addq $0xf8, %rsp
retq
nopw %cs:(%rax,%rax)