// File: dramninjasUMD[P]BOBSim/BOBWrapper.cpp
// Function: BOBSim::BOBWrapper::AddTransaction(BOBSim::Transaction*, unsigned int)
bool BOBWrapper::AddTransaction(Transaction* trans, unsigned port)
{
	if(inFlightRequestCounter[port]==0 &&
	        bob->ports[port].inputBuffer.size()<PORT_QUEUE_DEPTH)
	{
		trans->fullStartTime = currentClockCycle;
		requestCounterPerPort[port]++;

		inFlightRequest[port] = trans;
		inFlightRequest[port]->portID = port;
		inFlightRequest[port]->cyclesReqPort = currentClockCycle;

		inFlightRequestHeaderCounter[port] = 1;

		switch(trans->transactionType)
		{
		case DATA_READ:
			readsPerPort[port]++;
			inFlightRequestCounter[port] = 1;
			break;
		case DATA_WRITE:
			issuedWrites++;
			writesPerPort[port]++;
			inFlightRequestCounter[port] = TRANSACTION_SIZE / PORT_WIDTH;
			break;
		case LOGIC_OPERATION:
			issuedLogicOperations++;
			inFlightRequestCounter[port] = TRANSACTION_SIZE / PORT_WIDTH;
			break;
		default:
			ERROR(" = Error - wrong type");
			ERROR(trans);
			break;
		}

		if(DEBUG_PORTS) DEBUG(" = Putting transaction on port "<<port<<" - "<<*inFlightRequest[port]<<" for "<<inFlightRequestCounter[port]<<" CPU cycles");

		return true;
	}
	else
	{
		if(DEBUG_PORTS) DEBUG(" = Port Busy or Full");

		return false; //port busy
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x28(%rbp)
addq $0x30, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
cmpl $0x0, (%rax)
jne 0x144b7
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x10, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbee0
movq %rax, %rdi
addq $0x10, %rdi
callq 0xbe00
movl 0x24136(%rip), %ecx     # 0x382b0
cmpq %rcx, %rax
jae 0x144b7
movq -0x28(%rbp), %rdi
movq 0x8(%rdi), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x60(%rax)
addq $0xf8, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movq -0x28(%rbp), %rdi
movl (%rax), %ecx
incl %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
addq $0x18, %rdi
movq %rdi, -0x40(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x50(%rbp), %rcx
movq -0x40(%rbp), %rdi
movq %rcx, (%rax)
movl -0x1c(%rbp), %eax
movl %eax, %esi
movl %esi, %eax
movl %eax, -0x44(%rbp)
callq 0xbe40
movl -0x44(%rbp), %edx
movq -0x40(%rbp), %rdi
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq (%rcx), %rcx
movl %edx, 0x1c(%rcx)
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x38(%rbp), %rcx
movq -0x28(%rbp), %rdi
movq (%rax), %rax
movq %rcx, 0x38(%rax)
addq $0x48, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movl $0x1, (%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
testl %eax, %eax
je 0x14255
jmp 0x1423a
movl -0x2c(%rbp), %eax
subl $0x1, %eax
je 0x1428e
jmp 0x14244
movl -0x2c(%rbp), %eax
subl $0x3, %eax
je 0x142e6
jmp 0x14322
movq -0x28(%rbp), %rdi
addq $0x110, %rdi            # imm = 0x110
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movq -0x28(%rbp), %rdi
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
addq $0x30, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movl $0x1, (%rax)
jmp 0x143f5
movq -0x28(%rbp), %rdi
movl 0x290(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x290(%rdi)
addq $0x128, %rdi            # imm = 0x128
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movq -0x28(%rbp), %rdi
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movl 0x24089(%rip), %eax     # 0x3834c
xorl %edx, %edx
divl 0x24085(%rip)           # 0x38350
movl %eax, -0x54(%rbp)
addq $0x30, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movl -0x54(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x143f5
movq -0x28(%rbp), %rdi
movl 0x28c(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x28c(%rdi)
movl 0x2404d(%rip), %eax     # 0x3834c
xorl %edx, %edx
divl 0x24049(%rip)           # 0x38350
movl %eax, -0x58(%rbp)
addq $0x30, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movl -0x58(%rbp), %ecx
movl %ecx, (%rax)
jmp 0x143f5
movq 0x23cc7(%rip), %rdi     # 0x37ff0
leaq 0x15de0(%rip), %rsi     # 0x2a110
callq 0x31c0
movq %rax, %rdi
leaq 0x16bd7(%rip), %rsi     # 0x2af16
callq 0x31c0
movq %rax, %rdi
leaq 0x15d6b(%rip), %rsi     # 0x2a0b9
callq 0x31c0
movq %rax, %rdi
movl $0x14b, %esi            # imm = 0x14B
callq 0x3380
movq %rax, %rdi
leaq 0x15db7(%rip), %rsi     # 0x2a121
callq 0x31c0
movq %rax, %rdi
leaq 0x16be9(%rip), %rsi     # 0x2af62
callq 0x31c0
movq %rax, %rdi
movq 0x23c20(%rip), %rsi     # 0x37fa8
callq 0x3200
movq 0x23c5c(%rip), %rdi     # 0x37ff0
leaq 0x15d75(%rip), %rsi     # 0x2a110
callq 0x31c0
movq %rax, %rdi
leaq 0x16b6c(%rip), %rsi     # 0x2af16
callq 0x31c0
movq %rax, %rdi
leaq 0x15d00(%rip), %rsi     # 0x2a0b9
callq 0x31c0
movq %rax, %rdi
movl $0x14c, %esi            # imm = 0x14C
callq 0x3380
movq %rax, %rdi
leaq 0x15d4c(%rip), %rsi     # 0x2a121
callq 0x31c0
movq %rax, %rdi
movq -0x18(%rbp), %rsi
callq 0x3160
movq %rax, %rdi
movq 0x23bb8(%rip), %rsi     # 0x37fa8
callq 0x3200
testb $0x1, 0x2413d(%rip)     # 0x38539
je 0x144b1
leaq 0x23f6b(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x144af
movq 0x23baf(%rip), %rdi     # 0x37fc8
leaq 0x16b58(%rip), %rsi     # 0x2af78
callq 0x31c0
movq %rax, %rdi
movl -0x1c(%rbp), %esi
callq 0x32d0
movq %rax, %rdi
leaq 0x16b5e(%rip), %rsi     # 0x2af98
callq 0x31c0
movq -0x28(%rbp), %rdi
movq %rax, -0x68(%rbp)
addq $0x18, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbe40
movq -0x68(%rbp), %rdi
movq (%rax), %rsi
callq 0x294b0
movq %rax, %rdi
leaq 0x15e79(%rip), %rsi     # 0x2a2e4
callq 0x31c0
movq -0x28(%rbp), %rdi
movq %rax, -0x60(%rbp)
addq $0x30, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %esi
callq 0xbec0
movq -0x60(%rbp), %rdi
movl (%rax), %esi
callq 0x32d0
movq %rax, %rdi
leaq 0x16b01(%rip), %rsi     # 0x2af9c
callq 0x31c0
movq %rax, %rdi
movq 0x23afe(%rip), %rsi     # 0x37fa8
callq 0x3200
jmp 0x144b1
movb $0x1, -0x1(%rbp)
jmp 0x144f4
testb $0x1, 0x2407b(%rip)     # 0x38539
je 0x144f0
leaq 0x23ead(%rip), %rax     # 0x38374
cmpl $0x0, (%rax)
je 0x144ee
movq 0x23af5(%rip), %rdi     # 0x37fc8
leaq 0x16ace(%rip), %rsi     # 0x2afa8
callq 0x31c0
movq %rax, %rdi
movq 0x23abf(%rip), %rsi     # 0x37fa8
callq 0x3200
jmp 0x144f0
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nop