// File: /workspace/github_c_makefile_star2_O0/algodesigner[P]z80/z80.c
// Function: z80_debug
void z80_debug(z80 *cpu) {
    uint8_t ch = 0;
    uint16_t pos, l;
    static const char Flags[9] = "SZ5H3PNC";
    uint8_t J, I;
    unsigned int bpoint;
    bool loop = true;

    while (loop) {
        pos = PC;
        _puts("\r\n");
        _puts("BC:");  _puthex16(BC);
        _puts(" DE:"); _puthex16(DE);
        _puts(" HL:"); _puthex16(HL);
        _puts(" AF:"); _puthex16(AF);
        _puts(" : [");
        for (J = 0, I = LOW_REGISTER(AF); J < 8; ++J, I <<= 1) _putcon(I & 0x80 ? Flags[J] : '.');
        _puts("]\r\n");
        _puts("IX:");  _puthex16(IX);
        _puts(" IY:"); _puthex16(IY);
        _puts(" SP:"); _puthex16(SP);
        _puts(" PC:"); _puthex16(PC);
        _puts(" : ");

        disasm(cpu, pos);

        if (PC == 0x0005) {
            if (LOW_REGISTER(BC) > 40) {
                _puts(" (Unknown)");
            } else {
                _puts(" (");
                _puts(CPMCalls[LOW_REGISTER(BC)]);
                _puts(")");
            }
        }

        if (cpu->watch != -1) {
            watchprint(cpu, cpu->watch);
        }

        _puts("\r\n");
        _puts("Command|? : ");
        ch = _getch();
        if (ch > 21 && ch < 127)
            _putch(ch);
        switch(ch) {
        case 't':
            loop = false;
            break;
        case 'c':
            loop = false;
            _puts("\r\n");
            cpu->debug = 0;
            break;
        case 'b':
            _puts("\r\n"); memdump(cpu, BC); break;
        case 'd':
            _puts("\r\n"); memdump(cpu, DE); break;
        case 'h':
            _puts("\r\n"); memdump(cpu, HL); break;
        case 'p':
            _puts("\r\n"); memdump(cpu, PC & 0xFF00); break;
        case 's':
            _puts("\r\n"); memdump(cpu, SP & 0xFF00); break;
        case 'x':
            _puts("\r\n"); memdump(cpu, IX & 0xFF00); break;
        case 'y':
            _puts("\r\n"); memdump(cpu, IY & 0xFF00); break;
/*
        case 'a':
            _puts("\r\n"); memdump(dmaAddr); break;
*/
            case 'l':
            _puts("\r\n");
            I = 16;
            l = pos;
            while (I > 0) {
                _puthex16(l);
                _puts(" : ");
                l += disasm(cpu, l);
                _puts("\r\n");
                --I;
            }
            break;
        case 'B':
            _puts(" Addr: ");
            scanf("%04x", &bpoint);
            cpu->brk = bpoint;
            _puts("Breakpoint set to ");
            _puthex16(cpu->brk);
            _puts("\r\n");
            break;
        case 'C':
            cpu->brk = -1;
            _puts(" Breakpoint cleared\r\n");
            break;
        case 'D':
            _puts(" Addr: ");
            scanf("%04x", &bpoint);
            memdump(cpu, bpoint);
            break;
        case 'L':
            _puts(" Addr: ");
            scanf("%04x", &bpoint);
            I = 16;
            l = bpoint;
            while (I > 0) {
                _puthex16(l);
                _puts(" : ");
                l += disasm(cpu, l);
                _puts("\r\n");
                --I;
            }
            break;
        case 'T':
            loop = false;
            cpu->step = pos + 3; // This only works correctly with CALL
                            // If the called function messes with the stack, this will fail as well.
            cpu->debug = 0;
            break;
        case 'W':
            _puts(" Addr: ");
            scanf("%04x", &bpoint);
            cpu->watch = bpoint;
            _puts("Watch set to ");
            _puthex16(cpu->watch);
            _puts("\r\n");
            break;
        case 'q':
            _puts("\r\n");
            loop = false;
            cpu->status = 1;
            break;
        case '?':
            _puts("\r\n");
            _puts("Lowercase commands:\r\n");
            _puts("  t - traces to the next instruction\r\n");
            _puts("  c - Continue execution\r\n");
            _puts("  b - Dumps memory pointed by (BC)\r\n");
            _puts("  d - Dumps memory pointed by (DE)\r\n");
            _puts("  h - Dumps memory pointed by (HL)\r\n");
            _puts("  p - Dumps the page (PC) points to\r\n");
            _puts("  s - Dumps the page (SP) points to\r\n");
            _puts("  x - Dumps the page (IX) points to\r\n");
            _puts("  y - Dumps the page (IY) points to\r\n");
//          _puts("  a - Dumps memory pointed by dmaAddr\r\n");
            _puts("  l - Disassembles from current PC\r\n");
            _puts("  q - Quit simulation\r\n");
            _puts("Uppercase commands:\r\n");
            _puts("  B - Sets breakpoint at address\r\n");
            _puts("  C - Clears breakpoint\r\n");
            _puts("  D - Dumps memory at address\r\n");
            _puts("  L - Disassembles at address\r\n");
            _puts("  T - Steps over a call\r\n");
            _puts("  W - Sets a byte/word watch\r\n");
            break;
        default:
            _puts(" ???\r\n");
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movb $0x0, -0x9(%rbp)
movb $0x1, -0x15(%rbp)
testb $0x1, -0x15(%rbp)
je 0xa2d5
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movw %ax, -0xc(%rbp)
leaq 0x10117(%rip), %rdi     # 0x19d37
callq 0x92f0
leaq 0x10003(%rip), %rdi     # 0x19c2f
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movzwl %ax, %edi
callq 0x93b0
leaq 0xffec(%rip), %rdi      # 0x19c33
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
movzwl %ax, %edi
callq 0x93b0
leaq 0xffd6(%rip), %rdi      # 0x19c38
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0x10(%rax), %eax
movzwl %ax, %edi
callq 0x93b0
leaq 0xffc0(%rip), %rdi      # 0x19c3d
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
movzwl %ax, %edi
callq 0x93b0
leaq 0xffaa(%rip), %rdi      # 0x19c42
callq 0x92f0
movb $0x0, -0xf(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%rax), %eax
andl $0xff, %eax
movb %al, -0x10(%rbp)
movzbl -0xf(%rbp), %eax
cmpl $0x8, %eax
jge 0x9d05
movzbl -0x10(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x9cdd
movzbl -0xf(%rbp), %eax
movl %eax, %ecx
leaq 0xd50c(%rip), %rax      # 0x171e0
movsbl (%rax,%rcx), %eax
movl %eax, -0x1c(%rbp)
jmp 0x9ce7
movl $0x2e, %eax
movl %eax, -0x1c(%rbp)
jmp 0x9ce7
movl -0x1c(%rbp), %eax
movzbl %al, %edi
callq 0x92c0
movb -0xf(%rbp), %al
addb $0x1, %al
movb %al, -0xf(%rbp)
movzbl -0x10(%rbp), %eax
shll %eax
movb %al, -0x10(%rbp)
jmp 0x9cb0
leaq 0xff3b(%rip), %rdi      # 0x19c47
callq 0x92f0
leaq 0xff33(%rip), %rdi      # 0x19c4b
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
movzwl %ax, %edi
callq 0x93b0
leaq 0xff1c(%rip), %rdi      # 0x19c4f
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0x18(%rax), %eax
movzwl %ax, %edi
callq 0x93b0
leaq 0xff06(%rip), %rdi      # 0x19c54
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0x20(%rax), %eax
movzwl %ax, %edi
callq 0x93b0
leaq 0xfef0(%rip), %rdi      # 0x19c59
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movzwl %ax, %edi
callq 0x93b0
leaq 0xfef1(%rip), %rdi      # 0x19c75
callq 0x92f0
movq -0x8(%rbp), %rdi
movzwl -0xc(%rbp), %esi
callq 0x9800
movq -0x8(%rbp), %rax
cmpl $0x5, 0x1c(%rax)
jne 0x9df8
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
andl $0xff, %eax
cmpl $0x28, %eax
jle 0x9dbf
leaq 0xfea6(%rip), %rdi      # 0x19c5e
callq 0x92f0
jmp 0x9df6
leaq 0xfea3(%rip), %rdi      # 0x19c69
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
andl $0xff, %eax
movslq %eax, %rcx
leaq 0x16aaf(%rip), %rax     # 0x20890
movq (%rax,%rcx,8), %rdi
callq 0x92f0
leaq 0x10dde(%rip), %rdi     # 0x1abcf
callq 0x92f0
jmp 0x9df8
movq -0x8(%rbp), %rax
cmpl $-0x1, 0x4c(%rax)
je 0x9e15
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x4c(%rax), %eax
movzwl %ax, %esi
callq 0x9500
leaq 0xff1b(%rip), %rdi      # 0x19d37
callq 0x92f0
leaq 0xfe44(%rip), %rdi      # 0x19c6c
callq 0x92f0
callq 0x9290
movb %al, -0x9(%rbp)
movzbl -0x9(%rbp), %eax
cmpl $0x15, %eax
jle 0x9e50
movzbl -0x9(%rbp), %eax
cmpl $0x7f, %eax
jge 0x9e50
movzbl -0x9(%rbp), %edi
callq 0x92a0
movzbl -0x9(%rbp), %eax
addl $-0x3f, %eax
movl %eax, %ecx
movq %rcx, -0x28(%rbp)
subl $0x3a, %eax
ja 0xa2c4
movq -0x28(%rbp), %rax
leaq 0xc19f(%rip), %rcx      # 0x16010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x0, -0x15(%rbp)
jmp 0xa2d0
movb $0x0, -0x15(%rbp)
leaq 0xfea9(%rip), %rdi      # 0x19d37
callq 0x92f0
movq -0x8(%rbp), %rax
movl $0x0, 0x40(%rax)
jmp 0xa2d0
leaq 0xfe8d(%rip), %rdi      # 0x19d37
callq 0x92f0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movzwl %ax, %esi
callq 0x9650
jmp 0xa2d0
leaq 0xfe69(%rip), %rdi      # 0x19d37
callq 0x92f0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
movzwl %ax, %esi
callq 0x9650
jmp 0xa2d0
leaq 0xfe45(%rip), %rdi      # 0x19d37
callq 0x92f0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x10(%rax), %eax
movzwl %ax, %esi
callq 0x9650
jmp 0xa2d0
leaq 0xfe21(%rip), %rdi      # 0x19d37
callq 0x92f0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
movzwl %ax, %esi
callq 0x9650
jmp 0xa2d0
leaq 0xfdf8(%rip), %rdi      # 0x19d37
callq 0x92f0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x20(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
movzwl %ax, %esi
callq 0x9650
jmp 0xa2d0
leaq 0xfdcf(%rip), %rdi      # 0x19d37
callq 0x92f0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
movzwl %ax, %esi
callq 0x9650
jmp 0xa2d0
leaq 0xfda6(%rip), %rdi      # 0x19d37
callq 0x92f0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movl 0x18(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
movzwl %ax, %esi
callq 0x9650
jmp 0xa2d0
leaq 0xfd7d(%rip), %rdi      # 0x19d37
callq 0x92f0
movb $0x10, -0x10(%rbp)
movw -0xc(%rbp), %ax
movw %ax, -0xe(%rbp)
movzbl -0x10(%rbp), %eax
cmpl $0x0, %eax
jle 0xa019
movzwl -0xe(%rbp), %edi
callq 0x93b0
leaq 0xfc91(%rip), %rdi      # 0x19c75
callq 0x92f0
movq -0x8(%rbp), %rdi
movzwl -0xe(%rbp), %esi
callq 0x9800
movzbl %al, %ecx
movzwl -0xe(%rbp), %eax
addl %ecx, %eax
movw %ax, -0xe(%rbp)
leaq 0xfd2d(%rip), %rdi      # 0x19d37
callq 0x92f0
movb -0x10(%rbp), %al
addb $-0x1, %al
movb %al, -0x10(%rbp)
jmp 0x9fcb
jmp 0xa2d0
leaq 0xfc54(%rip), %rdi      # 0x19c79
callq 0x92f0
leaq 0xfc50(%rip), %rdi      # 0x19c81
leaq -0x14(%rbp), %rsi
movb $0x0, %al
callq 0x90c0
movl -0x14(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x44(%rax)
leaq 0xfc39(%rip), %rdi      # 0x19c86
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0x44(%rax), %eax
movzwl %ax, %edi
callq 0x93b0
leaq 0xfccf(%rip), %rdi      # 0x19d37
callq 0x92f0
jmp 0xa2d0
movq -0x8(%rbp), %rax
movl $0xffffffff, 0x44(%rax) # imm = 0xFFFFFFFF
leaq 0xfc15(%rip), %rdi      # 0x19c99
callq 0x92f0
jmp 0xa2d0
leaq 0xfbe4(%rip), %rdi      # 0x19c79
callq 0x92f0
leaq 0xfbe0(%rip), %rdi      # 0x19c81
leaq -0x14(%rbp), %rsi
movb $0x0, %al
callq 0x90c0
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %eax
movzwl %ax, %esi
callq 0x9650
jmp 0xa2d0
leaq 0xfbb2(%rip), %rdi      # 0x19c79
callq 0x92f0
leaq 0xfbae(%rip), %rdi      # 0x19c81
leaq -0x14(%rbp), %rsi
movb $0x0, %al
callq 0x90c0
movb $0x10, -0x10(%rbp)
movl -0x14(%rbp), %eax
movw %ax, -0xe(%rbp)
movzbl -0x10(%rbp), %eax
cmpl $0x0, %eax
jle 0xa137
movzwl -0xe(%rbp), %edi
callq 0x93b0
leaq 0xfb73(%rip), %rdi      # 0x19c75
callq 0x92f0
movq -0x8(%rbp), %rdi
movzwl -0xe(%rbp), %esi
callq 0x9800
movzbl %al, %ecx
movzwl -0xe(%rbp), %eax
addl %ecx, %eax
movw %ax, -0xe(%rbp)
leaq 0xfc0f(%rip), %rdi      # 0x19d37
callq 0x92f0
movb -0x10(%rbp), %al
addb $-0x1, %al
movb %al, -0x10(%rbp)
jmp 0xa0e9
jmp 0xa2d0
movb $0x0, -0x15(%rbp)
movzwl -0xc(%rbp), %ecx
addl $0x3, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x48(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x40(%rax)
jmp 0xa2d0
leaq 0xfb14(%rip), %rdi      # 0x19c79
callq 0x92f0
leaq 0xfb10(%rip), %rdi      # 0x19c81
leaq -0x14(%rbp), %rsi
movb $0x0, %al
callq 0x90c0
movl -0x14(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x4c(%rax)
leaq 0xfb22(%rip), %rdi      # 0x19caf
callq 0x92f0
movq -0x8(%rbp), %rax
movl 0x4c(%rax), %eax
movzwl %ax, %edi
callq 0x93b0
leaq 0xfb8f(%rip), %rdi      # 0x19d37
callq 0x92f0
jmp 0xa2d0
leaq 0xfb7e(%rip), %rdi      # 0x19d37
callq 0x92f0
movb $0x0, -0x15(%rbp)
movq -0x8(%rbp), %rax
movl $0x1, 0x3c(%rax)
jmp 0xa2d0
leaq 0xfb5e(%rip), %rdi      # 0x19d37
callq 0x92f0
leaq 0xfad8(%rip), %rdi      # 0x19cbd
callq 0x92f0
leaq 0xfae2(%rip), %rdi      # 0x19cd3
callq 0x92f0
leaq 0xfafd(%rip), %rdi      # 0x19cfa
callq 0x92f0
leaq 0xfb0c(%rip), %rdi      # 0x19d15
callq 0x92f0
leaq 0xfb25(%rip), %rdi      # 0x19d3a
callq 0x92f0
leaq 0xfb3e(%rip), %rdi      # 0x19d5f
callq 0x92f0
leaq 0xfb57(%rip), %rdi      # 0x19d84
callq 0x92f0
leaq 0xfb71(%rip), %rdi      # 0x19daa
callq 0x92f0
leaq 0xfb8b(%rip), %rdi      # 0x19dd0
callq 0x92f0
leaq 0xfba5(%rip), %rdi      # 0x19df6
callq 0x92f0
leaq 0xfbbf(%rip), %rdi      # 0x19e1c
callq 0x92f0
leaq 0xfbd8(%rip), %rdi      # 0x19e41
callq 0x92f0
leaq 0xfbe4(%rip), %rdi      # 0x19e59
callq 0x92f0
leaq 0xfbee(%rip), %rdi      # 0x19e6f
callq 0x92f0
leaq 0xfc05(%rip), %rdi      # 0x19e92
callq 0x92f0
leaq 0xfc13(%rip), %rdi      # 0x19eac
callq 0x92f0
leaq 0xfc27(%rip), %rdi      # 0x19ecc
callq 0x92f0
leaq 0xfc3b(%rip), %rdi      # 0x19eec
callq 0x92f0
leaq 0xfc49(%rip), %rdi      # 0x19f06
callq 0x92f0
jmp 0xa2d0
leaq 0xfc5a(%rip), %rdi      # 0x19f25
callq 0x92f0
jmp 0x9c04
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)