// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rsi, %rbx
movl 0x20(%rsi), %edx
testq %rdx, %rdx
je 0x11f97
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 0x11f82
cmpb $0x0, -0x376(%r8)
jne 0x11f82
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 0x11f82
leaq (%r14,%r15,8), %r14
shrl $0xc, %r11d
andl $0xfff, %r11d           # imm = 0xFFF
cmpl %r11d, 0x18(%r14)
jne 0x11f82
movq (%r8), %r11
cmpq %r10, %r11
jae 0x11f82
movb $0x1, %al
movq %r11, %r10
movl %r9d, %ecx
incq %r9
addq $0x380, %r8             # imm = 0x380
cmpq %r9, %rdx
jne 0x11f07
jmp 0x11f9e
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $-0x1, %ecx
jne 0x12048
testq %rdx, %rdx
je 0x12043
movl $0x378, %esi            # imm = 0x378
addq 0x98(%rbx), %rsi
leaq 0x409470(%rdi), %r8
movq $-0x1, %r9
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movq -0x38(%rsi), %r11
testq %r11, %r11
je 0x12032
cmpb $0x0, -0x376(%rsi)
jne 0x12032
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 %r8, %r14
leaq (%r15,%r15,4), %r15
shll $0x7, %r15d
addq %r14, %r15
leaq (%r11,%r11,4), %r11
cmpb $0x0, 0x1c(%r15,%r11,8)
jne 0x12032
movq (%rsi), %r11
cmpq %r9, %r11
cmovbq %r11, %r9
cmovbl %r10d, %ecx
incq %r10
addq $0x380, %rsi            # imm = 0x380
cmpq %r10, %rdx
jne 0x11fd2
jmp 0x12048
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpl $-0x1, %ecx
je 0x121a2
testb %al, %al
je 0x12060
leaq 0xa1c0(%rip), %rdx      # 0x1c21c
movl (%rdx), %edx
jmp 0x1207b
leaq 0xa1ad(%rip), %rsi      # 0x1c214
leaq 0xa1aa(%rip), %rdx      # 0x1c218
movl (%rdx), %edx
addl (%rsi), %edx
leaq 0xa1a3(%rip), %rsi      # 0x1c21c
addl (%rsi), %edx
movl %edx, %edx
movq 0x98(%rbx), %rsi
movslq %ecx, %r8
imulq $0x380, %r8, %r8        # imm = 0x380
movl 0x330(%rsi,%r8), %r9d
movl 0x340(%rsi,%r8), %r10d
movl %r10d, %r11d
shrl $0x5, %r11d
andl $0x3, %r11d
movl %r10d, %r14d
shrl $0xb, %r14d
andl $0x1, %r14d
movl %r10d, %r15d
shrl $0x7, %r15d
andl $0xf, %r15d
shrl $0xc, %r10d
andl $0xfff, %r10d           # 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 0x6edc5(%rip), %r11     # 0x80ec0
movq (%r11,%r9,8), %r15
addq %rdx, %r15
movq %r15, (%r14)
movl %ecx, 0x24(%r14)
movb %al, 0x1e(%r14)
cmpb $0x0, 0x25(%rbx)
je 0x12122
movw $0x1, 0x20(%r14)
incl 0x409468(%rdi)
jmp 0x1212f
movw $0x100, 0x20(%r14)      # imm = 0x100
incl 0x409464(%rdi)
movl %r10d, 0x18(%r14)
movb $0x1, 0x2(%rsi,%r8)
addq (%r11,%r9,8), %rdx
movq %rdx, 0x378(%rsi,%r8)
movq %rbx, %rsi
callq 0x11bc8
movl 0x20(%rbx), %eax
testq %rax, %rax
je 0x12192
movq 0x98(%rbx), %rdx
addq $0x2, %rdx
movq $-0x1, %rcx
xorl %edi, %edi
movl %eax, %esi
cmpb $0x0, (%rdx)
je 0x12181
movq 0x376(%rdx), %r8
cmpq %rcx, %r8
cmovbq %r8, %rcx
cmovbl %edi, %esi
incq %rdi
addq $0x380, %rdx            # imm = 0x380
cmpq %rdi, %rax
jne 0x1216b
jmp 0x1219b
movq $-0x1, %rcx
movl %eax, %esi
movq %rcx, 0x58(%rbx)
movl %esi, 0x44(%rbx)
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq