// File: /workspace/github_c_makefile_star2_O1/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 0x9859(%rip), %rax      # 0x11448
cmpl $0x0, (%rax)
je 0x7f95
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 0x7f96
movabsq $0x2020202020202020, %rax # imm = 0x2020202020202020
movq %rax, 0x18(%rsp)
movw $0x20, 0x20(%rsp)
movl (%r15), %r13d
testq %r13, %r13
je 0x7c6c
leaq 0x18(%rsp), %r15
leaq 0x4f6f(%rip), %r12      # 0xcbb7
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 0x7c4a
leaq 0x97d5(%rip), %r12      # 0x11448
cmpl $0x1, (%r12)
jle 0x7cb7
movzbl 0x98ff(%rip), %edi      # 0x11580
callq 0x8274
movzwl 0xa(%r14), %edx
testb %al, %al
je 0x7cf4
movzwl 0x98f6(%rip), %eax      # 0x1158c
addl 0x98f4(%rip), %eax      # 0x11590
xorl %esi, %esi
cmpl %edx, %eax
setne %sil
addl $0x1f, %esi
leaq 0x4b22(%rip), %rdi      # 0xc7d0
xorl %eax, %eax
callq 0x5050
jmp 0x7d04
movzwl 0xa(%r14), %esi
movzwl 0x8(%r14), %ecx
movzwl (%r14), %r8d
movzwl 0x2(%r14), %r9d
movzwl 0x4(%r14), %r10d
movzwl 0x6(%r14), %r11d
leaq 0x4b11(%rip), %rdi      # 0xc7ec
leaq 0x18(%rsp), %rdx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x5050
addq $0x10, %rsp
jmp 0x7d9d
leaq 0x4ae5(%rip), %rdi      # 0xc7e0
movl %edx, %esi
xorl %eax, %eax
callq 0x5050
leaq 0x4adc(%rip), %rdi      # 0xc7e7
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 0x9859(%rip), %r8w      # 0x1158a
movl $0x24, %ecx
cmovel %esi, %ecx
leaq 0x4c0c(%rip), %rdi      # 0xc94c
leaq 0x4c2c(%rip), %rdx      # 0xc973
xorl %eax, %eax
callq 0x5050
movl 0x4(%rsp), %edi
movq %r14, %rsi
xorl %edx, %edx
callq 0x830a
movl 0x4(%rsp), %edi
movl $0x2, %edx
movq %r14, %rsi
callq 0x830a
movl 0x4(%rsp), %edi
movl $0x4, %edx
movq %r14, %rsi
callq 0x830a
movl 0x4(%rsp), %edi
movl $0x6, %edx
movq %r14, %rsi
callq 0x830a
leaq 0x4a88(%rip), %rdi      # 0xc81e
xorl %eax, %eax
callq 0x5050
movzbl (%rbx), %eax
cmpq $0xcb, %rax
jne 0x7df1
movzbl 0x1(%rbx), %eax
movl %eax, %r8d
shrl $0x3, %r8d
andl $0x7, %r8d
movl %eax, %edx
andl $0x7, %edx
cmpl $0x3f, %eax
ja 0x7e13
leaq 0x8668(%rip), %rax      # 0x10430
movq (%rax,%r8,8), %rcx
leaq 0x86bd(%rip), %rax      # 0x10490
movq (%rax,%rdx,8), %r8
leaq 0x4a43(%rip), %rdx      # 0xc821
leaq 0x9(%rsp), %rdi
movl $0xf, %esi
xorl %eax, %eax
callq 0x5090
jmp 0x7e45
leaq 0x86d8(%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 0x7e45
shrl $0x6, %eax
leaq 0x8613(%rip), %rcx      # 0x10430
movq 0x38(%rcx,%rax,8), %rcx
leaq 0x8667(%rip), %rax      # 0x10490
movq (%rax,%rdx,8), %r9
leaq 0x49f3(%rip), %rdx      # 0xc827
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 0x7ec1
movzbl 0x1(%rax), %eax
cmpl $0x61, %eax
jle 0x7e7e
cmpl $0x62, %eax
je 0x7e8a
cmpl $0x64, %eax
je 0x7e91
cmpl $0x68, %eax
jne 0x7ec1
movzwl 0x6(%r14), %r15d
jmp 0x7ec1
cmpl $0x24, %eax
je 0x7e98
cmpl $0x25, %eax
je 0x7eae
jmp 0x7ec1
movzwl 0x2(%r14), %r15d
jmp 0x7ec1
movzwl 0x4(%r14), %r15d
jmp 0x7ec1
movzbl (%rbx), %eax
andl $0xf, %eax
cmpl $0x2, %eax
je 0x7eb5
testl %eax, %eax
jne 0x7eae
movzbl 0x1(%rbx), %r15d
jmp 0x7eba
movzwl 0x1(%rbx), %r15d
jmp 0x7ec1
movzbl 0x2(%r14), %r15d
orl $0xff00, %r15d          # imm = 0xFF00
cmpl $0x2, (%r12)
jne 0x7f10
movzbl (%rbx), %edi
callq 0x8274
leaq 0x495b(%rip), %rcx      # 0xc832
leaq 0x47ab(%rip), %rsi      # 0xc689
testb %al, %al
cmovneq %rcx, %rsi
cmpl $-0x1, %r15d
je 0x7f3c
leaq 0x954f(%rip), %rax      # 0x11440
movq (%rax), %rax
movl %r15d, %ecx
movzbl (%rax,%rcx), %ecx
leaq 0x4938(%rip), %rdi      # 0xc83a
leaq 0x9(%rsp), %rdx
xorl %eax, %eax
callq 0x5050
jmp 0x7f64
cmpl $-0x1, %r15d
je 0x7f51
leaq 0x9523(%rip), %rax      # 0x11440
movq (%rax), %rax
movl %r15d, %ecx
movzbl (%rax,%rcx), %edx
leaq 0x4931(%rip), %rdi      # 0xc85f
leaq 0x9(%rsp), %rsi
xorl %eax, %eax
callq 0x5050
jmp 0x7f64
leaq 0x490d(%rip), %rdi      # 0xc850
leaq 0x9(%rsp), %rdx
xorl %eax, %eax
callq 0x5050
jmp 0x7f64
leaq 0x4917(%rip), %rdi      # 0xc86f
leaq 0x9(%rsp), %rsi
xorl %eax, %eax
callq 0x5050
movl 0x8(%r14), %eax
movl %eax, 0x961c(%rip)      # 0x1158a
movq (%r14), %rax
movq %rax, 0x960a(%rip)      # 0x11582
movb (%rbx), %al
movb %al, 0x9600(%rip)       # 0x11580
movl %r13d, 0x9609(%rip)     # 0x11590
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq