// File: /workspace/github_cpp_makefile_star2_O1/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 $0x218, %rsp            # imm = 0x218
movq %rdi, (%rsp)
cmpb $0x0, 0x48(%rdi)
je 0x62e2
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x57dc
leaq 0xb0(%rbx), %rax
movq %rax, 0x40(%rsp)
movl $0x80, %eax
leaq (%rbx,%rax), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x50(%rbx), %r13
leaq 0x18(%rbx), %rbp
leaq 0x90(%rsp), %r15
addq %r15, %rax
movq %rax, 0x88(%rsp)
leaq 0x48(%rsp), %r12
leaq 0x10(%rsp), %rbx
movq (%rsp), %rax
movq 0xb0(%rax), %rsi
movq 0xb8(%rax), %rdx
movq 0x7471(%rip), %rdi      # 0xcff0
callq 0x3250
movl $0x2, %edx
movq %rax, %rdi
leaq 0x3708(%rip), %rsi      # 0x929b
callq 0x3250
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
movq $0x0, 0x70(%rsp)
movb $0x0, 0x78(%rsp)
movq 0x7421(%rip), %rcx      # 0xcfd8
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x31e0
leaq 0x68(%rsp), %r14
movsbl %al, %edx
movq 0x73fe(%rip), %rdi      # 0xcfd8
movq %r14, %rsi
callq 0x33c0
movq %r14, %rdi
leaq 0x3419(%rip), %rsi      # 0x9005
callq 0x3090
testl %eax, %eax
jne 0x5c02
movq %r14, %rdi
movq 0x40(%rsp), %rsi
callq 0x3120
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x3120
movq %r15, %rdi
movq %r14, %rsi
movl $0x18, %edx
callq 0x3320
leaq 0x58(%rsp), %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x50(%rsp)
movb $0x0, 0x58(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x32a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movb $0x1, %r14b
testb $0x5, 0x20(%rax,%rcx)
jne 0x6234
movq %r12, %rdi
leaq 0x34a9(%rip), %rsi      # 0x910a
callq 0x3090
testl %eax, %eax
je 0x61d1
movq %r12, %rdi
leaq 0x3626(%rip), %rsi      # 0x929e
callq 0x3090
testl %eax, %eax
je 0x61db
movq %r12, %rdi
leaq 0x3613(%rip), %rsi      # 0x92a2
callq 0x3090
testl %eax, %eax
je 0x5f7c
movq %r12, %rdi
leaq 0x3600(%rip), %rsi      # 0x92a6
callq 0x3090
testl %eax, %eax
je 0x5fc5
movq %r12, %rdi
leaq 0x35ed(%rip), %rsi      # 0x92aa
callq 0x3090
testl %eax, %eax
je 0x6021
movq %r12, %rdi
leaq 0x35db(%rip), %rsi      # 0x92af
callq 0x3090
testl %eax, %eax
je 0x60d5
movq %r12, %rdi
leaq 0x35c9(%rip), %rsi      # 0x92b4
callq 0x3090
testl %eax, %eax
je 0x613c
movq %r12, %rdi
leaq 0x35b9(%rip), %rsi      # 0x92bb
callq 0x3090
testl %eax, %eax
je 0x6179
movq %r12, %rdi
leaq 0x35a9(%rip), %rsi      # 0x92c2
callq 0x3090
testl %eax, %eax
je 0x61c3
movq %r12, %rdi
leaq 0x3598(%rip), %rsi      # 0x92c8
callq 0x3090
testl %eax, %eax
jne 0x6293
movl $0x2a, %edx
movq 0x72a7(%rip), %rdi      # 0xcff0
leaq 0x357d(%rip), %rsi      # 0x92cd
callq 0x3250
movq 0x7294(%rip), %rcx      # 0xcff0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq 0x7276(%rip), %rdi      # 0xcff0
callq 0x3040
movq %rax, %rdi
callq 0x3160
movq %rax, %r14
movl $0x2e, %edx
movq %rax, %rdi
leaq 0x355f(%rip), %rsi      # 0x92f8
callq 0x3250
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3160
movq %rax, %r14
movl $0x2e, %edx
movq %rax, %rdi
leaq 0x3550(%rip), %rsi      # 0x9327
callq 0x3250
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3160
movq %rax, %r14
movl $0x31, %edx
movq %rax, %rdi
leaq 0x3541(%rip), %rsi      # 0x9356
callq 0x3250
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3160
movq %rax, %r14
movl $0x32, %edx
movq %rax, %rdi
leaq 0x3535(%rip), %rsi      # 0x9388
callq 0x3250
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3160
movq %rax, %r14
movl $0x2d, %edx
movq %rax, %rdi
leaq 0x352a(%rip), %rsi      # 0x93bb
callq 0x3250
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3160
movq %rax, %r14
movl $0x2b, %edx
movq %rax, %rdi
leaq 0x351a(%rip), %rsi      # 0x93e9
callq 0x3250
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3160
movq %rax, %r14
movl $0x31, %edx
movq %rax, %rdi
leaq 0x3508(%rip), %rsi      # 0x9415
callq 0x3250
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3160
movq %rax, %r14
movl $0x28, %edx
movq %rax, %rdi
leaq 0x34fc(%rip), %rsi      # 0x9447
callq 0x3250
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3160
jmp 0x5c37
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xa8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xa8(%rsp,%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3390
movq %r13, %rdi
movq %rbx, %rsi
callq 0x6a50
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x6406
movb $0x21, (%rax)
jmp 0x5c37
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xa8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xa8(%rsp,%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3390
movq %r13, %rdi
movq %rbx, %rsi
callq 0x6afa
movq %r13, %rdi
movq %rax, %rsi
callq 0x6b72
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x6932
movq %rbp, %rdi
movq %rax, %rsi
callq 0x69aa
jmp 0x5c37
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xa8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xa8(%rsp,%rax)
movq %r15, %rdi
leaq 0xc(%rsp), %rsi
callq 0x3390
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movl 0xc(%rsp), %esi
callq 0x361c
movl %eax, 0x3c(%rsp)
movq %rbx, %rdi
leaq 0x3c(%rsp), %rsi
callq 0x646a
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x6f6f(%rip), %rdi      # 0xcff0
callq 0x3250
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3160
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5c37
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x5c37
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xa8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xa8(%rsp,%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3390
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 0x3390
movq (%rsp), %rax
movq 0x10(%rax), %rdi
movl 0x10(%rsp), %esi
movl 0xc(%rsp), %edx
callq 0x3744
jmp 0x5c37
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xa8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xa8(%rsp,%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3390
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x6a50
jmp 0x5c37
movq 0x90(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xa8(%rsp,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0xa8(%rsp,%rax)
movq %r15, %rdi
movq %rbx, %rsi
callq 0x3390
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x6afa
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x6b72
jmp 0x5c37
movq (%rsp), %rdi
callq 0x57dc
jmp 0x5c37
movq (%rsp), %rax
movb $0x1, 0x49(%rax)
jmp 0x6231
movq (%rsp), %rax
movb $0x0, 0x49(%rax)
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movl $0x64, %esi
movq %rbx, %rdi
movl $0xa, %edx
callq 0x3270
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x6de0(%rip), %rdi      # 0xcff0
callq 0x3250
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6231
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
xorl %r14d, %r14d
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x6250
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3200
movq %r15, %rdi
movq 0x6d3e(%rip), %rsi      # 0xcf98
callq 0x31a0
movq 0x88(%rsp), %rdi
callq 0x30a0
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x6288
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3200
testb %r14b, %r14b
jne 0x5b66
jmp 0x62f2
movl $0x10, %edx
movq 0x6d51(%rip), %rdi      # 0xcff0
leaq 0x31ca(%rip), %rsi      # 0x9470
callq 0x3250
movq 0x6d3e(%rip), %rcx      # 0xcff0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x31e0
movsbl %al, %esi
movq 0x6d20(%rip), %rdi      # 0xcff0
callq 0x3040
movq %rax, %rdi
callq 0x3160
jmp 0x6234
movq (%rsp), %rax
movq (%rax), %rdi
callq 0x3edc
testl %eax, %eax
je 0x6304
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rax
cmpb $0x0, 0x49(%rax)
jne 0x5b1b
movq (%rsp), %rbx
movq (%rbx), %rdi
callq 0x3ee4
movq 0x60(%rbx), %rsi
leaq 0x58(%rbx), %rcx
movq %rcx, %rdx
testq %rsi, %rsi
je 0x6348
movq %rcx, %rdx
xorl %edi, %edi
cmpl %eax, 0x20(%rsi)
setb %dil
cmovaeq %rsi, %rdx
movq 0x10(%rsi,%rdi,8), %rsi
testq %rsi, %rsi
jne 0x6331
movq %rcx, %rsi
cmpq %rcx, %rdx
je 0x635a
cmpl 0x20(%rdx), %eax
cmovbq %rcx, %rdx
movq %rdx, %rsi
cmpq %rcx, %rsi
jne 0x5b1b
jmp 0x62f2
jmp 0x63a1
jmp 0x637c
jmp 0x63a1
jmp 0x6377
jmp 0x63a1
jmp 0x63a1
jmp 0x63a1
jmp 0x63a1
jmp 0x63a1
movq %rax, %rbx
jmp 0x63e1
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x63a4
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x63a4
jmp 0x63a1
jmp 0x63a1
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x63c0
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x6bd1(%rip), %rsi      # 0xcf98
leaq 0x90(%rsp), %rdi
callq 0x31a0
leaq 0x110(%rsp), %rdi
callq 0x30a0
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x63fd
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3200
movq %rbx, %rdi
callq 0x33a0
nop