// File: /workspace/github_cpp_makefile_star2_O1/CMU-SAFARI[P]MQSim/src/ssd/TSU_Base.cpp
// Function: SSD_Components::TSU_Base::issue_command_to_chip(SSD_Components::Flash_Transaction_Queue*, SSD_Components::Flash_Transaction_Queue*, SSD_Components::Transaction_Type, bool)
bool TSU_Base::issue_command_to_chip(Flash_Transaction_Queue *sourceQueue1, Flash_Transaction_Queue *sourceQueue2, Transaction_Type transactionType, bool suspensionRequired)
	{
		flash_die_ID_type dieID = sourceQueue1->front()->Address.DieID;
		flash_page_ID_type pageID = sourceQueue1->front()->Address.PageID;
		unsigned int planeVector = 0;
		static int issueCntr = 0;
		
		for (unsigned int i = 0; i < die_no_per_chip; i++)
		{
			transaction_dispatch_slots.clear();
			planeVector = 0;

			for (Flash_Transaction_Queue::iterator it = sourceQueue1->begin(); it != sourceQueue1->end();)
			{
				if (transaction_is_ready(*it) && (*it)->Address.DieID == dieID && !(planeVector & 1 << (*it)->Address.PlaneID))
				{
					//Check for identical pages when running multiplane command
					if (planeVector == 0 || (*it)->Address.PageID == pageID)
					{
						(*it)->SuspendRequired = suspensionRequired;
						planeVector |= 1 << (*it)->Address.PlaneID;
						transaction_dispatch_slots.push_back(*it);
						DEBUG(issueCntr++ << ": " << Simulator->Time() <<" Issueing Transaction - Type:" << TRTOSTR((*it)) << ", PPA:" << (*it)->PPA << ", LPA:" << (*it)->LPA << ", Channel: " << (*it)->Address.ChannelID << ", Chip: " << (*it)->Address.ChipID);
						sourceQueue1->remove(it++);
						continue;
					}
				}
				it++;
			}

			if (sourceQueue2 != NULL && transaction_dispatch_slots.size() < plane_no_per_die)
			{
				for (Flash_Transaction_Queue::iterator it = sourceQueue2->begin(); it != sourceQueue2->end();)
				{
					if (transaction_is_ready(*it) && (*it)->Address.DieID == dieID && !(planeVector & 1 << (*it)->Address.PlaneID))
					{
						//Check for identical pages when running multiplane command
						if (planeVector == 0 || (*it)->Address.PageID == pageID)
						{
							(*it)->SuspendRequired = suspensionRequired;
							planeVector |= 1 << (*it)->Address.PlaneID;
							transaction_dispatch_slots.push_back(*it);
							DEBUG(issueCntr++ << ": " << Simulator->Time() << " Issueing Transaction - Type:" << TRTOSTR((*it)) << ", PPA:" << (*it)->PPA << ", LPA:" << (*it)->LPA << ", Channel: " << (*it)->Address.ChannelID << ", Chip: " << (*it)->Address.ChipID);
							sourceQueue2->remove(it++);
							continue;
						}
					}
					it++;
				}
			}

			if (transaction_dispatch_slots.size() > 0)
			{
				_NVMController->Send_command_to_chip(transaction_dispatch_slots);
				transaction_dispatch_slots.clear();
				dieID = (dieID + 1) % die_no_per_chip;
				return true;
			}
			else
			{
				transaction_dispatch_slots.clear();
				dieID = (dieID + 1) % die_no_per_chip;
				return false;
			}			
		}

		return false;
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl 0x4c(%rdi), %eax
testl %eax, %eax
je 0x4b2ee
movq %rsi, %r13
movq %rdi, %r12
movq %rdx, 0x10(%rsp)
movl %r8d, 0xc(%rsp)
movl %eax, 0x2c(%rsp)
leaq 0x8(%rsi), %r15
movq 0x8(%rsi), %rax
movq 0x10(%rax), %rax
movl 0x40(%rax), %ebp
movl 0x4c(%rax), %eax
movl %eax, 0x8(%rsp)
leaq 0x90(%rdi), %r14
movq 0x90(%rdi), %rdi
cmpq %r14, %rdi
je 0x4b1f8
movq (%rdi), %rbx
callq 0x7370
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x4b1e8
movq %r14, 0x98(%r12)
movq %r14, 0x90(%r12)
movq %r12, 0x18(%rsp)
movq $0x0, 0xa0(%r12)
movq (%r15), %rbx
cmpq %r15, %rbx
je 0x4b2f3
movq %r13, 0x30(%rsp)
xorl %r13d, %r13d
movq 0x10(%rsp), %rsi
movl 0x8(%rsp), %edi
movq %rbx, %r12
movq 0x10(%r12), %rax
movl 0x8(%rax), %ecx
cmpl $0x2, %ecx
je 0x4b255
cmpl $0x1, %ecx
jne 0x4b264
cmpq $0x0, 0x88(%rax)
jmp 0x4b25d
cmpq $0x0, 0x90(%rax)
sete %cl
testb %cl, %cl
je 0x4b283
cmpl %ebp, 0x40(%rax)
jne 0x4b283
movl 0x44(%rax), %ecx
movl $0x1, %edx
shll %cl, %edx
btl %ecx, %r13d
jb 0x4b283
testl %r13d, %r13d
je 0x4b28e
cmpl %edi, 0x4c(%rax)
je 0x4b28e
movq (%r12), %r12
cmpq %r15, %r12
jne 0x4b239
jmp 0x4b2ff
movl 0xc(%rsp), %ecx
movb %cl, 0x68(%rax)
orl %edx, %r13d
movl $0x18, %edi
callq 0x73a0
movq 0x10(%r12), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x7060
movq 0x18(%rsp), %rax
incq 0xa0(%rax)
movq (%r12), %rbx
movq %r12, 0x20(%rsp)
movq 0x30(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3ea54
movl 0x8(%rsp), %edi
movq 0x10(%rsp), %rsi
cmpq %r15, %rbx
jne 0x4b236
jmp 0x4b2ff
jmp 0x4b44e
xorl %r13d, %r13d
movq 0x10(%rsp), %rsi
movl 0x8(%rsp), %edi
testq %rsi, %rsi
je 0x4b3db
movq 0x18(%rsp), %rcx
movl 0x50(%rcx), %eax
cmpq %rax, 0xa0(%rcx)
jae 0x4b3db
leaq 0x8(%rsi), %r15
movq 0x8(%rsi), %rbx
cmpq %r15, %rbx
je 0x4b3db
movq %rbx, %r12
movq 0x10(%r12), %rax
movl 0x8(%rax), %ecx
cmpl $0x2, %ecx
je 0x4b34d
cmpl $0x1, %ecx
jne 0x4b35c
cmpq $0x0, 0x88(%rax)
jmp 0x4b355
cmpq $0x0, 0x90(%rax)
sete %cl
testb %cl, %cl
je 0x4b37b
cmpl %ebp, 0x40(%rax)
jne 0x4b37b
movl 0x44(%rax), %ecx
movl $0x1, %edx
shll %cl, %edx
btl %ecx, %r13d
jb 0x4b37b
testl %r13d, %r13d
je 0x4b386
cmpl %edi, 0x4c(%rax)
je 0x4b386
movq (%r12), %r12
cmpq %r15, %r12
jne 0x4b331
jmp 0x4b3db
movl 0xc(%rsp), %ecx
movb %cl, 0x68(%rax)
orl %edx, %r13d
movl $0x18, %edi
callq 0x73a0
movq 0x10(%r12), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x7060
movq 0x18(%rsp), %rax
incq 0xa0(%rax)
movq (%r12), %rbx
movq %r12, 0x20(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x3ea54
movl 0x8(%rsp), %edi
jmp 0x4b325
movq 0x18(%rsp), %r15
cmpq $0x0, 0xa0(%r15)
setne %bpl
je 0x4b419
movq 0x38(%r15), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x58(%rax)
movq 0x90(%r15), %rdi
cmpq %r14, %rdi
je 0x4b431
movq (%rdi), %rbx
callq 0x7370
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x4b407
jmp 0x4b431
movq (%r14), %rdi
cmpq %r14, %rdi
je 0x4b431
movq (%rdi), %rbx
callq 0x7370
movq %rbx, %rdi
cmpq %r14, %rbx
jne 0x4b421
movq %r14, 0x98(%r15)
movq %r14, 0x90(%r15)
movq $0x0, 0xa0(%r15)
movl 0x2c(%rsp), %eax
testl %eax, %eax
setne %al
andb %bpl, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop