// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdx, 0x30(%rsp)
movl 0x4c(%rdi), %eax
testl %eax, %eax
je 0x3c209
movq %rsi, %r12
movq %rdi, %rbx
movl %r8d, 0x8(%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, 0xc(%rsp)
addq $0x90, %rdi
movq %rdi, 0x18(%rsp)
callq 0x27258
movq (%r15), %r13
xorl %r14d, %r14d
cmpq %r15, %r13
je 0x3c137
movq 0x10(%r13), %rsi
movq %rbx, %rdi
callq 0x3c260
testb %al, %al
je 0x3c0fb
leaq 0x10(%r13), %rsi
movq (%rsi), %rax
cmpl %ebp, 0x40(%rax)
jne 0x3c0fb
movl 0x44(%rax), %ecx
pushq $0x1
popq %rdx
shll %cl, %edx
btl %ecx, %r14d
jb 0x3c0fb
testl %r14d, %r14d
je 0x3c101
movl 0xc(%rsp), %ecx
cmpl %ecx, 0x4c(%rax)
je 0x3c101
movq (%r13), %r13
jmp 0x3c0be
movl 0x8(%rsp), %ecx
movb %cl, 0x68(%rax)
orl %edx, %r14d
movq 0x18(%rsp), %rdi
callq 0x27294
movq (%r13), %rax
movq %rax, 0x10(%rsp)
movq %r13, 0x20(%rsp)
movq %r12, %rdi
leaq 0x20(%rsp), %rsi
callq 0x324c2
movq 0x10(%rsp), %r13
jmp 0x3c0be
movq 0x30(%rsp), %rcx
testq %rcx, %rcx
je 0x3c1db
movl 0x50(%rbx), %eax
cmpq %rax, 0xa0(%rbx)
jae 0x3c1db
leaq 0x8(%rcx), %r15
movq 0x8(%rcx), %r13
leaq 0x20(%rsp), %r12
cmpq %r15, %r13
je 0x3c1db
movq 0x10(%r13), %rsi
movq %rbx, %rdi
callq 0x3c260
testb %al, %al
je 0x3c19f
leaq 0x10(%r13), %rsi
movq (%rsi), %rax
cmpl %ebp, 0x40(%rax)
jne 0x3c19f
movl 0x44(%rax), %ecx
pushq $0x1
popq %rdx
shll %cl, %edx
btl %ecx, %r14d
jb 0x3c19f
testl %r14d, %r14d
je 0x3c1a5
movl 0xc(%rsp), %ecx
cmpl %ecx, 0x4c(%rax)
je 0x3c1a5
movq (%r13), %r13
jmp 0x3c162
movl 0x8(%rsp), %ecx
movb %cl, 0x68(%rax)
orl %edx, %r14d
movq 0x18(%rsp), %rdi
callq 0x27294
movq (%r13), %rax
movq %rax, 0x10(%rsp)
movq %r13, 0x20(%rsp)
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x324c2
movq 0x10(%rsp), %r13
jmp 0x3c162
cmpq $0x0, 0xa0(%rbx)
setne %bpl
movq 0x18(%rsp), %r14
je 0x3c1fb
movq 0x38(%rbx), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x58(%rax)
movq %r14, %rdi
callq 0x27258
movl 0x2c(%rsp), %eax
jmp 0x3c209
testl %eax, %eax
setne %al
andb %bpl, %al
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq