// File: /workspace/github_cpp_makefile_star2_O3/NishadSaraf[P]Championship-Simulator/src/dram_controller.cc
// Function: MEMORY_CONTROLLER::operate()
void MEMORY_CONTROLLER::operate()
{
    if (do_write == 0) {
        if (WQ.occupancy >= DRAM_WRITE_HIGH_WM) {
            do_write = 1;
            processed_writes = 0;

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

            for (uint32_t i=0; i<DRAM_CHANNELS; i++) {
                // add data bus turnaround time
                dbus_cycle_available[i] += DRAM_DBUS_TURN_AROUND_TIME;
            }
        }
    }
    else {
        if (WQ.occupancy == 0)
            do_write = 0;
        else if (RQ.occupancy && (WQ.occupancy < DRAM_WRITE_LOW_WM))
            do_write = 0;
        else if (RQ.occupancy && (MIN_DRAM_WRITES_PER_SWITCH <= processed_writes))
            do_write = 0;

        if (do_write == 0) {
            processed_writes = 0;

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

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

    // do FR-FCFS scheduling

    // handle write
    // schedule new entry
    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);
    }

    // process DRAM requests
    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(&WQ);
    }

    // handle read
    // schedule new entry
    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);
    }

    // process DRAM requests
    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);
    }
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
cmpb $0x0, 0x7840(%rdi)
je 0xb956
movl 0x828c(%rbx), %ecx
testl %ecx, %ecx
je 0xb93c
movl 0x9f2c(%rbx), %eax
testl %eax, %eax
setne %dl
cmpl $0xc, %ecx
setb %cl
testb %dl, %cl
jne 0xb93c
testl %eax, %eax
je 0xb994
cmpl $0xc, 0x7844(%rbx)
jb 0xb994
leaq 0x8258(%rbx), %rsi
movb $0x0, 0x7840(%rbx)
movl $0x0, 0x7844(%rbx)
jmp 0xb977
cmpl $0x24, 0x828c(%rbx)
jb 0xb987
movb $0x1, 0x7840(%rbx)
movl $0x0, 0x7844(%rbx)
leaq 0x9ef8(%rbx), %rsi
movq %rbx, %rdi
callq 0xb5c0
addq $0x18, 0x7568(%rbx)
cmpb $0x0, 0x7840(%rbx)
je 0xba3d
leaq 0x8258(%rbx), %r14
movl 0x8298(%rbx), %eax
cmpl 0x8278(%rbx), %eax
jae 0xb9e6
movq 0x82a8(%rbx), %rcx
movq 0x82f0(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x754f4(%rip), %rdx     # 0x80ec0
cmpq (%rdx,%rax,8), %rcx
ja 0xb9e6
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbae0
cmpb $0x0, 0x7840(%rbx)
je 0xba3d
movl 0x829c(%rbx), %eax
cmpl 0x8278(%rbx), %eax
jae 0xbac6
movq 0x82b0(%rbx), %rcx
movq 0x82f0(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x754a5(%rip), %rdx     # 0x80ec0
cmpq (%rdx,%rax,8), %rcx
ja 0xbac6
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbd6c
cmpb $0x0, 0x7840(%rbx)
jne 0xbac6
leaq 0x9ef8(%rbx), %r14
movl 0x9f38(%rbx), %eax
cmpl 0x9f18(%rbx), %eax
jae 0xba8f
movq 0x9f48(%rbx), %rcx
movq 0x9f90(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x7544b(%rip), %rdx     # 0x80ec0
cmpq (%rdx,%rax,8), %rcx
ja 0xba8f
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbae0
cmpb $0x0, 0x7840(%rbx)
jne 0xbac6
movl 0x9f3c(%rbx), %eax
cmpl 0x9f18(%rbx), %eax
jae 0xbac6
movq 0x9f50(%rbx), %rcx
movq 0x9f90(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x75400(%rip), %rdx     # 0x80ec0
cmpq (%rdx,%rax,8), %rcx
jbe 0xbace
addq $0x8, %rsp
popq %rbx
popq %r14
retq
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0xbd6c