// File: /workspace/github_cpp_makefile_star2_O1/dramninjasUMD[P]BOBSim/BOBWrapper.cpp
// Function: BOBSim::BOBWrapper::Update()
void BOBWrapper::Update()
{
	maxReadsPerCycle = max<uint64_t>(maxReadsPerCycle, readsPerCycle);
	maxWritesPerCycle = max<uint64_t>(maxWritesPerCycle, writesPerCycle);
	readsPerCycle = writesPerCycle = 0;

	bob->Update();

	//BOOK-KEEPING
	for(unsigned i=0; i<NUM_PORTS; i++)
	{
		//STATS
		if(inFlightResponse[i]==NULL)
		{
			responsePortEmptyCount[i]++;
		}

		if(inFlightRequest[i]==NULL)
		{
			requestPortEmptyCount[i]++;
		}

		//
		//Requests
		//
		if(inFlightRequestHeaderCounter[i]>0)
		{
			inFlightRequestHeaderCounter[i]--;
			//done
			if(inFlightRequestHeaderCounter[i]==0)
			{
				if(inFlightRequest[i]!=NULL)
				{
					if(DEBUG_PORTS)DEBUG("== Header done - Adding "<<*inFlightRequest[i]<<" to port "<<i);
					bob->ports[i].inputBuffer.push_back(inFlightRequest[i]);
				}
				else
				{
					ERROR("== Error - inFlightResponse null when not supposed to be");
					exit(0);
				}
			}
		}

		if(inFlightRequestCounter[i]>0)
		{
			inFlightRequestCounter[i]--;
			if(inFlightRequestCounter[i]==0)
			{
				inFlightRequest[i]=NULL;
			}
		}

		//
		//Responses
		//
		if(inFlightResponseHeaderCounter[i]>0)
		{
			//does nothing
			inFlightResponseHeaderCounter[i]--;
		}

		if(inFlightResponseCounter[i]>0)
		{
			inFlightResponseCounter[i]--;
			if(inFlightResponseCounter[i]==0)
			{
				if(inFlightResponse[i]->transactionType==RETURN_DATA)
				{
					if (readDoneCallback)
					{
						(*readDoneCallback)(i, inFlightResponse[i]->address);
					}

					UpdateLatencyStats(inFlightResponse[i]);

					returnsPerPort[i]++;
				}
				else if(inFlightResponse[i]->transactionType==LOGIC_RESPONSE)
				{
					if(logicDoneCallback)
					{
						(*logicDoneCallback)(inFlightResponse[i]->mappedChannel, (void*)inFlightResponse[i]->address);
					}

					UpdateLatencyStats(inFlightResponse[i]);

					totalLogicResponses++;
				}
				else
				{
					ERROR("== ERROR - unknown packet type coming down : "<<*inFlightResponse[i]);
					exit(0);
				}

				bob->ports[i].outputBuffer.erase(bob->ports[i].outputBuffer.begin());
				delete inFlightResponse[i];
				inFlightResponse[i]=NULL;
			}
		}
	}

	//NEW STUFF
	for(unsigned i=0; i<NUM_PORTS; i++)
	{
		//look for new stuff to return from bob controller
		if(bob->ports[i].outputBuffer.size()>0 &&
		        inFlightResponseCounter[i]==0)
		{
			inFlightResponse[i] = bob->ports[i].outputBuffer[0];
			inFlightResponseHeaderCounter[i] = 1;

			if(inFlightResponse[i]->transactionType==RETURN_DATA)
			{
				inFlightResponseCounter[i] = TRANSACTION_SIZE / PORT_WIDTH;
			}
			else if(inFlightResponse[i]->transactionType==LOGIC_RESPONSE)
			{
				inFlightResponseCounter[i] = 1;
			}
			else
			{
				ERROR("== Error - wrong type of transaction out of");
				ERROR("== "<<*inFlightResponse[i]);
				exit(0);
			}
		}
	}

	if(currentClockCycle%EPOCH_LENGTH==0 && currentClockCycle>0)
	{
		PrintStats(false);
	}
	currentClockCycle++;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0xa8(%rdi), %rax
movq 0xb8(%rdi), %rcx
cmpq %rcx, %rax
cmovaq %rax, %rcx
movq 0x10(%rdi), %rdi
movq %rcx, 0xa8(%rbx)
movq 0xb0(%rbx), %rax
movq 0xc0(%rbx), %rcx
cmpq %rcx, %rax
cmovaq %rax, %rcx
movq %rcx, 0xb0(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0xb8(%rbx)
movq (%rdi), %rax
callq *(%rax)
leaq 0xedd2(%rip), %r15      # 0x191d0
cmpl $0x0, (%r15)
je 0xa5ba
movl $0x10, %r12d
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x60(%rbx), %rax
cmpq $0x0, (%rax,%r14,8)
jne 0xa42a
movq 0xe0(%rbx), %rax
incl (%rax,%r14,4)
movq 0x18(%rbx), %rdx
movq (%rdx,%r14,8), %rax
testq %rax, %rax
jne 0xa442
movq 0xc8(%rbx), %rcx
incl (%rcx,%r14,4)
movq 0x48(%rbx), %rcx
movl (%rcx,%r14,4), %esi
testl %esi, %esi
je 0xa489
decl %esi
movl %esi, (%rcx,%r14,4)
jne 0xa489
testq %rax, %rax
je 0xa67f
movq 0x10(%rbx), %rcx
movq 0x10(%rcx), %rdi
movq 0x18(%rdi,%r13,8), %rsi
cmpq 0x20(%rdi,%r13,8), %rsi
je 0xa47e
movq %rax, (%rsi)
addq $0x8, 0x18(%rdi,%r13,8)
jmp 0xa489
addq %r13, %rdx
addq %r12, %rdi
callq 0x7e56
movq 0x30(%rbx), %rax
movl (%rax,%r14,4), %ecx
testl %ecx, %ecx
je 0xa4a9
decl %ecx
movl %ecx, (%rax,%r14,4)
jne 0xa4a9
movq 0x18(%rbx), %rax
movq $0x0, (%rax,%r14,8)
movq 0x90(%rbx), %rax
movl (%rax,%r14,4), %ecx
testl %ecx, %ecx
je 0xa4be
decl %ecx
movl %ecx, (%rax,%r14,4)
movq 0x78(%rbx), %rax
movl (%rax,%r14,4), %ecx
testl %ecx, %ecx
je 0xa5a3
decl %ecx
movl %ecx, (%rax,%r14,4)
jne 0xa5a3
movq 0x60(%rbx), %rax
movq (%rax,%r14,8), %rax
movl (%rax), %ecx
cmpl $0x4, %ecx
je 0xa528
cmpl $0x2, %ecx
jne 0xa796
movq 0x158(%rbx), %rdi
testq %rdi, %rdi
je 0xa50b
movq 0x8(%rax), %rdx
movq (%rdi), %rax
movl %r14d, %esi
callq *0x10(%rax)
movq 0x60(%rbx), %rax
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
callq 0xa812
movq 0x140(%rbx), %rax
incl (%rax,%r14,4)
jmp 0xa557
movq 0x168(%rbx), %rdi
testq %rdi, %rdi
je 0xa541
movl 0x10(%rax), %esi
movq 0x8(%rax), %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x60(%rbx), %rax
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
callq 0xa812
incl 0x2a8(%rbx)
movq 0x10(%rbx), %rax
movq 0x10(%rax), %rbp
movq 0x28(%rbp,%r13,8), %rdi
movq 0x30(%rbp,%r13,8), %rdx
leaq 0x8(%rdi), %rsi
cmpq %rdx, %rsi
je 0xa57a
subq %rsi, %rdx
callq 0x32f0
addq $-0x8, 0x30(%rbp,%r13,8)
movq 0x60(%rbx), %rax
movq (%rax,%r14,8), %rdi
testq %rdi, %rdi
je 0xa597
movl $0x90, %esi
callq 0x31f0
movq 0x60(%rbx), %rax
movq $0x0, (%rax,%r14,8)
incq %r14
movl (%r15), %eax
addq $0x8, %r13
addq $0x40, %r12
cmpq %rax, %r14
jb 0xa414
cmpl $0x0, (%r15)
je 0xa639
movl $0xc, %eax
xorl %r14d, %r14d
movq 0x10(%rbx), %rcx
movq 0x10(%rcx), %rdx
movq -0x8(%rdx,%rax,4), %rcx
cmpq %rcx, (%rdx,%rax,4)
je 0xa62a
movq 0x78(%rbx), %rdx
cmpl $0x0, (%rdx,%r14,4)
jne 0xa62a
movq (%rcx), %rcx
movq 0x60(%rbx), %rdx
movq %rcx, (%rdx,%r14,8)
movq 0x90(%rbx), %rcx
movl $0x1, (%rcx,%r14,4)
movq 0x60(%rbx), %rcx
movq (%rcx,%r14,8), %rcx
movl (%rcx), %edx
movl $0x4, %ecx
cmpl $0x2, %edx
je 0xa622
cmpl $0x4, %edx
jne 0xa6e0
movl $0x1, %ecx
movq 0x78(%rbx), %rdx
movl %ecx, (%rdx,%r14,4)
incq %r14
movl (%r15), %ecx
addq $0x10, %rax
cmpq %rcx, %r14
jb 0xa5c8
movq 0x8(%rbx), %rcx
movabsq $0x431bde82d7b634db, %rdx # imm = 0x431BDE82D7B634DB
movq %rcx, %rax
mulq %rdx
testq %rcx, %rcx
je 0xa66c
shrq $0x12, %rdx
imulq $0xf4240, %rdx, %rax    # imm = 0xF4240
subq %rax, %rcx
jne 0xa66c
movq %rbx, %rdi
xorl %esi, %esi
callq 0xac06
incq 0x8(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xe96a(%rip), %rdi      # 0x18ff0
leaq 0x8aeb(%rip), %rsi      # 0x13178
callq 0x31c0
leaq 0x9504(%rip), %rsi      # 0x13b9d
movq %rax, %rdi
callq 0x31c0
leaq 0x8a79(%rip), %rsi      # 0x13121
movq %rax, %rdi
callq 0x31c0
movq %rax, %rdi
movl $0x185, %esi            # imm = 0x185
callq 0x3330
leaq 0x8ac5(%rip), %rsi      # 0x13189
movq %rax, %rdi
callq 0x31c0
leaq 0x952c(%rip), %rsi      # 0x13bff
movq %rax, %rdi
callq 0x31c0
jmp 0xa802
movq 0xe909(%rip), %rdi      # 0x18ff0
leaq 0x8a8a(%rip), %r15      # 0x13178
movq %r15, %rsi
callq 0x31c0
leaq 0x94a0(%rip), %r12      # 0x13b9d
movq %rax, %rdi
movq %r12, %rsi
callq 0x31c0
leaq 0x8a12(%rip), %r13      # 0x13121
movq %rax, %rdi
movq %r13, %rsi
callq 0x31c0
movq %rax, %rdi
movl $0x1d9, %esi            # imm = 0x1D9
callq 0x3330
leaq 0x8a5b(%rip), %rbp      # 0x13189
movq %rax, %rdi
movq %rbp, %rsi
callq 0x31c0
leaq 0x9526(%rip), %rsi      # 0x13c66
movq %rax, %rdi
callq 0x31c0
movq %rax, %rdi
callq 0x30d0
movq 0xe899(%rip), %rdi      # 0x18ff0
movq %r15, %rsi
callq 0x31c0
movq %rax, %rdi
movq %r12, %rsi
callq 0x31c0
movq %rax, %rdi
movq %r13, %rsi
callq 0x31c0
movq %rax, %rdi
movl $0x1da, %esi            # imm = 0x1DA
callq 0x3330
movq %rax, %rdi
movq %rbp, %rsi
callq 0x31c0
leaq 0x8d64(%rip), %rsi      # 0x134f8
jmp 0xa7ea
movq 0xe853(%rip), %rdi      # 0x18ff0
leaq 0x89d4(%rip), %rsi      # 0x13178
callq 0x31c0
leaq 0x93ed(%rip), %rsi      # 0x13b9d
movq %rax, %rdi
callq 0x31c0
leaq 0x8962(%rip), %rsi      # 0x13121
movq %rax, %rdi
callq 0x31c0
movq %rax, %rdi
movl $0x1ba, %esi            # imm = 0x1BA
callq 0x3330
leaq 0x89ae(%rip), %rsi      # 0x13189
movq %rax, %rdi
callq 0x31c0
leaq 0x944e(%rip), %rsi      # 0x13c38
movq %rax, %rdi
callq 0x31c0
movq 0x60(%rbx), %rcx
movq (%rcx,%r14,8), %rsi
movq %rax, %rdi
callq 0x1243b
movq %rax, %rdi
callq 0x30d0
xorl %edi, %edi
callq 0x3220
nop