// File: /workspace/github_cpp_makefile_star2_O1/NishadSaraf[P]Championship-Simulator/src/drc_controller.cc
// Function: DRC_MEMORY_CONTROLLER::operate()
void DRC_MEMORY_CONTROLLER::operate()
{
    if (do_write == 0) {
        if (WQ.occupancy >= DRC_WRITE_HIGH_WM) {
            do_write = 1;
            processed_writes = 0;
            last_drc_read_mode = current_core_cycle[0];
            //cout << "DRC is now WRITE mode cycle: " << current_core_cycle[0] << endl;

            // reset scheduled RQ requests
            reset_remain_requests(&RQ);

            for (uint32_t i=0; i<DRC_CHANNELS; i++) {
                // add data bus turnaround time
                dbus_cycle_available[i] += DRC_DBUS_TURN_AROUND_TIME;
            }
        }
    } else {
        if (WQ.occupancy == 0)
            do_write = 0;
        else if (RQ.occupancy && (WQ.occupancy < DRC_WRITE_LOW_WM))
            do_write = 0;
        else if (RQ.occupancy && (MIN_DRC_WRITES_PER_SWITCH <= processed_writes))
            do_write = 0;

        if (do_write == 0) {
            processed_writes = 0;
            last_drc_write_mode = current_core_cycle[0];
            //cout << "DRC is now READ  mode cycle: " << current_core_cycle[0] << endl;

            // reset scheduled WQ requests
            reset_remain_requests(&WQ);

            for (uint32_t i=0; i<DRC_CHANNELS; i++) {
                // add data bus turnaround time
                dbus_cycle_available[i] += DRC_DBUS_TURN_AROUND_TIME;
            }
        }
    }

    // do FR-FCFS scheduling

    // handle write
    // schedule new entry
    //for (uint32_t channel=0; channel<DRC_CHANNELS; channel++) {
        //if (write_mode[channel] && (WQ.next_schedule_index < WQ.SIZE)) {
        if (do_write && (WQ.next_schedule_index < WQ.SIZE)) {
            if (WQ.next_schedule_cycle <= current_core_cycle[WQ.entry[WQ.next_schedule_index].cpu])
                schedule(&WQ);
                //schedule(channel, &WQ);
        }
    //}

    // process DRC requests
    //for (uint32_t channel=0; channel<DRC_CHANNELS; channel++) {
        //if (write_mode[channel] && (WQ.next_process_index < WQ.SIZE)) {
        if (do_write && (WQ.next_process_index < WQ.SIZE)) {
            if (WQ.next_process_cycle <= current_core_cycle[WQ.entry[WQ.next_process_index].cpu])
                //process(channel, &WQ);
                process(&WQ);
        }
    //}

    // handle read
    // schedule new entry
    //for (uint32_t channel=0; channel<DRC_CHANNELS; channel++) {
        //if ((write_mode[channel] == 0) && (RQ.next_schedule_index < RQ.SIZE)) {
        if ((do_write == 0) && (RQ.next_schedule_index < RQ.SIZE)) {
            if (RQ.next_schedule_cycle <= current_core_cycle[RQ.entry[RQ.next_schedule_index].cpu])
                schedule(&RQ);
                //schedule(channel, &RQ);
        }
    //}

    // process DRC requests
    //for (uint32_t channel=0; channel<DRC_CHANNELS; channel++) {
        //if ((write_mode[channel] == 0) && (RQ.next_process_index < RQ.SIZE)) {
        if ((do_write == 0) && (RQ.next_process_index < RQ.SIZE)) {
            if (RQ.next_process_cycle <= current_core_cycle[RQ.entry[RQ.next_process_index].cpu])
                process(&RQ);
                //process(channel, &RQ);
        }
    //}
}

// Assembly:
pushq %rbx
movq %rdi, %rbx
cmpb $0x0, 0x40944c(%rdi)
je 0x11d27
movl 0x40a8a4(%rbx), %ecx
testl %ecx, %ecx
je 0x11cbd
movl 0x40c544(%rbx), %eax
testl %eax, %eax
setne %dl
cmpl $0xc, %ecx
setb %cl
testb %dl, %cl
jne 0x11cbd
testl %eax, %eax
je 0x11cc4
cmpl $0xc, 0x409450(%rbx)
jb 0x11cc4
movb $0x0, 0x40944c(%rbx)
cmpb $0x0, 0x40944c(%rbx)
jne 0x11d8b
leaq 0x40a870(%rbx), %rsi
movl $0x0, 0x409450(%rbx)
leaq 0x6f1d7(%rip), %rax     # 0x80ec0
movq (%rax), %rax
movq %rax, 0x8b13d(%rip)     # 0x9ce30
movq %rbx, %rdi
callq 0x11928
xorl %eax, %eax
movdqa 0x338b(%rip), %xmm0     # 0x15090
movdqu 0x409010(%rbx,%rax,8), %xmm1
paddq %xmm0, %xmm1
movdqu %xmm1, 0x409010(%rbx,%rax,8)
addq $0x2, %rax
cmpq $0x4, %rax
jne 0x11d05
jmp 0x11d8b
cmpl $0x24, 0x40a8a4(%rbx)
jb 0x11d8b
movb $0x1, 0x40944c(%rbx)
movl $0x0, 0x409450(%rbx)
leaq 0x6f178(%rip), %rax     # 0x80ec0
movq (%rax), %rax
movq %rax, 0x8b0d6(%rip)     # 0x9ce28
leaq 0x40c510(%rbx), %rsi
movq %rbx, %rdi
callq 0x11928
xorl %eax, %eax
movdqa 0x3325(%rip), %xmm0     # 0x15090
movdqu 0x409010(%rbx,%rax,8), %xmm1
paddq %xmm0, %xmm1
movdqu %xmm1, 0x409010(%rbx,%rax,8)
addq $0x2, %rax
cmpq $0x4, %rax
jne 0x11d6b
cmpb $0x0, 0x40944c(%rbx)
je 0x11dda
movl 0x40a8b0(%rbx), %eax
cmpl 0x40a890(%rbx), %eax
jae 0x11dda
movq 0x40a8c0(%rbx), %rcx
movq 0x40a908(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x6f0fb(%rip), %rdx     # 0x80ec0
cmpq (%rdx,%rax,8), %rcx
ja 0x11dda
leaq 0x40a870(%rbx), %rsi
movq %rbx, %rdi
callq 0x11eca
cmpb $0x0, 0x40944c(%rbx)
je 0x11e29
movl 0x40a8b4(%rbx), %eax
cmpl 0x40a890(%rbx), %eax
jae 0x11e29
movq 0x40a8c8(%rbx), %rcx
movq 0x40a908(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x6f0ac(%rip), %rdx     # 0x80ec0
cmpq (%rdx,%rax,8), %rcx
ja 0x11e29
leaq 0x40a870(%rbx), %rsi
movq %rbx, %rdi
callq 0x121ac
cmpb $0x0, 0x40944c(%rbx)
jne 0x11e78
movl 0x40c550(%rbx), %eax
cmpl 0x40c530(%rbx), %eax
jae 0x11e78
movq 0x40c560(%rbx), %rcx
movq 0x40c5a8(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x6f05d(%rip), %rdx     # 0x80ec0
cmpq (%rdx,%rax,8), %rcx
ja 0x11e78
leaq 0x40c510(%rbx), %rsi
movq %rbx, %rdi
callq 0x11eca
cmpb $0x0, 0x40944c(%rbx)
jne 0x11eb8
movl 0x40c554(%rbx), %eax
cmpl 0x40c530(%rbx), %eax
jae 0x11eb8
movq 0x40c568(%rbx), %rcx
movq 0x40c5a8(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x6f00e(%rip), %rdx     # 0x80ec0
cmpq (%rdx,%rax,8), %rcx
jbe 0x11eba
popq %rbx
retq
leaq 0x40c510(%rbx), %rsi
movq %rbx, %rdi
popq %rbx
jmp 0x121ac