// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
movl 0x950(%rdi), %eax
movl 0x964(%rdi), %ecx
leal -0x1(%rax), %edx
leal 0x1(%rcx), %ebp
xorl %esi, %esi
cmpl %edx, %ecx
cmovel %esi, %ebp
leaq 0x3690(%rdi), %rcx
movq %rcx, 0x10(%rsp)
movl $0x4, %edx
movq 0x9b8(%r12), %r15
movl %ebp, %ecx
imulq $0x758, %rcx, %r14      # imm = 0x758
cmpq $0x0, 0x8(%r15,%r14)
je 0xd1a5
leaq (%r15,%r14), %r13
cmpb $0x0, 0x4c(%r13)
jne 0xd10d
movl %edx, 0x8(%rsp)
xorl %eax, %eax
movl %eax, 0x2b(%rsp)
movl %eax, 0x28(%rsp)
movl $0xffff00, 0x354(%rsp) # imm = 0xFFFF00
leaq 0x30(%rsp), %rbx
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%rbx)
movl $0x314, %edx            # imm = 0x314
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x3130
pxor %xmm0, %xmm0
movdqu %xmm0, 0x32c(%rbx)
movdqu %xmm0, 0x33c(%rbx)
xorl %eax, %eax
movl %eax, 0x34c(%rbx)
movdqu %xmm0, 0x358(%rbx)
movq $0x0, 0x368(%rbx)
movq $-0x1, 0x3a0(%rsp)
movw $0x101, 0x28(%rsp)      # imm = 0x101
movl $0x1, 0x30(%rsp)
movl (%r12), %eax
movl %eax, 0x358(%rsp)
movq 0x8(%r15,%r14), %rcx
movq %rcx, %rdx
shrq $0xc, %rdx
movq %rdx, 0x368(%rsp)
leaq 0xd302(%rip), %rsi      # 0x1a345
cmpb $0x0, (%rsi)
je 0xd065
movq %rcx, %rdx
shrq $0x3, %rdx
andq $-0x200, %rdx           # imm = 0xFE00
movzbl 0x5b(%r13), %esi
addq %rsi, %rdx
addq $0x100, %rdx            # imm = 0x100
movq %rdx, 0x368(%rsp)
movq %rcx, 0x370(%rsp)
movq (%r13), %rdx
movq %rdx, 0x390(%rsp)
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 0x73e04(%rip), %rcx     # 0x80ec0
movq (%rcx,%rax,8), %rax
movq %rax, 0x3a0(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x5ab4
cmpl $-0x2, %eax
movl 0x8(%rsp), %edx
je 0xd19d
movl %ebp, 0x964(%r12)
incl %ebp
movl 0x950(%r12), %eax
cmpl %eax, %ebp
movl $0x0, %ecx
cmovel %ecx, %ebp
decl %edx
jne 0xcf70
jmp 0xd1a5
cmpl 0x958(%r12), %ebp
je 0xd1a5
movq 0xceae(%rip), %rdi      # 0x19fd0
leaq 0x7c83(%rip), %rsi      # 0x14dac
callq 0x3250
movq %rax, %rdi
movl %ebp, %esi
callq 0x32d0
leaq 0x7c7a(%rip), %rsi      # 0x14db9
movq %rax, %rdi
callq 0x3250
movl 0x958(%r12), %esi
movq %rax, %rdi
callq 0x32d0
leaq 0x7c67(%rip), %rsi      # 0x14dc5
movq %rax, %rdi
callq 0x3250
movl 0x95c(%r12), %esi
movq %rax, %rdi
callq 0x32d0
movq %rax, %rdi
callq 0x3110
leaq 0x6ee2(%rip), %rdi      # 0x14067
leaq 0x7b5a(%rip), %rsi      # 0x14ce6
leaq 0x7c3e(%rip), %rcx      # 0x14dd1
movl $0x17b, %edx            # imm = 0x17B
callq 0x3160
movl 0x950(%r12), %eax
movl 0x968(%r12), %ecx
decl %eax
leal 0x1(%rcx), %ebp
xorl %edx, %edx
cmpl %eax, %ecx
cmovel %edx, %ebp
leaq 0x34188(%r12), %rax
movq %rax, 0x20(%rsp)
movq %r12, 0x8(%rsp)
movq 0x9b8(%r12), %rdx
movl $0x4, %ecx
movl %ebp, %eax
imulq $0x758, %rax, %r14      # imm = 0x758
cmpb $0x2, 0x4c(%rdx,%r14)
jne 0xd378
leaq (%rdx,%r14), %rbx
movq 0x8(%rsp), %rax
movl (%rax), %r12d
leaq 0x73cc0(%rip), %rax     # 0x80ec0
movq (%rax,%r12,8), %r13
cmpq %r13, 0x40(%rbx)
ja 0xd378
cmpl $0x0, 0x64(%rbx)
jne 0xd36b
movl %ecx, 0x1c(%rsp)
movw $0xff00, 0x354(%rsp)    # imm = 0xFF00
xorl %eax, %eax
movl %eax, 0x28(%rsp)
movl %eax, 0x2b(%rsp)
movb $-0x1, 0x356(%rsp)
leaq 0x30(%rsp), %r15
movl %ebp, 0x10(%rsp)
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%r15)
movq %rdx, %rbp
movl $0x314, %edx            # imm = 0x314
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
callq 0x3130
pxor %xmm0, %xmm0
movdqu %xmm0, 0x32c(%r15)
movdqu %xmm0, 0x33c(%r15)
xorl %edx, %edx
movl %edx, 0x34c(%r15)
movdqu %xmm0, 0x358(%r15)
movq $0x0, 0x368(%r15)
movl 0x10(%rsp), %r15d
movq $-0x1, 0x3a0(%rsp)
movb $0x1, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movl %r12d, 0x358(%rsp)
movq 0x588(%rbp,%r14), %rax
movq %rax, %rcx
shrq $0x6, %rcx
movq %rcx, 0x368(%rsp)
movq %rax, 0x378(%rsp)
movq %rax, 0x370(%rsp)
movl %r15d, 0x38(%rsp)
movl %edx, 0x3c(%rsp)
movdqu (%rbx), %xmm0
movdqu %xmm0, 0x390(%rsp)
movb $0x0, 0x357(%rsp)
movb 0x5b(%rbp,%r14), %al
movb %al, 0x355(%rsp)
movb 0x5c(%rbp,%r14), %al
movb %al, 0x356(%rsp)
movq %r13, 0x3a0(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x5ab4
cmpl $-0x2, %eax
je 0xd378
movq 0x8(%rsp), %rcx
movq 0x9b8(%rcx), %rdx
movl $0x1, 0x64(%rdx,%r14)
movl %r15d, 0x968(%rcx)
incl %r15d
xorl %ebp, %ebp
cmpl 0x950(%rcx), %r15d
movl 0x1c(%rsp), %ecx
je 0xd361
movl %r15d, %ebp
decl %ecx
jne 0xd1d8
jmp 0xd378
movq 0x8(%rsp), %rax
cmpl 0x958(%rax), %ebp
jne 0xd38a
addq $0x3a8, %rsp            # imm = 0x3A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xcc3f(%rip), %rdi      # 0x19fd0
leaq 0x7a5a(%rip), %rsi      # 0x14df2
callq 0x3250
movq %rax, %rdi
movl %ebp, %esi
callq 0x32d0
leaq 0x7a0b(%rip), %rsi      # 0x14db9
movq %rax, %rdi
callq 0x3250
movq 0x8(%rsp), %rbx
movl 0x958(%rbx), %esi
movq %rax, %rdi
callq 0x32d0
leaq 0x79f5(%rip), %rsi      # 0x14dc5
movq %rax, %rdi
callq 0x3250
movl 0x95c(%rbx), %esi
movq %rax, %rdi
callq 0x32d0
movq %rax, %rdi
callq 0x3110
leaq 0x6c72(%rip), %rdi      # 0x14067
leaq 0x78ea(%rip), %rsi      # 0x14ce6
leaq 0x79ce(%rip), %rcx      # 0x14dd1
movl $0x1b9, %edx            # imm = 0x1B9
callq 0x3160
nop