// File: /workspace/github_c_makefile_star2_O2/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 0x19c4
movq %rdi, %rbx
leaq 0x1712(%rip), %rax      # 0x30bc
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
andq $0x0, 0x24(%rbx)
leaq 0x1758(%rip), %rdi      # 0x3117
jmp 0x1a76
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq $0x4030, %rbx           # imm = 0x4030
pushq $0x40
popq %r14
leaq 0x1703(%rip), %r15      # 0x30e8
leaq 0x170f(%rip), %r12      # 0x30fb
cmpq $0x80, %r14
je 0x1afc
xorl %r13d, %r13d
cmpq $0x100, %r13            # imm = 0x100
je 0x1a2f
testb $0xf, %r13b
jne 0x1a1b
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
jmp 0x19fc
incq %r14
addq $0x100, %rbx            # imm = 0x100
jmp 0x19ec
movl $0x1, 0x24(%rbx)
leaq 0x16e1(%rip), %rdi      # 0x312a
jmp 0x1a76
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x28dc
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x24(%rbx)
leaq 0x16c9(%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 0x29a5
addq $0x30, %rbx
leaq 0x163e(%rip), %r14      # 0x30e8
leaq 0x164a(%rip), %r15      # 0x30fb
xorl %r12d, %r12d
cmpq $0x40, %r12
je 0x1afc
xorl %r13d, %r13d
cmpq $0x100, %r13            # imm = 0x100
je 0x1af0
testb $0xf, %r13b
jne 0x1adc
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
jmp 0x1abd
incq %r12
addq $0x100, %rbx            # imm = 0x100
jmp 0x1ab4
pushq $0xa
popq %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x11b0
nop