// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movq %rsi, %r15
movq %rdi, %r14
leaq 0x8785(%rip), %rax      # 0x18d70
movq %rax, (%rdi)
addq $0x28, %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
movups %xmm0, 0x20(%r14)
movups %xmm0, 0x30(%r14)
movups %xmm0, 0x40(%r14)
movups %xmm0, 0x50(%r14)
movups %xmm0, 0x60(%r14)
xorl %ebp, %ebp
movq %rbp, 0x70(%r14)
movq %rdi, 0x40(%rsp)
xorl %esi, %esi
callq 0xd8ba
leaq 0x80(%r14), %rdi
leaq 0xb0(%r14), %r13
leaq 0xc8(%r14), %r12
leaq 0xf8(%r14), %rax
movq %rax, 0x30(%rsp)
leaq 0x110(%r14), %rax
movq %rax, 0x28(%rsp)
leaq 0x128(%r14), %rax
movq %rax, 0x20(%rsp)
leaq 0x140(%r14), %rax
movq %rax, 0x18(%rsp)
leaq 0x158(%r14), %rbx
movl $0xf0, %edx
movq %rdi, 0x38(%rsp)
xorl %esi, %esi
callq 0x30f0
movaps 0x2987(%rip), %xmm0     # 0x13010
movups %xmm0, 0x188(%r14)
movabsq $0x300000003, %rax      # imm = 0x300000003
movq %rax, 0x198(%r14)
movl $0x6, 0x1a0(%r14)
leaq 0x8bfc(%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(%r14)
movq %r15, 0x170(%r14)
movq %rbp, 0x8(%r14)
movl $0x20, %edi
callq 0x3250
movq %rax, 0x78(%r14)
movl $0x40, %edi
movl $0x8, %esi
callq 0x3110
movq 0x78(%r14), %rcx
movq %rax, (%rcx,%rbp,8)
incq %rbp
cmpq $0x4, %rbp
jne 0x106eb
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movl $0x10, %edi
callq 0x31d0
movq %rsp, %r15
movq %rax, (%r15)
movq %rax, 0x8(%r15)
movq %rax, %rcx
addq $0x10, %rcx
movq %rcx, 0x10(%r15)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rcx, 0x8(%r15)
movq %r12, %rdi
movq %r15, %rsi
callq 0x7422
movq (%r15), %rdi
testq %rdi, %rdi
je 0x10764
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31f0
movl $0x4, %esi
movq %r13, %rdi
callq 0x11ef0
movl $0x4, %ebp
movq %rsp, %r15
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x12026
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x107aa
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31f0
decl %ebp
jne 0x10779
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movl $0x20, %edi
callq 0x31d0
movq %rsp, %r15
movq %rax, (%r15)
movq %rax, 0x8(%r15)
movq %rax, %rcx
addq $0x20, %rcx
movq %rcx, 0x10(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rcx, 0x8(%r15)
movq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x74be
movq (%r15), %rdi
testq %rdi, %rdi
je 0x1080d
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31f0
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movl $0x20, %edi
callq 0x31d0
movq %rsp, %r15
movq %rax, (%r15)
movq %rax, 0x8(%r15)
movq %rax, %rcx
addq $0x20, %rcx
movq %rcx, 0x10(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rcx, 0x8(%r15)
movq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x74be
movq (%r15), %rdi
testq %rdi, %rdi
je 0x1086c
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31f0
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movl $0x20, %edi
callq 0x31d0
movq %rsp, %r15
movq %rax, (%r15)
movq %rax, 0x8(%r15)
movq %rax, %rcx
addq $0x20, %rcx
movq %rcx, 0x10(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rcx, 0x8(%r15)
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x74be
movq (%r15), %rdi
testq %rdi, %rdi
je 0x108cb
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31f0
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movl $0x20, %edi
callq 0x31d0
movq %rsp, %r15
movq %rax, (%r15)
movq %rax, 0x8(%r15)
movq %rax, %rcx
addq $0x20, %rcx
movq %rcx, 0x10(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rcx, 0x8(%r15)
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x74be
movq (%r15), %rdi
testq %rdi, %rdi
je 0x1092a
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31f0
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movl $0x10, %edi
callq 0x31d0
movq %rsp, %r14
movq %rax, (%r14)
movq %rax, 0x8(%r14)
movq %rax, %rcx
addq $0x10, %rcx
movq %rcx, 0x10(%r14)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rcx, 0x8(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7422
movq (%r14), %rdi
testq %rdi, %rdi
je 0x10983
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31f0
movq (%r12), %rax
xorl %ecx, %ecx
movss 0x3ff3(%rip), %xmm0     # 0x14984
leaq 0x1(%rcx), %rdx
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %rsi
movl %esi, (%rax,%rcx,4)
movq %rdx, %rcx
cmpq $0x4, %rdx
jne 0x10991
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x109cc
jmp 0x109cc
jmp 0x109cc
jmp 0x109cc
jmp 0x109cc
jmp 0x109cc
movq %rax, %r15
jmp 0x109ea
movq %rax, %r15
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x109ea
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x31f0
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x10a01
movq 0x168(%r14), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0x18(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x10a1d
movq 0x150(%r14), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0x20(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x10a39
movq 0x138(%r14), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0x28(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x10a55
movq 0x120(%r14), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0x30(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x10a71
movq 0x108(%r14), %rsi
subq %rdi, %rsi
callq 0x31f0
movq (%r12), %rdi
testq %rdi, %rdi
je 0x10a89
movq 0xd8(%r14), %rsi
subq %rdi, %rsi
callq 0x31f0
movq %r13, %rdi
callq 0xc19c
movq 0x98(%r14), %rdi
testq %rdi, %rdi
je 0x10aac
movq 0xa8(%r14), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0x38(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x10ac8
movq 0x90(%r14), %rsi
subq %rdi, %rsi
callq 0x31f0
movq 0x40(%rsp), %rdi
callq 0xda30
movq %r15, %rdi
callq 0x3340