// File: /workspace/github_cpp_makefile_star2_O1/estwings57[P]CasHMC/sources/VaultController.cpp
// Function: CasHMC::VaultController::ConvPacketIntoCMDs(CasHMC::Packet*)
bool VaultController::ConvPacketIntoCMDs(Packet *packet)
{
	unsigned bankAdd, colAdd, rowAdd;
	AddressMapping(packet->ADRS, bankAdd, colAdd, rowAdd);
	
	DRAMCommandType tempCMD;
	bool tempPosted = false;
	bool atomic = false;
	switch(packet->CMD) {
		//Write
		case WR16:		tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	break;
		case WR32:		tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	break;
		case WR48:		tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	break;
		case WR64:		tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	break;
		case WR80:		tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	break;
		case WR96:		tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	break;
		case WR112:		tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	break;
		case WR128:		tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	break;
		case WR256:		tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	break;
		case MD_WR:		tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	break;
		//Poseted Write
		case P_WR16:	tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	tempPosted = true;	break;
		case P_WR32:	tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	tempPosted = true;	break;
		case P_WR48:	tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	tempPosted = true;	break;
		case P_WR64:	tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	tempPosted = true;	break;
		case P_WR80:	tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	tempPosted = true;	break;
		case P_WR96:	tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	tempPosted = true;	break;
		case P_WR112:	tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	tempPosted = true;	break;
		case P_WR128:	tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	tempPosted = true;	break;
		case P_WR256:	tempCMD = OPEN_PAGE ? WRITE : WRITE_P;	tempPosted = true;	break;
		//Read
		case RD16:		tempCMD = OPEN_PAGE ? READ : READ_P;	break;
		case RD32:		tempCMD = OPEN_PAGE ? READ : READ_P;	break;
		case RD48:		tempCMD = OPEN_PAGE ? READ : READ_P;	break;
		case RD64:		tempCMD = OPEN_PAGE ? READ : READ_P;	break;
		case RD80:		tempCMD = OPEN_PAGE ? READ : READ_P;	break;
		case RD96:		tempCMD = OPEN_PAGE ? READ : READ_P;	break;
		case RD112:		tempCMD = OPEN_PAGE ? READ : READ_P;	break;
		case RD128:		tempCMD = OPEN_PAGE ? READ : READ_P;	break;
		case RD256:		tempCMD = OPEN_PAGE ? READ : READ_P;	break;
		case MD_RD:		tempCMD = OPEN_PAGE ? READ : READ_P;	break;
		//Arithmetic atomic
		case _2ADD8:	atomic = true;	tempCMD = READ;	break;
		case ADD16:		atomic = true;	tempCMD = READ;	break;
		case P_2ADD8:	atomic = true;	tempCMD = READ;	tempPosted = true;	break;
		case P_ADD16:	atomic = true;	tempCMD = READ;	tempPosted = true;	break;
		case _2ADDS8R:	atomic = true;	tempCMD = READ;	break;
		case ADDS16R:	atomic = true;	tempCMD = READ;	break;
		case INC8:		atomic = true;	tempCMD = READ;	break;
		case P_INC8:	atomic = true;	tempCMD = READ;	tempPosted = true;	break;
		//Boolean atomic
		case XOR16:		atomic = true;	tempCMD = READ;	break;
		case OR16:		atomic = true;	tempCMD = READ;	break;
		case NOR16:		atomic = true;	tempCMD = READ;	break;
		case AND16:		atomic = true;	tempCMD = READ;	break;
		case NAND16:	atomic = true;	tempCMD = READ;	break;
		//Comparison atomic
		case CASGT8:	atomic = true;	tempCMD = READ;	break;
		case CASLT8:	atomic = true;	tempCMD = READ;	break;
		case CASGT16:	atomic = true;	tempCMD = READ;	break;
		case CASLT16:	atomic = true;	tempCMD = READ;	break;
		case CASEQ8:	atomic = true;	tempCMD = READ;	break;
		case CASZERO16:	atomic = true;	tempCMD = READ;	break;
		case EQ16:		atomic = true;	tempCMD = READ;	break;
		case EQ8:		atomic = true;	tempCMD = READ;	break;
		//Bitwise atomic
		case BWR:		atomic = true;	tempCMD = READ;	break;
		case P_BWR:		atomic = true;	tempCMD = READ;	tempPosted = true;	break;
		case BWR8R:		atomic = true;	tempCMD = READ;	break;
		case SWAP16:	atomic = true;	tempCMD = READ;	break;
		
		default:
			ERROR(header<<"  == Error - WRONG packet command type  (CurrentClock : "<<currentClockCycle<<")");
			exit(0);
	}
	
	//Due to the internal 32-byte granularity of the DRAM data bus within each vault in the HMC (HMC spec v2.1 p.99)
	if(commandQueue->AvailableSpace(bankAdd, ceil((double)packet->reqDataSize/32)+1)) {
		DEBUG(ALI(18)<<header<<ALI(15)<<*packet<<"Down) phyAdd : 0x"<<hex<<setw(9)<<setfill('0')<<packet->ADRS<<dec<<"  bankAdd : "<<bankAdd<<"  colAdd : "<<colAdd<<"  rowAdd : "<<rowAdd);
		//cmdtype, tag, bnk, col, rw, *dt, dSize, pst, *lat
		DRAMCommand *actCMD = new DRAMCommand(ACTIVATE, packet->TAG, bankAdd, colAdd, rowAdd, 0, false, packet->trace, true, packet->CMD, atomic, packet->segment);
		commandQueue->Enqueue(bankAdd, actCMD);
		
		for(int i=0; i<ceil((double)packet->reqDataSize/32); i++) {
			DRAMCommand *rwCMD;
			if(i < ceil((double)packet->reqDataSize/32)-1) {
				if(tempCMD == WRITE_P) {
					rwCMD = new DRAMCommand(WRITE, packet->TAG, bankAdd, colAdd, rowAdd, packet->reqDataSize, tempPosted, packet->trace, false, packet->CMD, atomic, packet->segment);
				}
				else if(tempCMD == READ_P) {
					rwCMD = new DRAMCommand(READ, packet->TAG, bankAdd, colAdd, rowAdd, packet->reqDataSize, tempPosted, packet->trace, false, packet->CMD, atomic, packet->segment);
				}
				else {
					rwCMD = new DRAMCommand(tempCMD, packet->TAG, bankAdd, colAdd, rowAdd, packet->reqDataSize, tempPosted, packet->trace, false, packet->CMD, atomic, packet->segment);
				}
			}
			else {
				rwCMD = new DRAMCommand(tempCMD, packet->TAG, bankAdd, colAdd, rowAdd, packet->reqDataSize, tempPosted, packet->trace, true, packet->CMD, atomic, packet->segment);
			}
			commandQueue->Enqueue(bankAdd, rwCMD);
			if(tempCMD == READ || tempCMD == READ_P) {
				pendingReadData.push_back(packet->TAG);
			}
		}
		return true;
	}
	else{
		return false;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x450(%rsi), %rsi
leaq 0x3c(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x18(%rsp), %r8
callq 0x2fd72
movl 0x434(%r14), %eax
addl $-0x8, %eax
cmpl $0x6f, %eax
ja 0x2fc44
leaq 0x5297(%rip), %rcx      # 0x34958
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, (%rsp)
movb $0x1, %r13b
jmp 0x2f6fe
leaq 0xceb4(%rip), %rax      # 0x3c591
movzbl (%rax), %eax
movl $0x4, %ecx
jmp 0x2f6f6
leaq 0xcea3(%rip), %rax      # 0x3c591
movzbl (%rax), %eax
movl $0x2, %ecx
subl %eax, %ecx
movl %ecx, (%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movq %rbx, 0x10(%rsp)
movq 0x218(%rbx), %rbx
movl 0x3c(%rsp), %r15d
movl 0x424(%r14), %eax
cvtsi2sd %rax, %xmm0
mulsd 0x522a(%rip), %xmm0     # 0x34950
callq 0x4090
addsd 0x197d(%rip), %xmm0     # 0x310b0
cvttsd2si %xmm0, %rdx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x14e5e
movb %al, 0x7(%rsp)
testb %al, %al
je 0x2fc00
leaq 0xcd32(%rip), %rax      # 0x3c488
cmpb $0x1, (%rax)
jne 0x2f957
leaq 0xcd23(%rip), %rax      # 0x3c489
cmpb $0x0, (%rax)
jne 0x2f957
movl %r12d, %ebp
movq 0xc857(%rip), %rax      # 0x3bfd0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0x12, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
leaq (%rax,%rcx), %rbx
cmpb $0x0, 0xe1(%rax,%rcx)
jne 0x2f7b5
movq %rbx, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%rbx)
movb $0x1, 0xe1(%rbx)
movb $0x20, 0xe0(%rbx)
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
movq 0xc800(%rip), %rdi      # 0x3bfd0
callq 0x4340
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rcx
movq $0xf, 0x10(%rbx,%rcx)
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r12
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0x2f818
movq %r12, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r12)
movb $0x1, 0xe1(%r12)
movb $0x20, 0xe0(%r12)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28bbe
movq %rax, %rbx
leaq 0x5709(%rip), %rsi      # 0x34f3f
movl $0x11, %edx
movq %rax, %rdi
callq 0x4340
movq (%rbx), %rax
movq -0x18(%rax), %rcx
movl 0x18(%rbx,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rbx,%rcx)
movq -0x18(%rax), %rcx
movq $0x9, 0x10(%rbx,%rcx)
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r12
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0x2f895
movq %r12, %rdi
movl $0x20, %esi
callq 0x42d0
movb %al, 0xe0(%r12)
movb $0x1, 0xe1(%r12)
movb $0x30, 0xe0(%r12)
movq 0x450(%r14), %rsi
movq %rbx, %rdi
callq 0x41e0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rax
movl 0x18(%rbx,%rax), %ecx
andl $-0x4b, %ecx
orl $0x2, %ecx
movl %ecx, 0x18(%rbx,%rax)
leaq 0x5685(%rip), %rsi      # 0x34f51
movl $0xc, %edx
movq %rbx, %rdi
callq 0x4340
movq %rbx, %rdi
movq %r15, %rsi
callq 0x41e0
movq %rax, %rbx
leaq 0x5670(%rip), %rsi      # 0x34f5e
movl $0xb, %edx
movq %rax, %rdi
callq 0x4340
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0x41e0
movq %rax, %rbx
leaq 0x5659(%rip), %rsi      # 0x34f6a
movl $0xb, %edx
movq %rax, %rdi
callq 0x4340
movl 0x18(%rsp), %esi
movq %rbx, %rdi
callq 0x41e0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x42d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x41f0
movl %ebp, %r12d
movl $0x38, %edi
callq 0x42f0
movq %rax, %rbx
movl 0x42c(%r14), %edx
movl 0x1c(%rsp), %r8d
movl 0x18(%rsp), %r9d
movl 0x434(%r14), %eax
movzbl 0x422(%r14), %r10d
subq $0x8, %rsp
movzbl %r13b, %r13d
xorl %r11d, %r11d
movq %rbx, %rdi
xorl %esi, %esi
movl %r15d, %ecx
movl %r8d, 0x14(%rsp)
movl %r9d, 0x10(%rsp)
pushq %r10
movq %r13, 0x38(%rsp)
pushq %r13
pushq %rax
pushq $0x1
pushq 0x8(%r14)
pushq %r11
pushq %r11
callq 0x1c9f0
addq $0x40, %rsp
movq 0x10(%rsp), %rax
movq 0x218(%rax), %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0x14eb4
movl 0x424(%r14), %ebp
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
mulsd 0x4f64(%rip), %xmm0     # 0x34950
callq 0x4090
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2fc00
leaq 0x42c(%r14), %rax
movq %rax, 0x40(%rsp)
movq 0x10(%rsp), %rax
addq $0x238, %rax            # imm = 0x238
movq %rax, 0x48(%rsp)
movl $0x1, %r13d
movzbl %r12b, %eax
movq %rax, 0x20(%rsp)
movsd %xmm1, 0x30(%rsp)
addsd 0x3ca0(%rip), %xmm0     # 0x336d8
movsd %xmm0, 0x50(%rsp)
movl $0x38, %edi
callq 0x42f0
movq %rax, %rbx
movsd 0x50(%rsp), %xmm0
ucomisd 0x30(%rsp), %xmm0
movl 0x42c(%r14), %edx
movq 0x8(%r14), %rax
movl 0x434(%r14), %r10d
movb 0x422(%r14), %cl
jbe 0x2fac5
movl (%rsp), %r12d
cmpl $0x2, %r12d
je 0x2fb00
cmpl $0x4, %r12d
jne 0x2fb38
subq $0x8, %rsp
movzbl %cl, %r11d
movq %rbx, %rdi
movl $0x3, %esi
movl %r15d, %ecx
movl 0x14(%rsp), %r8d
movl 0x10(%rsp), %r9d
pushq %r11
pushq 0x38(%rsp)
pushq %r10
pushq $0x0
pushq %rax
pushq 0x50(%rsp)
pushq %rbp
callq 0x1c9f0
addq $0x40, %rsp
jmp 0x2fb6c
subq $0x8, %rsp
movzbl %cl, %r11d
movq %rbx, %rdi
movl 0x8(%rsp), %r12d
movl %r12d, %esi
movl %r15d, %ecx
movl 0x14(%rsp), %r8d
movl 0x10(%rsp), %r9d
pushq %r11
pushq 0x38(%rsp)
pushq %r10
pushq $0x1
pushq %rax
pushq 0x50(%rsp)
pushq %rbp
callq 0x1c9f0
addq $0x40, %rsp
jmp 0x2fb6c
subq $0x8, %rsp
movzbl %cl, %r11d
movq %rbx, %rdi
movl $0x1, %esi
movl %r15d, %ecx
movl 0x14(%rsp), %r8d
movl 0x10(%rsp), %r9d
pushq %r11
pushq 0x38(%rsp)
pushq %r10
pushq $0x0
pushq %rax
pushq 0x50(%rsp)
pushq %rbp
callq 0x1c9f0
addq $0x40, %rsp
jmp 0x2fb6c
subq $0x8, %rsp
movzbl %cl, %r11d
movq %rbx, %rdi
movl %r12d, %esi
movl %r15d, %ecx
movl 0x14(%rsp), %r8d
movl 0x10(%rsp), %r9d
pushq %r11
pushq 0x38(%rsp)
pushq %r10
pushq $0x0
pushq %rax
pushq 0x50(%rsp)
pushq %rbp
callq 0x1c9f0
addq $0x40, %rsp
movq 0x10(%rsp), %rax
movq 0x218(%rax), %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0x14eb4
cmpl $0x2, %r12d
ja 0x2fbc3
movq 0x10(%rsp), %rcx
movq 0x240(%rcx), %rsi
cmpq 0x248(%rcx), %rsi
je 0x2fbb4
movq 0x40(%rsp), %rax
movl (%rax), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x240(%rcx)
jmp 0x2fbc3
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rdx
callq 0x14232
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x424(%r14), %ebp
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
mulsd 0x4d68(%rip), %xmm0     # 0x34950
callq 0x4090
movsd 0x30(%rsp), %xmm1
incl %r13d
ucomisd %xmm1, %xmm0
ja 0x2fa2a
movb 0x7(%rsp), %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc977(%rip), %rax      # 0x3c591
movzbl (%rax), %eax
movl $0x4, %ecx
subl %eax, %ecx
movl %ecx, (%rsp)
movb $0x1, %r12b
xorl %r13d, %r13d
jmp 0x2f701
movl $0x1, (%rsp)
movb $0x1, %r13b
movb $0x1, %r12b
jmp 0x2f701
movq 0xc3a5(%rip), %rdi      # 0x3bff0
leaq 0x13d0(%rip), %rsi      # 0x31022
callq 0x42e0
leaq 0x4f32(%rip), %rsi      # 0x34b90
movq %rax, %rdi
callq 0x42e0
leaq 0x1bca(%rip), %rsi      # 0x31837
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
movl $0x1b3, %esi            # imm = 0x1B3
callq 0x4500
leaq 0x13b8(%rip), %rsi      # 0x31041
movq %rax, %rdi
callq 0x42e0
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x4290
leaq 0x5262(%rip), %rsi      # 0x34f06
movq %rax, %rdi
callq 0x42e0
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x40c0
leaq 0x47a2(%rip), %rsi      # 0x34461
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
callq 0x4170
xorl %edi, %edi
callq 0x4360
jmp 0x2fcde
jmp 0x2fcde
jmp 0x2fcde
jmp 0x2fcde
movq %rax, %r14
movl $0x38, %esi
movq %rbx, %rdi
callq 0x4300
movq %r14, %rdi
callq 0x4540