// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %r15
pushq %r14
pushq %rbx
subq $0x20, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x40(%rdi), %rax
movq (%rsi), %rsi
movq 0x8(%r14), %rcx
movl 0x8(%rcx), %edi
movl 0x14(%rcx), %edx
movq (%rax,%rdi,8), %r15
movq %rbx, %rdi
callq 0x1f456
addq %rax, 0x48(%r15)
orq $-0x1, 0x18(%r15)
andq $0x0, 0x10(%r15)
andq $0x0, 0x28(%r15)
movl $0x1, 0xc(%r15)
movl 0x3c(%rbx), %eax
incl %eax
movl %eax, 0x3c(%rbx)
cmpl 0x48(%rbx), %eax
jne 0x1e6ea
andl $0x0, 0x38(%rbx)
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
subq 0xa0(%rbx), %rax
addq %rax, 0xd8(%rbx)
cmpq $-0x1, 0x98(%rbx)
je 0x1e6ea
callq 0x1f784
movq %rax, %rdi
callq 0x1fafc
subq 0x98(%rbx), %rax
addq %rax, 0xe8(%rbx)
movq (%r14), %rax
cmpq $0x35, %rax
jbe 0x1e823
leaq -0x60d0(%rax), %rcx
cmpq $0x2, %rcx
jb 0x1e8a5
leaq -0x8010(%rax), %rcx
cmpq $0x2, %rcx
jb 0x1e7b5
cmpq $0x85111, %rax          # imm = 0x85111
je 0x1e7b5
cmpq $0x8510, %rax           # imm = 0x8510
je 0x1e7b5
cmpq $0x321, %rax            # imm = 0x321
je 0x1e837
movq 0x438ac(%rip), %r14     # 0x61ff0
leaq 0x25e3d(%rip), %rsi     # 0x44588
movq %r14, %rdi
callq 0x7360
leaq 0x27876(%rip), %rsi     # 0x45fd0
movq %r14, %rdi
callq 0x7360
movq %rax, %r14
addq $0x8, %rbx
movq %rsp, %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x7480
movq %r14, %rdi
movq %r15, %rsi
callq 0x7310
leaq 0x278b3(%rip), %rsi     # 0x4603c
movq %rax, %rdi
callq 0x7360
movq %rax, %rdi
callq 0x71a0
movq %rsp, %rdi
callq 0x7678
movq 0x43828(%rip), %rdi     # 0x61fd0
callq 0x73d0
pushq $0x1
popq %rdi
callq 0x73e0
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rax
subq %rcx, %rax
pushq $0x18
popq %rsi
cqto
idivq %rsi
movq 0xb8(%rbx), %rdx
incq %rdx
xorl %esi, %esi
movl %esi, %edi
cmpq %rdi, %rax
jbe 0x1e90e
leaq (%rdx,%rsi), %r8
movq %r8, 0xb8(%rbx)
movq (%r15), %r8
imulq $0x18, %rdi, %r9
movl 0xc(%rcx,%r9), %r10d
movq (%r8,%r10,8), %r8
incq 0x18(%r8)
movq (%r8), %r8
movl 0x10(%rcx,%r9), %r10d
movl 0x14(%rcx,%r9), %r9d
movq (%r8,%r10,8), %r8
movq (%r8), %r8
movq 0x20(%r14), %r10
movq (%r10,%rdi,8), %rdi
movq %rdi, (%r8,%r9,8)
incq %rsi
jmp 0x1e7d4
movabsq $0x25000000000000, %rcx # imm = 0x25000000000000
btq %rax, %rcx
jae 0x1e6f7
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rax
subq %rcx, %rax
pushq $0x18
popq %rsi
cqto
idivq %rsi
movq 0xb0(%rbx), %rdx
incq %rdx
xorl %esi, %esi
movl %esi, %edi
cmpq %rdi, %rax
jbe 0x1e90e
leaq (%rdx,%rsi), %r8
movq %r8, 0xb0(%rbx)
movq (%r15), %r8
imulq $0x18, %rdi, %r9
movl 0xc(%rcx,%r9), %r10d
movq (%r8,%r10,8), %r8
incq 0x10(%r8)
movq (%r8), %r8
movl 0x10(%rcx,%r9), %r10d
movl 0x14(%rcx,%r9), %r9d
movq (%r8,%r10,8), %r8
movq (%r8), %r8
movq 0x20(%r14), %r10
movq (%r8,%r9,8), %r8
movq %r8, (%r10,%rdi,8)
incq %rsi
jmp 0x1e856
movq 0x8(%r14), %rcx
movq 0x10(%r14), %rax
subq %rcx, %rax
pushq $0x18
popq %rsi
cqto
idivq %rsi
movl 0x54(%rbx), %edx
movq 0xc0(%rbx), %rsi
xorl %edi, %edi
movl %edi, %edi
cmpq %rdi, %rax
jbe 0x1e90e
incq %rsi
movq %rsi, 0xc0(%rbx)
movq (%r15), %r8
imulq $0x18, %rdi, %r9
movl 0xc(%rcx,%r9), %r10d
movq (%r8,%r10,8), %r8
incq 0x20(%r8)
movq (%r8), %r8
movl 0x10(%rcx,%r9), %r9d
movq (%r8,%r9,8), %r8
xorl %r9d, %r9d
cmpq %r9, %rdx
je 0x1e90a
movq (%r8), %r10
orq $-0x1, (%r10,%r9,8)
incq %r9
jmp 0x1e8f8
incl %edi
jmp 0x1e8c4
movq %rbx, %rdi
movq %r14, %rsi
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
jmp 0x1eada
movq %rax, %rbx
movq %rsp, %rdi
callq 0x7678
movq %rbx, %rdi
callq 0x75d0
nop