// File: /workspace/github_c_makefile_star2_O2/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 0xaa61(%rip), %rax      # 0x12448
cmpl $0x0, (%rax)
je 0x7d6e
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), %r12
movq %r12, %rdx
callq 0x7d6f
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
leaq 0x18(%rsp), %r15
movq %rax, (%r15)
movw $0x20, 0x8(%r15)
movl (%r12), %r13d
leaq 0x51ab(%rip), %r12      # 0xcbe5
xorl %ebp, %ebp
cmpq %rbp, %r13
je 0x7a60
movzbl (%rbx,%rbp), %edx
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x52e0
movb $0x20, 0x2(%r15)
addq $0x3, %r15
incq %rbp
jmp 0x7a3c
leaq 0xa9e1(%rip), %r12      # 0x12448
cmpl $0x1, (%r12)
jle 0x7aab
movzbl 0xab0b(%rip), %edi      # 0x12580
callq 0x8037
movzwl 0xa(%r14), %edx
testb %al, %al
je 0x7ae6
movzwl 0xab02(%rip), %eax      # 0x1258c
addl 0xab00(%rip), %eax      # 0x12590
xorl %esi, %esi
cmpl %edx, %eax
setne %sil
addl $0x1f, %esi
leaq 0x4d66(%rip), %rdi      # 0xc808
xorl %eax, %eax
callq 0x5050
jmp 0x7af6
movzwl 0xa(%r14), %esi
movzwl 0x8(%r14), %ecx
movzwl (%r14), %r8d
movzwl 0x2(%r14), %r9d
movzwl 0x4(%r14), %r10d
movzwl 0x6(%r14), %r11d
leaq 0x4d55(%rip), %rdi      # 0xc824
leaq 0x18(%rsp), %rdx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x5050
popq %rcx
popq %rdx
jmp 0x7b87
leaq 0x4d2b(%rip), %rdi      # 0xc818
movl %edx, %esi
xorl %eax, %eax
callq 0x5050
leaq 0x4d22(%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 0xaa67(%rip), %r8w      # 0x1258a
pushq $0x24
popq %rcx
cmovel %esi, %ecx
leaq 0x4e4a(%rip), %rdi      # 0xc97a
leaq 0x4e6a(%rip), %rdx      # 0xc9a1
xorl %eax, %eax
callq 0x5050
movl 0x4(%rsp), %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x809b
movl 0x4(%rsp), %edi
pushq $0x2
popq %rdx
movq %r14, %rsi
callq 0x809b
movl 0x4(%rsp), %edi
pushq $0x4
popq %rdx
movq %r14, %rsi
callq 0x809b
movl 0x4(%rsp), %edi
pushq $0x6
popq %rdx
movq %r14, %rsi
callq 0x809b
leaq 0x4cd6(%rip), %rdi      # 0xc856
xorl %eax, %eax
callq 0x5050
movzbl (%rbx), %eax
cmpq $0xcb, %rax
jne 0x7bd9
movzbl 0x1(%rbx), %eax
movl %eax, %r8d
shrl $0x3, %r8d
andl $0x7, %r8d
movl %eax, %edx
andl $0x7, %edx
cmpl $0x3f, %eax
ja 0x7bf9
leaq 0x987e(%rip), %rax      # 0x11430
movq (%rax,%r8,8), %rcx
leaq 0x98d3(%rip), %rax      # 0x11490
movq (%rax,%rdx,8), %r8
leaq 0x4c91(%rip), %rdx      # 0xc859
leaq 0x9(%rsp), %rdi
pushq $0xf
popq %rsi
xorl %eax, %eax
callq 0x5090
jmp 0x7c29
leaq 0x98f0(%rip), %rcx      # 0x114d0
movq (%rcx,%rax,8), %rdx
movzwl 0x1(%rbx), %ecx
leaq 0x9(%rsp), %rdi
pushq $0xf
popq %rsi
xorl %eax, %eax
callq 0x5090
jmp 0x7c29
shrl $0x6, %eax
leaq 0x982d(%rip), %rcx      # 0x11430
movq 0x38(%rcx,%rax,8), %rcx
leaq 0x9881(%rip), %rax      # 0x11490
movq (%rax,%rdx,8), %r9
leaq 0x4c45(%rip), %rdx      # 0xc85f
leaq 0x9(%rsp), %rdi
pushq $0xf
popq %rsi
xorl %eax, %eax
callq 0x5090
leaq 0x9(%rsp), %rdi
pushq $0x5b
popq %rsi
callq 0x53f0
pushq $-0x1
popq %r15
testq %rax, %rax
je 0x7c9a
movzbl 0x1(%rax), %eax
cmpl $0x24, %eax
je 0x7c71
cmpl $0x25, %eax
je 0x7c87
cmpl $0x62, %eax
je 0x7c6a
cmpl $0x64, %eax
je 0x7c63
cmpl $0x68, %eax
jne 0x7c9a
movzwl 0x6(%r14), %r15d
jmp 0x7c9a
movzwl 0x4(%r14), %r15d
jmp 0x7c9a
movzwl 0x2(%r14), %r15d
jmp 0x7c9a
movzbl (%rbx), %eax
andl $0xf, %eax
cmpl $0x2, %eax
je 0x7c8e
testl %eax, %eax
jne 0x7c87
movzbl 0x1(%rbx), %r15d
jmp 0x7c93
movzwl 0x1(%rbx), %r15d
jmp 0x7c9a
movzbl 0x2(%r14), %r15d
orl $0xff00, %r15d          # imm = 0xFF00
cmpl $0x2, (%r12)
jne 0x7ce9
movzbl (%rbx), %edi
callq 0x8037
leaq 0x4bba(%rip), %rcx      # 0xc86a
leaq 0x4a22(%rip), %rsi      # 0xc6d9
testb %al, %al
cmovneq %rcx, %rsi
cmpl $-0x1, %r15d
je 0x7d15
leaq 0xa776(%rip), %rax      # 0x12440
movq (%rax), %rax
movl %r15d, %ecx
movzbl (%rax,%rcx), %ecx
leaq 0x4b97(%rip), %rdi      # 0xc872
leaq 0x9(%rsp), %rdx
xorl %eax, %eax
callq 0x5050
jmp 0x7d3d
cmpl $-0x1, %r15d
je 0x7d2a
leaq 0xa74a(%rip), %rax      # 0x12440
movq (%rax), %rax
movl %r15d, %ecx
movzbl (%rax,%rcx), %edx
leaq 0x4b90(%rip), %rdi      # 0xc897
leaq 0x9(%rsp), %rsi
xorl %eax, %eax
callq 0x5050
jmp 0x7d3d
leaq 0x4b6c(%rip), %rdi      # 0xc888
leaq 0x9(%rsp), %rdx
xorl %eax, %eax
callq 0x5050
jmp 0x7d3d
leaq 0x4b76(%rip), %rdi      # 0xc8a7
leaq 0x9(%rsp), %rsi
xorl %eax, %eax
callq 0x5050
movl 0x8(%r14), %eax
movl %eax, 0xa843(%rip)      # 0x1258a
movq (%r14), %rax
movq %rax, 0xa831(%rip)      # 0x12582
movb (%rbx), %al
movb %al, 0xa827(%rip)       # 0x12580
movl %r13d, 0xa830(%rip)     # 0x12590
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq