// File: /workspace/github_c_makefile_star2_O3/minkcv[P]vm/machine/vm.c
// Function: handleDebugKey
void handleDebugKey(VM* vm, SDL_Keycode key)
{
    if (key == SDLK_F2)
    {
        // Continue
        vm->cpu.breakState = 0;
        vm->cpu.step = 0;
        printf("Resuming execution\n");
    }
    if (key == SDLK_F3)
    {
        // Break all
        vm->cpu.breakState = 1;
        printf("Entering break state\n");
    }
    if (key == SDLK_F4)
    {
        // Step
        vm->cpu.breakState = 0;
        vm->cpu.step = 1;
        printf("Stepping\n");
    }
    if (key == SDLK_F9)
    {
	print_cpu_state(&vm->cpu);
    }
    if (key == SDLK_F10)
    {
        // Dump memory segments 0 through 63 (inclusive)
        int segment;
        int offset;
        int rowLength = 16;
        for (segment = 0; segment < 64; segment++)
        {
            for (offset = 0; offset < 256; offset++)
            {
                if (offset % rowLength == 0)
                    printf("\n0x%02X.0x%02X: 0x", segment, offset);
                printf("%02X", vm->memory[segment][offset]);
            }
        }
        printf("\n");
    }
    if (key == SDLK_F11)
    {
        // Dump memory segments 64 through 127 (inclusive)
        int segment;
        int offset;
        int rowLength = 16;
        for (segment = 64; segment < 128; segment++)
        {
            for (offset = 0; offset < 256; offset++)
            {
                if (offset % rowLength == 0)
                    printf("\n0x%02X.0x%02X: 0x", segment, offset);
                printf("%02X", vm->memory[segment][offset]);
            }
        }
        printf("\n");
    }
    if (key == SDLK_F12)
    {
	// Disassemble next 16 instructions
        print_disasm_block(&vm->cpu);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
addl $0xbfffffc5, %esi       # imm = 0xBFFFFFC5
cmpl $0xa, %esi
ja 0x19e2
movq %rdi, %rbx
leaq 0x16f7(%rip), %rax      # 0x30bc
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq $0x0, 0x24(%rbx)
leaq 0x173a(%rip), %rdi      # 0x3117
jmp 0x1a93
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x4030, %rbx           # imm = 0x4030
movl $0x40, %r14d
leaq 0x16e3(%rip), %r15      # 0x30e8
leaq 0x16ef(%rip), %r12      # 0x30fb
xorl %r13d, %r13d
testb $0xf, %r13b
jne 0x1a25
movq %r15, %rdi
movl %r14d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1040
movzbl (%rbx,%r13), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x1040
incq %r13
cmpq $0x100, %r13            # imm = 0x100
jne 0x1a0f
incq %r14
addq $0x100, %rbx            # imm = 0x100
cmpq $0x80, %r14
jne 0x1a0c
jmp 0x1b15
movl $0x1, 0x24(%rbx)
leaq 0x16c4(%rip), %rdi      # 0x312a
jmp 0x1a93
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x293e
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x24(%rbx)
leaq 0x16ac(%rip), %rdi      # 0x313f
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11d0
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2a37
addq $0x30, %rbx
leaq 0x1621(%rip), %r14      # 0x30e8
leaq 0x162d(%rip), %r15      # 0x30fb
xorl %r12d, %r12d
xorl %r13d, %r13d
testb $0xf, %r13b
jne 0x1aea
movq %r14, %rdi
movl %r12d, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x1040
movzbl (%rbx,%r13), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x1040
incq %r13
cmpq $0x100, %r13            # imm = 0x100
jne 0x1ad4
incq %r12
addq $0x100, %rbx            # imm = 0x100
cmpq $0x40, %r12
jne 0x1ad1
movl $0xa, %edi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11b0
nopl (%rax)