// File: NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::do_scheduling(unsigned int)
void O3_CPU::do_scheduling(uint32_t rob_index)
{
    ROB.entry[rob_index].reg_ready = 1; // reg_ready will be reset to 0 if there is RAW dependency 

    reg_dependency(rob_index);
    ROB.next_schedule = (rob_index == (ROB.SIZE - 1)) ? 0 : (rob_index + 1);

    if (ROB.entry[rob_index].is_memory)
        ROB.entry[rob_index].scheduled = INFLIGHT;
    else {
        ROB.entry[rob_index].scheduled = COMPLETED;

        // ADD LATENCY
        if (ROB.entry[rob_index].event_cycle < current_core_cycle[cpu])
            ROB.entry[rob_index].event_cycle = current_core_cycle[cpu] + SCHEDULING_LATENCY;
        else
            ROB.entry[rob_index].event_cycle += SCHEDULING_LATENCY;

        if (ROB.entry[rob_index].reg_ready) {

#ifdef SANITY_CHECK
            if (RTE1[RTE1_tail] < ROB_SIZE)
                assert(0);
#endif
            // remember this rob_index in the Ready-To-Execute array 1
            RTE1[RTE1_tail] = rob_index;

            DP (if (warmup_complete[cpu]) {
            cout << "[RTE1] " << __func__ << " instr_id: " << ROB.entry[rob_index].instr_id << " rob_index: " << rob_index << " is added to RTE1";
            cout << " head: " << RTE1_head << " tail: " << RTE1_tail << endl; }); 

            RTE1_tail++;
            if (RTE1_tail == ROB_SIZE)
                RTE1_tail = 0;
        }
    }
}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x20(%rsp)
movl %esi, 0x1c(%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, 0x10(%rsp)
movq 0x9b8(%rdi), %rax
movl 0x1c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movb $0x1, 0x59(%rax)
movl 0x1c(%rsp), %esi
callq 0x16ca0
movq 0x10(%rsp), %rcx
movl 0x1c(%rsp), %eax
movl 0x950(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x16af7
xorl %eax, %eax
movl %eax, 0xc(%rsp)
jmp 0x16b02
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
movq 0x10(%rsp), %rax
movl 0xc(%rsp), %ecx
movl %ecx, 0x980(%rax)
movq 0x9b8(%rax), %rax
movl 0x1c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x49(%rax)
je 0x16b52
movq 0x10(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x1c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movl $0x1, 0x68(%rax)
jmp 0x16c98
movq 0x10(%rsp), %rcx
movq 0x9b8(%rcx), %rax
movl 0x1c(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movl $0x2, 0x68(%rax)
movq 0x9b8(%rcx), %rax
movl 0x1c(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movq 0x40(%rax), %rax
movl (%rcx), %ecx
movl %ecx, %edx
leaq 0x79329(%rip), %rcx     # 0x8fec0
cmpq (%rcx,%rdx,8), %rax
jae 0x16bd5
movq 0x10(%rsp), %rax
movl (%rax), %ecx
movl %ecx, %edx
leaq 0x79313(%rip), %rcx     # 0x8fec0
movq (%rcx,%rdx,8), %rcx
movl 0x79319(%rip), %edx     # 0x8fed0
addq %rdx, %rcx
movq 0x9b8(%rax), %rax
movl 0x1c(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
movq %rcx, 0x40(%rax)
jmp 0x16bfd
movq 0x10(%rsp), %rax
movl 0x792f0(%rip), %ecx     # 0x8fed0
movq 0x9b8(%rax), %rax
movl 0x1c(%rsp), %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
addq %rdx, %rax
addq 0x40(%rax), %rcx
movq %rcx, 0x40(%rax)
movq 0x10(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x1c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpb $0x0, 0x59(%rax)
je 0x16c96
movq 0x10(%rsp), %rax
movl 0x324c(%rax), %ecx
cmpl $0x100, 0x2e48(%rax,%rcx,4) # imm = 0x100
jae 0x16c54
leaq 0xc3c8(%rip), %rdi      # 0x23004
leaq 0xcf72(%rip), %rsi      # 0x23bb5
movl $0x22a, %edx            # imm = 0x22A
leaq 0xd080(%rip), %rcx      # 0x23ccf
callq 0x3160
movq 0x10(%rsp), %rax
movl 0x1c(%rsp), %edx
movl 0x324c(%rax), %ecx
movl %edx, 0x2e48(%rax,%rcx,4)
movl 0x324c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x324c(%rax)
cmpl $0x100, 0x324c(%rax)    # imm = 0x100
jne 0x16c94
movq 0x10(%rsp), %rax
movl $0x0, 0x324c(%rax)
jmp 0x16c96
jmp 0x16c98
addq $0x28, %rsp
retq
nopl (%rax)