// File: /workspace/github_cpp_makefile_star2_O2/NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::complete_data_fetch(PACKET_QUEUE*, unsigned char)
void O3_CPU::complete_data_fetch(PACKET_QUEUE *queue, uint8_t is_it_tlb)
{
    uint32_t index = queue->head,
             rob_index = queue->entry[index].rob_index,
             sq_index = queue->entry[index].sq_index,
             lq_index = queue->entry[index].lq_index;

#ifdef SANITY_CHECK
    if (queue->entry[index].type != RFO) {
        if (rob_index != check_rob(queue->entry[index].instr_id))
            assert(0);
    }
#endif

    // update ROB entry
    if (is_it_tlb) { // DTLB

        if (queue->entry[index].type == RFO) {
            SQ.entry[sq_index].physical_address = (queue->entry[index].data_pa << PAGE_SHIFT) | (SQ.entry[sq_index].virtual_address & ((1 << PAGE_SHIFT) - 1)); // translated address
            SQ.entry[sq_index].translated = COMPLETED;
            SQ.entry[sq_index].event_cycle = current_core_cycle[cpu];

            RTS1[RTS1_tail] = sq_index;
            RTS1_tail++;
            if (RTS1_tail == SQ_SIZE)
                RTS1_tail = 0;

            DP (if (warmup_complete[cpu]) {
            cout << "[ROB] " << __func__ << " RFO instr_id: " << SQ.entry[sq_index].instr_id;
            cout << " DTLB_FETCH_DONE translation: " << +SQ.entry[sq_index].translated << hex << " page: " << (SQ.entry[sq_index].physical_address>>LOG2_PAGE_SIZE);
            cout << " full_addr: " << SQ.entry[sq_index].physical_address << dec << " store_merged: " << +queue->entry[index].store_merged;
            cout << " load_merged: " << +queue->entry[index].load_merged << endl; }); 

            handle_merged_translation(&queue->entry[index]);
        }
        else { 
            LQ.entry[lq_index].physical_address = (queue->entry[index].data_pa << PAGE_SHIFT) | (LQ.entry[lq_index].virtual_address & ((1 << PAGE_SHIFT) - 1)); // translated address
            LQ.entry[lq_index].translated = COMPLETED;
            LQ.entry[lq_index].event_cycle = current_core_cycle[cpu];

            RTL1[RTL1_tail] = lq_index;
            RTL1_tail++;
            if (RTL1_tail == LQ_SIZE)
                RTL1_tail = 0;

            DP (if (warmup_complete[cpu]) {
            cout << "[RTL1] " << __func__ << " instr_id: " << LQ.entry[lq_index].instr_id << " rob_index: " << LQ.entry[lq_index].rob_index << " is added to RTL1";
            cout << " head: " << RTL1_head << " tail: " << RTL1_tail << endl; }); 

            DP (if (warmup_complete[cpu]) {
            cout << "[ROB] " << __func__ << " load instr_id: " << LQ.entry[lq_index].instr_id;
            cout << " DTLB_FETCH_DONE translation: " << +LQ.entry[lq_index].translated << hex << " page: " << (LQ.entry[lq_index].physical_address>>LOG2_PAGE_SIZE);
            cout << " full_addr: " << LQ.entry[lq_index].physical_address << dec << " store_merged: " << +queue->entry[index].store_merged;
            cout << " load_merged: " << +queue->entry[index].load_merged << endl; }); 

            handle_merged_translation(&queue->entry[index]);
        }

        ROB.entry[rob_index].event_cycle = queue->entry[index].event_cycle;
    }
    else { // L1D

        if (queue->entry[index].type == RFO)
            handle_merged_load(&queue->entry[index]);
        else { 
#ifdef SANITY_CHECK
            if (queue->entry[index].store_merged)
                assert(0);
#endif
            LQ.entry[lq_index].fetched = COMPLETED;
            LQ.entry[lq_index].event_cycle = current_core_cycle[cpu];
            ROB.entry[rob_index].num_mem_ops--;
            ROB.entry[rob_index].event_cycle = queue->entry[index].event_cycle;

#ifdef SANITY_CHECK
            if (ROB.entry[rob_index].num_mem_ops < 0) {
                cerr << "instr_id: " << ROB.entry[rob_index].instr_id << endl;
                assert(0);
            }
#endif
            if (ROB.entry[rob_index].num_mem_ops == 0)
                inflight_mem_executions++;

            DP (if (warmup_complete[cpu]) {
            cout << "[ROB] " << __func__ << " load instr_id: " << LQ.entry[lq_index].instr_id;
            cout << " L1D_FETCH_DONE fetched: " << +LQ.entry[lq_index].fetched << hex << " address: " << (LQ.entry[lq_index].physical_address>>LOG2_BLOCK_SIZE);
            cout << " full_addr: " << LQ.entry[lq_index].physical_address << dec << " remain_mem_ops: " << ROB.entry[rob_index].num_mem_ops;
            cout << " load_merged: " << +queue->entry[index].load_merged << " inflight_mem: " << inflight_mem_executions << endl; }); 

            release_load_queue(lq_index);
            handle_merged_load(&queue->entry[index]);
        }
    }

    // remove this entry
    queue->remove_queue(&queue->entry[index]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r15
movl 0x2c(%rsi), %ecx
movq 0x98(%rsi), %rax
imulq $0x380, %rcx, %r14      # imm = 0x380
movl 0x10(%rax,%r14), %r12d
movl 0x33c(%rax,%r14), %ebp
movl 0x338(%rax,%r14), %r9d
cmpb $0x1, 0x32f(%rax,%r14)
je 0xc5a9
addq %r14, %rax
movq 0x368(%rax), %rsi
movq %r15, %rdi
movq %r9, %r13
movq %r15, (%rsp)
movq %rbx, %r15
movl %edx, %ebx
callq 0xa426
movl %ebx, %edx
movq %r15, %rbx
movq (%rsp), %r15
movq %r13, %r9
cmpl %eax, %r12d
jne 0xc76c
movq 0x98(%rbx), %rax
leaq (%rax,%r14), %rsi
movb 0x32f(%rax,%r14), %cl
testb %dl, %dl
je 0xc62a
movq 0x358(%rsi), %rax
shlq $0xc, %rax
cmpb $0x1, %cl
jne 0xc6c3
movq 0xa28(%r15), %rcx
imulq $0x140, %rbp, %rdx      # imm = 0x140
movq 0x10(%rcx,%rdx), %rdi
andl $0xfff, %edi            # imm = 0xFFF
orq %rax, %rdi
movq %rdi, 0x18(%rcx,%rdx)
movb $0x2, 0x3c(%rcx,%rdx)
movl (%r15), %eax
leaq 0x708ac(%rip), %rdi     # 0x7ceb0
movq (%rdi,%rax,8), %rax
movq %rax, 0x28(%rcx,%rdx)
leaq 0x366c(%r15), %rax
movl 0x366c(%r15), %ecx
movl %ebp, 0x3588(%r15,%rcx,4)
pushq $0x38
jmp 0xc714
cmpb $0x1, %cl
je 0xc6b6
cmpb $0x0, 0x32b(%rsi)
jne 0xc78b
movq 0x9f0(%r15), %rcx
imulq $0x140, %r9, %rdx       # imm = 0x140
movb $0x2, 0x3d(%rcx,%rdx)
movl (%r15), %esi
leaq 0x70853(%rip), %rdi     # 0x7ceb0
movq (%rdi,%rsi,8), %rsi
movq %rsi, 0x28(%rcx,%rdx)
movq 0x9b8(%r15), %rdx
imulq $0x758, %r12, %r13      # imm = 0x758
movl 0x70(%rdx,%r13), %esi
leal -0x1(%rsi), %ecx
movl %ecx, 0x70(%rdx,%r13)
movq 0x378(%rax,%r14), %rax
movq %rax, 0x40(%rdx,%r13)
testl %esi, %esi
jle 0xc7aa
testl %ecx, %ecx
jne 0xc6a1
incl 0x924(%r15)
movq %r15, %rdi
movl %r9d, %esi
callq 0xbc36
movq 0x98(%rbx), %rsi
addq %r14, %rsi
movq %r15, %rdi
callq 0xc92c
jmp 0xc74c
movq 0x9f0(%r15), %rcx
imulq $0x140, %r9, %rdx       # imm = 0x140
movq 0x10(%rcx,%rdx), %rdi
andl $0xfff, %edi            # imm = 0xFFF
orq %rax, %rdi
movq %rdi, 0x18(%rcx,%rdx)
movb $0x2, 0x3c(%rcx,%rdx)
movl (%r15), %eax
leaq 0x707bd(%rip), %rdi     # 0x7ceb0
movq (%rdi,%rax,8), %rax
movq %rax, 0x28(%rcx,%rdx)
leaq 0x349c(%r15), %rax
movl 0x349c(%r15), %ecx
movl %r9d, 0x3378(%r15,%rcx,4)
pushq $0x48
popq %rcx
movl (%rax), %edx
incl %edx
xorl %edi, %edi
cmpl %ecx, %edx
cmovnel %edx, %edi
movl %edi, (%rax)
movq %r15, %rdi
callq 0xc7f8
movq 0x98(%rbx), %rax
movq 0x378(%rax,%r14), %rax
movq 0x9b8(%r15), %rcx
imulq $0x758, %r12, %rdx      # imm = 0x758
movq %rax, 0x40(%rcx,%rdx)
addq 0x98(%rbx), %r14
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9cc0
leaq 0x38f4(%rip), %rdi      # 0x10067
leaq 0x4474(%rip), %rsi      # 0x10bee
leaq 0x4745(%rip), %rcx      # 0x10ec6
movl $0x61b, %edx            # imm = 0x61B
callq 0x2160
leaq 0x38d5(%rip), %rdi      # 0x10067
leaq 0x4455(%rip), %rsi      # 0x10bee
leaq 0x4726(%rip), %rcx      # 0x10ec6
movl $0x654, %edx            # imm = 0x654
callq 0x2160
movq 0x983f(%rip), %rdi      # 0x15ff0
leaq 0x46a1(%rip), %rsi      # 0x10e59
callq 0x2230
movq 0x9b8(%r15), %rcx
movq (%rcx,%r13), %rsi
movq %rax, %rdi
callq 0x2080
movq %rax, %rdi
callq 0x2110
leaq 0x3888(%rip), %rdi      # 0x10067
leaq 0x4408(%rip), %rsi      # 0x10bee
leaq 0x46d9(%rip), %rcx      # 0x10ec6
movl $0x65e, %edx            # imm = 0x65E
callq 0x2160
nop