// File: /workspace/github_cpp_makefile_star2_O3/NishadSaraf[P]Championship-Simulator/src/drc_controller.cc
// Function: DRC_MEMORY_CONTROLLER::schedule(PACKET_QUEUE*)
void DRC_MEMORY_CONTROLLER::schedule(PACKET_QUEUE *queue)
{
    uint64_t drc_addr;
    uint32_t drc_channel, drc_rank, drc_bank, drc_row;
    uint8_t  row_buffer_hit = 0;

    int oldest_index = -1;
    uint64_t oldest_cycle = UINT64_MAX;

    // first, search for the oldest open row hit
    for (uint32_t i=0; i<queue->SIZE; i++) {

        // empty entry
        drc_addr = queue->entry[i].address;
        if (drc_addr == 0)
            continue;

        // already scheduled
        if (queue->entry[i].scheduled) { 
            //cout << queue->NAME << " index: " << i << " is already scheduled" << endl;
            continue;
        }

        drc_channel = drc_get_channel(drc_addr);
        drc_rank = drc_get_rank(drc_addr);
        drc_bank = drc_get_bank(drc_addr);

        // bank is busy
        if (bank_request[drc_channel][drc_rank][drc_bank].working) {

            //DP ( if (warmup_complete[0]) {
            //cout << queue->NAME << " " << __func__ << " instr_id: " << queue->entry[i].instr_id << " bank is busy";
            //cout << " swrites: " << scheduled_writes << " sreads: " << scheduled_reads;
            //cout << " write: " << +bank_request[drc_channel][drc_rank][drc_bank].is_write << " read: " << +bank_request[drc_channel][drc_rank][drc_bank].is_read << hex;
            //cout << " address: " << queue->entry[i].address << dec << " channel: " << drc_channel << " rank: " << drc_rank << " bank: " << drc_bank << endl; });
            //cout << queue->NAME << " index: " << i << " rank: " << drc_rank << " bank: " << drc_bank << " is busy" << endl;

            continue;
        }

        // check open row
        drc_row = drc_get_row(drc_addr);
        if (bank_request[drc_channel][drc_rank][drc_bank].open_row != drc_row) {

            /*
            DP ( if (warmup_complete[0]) {
            cout << queue->NAME << " " << __func__ << " instr_id: " << queue->entry[i].instr_id << " row is inactive";
            cout << " swrites: " << scheduled_writes << " sreads: " << scheduled_reads;
            cout << " write: " << +bank_request[drc_channel][drc_rank][drc_bank].is_write << " read: " << +bank_request[drc_channel][drc_rank][drc_bank].is_read << hex;
            cout << " address: " << queue->entry[i].address << dec << " channel: " << drc_channel << " rank: " << drc_rank << " bank: " << drc_bank << endl; });
            */
            //cout << queue->NAME << " index: " << i << " event_cycle: " << queue->entry[i].event_cycle << " is selected" << endl;

            continue;
        }

        // select the oldest entry
        if (queue->entry[i].event_cycle < oldest_cycle) {
            oldest_cycle = queue->entry[i].event_cycle;
            oldest_index = i;
            row_buffer_hit = 1;
        }	  
    }

    if (oldest_index == -1) { // no matching open_row (row buffer miss)

        oldest_cycle = UINT64_MAX;
        for (uint32_t i=0; i<queue->SIZE; i++) {

            // empty entry
            drc_addr = queue->entry[i].address;
            if (drc_addr == 0)
                continue;
            
            // already scheduled
            if (queue->entry[i].scheduled) {
                //cout << queue->NAME << " index: " << i << " is already scheduled" << endl;
                continue;
            }

            drc_channel = drc_get_channel(drc_addr);
            drc_rank = drc_get_rank(drc_addr);
            drc_bank = drc_get_bank(drc_addr);

            // bank is busy
            if (bank_request[drc_channel][drc_rank][drc_bank].working) { 
                //cout << queue->NAME << " index: " << i << " rank: " << drc_rank << " bank: " << drc_bank << " is busy" << endl;
                continue;
            }

            // select the oldest entry
            if (queue->entry[i].event_cycle < oldest_cycle) {
                oldest_cycle = queue->entry[i].event_cycle;
                oldest_index = i;
                //cout << queue->NAME << " index: " << i << " event_cycle: " << queue->entry[i].event_cycle << " is selected" << endl;
            }
        }
    }

    // at this point, the scheduler knows which channel, rank, bank, row to access and if the request is a row buffer hit or miss
    if (oldest_index != -1) { // scheduler might not find anything if all requests are already scheduled or all banks are busy

        uint64_t LATENCY = 0;
        if (row_buffer_hit)
            LATENCY = tCAS;
            //LATENCY = tCAS + DRC_TAG_CHECK_TIME;
        else 
            LATENCY = tRP + tRCD + tCAS;
            //LATENCY = tRP + tRCD + tCAS + DRC_TAG_CHECK_TIME;

        uint64_t op_addr = queue->entry[oldest_index].address;
        uint32_t op_cpu = queue->entry[oldest_index].cpu,
                 op_channel = drc_get_channel(op_addr), 
                 op_rank = drc_get_rank(op_addr), 
                 op_bank = drc_get_bank(op_addr), 
                 op_row = drc_get_row(op_addr);
#ifdef DEBUG_PRINT
        uint32_t op_column = drc_get_column(op_addr);
#endif
        // this bank is now busy
        bank_request[op_channel][op_rank][op_bank].working = 1;
        bank_request[op_channel][op_rank][op_bank].cycle_available = current_core_cycle[op_cpu] + LATENCY;

        bank_request[op_channel][op_rank][op_bank].request_index = oldest_index;
        bank_request[op_channel][op_rank][op_bank].row_buffer_hit = row_buffer_hit;
        if (queue->is_WQ) {
            bank_request[op_channel][op_rank][op_bank].is_write = 1;
            bank_request[op_channel][op_rank][op_bank].is_read = 0;
            scheduled_writes++;
        } else {
            bank_request[op_channel][op_rank][op_bank].is_write = 0;
            bank_request[op_channel][op_rank][op_bank].is_read = 1;
            scheduled_reads++;
        }

        // update open row
        bank_request[op_channel][op_rank][op_bank].open_row = op_row;
        queue->entry[oldest_index].scheduled = 1;
        queue->entry[oldest_index].event_cycle = current_core_cycle[op_cpu] + LATENCY;

        DP (if (warmup_complete[op_cpu]) {
        cout << "[" << queue->NAME << "] " <<  __func__ << " instr_id: " << queue->entry[oldest_index].instr_id;
        cout << " row buffer: " << (row_buffer_hit ? (int)bank_request[op_channel][op_rank][op_bank].open_row : -1) << hex;
        cout << " address: " << queue->entry[oldest_index].address << " full_addr: " << queue->entry[oldest_index].full_addr << dec;
        cout << " index: " << oldest_index << " occupancy: " << queue->occupancy;
        cout << " ch: " << op_channel << " rank: " << op_rank << " bank: " << op_bank; // wrong from here
        cout << " row: " << op_row << " current: " << current_core_cycle[op_cpu] << " event: " << queue->entry[oldest_index].event_cycle << endl; });

        update_schedule_cycle(queue);
        update_process_cycle(queue);
    }
}

// Assembly:
movl 0x20(%rsi), %edx
testq %rdx, %rdx
je 0x11f86
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %rbx
leaq 0x409470(%rdi), %rsi
movl $0x378, %r8d            # imm = 0x378
addq 0x98(%rbx), %r8
movq $-0x1, %r10
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
xorl %eax, %eax
movq -0x38(%r8), %r11
testq %r11, %r11
je 0x11d68
cmpb $0x0, -0x376(%r8)
jne 0x11d68
movl %r11d, %r14d
shrl $0x5, %r14d
andl $0x3, %r14d
movl %r11d, %r15d
shrl $0xb, %r15d
andl $0x1, %r15d
movl %r11d, %r12d
shrl $0x7, %r12d
andl $0xf, %r12d
leaq (%r14,%r14,4), %r13
shll $0x8, %r13d
addq %rsi, %r13
leaq (%r15,%r15,4), %r14
shll $0x7, %r14d
addq %r13, %r14
leaq (%r12,%r12,4), %r15
cmpb $0x0, 0x1c(%r14,%r15,8)
jne 0x11d68
leaq (%r14,%r15,8), %r14
shrl $0xc, %r11d
andl $0xfff, %r11d           # imm = 0xFFF
cmpl %r11d, 0x18(%r14)
jne 0x11d68
movq (%r8), %r11
cmpq %r10, %r11
jae 0x11d68
movb $0x1, %al
movq %r11, %r10
movl %r9d, %ecx
incq %r9
addq $0x380, %r8             # imm = 0x380
cmpq %r9, %rdx
jne 0x11ced
cmpl $-0x1, %ecx
je 0x11d96
testb %al, %al
je 0x11e39
leaq 0xa48d(%rip), %rdx      # 0x1c21c
movl (%rdx), %edx
jmp 0x11e54
testq %rdx, %rdx
je 0x11f7d
movl $0x378, %r8d            # imm = 0x378
addq 0x98(%rbx), %r8
movq $-0x1, %r9
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movq -0x38(%r8), %r11
testq %r11, %r11
je 0x11e1c
cmpb $0x0, -0x376(%r8)
jne 0x11e1c
movl %r11d, %r14d
shrl $0x5, %r14d
andl $0x3, %r14d
movl %r11d, %r15d
shrl $0xb, %r15d
andl $0x1, %r15d
shrl $0x7, %r11d
andl $0xf, %r11d
leaq (%r14,%r14,4), %r14
shll $0x8, %r14d
addq %rsi, %r14
leaq (%r15,%r15,4), %r15
shll $0x7, %r15d
addq %r14, %r15
leaq (%r11,%r11,4), %r11
cmpb $0x0, 0x1c(%r15,%r11,8)
jne 0x11e1c
movq (%r8), %r11
cmpq %r9, %r11
cmovbq %r11, %r9
cmovbl %r10d, %ecx
incq %r10
addq $0x380, %r8             # imm = 0x380
cmpq %r10, %rdx
jne 0x11dbb
cmpl $-0x1, %ecx
jne 0x11d80
jmp 0x11f7d
leaq 0xa3d4(%rip), %rsi      # 0x1c214
leaq 0xa3d1(%rip), %rdx      # 0x1c218
movl (%rdx), %edx
addl (%rsi), %edx
leaq 0xa3ca(%rip), %rsi      # 0x1c21c
addl (%rsi), %edx
movl %edx, %edx
movq 0x98(%rbx), %rsi
movslq %ecx, %r10
imulq $0x380, %r10, %rcx      # imm = 0x380
movl 0x330(%rsi,%rcx), %r8d
movl 0x340(%rsi,%rcx), %r9d
movl %r9d, %r11d
shrl $0x5, %r11d
andl $0x3, %r11d
movl %r9d, %r14d
shrl $0xb, %r14d
andl $0x1, %r14d
movl %r9d, %r15d
shrl $0x7, %r15d
andl $0xf, %r15d
shrl $0xc, %r9d
andl $0xfff, %r9d            # imm = 0xFFF
leaq (%r11,%r11,4), %r11
shll $0x8, %r11d
addq %rdi, %r11
leaq (%r14,%r14,4), %r14
shll $0x7, %r14d
addq %r11, %r14
leaq (%r15,%r15,4), %r11
leaq (%r14,%r11,8), %r14
addq $0x409470, %r14         # imm = 0x409470
movb $0x1, 0x1c(%r14)
leaq 0x6efec(%rip), %r11     # 0x80ec0
movq (%r11,%r8,8), %r15
addq %rdx, %r15
movq %r15, (%r14)
movl %r10d, 0x24(%r14)
movb %al, 0x1e(%r14)
cmpb $0x0, 0x25(%rbx)
je 0x11ef9
incl 0x409468(%rdi)
movb $0x1, %dil
xorl %eax, %eax
jmp 0x11f03
incl 0x409464(%rdi)
movb $0x1, %al
xorl %edi, %edi
movb %dil, 0x20(%r14)
movb %al, 0x21(%r14)
movl %r9d, 0x18(%r14)
movb $0x1, 0x2(%rsi,%rcx)
addq (%r11,%r8,8), %rdx
movq %rdx, 0x378(%rsi,%rcx)
movq %rbx, %rsi
callq 0x11996
movl 0x20(%rbx), %eax
testq %rax, %rax
je 0x11f6d
movq 0x98(%rbx), %rsi
addq $0x2, %rsi
movq $-0x1, %rcx
xorl %edi, %edi
movl %eax, %edx
cmpb $0x0, (%rsi)
je 0x11f5c
movq 0x376(%rsi), %r8
cmpq %rcx, %r8
cmovbq %r8, %rcx
cmovbl %edi, %edx
incq %rdi
addq $0x380, %rsi            # imm = 0x380
cmpq %rdi, %rax
jne 0x11f46
jmp 0x11f76
xorl %edx, %edx
movq $-0x1, %rcx
movq %rcx, 0x58(%rbx)
movl %edx, 0x44(%rbx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop