// File: /workspace/github_cpp_makefile_star2_O3/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 0x2f420
movl 0x434(%r14), %eax
addl $-0x8, %eax
cmpl $0x6f, %eax
ja 0x2f2f1
leaq 0x4c85(%rip), %rcx      # 0x339d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x1, (%rsp)
movb $0x1, %r13b
jmp 0x2ed90
leaq 0xc822(%rip), %rax      # 0x3b591
movzbl (%rax), %eax
movl $0x4, %ecx
jmp 0x2ed88
leaq 0xc811(%rip), %rax      # 0x3b591
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 0x4c18(%rip), %xmm0     # 0x339d0
callq 0x4090
addsd 0x12eb(%rip), %xmm0     # 0x300b0
cvttsd2si %xmm0, %rdx
movq %rbx, %rdi
movl %r15d, %esi
callq 0x149be
movb %al, 0x7(%rsp)
testb %al, %al
je 0x2f2ad
leaq 0xc6a0(%rip), %rax      # 0x3b488
cmpb $0x1, (%rax)
jne 0x2efd3
leaq 0xc691(%rip), %rax      # 0x3b489
cmpb $0x0, (%rax)
jne 0x2efd3
movl %r12d, %ebp
movq 0xc1c5(%rip), %rax      # 0x3afd0
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 0x2ee41
movq %rbx, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%rbx)
movb $0x20, 0xe0(%rbx)
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
movq 0xc174(%rip), %rdi      # 0x3afd0
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 0x2ee9c
movq %r12, %rdi
movl $0x20, %esi
callq 0x42d0
movb $0x1, 0xe1(%r12)
movb $0x20, 0xe0(%r12)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28270
movq %rax, %rbx
leaq 0x5105(%rip), %rsi      # 0x33fbf
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 0x2ef11
movq %r12, %rdi
movl $0x20, %esi
callq 0x42d0
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 0x5089(%rip), %rsi      # 0x33fd1
movl $0xc, %edx
movq %rbx, %rdi
callq 0x4340
movq %rbx, %rdi
movq %r15, %rsi
callq 0x41e0
movq %rax, %rbx
leaq 0x5074(%rip), %rsi      # 0x33fde
movl $0xb, %edx
movq %rax, %rdi
callq 0x4340
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0x41e0
movq %rax, %rbx
leaq 0x505d(%rip), %rsi      # 0x33fea
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 0x1c274
addq $0x40, %rsp
movq 0x10(%rsp), %rax
movq 0x218(%rax), %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0x14a14
movl 0x424(%r14), %ebp
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
mulsd 0x4968(%rip), %xmm0     # 0x339d0
callq 0x4090
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2f2ad
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 0x36ac(%rip), %xmm0     # 0x32760
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 0x2f158
movl (%rsp), %r12d
cmpl $0x2, %r12d
je 0x2f196
cmpl $0x4, %r12d
jne 0x2f1e5
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 0x1c274
addq $0x40, %rsp
movq 0x10(%rsp), %rax
movq 0x218(%rax), %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0x14a14
jmp 0x2f270
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 0x1c274
addq $0x40, %rsp
jmp 0x2f219
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 0x1c274
addq $0x40, %rsp
movq 0x10(%rsp), %rax
movq 0x218(%rax), %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0x14a14
jmp 0x2f236
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 0x1c274
addq $0x40, %rsp
movq 0x10(%rsp), %rax
movq 0x218(%rax), %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0x14a14
cmpl $0x2, %r12d
ja 0x2f270
movq 0x10(%rsp), %rcx
movq 0x240(%rcx), %rsi
cmpq 0x248(%rcx), %rsi
je 0x2f261
movq 0x40(%rsp), %rax
movl (%rax), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x240(%rcx)
jmp 0x2f270
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rdx
callq 0x13de0
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x424(%r14), %ebp
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
mulsd 0x473b(%rip), %xmm0     # 0x339d0
callq 0x4090
movsd 0x30(%rsp), %xmm1
incl %r13d
ucomisd %xmm1, %xmm0
ja 0x2f0a6
movb 0x7(%rsp), %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc2ca(%rip), %rax      # 0x3b591
movzbl (%rax), %eax
movl $0x4, %ecx
subl %eax, %ecx
movl %ecx, (%rsp)
movb $0x1, %r12b
xorl %r13d, %r13d
jmp 0x2ed93
movl $0x1, (%rsp)
movb $0x1, %r13b
movb $0x1, %r12b
jmp 0x2ed93
movq 0xbcf8(%rip), %rdi      # 0x3aff0
leaq 0xd23(%rip), %rsi       # 0x30022
callq 0x42e0
leaq 0x4905(%rip), %rsi      # 0x33c10
movq %rax, %rdi
callq 0x42e0
leaq 0x157d(%rip), %rsi      # 0x30897
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
movl $0x1b3, %esi            # imm = 0x1B3
callq 0x4500
leaq 0xd0b(%rip), %rsi       # 0x30041
movq %rax, %rdi
callq 0x42e0
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x4290
leaq 0x4c35(%rip), %rsi      # 0x33f86
movq %rax, %rdi
callq 0x42e0
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x40c0
leaq 0x4175(%rip), %rsi      # 0x334e1
movq %rax, %rdi
callq 0x42e0
movq %rax, %rdi
callq 0x4170
xorl %edi, %edi
callq 0x4360
jmp 0x2f38b
jmp 0x2f38b
jmp 0x2f38b
jmp 0x2f38b
movq %rax, %r14
movl $0x38, %esi
movq %rbx, %rdi
callq 0x4300
movq %r14, %rdi
callq 0x4540
nop