// File: /workspace/github_cpp_makefile_star2_O2/CMU-SAFARI[P]ramulator/src/SALP.cpp
// Function: ramulator::SALP::init_prereq()
void SALP::init_prereq()
{
    prereq[int(Level::Rank)][int(Command::RD)] = [] (DRAM<SALP>* 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);
        }};
    prereq[int(Level::Rank)][int(Command::WR)] = prereq[int(Level::Rank)][int(Command::RD)];

    switch(int(type)){
        case int(Type::SALP_1):
            prereq[int(Level::Bank)][int(Command::RD)] = [] (DRAM<SALP>* node, Command cmd, int id) {
                switch (int(node->state)){
                    case int(State::Closed): return Command::ACT;
                    case int(State::Opened):
                        return Command::MAX;
                    default: assert(false);}};
            prereq[int(Level::Bank)][int(Command::WR)] = prereq[int(Level::Bank)][int(Command::RD)];
            prereq[int(Level::SubArray)][int(Command::RD)] = [] (DRAM<SALP>* node, Command cmd, int id) {
              if (node->row_state.find(id) != node->row_state.end()) {
                return cmd;
              } else if (node->row_state.size()) {
                return Command::PRE;
              } else {
                return Command::PRE_OTHER;
              }
            };
            prereq[int(Level::SubArray)][int(Command::WR)] = prereq[int(Level::SubArray)][int(Command::RD)];
            prereq[int(Level::Rank)][int(Command::REF)] = [] (DRAM<SALP>* node, Command cmd, int id) {
                for (auto bank : node->children) {
                    if (bank->state == State::Closed)
                        continue;
                    return Command::PRER;
                }
                return Command::REF;};
            break;
        case int(Type::SALP_2):
            prereq[int(Level::SubArray)][int(Command::RD)] = [] (DRAM<SALP>* node, Command cmd, int id) {
                switch (int(node->state)){
                    case int(State::Closed): return Command::ACT;
                    case int(State::Opened):
                        if (node->row_state.find(id) != node->row_state.end()) {
                          for (auto sa : node->parent->children) {
                              if (sa != node && sa->state == State::Opened) {
                                  return Command::PRE_OTHER;
                              }
                          }
                          return cmd;
                        } else {
                          // if this subarray has another row open, close it
                          // first
                          return Command::PRE;
                        }
                    default: assert(false);}};
            prereq[int(Level::SubArray)][int(Command::WR)] = prereq[int(Level::SubArray)][int(Command::RD)];
            prereq[int(Level::Rank)][int(Command::REF)] = [] (DRAM<SALP>* node, Command cmd, int id) {
                for (auto bank : node->children)
                    for (auto sa : bank->children) {
                        if (sa->state == State::Closed)
                            continue;
                        return Command::PRER;
                    }
                return Command::REF;};
            break;
        case int(Type::MASA):
            prereq[int(Level::SubArray)][int(Command::RD)] = [] (DRAM<SALP>* node, Command cmd, int id) {
                switch (int(node->state)){
                    case int(State::Closed): return Command::ACT;
                    case int(State::Opened):
                        if (node->row_state.find(id) != node->row_state.end()) return Command::SASEL;
                        else return Command::PRE;
                    case int(State::Selected):
                        if (node->row_state.find(id) != node->row_state.end()) return cmd;
                        else return Command::PRE;
                    default: assert(false);
                }};
            prereq[int(Level::SubArray)][int(Command::WR)] = prereq[int(Level::SubArray)][int(Command::RD)];
            prereq[int(Level::Rank)][int(Command::REF)] = [] (DRAM<SALP>* node, Command cmd, int id) {
                for (auto bank : node->children)
                    for (auto sa : bank->children){
                        if (sa->state == State::Closed)
                            continue;
                        return Command::PRER;
                    }
                return Command::REF;};
            break;
        default: assert(false);
    }
    // PD
    prereq[int(Level::Rank)][int(Command::PDE)] = [] (DRAM<SALP>* 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<SALP>* 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 %rbx
subq $0x20, %rsp
movq %rdi, %rbx
leaq 0x490(%rdi), %r14
movups 0x490(%rdi), %xmm0
movq %rsp, %rdi
movaps %xmm0, (%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x490(%rbx)
movups 0x4a0(%rbx), %xmm0
leaq 0x2222(%rip), %rax      # 0xc0514
movq %rax, 0x4a0(%rbx)
movaps %xmm0, 0x10(%rdi)
leaq 0x21d4(%rip), %rax      # 0xc04d8
movq %rax, 0x4a8(%rbx)
callq 0x96b6a
leaq 0x4b0(%rbx), %rdi
movq %r14, %rsi
callq 0xc14bc
movl 0x20(%rbx), %eax
cmpl $0x2, %eax
je 0xbe4cc
cmpl $0x1, %eax
je 0xbe433
testl %eax, %eax
jne 0xbe606
leaq 0x650(%rbx), %r14
movups 0x650(%rbx), %xmm0
movq %rsp, %r15
movaps %xmm0, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x650(%rbx)
movups 0x660(%rbx), %xmm0
leaq 0x21fb(%rip), %rax      # 0xc0564
movq %rax, 0x660(%rbx)
movaps %xmm0, 0x10(%r15)
leaq 0x21b0(%rip), %rax      # 0xc052c
movq %rax, 0x668(%rbx)
movq %r15, %rdi
callq 0x96b6a
leaq 0x670(%rbx), %rdi
movq %r14, %rsi
callq 0xc14bc
leaq 0x810(%rbx), %r14
movups 0x810(%rbx), %xmm0
movaps %xmm0, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x810(%rbx)
movups 0x820(%rbx), %xmm0
leaq 0x21fe(%rip), %rax      # 0xc05c2
movq %rax, 0x820(%rbx)
movaps %xmm0, 0x10(%r15)
leaq 0x21a5(%rip), %rax      # 0xc057c
movq %rax, 0x828(%rbx)
movq %rsp, %r15
movq %r15, %rdi
callq 0x96b6a
leaq 0x830(%rbx), %rdi
movq %r14, %rsi
callq 0xc14bc
movups 0x510(%rbx), %xmm0
movaps %xmm0, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x510(%rbx)
movups 0x520(%rbx), %xmm0
leaq 0x21ef(%rip), %rax      # 0xc060a
movq %rax, 0x520(%rbx)
movaps %xmm0, 0x10(%r15)
leaq 0x21ac(%rip), %rax      # 0xc05da
jmp 0xbe560
leaq 0x810(%rbx), %r14
movups 0x810(%rbx), %xmm0
movq %rsp, %r15
movaps %xmm0, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x810(%rbx)
movups 0x820(%rbx), %xmm0
leaq 0x2262(%rip), %rax      # 0xc06c2
movq %rax, 0x820(%rbx)
movaps %xmm0, 0x10(%r15)
leaq 0x21af(%rip), %rax      # 0xc0622
movq %rax, 0x828(%rbx)
movq %r15, %rdi
callq 0x96b6a
leaq 0x830(%rbx), %rdi
movq %r14, %rsi
callq 0xc14bc
movups 0x510(%rbx), %xmm0
movaps %xmm0, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x510(%rbx)
movups 0x520(%rbx), %xmm0
leaq 0x2274(%rip), %rax      # 0xc0728
movq %rax, 0x520(%rbx)
movaps %xmm0, 0x10(%r15)
leaq 0x2213(%rip), %rax      # 0xc06da
jmp 0xbe560
leaq 0x810(%rbx), %r14
movups 0x810(%rbx), %xmm0
movq %rsp, %r15
movaps %xmm0, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x810(%rbx)
movups 0x820(%rbx), %xmm0
leaq 0x22b9(%rip), %rax      # 0xc07b2
movq %rax, 0x820(%rbx)
movaps %xmm0, 0x10(%r15)
leaq 0x2234(%rip), %rax      # 0xc0740
movq %rax, 0x828(%rbx)
movq %r15, %rdi
callq 0x96b6a
leaq 0x830(%rbx), %rdi
movq %r14, %rsi
callq 0xc14bc
movups 0x510(%rbx), %xmm0
movaps %xmm0, (%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x510(%rbx)
movups 0x520(%rbx), %xmm0
leaq 0x22cb(%rip), %rax      # 0xc0818
movq %rax, 0x520(%rbx)
movaps %xmm0, 0x10(%r15)
leaq 0x226a(%rip), %rax      # 0xc07ca
movq %rax, 0x528(%rbx)
movq %rsp, %rdi
callq 0x96b6a
movups 0x530(%rbx), %xmm0
movq %rsp, %r14
movaps %xmm0, (%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x530(%rbx)
movups 0x540(%rbx), %xmm0
leaq 0x22d9(%rip), %rax      # 0xc086e
movq %rax, 0x540(%rbx)
movaps %xmm0, 0x10(%r14)
leaq 0x2288(%rip), %rax      # 0xc0830
movq %rax, 0x548(%rbx)
movq %r14, %rdi
callq 0x96b6a
movups 0x570(%rbx), %xmm0
movaps %xmm0, (%r14)
xorps %xmm0, %xmm0
movups %xmm0, 0x570(%rbx)
movups 0x580(%rbx), %xmm0
leaq 0x22ea(%rip), %rax      # 0xc08c4
movq %rax, 0x580(%rbx)
movaps %xmm0, 0x10(%r14)
leaq 0x2299(%rip), %rax      # 0xc0886
movq %rax, 0x588(%rbx)
movq %rsp, %rdi
callq 0x96b6a
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq
leaq 0x12d4c(%rip), %rdi     # 0xd1359
leaq 0x23f36(%rip), %rsi     # 0xe254a
leaq 0x23f8c(%rip), %rcx     # 0xe25a7
movl $0xc8, %edx
callq 0xb210
nop