// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]ramulator/src/Controller.cpp
// Function: ramulator::Controller<ramulator::TLDRAM>::tick()
void Controller<TLDRAM>::tick(){
    clk++;
    req_queue_length_sum += readq.size() + writeq.size();
    read_req_queue_length_sum += readq.size();
    write_req_queue_length_sum += writeq.size();

    /*** 1. Serve completed reads ***/
    if (pending.size()) {
        Request& req = pending[0];
        if (req.depart <= clk) {
          if (req.depart - req.arrive > 1) {
                  read_latency_sum += req.depart - req.arrive;
                  channel->update_serving_requests(
                      req.addr_vec.data(), -1, clk);
          }
            req.callback(req);
            pending.pop_front();
        }
    }

    /*** 2. Should we schedule refreshes? ***/
    refresh->tick_ref();

    /*** 3. Should we schedule writes? ***/
    if (!write_mode) {
        // yes -- write queue is almost full or read queue is empty
        if (writeq.size() >= int(0.8 * writeq.max) /*|| readq.size() == 0*/)
            write_mode = true;
    }
    else {
        // no -- write queue is almost empty and read queue is not empty
        if (writeq.size() <= int(0.2 * writeq.max) && readq.size() != 0)
            write_mode = false;
    }

    /*** 4. Find the best command to schedule, if any ***/
    Queue* queue = !write_mode ? &readq : &writeq;
    if (otherq.size())
        queue = &otherq;  // "other" requests are rare, so we give them precedence over reads/writes

    auto req = scheduler->get_head(queue->q);
    if (req == queue->q.end() || !is_ready(req)) {
        // we couldn't find a command to schedule -- let's try to be speculative
        auto cmd = TLDRAM::Command::PRE;
        vector<int> victim = rowpolicy->get_victim(cmd);
        if (!victim.empty()){
            issue_cmd(cmd, victim);
        }
        return;  // nothing more to be done this cycle
    }

    if (req->is_first_command) {
        int coreid = req->coreid;
        req->is_first_command = false;
        if (req->type == Request::Type::READ || req->type == Request::Type::WRITE) {
          channel->update_serving_requests(req->addr_vec.data(), 1, clk);
        }
        int tx = (channel->spec->prefetch_size * channel->spec->channel_width / 8);
        if (req->type == Request::Type::READ) {
            if (is_row_hit(req)) {
                ++read_row_hits[coreid];
                ++row_hits;
            } else if (is_row_open(req)) {
                ++read_row_conflicts[coreid];
                ++row_conflicts;
            } else {
                ++read_row_misses[coreid];
                ++row_misses;
            }
          read_transaction_bytes += tx;
        } else if (req->type == Request::Type::WRITE) {
          if (is_row_hit(req)) {
              ++write_row_hits[coreid];
              ++row_hits;
          } else if (is_row_open(req)) {
              ++write_row_conflicts[coreid];
              ++row_conflicts;
          } else {
              ++write_row_misses[coreid];
              ++row_misses;
          }
          write_transaction_bytes += tx;
        }
    }

    /*** 5. Change a read request to a migration request ***/
    if (req->type == Request::Type::READ) {
        req->type = Request::Type::EXTENSION;
    }

    // issue command on behalf of request
    auto cmd = get_first_cmd(req);
    issue_cmd(cmd, get_addr_vec(cmd, req));

    // check whether this is the last command (which finishes the request)
    if (cmd != channel->spec->translate[int(req->type)])
        return;

    // set a future completion time for read requests
    if (req->type == Request::Type::READ || req->type == Request::Type::EXTENSION) {
        req->depart = clk + channel->spec->read_latency;
        pending.push_back(*req);
    }
    if (req->type == Request::Type::WRITE) {
        channel->update_serving_requests(req->addr_vec.data(), -1, clk);
    }

    // remove request from queue
    queue->q.erase(req);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq 0x1090(%rdi), %rcx
incq %rcx
movq %rcx, 0x1090(%rdi)
movl 0x10d0(%rdi), %eax
movl 0x10f0(%rdi), %edx
leal (%rdx,%rax), %esi
cvtsi2sd %rsi, %xmm0
addsd 0x9e8(%rdi), %xmm0
movsd %xmm0, 0x9e8(%rdi)
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
addsd 0xb18(%rdi), %xmm0
cvtsi2sd %rdx, %xmm1
movsd %xmm0, 0xb18(%rdi)
addsd 0xc48(%rdi), %xmm1
movsd %xmm1, 0xc48(%rdi)
movq 0x1188(%rdi), %rax
movq 0x1168(%rdi), %rsi
movq %rax, %rdx
subq %rsi, %rdx
sarq $0x3, %rdx
cmpq $0x1, %rax
adcq $-0x1, %rdx
leaq 0x1140(%rdi), %r14
movq 0x1170(%rdi), %rax
subq 0x1178(%rdi), %rax
leaq (%rdx,%rdx,4), %rdi
sarq $0x5, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rax
addq %rdi, %rax
movq 0x1160(%rbx), %rdi
movq 0x1150(%rbx), %r15
subq %r15, %rdi
sarq $0x5, %rdi
movabsq $0x5555555555555555, %r8 # imm = 0x5555555555555555
imulq %rdi, %r8
cmpq %r8, %rax
je 0x106e6e
movq %r15, %rdi
subq 0x1158(%rbx), %rdi
movq %rdi, %rax
sarq $0x5, %rax
imulq %rdx, %rax
cmpq $-0x5f, %rdi
jl 0x106deb
cmpq $0x5, %rax
jb 0x106e10
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rdx
shrq $0x2, %rdx
jmp 0x106e02
notq %rax
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rdx
shrq $0x2, %rdx
notq %rdx
imulq $-0x1e0, %rdx, %r15     # imm = 0xFE20
addq (%rsi,%rdx,8), %r15
addq %rdi, %r15
movq 0x38(%r15), %rax
cmpq %rcx, %rax
jg 0x106e6e
subq 0x30(%r15), %rax
cmpq $0x2, %rax
jl 0x106e50
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
addsd 0x8b8(%rbx), %xmm0
movsd %xmm0, 0x8b8(%rbx)
movq 0x1098(%rbx), %rdi
movq 0x10(%r15), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x10724e
cmpq $0x0, 0x50(%r15)
je 0x107222
leaq 0x40(%r15), %rdi
movq %r15, %rsi
callq *0x58(%r15)
movq %r14, %rdi
callq 0x28dec
movq 0x10b8(%rbx), %rdi
callq 0x10749a
movb 0x1190(%rbx), %al
movl 0x10f0(%rbx), %ecx
movl 0x10f8(%rbx), %edx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
testb %al, %al
je 0x106eb7
mulsd 0x4eb18(%rip), %xmm0    # 0x1559b8
cvttsd2si %xmm0, %edx
cmpl %edx, %ecx
ja 0x106ed1
movl 0x10d0(%rbx), %ecx
testq %rcx, %rcx
je 0x106ed1
xorl %edx, %edx
jmp 0x106ec9
mulsd 0x4eaf1(%rip), %xmm0    # 0x1559b0
cvttsd2si %xmm0, %esi
movb $0x1, %dl
cmpl %esi, %ecx
jb 0x106ed1
movb %dl, 0x1190(%rbx)
movl %edx, %eax
movzbl %al, %eax
shll $0x5, %eax
orq $0x10c0, %rax           # imm = 0x10C0
cmpl $0x0, 0x1130(%rbx)
movl $0x1120, %r15d          # imm = 0x1120
cmoveq %rax, %r15
addq %rbx, %r15
movq 0x10a0(%rbx), %rdi
movq %r15, %rsi
callq 0x1074d2
cmpq %r15, %rax
je 0x106fcd
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
callq 0xfacc6
testb %al, %al
je 0x106fcd
movq %r14, 0x20(%rsp)
cmpb $0x1, 0x10(%r12)
jne 0x107123
movl 0x38(%r12), %ebp
movl 0x3c(%r12), %eax
movb $0x0, 0x10(%r12)
cmpl $0x1, %eax
ja 0x106f67
movq 0x1090(%rbx), %rcx
movq 0x1098(%rbx), %rdi
movq 0x20(%r12), %rsi
movl $0x1, %edx
callq 0x10724e
movl 0x3c(%r12), %eax
movq 0x1098(%rbx), %rdi
movq 0x390(%rdi), %rcx
movl 0x36d4(%rcx), %edx
imull 0x36d0(%rcx), %edx
leal 0x7(%rdx), %r14d
testl %edx, %edx
cmovnsl %edx, %r14d
cmpl $0x1, %eax
je 0x107043
testl %eax, %eax
jne 0x107123
movl %ebp, (%rsp)
movl 0x280(%rcx), %esi
movq 0x20(%r12), %rdx
callq 0xfb13a
movl $0x70, %r13d
testb %al, %al
je 0x10706c
movl $0x1a0, %ebp            # imm = 0x1A0
movl $0x2f8, %edi            # imm = 0x2F8
jmp 0x1070e0
movq 0x10a8(%rbx), %rax
movslq 0x8(%rax), %rcx
shlq $0x5, %rcx
movl $0x1, 0x4(%rsp)
cmpq $0x0, 0x20(%rax,%rcx)
je 0x107222
leaq (%rax,%rcx), %rsi
addq $0x10, %rsi
leaq 0x8(%rsp), %r14
leaq 0x4(%rsp), %rdx
movq %r14, %rdi
callq *0x18(%rsi)
movq (%r14), %rdi
cmpq 0x8(%r14), %rdi
je 0x107028
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x1078ae
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x107213
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
jmp 0x107213
movl %ebp, (%rsp)
movl 0x284(%rcx), %esi
movq 0x20(%r12), %rdx
callq 0xfb13a
movl $0x108, %r13d           # imm = 0x108
testb %al, %al
je 0x10709e
movl $0x1a0, %ebp            # imm = 0x1A0
movl $0x508, %edi            # imm = 0x508
jmp 0x1070e0
movq 0x1098(%rbx), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r12), %rcx
movl 0x280(%rax,%rcx,4), %esi
movq 0x20(%r12), %rdx
callq 0x108508
testb %al, %al
movl $0x458, %eax            # imm = 0x458
movl $0x3a8, %edi            # imm = 0x3A8
jmp 0x1070ce
movq 0x1098(%rbx), %rdi
movq 0x390(%rdi), %rax
movslq 0x3c(%r12), %rcx
movl 0x280(%rax,%rcx,4), %esi
movq 0x20(%r12), %rdx
callq 0x108508
testb %al, %al
movl $0x668, %eax            # imm = 0x668
movl $0x5b8, %edi            # imm = 0x5B8
cmovneq %rax, %rdi
movl $0x2d0, %eax            # imm = 0x2D0
movl $0x238, %ebp            # imm = 0x238
cmovneq %rax, %rbp
sarl $0x3, %r14d
addq %rbx, %rdi
movl (%rsp), %esi
callq 0x2ac3a
movsd 0x42fc1(%rip), %xmm0    # 0x14a0b8
movsd 0x70(%rax), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x70(%rax)
addsd (%rbx,%rbp), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
movsd %xmm0, (%rbx,%rbp)
addsd (%rbx,%r13), %xmm1
movsd %xmm1, (%rbx,%r13)
movl 0x3c(%r12), %eax
testl %eax, %eax
jne 0x10713a
movl $0x5, 0x3c(%r12)
movl $0x5, %eax
movq 0x1098(%rbx), %rdi
movq 0x390(%rdi), %rcx
cltq
movl 0x280(%rcx,%rax,4), %esi
leaq 0x20(%r12), %r13
movq 0x20(%r12), %rdx
callq 0xfad88
movl %eax, %ebp
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x24b42
movq %rbx, %rdi
movl %ebp, %esi
movq %r14, %rdx
callq 0x1078ae
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
movq 0x20(%rsp), %r14
je 0x10719b
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq 0x1098(%rbx), %rax
movq 0x390(%rax), %rax
movslq 0x3c(%r12), %rcx
cmpl 0x280(%rax,%rcx,4), %ebp
jne 0x107213
cmpl $0x5, %ecx
je 0x1071c0
testl %ecx, %ecx
jne 0x1071e7
movq %r12, %rsi
addq $0x10, %rsi
movslq 0x3f48(%rax), %rax
addq 0x1090(%rbx), %rax
movq %rax, 0x48(%r12)
movq %r14, %rdi
callq 0x294c4
movl 0x3c(%r12), %ecx
cmpl $0x1, %ecx
jne 0x107208
movq 0x1090(%rbx), %rcx
movq 0x1098(%rbx), %rdi
movq (%r13), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x10724e
movq %r15, %rdi
movq %r12, %rsi
callq 0x29c9c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xb160
jmp 0x107229
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x107243
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq %rbx, %rdi
callq 0xb510
nop