// File: /workspace/github_c_makefile_star2_O3/baines[P]MiniGBS/debug.c
// Function: debug_dump
void debug_dump(uint8_t* op, struct regs* regs){
	if(!cfg.debug_mode)
		return;

	uint32_t mask, len;
	debug_get_regs(op, &mask, &len);

	// opcode & operand bytes
	char op_str[10];
	{
		memset(op_str, ' ', sizeof(op_str)-1);
		op_str[9] = 0;

		char* p = op_str;
		for(uint32_t i = 0; i < len; ++i){
			sprintf(p, "%02hhx", op[i]);
			p[2] = ' ';
			p += 3;
		}
	}

	if(cfg.debug_mode >= 2){
		if(debug_is_jump(prev_op)){
			int c = (regs->pc != prev_regs.pc + prev_len)
				? colours[DBG_COLOUR_JUMP_TAKEN]
				: colours[DBG_COLOUR_JUMP_UNTAKEN]
				;

			printf("\e[%dm%04x\e[0m: ", c, regs->pc);
		} else {
			printf("%04x: ", regs->pc);
		}

		printf("%s| ", op_str);

		debug_print_colour_reg_16 (mask, regs, offsetof(struct regs, sp));
		debug_print_colour_reg    (mask, regs, offsetof(struct regs, af));
		debug_print_colour_reg    (mask, regs, offsetof(struct regs, bc));
		debug_print_colour_reg    (mask, regs, offsetof(struct regs, de));
		debug_print_colour_reg    (mask, regs, offsetof(struct regs, hl));

		printf("| ");
	} else {
		printf("%04x: %s| SP:%04x AF:%04x BC:%04x DE:%04x HL:%04x | ",
			   regs->pc, op_str, regs->sp, regs->af, regs->bc, regs->de, regs->hl);
	}

	char mnemomic[15];

	if(*op == 0xCB){
		size_t x = (op[1] >> 6);
		size_t y = (op[1] >> 3) & 7;
		size_t z = op[1] & 7;

		if(x == 0){
			snprintf(mnemomic, sizeof(mnemomic), "%s %s", cb_ops[y], cb_regnames[z]);
		} else {
			snprintf(mnemomic, sizeof(mnemomic), "%s %zu, %s", cb_ops[x + 7], y, cb_regnames[z]);
		}
	} else {
		snprintf(mnemomic, sizeof(mnemomic), opcodes[*op], *(uint16_t*)(op+1));
	}

	char* p = strchr(mnemomic, '[');
	int32_t addr = -1;

	if(p){
		switch(p[1]){
			case 'h': addr = regs->hl; break;
			case 'b': addr = regs->bc; break;
			case 'd': addr = regs->de; break;
			case '%': addr = *(uint16_t*)(op+1); break;
			case '$': {
				int lo = *op & 0xf;
				if(lo == 0){
					addr = 0xff00 + op[1];
				} else if(lo == 2){
					addr = 0xff00 + regs->c;
				} else {
					addr = *(uint16_t*)(op+1);
				}
				break;
			}
		}
	}

	if(cfg.debug_mode == 2){
		const char* colour = debug_is_jump(*op) ? "\e[1;34m" : "";

		if(addr != -1){
			printf("%s%-14s\e[0m | [%02x]\n", colour, mnemomic, mem[addr]);
		} else {
			printf("%s%-14s\e[0m |\n", colour, mnemomic);
		}
	} else {
		if(addr != -1){
			printf("%-14s | [%02x]\n", mnemomic, mem[addr]);
		} else {
			printf("%-14s |\n", mnemomic);
		}
	}

	prev_regs = *regs;
	prev_op = *op;
	prev_len = len;
}

