// File: 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:
subq $0x738, %rsp            # imm = 0x738
movq %rdi, 0x730(%rsp)
movq 0x730(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movl 0x964(%rcx), %eax
movl 0x950(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x160a7
xorl %eax, %eax
movl %eax, 0x4(%rsp)
jmp 0x160b9
movq 0x8(%rsp), %rax
movl 0x964(%rax), %eax
addl $0x1, %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x72c(%rsp)
movl $0x0, 0x728(%rsp)
cmpl $0x4, 0x728(%rsp)
jae 0x16436
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x72c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
jne 0x16106
jmp 0x16436
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x72c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x4c(%rax)
je 0x161da
movq 0x8(%rsp), %rcx
movl 0x72c(%rsp), %eax
cmpl 0x958(%rcx), %eax
jne 0x16146
jmp 0x16436
movq 0x12e83(%rip), %rdi     # 0x28fd0
leaq 0xdb27(%rip), %rsi      # 0x23c7b
callq 0x3220
movq %rax, %rdi
movl 0x72c(%rsp), %esi
callq 0x32c0
movq %rax, %rdi
leaq 0xdb16(%rip), %rsi      # 0x23c88
callq 0x3220
movq %rax, %rdi
movq 0x8(%rsp), %rax
movl 0x958(%rax), %esi
callq 0x32c0
movq %rax, %rdi
leaq 0xdb00(%rip), %rsi      # 0x23c94
callq 0x3220
movq %rax, %rdi
movq 0x8(%rsp), %rax
movl 0x95c(%rax), %esi
callq 0x32c0
movq %rax, %rdi
movq 0x12e02(%rip), %rsi     # 0x28fb8
callq 0x3250
leaq 0xce42(%rip), %rdi      # 0x23004
leaq 0xd9ec(%rip), %rsi      # 0x23bb5
movl $0x17b, %edx            # imm = 0x17B
leaq 0xdacb(%rip), %rcx      # 0x23ca0
callq 0x3160
leaq 0x3a8(%rsp), %rdi
callq 0x9750
movq 0x8(%rsp), %rax
movb $0x1, 0x3a8(%rsp)
movb $0x1, 0x3a9(%rsp)
movl $0x1, 0x3b0(%rsp)
movl (%rax), %ecx
movl %ecx, 0x6d8(%rsp)
movq 0x9b8(%rax), %rax
movl 0x72c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq $0xc, %rax
movq %rax, 0x6e8(%rsp)
leaq 0x13106(%rip), %rax     # 0x29345
cmpb $0x0, (%rax)
je 0x1629f
movq 0x8(%rsp), %rcx
movq 0x9b8(%rcx), %rax
movl 0x72c(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movq 0x8(%rax), %rax
shrq $0xc, %rax
shlq $0x9, %rax
movq 0x9b8(%rcx), %rcx
movl 0x72c(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rcx
movzbl 0x5b(%rcx), %ecx
addl $0x100, %ecx            # imm = 0x100
movslq %ecx, %rcx
orq %rcx, %rax
movq %rax, 0x6e8(%rsp)
jmp 0x162cc
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x72c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq $0xc, %rax
movq %rax, 0x6e8(%rsp)
movq 0x8(%rsp), %rdi
movq 0x9b8(%rdi), %rax
movl 0x72c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, 0x6f0(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x72c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movq (%rax), %rax
movq %rax, 0x710(%rsp)
movl 0x72c(%rsp), %eax
movl %eax, 0x3b8(%rsp)
movl $0x0, 0x3bc(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x72c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, 0x718(%rsp)
movb $0x0, 0x6d7(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x72c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movb 0x5b(%rax), %al
movb %al, 0x6d5(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x72c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movb 0x5c(%rax), %al
movb %al, 0x6d6(%rsp)
movl (%rdi), %eax
movl %eax, %ecx
leaq 0x79b14(%rip), %rax     # 0x8fec0
movq (%rax,%rcx,8), %rax
movq %rax, 0x720(%rsp)
addq $0x3690, %rdi           # imm = 0x3690
leaq 0x3a8(%rsp), %rsi
callq 0x81e0
movl %eax, 0x3a4(%rsp)
cmpl $-0x2, 0x3a4(%rsp)
jne 0x163df
jmp 0x16436
movq 0x8(%rsp), %rcx
movl 0x72c(%rsp), %eax
movl %eax, 0x964(%rcx)
movl 0x72c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x72c(%rsp)
movl 0x72c(%rsp), %eax
cmpl 0x950(%rcx), %eax
jne 0x1641c
movl $0x0, 0x72c(%rsp)
jmp 0x1641e
jmp 0x16420
movl 0x728(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x728(%rsp)
jmp 0x160cf
movq 0x8(%rsp), %rcx
movl 0x968(%rcx), %eax
movl 0x950(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x16455
xorl %eax, %eax
movl %eax, (%rsp)
jmp 0x16466
movq 0x8(%rsp), %rax
movl 0x968(%rax), %eax
addl $0x1, %eax
movl %eax, (%rsp)
movl (%rsp), %eax
movl %eax, 0x3a0(%rsp)
movl $0x0, 0x39c(%rsp)
cmpl $0x4, 0x39c(%rsp)
jae 0x167a9
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x3a0(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movzbl 0x4c(%rax), %eax
cmpl $0x2, %eax
jne 0x164e1
movq 0x8(%rsp), %rcx
movq 0x9b8(%rcx), %rax
movl 0x3a0(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movq 0x40(%rax), %rax
movl (%rcx), %ecx
movl %ecx, %edx
leaq 0x799e5(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
jbe 0x164e6
jmp 0x167a9
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x3a0(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x0, 0x64(%rax)
je 0x165ba
movq 0x8(%rsp), %rcx
movl 0x3a0(%rsp), %eax
cmpl 0x958(%rcx), %eax
jne 0x16526
jmp 0x167a9
movq 0x12aa3(%rip), %rdi     # 0x28fd0
leaq 0xd78d(%rip), %rsi      # 0x23cc1
callq 0x3220
movq %rax, %rdi
movl 0x3a0(%rsp), %esi
callq 0x32c0
movq %rax, %rdi
leaq 0xd736(%rip), %rsi      # 0x23c88
callq 0x3220
movq %rax, %rdi
movq 0x8(%rsp), %rax
movl 0x958(%rax), %esi
callq 0x32c0
movq %rax, %rdi
leaq 0xd720(%rip), %rsi      # 0x23c94
callq 0x3220
movq %rax, %rdi
movq 0x8(%rsp), %rax
movl 0x95c(%rax), %esi
callq 0x32c0
movq %rax, %rdi
movq 0x12a22(%rip), %rsi     # 0x28fb8
callq 0x3250
leaq 0xca62(%rip), %rdi      # 0x23004
leaq 0xd60c(%rip), %rsi      # 0x23bb5
movl $0x1b9, %edx            # imm = 0x1B9
leaq 0xd6eb(%rip), %rcx      # 0x23ca0
callq 0x3160
leaq 0x18(%rsp), %rdi
callq 0x9750
movq 0x8(%rsp), %rdi
movb $0x1, 0x18(%rsp)
movl $0x1, 0x20(%rsp)
movl (%rdi), %eax
movl %eax, 0x348(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x3a0(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movq 0x588(%rax), %rax
shrq $0x6, %rax
movq %rax, 0x358(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x3a0(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movq 0x588(%rax), %rax
movq %rax, 0x368(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x3a0(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movq 0x588(%rax), %rax
movq %rax, 0x360(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x3a0(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movq (%rax), %rax
movq %rax, 0x380(%rsp)
movl 0x3a0(%rsp), %eax
movl %eax, 0x28(%rsp)
movl $0x0, 0x2c(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x3a0(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, 0x388(%rsp)
movb $0x0, 0x347(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x3a0(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movb 0x5b(%rax), %al
movb %al, 0x345(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x3a0(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movb 0x5c(%rax), %al
movb %al, 0x346(%rsp)
movl (%rdi), %eax
movl %eax, %ecx
leaq 0x797b7(%rip), %rax     # 0x8fec0
movq (%rax,%rcx,8), %rax
movq %rax, 0x390(%rsp)
addq $0x34188, %rdi          # imm = 0x34188
leaq 0x18(%rsp), %rsi
callq 0x81e0
movl %eax, 0x14(%rsp)
cmpl $-0x2, 0x14(%rsp)
jne 0x16733
jmp 0x167a9
movq 0x8(%rsp), %rcx
movq 0x9b8(%rcx), %rax
movl 0x3a0(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movl $0x1, 0x64(%rax)
movl 0x3a0(%rsp), %eax
movl %eax, 0x968(%rcx)
movl 0x3a0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3a0(%rsp)
movl 0x3a0(%rsp), %eax
cmpl 0x950(%rcx), %eax
jne 0x1678f
movl $0x0, 0x3a0(%rsp)
jmp 0x16791
jmp 0x16793
movl 0x39c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x39c(%rsp)
jmp 0x1647b
addq $0x738, %rsp            # imm = 0x738
retq
nopw %cs:(%rax,%rax)