Skip to content

Commit

Permalink
More tweaks to new -dd debug mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
baines committed Aug 24, 2018
1 parent 8447e9d commit bf0c72f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 67 deletions.
150 changes: 86 additions & 64 deletions debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,71 @@

static const char* opcodes[] = {
[0x00] = "nop",
[0x01] = "ld bc, %#2hhx",
[0x01] = "ld bc, $%2hhx",
[0x02] = "ld [bc], a",
[0x03] = "inc bc",
[0x04] = "inc b",
[0x05] = "dec b",
[0x06] = "ld b, %#2hhx",
[0x06] = "ld b, $%2hhx",
[0x07] = "rlca",
[0x08] = "ld [%#4hx], sp",
[0x08] = "ld [$%4hx], sp",
[0x09] = "add hl, bc",
[0x0a] = "ld a, [bc]",
[0x0b] = "dec bc",
[0x0c] = "inc c",
[0x0d] = "dec c",
[0x0e] = "ld c, %#2hhx",
[0x0e] = "ld c, $%2hhx",
[0x0f] = "rrca",

[0x10] = "stop 0",
[0x11] = "ld de, %#4hx",
[0x11] = "ld de, $%4hx",
[0x12] = "ld [de], a",
[0x13] = "inc de",
[0x14] = "inc d",
[0x15] = "dec d",
[0x16] = "ld d, %#2hhx",
[0x16] = "ld d, $%2hhx",
[0x17] = "rla",
[0x18] = "jr %+hhd",
[0x19] = "add hl, de",
[0x1a] = "ld a, [de]",
[0x1b] = "dec de",
[0x1c] = "inc e",
[0x1d] = "dec e",
[0x1e] = "ld e, %#2hhx",
[0x1e] = "ld e, $%2hhx",
[0x1f] = "rra",

[0x20] = "jr nz, %+hhd",
[0x21] = "ld hl, %#4hx",
[0x21] = "ld hl, $%4hx",
[0x22] = "ld [hl+], a",
[0x23] = "inc hl",
[0x24] = "inc h",
[0x25] = "dec h",
[0x26] = "ld h, %#2hhx",
[0x26] = "ld h, $%02hhx",
[0x27] = "daa",
[0x28] = "jr z, %+hhd",
[0x29] = "add hl, hl",
[0x2a] = "ld a, [hl+]",
[0x2b] = "dec hl",
[0x2c] = "inc l",
[0x2d] = "dec l",
[0x2e] = "ld l, %#2hhx",
[0x2e] = "ld l, $%02hhx",
[0x2f] = "cpl",

[0x30] = "jr nc, %+hhd",
[0x31] = "ld sp, %#4hx",
[0x31] = "ld sp, $%4hx",
[0x32] = "ld [hl-], a",
[0x33] = "inc sp",
[0x34] = "inc [hl]",
[0x35] = "dec [hl]",
[0x36] = "ld [hl], %#2hhx",
[0x36] = "ld [hl], $%02hhx",
[0x37] = "scf",
[0x38] = "jr c, %+hhd",
[0x39] = "add hl, sp",
[0x3a] = "ld a, [hl-]",
[0x3b] = "dec sp",
[0x3c] = "inc a",
[0x3d] = "dec a",
[0x3e] = "ld a, %#2hhx",
[0x3e] = "ld a, $%02hhx",
[0x3f] = "ccf",

[0x40] = "ld b, b",
Expand Down Expand Up @@ -209,70 +209,70 @@ static const char* opcodes[] = {

[0xc0] = "ret nz",
[0xc1] = "pop bc",
[0xc2] = "jp nz, %#4hx",
[0xc3] = "jp %#4hx",
[0xc4] = "call nz, %#4hx",
[0xc2] = "jp nz, $%04hx",
[0xc3] = "jp $%04hx",
[0xc4] = "call nz, $%04hx",
[0xc5] = "push bc",
[0xc6] = "add a, %#2hhx",
[0xc6] = "add a, $%02hhx",
[0xc7] = "rst 00h",
[0xc8] = "ret z",
[0xc9] = "ret",
[0xca] = "jp z, %#4hx",
[0xca] = "jp z, $%04hx",
[0xcb] = "*",
[0xcc] = "call z, %#4hx",
[0xcd] = "call %#4hx",
[0xce] = "adc a, %#2hhx",
[0xcc] = "call z, $%04hx",
[0xcd] = "call $%04hx",
[0xce] = "adc a, $%02hhx",
[0xcf] = "rst 08h",

[0xd0] = "ret nc",
[0xd1] = "pop de",
[0xd2] = "jp nc, %#4hx",
[0xd2] = "jp nc, $%04hx",
[0xd3] = "???",
[0xd4] = "call nc, %#4hx",
[0xd4] = "call nc, $%04hx",
[0xd5] = "push de",
[0xd6] = "sub a, %#2hhx",
[0xd6] = "sub a, $%02hhx",
[0xd7] = "rst 10h",
[0xd8] = "ret c",
[0xd9] = "reti",
[0xda] = "jp c, %#4hx",
[0xda] = "jp c, $%04hx",
[0xdb] = "???",
[0xdc] = "call c, %#4hx",
[0xdc] = "call c, $%04hx",
[0xdd] = "???",
[0xde] = "sbc a, %#2hhx",
[0xde] = "sbc a, $%02hhx",
[0xdf] = "rst 18h",

[0xe0] = "ldh [$ff%2hhx], a",
[0xe0] = "ldh [$ff%02hhx], a",
[0xe1] = "pop hl",
[0xe2] = "ld [c], a",
[0xe2] = "ld [$ff+c], a",
[0xe3] = "???",
[0xe4] = "???",
[0xe5] = "push hl",
[0xe6] = "and a, %#2hhx",
[0xe6] = "and a, $%02hhx",
[0xe7] = "rst 20h",
[0xe8] = "add sp, %+hhd",
[0xe9] = "jp [hl]",
[0xea] = "ld [%#4x], a",
[0xea] = "ld [$%04x], a",
[0xeb] = "???",
[0xec] = "???",
[0xed] = "???",
[0xee] = "xor a, %#2hhx",
[0xee] = "xor a, $%02hhx",
[0xef] = "rst 28h",

[0xf0] = "ldh a, [$ff%2hhx]",
[0xf0] = "ldh a, [$ff%02hhx]",
[0xf1] = "pop af",
[0xf2] = "ld a, [c]",
[0xf2] = "ld a, [$ff+c]",
[0xf3] = "di",
[0xf4] = "???",
[0xf5] = "push af",
[0xf6] = "or a, %#2hhx",
[0xf6] = "or a, $%02hhx",
[0xf7] = "rst 30h",
[0xf8] = "ld hl,sp%+hhd",
[0xf9] = "ld sp, hl",
[0xfa] = "ld a, [%#4hx]",
[0xfa] = "ld a, [$%04hx]",
[0xfb] = "ei",
[0xfc] = "???",
[0xfd] = "???",
[0xfe] = "cp a, %#2hhx",
[0xfe] = "cp a, $%02hhx",
[0xff] = "rst 38h",
};

Expand Down Expand Up @@ -323,7 +323,8 @@ static bool debug_is_jump(uint8_t op){
return strncmp(str, "call", 4) == 0
|| strncmp(str, "jr", 2) == 0
|| strncmp(str, "jp", 2) == 0
|| strncmp(str, "rst", 3) == 0;
|| strncmp(str, "rst", 3) == 0
|| strncmp(str, "ret", 3) == 0;
}

static void debug_get_regs(const uint8_t* op, uint32_t* mask_out, uint32_t* len_out){
Expand Down Expand Up @@ -392,14 +393,12 @@ static void debug_get_regs(const uint8_t* op, uint32_t* mask_out, uint32_t* len_
break;
}



*mask_out = mask;
}

char* p = strchr(str, '%');
if(p){
while(strchr("%+0#", *p)){
while(strchr("%+0", *p)){
++p;
}

Expand Down Expand Up @@ -530,40 +529,59 @@ void debug_dump(uint8_t* op, struct regs* regs){
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){
//printf(" cb %2x: %s %s\n", op[1], cb_ops[y], regs[z]);
printf("%s %s\n", cb_ops[y], cb_regnames[z]);
snprintf(mnemomic, sizeof(mnemomic), "%s %s", cb_ops[y], cb_regnames[z]);
} else {
//printf(" cb %2x: %s %zu %s\n", op[1], cb_ops[x + 7], y, regs[z]);
printf("%s %zu %s\n", cb_ops[x + 7], y, cb_regnames[z]);
snprintf(mnemomic, sizeof(mnemomic), "%s %zu, %s", cb_ops[x + 7], y, cb_regnames[z]);
}
} else {
const char* fmt = opcodes[*op];
int n = printf(fmt, *(uint16_t*)(op+1));

char* p = strchr(fmt, '[');
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 '$': addr = 0xff00 + op[1]; break;
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 [%02x]", 16 - n, " ", mem[addr]);
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);
}

puts("");
}

