// File: /workspace/github_c_makefile_star2_O0/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
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
cmpl $0x4000003b, -0xc(%rbp) # imm = 0x4000003B
jne 0x1c2c
movq -0x8(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x28(%rax)
leaq 0x34bb(%rip), %rdi      # 0x50e0
movb $0x0, %al
callq 0x1040
cmpl $0x4000003c, -0xc(%rbp) # imm = 0x4000003C
jne 0x1c4e
movq -0x8(%rbp), %rax
movl $0x1, 0x24(%rax)
leaq 0x34ad(%rip), %rdi      # 0x50f4
movb $0x0, %al
callq 0x1040
cmpl $0x4000003d, -0xc(%rbp) # imm = 0x4000003D
jne 0x1c7b
movq -0x8(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x28(%rax)
leaq 0x3496(%rip), %rdi      # 0x510a
movb $0x0, %al
callq 0x1040
cmpl $0x40000042, -0xc(%rbp) # imm = 0x40000042
jne 0x1c8d
movq -0x8(%rbp), %rdi
callq 0x3f50
cmpl $0x40000043, -0xc(%rbp) # imm = 0x40000043
jne 0x1d2d
movl $0x10, -0x18(%rbp)
movl $0x0, -0x10(%rbp)
cmpl $0x40, -0x10(%rbp)
jge 0x1d1f
movl $0x0, -0x14(%rbp)
cmpl $0x100, -0x14(%rbp)     # imm = 0x100
jge 0x1d12
movl -0x14(%rbp), %eax
cltd
idivl -0x18(%rbp)
cmpl $0x0, %edx
jne 0x1cde
movl -0x10(%rbp), %esi
movl -0x14(%rbp), %edx
leaq 0x343d(%rip), %rdi      # 0x5114
movb $0x0, %al
callq 0x1040
movq -0x8(%rbp), %rax
addq $0x30, %rax
movslq -0x10(%rbp), %rcx
shlq $0x8, %rcx
addq %rcx, %rax
movslq -0x14(%rbp), %rcx
movzbl (%rax,%rcx), %esi
leaq 0x3427(%rip), %rdi      # 0x5127
movb $0x0, %al
callq 0x1040
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x1cb5
jmp 0x1d14
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x1ca8
leaq 0x3711(%rip), %rdi      # 0x5437
movb $0x0, %al
callq 0x1040
cmpl $0x40000044, -0xc(%rbp) # imm = 0x40000044
jne 0x1dd0
movl $0x10, -0x24(%rbp)
movl $0x40, -0x1c(%rbp)
cmpl $0x80, -0x1c(%rbp)
jge 0x1dc2
movl $0x0, -0x20(%rbp)
cmpl $0x100, -0x20(%rbp)     # imm = 0x100
jge 0x1db5
movl -0x20(%rbp), %eax
cltd
idivl -0x24(%rbp)
cmpl $0x0, %edx
jne 0x1d81
movl -0x1c(%rbp), %esi
movl -0x20(%rbp), %edx
leaq 0x339a(%rip), %rdi      # 0x5114
movb $0x0, %al
callq 0x1040
movq -0x8(%rbp), %rax
addq $0x30, %rax
movslq -0x1c(%rbp), %rcx
shlq $0x8, %rcx
addq %rcx, %rax
movslq -0x20(%rbp), %rcx
movzbl (%rax,%rcx), %esi
leaq 0x3384(%rip), %rdi      # 0x5127
movb $0x0, %al
callq 0x1040
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1d58
jmp 0x1db7
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1d48
leaq 0x366e(%rip), %rdi      # 0x5437
movb $0x0, %al
callq 0x1040
cmpl $0x40000045, -0xc(%rbp) # imm = 0x40000045
jne 0x1de2
movq -0x8(%rbp), %rdi
callq 0x3e40
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)