// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movq %rsi, %rbp
movq %rdi, %r14
leaq 0x8eb5(%rip), %rax      # 0x15d78
movq %rax, (%rdi)
addq $0x28, %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r14)
andq $0x0, 0x20(%r14)
movq %rdi, 0x60(%rsp)
callq 0xa5aa
leaq 0x80(%r14), %rdi
leaq 0xb0(%r14), %r13
leaq 0xc8(%r14), %rax
movq %rax, 0x50(%rsp)
leaq 0xf8(%r14), %rax
movq %rax, 0x48(%rsp)
leaq 0x110(%r14), %rax
movq %rax, 0x38(%rsp)
leaq 0x128(%r14), %rax
movq %rax, 0x30(%rsp)
leaq 0x140(%r14), %rax
movq %rax, 0x40(%rsp)
leaq 0x158(%r14), %rbx
movl $0xd8, %edx
movq %rdi, 0x58(%rsp)
xorl %esi, %esi
callq 0x20d0
leaq 0x10(%rsp), %rdx
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe252
pushq $0x4
popq %r15
movl %r15d, %edi
callq 0x5150
movl %eax, 0x188(%r14)
pushq $0x8
popq %r12
movl %r12d, %edi
callq 0x5150
movl %eax, 0x18c(%r14)
movl $0x10000, %edi          # imm = 0x10000
callq 0x5150
movl %eax, 0x190(%r14)
movl $0x800, %edi            # imm = 0x800
callq 0x5150
movl %eax, 0x194(%r14)
movl %r12d, %edi
callq 0x5150
movl %eax, 0x198(%r14)
movl %r12d, %edi
callq 0x5150
movl %eax, 0x19c(%r14)
pushq $0x40
popq %rdi
callq 0x5150
movl %eax, 0x1a0(%r14)
leaq 0x92bd(%rip), %rax      # 0x16290
movl (%rax), %edi
callq 0x5150
movl 0x198(%r14), %ecx
addl 0x194(%r14), %ecx
addl 0x19c(%r14), %ecx
addl 0x188(%r14), %ecx
addl 0x18c(%r14), %ecx
subl %ecx, %eax
movl %eax, 0x190(%r14)
movq %rbp, 0x170(%r14)
andq $0x0, 0x8(%r14)
pushq $0x20
popq %rdi
callq 0x2200
movq %rax, 0x78(%r14)
xorl %ebx, %ebx
pushq $0x40
popq %rbp
pushq $0x8
popq %r12
cmpq $0x4, %rbx
je 0xd045
movq %rbp, %rdi
movq %r12, %rsi
callq 0x20f0
movq 0x78(%r14), %rcx
movq %rax, (%rcx,%rbx,8)
incq %rbx
jmp 0xd027
leaq 0x8(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
leaq 0x7(%rsp), %rcx
callq 0x51aa
leaq 0x10(%rsp), %r12
movq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x57f6
movq %r12, %rdi
callq 0x549c
pushq $0x4
popq %rsi
movq %r13, %rdi
callq 0xe2a4
leaq 0x10(%rsp), %rbp
subl $0x1, %r15d
jb 0xd0b5
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
callq 0xe3de
movq %rbp, %rdi
callq 0x549c
jmp 0xd08c
leaq 0x8(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
leaq 0x7(%rsp), %rcx
callq 0x5256
leaq 0x10(%rsp), %r15
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x5a06
movq %r15, %rdi
callq 0x54d0
leaq 0x8(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
leaq 0x7(%rsp), %rcx
movq 0x40(%rsp), %r12
callq 0x5256
leaq 0x10(%rsp), %r15
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0x5a06
movq %r15, %rdi
callq 0x54d0
leaq 0x8(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
leaq 0x7(%rsp), %rcx
callq 0x5256
leaq 0x10(%rsp), %r15
movq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x5a06
movq %r15, %rdi
callq 0x54d0
leaq 0x8(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
leaq 0x7(%rsp), %rcx
callq 0x5256
leaq 0x10(%rsp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x5a06
movq %r15, %rdi
callq 0x54d0
leaq 0x8(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x10(%rsp), %rdi
pushq $0x4
popq %rsi
leaq 0x7(%rsp), %rcx
callq 0x51aa
leaq 0x10(%rsp), %r14
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x57f6
movq %r14, %rdi
callq 0x549c
movq (%rbx), %rax
xorl %ecx, %ecx
movss 0x37c7(%rip), %xmm0     # 0x10994
cmpq $0x4, %rcx
je 0xd1ec
leaq 0x1(%rcx), %rdx
cvtsi2ss %edx, %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %rsi
movl %esi, (%rax,%rcx,4)
movq %rdx, %rcx
jmp 0xd1cd
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xd207
jmp 0xd207
jmp 0xd207
jmp 0xd207
jmp 0xd207
jmp 0xd207
movq %rax, %r15
jmp 0xd21e
movq %rax, %r15
jmp 0xd228
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x549c
movq 0x28(%rsp), %rdi
callq 0x549c
addq $0x98, %r14
movq 0x40(%rsp), %rdi
callq 0x54d0
movq 0x30(%rsp), %rdi
callq 0x54d0
movq 0x38(%rsp), %rdi
callq 0x54d0
movq 0x48(%rsp), %rdi
callq 0x54d0
movq 0x50(%rsp), %rdi
callq 0x549c
movq %r13, %rdi
callq 0x9088
movq %r14, %rdi
callq 0xa7b2
movq 0x58(%rsp), %rdi
callq 0x549c
movq 0x60(%rsp), %rdi
callq 0xa7e6
movq %r15, %rdi
callq 0x22f0
nop