// File: estwings57[P]CasHMC/sources/VaultController.cpp
// Function: CasHMC::VaultController::UpdateCountdown()
void VaultController::UpdateCountdown()
{
	//Check for outgoing command and handle countdowns
	if(cmdBus != NULL) {
		cmdCyclesLeft--;
		if(cmdCyclesLeft == 0) {	//command is ready to be transmitted
			DE_CR(ALI(18)<<header<<ALI(15)<<*cmdBus<<"Down) ISSUING command to DRAM");
			if(cmdBus->trace != NULL && cmdBus->trace->vaultIssueTime == 0) {
				cmdBus->trace->vaultIssueTime = currentClockCycle;
			}
			dramP->receiveCMD(cmdBus);
			cmdBus = NULL;
		}
	}
	//Check for outgoing write data packets and handle countdowns
	if(dataBus != NULL) {
		dataCyclesLeft--;
		if(dataCyclesLeft == 0) {
			DE_CR(ALI(18)<<header<<ALI(15)<<*dataBus<<"Down) ISSUING data corresponding to previous write command");
			if(dataBus->lastCMD) {
				if(!dataBus->atomic && !dataBus->posted) {
					MakeRespondPacket(dataBus);
				}
				else if(dataBus->trace != NULL && dataBus->posted) {
					dataBus->trace->tranFullLat = ceil((double)currentClockCycle * (double)tCK/CPU_CLK_PERIOD) - dataBus->trace->tranTransmitTime;
					dataBus->trace->linkFullLat = ceil((double)currentClockCycle * (double)tCK/CPU_CLK_PERIOD) - dataBus->trace->linkTransmitTime;
					dataBus->trace->vaultFullLat = currentClockCycle - dataBus->trace->vaultIssueTime;
					delete dataBus->trace;
				}
			}
			dramP->receiveCMD(dataBus);
			dataBus = NULL;
		}
	}
	//Check write data to be sent to DRAM and handle countdowns
	if(writeDataCountdown.size() > 0) {
		if(writeDataCountdown[0] == 0) {
			if(dataBus != NULL) {
				ERROR(header<<"   == Error - Data Bus Collision  "<<*dataBus<<"  (CurrentClock : "<<currentClockCycle<<")");
				exit(0);
			}
			dataBus = writeDataToSend[0];
			dataCyclesLeft = BL;	//block size according to address mapping / DATA_WIDTH

			writeDataCountdown.erase(writeDataCountdown.begin());
			writeDataToSend.erase(writeDataToSend.begin());
		}
		
		for(int i=0; i<writeDataCountdown.size(); i++) {
			writeDataCountdown[i]--;
		}
	}
	
	//Time for a refresh issue a refresh
	refreshCountdown--;
	if(refreshCountdown == 0) {
		commandQueue->refreshWaiting = true;
		refreshCountdown = REFRESH_PERIOD/tCK;
		DEBUG(ALI(39)<<header<<"REFRESH countdown is over");
	}
	//If a rank is powered down, make sure we power it up in time for a refresh
	else if(powerDown && refreshCountdown<=tXP)	{
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, 0x258(%rax)
je 0x4e286
movq -0x70(%rbp), %rax
movl 0x260(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x260(%rax)
cmpl $0x0, 0x260(%rax)
jne 0x4e284
leaq 0x183cc(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x4e221
movl $0x12, %edi
callq 0x177b0
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %esi
movq 0x17e54(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0x88(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0x88(%rbp), %rdi
movb %al, -0xd(%rbp)
movzbl -0xd(%rbp), %esi
callq 0x4550
movq -0x70(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0x80(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0x80(%rbp), %rdi
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %esi
callq 0x4440
movq %rax, -0x78(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0x78(%rbp), %rdi
movb %al, -0x15(%rbp)
movzbl -0x15(%rbp), %esi
callq 0x4550
movq %rax, %rdi
movq -0x70(%rbp), %rax
movq 0x258(%rax), %rsi
callq 0x30f50
movq %rax, %rdi
leaq 0x6b83(%rip), %rsi      # 0x54d90
callq 0x4380
movq %rax, %rdi
movq 0x17d94(%rip), %rsi     # 0x65fb0
callq 0x43c0
movq -0x70(%rbp), %rax
movq 0x258(%rax), %rax
cmpq $0x0, 0x8(%rax)
je 0x4e25e
movq -0x70(%rbp), %rax
movq 0x258(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x0, 0x18(%rax)
jne 0x4e25e
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rcx
movq 0x258(%rax), %rax
movq 0x8(%rax), %rax
movl %ecx, 0x18(%rax)
movq -0x70(%rbp), %rax
movq 0x210(%rax), %rdi
movq 0x258(%rax), %rsi
callq 0x2d120
movq -0x70(%rbp), %rax
movq $0x0, 0x258(%rax)
jmp 0x4e286
movq -0x70(%rbp), %rax
cmpq $0x0, 0x268(%rax)
je 0x4e595
movq -0x70(%rbp), %rax
movl 0x270(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x270(%rax)
cmpl $0x0, 0x270(%rax)
jne 0x4e593
leaq 0x18269(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x4e390
movl $0x12, %edi
callq 0x177b0
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %esi
movq 0x17cf1(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0xa0(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0xa0(%rbp), %rdi
movb %al, -0x1d(%rbp)
movzbl -0x1d(%rbp), %esi
callq 0x4550
movq -0x70(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, -0x98(%rbp)
movl $0xf, %edi
callq 0x177b0
movq -0x98(%rbp), %rdi
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %esi
callq 0x4440
movq %rax, -0x90(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0x90(%rbp), %rdi
movb %al, -0x25(%rbp)
movzbl -0x25(%rbp), %esi
callq 0x4550
movq %rax, %rdi
movq -0x70(%rbp), %rax
movq 0x268(%rax), %rsi
callq 0x30f50
movq %rax, %rdi
leaq 0x6a32(%rip), %rsi      # 0x54dae
callq 0x4380
movq %rax, %rdi
movq 0x17c25(%rip), %rsi     # 0x65fb0
callq 0x43c0
movq -0x70(%rbp), %rax
movq 0x268(%rax), %rax
testb $0x1, 0x29(%rax)
je 0x4e56d
movq -0x70(%rbp), %rax
movq 0x268(%rax), %rax
testb $0x1, 0x30(%rax)
jne 0x4e3dc
movq -0x70(%rbp), %rax
movq 0x268(%rax), %rax
testb $0x1, 0x28(%rax)
jne 0x4e3dc
movq -0x70(%rbp), %rdi
movq 0x268(%rdi), %rsi
callq 0x4ca00
jmp 0x4e56b
movq -0x70(%rbp), %rax
movq 0x268(%rax), %rax
cmpq $0x0, 0x8(%rax)
je 0x4e569
movq -0x70(%rbp), %rax
movq 0x268(%rax), %rax
testb $0x1, 0x28(%rax)
je 0x4e569
movq -0x70(%rbp), %rax
movq 0x8(%rax), %xmm0
movaps 0x2d09(%rip), %xmm1     # 0x51120
movaps %xmm1, -0xe0(%rbp)
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x2d06(%rip), %xmm1     # 0x51130
movaps %xmm1, -0xd0(%rbp)
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
leaq 0x181f9(%rip), %rax     # 0x66640
movq %rax, -0xb8(%rbp)
movsd (%rax), %xmm1
mulsd %xmm1, %xmm0
leaq 0x180db(%rip), %rax     # 0x66538
movq %rax, -0xb0(%rbp)
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x40a0
movaps -0xe0(%rbp), %xmm2
movaps -0xd0(%rbp), %xmm1
movq -0xb8(%rbp), %rcx
movq -0xb0(%rbp), %rax
movq -0x70(%rbp), %rdx
movq 0x268(%rdx), %rsi
movq 0x8(%rsi), %rsi
movl 0x8(%rsi), %edi
cvtsi2sd %rdi, %xmm3
subsd %xmm3, %xmm0
cvttsd2si %xmm0, %rdi
movl %edi, 0xc(%rsi)
movq 0x8(%rdx), %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd (%rcx), %xmm1
mulsd %xmm1, %xmm0
movsd (%rax), %xmm1
divsd %xmm1, %xmm0
callq 0x40a0
movq -0x70(%rbp), %rax
movq 0x268(%rax), %rcx
movq 0x8(%rcx), %rcx
movl 0x10(%rcx), %ecx
cvtsi2sd %rcx, %xmm1
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movl %ecx, %edx
movq 0x268(%rax), %rcx
movq 0x8(%rcx), %rcx
movl %edx, 0x14(%rcx)
movq 0x8(%rax), %rcx
movq 0x268(%rax), %rdx
movq 0x8(%rdx), %rdx
movl 0x18(%rdx), %edx
subq %rdx, %rcx
movl %ecx, %edx
movq 0x268(%rax), %rcx
movq 0x8(%rcx), %rcx
movl %edx, 0x1c(%rcx)
movq 0x268(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x4e567
movq -0xa8(%rbp), %rdi
callq 0x17b80
movq -0xa8(%rbp), %rdi
movl $0x20, %esi
callq 0x43a0
jmp 0x4e569
jmp 0x4e56b
jmp 0x4e56d
movq -0x70(%rbp), %rax
movq 0x210(%rax), %rdi
movq 0x268(%rax), %rsi
callq 0x2d120
movq -0x70(%rbp), %rax
movq $0x0, 0x268(%rax)
jmp 0x4e595
movq -0x70(%rbp), %rdi
addq $0x290, %rdi            # imm = 0x290
callq 0x17eb0
cmpq $0x0, %rax
jbe 0x4e7b4
movq -0x70(%rbp), %rdi
addq $0x290, %rdi            # imm = 0x290
xorl %eax, %eax
movl %eax, %esi
callq 0x17e90
cmpl $0x0, (%rax)
jne 0x4e75b
movq -0x70(%rbp), %rax
cmpq $0x0, 0x268(%rax)
je 0x4e6a1
movq 0x17a0b(%rip), %rdi     # 0x65ff0
leaq 0x2a4e(%rip), %rsi      # 0x5103a
callq 0x4380
movq %rax, %rdi
leaq 0x64b5(%rip), %rsi      # 0x54ab0
callq 0x4380
movq %rax, %rdi
leaq 0x322b(%rip), %rsi      # 0x51835
callq 0x4380
movq %rax, %rdi
movl $0x14d, %esi            # imm = 0x14D
callq 0x4610
movq %rax, %rdi
leaq 0x2a33(%rip), %rsi      # 0x51059
callq 0x4380
movq -0x70(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, %rdi
leaq 0x67a4(%rip), %rsi      # 0x54de9
callq 0x4380
movq %rax, %rdi
movq -0x70(%rbp), %rax
movq 0x268(%rax), %rsi
callq 0x30f50
movq %rax, %rdi
leaq 0x6544(%rip), %rsi      # 0x54bab
callq 0x4380
movq %rax, %rdi
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x40f0
movq %rax, %rdi
leaq 0x5cf3(%rip), %rsi      # 0x54379
callq 0x4380
movq %rax, %rdi
movq 0x1791b(%rip), %rsi     # 0x65fb0
callq 0x43c0
xorl %edi, %edi
callq 0x4460
movq -0x70(%rbp), %rdi
addq $0x278, %rdi            # imm = 0x278
xorl %eax, %eax
movl %eax, %esi
callq 0x22ae0
movq -0x70(%rbp), %rdi
movq (%rax), %rax
movq %rax, 0x268(%rdi)
leaq 0x17f62(%rip), %rax     # 0x6662c
movl (%rax), %eax
shrl $0x5, %eax
movl %eax, 0x270(%rdi)
movq %rdi, %rax
addq $0x290, %rax            # imm = 0x290
movq %rax, -0xf0(%rbp)
addq $0x290, %rdi            # imm = 0x290
callq 0x19bc0
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x22aa0
movq -0xf0(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x22a30
movq -0x70(%rbp), %rdi
movq %rax, -0x40(%rbp)
movq %rdi, %rax
addq $0x278, %rax            # imm = 0x278
movq %rax, -0xe8(%rbp)
addq $0x278, %rdi            # imm = 0x278
callq 0x22bc0
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x22bf0
movq -0xe8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x22b50
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x70(%rbp), %rdi
movslq -0x5c(%rbp), %rax
movq %rax, -0xf8(%rbp)
addq $0x290, %rdi            # imm = 0x290
callq 0x17eb0
movq %rax, %rcx
movq -0xf8(%rbp), %rax
cmpq %rcx, %rax
jae 0x4e7b2
movq -0x70(%rbp), %rdi
addq $0x290, %rdi            # imm = 0x290
movslq -0x5c(%rbp), %rsi
callq 0x17e90
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x4e762
jmp 0x4e7b4
movq -0x70(%rbp), %rax
movl 0x208(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x208(%rax)
cmpl $0x0, 0x208(%rax)
jne 0x4e893
movq -0x70(%rbp), %rax
movq 0x218(%rax), %rcx
movb $0x1, 0x1d0(%rcx)
leaq 0x17e4f(%rip), %rcx     # 0x6663c
cvtsi2sdl (%rcx), %xmm0
leaq 0x17e48(%rip), %rcx     # 0x66640
movsd (%rcx), %xmm1
divsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movl %ecx, 0x208(%rax)
leaq 0x17d16(%rip), %rax     # 0x66528
testb $0x1, (%rax)
je 0x4e891
leaq 0x17d0b(%rip), %rax     # 0x66529
testb $0x1, (%rax)
jne 0x4e891
movl $0x27, %edi
callq 0x177b0
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %esi
movq 0x17796(%rip), %rdi     # 0x65fd0
callq 0x4440
movq %rax, -0x100(%rbp)
movl $0x20, %edi
callq 0x177d0
movq -0x100(%rbp), %rdi
movb %al, -0x61(%rbp)
movzbl -0x61(%rbp), %esi
callq 0x4550
movq -0x70(%rbp), %rsi
movq %rax, %rdi
addq $0x20, %rsi
callq 0x4320
movq %rax, %rdi
leaq 0x658f(%rip), %rsi      # 0x54e0c
callq 0x4380
movq %rax, %rdi
movq 0x17724(%rip), %rsi     # 0x65fb0
callq 0x43c0
jmp 0x4e8b9
movq -0x70(%rbp), %rax
testb $0x1, 0x20c(%rax)
je 0x4e8b7
movq -0x70(%rbp), %rax
movl 0x208(%rax), %eax
leaq 0x17dd7(%rip), %rcx     # 0x66688
cmpl (%rcx), %eax
ja 0x4e8b7
jmp 0x4e8b7
jmp 0x4e8b9
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)