// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0xc78e(%rip), %rax      # 0x16918
cmpl $0x0, (%rax)
jne 0xa194
jmp 0xa610
movq -0x8(%rbp), %rdi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
callq 0xa620
leaq -0x22(%rbp), %rdi
movl $0x20, %esi
movl $0x9, %edx
callq 0x5080
movb $0x0, -0x19(%rbp)
leaq -0x22(%rbp), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0xa20f
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rax
movl -0x34(%rbp), %ecx
movzbl (%rax,%rcx), %edx
leaq 0x6fee(%rip), %rsi      # 0x111d7
movb $0x0, %al
callq 0x52e0
movq -0x30(%rbp), %rax
movb $0x20, 0x2(%rax)
movq -0x30(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x30(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xa1cb
leaq 0xc702(%rip), %rax      # 0x16918
cmpl $0x2, (%rax)
jl 0xa30b
movzbl 0xc82a(%rip), %edi      # 0x16a50
callq 0xa970
testb $0x1, %al
jne 0xa231
jmp 0xa27f
movq -0x10(%rbp), %rax
movzwl 0xa(%rax), %eax
movzwl 0xc81c(%rip), %ecx      # 0x16a5c
addl 0xc81a(%rip), %ecx      # 0x16a60
cmpl %ecx, %eax
je 0xa255
movl 0xbacc(%rip), %eax      # 0x15d1c
movl %eax, -0x7c(%rbp)
jmp 0xa25e
movl 0xbac5(%rip), %eax      # 0x15d20
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %esi
movq -0x10(%rbp), %rax
movzwl 0xa(%rax), %edx
leaq 0x6b7a(%rip), %rdi      # 0x10df0
movb $0x0, %al
callq 0x5050
jmp 0xa295
movq -0x10(%rbp), %rax
movzwl 0xa(%rax), %esi
leaq 0x6b72(%rip), %rdi      # 0x10e00
movb $0x0, %al
callq 0x5050
leaq -0x22(%rbp), %rsi
leaq 0x6b67(%rip), %rdi      # 0x10e07
movb $0x0, %al
callq 0x5050
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rsi
movl $0x8, %edx
callq 0xaa50
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0xab40
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rsi
movl $0x2, %edx
callq 0xab40
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rsi
movl $0x4, %edx
callq 0xab40
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rsi
movl $0x6, %edx
callq 0xab40
leaq 0x6b3c(%rip), %rdi      # 0x10e3e
movb $0x0, %al
callq 0x5050
jmp 0xa357
movq -0x10(%rbp), %rax
movzwl 0xa(%rax), %esi
leaq -0x22(%rbp), %rdx
movq -0x10(%rbp), %rax
movzwl 0x8(%rax), %ecx
movq -0x10(%rbp), %rax
movzwl (%rax), %r8d
movq -0x10(%rbp), %rax
movzwl 0x2(%rax), %r9d
movq -0x10(%rbp), %rax
movzwl 0x4(%rax), %r10d
movq -0x10(%rbp), %rax
movzwl 0x6(%rax), %eax
leaq 0x6ac4(%rip), %rdi      # 0x10e0c
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x5050
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
cmpl $0xcb, %eax
jne 0xa419
movq -0x8(%rbp), %rax
movzbl 0x1(%rax), %eax
sarl $0x6, %eax
cltq
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x1(%rax), %eax
sarl $0x3, %eax
andl $0x7, %eax
cltq
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x1(%rax), %eax
andl $0x7, %eax
cltq
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xa3dd
leaq -0x47(%rbp), %rdi
movq -0x58(%rbp), %rcx
leaq 0xb97b(%rip), %rax      # 0x15d30
movq (%rax,%rcx,8), %rcx
movq -0x60(%rbp), %rdx
leaq 0xb9cc(%rip), %rax      # 0x15d90
movq (%rax,%rdx,8), %r8
movl $0xf, %esi
leaq 0x6a6d(%rip), %rdx      # 0x10e41
movb $0x0, %al
callq 0x5090
jmp 0xa417
leaq -0x47(%rbp), %rdi
movq -0x50(%rbp), %rcx
leaq 0xb944(%rip), %rax      # 0x15d30
movq 0x38(%rax,%rcx,8), %rcx
movq -0x58(%rbp), %r8
movq -0x60(%rbp), %rdx
leaq 0xb990(%rip), %rax      # 0x15d90
movq (%rax,%rdx,8), %r9
movl $0xf, %esi
leaq 0x6a37(%rip), %rdx      # 0x10e47
movb $0x0, %al
callq 0x5090
jmp 0xa445
leaq -0x47(%rbp), %rdi
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0xb9a3(%rip), %rax      # 0x15dd0
movq (%rax,%rcx,8), %rdx
movq -0x8(%rbp), %rax
movzwl 0x1(%rax), %ecx
movl $0xf, %esi
movb $0x0, %al
callq 0x5090
leaq -0x47(%rbp), %rdi
movl $0x5b, %esi
callq 0x53e0
movq %rax, -0x68(%rbp)
movl $0xffffffff, -0x6c(%rbp) # imm = 0xFFFFFFFF
cmpq $0x0, -0x68(%rbp)
je 0xa529
movq -0x68(%rbp), %rax
movsbl 0x1(%rax), %eax
movl %eax, -0x80(%rbp)
subl $0x24, %eax
je 0xa4db
jmp 0xa47b
movl -0x80(%rbp), %eax
subl $0x25, %eax
je 0xa4ce
jmp 0xa485
movl -0x80(%rbp), %eax
subl $0x62, %eax
je 0xa4b4
jmp 0xa48f
movl -0x80(%rbp), %eax
subl $0x64, %eax
je 0xa4c1
jmp 0xa499
movl -0x80(%rbp), %eax
subl $0x68, %eax
jne 0xa527
jmp 0xa4a7
movq -0x10(%rbp), %rax
movzwl 0x6(%rax), %eax
movl %eax, -0x6c(%rbp)
jmp 0xa527
movq -0x10(%rbp), %rax
movzwl 0x2(%rax), %eax
movl %eax, -0x6c(%rbp)
jmp 0xa527
movq -0x10(%rbp), %rax
movzwl 0x4(%rax), %eax
movl %eax, -0x6c(%rbp)
jmp 0xa527
movq -0x8(%rbp), %rax
movzwl 0x1(%rax), %eax
movl %eax, -0x6c(%rbp)
jmp 0xa527
movq -0x8(%rbp), %rax
movzbl (%rax), %eax
andl $0xf, %eax
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x70(%rbp)
jne 0xa500
movq -0x8(%rbp), %rax
movzbl 0x1(%rax), %eax
addl $0xff00, %eax           # imm = 0xFF00
movl %eax, -0x6c(%rbp)
jmp 0xa525
cmpl $0x2, -0x70(%rbp)
jne 0xa518
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
addl $0xff00, %eax           # imm = 0xFF00
movl %eax, -0x6c(%rbp)
jmp 0xa523
movq -0x8(%rbp), %rax
movzwl 0x1(%rax), %eax
movl %eax, -0x6c(%rbp)
jmp 0xa525
jmp 0xa527
jmp 0xa529
leaq 0xc3e8(%rip), %rax      # 0x16918
cmpl $0x2, (%rax)
jne 0xa5a4
movq -0x8(%rbp), %rax
movzbl (%rax), %edi
callq 0xa970
movb %al, %dl
leaq 0x6450(%rip), %rax      # 0x1099a
leaq 0x6901(%rip), %rcx      # 0x10e52
testb $0x1, %dl
cmovneq %rcx, %rax
movq %rax, -0x78(%rbp)
cmpl $-0x1, -0x6c(%rbp)
je 0xa58c
movq -0x78(%rbp), %rsi
leaq -0x47(%rbp), %rdx
leaq 0xc39f(%rip), %rax      # 0x16910
movq (%rax), %rax
movslq -0x6c(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
leaq 0x68d7(%rip), %rdi      # 0x10e5a
movb $0x0, %al
callq 0x5050
jmp 0xa5a2
movq -0x78(%rbp), %rsi
leaq -0x47(%rbp), %rdx
leaq 0x68d5(%rip), %rdi      # 0x10e70
movb $0x0, %al
callq 0x5050
jmp 0xa5e4
cmpl $-0x1, -0x6c(%rbp)
je 0xa5d0
leaq -0x47(%rbp), %rsi
leaq 0xc35b(%rip), %rax      # 0x16910
movq (%rax), %rax
movslq -0x6c(%rbp), %rcx
movzbl (%rax,%rcx), %edx
leaq 0x68b8(%rip), %rdi      # 0x10e7f
movb $0x0, %al
callq 0x5050
jmp 0xa5e2
leaq -0x47(%rbp), %rsi
leaq 0x68b4(%rip), %rdi      # 0x10e8f
movb $0x0, %al
callq 0x5050
jmp 0xa5e4
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq %rcx, 0xc460(%rip)      # 0x16a52
movl 0x8(%rax), %eax
movl %eax, 0xc45f(%rip)      # 0x16a5a
movq -0x8(%rbp), %rax
movb (%rax), %al
movb %al, 0xc449(%rip)       # 0x16a50
movl -0x18(%rbp), %eax
movl %eax, 0xc450(%rip)      # 0x16a60
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)