// File: CMU-SAFARI[P]MQSim/src/ssd/NVM_PHY_ONFI_NVDDR2.cpp
// Function: SSD_Components::NVM_PHY_ONFI_NVDDR2::perform_interleaved_cmd_data_transfer(NVM::FlashMemory::Flash_Chip*, SSD_Components::DieBookKeepingEntry*)
void NVM_PHY_ONFI_NVDDR2::perform_interleaved_cmd_data_transfer(NVM::FlashMemory::Flash_Chip* chip, DieBookKeepingEntry* bookKeepingEntry)
	{
		ONFI_Channel_NVDDR2* target_channel = channels[bookKeepingEntry->ActiveTransactions.front()->Address.ChannelID];
		/*if (target_channel->Status == BusChannelStatus::BUSY)
			PRINT_ERROR("Requesting communication on a busy bus!")*/

		switch (bookKeepingEntry->ActiveTransactions.front()->Type)
		{
			case Transaction_Type::READ:
				chip->StartCMDXfer();
				bookKeepingTable[chip->ChannelID][chip->ChipID].Status = ChipStatus::CMD_IN;
				Simulator->Register_sim_event(Simulator->Time() + bookKeepingEntry->DieInterleavedTime,
					this, bookKeepingEntry, (int)NVDDR2_SimEventType::READ_CMD_ADDR_TRANSFERRED);
				break;
			case Transaction_Type::WRITE:
				if (((NVM_Transaction_Flash_WR*)bookKeepingEntry->ActiveTransactions.front())->RelatedRead == NULL) {
					chip->StartCMDDataInXfer();
					bookKeepingTable[chip->ChannelID][chip->ChipID].Status = ChipStatus::CMD_DATA_IN;
					Simulator->Register_sim_event(Simulator->Time() + bookKeepingEntry->DieInterleavedTime,
						this, bookKeepingEntry, (int)NVDDR2_SimEventType::PROGRAM_CMD_ADDR_DATA_TRANSFERRED);
				} else {
					chip->StartCMDXfer();
					bookKeepingTable[chip->ChannelID][chip->ChipID].Status = ChipStatus::CMD_IN;
					Simulator->Register_sim_event(Simulator->Time() + bookKeepingEntry->DieInterleavedTime, this,
						bookKeepingEntry, (int)NVDDR2_SimEventType::READ_CMD_ADDR_TRANSFERRED);
				}
				break;
			case Transaction_Type::ERASE:
				chip->StartCMDXfer();
				bookKeepingTable[chip->ChannelID][chip->ChipID].Status = ChipStatus::CMD_IN;
				Simulator->Register_sim_event(Simulator->Time() + bookKeepingEntry->DieInterleavedTime,
					this, bookKeepingEntry, (int)NVDDR2_SimEventType::ERASE_SETUP_COMPLETED);
				break;
			default:
				PRINT_ERROR("NVMController_NVDDR2: Uknown flash transaction type!")
		}
		target_channel->SetStatus(BusChannelStatus::BUSY, chip);
	}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x88(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x40(%rsp), %rdi
addq $0x8, %rdi
callq 0x7fe10
movq %rax, %rcx
movq 0x28(%rsp), %rax
movq (%rcx), %rcx
movl 0x38(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rdi
addq $0x8, %rdi
callq 0x7fe10
movq (%rax), %rax
movl 0x8(%rax), %eax
movl %eax, 0x34(%rsp)
testl %eax, %eax
je 0x90c3d
jmp 0x90c1c
movl 0x34(%rsp), %eax
subl $0x1, %eax
je 0x90cb6
jmp 0x90c2b
movl 0x34(%rsp), %eax
subl $0x2, %eax
je 0x90dc0
jmp 0x90e34
movq 0x48(%rsp), %rdi
callq 0x91cb0
movq 0x20(%rsp), %rax
movq 0x90(%rax), %rax
movq 0x48(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x48(%rsp), %rcx
movl 0x30(%rcx), %ecx
shlq $0x7, %rcx
addq %rcx, %rax
movl $0x1, (%rax)
callq 0x46500
movq %rax, 0x18(%rsp)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %rax, %rsi
movq 0x40(%rsp), %rax
addq 0x60(%rax), %rsi
movq 0x40(%rsp), %rcx
movl $0x1, %r8d
callq 0x46c60
jmp 0x90e7f
movq 0x40(%rsp), %rdi
addq $0x8, %rdi
callq 0x7fe10
movq (%rax), %rax
cmpq $0x0, 0x88(%rax)
jne 0x90d47
movq 0x48(%rsp), %rdi
callq 0x91d20
movq 0x20(%rsp), %rax
movq 0x90(%rax), %rax
movq 0x48(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x48(%rsp), %rcx
movl 0x30(%rcx), %ecx
shlq $0x7, %rcx
addq %rcx, %rax
movl $0x2, (%rax)
callq 0x46500
movq %rax, 0x10(%rsp)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %rax, %rsi
movq 0x40(%rsp), %rax
addq 0x60(%rax), %rsi
movq 0x40(%rsp), %rcx
movl $0x2, %r8d
callq 0x46c60
jmp 0x90dbb
movq 0x48(%rsp), %rdi
callq 0x91cb0
movq 0x20(%rsp), %rax
movq 0x90(%rax), %rax
movq 0x48(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x48(%rsp), %rcx
movl 0x30(%rcx), %ecx
shlq $0x7, %rcx
addq %rcx, %rax
movl $0x1, (%rax)
callq 0x46500
movq %rax, 0x8(%rsp)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %rax, %rsi
movq 0x40(%rsp), %rax
addq 0x60(%rax), %rsi
movq 0x40(%rsp), %rcx
movl $0x1, %r8d
callq 0x46c60
jmp 0x90e7f
movq 0x48(%rsp), %rdi
callq 0x91cb0
movq 0x20(%rsp), %rax
movq 0x90(%rax), %rax
movq 0x48(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x48(%rsp), %rcx
movl 0x30(%rcx), %ecx
shlq $0x7, %rcx
addq %rcx, %rax
movl $0x1, (%rax)
callq 0x46500
movq %rax, (%rsp)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq (%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %rax, %rsi
movq 0x40(%rsp), %rax
addq 0x60(%rax), %rsi
movq 0x40(%rsp), %rcx
movl $0x4, %r8d
callq 0x46c60
jmp 0x90e7f
movq 0x491b5(%rip), %rdi     # 0xd9ff0
leaq 0x19aa6(%rip), %rsi     # 0xaa8e8
callq 0x8440
movq 0x491a2(%rip), %rdi     # 0xd9ff0
leaq 0x1d894(%rip), %rsi     # 0xae6e9
callq 0x8440
movq %rax, %rdi
movq 0x490f4(%rip), %rsi     # 0xd9f58
callq 0x8480
movq 0x49160(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
movq 0x38(%rsp), %rdi
movq 0x48(%rsp), %rdx
xorl %esi, %esi
callq 0x91d60
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)
nop