// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq 0x28a3e(%rip), %rax     # 0x6f678
movq 0x90(%rax), %rdx
movl 0x2c(%rdi), %ecx
movq %rdi, 0x8(%rsp)
movl 0x30(%rdi), %esi
movq (%rdx,%rcx,8), %rdx
shlq $0x7, %rsi
movq 0x8(%r15), %rdi
movl 0x8(%rdi), %edi
imulq $0x68, %rdi, %r14
addq 0x8(%rdx,%rsi), %r14
leaq (%rdx,%rsi), %r13
movq %r14, 0x10(%rsp)
movq (%r15), %rdx
cmpq $0x800f, %rdx           # imm = 0x800F
movq %r13, (%rsp)
jg 0x46cad
cmpq $0x320, %rdx            # imm = 0x320
jg 0x46d66
cmpq $0x30, %rdx
je 0x46df0
cmpq $0x32, %rdx
je 0x46df0
cmpq $0x35, %rdx
je 0x46f6e
jmp 0x472fb
leaq -0x8010(%rdx), %rax
cmpq $0x2, %rax
jb 0x46cd0
cmpq $0x8510, %rdx           # imm = 0x8510
je 0x46cd0
cmpq $0x85111, %rdx          # imm = 0x85111
jne 0x472fb
leaq 0x8(%r14), %rbx
movq 0x8(%r14), %r12
cmpq %rbx, %r12
je 0x46d14
xorl %r13d, %r13d
movq 0x20(%r15), %rax
movq (%rax,%r13), %rax
movq 0x10(%r12), %rsi
movq %rax, 0x80(%rsi)
movq 0x2897d(%rip), %rdi     # 0x6f678
callq 0x44d6e
movq (%r12), %r12
addq $0x8, %r13
cmpq %rbx, %r12
jne 0x46ce0
movq (%rbx), %r12
movq (%rsp), %r13
cmpq %rbx, %r12
je 0x46d2d
movq (%r12), %r15
movq %r12, %rdi
callq 0x7370
movq %r15, %r12
cmpq %rbx, %r15
jne 0x46d19
movq %rbx, 0x10(%r14)
movq %rbx, 0x8(%r14)
movq $0x0, 0x18(%r14)
movq %r14, %rdi
callq 0x475fa
decl 0x7c(%r13)
jne 0x46d56
cmpl $0x0, 0x78(%r13)
je 0x47095
cmpl $0x0, (%r13)
jne 0x472fb
jmp 0x4709d
leaq -0x60d0(%rdx), %rsi
cmpq $0x2, %rsi
jae 0x46f61
leaq 0x8(%r14), %rbx
movq 0x8(%r14), %r15
cmpq %rbx, %r15
je 0x46d9f
movq 0x288ed(%rip), %rdi     # 0x6f678
movq 0x10(%r15), %rsi
callq 0x44d6e
movq (%r15), %r15
cmpq %rbx, %r15
jne 0x46d84
movq (%rbx), %r15
cmpq %rbx, %r15
je 0x46db7
movq (%r15), %r12
movq %r15, %rdi
callq 0x7370
movq %r12, %r15
cmpq %rbx, %r12
jne 0x46da4
movq %rbx, 0x10(%r14)
movq %rbx, 0x8(%r14)
movq $0x0, 0x18(%r14)
movq %r14, %rdi
callq 0x475fa
decl 0x7c(%r13)
jne 0x46de0
cmpl $0x0, 0x78(%r13)
je 0x47217
cmpl $0x0, (%r13)
jne 0x472fb
jmp 0x4721f
decl 0x7c(%r13)
jne 0x46dfe
movl $0x7, (%r13)
leaq 0x8(%r14), %rbp
movq 0x8(%r14), %rbx
cmpq %rbp, %rbx
je 0x472fb
incl 0x78(%r13)
movq 0x2885e(%rip), %r15     # 0x6f678
movq 0x88(%r15), %rcx
movq 0x8(%rsp), %rax
movl 0x2c(%rax), %eax
movq (%rcx,%rax,8), %rdx
cmpl $0x1, 0x14(%rdx)
jne 0x46eff
movq 0x10(%rbx), %r13
movq %r13, 0x40(%r14)
movl 0x38(%r13), %eax
movl 0x3c(%r13), %edx
movq (%rcx,%rax,8), %rax
movq 0x8(%rax), %rax
movq (%rax,%rdx,8), %r12
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq %rax, 0x98(%r12)
movq (%rsp), %rax
movl $0x3, (%rax)
callq 0x28018
movq %rax, %r12
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq %rax, %rsi
movl 0x38(%r13), %ecx
movl 0x50(%r13), %eax
movq 0x88(%r15), %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 %r15, %rdx
movq %r14, %rcx
xorl %r8d, %r8d
callq 0x2841e
movl 0x50(%r13), %eax
movq 0x88(%r15), %rcx
movl 0x38(%r13), %edx
movl 0x3c(%r13), %esi
movq (%rcx,%rdx,8), %rdi
xorl %edx, %edx
divl 0xd8(%rdi)
shrl %eax
imulq 0x20(%rdi), %rax
addq %rax, 0x30(%r13)
movq (%rsp), %r13
movq 0x8(%rdi), %rax
movq (%rax,%rsi,8), %rdx
xorl %esi, %esi
callq 0x473c0
jmp 0x46f59
movq (%rbp), %rcx
movq 0x10(%rcx), %rcx
movl 0x4(%rcx), %ecx
cmpl $0x2, %ecx
jb 0x46f2a
je 0x46f3e
cmpl $0x3, %ecx
jne 0x46f59
movq %rax, %rcx
shlq $0x8, %rcx
leaq (%rcx,%rax,8), %rdi
addq 0xa8(%r15), %rdi
jmp 0x46f50
movq %rax, %rcx
shlq $0x8, %rcx
leaq (%rcx,%rax,8), %rdi
addq 0x98(%r15), %rdi
jmp 0x46f50
movq %rax, %rcx
shlq $0x8, %rcx
leaq (%rcx,%rax,8), %rdi
addq 0xa0(%r15), %rdi
leaq 0x10(%rbx), %rsi
callq 0x3df04
movq (%rbx), %rbx
jmp 0x46e06
cmpq $0x321, %rdx            # imm = 0x321
jne 0x472fb
decl 0x7c(%r13)
jne 0x46f7c
movl $0x8, (%r13)
movq 0x88(%rax), %rdx
movq (%rdx,%rcx,8), %rcx
cmpl $0x1, 0x14(%rcx)
jne 0x4706c
cmpq $0x2, 0x18(%r14)
leaq 0x28753(%rip), %rdx     # 0x6f6f0
cmovaeq 0x27ff3(%rip), %rdx     # 0x6ef98
movl $0x85111, %eax          # imm = 0x85111
movl $0x8510, %esi           # imm = 0x8510
cmovaeq %rax, %rsi
leaq 0x8(%r14), %rax
incq (%rdx)
movq (%r14), %rdx
movq %rsi, (%rdx)
movq 0x8(%r14), %rdx
cmpq %rax, %rdx
je 0x46fe2
movq 0x18(%r14), %rsi
movq 0x78(%rcx,%rsi,8), %rdi
movq 0x10(%rdx), %r8
addq %rdi, 0x30(%r8)
movq (%rdx), %rdx
cmpq %rax, %rdx
jne 0x46fcd
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq 0x8(%rsp), %rbx
movq %rax, 0x98(%rbx)
movl $0x1, (%r13)
callq 0x28018
movq %rax, %r15
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq 0x28659(%rip), %rdx     # 0x6f678
movq 0x88(%rdx), %rcx
movl 0x2c(%rbx), %esi
movq (%rcx,%rsi,8), %rcx
movq 0x18(%r14), %rsi
addq 0x78(%rcx,%rsi,8), %rax
movq %r15, %rdi
movq %rax, %rsi
movq %r14, %rcx
movl $0x3, %r8d
callq 0x2841e
movq 0x58(%r13), %rax
movq 0x68(%r13), %rcx
addq $-0x8, %rcx
cmpq %rcx, %rax
je 0x4717e
movq %r14, (%rax)
addq $0x8, 0x58(%r13)
jmp 0x4718c
movq 0xb0(%rax), %r15
movl $0x18, %edi
callq 0x73a0
movq %r14, 0x10(%rax)
movq %rax, %rdi
movq %r15, %rsi
callq 0x7060
incq 0x10(%r15)
jmp 0x472fb
movl $0x0, (%r13)
cmpb $0x1, 0x20(%r13)
jne 0x472fb
movq 0x285c9(%rip), %rbx     # 0x6f678
cmpl $0x0, 0x34(%rbx)
je 0x472fb
xorl %r14d, %r14d
movabsq $0x25000000000000, %r15 # imm = 0x25000000000000
xorl %r12d, %r12d
movq %r13, %rbp
movq 0x8(%r13), %r13
leaq (%r14,%r13), %rdi
callq 0x47ae8
movb $0x0, 0x20(%rbp)
movq (%r13,%r14), %rax
movq 0x8(%rax), %rax
movl 0x8(%rax), %esi
movq 0x8(%rsp), %rdi
callq 0x26fe2
movq (%r13,%r14), %rax
movq (%rax), %rax
cmpq $0x800f, %rax           # imm = 0x800F
jg 0x47126
movl $0x4, %ecx
cmpq $0x35, %rax
jbe 0x4714e
leaq -0x60d0(%rax), %rdx
cmpq $0x2, %rdx
movq (%rsp), %r13
jae 0x4715a
movl $0x6, %ecx
jmp 0x47162
leaq -0x8010(%rax), %rcx
cmpq $0x2, %rcx
movq (%rsp), %r13
jb 0x47147
cmpq $0x85111, %rax          # imm = 0x85111
je 0x47147
cmpq $0x8510, %rax           # imm = 0x8510
jne 0x47166
movl $0x5, %ecx
jmp 0x47162
btq %rax, %r15
jae 0x4710e
movq (%rsp), %r13
jmp 0x47162
cmpq $0x321, %rax            # imm = 0x321
jne 0x47166
movl %ecx, (%r13)
incq %r12
movl 0x34(%rbx), %eax
addq $0x68, %r14
cmpq %rax, %r12
jb 0x470c9
jmp 0x472fb
leaq 0x28(%r13), %rdi
leaq 0x10(%rsp), %rsi
callq 0x47c9a
movq 0x284e5(%rip), %rax     # 0x6f678
movq 0x88(%rax), %rax
movq 0x8(%rsp), %rbx
movl 0x2c(%rbx), %ecx
movq (%rax,%rcx,8), %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x473c0
callq 0x28018
movq %rax, %rdi
callq 0x2841a
movq %rax, %r14
movq 0x284b1(%rip), %rax     # 0x6f678
movq 0x88(%rax), %rax
movl 0x2c(%rbx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%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 0x27ce2
addq %r14, %rax
movq 0x10(%rsp), %rcx
movq %rax, 0x50(%rcx)
cmpq %rax, 0x10(%r13)
jae 0x472fb
movq %rax, 0x10(%r13)
jmp 0x472fb
movl $0x0, (%r13)
cmpb $0x1, 0x20(%r13)
jne 0x472fb
movq 0x28447(%rip), %rbx     # 0x6f678
cmpl $0x0, 0x34(%rbx)
je 0x472fb
xorl %r14d, %r14d
movabsq $0x25000000000000, %r15 # imm = 0x25000000000000
xorl %r12d, %r12d
movq %r13, %rbp
movq 0x8(%r13), %r13
leaq (%r14,%r13), %rdi
callq 0x47ae8
movb $0x0, 0x20(%rbp)
movq (%r13,%r14), %rax
movq 0x8(%rax), %rax
movl 0x8(%rax), %esi
movq 0x8(%rsp), %rdi
callq 0x26fe2
movq (%r13,%r14), %rax
movq (%rax), %rax
cmpq $0x800f, %rax           # imm = 0x800F
jg 0x472a8
movl $0x4, %ecx
cmpq $0x35, %rax
jbe 0x472d0
leaq -0x60d0(%rax), %rdx
cmpq $0x2, %rdx
movq (%rsp), %r13
jae 0x472dc
movl $0x6, %ecx
jmp 0x472e4
leaq -0x8010(%rax), %rcx
cmpq $0x2, %rcx
movq (%rsp), %r13
jb 0x472c9
cmpq $0x85111, %rax          # imm = 0x85111
je 0x472c9
cmpq $0x8510, %rax           # imm = 0x8510
jne 0x472e8
movl $0x5, %ecx
jmp 0x472e4
btq %rax, %r15
jae 0x47290
movq (%rsp), %r13
jmp 0x472e4
cmpq $0x321, %rax            # imm = 0x321
jne 0x472e8
movl %ecx, (%r13)
incq %r12
movl 0x34(%rbx), %eax
addq $0x68, %r14
cmpq %rax, %r12
jb 0x4724b
movq 0x28376(%rip), %rdi     # 0x6f678
movq 0x88(%rdi), %rax
movq 0x8(%rsp), %rcx
movl 0x2c(%rcx), %esi
movq (%rax,%rsi,8), %rax
cmpl $0x1, 0x14(%rax)
jne 0x47322
callq 0x44dca
jmp 0x47333
cmpl $0x0, (%r13)
jne 0x47333
movq 0x8(%rsp), %rsi
callq 0x44e22
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq