// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rsi
movq %rsi, -0x38(%rbp)
movq %rsi, %rdi
addq $0xa8, %rdi
addq $0xb8, %rsi
callq 0xd310
movq -0x38(%rbp), %rsi
movq (%rax), %rax
movq %rax, 0xa8(%rsi)
movq %rsi, %rdi
addq $0xb0, %rdi
addq $0xc0, %rsi
callq 0xd310
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq (%rcx), %rcx
movq %rcx, 0xb0(%rax)
movq $0x0, 0xc0(%rax)
movq $0x0, 0xb8(%rax)
movq 0x10(%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
movl $0x0, -0xc(%rbp)
movl -0xc(%rbp), %eax
leaq 0x23d9e(%rip), %rcx     # 0x38370
cmpl (%rcx), %eax
jae 0x14b60
movq -0x38(%rbp), %rdi
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
cmpq $0x0, (%rax)
jne 0x1460e
movq -0x38(%rbp), %rdi
addq $0xe0, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rdi
addq $0x18, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
cmpq $0x0, (%rax)
jne 0x14642
movq -0x38(%rbp), %rdi
addq $0xc8, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rdi
addq $0x48, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
cmpl $0x0, (%rax)
jbe 0x147d6
movq -0x38(%rbp), %rdi
addq $0x48, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movq -0x38(%rbp), %rdi
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
addq $0x48, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
cmpl $0x0, (%rax)
jne 0x147d4
movq -0x38(%rbp), %rdi
addq $0x18, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
cmpq $0x0, (%rax)
je 0x14760
testb $0x1, 0x23e85(%rip)     # 0x38539
je 0x14722
leaq 0x23cb7(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x14720
movq 0x238ff(%rip), %rdi     # 0x37fc8
leaq 0x168ed(%rip), %rsi     # 0x2afbd
callq 0x31c0
movq -0x38(%rbp), %rdi
movq %rax, -0x40(%rbp)
addq $0x18, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x40(%rbp), %rdi
movq (%rax), %rsi
callq 0x294b0
movq %rax, %rdi
leaq 0x16857(%rip), %rsi     # 0x2af58
callq 0x31c0
movq %rax, %rdi
movl -0xc(%rbp), %esi
callq 0x32d0
movq %rax, %rdi
movq 0x2388d(%rip), %rsi     # 0x37fa8
callq 0x3200
jmp 0x14722
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x10, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbee0
movq -0x38(%rbp), %rdi
addq $0x10, %rax
movq %rax, -0x48(%rbp)
addq $0x18, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0xbfd0
jmp 0x147d2
movq 0x23889(%rip), %rdi     # 0x37ff0
leaq 0x159a2(%rip), %rsi     # 0x2a110
callq 0x31c0
movq %rax, %rdi
leaq 0x16799(%rip), %rsi     # 0x2af16
callq 0x31c0
movq %rax, %rdi
leaq 0x1592d(%rip), %rsi     # 0x2a0b9
callq 0x31c0
movq %rax, %rdi
movl $0x185, %esi            # imm = 0x185
callq 0x3380
movq %rax, %rdi
leaq 0x15979(%rip), %rsi     # 0x2a121
callq 0x31c0
movq %rax, %rdi
leaq 0x1681f(%rip), %rsi     # 0x2afd6
callq 0x31c0
movq %rax, %rdi
movq 0x237e2(%rip), %rsi     # 0x37fa8
callq 0x3200
xorl %edi, %edi
callq 0x3270
jmp 0x147d4
jmp 0x147d6
movq -0x38(%rbp), %rdi
addq $0x30, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
cmpl $0x0, (%rax)
jbe 0x14838
movq -0x38(%rbp), %rdi
addq $0x30, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movq -0x38(%rbp), %rdi
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
addq $0x30, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
cmpl $0x0, (%rax)
jne 0x14836
movq -0x38(%rbp), %rdi
addq $0x18, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq $0x0, (%rax)
jmp 0x14838
movq -0x38(%rbp), %rdi
addq $0x90, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
cmpl $0x0, (%rax)
jbe 0x1486e
movq -0x38(%rbp), %rdi
addq $0x90, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
cmpl $0x0, (%rax)
jbe 0x14b50
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movq -0x38(%rbp), %rdi
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
addq $0x78, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
cmpl $0x0, (%rax)
jne 0x14b4e
movq -0x38(%rbp), %rdi
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq (%rax), %rax
cmpl $0x2, (%rax)
jne 0x1495f
movq -0x38(%rbp), %rax
cmpq $0x0, 0x158(%rax)
je 0x14920
movq -0x38(%rbp), %rdi
movq 0x158(%rdi), %rax
movq %rax, -0x58(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x4c(%rbp)
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x58(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq (%rax), %rax
movq 0x8(%rax), %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movq -0x38(%rbp), %rdi
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x38(%rbp), %rdi
movq (%rax), %rsi
callq 0x14e10
movq -0x38(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x14aa3
movq -0x38(%rbp), %rdi
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq (%rax), %rax
cmpl $0x4, (%rax)
jne 0x14a0d
movq -0x38(%rbp), %rax
cmpq $0x0, 0x168(%rax)
je 0x149d7
movq -0x38(%rbp), %rdi
movq 0x168(%rdi), %rax
movq %rax, -0x68(%rbp)
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x38(%rbp), %rdi
movq (%rax), %rax
movl 0x10(%rax), %eax
movl %eax, -0x5c(%rbp)
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x68(%rbp), %rdi
movl -0x5c(%rbp), %esi
movq (%rax), %rax
movq 0x8(%rax), %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movq -0x38(%rbp), %rdi
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x38(%rbp), %rdi
movq (%rax), %rsi
callq 0x14e10
movq -0x38(%rbp), %rax
movl 0x2a8(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x2a8(%rax)
jmp 0x14aa1
movq 0x235dc(%rip), %rdi     # 0x37ff0
leaq 0x156f5(%rip), %rsi     # 0x2a110
callq 0x31c0
movq %rax, %rdi
leaq 0x164ec(%rip), %rsi     # 0x2af16
callq 0x31c0
movq %rax, %rdi
leaq 0x15680(%rip), %rsi     # 0x2a0b9
callq 0x31c0
movq %rax, %rdi
movl $0x1ba, %esi            # imm = 0x1BA
callq 0x3380
movq %rax, %rdi
leaq 0x156cc(%rip), %rsi     # 0x2a121
callq 0x31c0
movq %rax, %rdi
leaq 0x165ab(%rip), %rsi     # 0x2b00f
callq 0x31c0
movq -0x38(%rbp), %rdi
movq %rax, -0x70(%rbp)
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x70(%rbp), %rdi
movq (%rax), %rsi
callq 0x294b0
movq %rax, %rdi
movq 0x23513(%rip), %rsi     # 0x37fa8
callq 0x3200
xorl %edi, %edi
callq 0x3270
jmp 0x14aa3
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x10, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbee0
movq %rax, %rcx
movq -0x38(%rbp), %rax
addq $0x28, %rcx
movq %rcx, -0x80(%rbp)
movq 0x10(%rax), %rdi
addq $0x10, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbee0
movq %rax, %rdi
addq $0x28, %rdi
callq 0xc0b0
movq %rax, -0x20(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0xc120
movq -0x80(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0xc040
movq -0x38(%rbp), %rdi
movq %rax, -0x28(%rbp)
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq (%rax), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, %rax
je 0x14b35
movq -0x78(%rbp), %rdi
movl $0x90, %esi
callq 0x31f0
movq -0x38(%rbp), %rdi
addq $0x60, %rdi
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq $0x0, (%rax)
jmp 0x14b50
jmp 0x14b52
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x145c8
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
leaq 0x237ff(%rip), %rcx     # 0x38370
cmpl (%rcx), %eax
jae 0x14dc4
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x10, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xbee0
movq %rax, %rdi
addq $0x28, %rdi
callq 0xbe00
cmpq $0x0, %rax
jbe 0x14db4
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
cmpl $0x0, (%rax)
jne 0x14db4
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x10, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xbee0
movq %rax, %rdi
addq $0x28, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xbe40
movq -0x38(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x88(%rbp)
addq $0x60, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x88(%rbp), %rcx
movq -0x38(%rbp), %rdi
movq %rcx, (%rax)
addq $0x90, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movq -0x38(%rbp), %rdi
movl $0x1, (%rax)
addq $0x60, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq (%rax), %rax
cmpl $0x2, (%rax)
jne 0x14c74
movq -0x38(%rbp), %rdi
movl 0x23701(%rip), %eax     # 0x3834c
xorl %edx, %edx
divl 0x236fd(%rip)           # 0x38350
movl %eax, -0x8c(%rbp)
addq $0x78, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movl -0x8c(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x14db2
movq -0x38(%rbp), %rdi
addq $0x60, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq (%rax), %rax
cmpl $0x4, (%rax)
jne 0x14cab
movq -0x38(%rbp), %rdi
addq $0x78, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movl $0x1, (%rax)
jmp 0x14db0
movq 0x2333e(%rip), %rdi     # 0x37ff0
leaq 0x15457(%rip), %rsi     # 0x2a110
callq 0x31c0
movq %rax, %rdi
leaq 0x1624e(%rip), %rsi     # 0x2af16
callq 0x31c0
movq %rax, %rdi
leaq 0x153e2(%rip), %rsi     # 0x2a0b9
callq 0x31c0
movq %rax, %rdi
movl $0x1d9, %esi            # imm = 0x1D9
callq 0x3380
movq %rax, %rdi
leaq 0x1542e(%rip), %rsi     # 0x2a121
callq 0x31c0
movq %rax, %rdi
leaq 0x1633b(%rip), %rsi     # 0x2b03d
callq 0x31c0
movq %rax, %rdi
movq 0x23297(%rip), %rsi     # 0x37fa8
callq 0x3200
movq 0x232d3(%rip), %rdi     # 0x37ff0
leaq 0x153ec(%rip), %rsi     # 0x2a110
callq 0x31c0
movq %rax, %rdi
leaq 0x161e3(%rip), %rsi     # 0x2af16
callq 0x31c0
movq %rax, %rdi
leaq 0x15377(%rip), %rsi     # 0x2a0b9
callq 0x31c0
movq %rax, %rdi
movl $0x1da, %esi            # imm = 0x1DA
callq 0x3380
movq %rax, %rdi
leaq 0x153c3(%rip), %rsi     # 0x2a121
callq 0x31c0
movq %rax, %rdi
leaq 0x15af0(%rip), %rsi     # 0x2a85d
callq 0x31c0
movq -0x38(%rbp), %rdi
movq %rax, -0x98(%rbp)
addq $0x60, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x98(%rbp), %rdi
movq (%rax), %rsi
callq 0x294b0
movq %rax, %rdi
movq 0x23204(%rip), %rsi     # 0x37fa8
callq 0x3200
xorl %edi, %edi
callq 0x3270
jmp 0x14db2
jmp 0x14db4
jmp 0x14db6
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x14b67
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x234e6(%rip), %ecx     # 0x382b8
xorl %edx, %edx
divq %rcx
cmpq $0x0, %rdx
jne 0x14df3
movq -0x38(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jbe 0x14df3
movq -0x38(%rbp), %rdi
xorl %esi, %esi
callq 0x151b0
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)