// File: dramninjasUMD[P]BOBSim/BOB.cpp
// Function: BOBSim::BOB::FindChannelID(BOBSim::Transaction*)
unsigned BOB::FindChannelID(Transaction* trans)
{
	unsigned channelID = 0;
	unsigned bitWidth = log2(NUM_CHANNELS);
	uint64_t channelMask = 0;
	unsigned channelIDOffset;// = log2(BUS_ALIGNMENT_SIZE) + CHANNEL_ID_OFFSET;

	switch(mappingScheme)
	{
	case BK_CLH_RW_RK_CH_CLL_BY://bank:col_high:row:rank:chan:col_low:by
		channelIDOffset = cacheOffset;//bus alignment + column low bits should make the cache offset
		break;
	case CLH_RW_RK_BK_CH_CLL_BY://col_high:row:rank:bank:chan:col_low:byte
		channelIDOffset = cacheOffset;//bus alignment + column low bits should make the cache offset
		break;
	case RK_BK_RW_CLH_CH_CLL_BY://rank:bank:row:col_high:chan:col_low:by
		channelIDOffset = cacheOffset;//bus alignment + column low bits should make the cache offset
		break;
	case RW_BK_RK_CH_CL_BY://row:bank:rank:chan:col:byte
		channelIDOffset = colBitWidth + busOffsetBitWidth;
		break;
	case RW_CH_BK_RK_CL_BY://row:chan:bank:rank:col:byte
		channelIDOffset = busOffsetBitWidth + colBitWidth + rankBitWidth + bankBitWidth;
		break;
	case RW_BK_RK_CLH_CH_CLL_BY://row:bank:rank:col_high:chan:col_low:byte
		channelIDOffset = cacheOffset; //bus alignment + column low bits should make the cache offset
		break;
	case RW_CLH_BK_RK_CH_CLL_BY://row:col_high:bank:rank:chan:col_low:byte
		channelIDOffset = cacheOffset;
		break;
	case CH_RW_BK_RK_CL_BY: //chan:row:bank:rank:col:byte
		channelIDOffset = rowBitWidth + bankBitWidth + rankBitWidth + colBitWidth + busOffsetBitWidth;
		break;
	default:
		ERROR("== ERROR - Unknown address mapping???");
		exit(1);
		break;
	};

	if(DEBUG_BOB) DEBUGN("    == Mapping "<<*trans);

	//build channel id mask
	for(unsigned i=0; i<bitWidth; i++)
	{
		channelMask = channelMask<<1;
		channelMask++;
	}

	channelMask = channelMask << channelIDOffset;
	channelID = (trans->address & channelMask) >> channelIDOffset;

	if(DEBUG_BOB) DEBUG(" to channel "<<channelID);
	return channelID;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0x14(%rbp)
movl 0x2f077(%rip), %edi     # 0x3822c
callq 0xb660
movl %eax, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl 0x2f0a5(%rip), %eax     # 0x38270
movq %rax, -0x30(%rbp)
subq $0x7, %rax
ja 0x92aa
movq -0x30(%rbp), %rax
leaq 0x20e44(%rip), %rcx     # 0x2a028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x38(%rbp), %rax
movl 0x1f8(%rax), %eax
movl %eax, -0x24(%rbp)
jmp 0x931f
movq -0x38(%rbp), %rax
movl 0x1f8(%rax), %eax
movl %eax, -0x24(%rbp)
jmp 0x931f
movq -0x38(%rbp), %rax
movl 0x1f8(%rax), %eax
movl %eax, -0x24(%rbp)
jmp 0x931f
movq -0x38(%rbp), %rcx
movl 0x1ec(%rcx), %eax
addl 0x1f0(%rcx), %eax
movl %eax, -0x24(%rbp)
jmp 0x931f
movq -0x38(%rbp), %rcx
movl 0x1f0(%rcx), %eax
addl 0x1ec(%rcx), %eax
addl 0x1e0(%rcx), %eax
addl 0x1e4(%rcx), %eax
movl %eax, -0x24(%rbp)
jmp 0x931f
movq -0x38(%rbp), %rax
movl 0x1f8(%rax), %eax
movl %eax, -0x24(%rbp)
jmp 0x931f
movq -0x38(%rbp), %rax
movl 0x1f8(%rax), %eax
movl %eax, -0x24(%rbp)
jmp 0x931f
movq -0x38(%rbp), %rcx
movl 0x1e8(%rcx), %eax
addl 0x1e4(%rcx), %eax
addl 0x1e0(%rcx), %eax
addl 0x1ec(%rcx), %eax
addl 0x1f0(%rcx), %eax
movl %eax, -0x24(%rbp)
jmp 0x931f
movq 0x2ed3f(%rip), %rdi     # 0x37ff0
leaq 0x20e58(%rip), %rsi     # 0x2a110
callq 0x31c0
movq %rax, %rdi
leaq 0x20e52(%rip), %rsi     # 0x2a119
callq 0x31c0
movq %rax, %rdi
leaq 0x20de3(%rip), %rsi     # 0x2a0b9
callq 0x31c0
movq %rax, %rdi
movl $0x328, %esi            # imm = 0x328
callq 0x3380
movq %rax, %rdi
leaq 0x20e2f(%rip), %rsi     # 0x2a121
callq 0x31c0
movq %rax, %rdi
leaq 0x21400(%rip), %rsi     # 0x2a701
callq 0x31c0
movq %rax, %rdi
movq 0x2ec98(%rip), %rsi     # 0x37fa8
callq 0x3200
movl $0x1, %edi
callq 0x3270
testb $0x1, 0x2f1fe(%rip)     # 0x38524
je 0x9355
leaq 0x2f045(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x9353
movq 0x2ec8d(%rip), %rdi     # 0x37fc8
leaq 0x213e5(%rip), %rsi     # 0x2a727
callq 0x31c0
movq %rax, %rdi
movq -0x10(%rbp), %rsi
callq 0x294b0
jmp 0x9355
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x9386
movq -0x20(%rbp), %rax
shlq %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x935c
movq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shlq %cl, %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
andq -0x20(%rbp), %rax
movl -0x24(%rbp), %ecx
shrq %cl, %rax
movl %eax, -0x14(%rbp)
testb $0x1, 0x2f174(%rip)     # 0x38524
je 0x93ed
leaq 0x2efbb(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x93eb
movq 0x2ec03(%rip), %rdi     # 0x37fc8
leaq 0x20f7a(%rip), %rsi     # 0x2a346
callq 0x31c0
movq %rax, %rdi
movl -0x14(%rbp), %esi
callq 0x32d0
movq %rax, %rdi
movq 0x2ebc2(%rip), %rsi     # 0x37fa8
callq 0x3200
jmp 0x93ed
movl -0x14(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)