// File: dramninjasUMD[P]BOBSim/DRAMChannel.cpp
// Function: BOBSim::DRAMChannel::Update()
void DRAMChannel::Update()
{
	if(DEBUG_CHANNEL)
	{
		DEBUG("   == DRAM Channel "<<channelID<<" [DRAM cycle "<<currentClockCycle<<"]");

		DEBUG("     == Read Return Queue");
		for(unsigned i=0; i<readReturnQueue.size(); i++)
		{
			DEBUG("     "<<i<<"] "<<*readReturnQueue[i]);
		}
	}

	if(inFlightDataPacket==NULL)
	{
		DRAMBusIdleCount++;
	}

	//update buses
	if(inFlightCommandPacket!=NULL)
	{
		inFlightCommandCountdown--;
		if(inFlightCommandCountdown==0)
		{
			ranks[inFlightCommandPacket->rank].ReceiveFromBus(inFlightCommandPacket);
			inFlightCommandPacket = NULL;
		}
	}

	if(inFlightDataPacket!=NULL)
	{
		inFlightDataCountdown--;
		if(inFlightDataCountdown==0)
		{
			switch(inFlightDataPacket->busPacketType)
			{
			case READ_DATA:
				if(DEBUG_CHANNEL) DEBUG("     == Data burst complete : " << *inFlightDataPacket);

				//if the bus packet was from a request originating from a logic operation, send it back to logic layer
				if(inFlightDataPacket->fromLogicOp)
				{
					(*SendToLogicLayer)(new Transaction(RETURN_DATA, 64, inFlightDataPacket->address),0);
				}
				//if it was a regular request, add to return queue
				else
				{
					readReturnQueue.push_back(inFlightDataPacket);

					simpleController.outstandingReads--;

					(*ReportCallback)(inFlightDataPacket, 0);

					//keep track of total number of entries in return queue
					if(readReturnQueue.size()>readReturnQueueMax)
					{
						readReturnQueueMax = readReturnQueue.size();
					}
				}
				break;
			case WRITE_DATA:
				//(*ReportCallback)(inFlightDataPacket, 0);
				ranks[inFlightDataPacket->rank].ReceiveFromBus(inFlightDataPacket);
				break;
			default:
				ERROR("Encountered unexpected bus packet type" << *inFlightDataPacket);
				abort();
			}

			inFlightDataPacket = NULL;
		}
	}

	//updates
	if(logicLayer!=NULL)
	{
		logicLayer->Update();
	}

	simpleController.Update();
	for(unsigned i=0; i<NUM_RANKS; i++)
	{
		ranks[i].Update();
	}

	currentClockCycle++;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
testb $0x1, 0x1e3d8(%rip)     # 0x38543
je 0x1a2b6
leaq 0x1e1fc(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x1a1df
movq 0x1de44(%rip), %rdi     # 0x37fc8
leaq 0x1146c(%rip), %rsi     # 0x2b5f7
callq 0x31c0
movq %rax, %rdi
movq -0x28(%rbp), %rax
movl 0x1e0(%rax), %esi
callq 0x32d0
movq %rax, %rdi
leaq 0x1145f(%rip), %rsi     # 0x2b60b
callq 0x31c0
movq %rax, %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x3070
movq %rax, %rdi
leaq 0x113ca(%rip), %rsi     # 0x2b595
callq 0x31c0
movq %rax, %rdi
movq 0x1ddce(%rip), %rsi     # 0x37fa8
callq 0x3200
leaq 0x1e18e(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x1a20d
movq 0x1ddd6(%rip), %rdi     # 0x37fc8
leaq 0x11420(%rip), %rsi     # 0x2b619
callq 0x31c0
movq %rax, %rdi
movq 0x1dda0(%rip), %rsi     # 0x37fa8
callq 0x3200
movl $0x0, -0xc(%rbp)
movq -0x28(%rbp), %rdi
movl -0xc(%rbp), %eax
movq %rax, -0x30(%rbp)
addq $0x200, %rdi            # imm = 0x200
callq 0xbe60
movq %rax, %rcx
movq -0x30(%rbp), %rax
cmpq %rcx, %rax
jae 0x1a2b4
leaq 0x1e136(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x1a2a4
movq 0x1dd7e(%rip), %rdi     # 0x37fc8
leaq 0x105c1(%rip), %rsi     # 0x2a812
callq 0x31c0
movq %rax, %rdi
movl -0xc(%rbp), %esi
callq 0x32d0
movq %rax, %rdi
leaq 0x11f98(%rip), %rsi     # 0x2c203
callq 0x31c0
movq -0x28(%rbp), %rdi
movq %rax, -0x38(%rbp)
addq $0x200, %rdi            # imm = 0x200
movl -0xc(%rbp), %eax
movl %eax, %esi
callq 0xbe90
movq -0x38(%rbp), %rdi
movq (%rax), %rsi
callq 0x18f90
movq %rax, %rdi
movq 0x1dd09(%rip), %rsi     # 0x37fa8
callq 0x3200
jmp 0x1a2a6
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0x1a214
jmp 0x1a2b6
movq -0x28(%rbp), %rax
cmpq $0x0, 0x270(%rax)
jne 0x1a2d7
movq -0x28(%rbp), %rax
movl 0x27c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x27c(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x260(%rax)
je 0x1a344
movq -0x28(%rbp), %rax
movl 0x268(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x268(%rax)
cmpl $0x0, 0x268(%rax)
jne 0x1a342
movq -0x28(%rbp), %rax
movq %rax, %rdi
addq $0x1c8, %rdi            # imm = 0x1C8
movq 0x260(%rax), %rax
movl 0x14(%rax), %eax
movl %eax, %esi
callq 0x1a8c0
movq %rax, %rdi
movq -0x28(%rbp), %rax
movq 0x260(%rax), %rsi
callq 0x22200
movq -0x28(%rbp), %rax
movq $0x0, 0x260(%rax)
jmp 0x1a344
movq -0x28(%rbp), %rax
cmpq $0x0, 0x270(%rax)
je 0x1a5c3
movq -0x28(%rbp), %rax
movl 0x278(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x278(%rax)
cmpl $0x0, 0x278(%rax)
jne 0x1a5c1
movq -0x28(%rbp), %rax
movq 0x270(%rax), %rax
movl 0x4(%rax), %eax
movl %eax, -0x3c(%rbp)
subl $0x7, %eax
je 0x1a39f
jmp 0x1a38e
movl -0x3c(%rbp), %eax
subl $0x8, %eax
je 0x1a4f8
jmp 0x1a52f
testb $0x1, 0x1e19d(%rip)     # 0x38543
je 0x1a3eb
leaq 0x1dfc5(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x1a3e9
movq 0x1dc0d(%rip), %rdi     # 0x37fc8
leaq 0x11271(%rip), %rsi     # 0x2b633
callq 0x31c0
movq %rax, %rdi
movq -0x28(%rbp), %rax
movq 0x270(%rax), %rsi
callq 0x18f90
movq %rax, %rdi
movq 0x1dbc4(%rip), %rsi     # 0x37fa8
callq 0x3200
jmp 0x1a3eb
movq -0x28(%rbp), %rax
movq 0x270(%rax), %rax
testb $0x1, (%rax)
je 0x1a472
movq -0x28(%rbp), %rax
movq 0x258(%rax), %rax
movq %rax, -0x50(%rbp)
movl $0x90, %edi
callq 0x31d0
movq %rax, %rdi
movq -0x28(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0x48(%rbp)
movq 0x270(%rax), %rax
movq 0x30(%rax), %rcx
movl $0x2, %esi
movl $0x40, %edx
callq 0x293f0
jmp 0x1a43e
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdi
movq (%rdi), %rax
xorl %edx, %edx
callq *0x10(%rax)
jmp 0x1a4f3
movq -0x48(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x90, %esi
callq 0x31f0
jmp 0x1a63e
movq -0x28(%rbp), %rsi
movq %rsi, %rdi
addq $0x200, %rdi            # imm = 0x200
addq $0x270, %rsi            # imm = 0x270
callq 0x1aa30
movq -0x28(%rbp), %rax
movl 0x100(%rax), %ecx
addl $-0x1, %ecx
movl %ecx, 0x100(%rax)
movq 0x250(%rax), %rdi
movq 0x270(%rax), %rsi
movq (%rdi), %rax
xorl %edx, %edx
callq *0x10(%rax)
movq -0x28(%rbp), %rdi
addq $0x200, %rdi            # imm = 0x200
callq 0xbe60
movq -0x28(%rbp), %rcx
movl 0x1f8(%rcx), %ecx
cmpq %rcx, %rax
jbe 0x1a4f1
movq -0x28(%rbp), %rdi
addq $0x200, %rdi            # imm = 0x200
callq 0xbe60
movq %rax, %rcx
movq -0x28(%rbp), %rax
movl %ecx, 0x1f8(%rax)
jmp 0x1a4f3
jmp 0x1a5b2
movq -0x28(%rbp), %rax
movq %rax, %rdi
addq $0x1c8, %rdi            # imm = 0x1C8
movq 0x270(%rax), %rax
movl 0x14(%rax), %eax
movl %eax, %esi
callq 0x1a8c0
movq %rax, %rdi
movq -0x28(%rbp), %rax
movq 0x270(%rax), %rsi
callq 0x22200
jmp 0x1a5b2
movq 0x1daba(%rip), %rdi     # 0x37ff0
leaq 0xfbd3(%rip), %rsi      # 0x2a110
callq 0x31c0
movq %rax, %rdi
leaq 0x11106(%rip), %rsi     # 0x2b652
callq 0x31c0
movq %rax, %rdi
leaq 0xfb5e(%rip), %rsi      # 0x2a0b9
callq 0x31c0
movq %rax, %rdi
movl $0x98, %esi
callq 0x3380
movq %rax, %rdi
leaq 0xfbaa(%rip), %rsi      # 0x2a121
callq 0x31c0
movq %rax, %rdi
leaq 0x110dc(%rip), %rsi     # 0x2b662
callq 0x31c0
movq %rax, %rdi
movq -0x28(%rbp), %rax
movq 0x270(%rax), %rsi
callq 0x18f90
movq %rax, %rdi
movq 0x1da00(%rip), %rsi     # 0x37fa8
callq 0x3200
callq 0x30f0
movq -0x28(%rbp), %rax
movq $0x0, 0x270(%rax)
jmp 0x1a5c3
movq -0x28(%rbp), %rax
cmpq $0x0, 0x1e8(%rax)
je 0x1a5e1
movq -0x28(%rbp), %rax
movq 0x1e8(%rax), %rdi
callq 0x1cd60
movq -0x28(%rbp), %rdi
addq $0x10, %rdi
callq 0x24af0
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl 0x1dd5e(%rip), %eax     # 0x3835c
jae 0x1a628
movq -0x28(%rbp), %rdi
addq $0x1c8, %rdi            # imm = 0x1C8
movl -0x20(%rbp), %eax
movl %eax, %esi
callq 0x1a8c0
movq %rax, %rdi
movq (%rdi), %rax
callq *(%rax)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1a5f5
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
addq $0x50, %rsp
popq %rbp
retq
movq -0x18(%rbp), %rdi
callq 0x33a0
nopw (%rax,%rax)