// File: /workspace/github_cpp_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %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 0x27e7c
addq %rax, 0x48(%r15)
movq $-0x1, 0x18(%r15)
xorl %eax, %eax
movq %rax, 0x10(%r15)
movq %rax, 0x28(%r15)
movl $0x1, 0xc(%r15)
movl 0x3c(%rbx), %eax
incl %eax
movl %eax, 0x3c(%rbx)
cmpl 0x48(%rbx), %eax
jne 0x26b13
movl $0x0, 0x38(%rbx)
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
subq 0xa0(%rbx), %rax
addq %rax, 0xd8(%rbx)
cmpq $-0x1, 0x98(%rbx)
je 0x26b13
callq 0x281b4
movq %rax, %rdi
callq 0x285a6
subq 0x98(%rbx), %rax
addq %rax, 0xe8(%rbx)
movq (%r14), %rax
cmpq $0x800f, %rax           # imm = 0x800F
jg 0x26be1
cmpq $0x35, %rax
jbe 0x26c7d
leaq -0x60d0(%rax), %rcx
cmpq $0x2, %rcx
jae 0x26c92
movq 0x8(%r14), %r12
movq 0x10(%r14), %rax
subq %r12, %rax
je 0x26d10
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq (%r15), %rax
movq %rax, 0x8(%rsp)
movl 0x54(%rbx), %r15d
movq 0xc0(%rbx), %rbp
movq %r15, %rax
shlq $0x3, %rax
movq %rax, (%rsp)
movl $0x1, %r13d
xorl %eax, %eax
leaq (%rax,%rax,2), %rax
movl 0xc(%r12,%rax,8), %ecx
movq 0x8(%rsp), %rdx
movq (%rdx,%rcx,8), %rcx
incq 0x20(%rcx)
testl %r15d, %r15d
je 0x26bc5
leaq (%r12,%rax,8), %rax
movq (%rcx), %rcx
movl 0x10(%rax), %eax
movq (%rcx,%rax,8), %rax
movq (%rax), %rdi
movl $0xff, %esi
movq (%rsp), %rdx
callq 0x7230
movl %r13d, %eax
incl %r13d
incq %rbp
cmpq %rax, 0x10(%rsp)
ja 0x26b8b
movq %rbp, 0xc0(%rbx)
jmp 0x26d10
leaq -0x8010(%rax), %rcx
cmpq $0x2, %rcx
jb 0x26c02
cmpq $0x8510, %rax           # imm = 0x8510
je 0x26c02
cmpq $0x85111, %rax          # imm = 0x85111
jne 0x26d3e
movq 0x8(%r14), %rax
movq 0x10(%r14), %rdx
subq %rax, %rdx
je 0x26d10
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
movq (%r15), %rsi
movq 0x20(%r14), %rdi
movq 0xb8(%rbx), %rdx
addq $0x14, %rax
xorl %r8d, %r8d
movl -0x8(%rax), %r9d
movq (%rsi,%r9,8), %r9
incq 0x18(%r9)
movq (%r9), %r9
movl -0x4(%rax), %r10d
movl (%rax), %r11d
movq (%r9,%r10,8), %r9
movq (%r9), %r9
movq (%rdi,%r8,8), %r10
movq %r10, (%r9,%r11,8)
incq %r8
movl %r8d, %r9d
addq $0x18, %rax
cmpq %r9, %rcx
ja 0x26c3a
addq %r8, %rdx
movq %rdx, 0xb8(%rbx)
jmp 0x26d10
movabsq $0x25000000000000, %rcx # imm = 0x25000000000000
btq %rax, %rcx
jb 0x26c9e
jmp 0x26b2c
cmpq $0x321, %rax            # imm = 0x321
jne 0x26d3e
movq 0x8(%r14), %rax
movq 0x10(%r14), %rdx
subq %rax, %rdx
je 0x26d10
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
movq (%r15), %rsi
movq 0x20(%r14), %rdi
movq 0xb0(%rbx), %rdx
addq $0x14, %rax
xorl %r8d, %r8d
movl -0x8(%rax), %r9d
movq (%rsi,%r9,8), %r9
incq 0x10(%r9)
movq (%r9), %r9
movl -0x4(%rax), %r10d
movl (%rax), %r11d
movq (%r9,%r10,8), %r9
movq (%r9), %r9
movq (%r9,%r11,8), %r9
movq %r9, (%rdi,%r8,8)
incq %r8
movl %r8d, %r9d
addq $0x18, %rax
cmpq %r9, %rcx
ja 0x26cd2
addq %r8, %rdx
movq %rdx, 0xb0(%rbx)
movq 0xf0(%rbx), %r15
cmpq 0xf8(%rbx), %r15
je 0x26d2f
movq %rbx, %rdi
movq %r14, %rsi
callq *(%r15)
addq $0x8, %r15
jmp 0x26d17
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x482ab(%rip), %r14     # 0x6eff0
leaq 0x2f85c(%rip), %rsi     # 0x565a8
movl $0x6, %edx
movq %r14, %rdi
callq 0x7400
leaq 0x3135f(%rip), %rsi     # 0x580bf
movl $0xb, %edx
movq %r14, %rdi
callq 0x7400
leaq 0x28(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdx
addq %rsi, %rdx
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0xb036
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq %r14, %rdi
callq 0x7400
movq %rax, %rbx
leaq 0x31384(%rip), %rsi     # 0x5812b
movl $0x1f, %edx
movq %rax, %rdi
callq 0x7400
movq %rbx, %rdi
callq 0x71c0
leaq 0x18(%rsp), %rdi
callq 0x76b8
movq 0x48203(%rip), %rdi     # 0x6efd0
callq 0x7430
movl $0x1, %edi
callq 0x7440
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r15, %rdi
je 0x26dee
callq 0x7370
movq %rbx, %rdi
callq 0x7620