// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]ramulator/src/DSARP.cpp
// Function: ramulator::DSARP::init_prereq()
void DSARP::init_prereq()
{
  // RD
  prereq[int(Level::Rank)][int(Command::RD)] = [] (DRAM<DSARP>* node, Command cmd, int id) {
    switch (int(node->state)) {
      case int(State::PowerUp): return Command::MAX;
      case int(State::ActPowerDown): return Command::PDX;
      case int(State::PrePowerDown): return Command::PDX;
      case int(State::SelfRefresh): return Command::SRX;
      default: assert(false);
    }};
  // Rank transitions to Bank
  prereq[int(Level::Bank)][int(Command::RD)] = [] (DRAM<DSARP>* node, Command cmd, int id) {
    switch (int(node->state)) {
      case int(State::Closed): return Command::ACT;
      case int(State::Opened):
        // Really is the subarray state. If the subarray matches, check the row ID
        if (node->row_state.find(id) != node->row_state.end())
          return Command::MAX;
        return Command::PRE;
      default: assert(false);
    }};
  // Bank transitions to Subarray
  prereq[int(Level::SubArray)][int(Command::RD)] = [] (DRAM<DSARP>* node, Command cmd, int id) {
    switch (int(node->state)) {
      case int(State::Closed): return Command::ACT;
      case int(State::Opened):
        // Actual row state
        if (node->row_state.find(id) != node->row_state.end())
          return cmd;
        return Command::PRE;
      default: assert(false);
    }};

  // WR
  prereq[int(Level::Rank)][int(Command::WR)] = prereq[int(Level::Rank)][int(Command::RD)];
  prereq[int(Level::Bank)][int(Command::WR)] = prereq[int(Level::Bank)][int(Command::RD)];
  prereq[int(Level::SubArray)][int(Command::WR)] = prereq[int(Level::SubArray)][int(Command::RD)];

  // REF -- on all banks
  prereq[int(Level::Rank)][int(Command::REF)] = [] (DRAM<DSARP>* node, Command cmd, int id) {
    for (auto bank : node->children) {
      if (bank->state == State::Closed)
        continue;
      return Command::PREA;
    }
    return Command::REF;};

  // REF -- per bank
  prereq[int(Level::Bank)][int(Command::REFPB)] = [] (DRAM<DSARP>* node, Command cmd, int id) {
    if (node->state == State::Closed) return Command::REFPB;
    else return Command::PRE;};

  // PD
  prereq[int(Level::Rank)][int(Command::PDE)] = [] (DRAM<DSARP>* node, Command cmd, int id) {
    switch (int(node->state)) {
      case int(State::PowerUp): return Command::PDE;
      case int(State::ActPowerDown): return Command::PDE;
      case int(State::PrePowerDown): return Command::PDE;
      case int(State::SelfRefresh): return Command::SRX;
      default: assert(false);
    }};

  // SR
  prereq[int(Level::Rank)][int(Command::SRE)] = [] (DRAM<DSARP>* node, Command cmd, int id) {
    switch (int(node->state)) {
      case int(State::PowerUp): return Command::SRE;
      case int(State::ActPowerDown): return Command::PDX;
      case int(State::PrePowerDown): return Command::PDX;
      case int(State::SelfRefresh): return Command::SRE;
      default: assert(false);
    }};
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rdi, %rbx
leaq 0x410(%rdi), %r14
movups 0x410(%rdi), %xmm0
movq %rsp, %r12
movaps %xmm0, (%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x410(%rdi)
movups 0x420(%rdi), %xmm0
leaq 0x1e56(%rip), %rax      # 0xa790e
movq %rax, 0x420(%rdi)
movaps %xmm0, 0x10(%r12)
leaq 0x1e04(%rip), %rax      # 0xa78d0
movq %rax, 0x428(%rdi)
movq %r12, %rdi
callq 0x96b6a
leaq 0x5b0(%rbx), %r15
movups 0x5b0(%rbx), %xmm0
movaps %xmm0, (%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x5b0(%rbx)
movups 0x5c0(%rbx), %xmm0
leaq 0x1e88(%rip), %rax      # 0xa798e
movq %rax, 0x5c0(%rbx)
movaps %xmm0, 0x10(%r12)
leaq 0x1e0c(%rip), %rax      # 0xa7926
movq %rax, 0x5c8(%rbx)
movq %rsp, %r12
movq %r12, %rdi
callq 0x96b6a
leaq 0x750(%rbx), %r13
movups 0x750(%rbx), %xmm0
movaps %xmm0, (%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x750(%rbx)
movups 0x760(%rbx), %xmm0
leaq 0x1ebb(%rip), %rax      # 0xa7a12
movq %rax, 0x760(%rbx)
movaps %xmm0, 0x10(%r12)
leaq 0x1e3b(%rip), %rax      # 0xa79a6
movq %rax, 0x768(%rbx)
movq %rsp, %r12
movq %r12, %rdi
callq 0x96b6a
leaq 0x430(%rbx), %rdi
movq %r14, %rsi
callq 0xa8206
leaq 0x5d0(%rbx), %rdi
movq %r15, %rsi
callq 0xa8206
leaq 0x770(%rbx), %rdi
movq %r13, %rsi
callq 0xa8206
movups 0x490(%rbx), %xmm0
movaps %xmm0, (%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0x490(%rbx)
movups 0x4a0(%rbx), %xmm0
leaq 0x1e8c(%rip), %rax      # 0xa7a5a
movq %rax, 0x4a0(%rbx)
movaps %xmm0, 0x10(%r12)
leaq 0x1e48(%rip), %rax      # 0xa7a2a
movq %rax, 0x4a8(%rbx)
movq %rsp, %r14
movq %r14, %rdi
callq 0x96b6a
movups 0x650(%rbx), %xmm0
movaps %xmm0, (%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x650(%rbx)
movups 0x660(%rbx), %xmm0
leaq 0x1e6f(%rip), %rax      # 0xa7a86
movq %rax, 0x660(%rbx)
movaps %xmm0, 0x10(%r14)
leaq 0x1e48(%rip), %rax      # 0xa7a72
movq %rax, 0x668(%rbx)
movq %rsp, %r14
movq %r14, %rdi
callq 0x96b6a
movups 0x4d0(%rbx), %xmm0
movaps %xmm0, (%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x4d0(%rbx)
movups 0x4e0(%rbx), %xmm0
leaq 0x1e7d(%rip), %rax      # 0xa7adc
movq %rax, 0x4e0(%rbx)
movaps %xmm0, 0x10(%r14)
leaq 0x1e2c(%rip), %rax      # 0xa7a9e
movq %rax, 0x4e8(%rbx)
movq %rsp, %r14
movq %r14, %rdi
callq 0x96b6a
movups 0x510(%rbx), %xmm0
movaps %xmm0, (%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x510(%rbx)
movups 0x520(%rbx), %xmm0
leaq 0x1e8b(%rip), %rax      # 0xa7b32
movq %rax, 0x520(%rbx)
movaps %xmm0, 0x10(%r14)
leaq 0x1e3a(%rip), %rax      # 0xa7af4
movq %rax, 0x528(%rbx)
movq %rsp, %rdi
callq 0x96b6a
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nop