// File: dramninjasUMD[P]BOBSim/RandomStreamSim.cpp
// Function: main
int main(int argc, char **argv)
{
	while (1)
	{
		static struct option long_options[] =
		{
			{"pwd", required_argument, 0, 'p'},
			{"numcycles",  required_argument,	0, 'c'},
			{"quiet",  no_argument, &BOBSim::SHOW_SIM_OUTPUT, 'q'},
			{"help", no_argument, 0, 'h'},
			{0, 0, 0, 0}
		};
		int option_index=0; //for getopt
		c = getopt_long (argc, argv, "c:n:p:bkq", long_options, &option_index);
		if (c == -1)
		{
			break;
		}
		switch (c)
		{
		case 0: //TODO: figure out what the hell this does, cuz it never seems to get called
			if (long_options[option_index].flag != 0) //do nothing on a flag
			{
				printf("setting flag\n");
				break;
			}
			printf("option %s",long_options[option_index].name);
			if (optarg)
			{
				printf(" with arg %s", optarg);
			}
			printf("\n");
			break;
		case 'h':
		case '?':
			usage();
			exit(0);
			break;
		case 'c':
			numCycles = atol(optarg);
			break;
		case 'n':
			BOBSim::NUM_PORTS = atoi(optarg);
		case 'p':
			pwdString = string(optarg);
			break;
		case 'q':
			BOBSim::SHOW_SIM_OUTPUT=0;
			break;
		}
	}

	//open up verification file
	if(VERIFICATION_OUTPUT)
	{
		verificationOutput.open("sim_out_DDR3_micron_64M_8B_x4_sg187E.ini.tmp");
	}

	BOBWrapper bobWrapper(0);
	transactionBuffer = vector< vector<Transaction *> >(NUM_PORTS,vector<Transaction *>());

	waitCounters = vector<unsigned>(NUM_PORTS,0);
	useCounters = vector<unsigned>(NUM_PORTS,0);

	randomSeeds = vector<unsigned>(NUM_PORTS,0);
	for(unsigned i=0; i<NUM_PORTS; i++)
	{
		randomSeeds[i]=i*SEED_CONSTANT + SEED_CONSTANT;
	}

	//iterate over total number of cycles
	//  "main loop"
	//   numCycles is the number of CPU cycles to simulate
	for (int cpuCycle=0; cpuCycle<numCycles; cpuCycle++)
	{
		DEBUG("\n=========================["<<currentClockCycle<<"]==========================");

		//adding new stuff
		for(unsigned l=0; l<NUM_PORTS; l++)
		{
			if(transactionBuffer[l].size()>0)
			{
				if(DEBUG_PORTS) DEBUG("== TraceBasedSim trying to send : port "<<l<<" : "<<*transactionBuffer[l][0]);

				if(bobWrapper.AddTransaction(transactionBuffer[l][0],l))
				{
					transactionBuffer[l].erase(transactionBuffer[l].begin());
				}
			}
			else
			{
				if(waitCounters[l]>0) waitCounters[l]--;

				//make sure we are not waiting during idle time
				if(waitCounters[l]==0)
				{
					FillTransactionBuffer(l);
				}
			}
		}


		//
		//Update bobWrapper
		//
		bobWrapper.Update();
		currentClockCycle++;
	}

	//
	//Debug output
	//
	bobWrapper.PrintStats(true);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x8b0, %rsp            # imm = 0x8B0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0xba41(%rip), %rdx      # 0x2bd9f
leaq 0x1803b(%rip), %rcx     # 0x383a0
leaq -0x14(%rbp), %r8
callq 0x3360
movl %eax, 0x181e4(%rip)     # 0x38558
cmpl $-0x1, 0x181dd(%rip)    # 0x38558
jne 0x20382
jmp 0x20547
movl 0x181d0(%rip), %eax     # 0x38558
movl %eax, -0x7d4(%rbp)
testl %eax, %eax
je 0x203fd
jmp 0x20394
movl -0x7d4(%rbp), %eax
subl $0x3f, %eax
je 0x20484
jmp 0x203a5
movl -0x7d4(%rbp), %eax
subl $0x63, %eax
je 0x20490
jmp 0x203b6
movl -0x7d4(%rbp), %eax
subl $0x68, %eax
je 0x20484
jmp 0x203c7
movl -0x7d4(%rbp), %eax
subl $0x6e, %eax
je 0x204ab
jmp 0x203d8
movl -0x7d4(%rbp), %eax
subl $0x70, %eax
je 0x204c0
jmp 0x203e9
movl -0x7d4(%rbp), %eax
subl $0x71, %eax
je 0x20538
jmp 0x20542
movslq -0x14(%rbp), %rcx
leaq 0x17f98(%rip), %rax     # 0x383a0
shlq $0x5, %rcx
addq %rcx, %rax
cmpq $0x0, 0x10(%rax)
je 0x20429
leaq 0xb98c(%rip), %rdi      # 0x2bda9
movb $0x0, %al
callq 0x3030
jmp 0x20542
movslq -0x14(%rbp), %rcx
leaq 0x17f6c(%rip), %rax     # 0x383a0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rsi
leaq 0xb972(%rip), %rdi      # 0x2bdb7
movb $0x0, %al
callq 0x3030
movq 0x17b5d(%rip), %rax     # 0x37fb0
cmpq $0x0, (%rax)
je 0x20471
movq 0x17b50(%rip), %rax     # 0x37fb0
movq (%rax), %rsi
leaq 0xb957(%rip), %rdi      # 0x2bdc1
movb $0x0, %al
callq 0x3030
leaq 0xa4cc(%rip), %rdi      # 0x2a944
movb $0x0, %al
callq 0x3030
jmp 0x20542
callq 0x20020
xorl %edi, %edi
callq 0x3270
movq 0x17b19(%rip), %rax     # 0x37fb0
movq (%rax), %rdi
callq 0x3260
movq %rax, 0x17ed2(%rip)     # 0x38378
jmp 0x20542
movq 0x17afe(%rip), %rax     # 0x37fb0
movq (%rax), %rdi
callq 0x3300
movl %eax, 0x17eb0(%rip)     # 0x38370
movq 0x17ae9(%rip), %rax     # 0x37fb0
movq (%rax), %rax
movq %rax, -0x7e8(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x7e0(%rbp)
callq 0x33b0
movq -0x7e8(%rbp), %rsi
movq -0x7e0(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x20be0
jmp 0x204fa
leaq 0x1807f(%rip), %rdi     # 0x38580
leaq -0x38(%rbp), %rsi
callq 0x3290
leaq -0x38(%rbp), %rdi
callq 0x3408
leaq -0x39(%rbp), %rdi
callq 0x3220
jmp 0x20542
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x3220
jmp 0x20bcb
movl $0x0, 0x17e32(%rip)     # 0x38374
jmp 0x20349
testb $0x1, 0x182f2(%rip)     # 0x38840
je 0x20568
leaq 0x18071(%rip), %rdi     # 0x385c8
leaq 0xb870(%rip), %rsi      # 0x2bdce
movl $0x10, %edx
callq 0x3040
xorl %eax, %eax
movl %eax, %esi
leaq -0x708(%rbp), %rdi
callq 0x115e0
movl 0x17df2(%rip), %eax     # 0x38370
movq %rax, -0x800(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x740(%rbp)
movq $0x0, -0x730(%rbp)
leaq -0x740(%rbp), %rdi
movq %rdi, -0x7f8(%rbp)
callq 0xb640
leaq -0x741(%rbp), %rdi
movq %rdi, -0x7f0(%rbp)
callq 0x20d50
movq -0x800(%rbp), %rsi
movq -0x7f8(%rbp), %rdx
movq -0x7f0(%rbp), %rcx
leaq -0x720(%rbp), %rdi
callq 0x20d70
jmp 0x205e3
leaq 0x18226(%rip), %rdi     # 0x38810
leaq -0x720(%rbp), %rsi
movq %rsi, -0x818(%rbp)
callq 0x20df0
movq -0x818(%rbp), %rdi
callq 0x20cb0
leaq -0x741(%rbp), %rdi
callq 0x20e20
leaq -0x740(%rbp), %rdi
callq 0xb9d0
movl 0x17d49(%rip), %eax     # 0x38370
movq %rax, -0x810(%rbp)
movl $0x0, -0x764(%rbp)
leaq -0x765(%rbp), %rdi
movq %rdi, -0x808(%rbp)
callq 0xb7b0
movq -0x810(%rbp), %rsi
movq -0x808(%rbp), %rcx
leaq -0x760(%rbp), %rdi
leaq -0x764(%rbp), %rdx
callq 0xb7d0
jmp 0x2066e
leaq 0x1816b(%rip), %rdi     # 0x387e0
leaq -0x760(%rbp), %rsi
movq %rsi, -0x830(%rbp)
callq 0xb850
movq -0x830(%rbp), %rdi
callq 0xb880
leaq -0x765(%rbp), %rdi
callq 0xb8e0
movl 0x17cca(%rip), %eax     # 0x38370
movq %rax, -0x828(%rbp)
movl $0x0, -0x784(%rbp)
leaq -0x785(%rbp), %rdi
movq %rdi, -0x820(%rbp)
callq 0xb7b0
movq -0x828(%rbp), %rsi
movq -0x820(%rbp), %rcx
leaq -0x780(%rbp), %rdi
leaq -0x784(%rbp), %rdx
callq 0xb7d0
jmp 0x206ed
leaq 0x18104(%rip), %rdi     # 0x387f8
leaq -0x780(%rbp), %rsi
movq %rsi, -0x848(%rbp)
callq 0xb850
movq -0x848(%rbp), %rdi
callq 0xb880
leaq -0x785(%rbp), %rdi
callq 0xb8e0
movl 0x17c4b(%rip), %eax     # 0x38370
movq %rax, -0x840(%rbp)
movl $0x0, -0x7a4(%rbp)
leaq -0x7a5(%rbp), %rdi
movq %rdi, -0x838(%rbp)
callq 0xb7b0
movq -0x840(%rbp), %rsi
movq -0x838(%rbp), %rcx
leaq -0x7a0(%rbp), %rdi
leaq -0x7a4(%rbp), %rdx
callq 0xb7d0
jmp 0x2076c
leaq 0x18055(%rip), %rdi     # 0x387c8
leaq -0x7a0(%rbp), %rsi
callq 0xb850
leaq -0x7a0(%rbp), %rdi
callq 0xb880
leaq -0x7a5(%rbp), %rdi
callq 0xb8e0
movl $0x0, -0x7ac(%rbp)
movl -0x7ac(%rbp), %eax
cmpl 0x17bc3(%rip), %eax     # 0x38370
jae 0x20879
movl -0x7ac(%rbp), %eax
imull 0x17c80(%rip), %eax     # 0x38440
addl 0x17c7a(%rip), %eax     # 0x38440
movl %eax, -0x84c(%rbp)
movl -0x7ac(%rbp), %eax
movl %eax, %esi
leaq 0x17fed(%rip), %rdi     # 0x387c8
callq 0xbec0
movl -0x84c(%rbp), %ecx
movl %ecx, (%rax)
movl -0x7ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7ac(%rbp)
jmp 0x207a1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x741(%rbp), %rdi
callq 0x20e20
leaq -0x740(%rbp), %rdi
callq 0xb9d0
jmp 0x20bbf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x765(%rbp), %rdi
callq 0xb8e0
jmp 0x20bbf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x785(%rbp), %rdi
callq 0xb8e0
jmp 0x20bbf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x7a5(%rbp), %rdi
callq 0xb8e0
jmp 0x20bbf
movl $0x0, -0x7b0(%rbp)
movslq -0x7b0(%rbp), %rax
cmpq 0x17ae7(%rip), %rax     # 0x38378
jge 0x20b94
cmpl $0x0, 0x17ad6(%rip)     # 0x38374
je 0x2091c
movq 0x17721(%rip), %rdi     # 0x37fc8
leaq 0xb54d(%rip), %rsi      # 0x2bdfb
callq 0x31c0
movq %rax, -0x858(%rbp)
jmp 0x208bc
movq -0x858(%rbp), %rdi
movq 0x17c86(%rip), %rsi     # 0x38550
callq 0x3070
movq %rax, -0x860(%rbp)
jmp 0x208d8
movq -0x860(%rbp), %rdi
leaq 0xb531(%rip), %rsi      # 0x2be17
callq 0x31c0
movq %rax, -0x868(%rbp)
jmp 0x208f4
movq -0x868(%rbp), %rdi
movq 0x176a6(%rip), %rsi     # 0x37fa8
callq 0x3200
jmp 0x20909
jmp 0x2091c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x20bbf
movl $0x0, -0x7b4(%rbp)
movl -0x7b4(%rbp), %eax
cmpl 0x17a3e(%rip), %eax     # 0x38370
jae 0x20b60
movl -0x7b4(%rbp), %eax
movl %eax, %esi
leaq 0x17ec9(%rip), %rdi     # 0x38810
callq 0x20d10
movq %rax, %rdi
callq 0xbe00
cmpq $0x0, %rax
jbe 0x20aec
testb $0x1, 0x17edc(%rip)     # 0x38841
je 0x20a1c
cmpl $0x0, 0x17a02(%rip)     # 0x38374
je 0x20a1a
movq 0x17649(%rip), %rdi     # 0x37fc8
leaq 0xb4ad(%rip), %rsi      # 0x2be33
callq 0x31c0
movq %rax, -0x870(%rbp)
jmp 0x20994
movq -0x870(%rbp), %rdi
movl -0x7b4(%rbp), %esi
callq 0x32d0
movq %rax, -0x878(%rbp)
jmp 0x209af
movq -0x878(%rbp), %rdi
leaq 0xa7b1(%rip), %rsi      # 0x2b16e
callq 0x31c0
movq %rax, -0x880(%rbp)
jmp 0x209cb
movl -0x7b4(%rbp), %eax
movl %eax, %esi
leaq 0x17e36(%rip), %rdi     # 0x38810
callq 0x20d10
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xbe40
movq -0x880(%rbp), %rdi
movq (%rax), %rsi
callq 0x294b0
movq %rax, -0x888(%rbp)
jmp 0x20a03
movq -0x888(%rbp), %rdi
movq 0x17597(%rip), %rsi     # 0x37fa8
callq 0x3200
jmp 0x20a18
jmp 0x20a1a
jmp 0x20a1c
movl -0x7b4(%rbp), %eax
movl %eax, %esi
leaq 0x17de5(%rip), %rdi     # 0x38810
callq 0x20d10
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xbe40
movq (%rax), %rsi
movl -0x7b4(%rbp), %edx
leaq -0x708(%rbp), %rdi
callq 0x14120
movb %al, -0x889(%rbp)
jmp 0x20a59
movb -0x889(%rbp), %al
testb $0x1, %al
jne 0x20a68
jmp 0x20aea
movl -0x7b4(%rbp), %eax
movl %eax, %esi
leaq 0x17d99(%rip), %rdi     # 0x38810
movq %rdi, -0x8a8(%rbp)
callq 0x20d10
movq -0x8a8(%rbp), %rdi
movq %rax, -0x8a0(%rbp)
movl -0x7b4(%rbp), %eax
movl %eax, %esi
callq 0x20d10
movq %rax, %rdi
callq 0xc0b0
movq %rax, -0x7c8(%rbp)
leaq -0x7c0(%rbp), %rdi
leaq -0x7c8(%rbp), %rsi
callq 0xc120
movq -0x8a0(%rbp), %rdi
movq -0x7c0(%rbp), %rsi
callq 0xc040
movq %rax, -0x898(%rbp)
jmp 0x20adc
movq -0x898(%rbp), %rax
movq %rax, -0x7d0(%rbp)
jmp 0x20b4a
movl -0x7b4(%rbp), %eax
movl %eax, %esi
leaq 0x17ce5(%rip), %rdi     # 0x387e0
callq 0xbec0
cmpl $0x0, (%rax)
jbe 0x20b20
movl -0x7b4(%rbp), %eax
movl %eax, %esi
leaq 0x17ccc(%rip), %rdi     # 0x387e0
callq 0xbec0
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movl -0x7b4(%rbp), %eax
movl %eax, %esi
leaq 0x17cb1(%rip), %rdi     # 0x387e0
callq 0xbec0
cmpl $0x0, (%rax)
jne 0x20b48
movl -0x7b4(%rbp), %edi
callq 0x20050
jmp 0x20b46
jmp 0x20b48
jmp 0x20b4a
jmp 0x20b4c
movl -0x7b4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7b4(%rbp)
jmp 0x20926
leaq -0x708(%rbp), %rdi
callq 0x14540
jmp 0x20b6e
movq 0x179db(%rip), %rax     # 0x38550
addq $0x1, %rax
movq %rax, 0x179d0(%rip)     # 0x38550
movl -0x7b0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7b0(%rbp)
jmp 0x20883
leaq -0x708(%rbp), %rdi
movl $0x1, %esi
callq 0x151b0
jmp 0x20ba7
leaq -0x708(%rbp), %rdi
callq 0x20e40
movl -0x4(%rbp), %eax
addq $0x8b0, %rsp            # imm = 0x8B0
popq %rbp
retq
leaq -0x708(%rbp), %rdi
callq 0x20e40
movq -0x48(%rbp), %rdi
callq 0x33a0
nopw %cs:(%rax,%rax)
nop