// File: /workspace/github_cpp_makefile_star2_O2/NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::add_store_queue(unsigned int, unsigned int)
void O3_CPU::add_store_queue(uint32_t rob_index, uint32_t data_index)
{
    uint32_t sq_index = SQ.tail;
#ifdef SANITY_CHECK
    if (SQ.entry[sq_index].virtual_address)
        assert(0);
#endif

    /*
    // search for an empty slot 
    uint32_t sq_index = SQ.SIZE;
    for (uint32_t i=0; i<SQ.SIZE; i++) {
        if (SQ.entry[i].virtual_address == 0) {
            sq_index = i;
            break;
        }
    }

    // sanity check
    if (sq_index == SQ.SIZE) {
        cerr << "instr_id: " << ROB.entry[rob_index].instr_id << " no empty slot in the store queue!!!" << endl;
        assert(0);
    }
    */

    // add it to the store queue
    ROB.entry[rob_index].sq_index[data_index] = sq_index;
    SQ.entry[sq_index].instr_id = ROB.entry[rob_index].instr_id;
    SQ.entry[sq_index].virtual_address = ROB.entry[rob_index].destination_memory[data_index];
    SQ.entry[sq_index].ip = ROB.entry[rob_index].ip;
    SQ.entry[sq_index].data_index = data_index;
    SQ.entry[sq_index].rob_index = rob_index;
    SQ.entry[sq_index].asid[0] = ROB.entry[rob_index].asid[0];
    SQ.entry[sq_index].asid[1] = ROB.entry[rob_index].asid[1];
    SQ.entry[sq_index].event_cycle = current_core_cycle[cpu] + SCHEDULING_LATENCY;

    SQ.occupancy++;
    SQ.tail++;
    if (SQ.tail == SQ.SIZE)
        SQ.tail = 0;

    // succesfully added to the store queue
    ROB.entry[rob_index].destination_added[data_index] = 1;
    
    STA[STA_head] = UINT64_MAX;
    STA_head++;
    if (STA_head == STA_SIZE)
        STA_head = 0;

    RTS0[RTS0_tail] = sq_index;
    RTS0_tail++;
    if (RTS0_tail == SQ_SIZE)
        RTS0_tail = 0;

    DP(if(warmup_complete[cpu]) {
    cout << "[SQ] " << __func__ << " instr_id: " << SQ.entry[sq_index].instr_id;
    cout << " is added in the SQ translated: " << +SQ.entry[sq_index].translated << " fetched: " << +SQ.entry[sq_index].fetched << " is_producer: " << +ROB.entry[rob_index].is_producer;
    cout << " cycle: " << current_core_cycle[cpu] << endl; });
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movl 0xa24(%rdi), %eax
movq 0xa28(%rdi), %rcx
imulq $0x140, %rax, %r9       # imm = 0x140
cmpq $0x0, 0x10(%rcx,%r9)
jne 0xbb8d
leaq (%rcx,%r9), %r10
addq $0x10, %r10
movq 0x9b8(%rdi), %r11
movl %esi, %r8d
movl %edx, %ecx
imulq $0x758, %r8, %r8        # imm = 0x758
leaq (%r11,%r8), %rbx
movl %eax, 0x738(%rbx,%rcx,4)
movq (%r11,%r8), %r14
movq %r14, -0x10(%r10)
movq 0x5a8(%rbx,%rcx,8), %rbx
movq %rbx, (%r10)
movq 0x8(%r11,%r8), %rbx
movq %rbx, 0x10(%r10)
movl %edx, 0x24(%r10)
movl %esi, 0x20(%r10)
movb 0x5b(%r11,%r8), %dl
movb %dl, 0x2e(%r10)
movq 0x9b8(%rdi), %rdx
movq 0xa28(%rdi), %rsi
movb 0x5c(%rdx,%r8), %dl
movb %dl, 0x3f(%rsi,%r9)
movl (%rdi), %edx
leaq 0x713c0(%rip), %rsi     # 0x7ceb0
movl 0x713c9(%rip), %r10d    # 0x7cec0
addq (%rsi,%rdx,8), %r10
movq 0xa28(%rdi), %rdx
movq %r10, 0x28(%rdx,%r9)
incl 0xa1c(%rdi)
movl 0xa24(%rdi), %edx
incl %edx
xorl %esi, %esi
cmpl 0xa18(%rdi), %edx
cmovel %esi, %edx
movl %edx, 0xa24(%rdi)
addq 0x9b8(%rdi), %r8
movb $0x1, 0x52(%rcx,%r8)
movq 0x2a30(%rdi), %rcx
orq $-0x1, 0xa30(%rdi,%rcx,8)
movq 0x2a30(%rdi), %rcx
incq %rcx
cmpq $0x400, %rcx            # imm = 0x400
movl $0x0, %edx
cmovneq %rcx, %rdx
movq %rdx, 0x2a30(%rdi)
movl 0x3584(%rdi), %ecx
movl %eax, 0x34a0(%rdi,%rcx,4)
movl 0x3584(%rdi), %eax
incl %eax
cmpl $0x38, %eax
cmovel %esi, %eax
movl %eax, 0x3584(%rdi)
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0x44d3(%rip), %rdi      # 0x10067
leaq 0x5053(%rip), %rsi      # 0x10bee
leaq 0x520c(%rip), %rcx      # 0x10dae
movl $0x410, %edx            # imm = 0x410
callq 0x2160