// File: /workspace/github_c_makefile_star2_O3/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
leaq 0x8123(%rip), %r12      # 0x11840
movzwl 0x1c(%rbx), %r15d
movq %r12, %rdi
callq 0x927a
leaq 0x8007(%rip), %rdi      # 0x11738
callq 0x927a
movzwl 0x8(%rbx), %edi
callq 0x92ff
leaq 0x7ff6(%rip), %rdi      # 0x1173c
callq 0x927a
movzwl 0xc(%rbx), %edi
callq 0x92ff
leaq 0x7fe6(%rip), %rdi      # 0x11741
callq 0x927a
movzwl 0x10(%rbx), %edi
callq 0x92ff
leaq 0x7fd6(%rip), %rdi      # 0x11746
callq 0x927a
movzwl 0x4(%rbx), %edi
callq 0x92ff
leaq 0x7fc6(%rip), %rdi      # 0x1174b
callq 0x927a
movq %rbx, %r12
movl 0x4(%rbx), %r14d
xorl %r13d, %r13d
leaq 0x7f94(%rip), %rbx      # 0x1172f
movb $0x2e, %al
testb %r14b, %r14b
jns 0x97a7
movb (%r13,%rbx), %al
movzbl %al, %edi
callq 0x9267
incq %r13
movzbl %r14b, %r14d
addl %r14d, %r14d
cmpq $0x8, %r13
jne 0x979b
leaq 0x7f8a(%rip), %rdi      # 0x11750
callq 0x927a
leaq 0x7f82(%rip), %rdi      # 0x11754
callq 0x927a
movq %r12, %rbx
movzwl 0x14(%r12), %edi
callq 0x92ff
leaq 0x7f6c(%rip), %rdi      # 0x11758
callq 0x927a
movzwl 0x18(%r12), %edi
callq 0x92ff
leaq 0x7f5a(%rip), %rdi      # 0x1175d
callq 0x927a
movzwl 0x20(%r12), %edi
callq 0x92ff
leaq 0x7f48(%rip), %rdi      # 0x11762
callq 0x927a
movzwl 0x1c(%r12), %edi
callq 0x92ff
leaq 0x7f4d(%rip), %rdi      # 0x1177e
callq 0x927a
movq %r12, %rdi
movl %r15d, %esi
callq 0x9588
cmpl $0x5, 0x1c(%r12)
leaq 0x7ff2(%rip), %r14      # 0x11840
jne 0x988f
movzbl 0x8(%rbx), %eax
leaq 0x7f0c(%rip), %rdi      # 0x11767
cmpl $0x28, %eax
ja 0x988a
leaq 0x7f0b(%rip), %rdi      # 0x11772
callq 0x927a
movzbl 0x8(%rbx), %eax
leaq 0x55d5(%rip), %rcx      # 0xee4c
movslq (%rcx,%rax,4), %rdi
addq %rcx, %rdi
callq 0x927a
leaq 0x8e4e(%rip), %rdi      # 0x126d8
callq 0x927a
cmpl $-0x1, 0x4c(%rbx)
je 0x989f
movq %rbx, %rdi
xorl %esi, %esi
callq 0x938a
movq %r14, %rdi
callq 0x927a
leaq 0x7ec7(%rip), %rdi      # 0x11775
callq 0x927a
callq 0x9252
leal -0x16(%rax), %ecx
movzbl %al, %r13d
cmpb $0x68, %cl
ja 0x98cc
movl %r13d, %edi
callq 0x925d
cmpl $0x56, %r13d
jle 0x9900
leal -0x62(%r13), %eax
cmpl $0x17, %eax
ja 0x9ae6
leaq 0x4742(%rip), %rcx      # 0xe028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x927a
movzwl 0x8(%rbx), %esi
jmp 0x9bd3
leal -0x3f(%r13), %eax
cmpl $0x5, %eax
ja 0x9a09
leaq 0x46fc(%rip), %rcx      # 0xe010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x927a
leaq 0x7e9a(%rip), %rdi      # 0x117c6
callq 0x927a
leaq 0x7ea4(%rip), %rdi      # 0x117dc
callq 0x927a
leaq 0x7ebf(%rip), %rdi      # 0x11803
callq 0x927a
leaq 0x7ece(%rip), %rdi      # 0x1181e
callq 0x927a
leaq 0x7ee7(%rip), %rdi      # 0x11843
callq 0x927a
leaq 0x7f00(%rip), %rdi      # 0x11868
callq 0x927a
leaq 0x7f19(%rip), %rdi      # 0x1188d
callq 0x927a
leaq 0x7f33(%rip), %rdi      # 0x118b3
callq 0x927a
leaq 0x7f4d(%rip), %rdi      # 0x118d9
callq 0x927a
leaq 0x7f67(%rip), %rdi      # 0x118ff
callq 0x927a
leaq 0x7f81(%rip), %rdi      # 0x11925
callq 0x927a
leaq 0x7f9a(%rip), %rdi      # 0x1194a
callq 0x927a
leaq 0x7fa6(%rip), %rdi      # 0x11962
callq 0x927a
leaq 0x7fb0(%rip), %rdi      # 0x11978
callq 0x927a
leaq 0x7fc7(%rip), %rdi      # 0x1199b
callq 0x927a
leaq 0x7fd5(%rip), %rdi      # 0x119b5
callq 0x927a
leaq 0x7fe9(%rip), %rdi      # 0x119d5
callq 0x927a
leaq 0x7ffd(%rip), %rdi      # 0x119f5
callq 0x927a
leaq 0x800b(%rip), %rdi      # 0x11a0f
jmp 0x9bf0
cmpl $0x4c, %r13d
jne 0x9be3
leaq 0x7d68(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7d64(%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 0x92ff
leaq 0x7d30(%rip), %rdi      # 0x1177e
callq 0x927a
movq %rbx, %rdi
movl %r15d, %esi
callq 0x9588
movzbl %al, %eax
addl %eax, %r13d
movq %r12, %rdi
callq 0x927a
incb %r14b
jne 0x9a3b
jmp 0x971d
movl $0xffffffff, 0x44(%rbx) # imm = 0xFFFFFFFF
leaq 0x7d1e(%rip), %rdi      # 0x117a2
jmp 0x9bf0
leaq 0x7cf2(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7cee(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movzwl -0x2c(%rbp), %esi
jmp 0x9bd3
leaq 0x7ccb(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7cc7(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movl -0x2c(%rbp), %eax
movl %eax, 0x44(%rbx)
leaq 0x7cb4(%rip), %rdi      # 0x1178f
callq 0x927a
movzwl 0x44(%rbx), %edi
jmp 0x9b24
cmpl $0x57, %r13d
jne 0x9be9
leaq 0x7c8b(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7c87(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movl -0x2c(%rbp), %eax
movl %eax, 0x4c(%rbx)
leaq 0x7c9d(%rip), %rdi      # 0x117b8
callq 0x927a
movzwl 0x4c(%rbx), %edi
callq 0x92ff
movq %r14, %rdi
jmp 0x9bf0
movq %r14, %rdi
callq 0x927a
movzbl 0x1d(%rbx), %esi
jmp 0x9bd0
movq %r14, %rdi
callq 0x927a
movq %r14, %r12
movb $-0x10, %r14b
movzwl %r15w, %r13d
movl %r13d, %edi
callq 0x92ff
leaq 0x7c1b(%rip), %rdi      # 0x1177e
callq 0x927a
movq %rbx, %rdi
movl %r13d, %esi
callq 0x9588
movzbl %al, %r15d
addl %r13d, %r15d
movq %r12, %rdi
callq 0x927a
incb %r14b
jne 0x9b50
jmp 0x971d
movq %r14, %rdi
callq 0x927a
movzwl 0xc(%rbx), %esi
jmp 0x9bd3
movq %r14, %rdi
callq 0x927a
movzwl 0x10(%rbx), %esi
jmp 0x9bd3
movq %r14, %rdi
callq 0x927a
movzbl 0x15(%rbx), %esi
jmp 0x9bd0
movq %r14, %rdi
callq 0x927a
movzbl 0x21(%rbx), %esi
jmp 0x9bd0
movq %r14, %rdi
callq 0x927a
movzbl 0x19(%rbx), %esi
shll $0x8, %esi
movq %rbx, %rdi
callq 0x9452
movq %r14, %r12
jmp 0x971d
cmpl $0x54, %r13d
je 0x9bfd
leaq 0x7e3e(%rip), %rdi      # 0x11a2e
callq 0x927a
movq %r14, %r12
jmp 0x971d
addl $0x3, %r15d
movl %r15d, 0x48(%rbx)
jmp 0x9c28
leaq 0x7c32(%rip), %rdi      # 0x11840
callq 0x927a
movl $0x1, 0x3c(%rbx)
jmp 0x9c2f
leaq 0x7c1d(%rip), %rdi      # 0x11840
callq 0x927a
movl $0x0, 0x40(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq