// File: CMU-SAFARI[P]MQSim/src/nvm_chip/flash_memory/Flash_Chip.cpp
// Function: NVM::FlashMemory::Flash_Chip::finish_command_execution(NVM::FlashMemory::Flash_Command*)
void Flash_Chip::finish_command_execution(Flash_Command* command)
		{
			Die* targetDie = Dies[command->Address[0].DieID];

			targetDie->STAT_TotalReadTime += Get_command_execution_latency(command->CommandCode, command->Address[0].PageID);
			targetDie->Expected_finish_time = INVALID_TIME;
			targetDie->CommandFinishEvent = NULL;
			targetDie->CurrentCMD = NULL;
			targetDie->Status = DieStatus::IDLE;
			this->idleDieNo++;
			if (idleDieNo == die_no) {
				this->status = Internal_Status::IDLE;
				STAT_totalExecTime += Simulator->Time() - executionStartTime;
				if (this->lastTransferStart != INVALID_TIME) {
					STAT_totalOverlappedXferExecTime += Simulator->Time() - lastTransferStart;
				}
			}

			switch (command->CommandCode)
			{
				case CMD_READ_PAGE:
				case CMD_READ_PAGE_MULTIPLANE:
				case CMD_READ_PAGE_COPYBACK:
				case CMD_READ_PAGE_COPYBACK_MULTIPLANE:
					DEBUG("Channel " << this->ChannelID << " Chip " << this->ChipID << "- Finished executing read command")
					for (unsigned int planeCntr = 0; planeCntr < command->Address.size(); planeCntr++) {
						STAT_readCount++;
						targetDie->Planes[command->Address[planeCntr].PlaneID]->Read_count++;
						targetDie->Planes[command->Address[planeCntr].PlaneID]->Blocks[command->Address[planeCntr].BlockID]->Pages[command->Address[planeCntr].PageID].Read_metadata(command->Meta_data[planeCntr]);
					}
					break;
				case CMD_PROGRAM_PAGE:
				case CMD_PROGRAM_PAGE_MULTIPLANE:
				case CMD_PROGRAM_PAGE_COPYBACK:
				case CMD_PROGRAM_PAGE_COPYBACK_MULTIPLANE:
					DEBUG("Channel " << this->ChannelID << " Chip " << this->ChipID << "- Finished executing program command")
					for (unsigned int planeCntr = 0; planeCntr < command->Address.size(); planeCntr++) {
						STAT_progamCount++;
						targetDie->Planes[command->Address[planeCntr].PlaneID]->Progam_count++;
						targetDie->Planes[command->Address[planeCntr].PlaneID]->Blocks[command->Address[planeCntr].BlockID]->Pages[command->Address[planeCntr].PageID].Write_metadata(command->Meta_data[planeCntr]);
					}
					break;
				case CMD_ERASE_BLOCK:
				case CMD_ERASE_BLOCK_MULTIPLANE:
				{
					for (unsigned int planeCntr = 0; planeCntr < command->Address.size(); planeCntr++) {
						STAT_eraseCount++;
						targetDie->Planes[command->Address[planeCntr].PlaneID]->Erase_count++;
						Block* targetBlock = targetDie->Planes[command->Address[planeCntr].PlaneID]->Blocks[command->Address[planeCntr].BlockID];
						for (unsigned int i = 0; i < page_no_per_block; i++) {
							//targetBlock->Pages[i].Metadata.SourceStreamID = NO_STREAM;
							//targetBlock->Pages[i].Metadata.Status = FREE_PAGE;
							targetBlock->Pages[i].Metadata.LPA = NO_LPA;
						}
					}
					break;
				}
				default:
					PRINT_ERROR("Flash chip " << ID() << ": unhandled flash command type!")
			}

			//In MQSim, flash chips always announce their status using the ready/busy signal; the controller does not issue a die status read command
			broadcast_ready_signal(command);
		}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x130(%rsp)
movq %rsi, 0x128(%rsp)
movq 0x130(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x40(%rax), %rax
movq %rax, 0xb8(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0xb8(%rsp), %rax
movl 0x8(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x120(%rsp)
movq 0x128(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xc0(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x45760
movq 0xc0(%rsp), %rsi
movq 0xc8(%rsp), %rdi
movl 0x14(%rax), %edx
callq 0x457a0
movq 0xc8(%rsp), %rcx
movq %rax, %rdx
movq 0x120(%rsp), %rax
addq 0x48(%rax), %rdx
movq %rdx, 0x48(%rax)
movq 0x120(%rsp), %rax
movq $-0x1, 0x18(%rax)
movq 0x120(%rsp), %rax
movq $0x0, 0x10(%rax)
movq 0x120(%rsp), %rax
movq $0x0, 0x28(%rax)
movq 0x120(%rsp), %rax
movl $0x1, 0xc(%rax)
movl 0x3c(%rcx), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rcx)
movl 0x3c(%rcx), %eax
cmpl 0x48(%rcx), %eax
jne 0x43d42
movq 0xc8(%rsp), %rax
movl $0x0, 0x38(%rax)
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq %rax, %rcx
movq 0xc8(%rsp), %rax
subq 0xa0(%rax), %rcx
addq 0xd8(%rax), %rcx
movq %rcx, 0xd8(%rax)
cmpq $-0x1, 0x98(%rax)
je 0x43d40
callq 0x46500
movq %rax, %rdi
callq 0x46c50
movq %rax, %rcx
movq 0xc8(%rsp), %rax
subq 0x98(%rax), %rcx
addq 0xe8(%rax), %rcx
movq %rcx, 0xe8(%rax)
jmp 0x43d42
movq 0x128(%rsp), %rax
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
subq $0x30, %rax
je 0x43dfa
jmp 0x43d61
movq 0xb0(%rsp), %rax
subq $0x32, %rax
je 0x43dfa
jmp 0x43d75
movq 0xb0(%rsp), %rax
subq $0x35, %rax
je 0x43dfa
jmp 0x43d85
movq 0xb0(%rsp), %rax
subq $0x321, %rax            # imm = 0x321
je 0x43dfa
jmp 0x43d97
movq 0xb0(%rsp), %rax
addq $-0x60d0, %rax          # imm = 0x9F30
subq $0x2, %rax
jb 0x44118
jmp 0x43db1
movq 0xb0(%rsp), %rax
addq $-0x8010, %rax          # imm = 0xFFFF7FF0
subq $0x2, %rax
jb 0x43f9b
jmp 0x43dcb
movq 0xb0(%rsp), %rax
subq $0x8510, %rax           # imm = 0x8510
je 0x43f9b
jmp 0x43de1
movq 0xb0(%rsp), %rax
subq $0x85111, %rax          # imm = 0x85111
je 0x43f9b
jmp 0x4428b
movl $0x0, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
movq %rax, 0xa8(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
callq 0x45780
movq %rax, %rcx
movq 0xa8(%rsp), %rax
cmpq %rcx, %rax
jae 0x43f96
movq 0xc8(%rsp), %rax
movq 0xb0(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xb0(%rax)
movq 0x120(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x80(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x11c(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x80(%rsp), %rax
movl 0xc(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movq 0x120(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x88(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x11c(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x88(%rsp), %rax
movl 0xc(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, 0x90(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x11c(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x90(%rsp), %rax
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x11c(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x98(%rsp), %rax
movl 0x14(%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0xa0(%rsp)
movq 0x128(%rsp), %rdi
addq $0x20, %rdi
movl 0x11c(%rsp), %eax
movl %eax, %esi
callq 0x45990
movq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x45970
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x43e05
jmp 0x4435a
movl $0x0, 0x118(%rsp)
movl 0x118(%rsp), %eax
movq %rax, 0x78(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
callq 0x45780
movq %rax, %rcx
movq 0x78(%rsp), %rax
cmpq %rcx, %rax
jae 0x44113
movq 0xc8(%rsp), %rax
movq 0xb8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xb8(%rax)
movq 0x120(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x50(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x118(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x50(%rsp), %rax
movl 0xc(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x18(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x18(%rax)
movq 0x120(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x118(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x58(%rsp), %rax
movl 0xc(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x118(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x60(%rsp), %rax
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, 0x68(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x118(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x68(%rsp), %rax
movl 0x14(%rcx), %ecx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x70(%rsp)
movq 0x128(%rsp), %rdi
addq $0x20, %rdi
movl 0x118(%rsp), %eax
movl %eax, %esi
callq 0x45990
movq 0x70(%rsp), %rdi
movq %rax, %rsi
callq 0x459b0
movl 0x118(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x118(%rsp)
jmp 0x43fa6
jmp 0x4435a
movl $0x0, 0x114(%rsp)
movl 0x114(%rsp), %eax
movq %rax, 0x48(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
callq 0x45780
movq %rax, %rcx
movq 0x48(%rsp), %rax
cmpq %rcx, %rax
jae 0x44286
movq 0xc8(%rsp), %rax
movq 0xc0(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0xc0(%rax)
movq 0x120(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x114(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x30(%rsp), %rax
movl 0xc(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20(%rax)
movq 0x120(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x114(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x38(%rsp), %rax
movl 0xc(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x128(%rsp), %rdi
addq $0x8, %rdi
movl 0x114(%rsp), %eax
movl %eax, %esi
callq 0x45760
movq %rax, %rcx
movq 0x40(%rsp), %rax
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, 0x108(%rsp)
movl $0x0, 0x104(%rsp)
movq 0xc8(%rsp), %rcx
movl 0x104(%rsp), %eax
cmpl 0x54(%rcx), %eax
jae 0x4426e
movq 0x108(%rsp), %rax
movq (%rax), %rax
movl 0x104(%rsp), %ecx
movq $-0x1, (%rax,%rcx,8)
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x4422d
jmp 0x44270
movl 0x114(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
jmp 0x44123
jmp 0x4435a
movq 0x95d5e(%rip), %rdi     # 0xd9ff0
movq %rdi, 0x10(%rsp)
leaq 0x6664a(%rip), %rsi     # 0xaa8e8
callq 0x8440
movq 0x10(%rsp), %rdi
leaq 0x67ec1(%rip), %rsi     # 0xac170
callq 0x8440
movq 0xc8(%rsp), %rsi
movq %rax, 0x18(%rsp)
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0xcdf0
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x83e0
movq %rax, 0x28(%rsp)
jmp 0x442e9
movq 0x28(%rsp), %rdi
leaq 0x67ee7(%rip), %rsi     # 0xac1dc
callq 0x8440
movq %rax, 0x8(%rsp)
jmp 0x44301
movq 0x8(%rsp), %rdi
movq 0x95c4b(%rip), %rsi     # 0xd9f58
callq 0x8480
jmp 0x44314
leaq 0xe0(%rsp), %rdi
callq 0x8848
movq 0x95ca8(%rip), %rdi     # 0xd9fd0
callq 0x84f0
movl $0x1, %edi
callq 0x8500
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x8848
jmp 0x44377
movq 0xc8(%rsp), %rdi
movq 0x128(%rsp), %rsi
callq 0x44690
addq $0x138, %rsp            # imm = 0x138
retq
movq 0xd8(%rsp), %rdi
callq 0x8770
nopw %cs:(%rax,%rax)