// File: /workspace/github_cpp_makefile_star2_O1/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, %r15
movq 0x8(%rdi), %rax
movq 0x1098(%rax), %rax
movq 0x3b0(%rax), %rbp
movq 0x3b8(%rax), %r12
testl %esi, %esi
je 0x132f25
cmpq %r12, %rbp
je 0x1330f1
movq (%rbp), %rax
movq 0x8(%r15), %rsi
movl 0x39c(%rax), %edx
movq %r15, %rdi
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x133626
addq $0x8, %rbp
jmp 0x132ef5
cmpq %r12, %rbp
je 0x1330f1
leaq 0x30(%rsp), %r14
movq %r15, 0x8(%rsp)
movq %r12, (%rsp)
movq (%rbp), %rax
movslq 0x39c(%rax), %rsi
movq 0x20(%r15), %rcx
movq 0x50(%r15), %rax
movslq (%rcx,%rsi,4), %r13
movq (%rax,%rsi,8), %rax
movq (%rax), %rax
decl (%rax,%r13,4)
movl (%rcx,%rsi,4), %eax
incl %eax
cltd
idivl 0x3c(%r15)
movq %rsi, 0x18(%rsp)
movl %edx, (%rcx,%rsi,4)
movq 0x8(%r15), %rbx
movq 0x1098(%rbx), %rax
movq 0x390(%rax), %rax
movl (%rax), %eax
cmpl $0x4, %eax
je 0x132f92
cmpl $0x2, %eax
jne 0x133088
movq %r13, 0x10(%rsp)
addq $0x1120, %rbx           # imm = 0x1120
xorl %r13d, %r13d
movq %rbx, %r12
movq (%r12), %r12
cmpq %rbx, %r12
je 0x13303c
movups 0x10(%r12), %xmm0
movaps %xmm0, 0x20(%rsp)
leaq 0x20(%r12), %rsi
movq %r14, %rdi
callq 0x24b6e
movq 0x48(%r12), %rax
movq %rax, 0x28(%r14)
movups 0x38(%r12), %xmm0
movups %xmm0, 0x18(%r14)
leaq 0x50(%r12), %rsi
leaq 0x60(%rsp), %rdi
callq 0x295b4
movl 0x4c(%rsp), %r15d
cmpl $0x2, %r15d
movzbl %r13b, %r13d
movl $0x1, %eax
cmovel %eax, %r13d
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0x13301b
leaq 0x60(%rsp), %rdi
movq %rdi, %rsi
movl $0x3, %edx
callq *%rax
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x133032
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
cmpl $0x2, %r15d
jne 0x132fa4
testb $0x1, %r13b
je 0x13304b
xorl %eax, %eax
movq 0x8(%rsp), %r15
jmp 0x13305e
movq 0x8(%rsp), %r15
movq 0x8(%r15), %rax
cmpl $0x0, 0x10d0(%rax)
sete %al
movq (%rsp), %r12
movq 0x10(%rsp), %r13
movq 0x50(%r15), %rcx
movq 0x18(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movq (%rcx), %rcx
movl (%rcx,%r13,4), %ecx
cmpl 0x88(%r15), %ecx
jle 0x133088
testb %al, %al
je 0x1330e4
movq 0x8(%r15), %rsi
movq 0x68(%r15), %rax
movq 0x18(%rsp), %r12
leaq (,%r12,8), %rcx
leaq (%rcx,%rcx,2), %rbx
movq (%rax,%rbx), %rax
movl (%rax,%r13,4), %r8d
movq %r15, %rdi
movl %r12d, %edx
movl %r13d, %ecx
callq 0x133626
movq 0x50(%r15), %rax
movq (%rax,%r12,8), %rax
movq (%rsp), %r12
movq (%rax), %rax
incl (%rax,%r13,4)
movq 0x68(%r15), %rax
movq (%rax,%rbx), %rcx
movl (%rcx,%r13,4), %eax
incl %eax
cltd
idivl 0x80(%r15)
movl %edx, (%rcx,%r13,4)
addq $0x8, %rbp
cmpq %r12, %rbp
jne 0x132f3c
movq 0x10(%r15), %rax
movq %rax, 0x18(%r15)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdi
callq 0x1718f
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x13312d
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0xb2f0
movq %rbx, %rdi
callq 0xb510