// File: /workspace/github_cpp_makefile_star2_O2/daniel5151[P]mips241/src/debug.cc
// Function: MIPS::Debugger::debugREPL()
void Debugger::debugREPL() {
  // If we are not stepping each cycle, then we might be able
  //  to skip some instructions
  if (!cycleStep) {
    // If we are not in Stage 0, don't debug
    if (cpu.getStage() != 0) return;

    // If we are stepping, or there is a breakpoint, debug
    if (step || breakpoints.count(cpu.getPC()) != 0) {
      // do the debug
    } else return;
  }

  debugPrint();

  // the worst REPL you've ever seen
  for (;;) {
    cerr << prevInput << "> "; // prompt

    string input;
    getline(cin, input);

    if (input == "") {
      input = prevInput; // Redo prev input
    }

    prevInput = input; // Save new input in history

    stringstream in_ss (input);

    bool valid = true;

    string tok;
    while (in_ss >> tok) {
      if (tok == "step") {
        step = true;  return;
      }
      else if (tok == "run")  {
        step = false;

        // "clear" screen
        cerr << string(100, '\n');
        return;
      }
      else if (tok == "+bp") {
        uint32_t bp_addr;
        in_ss >> hex >> bp_addr;
        breakpoints.insert(bp_addr);
        higlight[bp_addr] = '!';
      }
      else if (tok == "-bp") {
        uint32_t bp_addr;
        in_ss >> hex >> bp_addr;
        breakpoints.erase(bp_addr);
           higlight.erase(bp_addr);
      }
      else if (tok == "peek") {
        uint32_t memaddr;
        in_ss >> hex >> memaddr;
        cerr << toHex(bus.load(memaddr)) << endl;
      }
      else if (tok == "poke") {
        uint32_t memaddr;
        uint32_t new_val;
        in_ss
          >> hex >> memaddr
          >> hex >> new_val;

        bus.store(memaddr, new_val);
      }
      else if (tok == "+watch") {
        uint32_t memaddr;
        in_ss >> hex >> memaddr;
        watch.insert(memaddr);
      }
      else if (tok == "-watch") {
        uint32_t memaddr;
        in_ss >> hex >> memaddr;
        watch.erase(memaddr);
      }
      else if (tok == "print") {
        debugPrint();
      }
      else if (tok == "help") {
        cerr
          << "  step              - exec one instruction"         << endl
          << "  run               - resume regular execution"     << endl
          << "  +bp <addr>        - add breakpoint at <addr>"     << endl
          << "  -bp <addr>        - remove breakpoint at <addr>"  << endl
          << "  peek <addr>       - print out contents of <addr>" << endl
          << "  poke <addr> <val> - edit contents of <addr>"      << endl
          << "  +watch <addr>     - keep an eye on <addr>"        << endl
          << "  -watch <addr>     - don't keep an eye on <addr>"  << endl
          << "  print             - display debug data"           << endl;
      }

      else {
        valid = false;
        break;

      }
    }
    if (!valid) cerr << "Unknown command." << endl;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rdi, %rbx
cmpb $0x0, 0x48(%rdi)
je 0x4615
movq %rbx, %rdi
callq 0x3f1a
leaq 0xb0(%rbx), %rsi
leaq 0x38(%rsp), %r12
leaq 0x80(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x50(%rbx), %rax
movq %rax, 0x58(%rsp)
movq %rbx, 0x48(%rsp)
leaq 0x18(%rbx), %rax
movq %rax, 0x50(%rsp)
movq 0x6ea6(%rip), %rdi      # 0xaff0
leaq 0x314a(%rip), %r13      # 0x729b
movq 0x6e80(%rip), %rbx      # 0xafd8
leaq 0x28(%rsp), %rbp
leaq 0x80(%rsp), %r15
leaq 0x60(%rsp), %r14
movq %rsi, 0x10(%rsp)
callq 0x21d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x21e0
movq %r12, 0x28(%rsp)
andq $0x0, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2050
movq %rbp, %rdi
leaq 0x2e61(%rip), %rsi      # 0x7005
callq 0x32ea
testb %al, %al
je 0x41ba
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x2120
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x2120
leaq 0xa0(%rsp), %rdi
movq %rbp, %rsi
pushq $0x18
popq %rdx
callq 0x2320
movq 0x58(%rsp), %r12
movq 0x50(%rsp), %r13
movq 0x48(%rsp), %rbx
leaq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
andq $0x0, 0x88(%rsp)
movb $0x0, 0x90(%rsp)
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2290
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x45d4
movq %r15, %rdi
leaq 0x2ed1(%rip), %rsi      # 0x710a
callq 0x32ea
testb %al, %al
jne 0x4658
movq %r15, %rdi
leaq 0x304e(%rip), %rsi      # 0x729e
callq 0x32ea
testb %al, %al
jne 0x465e
movq %r15, %rdi
leaq 0x303b(%rip), %rsi      # 0x72a2
callq 0x32ea
testb %al, %al
je 0x42b9
movq 0xa0(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xb8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xb8(%rsp,%rax)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2390
movq %r12, %rdi
movq %r14, %rsi
callq 0x4efa
movq %r13, %rdi
movq %r14, %rsi
callq 0x4740
movb $0x21, (%rax)
jmp 0x4212
movq %r15, %rdi
leaq 0x2fe3(%rip), %rsi      # 0x72a6
callq 0x32ea
testb %al, %al
je 0x4312
movq 0xa0(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xb8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xb8(%rsp,%rax)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2390
movq %r12, %rdi
movq %r14, %rsi
callq 0x4fa8
movq %r13, %rdi
movq %r14, %rsi
callq 0x4d24
jmp 0x4212
movq %r15, %rdi
leaq 0x2f8e(%rip), %rsi      # 0x72aa
callq 0x32ea
testb %al, %al
je 0x4394
movq 0xa0(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xb8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xb8(%rsp,%rax)
movq %rbp, %rdi
leaq 0xc(%rsp), %rsi
callq 0x2390
movq 0x10(%rbx), %rdi
movl 0xc(%rsp), %esi
callq 0x25e0
movl %eax, 0x1c(%rsp)
movq %r14, %rdi
leaq 0x1c(%rsp), %rsi
callq 0x478e
movq 0x6c79(%rip), %rdi      # 0xaff0
movq %r14, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x20f0
movq %r14, %rdi
callq 0x23e8
jmp 0x4212
movq %r15, %rdi
leaq 0x2f11(%rip), %rsi      # 0x72af
callq 0x32ea
testb %al, %al
je 0x440a
movq 0xa0(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xb8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xb8(%rsp,%rax)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2390
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
leaq 0xc(%rsp), %rsi
callq 0x2390
movq 0x10(%rbx), %rdi
movl 0x60(%rsp), %esi
movl 0xc(%rsp), %edx
callq 0x26a4
jmp 0x4212
movq %r15, %rdi
leaq 0x2ea0(%rip), %rsi      # 0x72b4
callq 0x32ea
testb %al, %al
je 0x445a
movq 0xa0(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xb8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xb8(%rsp,%rax)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2390
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x4efa
jmp 0x4212
movq %r15, %rdi
leaq 0x2e57(%rip), %rsi      # 0x72bb
callq 0x32ea
testb %al, %al
je 0x44aa
movq 0xa0(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xb8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xb8(%rsp,%rax)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2390
movq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x4fa8
jmp 0x4212
movq %r15, %rdi
leaq 0x2e0e(%rip), %rsi      # 0x72c2
callq 0x32ea
testb %al, %al
je 0x44ca
movq %rbx, %rdi
callq 0x3f1a
jmp 0x4212
movq %r15, %rdi
leaq 0x2df4(%rip), %rsi      # 0x72c8
callq 0x32ea
testb %al, %al
je 0x45b9
movq 0x6b08(%rip), %rdi      # 0xaff0
leaq 0x2dde(%rip), %rsi      # 0x72cd
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
leaq 0x2df2(%rip), %rsi      # 0x72f8
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
leaq 0x2e0a(%rip), %rsi      # 0x7327
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
leaq 0x2e22(%rip), %rsi      # 0x7356
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
leaq 0x2e3d(%rip), %rsi      # 0x7388
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
leaq 0x2e59(%rip), %rsi      # 0x73bb
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
leaq 0x2e70(%rip), %rsi      # 0x73e9
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
leaq 0x2e85(%rip), %rsi      # 0x7415
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %rax, %rdi
leaq 0x2ea0(%rip), %rsi      # 0x7447
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
jmp 0x4212
movq 0x6a30(%rip), %rdi      # 0xaff0
leaq 0x2ea9(%rip), %rsi      # 0x7470
callq 0x21e0
movq %rax, %rdi
callq 0x20f0
movq %r15, %rdi
callq 0x23e8
movq %rbp, %rdi
callq 0x2190
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
callq 0x23e8
movq 0x10(%rsp), %rsi
leaq 0x38(%rsp), %r12
movq 0x69ee(%rip), %rdi      # 0xaff0
leaq 0x2c92(%rip), %r13      # 0x729b
movq 0x69c8(%rip), %rbx      # 0xafd8
jmp 0x416f
movq (%rbx), %rdi
callq 0x2e20
testl %eax, %eax
jne 0x46ba
cmpb $0x0, 0x49(%rbx)
jne 0x410c
leaq 0x50(%rbx), %r14
movq (%rbx), %rdi
callq 0x2e28
leaq 0xa0(%rsp), %rsi
movl %eax, (%rsi)
movq %r14, %rdi
callq 0x4848
testq %rax, %rax
jne 0x410c
jmp 0x46ba
movb $0x1, 0x49(%rbx)
jmp 0x4696
movb $0x0, 0x49(%rbx)
leaq 0x70(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x60(%rsp), %rdi
pushq $0x64
popq %rsi
pushq $0xa
popq %rdx
callq 0x2250
movq 0x696e(%rip), %rdi      # 0xaff0
leaq 0x60(%rsp), %rsi
callq 0x21d0
leaq 0x60(%rsp), %rdi
callq 0x23e8
leaq 0x80(%rsp), %rdi
callq 0x23e8
leaq 0xa0(%rsp), %rdi
callq 0x2190
leaq 0x28(%rsp), %rdi
callq 0x23e8
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x46f6
jmp 0x46f6
jmp 0x46dc
jmp 0x46f6
jmp 0x46f6
jmp 0x46ed
jmp 0x46f6
jmp 0x46f6
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x23e8
jmp 0x46f9
jmp 0x46f6
movq %rax, %rbx
jmp 0x4713
jmp 0x46f6
jmp 0x46f6
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x23e8
leaq 0xa0(%rsp), %rdi
callq 0x2190
leaq 0x28(%rsp), %rdi
callq 0x23e8
movq %rbx, %rdi
callq 0x23a0
nop