prev_regs = *regs;
Expand All @@ -575,7 +593,11 @@ void debug_separator(void){
if(!cfg.debug_mode)
return;

puts("---------------+-----------------------------------------+----------------------");
puts("---------------+-----------------------------------------+----------------+-----");

// XXX: not obvious that the function will do this...
prev_op = 0;
prev_len = 0;
}

void debug_msg(const char* fmt, ...){
Expand All @@ -589,9 +611,9 @@ void debug_msg(const char* fmt, ...){
vsnprintf(msg, sizeof(msg), fmt, va);

if(cfg.debug_mode == 2){
printf(" \e[0;35m* * * \e[0m+ \e[0;35m%-39s\e[0m +\n", msg);
printf(" \e[0;35m* * * \e[0m+ \e[0;35m%-39s\e[0m + +\n", msg);
} else {
printf(" * * * + %-39s +\n", msg);
printf(" * * * + %-39s + +\n", msg);
}

va_end(va);
Expand Down
6 changes: 3 additions & 3 deletions minigbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ static inline uint8_t mem_read(uint16_t addr){
if(cfg.debug_mode){
switch(addr){
case 0xFF10 ... 0xFF26: {
int i = (addr - 0xFF00)/5;
int j = (addr - 0xFF00)%5;
debug_msg("Audio read : %4x / NR%1d%1d -> %2x", addr, i, j, val);
int i = (addr - 0xFF10)/5;
int j = (addr - 0xFF10)%5;
debug_msg("Audio read : %4x / NR%1d%1d -> %2x", addr, i+1, j, val);
break;
}
case 0xFF27 ... 0xFF40:
Expand Down

0 comments on commit bf0c72f

Please sign in to comment.