// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
movq 0x450(%rsi), %rsi
leaq 0x34(%rsp), %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x18(%rsp), %r8
callq 0x231be
movl 0x434(%r14), %eax
addl $-0x8, %eax
cmpl $0x6f, %eax
ja 0x2308c
pushq $0x1
popq %rdx
leaq 0x4d6a(%rip), %rcx      # 0x279c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl %edx, (%rsp)
movb $0x1, %r13b
jmp 0x22c92
leaq 0xe91b(%rip), %rax      # 0x31591
movzbl (%rax), %eax
pushq $0x4
jmp 0x22c89
leaq 0xe90d(%rip), %rax      # 0x31591
movzbl (%rax), %eax
pushq $0x2
popq %rcx
subl %eax, %ecx
movl %ecx, (%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x10(%rsp), %rax
movq 0x218(%rax), %rbx
movl 0x34(%rsp), %r15d
movl 0x424(%r14), %eax
cvtsi2sd %rax, %xmm0
mulsd 0x4d07(%rip), %xmm0     # 0x279c0
callq 0x4080
addsd 0x13ea(%rip), %xmm0     # 0x240b0
cvttsd2si %xmm0, %rdx
movq %rbx, %rdi
movl %r15d, %esi
callq 0xf11c
movb %al, 0x7(%rsp)
testb %al, %al
je 0x2304e
leaq 0xe79f(%rip), %rax      # 0x31488
cmpb $0x1, (%rax)
jne 0x22e11
leaq 0xe790(%rip), %rax      # 0x31489
cmpb $0x0, (%rax)
jne 0x22e11
movq 0xe2c7(%rip), %rdi      # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x12, 0x10(%rdi,%rax)
pushq $0x20
popq %rbx
movl %ebx, %esi
callq 0x4440
movq 0x10(%rsp), %rcx
leaq 0x20(%rcx), %rsi
movq %rax, %rdi
callq 0x42b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xf, 0x10(%rax,%rcx)
movq %rax, %rdi
movl %ebx, %esi
callq 0x4440
movq %rax, %rdi
movq %r14, %rsi
callq 0x1e2c8
leaq 0x524f(%rip), %rsi      # 0x27faf
movq %rax, %rdi
callq 0x4300
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movl 0x18(%rax,%rdx), %esi
andl $-0x4b, %esi
orl $0x8, %esi
movl %esi, 0x18(%rax,%rdx)
movq -0x18(%rcx), %rcx
movq $0x9, 0x10(%rax,%rcx)
pushq $0x30
popq %rsi
movq %rax, %rdi
callq 0x4440
movq 0x450(%r14), %rsi
movq %rax, %rdi
callq 0x4210
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
leaq 0x5201(%rip), %rsi      # 0x27fc1
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movq %r15, %rsi
callq 0x4210
leaq 0x51f4(%rip), %rsi      # 0x27fce
movq %rax, %rdi
callq 0x4300
movl 0x1c(%rsp), %esi
movq %rax, %rdi
callq 0x4210
leaq 0x51e5(%rip), %rsi      # 0x27fda
movq %rax, %rdi
callq 0x4300
movl 0x18(%rsp), %esi
movq %rax, %rdi
callq 0x4210
movq %rax, %rdi
callq 0x4180
pushq $0x38
popq %rdi
callq 0x4310
movq %rax, %rbx
movl 0x42c(%r14), %edx
movl 0x1c(%rsp), %r8d
movl 0x18(%rsp), %r9d
movq 0x8(%r14), %rax
movl 0x434(%r14), %ecx
movzbl 0x422(%r14), %esi
subq $0x40, %rsp
movl %esi, 0x30(%rsp)
movzbl %r13b, %esi
movq %rsi, 0x68(%rsp)
movl %esi, 0x28(%rsp)
movl %ecx, 0x20(%rsp)
andl $0x0, 0x8(%rsp)
movq %rax, 0x10(%rsp)
andl $0x0, (%rsp)
movl $0x1, 0x18(%rsp)
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %esi, %esi
movl %r15d, %ecx
movl %r8d, 0x4c(%rsp)
movl %r9d, 0x48(%rsp)
callq 0x151c0
addq $0x40, %rsp
leaq 0x42c(%r14), %rax
movq %rax, 0x48(%rsp)
movq 0x10(%rsp), %r12
movq 0x218(%r12), %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0xf184
leaq 0x238(%r12), %rax
movq %rax, 0x40(%rsp)
movzbl %bpl, %eax
movq %rax, 0x20(%rsp)
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x424(%r14), %ebp
cvtsi2sd %rbp, %xmm0
mulsd 0x4ada(%rip), %xmm0     # 0x279c0
callq 0x4080
ucomisd 0x38(%rsp), %xmm0
jbe 0x2304e
addsd 0x3851(%rip), %xmm0     # 0x26750
movsd %xmm0, 0x50(%rsp)
pushq $0x38
popq %rdi
callq 0x4310
movq %rax, %rbx
movsd 0x50(%rsp), %xmm0
ucomisd 0x38(%rsp), %xmm0
movl 0x42c(%r14), %edx
movq 0x8(%r14), %rax
movl 0x434(%r14), %r10d
movzbl 0x422(%r14), %r11d
jbe 0x22f81
movl (%rsp), %r12d
cmpl $0x2, %r12d
je 0x22fb8
cmpl $0x4, %r12d
jne 0x22fea
subq $0x8, %rsp
movq %rbx, %rdi
pushq $0x3
popq %rsi
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 0x151c0
addq $0x40, %rsp
jmp 0x2301a
subq $0x8, %rsp
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 0x151c0
addq $0x40, %rsp
jmp 0x2301a
subq $0x8, %rsp
movq %rbx, %rdi
pushq $0x1
popq %rsi
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 0x151c0
addq $0x40, %rsp
jmp 0x2301a
subq $0x8, %rsp
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 0x151c0
addq $0x40, %rsp
movq 0x10(%rsp), %rax
movq 0x218(%rax), %rdi
movl %r15d, %esi
movq %rbx, %rdx
callq 0xf184
cmpl $0x2, %r12d
ja 0x23046
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0xe58e
incl %r13d
jmp 0x22ec7
movb 0x7(%rsp), %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe529(%rip), %rax      # 0x31591
movzbl (%rax), %eax
pushq $0x4
popq %rcx
subl %eax, %ecx
movl %ecx, (%rsp)
movb $0x1, %bpl
xorl %r13d, %r13d
jmp 0x22c94
movl %edx, (%rsp)
movb $0x1, %r13b
movb $0x1, %bpl
jmp 0x22c94
movq 0xdf5d(%rip), %rdi      # 0x30ff0
leaq 0xf88(%rip), %rsi       # 0x24022
callq 0x4300
leaq 0x4b5a(%rip), %rsi      # 0x27c00
movq %rax, %rdi
callq 0x4300
leaq 0x17c2(%rip), %rsi      # 0x24877
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movl $0x1b3, %esi            # imm = 0x1B3
callq 0x44f0
leaq 0xf70(%rip), %rsi       # 0x24041
movq %rax, %rdi
callq 0x4300
movq 0x10(%rsp), %rbx
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x42b0
leaq 0x4e85(%rip), %rsi      # 0x27f76
movq %rax, %rdi
callq 0x4300
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x40d0
leaq 0x43c5(%rip), %rsi      # 0x274d1
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
xorl %edi, %edi
callq 0x4370
jmp 0x2312b
jmp 0x2312b
jmp 0x2312b
jmp 0x2312b
movq %rax, %r14
pushq $0x38
popq %rsi
movq %rbx, %rdi
callq 0x4320
movq %r14, %rdi
callq 0x4520
nop