// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]MQSim/src/ssd/NVM_PHY_ONFI_NVDDR2.cpp
// Function: SSD_Components::NVM_PHY_ONFI_NVDDR2::handle_ready_signal_from_chip(NVM::FlashMemory::Flash_Chip*, NVM::FlashMemory::Flash_Command*)
inline void NVM_PHY_ONFI_NVDDR2::handle_ready_signal_from_chip(NVM::FlashMemory::Flash_Chip* chip, NVM::FlashMemory::Flash_Command* command)
	{
		ChipBookKeepingEntry *chipBKE = &_my_instance->bookKeepingTable[chip->ChannelID][chip->ChipID];
		DieBookKeepingEntry *dieBKE = &(chipBKE->Die_book_keeping_records[command->Address[0].DieID]);

		switch (command->CommandCode)
		{
		case CMD_READ_PAGE:
		case CMD_READ_PAGE_MULTIPLANE:
			DEBUG("Chip " << chip->ChannelID << ", " << chip->ChipID << ": finished  read command")
			chipBKE->No_of_active_dies--;
			if (chipBKE->No_of_active_dies == 0)//After finishing the last command, the chip state is changed
				chipBKE->Status = ChipStatus::WAIT_FOR_DATA_OUT;

			for (std::list<NVM_Transaction_Flash*>::iterator it = dieBKE->ActiveTransactions.begin();
				it != dieBKE->ActiveTransactions.end(); it++)
			{
				chipBKE->WaitingReadTXCount++;
				if (_my_instance->channels[chip->ChannelID]->GetStatus() == BusChannelStatus::IDLE)
					_my_instance->transfer_read_data_from_chip(chipBKE, dieBKE, (*it));
				else
				{
					switch (dieBKE->ActiveTransactions.front()->Source)
					{
					case Transaction_Source_Type::CACHE:
					case Transaction_Source_Type::USERIO:
						_my_instance->WaitingReadTX[chip->ChannelID].push_back((*it));
						break;
					case Transaction_Source_Type::GC_WL:
						_my_instance->WaitingGCRead_TX[chip->ChannelID].push_back((*it));
						break;
					case Transaction_Source_Type::MAPPING:
						_my_instance->WaitingMappingRead_TX[chip->ChannelID].push_back((*it));
						break;
					}
				}
			}
			break;
		case CMD_READ_PAGE_COPYBACK:
		case CMD_READ_PAGE_COPYBACK_MULTIPLANE:
			chipBKE->No_of_active_dies--;
			if (chipBKE->No_of_active_dies == 0)
				chipBKE->Status = ChipStatus::WAIT_FOR_COPYBACK_CMD;
			if (_my_instance->channels[chip->ChannelID]->GetStatus() == BusChannelStatus::IDLE)
			{
				if (dieBKE->ActiveTransactions.size() > 1)
				{
					Stats::IssuedMultiplaneCopybackProgramCMD++;
					dieBKE->ActiveCommand->CommandCode = CMD_PROGRAM_PAGE_COPYBACK_MULTIPLANE;
				}
				else
				{
					Stats::IssuedCopybackProgramCMD++;
					dieBKE->ActiveCommand->CommandCode = CMD_PROGRAM_PAGE_COPYBACK;
				}

				for (std::list<NVM_Transaction_Flash*>::iterator it = dieBKE->ActiveTransactions.begin();
					it != dieBKE->ActiveTransactions.end(); it++)
				{
					(*it)->STAT_transfer_time += _my_instance->channels[chip->ChannelID]->ProgramCommandTime[dieBKE->ActiveTransactions.size()];
				}
				chip->StartCMDXfer();
				chipBKE->Status = ChipStatus::CMD_IN;
				Simulator->Register_sim_event(Simulator->Time() + _my_instance->channels[chip->ChannelID]->ProgramCommandTime[dieBKE->ActiveTransactions.size()],
					_my_instance, dieBKE, (int)NVDDR2_SimEventType::PROGRAM_COPYBACK_CMD_ADDR_TRANSFERRED);
				chipBKE->OngoingDieCMDTransfers.push(dieBKE);
				_my_instance->channels[chip->ChannelID]->SetStatus(BusChannelStatus::BUSY, chip);

				dieBKE->Expected_finish_time = Simulator->Time() + _my_instance->channels[chip->ChannelID]->ProgramCommandTime[dieBKE->ActiveTransactions.size()]
					+ chip->Get_command_execution_latency(dieBKE->ActiveCommand->CommandCode, dieBKE->ActiveCommand->Address[0].PageID);
				if (chipBKE->Expected_command_exec_finish_time < dieBKE->Expected_finish_time)
					chipBKE->Expected_command_exec_finish_time = dieBKE->Expected_finish_time;
#if 0	
				//Copyback data should be read out in order to get rid of bit error propagation
				Simulator->RegisterEvent(Simulator->Time() + channels[targetChip->ChannelID]->ProgramCommandTime + NVDDR2DataOutTransferTime(targetTransaction->SizeInByte, channels[targetChip->ChannelID]),
					this, targetTransaction, (int)NVDDR2_SimEventType::READ_DATA_TRANSFERRED);
				targetTransaction->STAT_TransferTime += NVDDR2DataOutTransferTime(targetTransaction->SizeInByte, channels[targetChip->ChannelID]);
#endif
			}
			else _my_instance->WaitingCopybackWrites->push_back(dieBKE);
			break;
		case CMD_PROGRAM_PAGE:
		case CMD_PROGRAM_PAGE_MULTIPLANE:
		case CMD_PROGRAM_PAGE_COPYBACK:
		case CMD_PROGRAM_PAGE_COPYBACK_MULTIPLANE:
		{
			DEBUG("Chip " << chip->ChannelID << ", " << chip->ChipID << ": finished program command")
			int i = 0;
			for (std::list<NVM_Transaction_Flash*>::iterator it = dieBKE->ActiveTransactions.begin();
				it != dieBKE->ActiveTransactions.end(); it++, i++)
			{
				((NVM_Transaction_Flash_WR*)(*it))->Content = command->Meta_data[i].LPA;
				_my_instance->broadcastTransactionServicedSignal(*it);
			}
			dieBKE->ActiveTransactions.clear();
			dieBKE->ClearCommand();

			chipBKE->No_of_active_dies--;
			if (chipBKE->No_of_active_dies == 0 && chipBKE->WaitingReadTXCount == 0)
				chipBKE->Status = ChipStatus::IDLE;
			//Since the time required to send the resume command is very small, we ignore it
			if (chipBKE->Status == ChipStatus::IDLE)
				if (chipBKE->HasSuspend)
					_my_instance->send_resume_command_to_chip(chip, chipBKE);
			break;
		}
		case CMD_ERASE_BLOCK:
		case CMD_ERASE_BLOCK_MULTIPLANE:
			DEBUG("Chip " << chip->ChannelID << ", " << chip->ChipID << ": finished erase command")
			for (std::list<NVM_Transaction_Flash*>::iterator it = dieBKE->ActiveTransactions.begin();
				it != dieBKE->ActiveTransactions.end(); it++)
				_my_instance->broadcastTransactionServicedSignal(*it);
			dieBKE->ActiveTransactions.clear();
			dieBKE->ClearCommand();

			chipBKE->No_of_active_dies--;
			if (chipBKE->No_of_active_dies == 0 && chipBKE->WaitingReadTXCount == 0)
				chipBKE->Status = ChipStatus::IDLE;
			//Since the time required to send the resume command is very small, we ignore it
			if (chipBKE->Status == ChipStatus::IDLE)
				if (chipBKE->HasSuspend)
					_my_instance->send_resume_command_to_chip(chip, chipBKE);
			break;
		default:
			break;
		}

		if (_my_instance->channels[chip->ChannelID]->GetStatus() == BusChannelStatus::IDLE)
			_my_instance->broadcastChannelIdleSignal(chip->ChannelID);
		else if (chipBKE->Status == ChipStatus::IDLE)
			_my_instance->broadcastChipIdleSignal(chip);
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %rbp
movq 0x29901(%rip), %rax     # 0x62648
movq 0x90(%rax), %rdx
movl 0x2c(%rdi), %ecx
movl 0x30(%rdi), %esi
movq (%rdx,%rcx,8), %rdx
shlq $0x7, %rsi
leaq (%rdx,%rsi), %r14
movq 0x8(%r12), %rdi
movl 0x8(%rdi), %edi
imulq $0x68, %rdi, %r15
addq 0x8(%rdx,%rsi), %r15
movq %r15, 0x10(%rsp)
movq (%r12), %rdx
leaq -0x60d0(%rdx), %rsi
cmpq $0x2, %rsi
jb 0x38f65
leaq -0x8010(%rdx), %rsi
cmpq $0x2, %rsi
jb 0x38e84
cmpq $0x30, %rdx
je 0x38ddd
cmpq $0x85111, %rdx          # imm = 0x85111
je 0x38e84
cmpq $0x35, %rdx
je 0x38ef6
cmpq $0x321, %rdx            # imm = 0x321
je 0x38ef6
cmpq $0x8510, %rdx           # imm = 0x8510
je 0x38e84
cmpq $0x32, %rdx
jne 0x390d4
decl 0x7c(%r14)
jne 0x38dea
movl $0x7, (%r14)
leaq 0x8(%r15), %r12
movq %r12, %r13
movq (%r13), %r13
cmpq %r12, %r13
je 0x390d4
incl 0x78(%r14)
movq 0x2983f(%rip), %rdi     # 0x62648
movq 0x88(%rdi), %rcx
movl 0x2c(%rbp), %eax
movq (%rcx,%rax,8), %rcx
cmpl $0x1, 0x14(%rcx)
jne 0x38e2e
movq 0x10(%r13), %rcx
movq %r14, %rsi
movq %r15, %rdx
callq 0x394f8
jmp 0x38df1
movq (%r12), %rcx
movq 0x10(%rcx), %rcx
movl 0x4(%rcx), %ecx
cmpl $0x2, %ecx
jb 0x38e55
je 0x38e65
cmpl $0x3, %ecx
jne 0x38df1
imulq $0x108, %rax, %rax      # imm = 0x108
addq 0xa8(%rdi), %rax
jmp 0x38e73
imulq $0x108, %rax, %rax      # imm = 0x108
addq 0x98(%rdi), %rax
jmp 0x38e73
imulq $0x108, %rax, %rax      # imm = 0x108
addq 0xa0(%rdi), %rax
leaq 0x10(%r13), %rsi
movq %rax, %rdi
callq 0x3240c
jmp 0x38df1
movq %rbp, 0x8(%rsp)
leaq 0x8(%r15), %rbx
xorl %ebp, %ebp
movq %rbx, %r13
movq (%r13), %r13
cmpq %rbx, %r13
je 0x38ec1
movq 0x20(%r12), %rax
movq (%rax,%rbp), %rax
movq 0x10(%r13), %rsi
movq %rax, 0x80(%rsi)
movq 0x29792(%rip), %rdi     # 0x62648
callq 0x3742a
addq $0x8, %rbp
jmp 0x38e92
movq %rbx, %rdi
callq 0x27258
movq %r15, %rdi
callq 0x393f0
decl 0x7c(%r14)
jne 0x38ee2
cmpl $0x0, 0x78(%r14)
je 0x390ac
cmpl $0x0, (%r14)
movq 0x8(%rsp), %rbp
jne 0x390d4
jmp 0x390bb
decl 0x7c(%r14)
jne 0x38f03
movl $0x8, (%r14)
movq 0x88(%rax), %rdx
movq (%rdx,%rcx,8), %rcx
cmpl $0x1, 0x14(%rcx)
jne 0x38fb6
cmpq $0x2, 0x18(%r15)
leaq 0x2979c(%rip), %rax     # 0x626c0
cmovaeq 0x2906c(%rip), %rax     # 0x61f98
movl $0x85111, %edx          # imm = 0x85111
movl $0x8510, %esi           # imm = 0x8510
cmovaeq %rdx, %rsi
incq (%rax)
leaq 0x8(%r15), %rax
movq (%r15), %rdx
movq %rsi, (%rdx)
movq %rax, %rdx
movq (%rdx), %rdx
cmpq %rax, %rdx
je 0x38fcc
movq 0x18(%r15), %rsi
movq 0x78(%rcx,%rsi,8), %rsi
movq 0x10(%rdx), %rdi
addq %rsi, 0x30(%rdi)
jmp 0x38f4a
leaq 0x8(%r15), %r12
movq %r12, %rbx
movq (%rbx), %rbx
cmpq %r12, %rbx
je 0x38f86
movq 0x296cd(%rip), %rdi     # 0x62648
movq 0x10(%rbx), %rsi
callq 0x3742a
jmp 0x38f6c
movq %r12, %rdi
callq 0x27258
movq %r15, %rdi
callq 0x393f0
decl 0x7c(%r14)
jne 0x38fa7
cmpl $0x0, 0x78(%r14)
je 0x390b7
cmpl $0x0, (%r14)
jne 0x390d4
jmp 0x390bb
movq 0xb0(%rax), %rdi
leaq 0x10(%rsp), %rsi
callq 0x39aee
jmp 0x390d4
movq %rbp, %rdi
callq 0x3918e
movl $0x1, (%r14)
callq 0x1f784
movq %rax, %r12
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
movq 0x29651(%rip), %rdx     # 0x62648
movq 0x88(%rdx), %rcx
movl 0x2c(%rbp), %esi
movq (%rcx,%rsi,8), %rcx
movq 0x18(%r15), %rsi
addq 0x78(%rcx,%rsi,8), %rax
pushq $0x3
popq %r8
movq %r12, %rdi
movq %rax, %rsi
movq %r15, %rcx
callq 0x1fb00
leaq 0x28(%r14), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0x39dae
movq 0x29610(%rip), %rax     # 0x62648
movq 0x88(%rax), %rax
movl 0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x391c2
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
movq %rax, %r12
movq 0x295e1(%rip), %rax     # 0x62648
movq 0x88(%rax), %rax
movl 0x2c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq (%r15), %rcx
movq 0x18(%rcx), %rdx
addq 0x78(%rax,%rdx,8), %r12
movq (%rcx), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rax
movl 0x14(%rax), %edx
movq %rbp, %rdi
callq 0x1f456
addq %r12, %rax
movq (%r15), %rcx
movq %rax, 0x50(%rcx)
cmpq %rax, 0x10(%r14)
jae 0x390d4
movq %rax, 0x10(%r14)
jmp 0x390d4
andl $0x0, (%r14)
movq 0x8(%rsp), %rbp
jmp 0x390bb
andl $0x0, (%r14)
cmpb $0x1, 0x20(%r14)
jne 0x390d4
movq 0x2957f(%rip), %rdi     # 0x62648
movq %rbp, %rsi
movq %r14, %rdx
callq 0x39428
movq 0x2956d(%rip), %rdi     # 0x62648
movq 0x88(%rdi), %rax
movl 0x2c(%rbp), %esi
movq (%rax,%rsi,8), %rax
cmpl $0x1, 0x14(%rax)
jne 0x390f6
callq 0x3746e
jmp 0x39104
cmpl $0x0, (%r14)
jne 0x39104
movq %rbp, %rsi
callq 0x374aa
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop