// File: /workspace/github_cpp_makefile_star2_O1/NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::fetch_instruction()
void O3_CPU::fetch_instruction()
{
    // TODO: can we model wrong path execusion?

    // add this request to ITLB
    uint32_t read_index = (ROB.last_read == (ROB.SIZE-1)) ? 0 : (ROB.last_read + 1);
    for (uint32_t i=0; i<FETCH_WIDTH; i++) {

        if (ROB.entry[read_index].ip == 0)
            break;

#ifdef SANITY_CHECK
        // sanity check
        if (ROB.entry[read_index].translated) {
            if (read_index == ROB.head)
                break;
            else {
                cout << "read_index: " << read_index << " ROB.head: " << ROB.head << " ROB.tail: " << ROB.tail << endl;
                assert(0);
            }
        }
#endif

        PACKET trace_packet;
        trace_packet.instruction = 1;
        trace_packet.tlb_access = 1;
        trace_packet.fill_level = FILL_L1;
        trace_packet.cpu = cpu;
        trace_packet.address = ROB.entry[read_index].ip >> LOG2_PAGE_SIZE;
        if (knob_cloudsuite)
            trace_packet.address = ((ROB.entry[read_index].ip >> LOG2_PAGE_SIZE) << 9) | ( 256 + ROB.entry[read_index].asid[0]);
        else
            trace_packet.address = ROB.entry[read_index].ip >> LOG2_PAGE_SIZE;
        trace_packet.full_addr = ROB.entry[read_index].ip;
        trace_packet.instr_id = ROB.entry[read_index].instr_id;
        trace_packet.rob_index = read_index;
        trace_packet.producer = 0; // TODO: check if this guy gets used or not
        trace_packet.ip = ROB.entry[read_index].ip;
        trace_packet.type = LOAD; 
        trace_packet.asid[0] = ROB.entry[read_index].asid[0];
        trace_packet.asid[1] = ROB.entry[read_index].asid[1];
        trace_packet.event_cycle = current_core_cycle[cpu];

        int rq_index = ITLB.add_rq(&trace_packet);

        if (rq_index == -2)
            break;
        else {
            /*
            if (rq_index >= 0) {
                uint32_t producer = ITLB.RQ.entry[rq_index].rob_index;
                ROB.entry[read_index].fetch_producer = producer;
                ROB.entry[read_index].is_consumer = 1;

                ROB.entry[producer].memory_instrs_depend_on_me[read_index] = 1;
                ROB.entry[producer].is_producer = 1; // producer for fetch
            }
            */

            ROB.last_read = read_index;
            read_index++;
            if (read_index == ROB.SIZE)
                read_index = 0;
        }
    }
    
    uint32_t fetch_index = (ROB.last_fetch == (ROB.SIZE-1)) ? 0 : (ROB.last_fetch + 1);

    for (uint32_t i=0; i<FETCH_WIDTH; i++) {

        // fetch is in-order so it should be break
        if ((ROB.entry[fetch_index].translated != COMPLETED) || (ROB.entry[fetch_index].event_cycle > current_core_cycle[cpu])) 
            break;

        // sanity check
        if (ROB.entry[fetch_index].fetched) {
            if (fetch_index == ROB.head)
                break;
            else {
                cout << "fetch_index: " << fetch_index << " ROB.head: " << ROB.head << " ROB.tail: " << ROB.tail << endl;
                assert(0);
            }
        }

        // add it to L1I
        PACKET fetch_packet;
        fetch_packet.instruction = 1;
        fetch_packet.fill_level = FILL_L1;
        fetch_packet.cpu = cpu;
        fetch_packet.address = ROB.entry[fetch_index].instruction_pa >> 6;
        fetch_packet.instruction_pa = ROB.entry[fetch_index].instruction_pa;
        fetch_packet.full_addr = ROB.entry[fetch_index].instruction_pa;
        fetch_packet.instr_id = ROB.entry[fetch_index].instr_id;
        fetch_packet.rob_index = fetch_index;
        fetch_packet.producer = 0;
        fetch_packet.ip = ROB.entry[fetch_index].ip;
        fetch_packet.type = LOAD; 
        fetch_packet.asid[0] = ROB.entry[fetch_index].asid[0];
        fetch_packet.asid[1] = ROB.entry[fetch_index].asid[1];
        fetch_packet.event_cycle = current_core_cycle[cpu];

        int rq_index = L1I.add_rq(&fetch_packet);

        if (rq_index == -2)
            break;
        else {
            /*
            if (rq_index >= 0) {
                uint32_t producer = L1I.RQ.entry[rq_index].rob_index;
                ROB.entry[fetch_index].fetch_producer = producer;
                ROB.entry[fetch_index].is_consumer = 1;

                ROB.entry[producer].memory_instrs_depend_on_me[fetch_index] = 1;
                ROB.entry[producer].is_producer = 1;
            }
            */

            ROB.entry[fetch_index].fetched = INFLIGHT;
            ROB.last_fetch = fetch_index;
            fetch_index++;
            if (fetch_index == ROB.SIZE)
                fetch_index = 0;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3a8, %rsp            # imm = 0x3A8
movq %rdi, %r13
movl 0x950(%rdi), %eax
movl 0x964(%rdi), %ecx
decl %eax
leal 0x1(%rcx), %ebp
xorl %edx, %edx
cmpl %eax, %ecx
cmovel %edx, %ebp
leaq 0x3690(%rdi), %rax
movq %rax, 0x18(%rsp)
movl $0x3, %edx
xorl %r12d, %r12d
movq 0x9b8(%r13), %r15
movl %ebp, %eax
imulq $0x758, %rax, %r14      # imm = 0x758
cmpq $0x0, 0x8(%r15,%r14)
je 0xd1d9
leaq (%r15,%r14), %rbx
cmpb $0x0, 0x4c(%rbx)
jne 0xd1cc
movl %edx, 0xc(%rsp)
movl %ebp, 0x10(%rsp)
movl %r12d, 0x2b(%rsp)
movl %r12d, 0x28(%rsp)
movl $0xffff00, 0x354(%rsp) # imm = 0xFFFF00
leaq 0x30(%rsp), %rax
movq %r13, %rbp
movq %rax, %r13
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%rax)
movl $0x314, %edx            # imm = 0x314
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x3130
movl $0x1, %eax
movl %eax, 0x358(%rsp)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x32c(%r13)
movdqu %xmm0, 0x33c(%r13)
movl %r12d, 0x34c(%r13)
movdqu %xmm0, 0x358(%r13)
movq $0x0, 0x368(%r13)
movq %rbp, %r13
movq $-0x1, 0x3a0(%rsp)
movw $0x101, 0x28(%rsp)      # imm = 0x101
movl %eax, 0x30(%rsp)
movl (%rbp), %eax
movl %eax, 0x358(%rsp)
movq 0x8(%r15,%r14), %rcx
movq %rcx, %rdx
shrq $0xc, %rdx
movq %rdx, 0x368(%rsp)
leaq 0xd24c(%rip), %rsi      # 0x1a345
cmpb $0x0, (%rsi)
je 0xd11b
movq 0x8(%rbx), %rdx
shrq $0x3, %rdx
andq $-0x200, %rdx           # imm = 0xFE00
movzbl 0x5b(%rbx), %esi
addq %rsi, %rdx
addq $0x100, %rdx            # imm = 0x100
movq %rdx, 0x368(%rsp)
movq %rcx, 0x370(%rsp)
movq (%rbx), %rdx
movq %rdx, 0x390(%rsp)
movl 0x10(%rsp), %ebp
movl %ebp, 0x38(%rsp)
movl $0x0, 0x3c(%rsp)
movq %rcx, 0x398(%rsp)
movb $0x0, 0x357(%rsp)
movb 0x5b(%r15,%r14), %cl
movb %cl, 0x355(%rsp)
movb 0x5c(%r15,%r14), %cl
movb %cl, 0x356(%rsp)
leaq 0x73d4b(%rip), %rcx     # 0x80ec0
movq (%rcx,%rax,8), %rax
movq %rax, 0x3a0(%rsp)
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x5a80
cmpl $-0x2, %eax
jne 0xd19b
movl 0xc(%rsp), %edx
jmp 0xd1b7
movl %ebp, 0x964(%r13)
incl %ebp
xorl %ecx, %ecx
cmpl 0x950(%r13), %ebp
movl 0xc(%rsp), %edx
je 0xd1b5
movl %ebp, %ecx
movl %ecx, %ebp
subl $0x1, %edx
setb %cl
cmpl $-0x2, %eax
je 0xd1d9
testb %cl, %cl
je 0xd012
jmp 0xd1d9
cmpl 0x958(%r13), %ebp
jne 0xd3e0
movl 0x950(%r13), %eax
movl 0x968(%r13), %ecx
decl %eax
leal 0x1(%rcx), %r12d
xorl %edx, %edx
cmpl %eax, %ecx
cmovel %edx, %r12d
movq %r13, 0x10(%rsp)
leaq 0x34188(%r13), %rax
movq %rax, 0x20(%rsp)
movl $0x3, %esi
movq 0x10(%rsp), %rax
movq 0x9b8(%rax), %r14
movl %r12d, %eax
imulq $0x758, %rax, %r15      # imm = 0x758
cmpb $0x2, 0x4c(%r14,%r15)
jne 0xd3ce
leaq (%r14,%r15), %r13
movq 0x10(%rsp), %rax
movl (%rax), %ebp
leaq 0x73c81(%rip), %rax     # 0x80ec0
movq (%rax,%rbp,8), %rbx
cmpq %rbx, 0x40(%r13)
ja 0xd3ce
cmpl $0x0, 0x64(%r13)
jne 0xd3bc
movl %esi, 0x18(%rsp)
movl %edx, 0x2b(%rsp)
movl %edx, 0x28(%rsp)
movl $0xffff00, 0x354(%rsp) # imm = 0xFFFF00
leaq 0x30(%rsp), %rax
movl %r12d, 0xc(%rsp)
movq %rax, %r12
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%rax)
movl $0x314, %edx            # imm = 0x314
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x3130
movl $0x1, %eax
movl %eax, 0x358(%rsp)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x32c(%r12)
movdqu %xmm0, 0x33c(%r12)
xorl %ecx, %ecx
movl %ecx, 0x34c(%r12)
movdqu %xmm0, 0x358(%r12)
movq $0x0, 0x368(%r12)
movl 0xc(%rsp), %r12d
movq $-0x1, 0x3a0(%rsp)
movb $0x1, 0x28(%rsp)
movl %eax, 0x30(%rsp)
movl %ebp, 0x358(%rsp)
xorl %ebp, %ebp
movq 0x588(%r14,%r15), %rax
movq %rax, %rcx
shrq $0x6, %rcx
movq %rcx, 0x368(%rsp)
movq %rax, 0x378(%rsp)
movq %rax, 0x370(%rsp)
movl %r12d, 0x38(%rsp)
movl %ebp, 0x3c(%rsp)
movdqu (%r13), %xmm0
movdqu %xmm0, 0x390(%rsp)
movb $0x0, 0x357(%rsp)
movb 0x5b(%r14,%r15), %al
movb %al, 0x355(%rsp)
movb 0x5c(%r14,%r15), %al
movb %al, 0x356(%rsp)
movq %rbx, 0x3a0(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x5a80
cmpl $-0x2, %eax
je 0xd3a1
movq 0x10(%rsp), %rdx
movq 0x9b8(%rdx), %rcx
movl $0x1, 0x64(%rcx,%r15)
movl %r12d, 0x968(%rdx)
incl %r12d
cmpl 0x950(%rdx), %r12d
cmovel %ebp, %r12d
xorl %edx, %edx
movl 0x18(%rsp), %esi
subl $0x1, %esi
setb %cl
cmpl $-0x2, %eax
je 0xd3ce
testb %cl, %cl
je 0xd20b
jmp 0xd3ce
movq 0x10(%rsp), %rax
cmpl 0x958(%rax), %r12d
jne 0xd460
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xcbe9(%rip), %rdi      # 0x19fd0
leaq 0x798e(%rip), %rsi      # 0x14d7c
callq 0x3250
movq %rax, %rdi
movl %ebp, %esi
callq 0x32d0
leaq 0x7985(%rip), %rsi      # 0x14d89
movq %rax, %rdi
callq 0x3250
movl 0x958(%r13), %esi
movq %rax, %rdi
callq 0x32d0
leaq 0x7973(%rip), %rsi      # 0x14d95
movq %rax, %rdi
callq 0x3250
movl 0x95c(%r13), %esi
movq %rax, %rdi
callq 0x32d0
movq %rax, %rdi
callq 0x3110
leaq 0x6c1f(%rip), %rdi      # 0x14067
leaq 0x7867(%rip), %rsi      # 0x14cb6
leaq 0x794b(%rip), %rcx      # 0x14da1
movl $0x17b, %edx            # imm = 0x17B
callq 0x3160
movq 0xcb69(%rip), %rdi      # 0x19fd0
leaq 0x7954(%rip), %rsi      # 0x14dc2
callq 0x3250
movq %rax, %rdi
movl %r12d, %esi
callq 0x32d0
leaq 0x7904(%rip), %rsi      # 0x14d89
movq %rax, %rdi
callq 0x3250
movq 0x10(%rsp), %rbx
movl 0x958(%rbx), %esi
movq %rax, %rdi
callq 0x32d0
leaq 0x78ee(%rip), %rsi      # 0x14d95
movq %rax, %rdi
callq 0x3250
movl 0x95c(%rbx), %esi
movq %rax, %rdi
callq 0x32d0
movq %rax, %rdi
callq 0x3110
leaq 0x6b9b(%rip), %rdi      # 0x14067
leaq 0x77e3(%rip), %rsi      # 0x14cb6
leaq 0x78c7(%rip), %rcx      # 0x14da1
movl $0x1b9, %edx            # imm = 0x1B9
callq 0x3160