// File: dramninjasUMD[P]BOBSim/DRAMChannel.cpp
// Function: BOBSim::DRAMChannel::DRAMChannel(unsigned int, BOBSim::Callback<BOBSim::BOB, void, BOBSim::BusPacket*, unsigned int>*)
DRAMChannel::DRAMChannel(unsigned id, Callback<BOB, void, BusPacket*, unsigned> *reportCB):
	inFlightCommandCountdown(0),
	inFlightDataCountdown(0),
	channelID(id),
	inFlightCommandPacket(NULL),
	inFlightDataPacket(NULL),
	readReturnQueueMax(0),
	simpleController(this),
	logicLayer(NULL),
	DRAMBusIdleCount(0)
{
	ReportCallback = reportCB;

	currentClockCycle = 0;

	Callback<DRAMChannel,void,BusPacket*,unsigned> *dataCallback = new Callback<DRAMChannel,void, BusPacket*,unsigned>(this, &DRAMChannel::ReceiveOnDataBus);
	Callback<DRAMChannel,void,BusPacket*,unsigned> *cmdCallback = new Callback<DRAMChannel,void, BusPacket*,unsigned>(this, &DRAMChannel::ReceiveOnCmdBus);
	simpleController.RegisterCallback(cmdCallback, dataCallback);

	for(int i=0; i<NUM_RANKS; i++)
	{
		ranks.push_back(Rank(i));
		ranks[i].RegisterCallback(dataCallback);
	}

	logicLayer = new LogicLayerInterface(id);

	Callback<LogicLayerInterface, void, Transaction*, unsigned> *logicCallback = new Callback<LogicLayerInterface, void, Transaction*, unsigned>(logicLayer, &LogicLayerInterface::ReceiveLogicOperation);
	SendToLogicLayer = logicCallback;

	Callback<DRAMChannel, bool, Transaction*, unsigned> *returnCallback = new Callback<DRAMChannel, bool, Transaction*, unsigned>(this, &DRAMChannel::AddTransaction);
	logicLayer->RegisterReturnCallback(returnCallback);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0xb5a0
movq -0xf8(%rbp), %rsi
leaq 0x1e6cc(%rip), %rax     # 0x37be0
movq %rax, (%rsi)
movq %rsi, %rdi
addq $0x10, %rdi
movq %rdi, -0x108(%rbp)
callq 0x24070
movq -0xf8(%rbp), %rdi
addq $0x1c8, %rdi            # imm = 0x1C8
movq %rdi, -0x100(%rbp)
callq 0x1a670
movq -0xf8(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, 0x1e0(%rdi)
movq $0x0, 0x1e8(%rdi)
movl $0x0, 0x1f8(%rdi)
addq $0x200, %rdi            # imm = 0x200
movq %rdi, -0xf0(%rbp)
callq 0x1a690
jmp 0x1957e
movq -0xf8(%rbp), %rax
movq $0x0, 0x260(%rax)
movl $0x0, 0x268(%rax)
movq $0x0, 0x270(%rax)
movl $0x0, 0x278(%rax)
movl $0x0, 0x27c(%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x250(%rax)
movq $0x0, 0x8(%rax)
movl $0x20, %edi
callq 0x31d0
movq %rax, -0x110(%rbp)
jmp 0x195df
movq -0xf8(%rbp), %rsi
movq -0x110(%rbp), %rdi
leaq 0x36c(%rip), %rax       # 0x19960
movq %rax, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
callq 0x1a7e0
jmp 0x1960f
movq -0x110(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x20, %edi
callq 0x31d0
movq %rax, -0x118(%rbp)
jmp 0x1962d
movq -0xf8(%rbp), %rsi
movq -0x118(%rbp), %rdi
leaq 0x6de(%rip), %rax       # 0x19d20
movq %rax, -0x58(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x58(%rbp), %rdx
movq -0x50(%rbp), %rcx
callq 0x1a7e0
jmp 0x1965d
movq -0xf8(%rbp), %rdi
movq -0x118(%rbp), %rax
movq %rax, -0x48(%rbp)
addq $0x10, %rdi
movq -0x48(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x285e0
jmp 0x19682
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl 0x1ecca(%rip), %eax     # 0x3835c
jae 0x19796
movq -0xf8(%rbp), %rax
addq $0x1c8, %rax            # imm = 0x1C8
movq %rax, -0x120(%rbp)
movl -0x5c(%rbp), %esi
leaq -0xb8(%rbp), %rdi
callq 0x21ff0
jmp 0x196bd
movq -0x120(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x1a850
jmp 0x196d2
leaq -0xb8(%rbp), %rdi
callq 0x1a880
movq -0xf8(%rbp), %rdi
addq $0x1c8, %rdi            # imm = 0x1C8
movslq -0x5c(%rbp), %rsi
callq 0x1a8c0
movq %rax, %rdi
movq -0x30(%rbp), %rsi
callq 0x23090
jmp 0x19703
jmp 0x19705
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x19689
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x19931
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x19925
movq -0x110(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x20, %esi
callq 0x31f0
jmp 0x19925
movq -0x118(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x20, %esi
callq 0x31f0
jmp 0x19925
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x1a880
jmp 0x19925
movl $0xb0, %edi
callq 0x31d0
movq %rax, -0x128(%rbp)
jmp 0x197a9
movq -0x128(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x1cc00
jmp 0x197ba
movq -0xf8(%rbp), %rax
movq -0x128(%rbp), %rcx
movq %rcx, 0x1e8(%rax)
movl $0x20, %edi
callq 0x31d0
movq %rax, -0x130(%rbp)
jmp 0x197e2
movq -0x130(%rbp), %rdi
movq -0xf8(%rbp), %rax
movq 0x1e8(%rax), %rsi
leaq 0x3492(%rip), %rax      # 0x1cc90
movq %rax, -0xd0(%rbp)
movq $0x0, -0xc8(%rbp)
movq -0xd0(%rbp), %rdx
movq -0xc8(%rbp), %rcx
callq 0x1a8e0
jmp 0x19825
movq -0xf8(%rbp), %rax
movq -0x130(%rbp), %rcx
movq %rcx, -0xc0(%rbp)
movq -0xc0(%rbp), %rcx
movq %rcx, 0x258(%rax)
movl $0x20, %edi
callq 0x31d0
movq %rax, -0x138(%rbp)
jmp 0x1985b
movq -0xf8(%rbp), %rsi
movq -0x138(%rbp), %rdi
leaq 0x6f0(%rip), %rax       # 0x19f60
movq %rax, -0xe8(%rbp)
movq $0x0, -0xe0(%rbp)
movq -0xe8(%rbp), %rdx
movq -0xe0(%rbp), %rcx
callq 0x1a950
jmp 0x19897
movq -0xf8(%rbp), %rax
movq -0x138(%rbp), %rcx
movq %rcx, -0xd8(%rbp)
movq 0x1e8(%rax), %rdi
movq -0xd8(%rbp), %rsi
callq 0x1cc70
jmp 0x198c1
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x128(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0xb0, %esi
callq 0x31f0
jmp 0x19925
movq -0x130(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x20, %esi
callq 0x31f0
jmp 0x19925
movq -0x138(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
movl $0x20, %esi
callq 0x31f0
movq -0xf0(%rbp), %rdi
callq 0x1a9c0
movq -0x100(%rbp), %rdi
callq 0x1a6b0
movq -0x108(%rbp), %rdi
callq 0x1a710
movq -0x20(%rbp), %rdi
callq 0x33a0
nopw %cs:(%rax,%rax)