// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rdi, %rbx
movq 0x8(%rdi), %rax
movq 0x1098(%rax), %rax
movq 0x3b0(%rax), %r13
movq 0x3b8(%rax), %r15
testl %esi, %esi
je 0xbcba7
pushq $-0x1
popq %r14
cmpq %r15, %r13
je 0xbccdf
movq (%r13), %rax
movq 0x8(%rbx), %rsi
movl 0x39c(%rax), %edx
movq %rbx, %rdi
movl %r14d, %ecx
movl %r14d, %r8d
callq 0xbd00c
addq $0x8, %r13
jmp 0xbcb7c
leaq 0x18(%rsp), %r14
movq %r15, 0x10(%rsp)
cmpq %r15, %r13
je 0xbccdf
movq (%r13), %rax
movslq 0x39c(%rax), %r12
movq 0x20(%rbx), %rcx
movq 0x50(%rbx), %rax
movslq (%rcx,%r12,4), %rdx
movq (%rax,%r12,8), %rax
movq (%rax), %rax
movq %rdx, 0x8(%rsp)
decl (%rax,%rdx,4)
movl (%rcx,%r12,4), %eax
incl %eax
cltd
idivl 0x3c(%rbx)
movl %edx, (%rcx,%r12,4)
movq 0x8(%rbx), %rbp
movq 0x1098(%rbp), %rax
movq 0x390(%rax), %rax
movl (%rax), %eax
cmpl $0x4, %eax
je 0xbcc0c
cmpl $0x2, %eax
jne 0xbcc7b
movq %r13, (%rsp)
addq $0x1120, %rbp           # imm = 0x1120
movq %rbp, %r13
movq (%r13), %r13
cmpq %rbp, %r13
je 0xbcc46
leaq 0x10(%r13), %rsi
movq %r14, %rdi
callq 0x2000a
movl 0x44(%rsp), %r15d
movq %r14, %rdi
callq 0x1cc14
cmpl $0x2, %r15d
jne 0xbcc1a
xorl %eax, %eax
jmp 0xbcc54
movq 0x8(%rbx), %rax
cmpl $0x0, 0x10d0(%rax)
sete %al
movq 0x50(%rbx), %rcx
movq (%rcx,%r12,8), %rcx
movq (%rcx), %rcx
movq 0x8(%rsp), %rdx
movl (%rcx,%rdx,4), %ecx
cmpl 0x88(%rbx), %ecx
movq (%rsp), %r13
jle 0xbcc77
testb %al, %al
je 0xbccd1
movq 0x8(%rbx), %rbp
movq 0x68(%rbx), %rax
imulq $0x18, %r12, %rcx
movq %rcx, (%rsp)
movq (%rax,%rcx), %rax
movq 0x8(%rsp), %r15
movl (%rax,%r15,4), %r8d
movq %rbx, %rdi
movq %rbp, %rsi
movl %r12d, %edx
movl %r15d, %ecx
callq 0xbd00c
movq 0x50(%rbx), %rax
movq (%rax,%r12,8), %rax
movq (%rax), %rax
incl (%rax,%r15,4)
movq 0x68(%rbx), %rax
movq (%rsp), %rcx
movq (%rax,%rcx), %rcx
movl (%rcx,%r15,4), %eax
incl %eax
cltd
idivl 0x80(%rbx)
movl %edx, (%rcx,%r15,4)
addq $0x8, %r13
movq 0x10(%rsp), %r15
jmp 0xbcbb1
movq 0x10(%rbx), %rax
movq %rax, 0x18(%rbx)
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq