// File: /workspace/github_cpp_makefile_star2_O3/dramninjasUMD[P]BOBSim/SimpleController.cpp
// Function: BOBSim::SimpleController::SimpleController(BOBSim::DRAMChannel*)
SimpleController::SimpleController(DRAMChannel *parent) :
	refreshCounter(0),
	readCounter(0),
	writeCounter(0),
	commandQueueMax(0),
	commandQueueAverage(0),
	numIdleBanksAverage(0),
	numActBanksAverage(0),
	numPreBanksAverage(0),
	numRefBanksAverage(0),
	RRQFull(0),
	waitingACTS(0),
	idd2nCount(0),
	rankBitWidth(log2(NUM_RANKS)),
	bankBitWidth(log2(NUM_BANKS)),
	rowBitWidth(log2(NUM_ROWS)),
	colBitWidth(log2(NUM_COLS)),
	busOffsetBitWidth(log2(BUS_ALIGNMENT_SIZE)),
	channelBitWidth(log2(NUM_CHANNELS)),
	cacheOffset(log2(CACHE_LINE_SIZE)),
	outstandingReads(0)

{
	/*
	  HACK!!! - QEMU does not support the huge memory sizes BOB can simulate.
	  Removing bits from the row address will have the least impact on function
	*/
	uint qemuMemoryBitWidth = log2(QEMU_MEMORY_SIZE);
	uint bitDifference = (busOffsetBitWidth + colBitWidth + rowBitWidth + channelBitWidth + rankBitWidth + bankBitWidth) - qemuMemoryBitWidth;
	rowBitWidth = rowBitWidth - bitDifference;
	//end hack

	//Registers the parent channel object
	channel = parent;

	//Initialize
	currentClockCycle = 0;

	//Make the bank state objects
	bankStates = (BankState**) malloc(sizeof(BankState*)*(NUM_RANKS));
	for(unsigned i=0; i<NUM_RANKS; i++)
	{
		bankStates[i]= (BankState*) calloc(sizeof(BankState), NUM_BANKS);
	}

	//Used to keep track of refreshes 
	refreshCounters = vector<unsigned>(NUM_RANKS,0);

	//make tFAW sliding window - one per rank
	tFAWWindow.reserve(NUM_RANKS);
	for(unsigned i=0; i<NUM_RANKS; i++)
	{
		tFAWWindow.push_back(vector<unsigned>());
	}


	//init power fields
	backgroundEnergy = vector<uint64_t>(NUM_RANKS,0);
	burstEnergy = vector<uint64_t>(NUM_RANKS,0);
	actpreEnergy = vector<uint64_t>(NUM_RANKS,0);
	refreshEnergy = vector<uint64_t>(NUM_RANKS,0);
	idd2nCount = vector<unsigned>(NUM_RANKS,0);


	//init refresh counters
	for(unsigned i=0; i<NUM_RANKS; i++)
	{
		refreshCounters[i] = ((7800/tCK)/NUM_RANKS)*(i+1);
	}
	//Prints all of the initialized refresh counters
	if(DEBUG_CHANNEL)
	{
		for(unsigned i=0; i<NUM_RANKS; i++)
		{
			PRINT(i<<"  "<<refreshCounters[i]);
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r13
movq %rdi, %rbx
leaq 0x88f9(%rip), %rax      # 0x18d70
movq %rax, (%rdi)
leaq 0x28(%rdi), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x30(%rdi)
movups %xmm0, 0x40(%rdi)
movups %xmm0, 0x50(%rdi)
movups %xmm0, 0x60(%rdi)
xorl %ebp, %ebp
movq %rbp, 0x70(%rdi)
movq %r14, %rdi
xorl %esi, %esi
callq 0xd6d8
leaq 0x80(%rbx), %r15
leaq 0xb0(%rbx), %r12
movl $0xf0, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x30f0
movabsq $0x300000002, %rax      # imm = 0x300000002
movq %rax, 0x188(%rbx)
movaps 0x2b32(%rip), %xmm0     # 0x13010
movups %xmm0, 0x194(%rbx)
leaq 0x8dc4(%rip), %rax      # 0x192b0
movl (%rax), %eax
bsrl %eax, %ecx
cmpl $0x2, %eax
cmovbl %ebp, %ecx
movl $0x1, %edx
shll %cl, %edx
cmpl %eax, %edx
adcl $-0x16, %ecx
movl %ecx, 0x190(%rbx)
movq %r13, 0x170(%rbx)
movq %rbp, 0x8(%rbx)
movl $0x20, %edi
callq 0x3250
movq %rax, 0x78(%rbx)
movl $0x40, %edi
movl $0x8, %esi
callq 0x3110
movq 0x78(%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
incq %rbp
cmpq $0x4, %rbp
jne 0x10522
movl $0x10, %edi
callq 0x31d0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, %rcx
addq $0x10, %rcx
movq 0xc8(%rbx), %rdi
movq 0xd8(%rbx), %rsi
movq %rax, 0xc8(%rbx)
movq %rcx, 0xd0(%rbx)
movq %rcx, 0xd8(%rbx)
testq %rdi, %rdi
je 0x10589
subq %rdi, %rsi
callq 0x31f0
movl $0x4, %esi
movq %r12, %rdi
callq 0x11de2
movl $0x4, %ebp
movq %rsp, %r13
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x11f06
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x105cf
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31f0
decl %ebp
jne 0x1059e
movl $0x20, %edi
callq 0x31d0
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, %rcx
addq $0x20, %rcx
movq 0xf8(%rbx), %rdi
movq 0x108(%rbx), %rsi
movq %rax, 0xf8(%rbx)
movq %rcx, 0x100(%rbx)
movq %rcx, 0x108(%rbx)
testq %rdi, %rdi
je 0x1061e
subq %rdi, %rsi
callq 0x31f0
movl $0x20, %edi
callq 0x31d0
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, %rcx
addq $0x20, %rcx
movq 0x110(%rbx), %rdi
movq 0x120(%rbx), %rsi
movq %rax, 0x110(%rbx)
movq %rcx, 0x118(%rbx)
movq %rcx, 0x120(%rbx)
testq %rdi, %rdi
je 0x10669
subq %rdi, %rsi
callq 0x31f0
movl $0x20, %edi
callq 0x31d0
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, %rcx
addq $0x20, %rcx
movq 0x128(%rbx), %rdi
movq 0x138(%rbx), %rsi
movq %rax, 0x128(%rbx)
movq %rcx, 0x130(%rbx)
movq %rcx, 0x138(%rbx)
testq %rdi, %rdi
je 0x106b4
subq %rdi, %rsi
callq 0x31f0
movl $0x20, %edi
callq 0x31d0
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rax, %rcx
addq $0x20, %rcx
movq 0x140(%rbx), %rdi
movq 0x150(%rbx), %rsi
movq %rax, 0x140(%rbx)
movq %rcx, 0x148(%rbx)
movq %rcx, 0x150(%rbx)
testq %rdi, %rdi
je 0x106ff
subq %rdi, %rsi
callq 0x31f0
movl $0x10, %edi
callq 0x31d0
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, %rcx
addq $0x10, %rcx
movq 0x158(%rbx), %rdi
movq 0x168(%rbx), %rsi
movq %rax, 0x158(%rbx)
movq %rcx, 0x160(%rbx)
movq %rcx, 0x168(%rbx)
testq %rdi, %rdi
je 0x10746
subq %rdi, %rsi
callq 0x31f0
movq 0xc8(%rbx), %rax
movaps 0x42dc(%rip), %xmm0     # 0x14a30
movups %xmm0, (%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x10772
jmp 0x10772
jmp 0x10772
jmp 0x10772
jmp 0x10772
jmp 0x10772
movq %rax, %r13
jmp 0x10790
movq %rax, %r13
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x10790
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0x158(%rbx), %rdi
testq %rdi, %rdi
je 0x107ab
movq 0x168(%rbx), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0x140(%rbx), %rdi
testq %rdi, %rdi
je 0x107c6
movq 0x150(%rbx), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0x128(%rbx), %rdi
testq %rdi, %rdi
je 0x107e1
movq 0x138(%rbx), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0x110(%rbx), %rdi
testq %rdi, %rdi
je 0x107fc
movq 0x120(%rbx), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0xf8(%rbx), %rdi
testq %rdi, %rdi
je 0x10817
movq 0x108(%rbx), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0xc8(%rbx), %rdi
testq %rdi, %rdi
je 0x10832
movq 0xd8(%rbx), %rsi
subq %rdi, %rsi
callq 0x31f0
movq %r12, %rdi
callq 0xbfa8
movq 0x98(%rbx), %rdi
testq %rdi, %rdi
je 0x10855
movq 0xa8(%rbx), %rsi
subq %rdi, %rsi
callq 0x31f0
movq (%r15), %rdi
testq %rdi, %rdi
je 0x1086c
movq 0x90(%rbx), %rsi
subq %rdi, %rsi
callq 0x31f0
movq %r14, %rdi
callq 0xd84e
movq %r13, %rdi
callq 0x3340