// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movb $0x1, %al
movl %eax, -0x30(%rbp)
leaq 0x811c(%rip), %r14      # 0x11840
movzwl 0x1c(%rbx), %r15d
movq %r14, %r12
movq %r14, %rdi
callq 0x927a
leaq 0x7ffd(%rip), %rdi      # 0x11738
callq 0x927a
movzwl 0x8(%rbx), %edi
callq 0x92f4
leaq 0x7fec(%rip), %rdi      # 0x1173c
callq 0x927a
movzwl 0xc(%rbx), %edi
callq 0x92f4
leaq 0x7fdc(%rip), %rdi      # 0x11741
callq 0x927a
movzwl 0x10(%rbx), %edi
callq 0x92f4
leaq 0x7fcc(%rip), %rdi      # 0x11746
callq 0x927a
movzwl 0x4(%rbx), %edi
callq 0x92f4
leaq 0x7fbc(%rip), %rdi      # 0x1174b
callq 0x927a
movl 0x4(%rbx), %r14d
xorl %r13d, %r13d
movb $0x2e, %al
testb %r14b, %r14b
jns 0x97ae
leaq 0x7f86(%rip), %rax      # 0x1172f
movb (%r13,%rax), %al
movzbl %al, %edi
callq 0x9267
incq %r13
movzbl %r14b, %r14d
addl %r14d, %r14d
cmpq $0x8, %r13
jne 0x979b
leaq 0x7f83(%rip), %rdi      # 0x11750
callq 0x927a
leaq 0x7f7b(%rip), %rdi      # 0x11754
callq 0x927a
movzwl 0x14(%rbx), %edi
callq 0x92f4
leaq 0x7f6a(%rip), %rdi      # 0x11758
callq 0x927a
movzwl 0x18(%rbx), %edi
callq 0x92f4
leaq 0x7f5a(%rip), %rdi      # 0x1175d
callq 0x927a
movzwl 0x20(%rbx), %edi
callq 0x92f4
leaq 0x7f4a(%rip), %rdi      # 0x11762
callq 0x927a
movzwl 0x1c(%rbx), %edi
callq 0x92f4
leaq 0x7f51(%rip), %rdi      # 0x1177e
callq 0x927a
movq %rbx, %rdi
movl %r15d, %esi
callq 0x958a
cmpl $0x5, 0x1c(%rbx)
movq %r12, %r14
jne 0x9885
movzbl 0x8(%rbx), %eax
leaq 0x7f16(%rip), %rdi      # 0x11767
cmpl $0x28, %eax
ja 0x9880
leaq 0x7f15(%rip), %rdi      # 0x11772
callq 0x927a
movzbl 0x8(%rbx), %eax
leaq 0x55df(%rip), %rcx      # 0xee4c
movslq (%rcx,%rax,4), %rdi
addq %rcx, %rdi
callq 0x927a
leaq 0x8e58(%rip), %rdi      # 0x126d8
callq 0x927a
cmpl $-0x1, 0x4c(%rbx)
je 0x9895
movq %rbx, %rdi
xorl %esi, %esi
callq 0x938c
movq %r14, %rdi
callq 0x927a
leaq 0x7ed1(%rip), %rdi      # 0x11775
callq 0x927a
callq 0x9252
leal -0x16(%rax), %ecx
movzbl %al, %r13d
cmpb $0x68, %cl
ja 0x98c2
movl %r13d, %edi
callq 0x925d
cmpl $0x56, %r13d
jle 0x98f6
leal -0x62(%r13), %eax
cmpl $0x17, %eax
ja 0x9a90
leaq 0x474c(%rip), %rcx      # 0xe028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x927a
movzwl 0x8(%rbx), %esi
jmp 0x9c13
leal -0x3f(%r13), %eax
cmpl $0x5, %eax
ja 0x99ff
leaq 0x4706(%rip), %rcx      # 0xe010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x927a
leaq 0x7ea4(%rip), %rdi      # 0x117c6
callq 0x927a
leaq 0x7eae(%rip), %rdi      # 0x117dc
callq 0x927a
leaq 0x7ec9(%rip), %rdi      # 0x11803
callq 0x927a
leaq 0x7ed8(%rip), %rdi      # 0x1181e
callq 0x927a
leaq 0x7ef1(%rip), %rdi      # 0x11843
callq 0x927a
leaq 0x7f0a(%rip), %rdi      # 0x11868
callq 0x927a
leaq 0x7f23(%rip), %rdi      # 0x1188d
callq 0x927a
leaq 0x7f3d(%rip), %rdi      # 0x118b3
callq 0x927a
leaq 0x7f57(%rip), %rdi      # 0x118d9
callq 0x927a
leaq 0x7f71(%rip), %rdi      # 0x118ff
callq 0x927a
leaq 0x7f8b(%rip), %rdi      # 0x11925
callq 0x927a
leaq 0x7fa4(%rip), %rdi      # 0x1194a
callq 0x927a
leaq 0x7fb0(%rip), %rdi      # 0x11962
callq 0x927a
leaq 0x7fba(%rip), %rdi      # 0x11978
callq 0x927a
leaq 0x7fd1(%rip), %rdi      # 0x1199b
callq 0x927a
leaq 0x7fdf(%rip), %rdi      # 0x119b5
callq 0x927a
leaq 0x7ff3(%rip), %rdi      # 0x119d5
callq 0x927a
leaq 0x8007(%rip), %rdi      # 0x119f5
callq 0x927a
leaq 0x8015(%rip), %rdi      # 0x11a0f
jmp 0x9adb
cmpl $0x4c, %r13d
je 0x9ae5
cmpl $0x54, %r13d
jne 0x9ad4
addl $0x3, %r15d
movl %r15d, 0x48(%rbx)
jmp 0x9bc7
leaq 0x7d5b(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7d57(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movzwl -0x2c(%rbp), %esi
jmp 0x9c13
movl $0xffffffff, 0x44(%rbx) # imm = 0xFFFFFFFF
leaq 0x7d4d(%rip), %rdi      # 0x117a2
jmp 0x9adb
leaq 0x7d21(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7d1d(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movl -0x2c(%rbp), %eax
movl %eax, 0x44(%rbx)
leaq 0x7d0a(%rip), %rdi      # 0x1178f
callq 0x927a
movzwl 0x44(%rbx), %edi
jmp 0x9aca
cmpl $0x57, %r13d
jne 0x9ad4
leaq 0x7ce5(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7ce1(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movl -0x2c(%rbp), %eax
movl %eax, 0x4c(%rbx)
leaq 0x7cf7(%rip), %rdi      # 0x117b8
callq 0x927a
movzwl 0x4c(%rbx), %edi
callq 0x92f4
movq %r14, %rdi
jmp 0x9adb
leaq 0x7f53(%rip), %rdi      # 0x11a2e
callq 0x927a
jmp 0x9c1b
leaq 0x7c96(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7c92(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movq %r14, %r12
movb $-0x10, %r14b
movl -0x2c(%rbp), %r13d
movzwl %r13w, %r15d
movl %r15d, %edi
callq 0x92f4
leaq 0x7c5e(%rip), %rdi      # 0x1177e
callq 0x927a
movq %rbx, %rdi
movl %r15d, %esi
callq 0x958a
movzbl %al, %eax
addl %eax, %r13d
movq %r12, %rdi
callq 0x927a
incb %r14b
jne 0x9b0d
jmp 0x9bac
movq %r14, %rdi
callq 0x927a
movzwl 0x10(%rbx), %esi
jmp 0x9c13
movq %r14, %rdi
callq 0x927a
movzbl 0x21(%rbx), %esi
jmp 0x9c10
movq %r14, %rdi
callq 0x927a
movq %r14, %r12
movb $-0x10, %r14b
movzwl %r15w, %r13d
movl %r13d, %edi
callq 0x92f4
leaq 0x7bf6(%rip), %rdi      # 0x1177e
callq 0x927a
movq %rbx, %rdi
movl %r13d, %esi
callq 0x958a
movzbl %al, %r15d
addl %r13d, %r15d
movq %r12, %rdi
callq 0x927a
incb %r14b
jne 0x9b75
movq %r12, %r14
jmp 0x9c1b
movq %r14, %rdi
callq 0x927a
movzbl 0x1d(%rbx), %esi
jmp 0x9c10
movq %r14, %rdi
callq 0x927a
movl $0x0, 0x40(%rbx)
jmp 0x9bdf
movq %r14, %rdi
callq 0x927a
movl $0x1, 0x3c(%rbx)
movl $0x0, -0x30(%rbp)
jmp 0x9c1b
movq %r14, %rdi
callq 0x927a
movzwl 0xc(%rbx), %esi
jmp 0x9c13
movq %r14, %rdi
callq 0x927a
movzbl 0x19(%rbx), %esi
jmp 0x9c10
movq %r14, %rdi
callq 0x927a
movzbl 0x15(%rbx), %esi
shll $0x8, %esi
movq %rbx, %rdi
callq 0x9454
testb $0x1, -0x30(%rbp)
jne 0x9724
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq