// File: /workspace/github_c_makefile_star2_O2/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 0x8041(%rip), %r12      # 0x1172f
movzwl 0x1c(%rbx), %r15d
leaq 0x8146(%rip), %rdi      # 0x11840
callq 0x927a
leaq 0x8032(%rip), %rdi      # 0x11738
callq 0x927a
movzwl 0x8(%rbx), %edi
callq 0x92eb
leaq 0x8021(%rip), %rdi      # 0x1173c
callq 0x927a
movzwl 0xc(%rbx), %edi
callq 0x92eb
leaq 0x8011(%rip), %rdi      # 0x11741
callq 0x927a
movzwl 0x10(%rbx), %edi
callq 0x92eb
leaq 0x8001(%rip), %rdi      # 0x11746
callq 0x927a
movzwl 0x4(%rbx), %edi
callq 0x92eb
leaq 0x7ff1(%rip), %rdi      # 0x1174b
callq 0x927a
movl 0x4(%rbx), %r14d
xorl %r13d, %r13d
cmpq $0x8, %r13
je 0x978c
movb $0x2e, %al
testb %r14b, %r14b
jns 0x9778
movb (%r13,%r12), %al
movzbl %al, %edi
callq 0x9265
incq %r13
movzbl %r14b, %r14d
addl %r14d, %r14d
jmp 0x9766
leaq 0x7fbd(%rip), %rdi      # 0x11750
callq 0x927a
leaq 0x7fb5(%rip), %rdi      # 0x11754
callq 0x927a
movzwl 0x14(%rbx), %edi
callq 0x92eb
leaq 0x7fa4(%rip), %rdi      # 0x11758
callq 0x927a
movzwl 0x18(%rbx), %edi
callq 0x92eb
leaq 0x7f94(%rip), %rdi      # 0x1175d
callq 0x927a
movzwl 0x20(%rbx), %edi
callq 0x92eb
leaq 0x7f84(%rip), %rdi      # 0x11762
callq 0x927a
movzwl 0x1c(%rbx), %edi
callq 0x92eb
leaq 0x7f8b(%rip), %rdi      # 0x1177e
callq 0x927a
movq %rbx, %rdi
movl %r15d, %esi
callq 0x956f
cmpl $0x5, 0x1c(%rbx)
jne 0x9848
movzbl 0x8(%rbx), %eax
leaq 0x7f53(%rip), %rdi      # 0x11767
cmpl $0x28, %eax
ja 0x9843
leaq 0x7f52(%rip), %rdi      # 0x11772
callq 0x927a
movzbl 0x8(%rbx), %eax
leaq 0x561c(%rip), %rcx      # 0xee4c
movslq (%rcx,%rax,4), %rdi
addq %rcx, %rdi
callq 0x927a
leaq 0x8e95(%rip), %rdi      # 0x126d8
callq 0x927a
cmpl $-0x1, 0x4c(%rbx)
je 0x9858
movq %rbx, %rdi
xorl %esi, %esi
callq 0x9365
leaq 0x7fe1(%rip), %rdi      # 0x11840
callq 0x927a
leaq 0x7f0a(%rip), %rdi      # 0x11775
callq 0x927a
callq 0x9250
leal -0x16(%rax), %ecx
movzbl %al, %r13d
cmpb $0x68, %cl
ja 0x9889
movl %r13d, %edi
callq 0x925b
leal -0x62(%r13), %eax
cmpl $0x17, %eax
ja 0x98b7
leaq 0x478f(%rip), %rcx      # 0xe028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x7f97(%rip), %rdi      # 0x11840
callq 0x927a
movzwl 0x8(%rbx), %esi
jmp 0x9ac1
leal -0x3f(%r13), %eax
cmpl $0x5, %eax
ja 0x99c9
leaq 0x4745(%rip), %rcx      # 0xe010
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x7f65(%rip), %rdi      # 0x11840
callq 0x927a
leaq 0x7edf(%rip), %rdi      # 0x117c6
callq 0x927a
leaq 0x7ee9(%rip), %rdi      # 0x117dc
callq 0x927a
leaq 0x7f04(%rip), %rdi      # 0x11803
callq 0x927a
leaq 0x7f13(%rip), %rdi      # 0x1181e
callq 0x927a
leaq 0x7f2c(%rip), %rdi      # 0x11843
callq 0x927a
leaq 0x7f45(%rip), %rdi      # 0x11868
callq 0x927a
leaq 0x7f5e(%rip), %rdi      # 0x1188d
callq 0x927a
leaq 0x7f78(%rip), %rdi      # 0x118b3
callq 0x927a
leaq 0x7f92(%rip), %rdi      # 0x118d9
callq 0x927a
leaq 0x7fac(%rip), %rdi      # 0x118ff
callq 0x927a
leaq 0x7fc6(%rip), %rdi      # 0x11925
callq 0x927a
leaq 0x7fdf(%rip), %rdi      # 0x1194a
callq 0x927a
leaq 0x7feb(%rip), %rdi      # 0x11962
callq 0x927a
leaq 0x7ff5(%rip), %rdi      # 0x11978
callq 0x927a
leaq 0x800c(%rip), %rdi      # 0x1199b
callq 0x927a
leaq 0x801a(%rip), %rdi      # 0x119b5
callq 0x927a
leaq 0x802e(%rip), %rdi      # 0x119d5
callq 0x927a
leaq 0x8042(%rip), %rdi      # 0x119f5
callq 0x927a
leaq 0x8050(%rip), %rdi      # 0x11a0f
callq 0x927a
jmp 0x96ee
cmpl $0x4c, %r13d
je 0x9b40
cmpl $0x57, %r13d
je 0x9ba8
cmpl $0x54, %r13d
je 0x9c15
leaq 0x8040(%rip), %rdi      # 0x11a2e
callq 0x927a
jmp 0x96ee
leaq 0x7e41(%rip), %rdi      # 0x11840
callq 0x927a
movzbl 0x19(%rbx), %esi
jmp 0x9abe
leaq 0x7e2c(%rip), %rdi      # 0x11840
callq 0x927a
movzbl 0x15(%rbx), %esi
jmp 0x9abe
leaq 0x7e17(%rip), %rdi      # 0x11840
callq 0x927a
movzwl 0x10(%rbx), %esi
jmp 0x9ac1
leaq 0x7e02(%rip), %rdi      # 0x11840
callq 0x927a
movzwl 0xc(%rbx), %esi
jmp 0x9ac1
leaq 0x7df0(%rip), %rdi      # 0x11840
callq 0x927a
movb $-0x10, %r14b
testb %r14b, %r14b
je 0x96ee
movzwl %r15w, %r13d
movl %r13d, %edi
callq 0x92eb
leaq 0x7d0a(%rip), %rdi      # 0x1177e
callq 0x927a
movq %rbx, %rdi
movl %r13d, %esi
callq 0x956f
movzbl %al, %r15d
addl %r13d, %r15d
leaq 0x7dae(%rip), %rdi      # 0x11840
callq 0x927a
incb %r14b
jmp 0x9a58
leaq 0x7d9d(%rip), %rdi      # 0x11840
callq 0x927a
movzbl 0x1d(%rbx), %esi
jmp 0x9abe
leaq 0x7d8b(%rip), %rdi      # 0x11840
callq 0x927a
movzbl 0x21(%rbx), %esi
shll $0x8, %esi
movq %rbx, %rdi
callq 0x942f
jmp 0x96ee
leaq 0x7cad(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7ca9(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movzwl -0x2c(%rbp), %esi
jmp 0x9ac1
leaq 0x7c89(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7c85(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movl -0x2c(%rbp), %eax
movl %eax, 0x44(%rbx)
leaq 0x7c72(%rip), %rdi      # 0x1178f
callq 0x927a
movzwl 0x44(%rbx), %edi
jmp 0x9bdc
orl $-0x1, 0x44(%rbx)
leaq 0x7c6c(%rip), %rdi      # 0x117a2
callq 0x927a
jmp 0x96ee
leaq 0x7c3b(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7c37(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movb $-0x10, %r14b
movl -0x2c(%rbp), %r13d
testb %r14b, %r14b
je 0x96ee
movzwl %r13w, %r15d
movl %r15d, %edi
callq 0x92eb
leaq 0x7bfd(%rip), %rdi      # 0x1177e
callq 0x927a
movq %rbx, %rdi
movl %r15d, %esi
callq 0x956f
movzbl %al, %eax
addl %eax, %r13d
leaq 0x7ca2(%rip), %rdi      # 0x11840
callq 0x927a
incb %r14b
jmp 0x9b65
leaq 0x7bd3(%rip), %rdi      # 0x11782
callq 0x927a
leaq 0x7bcf(%rip), %rdi      # 0x1178a
leaq -0x2c(%rbp), %rsi
xorl %eax, %eax
callq 0x90b0
movl -0x2c(%rbp), %eax
movl %eax, 0x4c(%rbx)
leaq 0x7be5(%rip), %rdi      # 0x117b8
callq 0x927a
movzwl 0x4c(%rbx), %edi
callq 0x92eb
leaq 0x7c58(%rip), %rdi      # 0x11840
callq 0x927a
jmp 0x96ee
leaq 0x7c47(%rip), %rdi      # 0x11840
callq 0x927a
movl $0x1, 0x3c(%rbx)
jmp 0x9c21
leaq 0x7c32(%rip), %rdi      # 0x11840
callq 0x927a
jmp 0x9c1d
addl $0x3, %r15d
movl %r15d, 0x48(%rbx)
andl $0x0, 0x40(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq