// File: /workspace/github_cpp_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movl 0x20(%rsi), %esi
leaq 0x409470(%rdi), %rax
pushq $-0x1
popq %rdx
pushq $-0x1
popq %r10
movl $0x378, %r8d            # imm = 0x378
xorl %r9d, %r9d
xorl %ecx, %ecx
cmpq %r9, %rsi
je 0xe828
movq 0x98(%rbx), %r11
movq -0x38(%r11,%r8), %r14
testq %r14, %r14
je 0xe819
cmpb $0x0, -0x376(%r11,%r8)
jne 0xe819
movl %r14d, %r15d
shrl $0x5, %r15d
andl $0x3, %r15d
movl %r14d, %r12d
shrl $0xb, %r12d
andl $0x1, %r12d
movl %r14d, %r13d
shrl $0x7, %r13d
andl $0xf, %r13d
imulq $0x500, %r15, %rbp      # imm = 0x500
addq %rax, %rbp
imulq $0x280, %r12, %r15      # imm = 0x280
addq %rbp, %r15
imulq $0x28, %r13, %r12
cmpb $0x0, 0x1c(%r12,%r15)
jne 0xe819
addq %r12, %r15
shrl $0xc, %r14d
andl $0xfff, %r14d           # imm = 0xFFF
cmpl %r14d, 0x18(%r15)
jne 0xe819
movq (%r11,%r8), %r11
cmpq %r10, %r11
jae 0xe819
movb $0x1, %cl
movq %r11, %r10
movl %r9d, %edx
incq %r9
addq $0x380, %r8             # imm = 0x380
jmp 0xe78e
cmpl $-0x1, %edx
jne 0xe8c4
pushq $-0x1
popq %rdx
pushq $-0x1
popq %r8
movl $0x378, %r9d            # imm = 0x378
xorl %r10d, %r10d
cmpq %r10, %rsi
je 0xe8bb
movq 0x98(%rbx), %r11
movq -0x38(%r11,%r9), %r14
testq %r14, %r14
je 0xe8af
cmpb $0x0, -0x376(%r11,%r9)
jne 0xe8af
movl %r14d, %r15d
shrl $0x5, %r15d
andl $0x3, %r15d
movl %r14d, %r12d
shrl $0xb, %r12d
andl $0x1, %r12d
shrl $0x7, %r14d
andl $0xf, %r14d
imulq $0x500, %r15, %r15      # imm = 0x500
addq %rax, %r15
imulq $0x280, %r12, %r12      # imm = 0x280
addq %r15, %r12
imulq $0x28, %r14, %r14
cmpb $0x0, 0x1c(%r14,%r12)
jne 0xe8af
movq (%r11,%r9), %r11
cmpq %r8, %r11
cmovbq %r11, %r8
cmovbl %r10d, %edx
incq %r10
addq $0x380, %r9             # imm = 0x380
jmp 0xe841
cmpl $-0x1, %edx
je 0xe9d6
testb %cl, %cl
je 0xe8d3
leaq 0x993d(%rip), %rsi      # 0x1820c
movl (%rsi), %esi
jmp 0xe8f0
leaq 0x992a(%rip), %r8       # 0x18204
leaq 0x9927(%rip), %rsi      # 0x18208
movl (%rsi), %esi
addl (%r8), %esi
leaq 0x991f(%rip), %r8       # 0x1820c
addl (%r8), %esi
movl %esi, %esi
movq 0x98(%rbx), %r8
movslq %edx, %r11
imulq $0x380, %r11, %rdx      # imm = 0x380
movl 0x330(%r8,%rdx), %r9d
movl 0x340(%r8,%rdx), %r10d
movl %r10d, %r14d
shrl $0x5, %r14d
andl $0x3, %r14d
movl %r10d, %r15d
shrl $0xb, %r15d
andl $0x1, %r15d
movl %r10d, %r12d
shrl $0x7, %r12d
andl $0xf, %r12d
shrl $0xc, %r10d
andl $0xfff, %r10d           # imm = 0xFFF
imulq $0x500, %r14, %r14      # imm = 0x500
addq %r14, %rax
imulq $0x280, %r15, %r15      # imm = 0x280
addq %rax, %r15
imulq $0x28, %r12, %r12
leaq (%r15,%r12), %r14
movb $0x1, 0x1c(%r12,%r15)
leaq 0x6e548(%rip), %rax     # 0x7ceb0
movq (%rax,%r9,8), %r13
addq %rsi, %r13
movq %r13, (%r12,%r15)
movl %r11d, 0x24(%r12,%r15)
movb %cl, 0x1e(%r12,%r15)
cmpb $0x0, 0x25(%rbx)
je 0xe990
incl 0x409468(%rdi)
movb $0x1, %dil
xorl %ecx, %ecx
jmp 0xe99a
incl 0x409464(%rdi)
movb $0x1, %cl
xorl %edi, %edi
movb %dil, 0x20(%r14)
movb %cl, 0x21(%r14)
movl %r10d, 0x18(%r14)
movb $0x1, 0x2(%r8,%rdx)
addq (%rax,%r9,8), %rsi
movq %rsi, 0x378(%r8,%rdx)
movq %rbx, %rsi
callq 0xe496
movq %rbx, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe4ea
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop