// File: /workspace/github_cpp_makefile_star2_O1/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, %r14
movq 0x27fa8(%rip), %rcx     # 0x6f678
movq 0x90(%rcx), %rax
movl 0x2c(%rdi), %edx
movq %rdi, 0x10(%rsp)
movl 0x30(%rdi), %esi
movq (%rax,%rdx,8), %rdi
shlq $0x7, %rsi
movq 0x8(%r14), %rax
movl 0x8(%rax), %eax
imulq $0x68, %rax, %rax
addq 0x8(%rdi,%rsi), %rax
leaq (%rdi,%rsi), %r13
movq %rax, 0x8(%rsp)
movq (%r14), %rsi
cmpq $0x800f, %rsi           # imm = 0x800F
jg 0x4773f
cmpq $0x320, %rsi            # imm = 0x320
jg 0x478d0
cmpq $0x30, %rsi
je 0x47a2c
cmpq $0x32, %rsi
je 0x47a2c
cmpq $0x35, %rsi
je 0x47ba2
jmp 0x47d5e
leaq -0x8010(%rsi), %rcx
cmpq $0x2, %rcx
jb 0x47762
cmpq $0x8510, %rsi           # imm = 0x8510
je 0x47762
cmpq $0x85111, %rsi          # imm = 0x85111
jne 0x47d5e
movq 0x8(%rax), %r12
movq 0x8(%rsp), %rbx
leaq 0x8(%rbx), %r15
cmpq %r15, %r12
je 0x477ac
xorl %ebp, %ebp
movq 0x20(%r14), %rax
movq (%rax,%rbp), %rax
movq 0x10(%r12), %rsi
movq %rax, 0x80(%rsi)
movq 0x27ee7(%rip), %rdi     # 0x6f678
callq 0x457f2
movq (%r12), %r12
movq 0x8(%rsp), %rbx
leaq 0x8(%rbx), %r15
addq $0x8, %rbp
cmpq %r15, %r12
jne 0x47776
movq 0x8(%rbx), %rdi
cmpq %r15, %rdi
je 0x477c5
movq (%rdi), %r14
callq 0x7370
movq %r14, %rdi
cmpq %r15, %r14
jne 0x477b5
movq %r15, 0x10(%rbx)
movq %r15, (%r15)
movq $0x0, 0x18(%rbx)
movq 0x8(%rsp), %rdi
callq 0x4805e
decl 0x7c(%r13)
jne 0x477f3
cmpl $0x0, 0x78(%r13)
jne 0x477f3
movl $0x0, (%r13)
cmpl $0x0, (%r13)
jne 0x47d5e
cmpb $0x1, 0x20(%r13)
jne 0x47d5e
movq 0x27e68(%rip), %r14     # 0x6f678
cmpl $0x0, 0x34(%r14)
je 0x47d5e
xorl %r15d, %r15d
movabsq $0x25000000000000, %r12 # imm = 0x25000000000000
xorl %ebp, %ebp
movq 0x8(%r13), %rbx
leaq (%rbx,%r15), %rdi
callq 0x48544
movb $0x0, 0x20(%r13)
movq (%rbx,%r15), %rax
movq 0x8(%rax), %rax
movl 0x8(%rax), %esi
movq 0x10(%rsp), %rdi
callq 0x2717a
movq (%rbx,%r15), %rax
movq (%rax), %rax
cmpq $0x800f, %rax           # imm = 0x800F
jg 0x4787f
movl $0x4, %ecx
cmpq $0x35, %rax
jbe 0x478a3
leaq -0x60d0(%rax), %rdx
cmpq $0x2, %rdx
jae 0x478ab
movl $0x6, %ecx
jmp 0x478b3
leaq -0x8010(%rax), %rcx
cmpq $0x2, %rcx
jb 0x4789c
cmpq $0x85111, %rax          # imm = 0x85111
je 0x4789c
cmpq $0x8510, %rax           # imm = 0x8510
jne 0x478b7
movl $0x5, %ecx
jmp 0x478b3
btq %rax, %r12
jb 0x478b3
jmp 0x4786b
cmpq $0x321, %rax            # imm = 0x321
jne 0x478b7
movl %ecx, (%r13)
incq %rbp
movl 0x34(%r14), %eax
addq $0x68, %r15
cmpq %rax, %rbp
jb 0x4782a
jmp 0x47d5e
leaq -0x60d0(%rsi), %rdi
cmpq $0x2, %rdi
jae 0x47b95
movq 0x8(%rax), %r15
movq 0x8(%rsp), %rbx
leaq 0x8(%rbx), %r14
cmpq %r14, %r15
je 0x47908
movq 0x27d7e(%rip), %rdi     # 0x6f678
movq 0x10(%r15), %rsi
callq 0x457f2
movq (%r15), %r15
jmp 0x478e5
movq 0x8(%rbx), %rdi
cmpq %r14, %rdi
je 0x47921
movq (%rdi), %r15
callq 0x7370
movq %r15, %rdi
cmpq %r14, %r15
jne 0x47911
movq %r14, 0x10(%rbx)
movq %r14, (%r14)
movq $0x0, 0x18(%rbx)
movq 0x8(%rsp), %rdi
callq 0x4805e
decl 0x7c(%r13)
jne 0x4794f
cmpl $0x0, 0x78(%r13)
jne 0x4794f
movl $0x0, (%r13)
cmpl $0x0, (%r13)
jne 0x47d5e
cmpb $0x1, 0x20(%r13)
jne 0x47d5e
movq 0x27d0c(%rip), %r14     # 0x6f678
cmpl $0x0, 0x34(%r14)
je 0x47d5e
xorl %r15d, %r15d
movabsq $0x25000000000000, %r12 # imm = 0x25000000000000
xorl %ebp, %ebp
movq 0x8(%r13), %rbx
leaq (%rbx,%r15), %rdi
callq 0x48544
movb $0x0, 0x20(%r13)
movq (%rbx,%r15), %rax
movq 0x8(%rax), %rax
movl 0x8(%rax), %esi
movq 0x10(%rsp), %rdi
callq 0x2717a
movq (%rbx,%r15), %rax
movq (%rax), %rax
cmpq $0x800f, %rax           # imm = 0x800F
jg 0x479db
movl $0x4, %ecx
cmpq $0x35, %rax
jbe 0x479ff
leaq -0x60d0(%rax), %rdx
cmpq $0x2, %rdx
jae 0x47a07
movl $0x6, %ecx
jmp 0x47a0f
leaq -0x8010(%rax), %rcx
cmpq $0x2, %rcx
jb 0x479f8
cmpq $0x85111, %rax          # imm = 0x85111
je 0x479f8
cmpq $0x8510, %rax           # imm = 0x8510
jne 0x47a13
movl $0x5, %ecx
jmp 0x47a0f
btq %rax, %r12
jb 0x47a0f
jmp 0x479c7
cmpq $0x321, %rax            # imm = 0x321
jne 0x47a13
movl %ecx, (%r13)
incq %rbp
movl 0x34(%r14), %eax
addq $0x68, %r15
cmpq %rax, %rbp
jb 0x47986
jmp 0x47d5e
decl 0x7c(%r13)
jne 0x47a3a
movl $0x7, (%r13)
movq 0x8(%rax), %rbp
movq 0x8(%rsp), %r15
leaq 0x8(%r15), %rax
cmpq %rax, %rbp
je 0x47d5e
incl 0x78(%r13)
movq 0x27c1d(%rip), %r14     # 0x6f678
movq 0x88(%r14), %rdx
movq 0x10(%rsp), %rcx
movl 0x2c(%rcx), %ecx
movq (%rdx,%rcx,8), %rsi
cmpl $0x1, 0x14(%rsi)
jne 0x47b33
movq 0x10(%rbp), %rbx
movq %rbx, 0x40(%r15)
movl 0x38(%rbx), %eax
movl 0x3c(%rbx), %ecx
movq (%rdx,%rax,8), %rax
movq 0x8(%rax), %rax
movq (%rax,%rcx,8), %r12
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
movq %rax, 0x98(%r12)
movl $0x3, (%r13)
callq 0x281b4
movq %rax, %r12
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
movq %rax, %rsi
movl 0x38(%rbx), %ecx
movl 0x50(%rbx), %eax
movq 0x88(%r14), %rdx
movq (%rdx,%rcx,8), %rcx
xorl %edx, %edx
divl 0xd8(%rcx)
shrl %eax
imulq 0x20(%rcx), %rax
addq %rax, %rsi
movq %r12, %rdi
movq %r14, %rdx
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x285aa
movl 0x50(%rbx), %eax
movq 0x88(%r14), %rcx
movl 0x38(%rbx), %edx
movl 0x3c(%rbx), %esi
movq (%rcx,%rdx,8), %rdi
xorl %edx, %edx
divl 0xd8(%rdi)
shrl %eax
imulq 0x20(%rdi), %rax
addq %rax, 0x30(%rbx)
movq 0x8(%rdi), %rax
movq (%rax,%rsi,8), %rdx
xorl %esi, %esi
callq 0x47e24
jmp 0x47b8c
movq (%rax), %rax
movq 0x10(%rax), %rax
movl 0x4(%rax), %eax
cmpl $0x2, %eax
jb 0x47b5d
je 0x47b71
cmpl $0x3, %eax
jne 0x47b8c
movq %rcx, %rax
shlq $0x8, %rax
leaq (%rax,%rcx,8), %rdi
addq 0xa8(%r14), %rdi
jmp 0x47b83
movq %rcx, %rax
shlq $0x8, %rax
leaq (%rax,%rcx,8), %rdi
addq 0x98(%r14), %rdi
jmp 0x47b83
movq %rcx, %rax
shlq $0x8, %rax
leaq (%rax,%rcx,8), %rdi
addq 0xa0(%r14), %rdi
leaq 0x10(%rbp), %rsi
callq 0x3e954
movq (%rbp), %rbp
jmp 0x47a3e
cmpq $0x321, %rsi            # imm = 0x321
jne 0x47d5e
decl 0x7c(%r13)
jne 0x47bb0
movl $0x8, (%r13)
movq 0x88(%rcx), %rsi
movq (%rsi,%rdx,8), %rdx
cmpl $0x1, 0x14(%rdx)
jne 0x47ca0
cmpq $0x2, 0x18(%rax)
leaq 0x27b1f(%rip), %rsi     # 0x6f6f0
cmovaeq 0x273c7(%rip), %rsi     # 0x6efa0
movl $0x85111, %ecx          # imm = 0x85111
movl $0x8510, %edi           # imm = 0x8510
cmovaeq %rcx, %rdi
leaq 0x8(%rax), %rcx
incq (%rsi)
movq (%rax), %rsi
movq %rdi, (%rsi)
movq 0x8(%rax), %rsi
cmpq %rcx, %rsi
je 0x47c16
movq 0x18(%rax), %rax
movq 0x78(%rdx,%rax,8), %rdi
movq 0x10(%rsi), %r8
addq %rdi, 0x30(%r8)
movq (%rsi), %rsi
cmpq %rcx, %rsi
jne 0x47c01
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
movq 0x10(%rsp), %rbx
movq %rax, 0x98(%rbx)
movl $0x1, (%r13)
callq 0x281b4
movq %rax, %r14
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
movq 0x27a25(%rip), %rdx     # 0x6f678
movq 0x88(%rdx), %rcx
movl 0x2c(%rbx), %esi
movq (%rcx,%rsi,8), %rsi
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rdi
addq 0x78(%rsi,%rdi,8), %rax
movq %r14, %rdi
movq %rax, %rsi
movl $0x3, %r8d
callq 0x285aa
movq 0x58(%r13), %rax
movq 0x68(%r13), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x47cce
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
addq $0x8, 0x58(%r13)
jmp 0x47cdc
movq 0xb0(%rcx), %r14
movl $0x18, %edi
callq 0x73a0
movq 0x8(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq %rax, %rdi
movq %r14, %rsi
callq 0x7060
incq 0x10(%r14)
jmp 0x47d5e
leaq 0x28(%r13), %rdi
leaq 0x8(%rsp), %rsi
callq 0x486f6
movq 0x27995(%rip), %rax     # 0x6f678
movq 0x88(%rax), %rax
movq 0x10(%rsp), %rbx
movl 0x2c(%rbx), %ecx
movq (%rax,%rcx,8), %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x47e24
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
movq %rax, %r14
movq 0x27961(%rip), %rax     # 0x6f678
movq 0x88(%rax), %rax
movl 0x2c(%rbx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rdx
addq 0x78(%rax,%rdx,8), %r14
movq (%rcx), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rax
movl 0x14(%rax), %edx
movq %rbx, %rdi
callq 0x27e7c
addq %r14, %rax
movq 0x8(%rsp), %rcx
movq %rax, 0x50(%rcx)
cmpq %rax, 0x10(%r13)
jae 0x47d5e
movq %rax, 0x10(%r13)
movq 0x27913(%rip), %rdi     # 0x6f678
movq 0x88(%rdi), %rax
movq 0x10(%rsp), %rcx
movl 0x2c(%rcx), %esi
movq (%rax,%rsi,8), %rax
cmpl $0x1, 0x14(%rax)
jne 0x47d85
callq 0x4585a
jmp 0x47d96
cmpl $0x0, (%r13)
jne 0x47d96
movq 0x10(%rsp), %rsi
callq 0x458b2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop