// File: /workspace/github_cpp_makefile_star2_O2/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 0x9363
movl 0x828c(%rbx), %ecx
testl %ecx, %ecx
je 0x934c
movl 0x9f2c(%rbx), %eax
testl %eax, %eax
setne %dl
cmpl $0xc, %ecx
setb %cl
testb %dl, %cl
jne 0x934c
testl %eax, %eax
je 0x939e
cmpl $0xc, 0x7844(%rbx)
jb 0x939e
leaq 0x8258(%rbx), %rsi
movb $0x0, 0x7840(%rbx)
andl $0x0, 0x7844(%rbx)
jmp 0x9381
cmpl $0x24, 0x828c(%rbx)
jb 0x9391
movb $0x1, 0x7840(%rbx)
andl $0x0, 0x7844(%rbx)
leaq 0x9ef8(%rbx), %rsi
movq %rbx, %rdi
callq 0x9080
addq $0x18, 0x7568(%rbx)
cmpb $0x0, 0x7840(%rbx)
je 0x9447
leaq 0x8258(%rbx), %r14
movl 0x8298(%rbx), %eax
cmpl 0x8278(%rbx), %eax
jae 0x93f0
movq 0x82a8(%rbx), %rcx
movq 0x82f0(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x73ada(%rip), %rdx     # 0x7ceb0
cmpq (%rdx,%rax,8), %rcx
ja 0x93f0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x94ea
cmpb $0x0, 0x7840(%rbx)
je 0x9447
movl 0x829c(%rbx), %eax
cmpl 0x8278(%rbx), %eax
jae 0x94d0
movq 0x82b0(%rbx), %rcx
movq 0x82f0(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x73a8b(%rip), %rdx     # 0x7ceb0
cmpq (%rdx,%rax,8), %rcx
ja 0x94d0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9718
cmpb $0x0, 0x7840(%rbx)
jne 0x94d0
leaq 0x9ef8(%rbx), %r14
movl 0x9f38(%rbx), %eax
cmpl 0x9f18(%rbx), %eax
jae 0x9499
movq 0x9f48(%rbx), %rcx
movq 0x9f90(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x73a31(%rip), %rdx     # 0x7ceb0
cmpq (%rdx,%rax,8), %rcx
ja 0x9499
movq %rbx, %rdi
movq %r14, %rsi
callq 0x94ea
cmpb $0x0, 0x7840(%rbx)
jne 0x94d0
movl 0x9f3c(%rbx), %eax
cmpl 0x9f18(%rbx), %eax
jae 0x94d0
movq 0x9f50(%rbx), %rcx
movq 0x9f90(%rbx), %rdx
imulq $0x380, %rax, %rax      # imm = 0x380
movl 0x330(%rdx,%rax), %eax
leaq 0x739e6(%rip), %rdx     # 0x7ceb0
cmpq (%rdx,%rax,8), %rcx
jbe 0x94d8
addq $0x8, %rsp
popq %rbx
popq %r14
retq
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x9718