// File: /workspace/github_cpp_makefile_star2_O3/CMU-SAFARI[P]ramulator/src/Refresh.cpp
// Function: ramulator::Refresh<ramulator::DSARP>::inject_refresh(bool)
void Refresh<DSARP>::inject_refresh(bool b_ref_rank) {
  // Rank-level refresh
  if (b_ref_rank)
    for (auto rank : ctrl->channel->children)
      refresh_target(ctrl, rank->id, -1, -1);
  // Bank-level refresh. Simultaneously issue to all ranks (better performance than staggered refreshes).
  else {
    for (auto rank : ctrl->channel->children) {
      int rid = rank->id;
      int bid = bank_ref_counters[rid];

      // Behind refresh schedule by 1 ref
      (*(bank_refresh_backlog[rid]))[bid]--;

      // Next time, refresh the next bank in the same bank
      bank_ref_counters[rid] = (bank_ref_counters[rid] + 1) % max_bank_count;

      // Check to see if we can skip a refresh
      if (ctrl->channel->spec->type == DSARP::Type::DARP ||
        ctrl->channel->spec->type == DSARP::Type::DSARP) {

        bool ref_now = false;
        // 1. Any pending refrehes?
        bool pending_ref = false;
        for (Request req : ctrl->otherq.q) {
          if (req.type == Request::Type::REFRESH) {
            pending_ref = true;
            break;
          }
        }

        // 2. Track readq
        if (!pending_ref && ctrl->readq.size() == 0)
          ref_now = true;

        // 3. Track log status. If we are too behind the schedule, then we need to refresh now.
        if ((*(bank_refresh_backlog[rid]))[bid] <= backlog_min)
          ref_now = true;

        // Otherwise skip refresh
        if (!ref_now)
          continue;
      }

      refresh_target(ctrl, rid, bid, subarray_ref_counters[rid][bid]);
      // Get 1 ref credit
      (*(bank_refresh_backlog[rid]))[bid]++;
      // Next time, refresh the next sa in the same bank
      subarray_ref_counters[rid][bid] = (subarray_ref_counters[rid][bid]+1) % max_sa_count;
    }
  }
  refreshed = clk;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbp
movq 0x8(%rdi), %rax
movq 0x1098(%rax), %rax
movq 0x3b0(%rax), %r12
movq 0x3b8(%rax), %rbx
testl %esi, %esi
je 0x12fdef
cmpq %rbx, %r12
je 0x12ff8e
movq (%r12), %rax
movq 0x8(%rbp), %rsi
movl 0x39c(%rax), %edx
movq %rbp, %rdi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x1304c2
addq $0x8, %r12
jmp 0x12fdbf
cmpq %rbx, %r12
je 0x12ff8e
leaq 0x30(%rsp), %r14
movq 0x50(%rbp), %rcx
movq %rbp, (%rsp)
movq %rbx, 0x10(%rsp)
movq (%r12), %rax
movslq 0x39c(%rax), %rdi
movq 0x20(%rbp), %rsi
movslq (%rsi,%rdi,4), %rdx
movq (%rcx,%rdi,8), %rax
movq (%rax), %rax
movq %rdx, 0x8(%rsp)
decl (%rax,%rdx,4)
movl (%rsi,%rdi,4), %eax
incl %eax
cltd
idivl 0x3c(%rbp)
movq %rdi, %r15
movl %edx, (%rsi,%rdi,4)
movq 0x8(%rbp), %rbx
movq 0x1098(%rbx), %rax
movq 0x390(%rax), %rax
movl (%rax), %eax
cmpl $0x4, %eax
je 0x12fe5d
cmpl $0x2, %eax
jne 0x12ff1c
addq $0x1120, %rbx           # imm = 0x1120
movq %rbx, %rbp
movq (%rbp), %rbp
cmpq %rbx, %rbp
je 0x12fee7
movups 0x10(%rbp), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x20(%rbp), %rsi
movq %r14, %rdi
callq 0x24b42
movq 0x48(%rbp), %rax
movq %rax, 0x28(%r14)
movups 0x38(%rbp), %xmm0
movups %xmm0, 0x18(%r14)
leaq 0x50(%rbp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x29968
movl 0x4c(%rsp), %r13d
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0x12fec2
leaq 0x60(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x12fed9
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
cmpl $0x2, %r13d
jne 0x12fe67
xorl %eax, %eax
movq (%rsp), %rbp
jmp 0x12fef9
movq (%rsp), %rbp
movq 0x8(%rbp), %rax
cmpl $0x0, 0x10d0(%rax)
sete %al
movq 0x50(%rbp), %rcx
movq (%rcx,%r15,8), %rdx
movq (%rdx), %rdx
movq 0x8(%rsp), %rsi
movl (%rdx,%rsi,4), %edx
cmpl 0x88(%rbp), %edx
jle 0x12ff18
testb %al, %al
je 0x12ff7c
movq 0x8(%rbp), %rbx
movq 0x68(%rbp), %rax
leaq (,%r15,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rcx, 0x18(%rsp)
movq (%rax,%rcx), %rax
movq 0x8(%rsp), %r13
movl (%rax,%r13,4), %r8d
movq %rbp, %rdi
movq %rbx, %rsi
movl %r15d, %edx
movl %r13d, %ecx
callq 0x1304c2
movq 0x50(%rbp), %rcx
movq (%rcx,%r15,8), %rax
movq (%rax), %rax
incl (%rax,%r13,4)
movq 0x68(%rbp), %rax
movq 0x18(%rsp), %rdx
movq (%rax,%rdx), %rsi
movl (%rsi,%r13,4), %eax
incl %eax
cltd
idivl 0x80(%rbp)
movl %edx, (%rsi,%r13,4)
addq $0x8, %r12
movq 0x10(%rsp), %rbx
cmpq %rbx, %r12
jne 0x12fe0a
movq 0x10(%rbp), %rax
movq %rax, 0x18(%rbp)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x171a3
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x12ffca
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq %rbx, %rdi
callq 0xb510