// File: /workspace/github_cpp_makefile_star2_O1/NishadSaraf[P]Championship-Simulator/src/drc_controller.cc
// Function: DRC_MEMORY_CONTROLLER::add_rq(PACKET*)
int DRC_MEMORY_CONTROLLER::add_rq(PACKET *packet)
{
    // check for the latest wirtebacks in the write queue
    //int wq_index = WQ.check_queue(packet);
    int wq_index = check_drc_queue(&WQ, packet);
    if (wq_index != -1) {
        
        // check fill level
        if (packet->fill_level < fill_level) {

            packet->data = WQ.entry[wq_index].data;
            if (packet->instruction) 
                upper_level_icache[packet->cpu]->return_data(packet);
            else // data
                upper_level_dcache[packet->cpu]->return_data(packet);
        }

        DP ( if (packet->cpu) {
        cout << "[" << NAME << "_RQ] " << __func__ << " instr_id: " << packet->instr_id << " found recent writebacks";
        cout << hex << " read: " << packet->address << " writeback: " << WQ.entry[wq_index].address << dec << endl; });

        ACCESS[1]++;
        HIT[1]++;

        WQ.FORWARD++;
        RQ.ACCESS++;
        //assert(0);

        return -1;
    }

    // check for duplicates in the read queue
    int index = check_drc_queue(&RQ, packet);
    if (index != -1)
        return index; // merged index

    // search for the empty index
    for (index=0; index<DRC_RQ_SIZE; index++) {
        if (RQ.entry[index].address == 0) {
            
            memcpy(&RQ.entry[index], packet, sizeof(PACKET));
            RQ.occupancy++;

#ifdef DEBUG_PRINT
            uint32_t channel = drc_get_channel(packet->address),
                     rank = drc_get_rank(packet->address),
                     bank = drc_get_bank(packet->address),
                     row = drc_get_row(packet->address),
                     column = drc_get_column(packet->address); 
#endif

            DP ( if(warmup_complete[packet->cpu]) {
            cout << "[" << NAME << "_RQ] " <<  __func__ << " instr_id: " << packet->instr_id << " address: " << hex << packet->address;
            cout << " full_addr: " << packet->full_addr << dec << " ch: " << channel;
            cout << " rank: " << rank << " bank: " << bank << " row: " << row << " col: " << column;
            cout << " occupancy: " << RQ.occupancy << " current: " << current_core_cycle[packet->cpu] << " event: " << packet->event_cycle << endl; });

            break;
        }
    }

    update_schedule_cycle(&RQ);

    return -1;
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl 0x40a890(%rdi), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0x1310e
movq 0x340(%rsi), %rdx
movl $0x340, %edi            # imm = 0x340
addq 0x40a908(%rbx), %rdi
xorl %r8d, %r8d
cmpq %rdx, (%rdi)
je 0x1310b
incq %r8
addq $0x380, %rdi            # imm = 0x380
cmpq %r8, %rcx
jne 0x130f5
jmp 0x1310e
movl %r8d, %eax
cmpl $-0x1, %eax
je 0x1317d
movl 0x8(%rsi), %ecx
cmpl 0x40946c(%rbx), %ecx
jge 0x1315a
movq 0x40a908(%rbx), %rcx
cltq
imulq $0x380, %rax, %rax      # imm = 0x380
movq 0x360(%rcx,%rax), %rax
movq %rax, 0x360(%rsi)
xorl %eax, %eax
cmpb $0x0, (%rsi)
sete %al
movl 0x330(%rsi), %ecx
leaq (%rbx,%rax,8), %rax
movq 0x8(%rax,%rcx,8), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
incq 0x72b0(%rbx)
incq 0x72d0(%rbx)
incq 0x40a8d8(%rbx)
incq 0x40c570(%rbx)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x131c1
movl 0x40c530(%rbx), %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testq %rcx, %rcx
je 0x131bc
movq 0x340(%rsi), %rdx
movl $0x340, %edi            # imm = 0x340
addq 0x40c5a8(%rbx), %rdi
xorl %r8d, %r8d
cmpq %rdx, (%rdi)
je 0x131b9
incq %r8
addq $0x380, %rdi            # imm = 0x380
cmpq %r8, %rcx
jne 0x131a3
jmp 0x131bc
movl %r8d, %eax
cmpl $-0x1, %eax
je 0x131c9
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0x40c510(%rbx), %r14
movq 0x40c5a8(%rbx), %rdi
xorl %eax, %eax
cmpq $0x0, 0x340(%rdi,%rax)
je 0x131f4
addq $0x380, %rax            # imm = 0x380
cmpq $0xa800, %rax           # imm = 0xA800
jne 0x131d9
jmp 0x13207
addq %rax, %rdi
movl $0x380, %edx            # imm = 0x380
callq 0x31c0
incl 0x40c544(%rbx)
movq %r14, %rsi
callq 0x11bc8
jmp 0x13176