// File: /workspace/github_cpp_makefile_star2_O2/estwings57[P]CasHMC/sources/VaultController.cpp
// Function: CasHMC::VaultController::Update()
void VaultController::Update()
{
	//Update DRAM state and various countdown
	UpdateCountdown();

	//Convert request packet into DRAM commands
	if(bufPopDelay == 0) {
		for(int i=0; i<downBuffers.size(); i++) {
			//Make sure that buffer[0] is not virtual tail packet.
			if(downBuffers[i] != NULL) {
				if(ConvPacketIntoCMDs(downBuffers[i])) {
					int tempLNG = downBuffers[i]->LNG;
					delete downBuffers[i];
					downBuffers.erase(downBuffers.begin()+i, downBuffers.begin()+i+tempLNG);
				}
			}
		}
	}
	
	//Send response packet to crossbar switch
	if(upBuffers.size() > 0) {
		//Make sure that buffer[0] is not virtual tail packet.
		if(upBuffers[0] == NULL) {
			ERROR(header<<"  == Error - Vault controller up buffer[0] is NULL (It could be one of virtual tail packet occupying packet length  (CurrentClock : "<<currentClockCycle<<")");
			exit(0);
		}
		else{
			if(upBufferDest->ReceiveUp(upBuffers[0])) {
				DEBUG(ALI(18)<<header<<ALI(15)<<*upBuffers[0]<<"Up)   SENDING packet to crossbar switch (CS)");
				upBuffers.erase(upBuffers.begin(), upBuffers.begin()+upBuffers[0]->LNG);
			}
			else {
				//DEBUG(ALI(18)<<header<<ALI(15)<<*upBuffers[0]<<"Up)   Crossbar switch buffer FULL");	
			}
		}
	}
	
	//Pop command from command queue
	if(commandQueue->CmdPop(&poppedCMD)) {
		//Write data command will be issued after countdown
		if(poppedCMD->commandType == WRITE || poppedCMD->commandType == WRITE_P) {
			DRAMCommand *writeData = new DRAMCommand(*poppedCMD);
			writeData->commandType = WRITE_DATA;
			writeDataToSend.push_back(writeData);
			writeDataCountdown.push_back(WL);
		}
		
		if(poppedCMD->lastCMD == true) {
			if(!poppedCMD->posted) {
				if(poppedCMD->commandType == WRITE || poppedCMD->commandType == WRITE_P) {
					if(!poppedCMD->atomic) {
						pendingDataSize += 1;
						//DEBUG(ALI(18)<<header<<ALI(15)<<*poppedCMD<<"Down) pendingDataSize 1 increased   (current pendingDataSize : "<<pendingDataSize<<")");
					}
				}
				else if(poppedCMD->commandType == READ || poppedCMD->commandType == READ_P) {
					if(!poppedCMD->atomic) {
						pendingDataSize += (poppedCMD->dataSize/16)+1;
						//DEBUG(ALI(18)<<header<<ALI(15)<<*poppedCMD<<"Down) pendingDataSize "<<(poppedCMD->dataSize/16)+1<<" increased   (current pendingDataSize : "<<pendingDataSize<<")");
					}
					else {
						if(poppedCMD->packetCMD == _2ADD8 || poppedCMD->packetCMD == ADD16 || poppedCMD->packetCMD == INC8
						|| poppedCMD->packetCMD == EQ8 || poppedCMD->packetCMD == EQ16 || poppedCMD->packetCMD == BWR) {
							pendingDataSize += 1;
						}
						else {
							pendingDataSize += 2;
						}
					}
				}
			}
		}
		
		//Check for collision on bus
		if(cmdBus != NULL) {
			ERROR(header<<"  == Error - Command bus collision  (cmdBus:"<<*cmdBus<<", poppedCMD:"<<*poppedCMD<<")  (CurrentClock : "<<currentClockCycle<<")");
			exit(0);
		}
		cmdBus = poppedCMD;
		cmdCyclesLeft = tCMD;
		poppedCMD = NULL;
	}
	
	//Atomic command operation (assume that all operations consume one clock cycle)
	if(atomicCMD != NULL) {
		if(atomicOperLeft == 0) {
			//The results are written back to DRAM, overwriting the original memory operands
			if(atomicCMD->packetCMD != EQ16 && atomicCMD->packetCMD != EQ8) {
				DRAMCommand *atmRstCMD = new DRAMCommand(*atomicCMD);
				atmRstCMD->commandType = (OPEN_PAGE ? WRITE : WRITE_P);
				atmRstCMD->dataSize = 16;
				atmRstCMD->lastCMD = true;
				if(!atmRstCMD->posted) {
					atmRstCMD->trace = NULL;
				}
				if(QUE_PER_BANK) {
					commandQueue->queue[atomicCMD->bank].insert(commandQueue->queue[atomicCMD->bank].begin(), atmRstCMD);
					classID.str( string() );	classID.clear();
					classID << atomicCMD->bank;
					DE_CR(ALI(18)<<(commandQueue->header+"-"+classID.str()+")")<<ALI(15)<<*atmRstCMD<<"Down) PUSHING atomic result command into command queue");
				}
				else {
					commandQueue->queue[0].insert(commandQueue->queue[0].begin(), atmRstCMD);
					DE_CR(ALI(18)<<(commandQueue->header+")")<<ALI(15)<<*atmRstCMD<<"Down) PUSHING atomic result command into command queue");
				}
			}
			
			if(!atomicCMD->posted) {
				MakeRespondPacket(atomicCMD);
			}
			delete atomicCMD;
			atomicCMD = NULL;
		}
		else {
			DEBUG(ALI(18)<<(commandQueue->header+")")<<ALI(15)<<*atomicCMD<<"Down) Atomic command is now operating ["<<atomicOperLeft<<" clk]");
			atomicOperLeft--;
		}
	}

	//Power-down mode setting
	EnablePowerdown();

	commandQueue->Update();
	Step();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
callq 0x227dc
cmpl $0x0, 0x200(%rbx)
je 0x22160
movq 0x1e8(%rbx), %rax
cmpq %rax, 0x1f0(%rbx)
je 0x22093
movq (%rax), %rsi
testq %rsi, %rsi
je 0x22647
movq 0x250(%rbx), %rdi
callq 0x23512
testb %al, %al
je 0x22093
leaq 0x1e8(%rbx), %r14
leaq 0xf48e(%rip), %rax      # 0x31488
cmpb $0x1, (%rax)
jne 0x2207b
leaq 0xf483(%rip), %rax      # 0x31489
cmpb $0x0, (%rax)
jne 0x2207b
movq 0xefbe(%rip), %rdi      # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x12, 0x10(%rdi,%rax)
pushq $0x20
popq %rbp
movl %ebp, %esi
callq 0x4440
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x42b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xf, 0x10(%rax,%rcx)
movq %rax, %rdi
movl %ebp, %esi
callq 0x4440
movq 0x1e8(%rbx), %rcx
movq (%rcx), %rsi
movq %rax, %rdi
callq 0x1e2c8
leaq 0x5daf(%rip), %rsi      # 0x27e1a
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
movq (%r14), %rsi
movq (%rsi), %rax
movl 0x430(%rax), %eax
leaq (%rsi,%rax,8), %rdx
movq %r14, %rdi
callq 0x132b4
movq 0x218(%rbx), %rdi
leaq 0x220(%rbx), %r14
movq %r14, %rsi
callq 0xf50a
testb %al, %al
je 0x22248
movq (%r14), %r14
movl 0x10(%r14), %eax
addl $-0x3, %eax
cmpl $0x1, %eax
ja 0x2211e
pushq $0x38
popq %rdi
callq 0x4310
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x15228
leaq 0x8(%rsp), %rsi
movq %r15, (%rsi)
movl $0x6, 0x10(%r15)
leaq 0x278(%rbx), %rdi
callq 0x1118a
leaq 0x290(%rbx), %rdi
leaq 0xf4b0(%rip), %rax      # 0x315b0
leaq 0xf4a1(%rip), %rcx      # 0x315a8
movl (%rcx), %ecx
addl (%rax), %ecx
leaq 0x28(%rsp), %rsi
movl %ecx, (%rsi)
callq 0xe3de
movq 0x220(%rbx), %r14
cmpb $0x1, 0x29(%r14)
jne 0x2221c
cmpb $0x0, 0x28(%r14)
jne 0x2221c
movl 0x10(%r14), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jb 0x221cf
addl $-0x3, %eax
cmpl $0x1, %eax
ja 0x2221c
cmpb $0x0, 0x30(%r14)
je 0x22216
jmp 0x2221c
leaq 0x1d0(%rbx), %r14
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x1d0(%rbx), %rax
movq 0x1d8(%rbx), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r12, %rcx
jbe 0x21fb8
movq (%rax,%r12,8), %rsi
testq %rsi, %rsi
je 0x221c6
movq %rbx, %rdi
callq 0x22c10
testb %al, %al
je 0x221c6
movq (%r14), %rax
movq (%rax,%r12,8), %rdi
movslq 0x430(%rdi), %r13
movq (%rdi), %rax
callq *0x8(%rax)
movq (%r14), %rsi
addq %r15, %rsi
leaq (%rsi,%r13,8), %rdx
movq %r14, %rdi
callq 0x132b4
incq %r12
addq $0x8, %r15
jmp 0x2216d
cmpb $0x0, 0x30(%r14)
je 0x221f3
movl 0x2c(%r14), %eax
leal -0x50(%rax), %ecx
cmpl $0x19, %ecx
jbe 0x2220c
addl $-0x11, %eax
cmpl $0x3, %eax
jb 0x22216
addl $0x2, 0x234(%rbx)
jmp 0x2221c
movl 0x24(%r14), %eax
shrl $0x4, %eax
movl 0x234(%rbx), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x234(%rbx)
jmp 0x2221c
movl $0x3000001, %edx        # imm = 0x3000001
btl %ecx, %edx
jae 0x221e2
incl 0x234(%rbx)
cmpq $0x0, 0x258(%rbx)
jne 0x226ac
movq %r14, 0x258(%rbx)
leaq 0xf3b4(%rip), %rax      # 0x315ec
movl (%rax), %eax
movl %eax, 0x260(%rbx)
andq $0x0, 0x220(%rbx)
movq 0x228(%rbx), %r14
testq %r14, %r14
je 0x22618
movl 0x230(%rbx), %eax
testl %eax, %eax
je 0x2234d
leaq 0xf21b(%rip), %rcx      # 0x31488
cmpb $0x1, (%rcx)
jne 0x22340
leaq 0xf20c(%rip), %rcx      # 0x31489
cmpb $0x0, (%rcx)
jne 0x22340
movq 0xed43(%rip), %rdi      # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x12, 0x10(%rdi,%rax)
pushq $0x20
popq %rsi
callq 0x4440
movq %rax, %r14
movq 0x218(%rbx), %rsi
addq $0x20, %rsi
leaq 0x5217(%rip), %rdx      # 0x274d1
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0xdeb2
movq %r14, %rdi
movq %r15, %rsi
callq 0x42b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xf, 0x10(%rax,%rcx)
pushq $0x20
popq %rsi
movq %rax, %rdi
callq 0x4440
movq 0x228(%rbx), %rsi
movq %rax, %rdi
callq 0x15274
leaq 0x5bb5(%rip), %rsi      # 0x27eb8
movq %rax, %rdi
callq 0x4300
movl 0x230(%rbx), %esi
movq %rax, %rdi
callq 0x4210
leaq 0x3cf6(%rip), %rsi      # 0x26016
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
leaq 0x8(%rsp), %rdi
callq 0x4598
movl 0x230(%rbx), %eax
decl %eax
movl %eax, 0x230(%rbx)
jmp 0x22618
movl 0x2c(%r14), %eax
andl $-0x2, %eax
cmpl $0x68, %eax
je 0x225e9
pushq $0x38
popq %rdi
callq 0x4310
movq %rax, %r15
movq %rax, %rdi
movq %r14, %rsi
callq 0x15228
movq %r15, (%rsp)
leaq 0xf213(%rip), %rax      # 0x31591
movzbl (%rax), %eax
pushq $0x4
popq %rcx
subl %eax, %ecx
movl %ecx, 0x10(%r15)
movl $0x10, 0x24(%r15)
movb $0x1, 0x29(%r15)
cmpb $0x0, 0x28(%r15)
jne 0x223a3
andq $0x0, 0x8(%r15)
leaq 0xf1e6(%rip), %rcx      # 0x31590
movq 0x218(%rbx), %rax
cmpb $0x1, (%rcx)
jne 0x2252c
movq 0x228(%rbx), %rcx
movl 0x18(%rcx), %ecx
movq 0x238(%rax), %rax
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %rdi
movq (%rax,%rcx), %rsi
movq %rsp, %rdx
callq 0x235ba
leaq 0x18(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x58(%rbx), %r14
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
leaq 0x40(%rbx), %r15
leaq 0x8(%rsp), %rdi
callq 0x4598
movq 0x40(%rbx), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x44d0
leaq 0x50(%rbx), %rdi
movq 0x228(%rbx), %rax
movl 0x18(%rax), %esi
callq 0x4210
leaq 0xf04d(%rip), %rax      # 0x31488
cmpb $0x1, (%rax)
jne 0x225e2
movq 0xeb85(%rip), %rdi      # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x12, 0x10(%rdi,%rax)
pushq $0x20
popq %rsi
callq 0x4440
movq %rax, %r15
movq 0x218(%rbx), %rsi
addq $0x20, %rsi
leaq 0x24ff(%rip), %rdx      # 0x24977
leaq 0x68(%rsp), %rdi
callq 0xdeb2
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x4450
leaq 0x28(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x11123
leaq 0x5027(%rip), %rdx      # 0x274d1
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xdeea
leaq 0x8(%rsp), %rsi
movq %r15, %rdi
callq 0x42b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xf, 0x10(%rax,%rcx)
pushq $0x20
popq %rsi
movq %rax, %rdi
callq 0x4440
movq (%rsp), %rsi
movq %rax, %rdi
callq 0x15274
leaq 0x598d(%rip), %rsi      # 0x27e81
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
leaq 0x8(%rsp), %rdi
callq 0x4598
leaq 0x28(%rsp), %rdi
callq 0x4598
leaq 0x48(%rsp), %rdi
callq 0x4598
leaq 0x68(%rsp), %rdi
jmp 0x225dd
movq 0x238(%rax), %rdi
movq (%rdi), %rsi
movq %rsp, %rdx
callq 0x235ba
leaq 0xef43(%rip), %rax      # 0x31488
cmpb $0x1, (%rax)
jne 0x225e2
movq 0xea7b(%rip), %rdi      # 0x30fd0
movq (%rdi), %rax
movq -0x18(%rax), %rax
movq $0x12, 0x10(%rdi,%rax)
pushq $0x20
popq %rsi
callq 0x4440
movq %rax, %r14
movq 0x218(%rbx), %rsi
addq $0x20, %rsi
leaq 0x4f4f(%rip), %rdx      # 0x274d1
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0xdeb2
movq %r14, %rdi
movq %r15, %rsi
callq 0x42b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xf, 0x10(%rax,%rcx)
pushq $0x20
popq %rsi
movq %rax, %rdi
callq 0x4440
movq (%rsp), %rsi
movq %rax, %rdi
callq 0x15274
leaq 0x58b9(%rip), %rsi      # 0x27e81
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
leaq 0x8(%rsp), %rdi
callq 0x4598
movq 0x228(%rbx), %r14
cmpb $0x0, 0x28(%r14)
jne 0x22607
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21cce
movq 0x228(%rbx), %r14
testq %r14, %r14
je 0x22610
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
andq $0x0, 0x228(%rbx)
movq %rbx, %rdi
callq 0x23142
movq 0x218(%rbx), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq %rbx, %rdi
callq 0x13508
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe9a2(%rip), %rdi      # 0x30ff0
leaq 0x19cd(%rip), %rsi      # 0x24022
callq 0x4300
leaq 0x559f(%rip), %rsi      # 0x27c00
movq %rax, %rdi
callq 0x4300
leaq 0x2207(%rip), %rsi      # 0x24877
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movl $0xbd, %esi
callq 0x44f0
leaq 0x19b5(%rip), %rsi      # 0x24041
movq %rax, %rdi
callq 0x4300
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x42b0
leaq 0x56ee(%rip), %rsi      # 0x27d95
jmp 0x22748
movq 0xe93d(%rip), %rdi      # 0x30ff0
leaq 0x1968(%rip), %rsi      # 0x24022
callq 0x4300
leaq 0x553a(%rip), %rsi      # 0x27c00
movq %rax, %rdi
callq 0x4300
leaq 0x21a2(%rip), %rsi      # 0x24877
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
movl $0xf1, %esi
callq 0x44f0
leaq 0x1950(%rip), %rsi      # 0x24041
movq %rax, %rdi
callq 0x4300
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
callq 0x42b0
leaq 0x573b(%rip), %rsi      # 0x27e47
movq %rax, %rdi
callq 0x4300
movq 0x258(%rbx), %rsi
movq %rax, %rdi
callq 0x15274
leaq 0x574a(%rip), %rsi      # 0x27e74
movq %rax, %rdi
callq 0x4300
movq 0x220(%rbx), %rsi
movq %rax, %rdi
callq 0x15274
leaq 0x55b2(%rip), %rsi      # 0x27cfa
movq %rax, %rdi
callq 0x4300
movq 0x8(%rbx), %rsi
movq %rax, %rdi
callq 0x40d0
leaq 0x4d6e(%rip), %rsi      # 0x274d1
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
callq 0x4180
xorl %edi, %edi
callq 0x4370
movq %rax, %rbx
jmp 0x2279a
movq %rax, %rbx
jmp 0x227a4
movq %rax, %rbx
jmp 0x227ae
jmp 0x227c7
jmp 0x227b7
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4598
leaq 0x28(%rsp), %rdi
callq 0x4598
leaq 0x48(%rsp), %rdi
callq 0x4598
leaq 0x68(%rsp), %rdi
jmp 0x227cf
jmp 0x227c7
movq %rax, %rbx
pushq $0x38
popq %rsi
movq %r15, %rdi
callq 0x4320
jmp 0x227d4
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x4598
movq %rbx, %rdi
callq 0x4520