// Assembly:
leaq 0x9901(%rip), %rax      # 0x11448
cmpl $0x0, (%rax)
je 0x7eed
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x4(%rsp), %rsi
leaq 0x24(%rsp), %r15
movq %r15, %rdx
callq 0x7eee
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
movq %rax, 0x18(%rsp)
movw $0x20, 0x20(%rsp)
movl (%r15), %r13d
testq %r13, %r13
je 0x7bc4
leaq 0x18(%rsp), %r15
leaq 0x5045(%rip), %r12      # 0xcbe5
xorl %ebp, %ebp
movzbl (%rbx,%rbp), %edx
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x52e0
movb $0x20, 0x2(%r15)
addq $0x3, %r15
incq %rbp
cmpq %rbp, %r13
jne 0x7ba2
leaq 0x987d(%rip), %r12      # 0x11448
cmpl $0x1, (%r12)
jle 0x7c0f
movzbl 0x99a7(%rip), %edi      # 0x11580
callq 0x81cc
movzwl 0xa(%r14), %edx
testb %al, %al
je 0x7c4c
movzwl 0x999e(%rip), %eax      # 0x1158c
addl 0x999c(%rip), %eax      # 0x11590
xorl %esi, %esi
cmpl %edx, %eax
setne %sil
addl $0x1f, %esi
leaq 0x4c02(%rip), %rdi      # 0xc808
xorl %eax, %eax
callq 0x5050
jmp 0x7c5c
movzwl 0xa(%r14), %esi
movzwl 0x8(%r14), %ecx
movzwl (%r14), %r8d
movzwl 0x2(%r14), %r9d
movzwl 0x4(%r14), %r10d
movzwl 0x6(%r14), %r11d
leaq 0x4bf1(%rip), %rdi      # 0xc824
leaq 0x18(%rsp), %rdx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x5050
addq $0x10, %rsp
jmp 0x7cf5
leaq 0x4bc5(%rip), %rdi      # 0xc818
movl %edx, %esi
xorl %eax, %eax
callq 0x5050
leaq 0x4bbc(%rip), %rdi      # 0xc81f
leaq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x5050
movl 0x4(%rsp), %esi
movzwl 0x8(%r14), %r8d
shll $0x17, %esi
sarl $0x1f, %esi
andl $0x21, %esi
cmpw %r8w, 0x9901(%rip)      # 0x1158a
movl $0x24, %ecx
cmovel %esi, %ecx
leaq 0x4ce2(%rip), %rdi      # 0xc97a
leaq 0x4d02(%rip), %rdx      # 0xc9a1
xorl %eax, %eax
callq 0x5050
movl 0x4(%rsp), %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x8237
movl 0x4(%rsp), %edi
movl $0x2, %edx
movq %r14, %rsi
callq 0x8237
movl 0x4(%rsp), %edi
movl $0x4, %edx
movq %r14, %rsi
callq 0x8237
movl 0x4(%rsp), %edi
movl $0x6, %edx
movq %r14, %rsi
callq 0x8237
leaq 0x4b68(%rip), %rdi      # 0xc856
xorl %eax, %eax
callq 0x5050
movzbl (%rbx), %eax
cmpq $0xcb, %rax
jne 0x7d49
movzbl 0x1(%rbx), %eax
movl %eax, %r8d
shrl $0x3, %r8d
andl $0x7, %r8d
movl %eax, %edx
andl $0x7, %edx
cmpl $0x3f, %eax
ja 0x7d6b
leaq 0x8710(%rip), %rax      # 0x10430
movq (%rax,%r8,8), %rcx
leaq 0x8765(%rip), %rax      # 0x10490
movq (%rax,%rdx,8), %r8
leaq 0x4b23(%rip), %rdx      # 0xc859
leaq 0x9(%rsp), %rdi
movl $0xf, %esi
xorl %eax, %eax
callq 0x5090
jmp 0x7d9d
leaq 0x8780(%rip), %rcx      # 0x104d0
movq (%rcx,%rax,8), %rdx
movzwl 0x1(%rbx), %ecx
leaq 0x9(%rsp), %rdi
movl $0xf, %esi
xorl %eax, %eax
callq 0x5090
jmp 0x7d9d
shrl $0x6, %eax
leaq 0x86bb(%rip), %rcx      # 0x10430
movq 0x38(%rcx,%rax,8), %rcx
leaq 0x870f(%rip), %rax      # 0x10490
movq (%rax,%rdx,8), %r9
leaq 0x4ad3(%rip), %rdx      # 0xc85f
leaq 0x9(%rsp), %rdi
movl $0xf, %esi
xorl %eax, %eax
callq 0x5090
leaq 0x9(%rsp), %rdi
movl $0x5b, %esi
callq 0x53e0
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testq %rax, %rax
je 0x7e19
movzbl 0x1(%rax), %eax
cmpl $0x61, %eax
jle 0x7dd6
cmpl $0x62, %eax
je 0x7de2
cmpl $0x64, %eax
je 0x7de9
cmpl $0x68, %eax
jne 0x7e19
movzwl 0x6(%r14), %r15d
jmp 0x7e19
cmpl $0x24, %eax
je 0x7df0
cmpl $0x25, %eax
je 0x7e06
jmp 0x7e19
movzwl 0x2(%r14), %r15d
jmp 0x7e19
movzwl 0x4(%r14), %r15d
jmp 0x7e19
movzbl (%rbx), %eax
andl $0xf, %eax
cmpl $0x2, %eax
je 0x7e0d
testl %eax, %eax
jne 0x7e06
movzbl 0x1(%rbx), %r15d
jmp 0x7e12
movzwl 0x1(%rbx), %r15d
jmp 0x7e19
movzbl 0x2(%r14), %r15d
orl $0xff00, %r15d          # imm = 0xFF00
cmpl $0x2, (%r12)
jne 0x7e68
movzbl (%rbx), %edi
callq 0x81cc
leaq 0x4a3b(%rip), %rcx      # 0xc86a
leaq 0x48a3(%rip), %rsi      # 0xc6d9
testb %al, %al
cmovneq %rcx, %rsi
cmpl $-0x1, %r15d
je 0x7e94
leaq 0x95f7(%rip), %rax      # 0x11440
movq (%rax), %rax
movl %r15d, %ecx
movzbl (%rax,%rcx), %ecx
leaq 0x4a18(%rip), %rdi      # 0xc872
leaq 0x9(%rsp), %rdx
xorl %eax, %eax
callq 0x5050
jmp 0x7ebc
cmpl $-0x1, %r15d
je 0x7ea9
leaq 0x95cb(%rip), %rax      # 0x11440
movq (%rax), %rax
movl %r15d, %ecx
movzbl (%rax,%rcx), %edx
leaq 0x4a11(%rip), %rdi      # 0xc897
leaq 0x9(%rsp), %rsi
xorl %eax, %eax
callq 0x5050
jmp 0x7ebc
leaq 0x49ed(%rip), %rdi      # 0xc888
leaq 0x9(%rsp), %rdx
xorl %eax, %eax
callq 0x5050
jmp 0x7ebc
leaq 0x49f7(%rip), %rdi      # 0xc8a7
leaq 0x9(%rsp), %rsi
xorl %eax, %eax
callq 0x5050
movl 0x8(%r14), %eax
movl %eax, 0x96c4(%rip)      # 0x1158a
movq (%r14), %rax
movq %rax, 0x96b2(%rip)      # 0x11582
movb (%rbx), %al
movb %al, 0x96a8(%rip)       # 0x11580
movl %r13d, 0x96b1(%rip)     # 0x11590
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq