2016-03-30 20:07:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "gb.h"
|
|
|
|
|
2016-07-15 14:06:46 +00:00
|
|
|
typedef struct {
|
|
|
|
bool has_bank;
|
|
|
|
uint16_t bank:9;
|
|
|
|
uint16_t value;
|
|
|
|
} value_t;
|
2016-03-30 20:07:55 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
enum {
|
|
|
|
LVALUE_MEMORY,
|
2018-02-28 17:39:22 +00:00
|
|
|
LVALUE_MEMORY16,
|
2016-03-30 20:07:55 +00:00
|
|
|
LVALUE_REG16,
|
|
|
|
LVALUE_REG_H,
|
|
|
|
LVALUE_REG_L,
|
|
|
|
} kind;
|
|
|
|
union {
|
2016-06-18 17:29:11 +00:00
|
|
|
uint16_t *register_address;
|
2016-07-15 14:06:46 +00:00
|
|
|
value_t memory_address;
|
2016-03-30 20:07:55 +00:00
|
|
|
};
|
|
|
|
} lvalue_t;
|
|
|
|
|
2016-07-14 17:46:00 +00:00
|
|
|
#define VALUE_16(x) ((value_t){false, 0, (x)})
|
|
|
|
|
2016-07-01 15:24:21 +00:00
|
|
|
struct GB_breakpoint_s {
|
2016-07-14 20:25:16 +00:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint16_t addr;
|
|
|
|
uint16_t bank; /* -1 = any bank*/
|
|
|
|
};
|
|
|
|
uint32_t key; /* For sorting and comparing */
|
|
|
|
};
|
2016-07-01 15:24:21 +00:00
|
|
|
char *condition;
|
2019-04-12 17:29:43 +00:00
|
|
|
bool is_jump_to;
|
2016-07-01 15:24:21 +00:00
|
|
|
};
|
|
|
|
|
2016-07-14 20:25:16 +00:00
|
|
|
#define BP_KEY(x) (((struct GB_breakpoint_s){.addr = ((x).value), .bank = (x).has_bank? (x).bank : -1 }).key)
|
|
|
|
|
2016-07-06 21:29:25 +00:00
|
|
|
#define GB_WATCHPOINT_R (1)
|
|
|
|
#define GB_WATCHPOINT_W (2)
|
|
|
|
|
|
|
|
struct GB_watchpoint_s {
|
2016-07-14 20:25:16 +00:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint16_t addr;
|
|
|
|
uint16_t bank; /* -1 = any bank*/
|
|
|
|
};
|
|
|
|
uint32_t key; /* For sorting and comparing */
|
|
|
|
};
|
2016-07-06 21:29:25 +00:00
|
|
|
char *condition;
|
|
|
|
uint8_t flags;
|
|
|
|
};
|
|
|
|
|
2016-07-14 20:25:16 +00:00
|
|
|
#define WP_KEY(x) (((struct GB_watchpoint_s){.addr = ((x).value), .bank = (x).has_bank? (x).bank : -1 }).key)
|
|
|
|
|
|
|
|
static uint16_t bank_for_addr(GB_gameboy_t *gb, uint16_t addr)
|
|
|
|
{
|
|
|
|
if (addr < 0x4000) {
|
|
|
|
return gb->mbc_rom0_bank;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr < 0x8000) {
|
|
|
|
return gb->mbc_rom_bank;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr < 0xD000) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr < 0xE000) {
|
|
|
|
return gb->cgb_ram_bank;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-13 20:00:50 +00:00
|
|
|
|
2016-07-15 14:06:46 +00:00
|
|
|
typedef struct {
|
|
|
|
uint16_t rom0_bank;
|
|
|
|
uint16_t rom_bank;
|
|
|
|
uint8_t mbc_ram_bank;
|
|
|
|
bool mbc_ram_enable;
|
|
|
|
uint8_t ram_bank;
|
|
|
|
uint8_t vram_bank;
|
|
|
|
} banking_state_t;
|
|
|
|
|
|
|
|
static inline void save_banking_state(GB_gameboy_t *gb, banking_state_t *state)
|
|
|
|
{
|
|
|
|
state->rom0_bank = gb->mbc_rom0_bank;
|
|
|
|
state->rom_bank = gb->mbc_rom_bank;
|
|
|
|
state->mbc_ram_bank = gb->mbc_ram_bank;
|
|
|
|
state->mbc_ram_enable = gb->mbc_ram_enable;
|
|
|
|
state->ram_bank = gb->cgb_ram_bank;
|
|
|
|
state->vram_bank = gb->cgb_vram_bank;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void restore_banking_state(GB_gameboy_t *gb, banking_state_t *state)
|
|
|
|
{
|
|
|
|
|
|
|
|
gb->mbc_rom0_bank = state->rom0_bank;
|
|
|
|
gb->mbc_rom_bank = state->rom_bank;
|
|
|
|
gb->mbc_ram_bank = state->mbc_ram_bank;
|
|
|
|
gb->mbc_ram_enable = state->mbc_ram_enable;
|
|
|
|
gb->cgb_ram_bank = state->ram_bank;
|
|
|
|
gb->cgb_vram_bank = state->vram_bank;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void switch_banking_state(GB_gameboy_t *gb, uint16_t bank)
|
|
|
|
{
|
|
|
|
gb->mbc_rom0_bank = bank;
|
|
|
|
gb->mbc_rom_bank = bank;
|
|
|
|
gb->mbc_ram_bank = bank;
|
|
|
|
gb->mbc_ram_enable = true;
|
2018-06-16 10:59:33 +00:00
|
|
|
if (GB_is_cgb(gb)) {
|
2016-07-15 14:06:46 +00:00
|
|
|
gb->cgb_ram_bank = bank & 7;
|
|
|
|
gb->cgb_vram_bank = bank & 1;
|
2018-04-02 16:57:39 +00:00
|
|
|
if (gb->cgb_ram_bank == 0) {
|
|
|
|
gb->cgb_ram_bank = 1;
|
|
|
|
}
|
2016-07-15 14:06:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 20:00:50 +00:00
|
|
|
static const char *value_to_string(GB_gameboy_t *gb, uint16_t value, bool prefer_name)
|
|
|
|
{
|
|
|
|
static __thread char output[256];
|
|
|
|
const GB_bank_symbol_t *symbol = GB_debugger_find_symbol(gb, value);
|
|
|
|
|
|
|
|
if (symbol && (value - symbol->addr > 0x1000 || symbol->addr == 0) ) {
|
|
|
|
symbol = NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-13 20:07:36 +00:00
|
|
|
/* Avoid overflow */
|
2016-07-13 20:46:18 +00:00
|
|
|
if (symbol && strlen(symbol->name) > 240) {
|
2016-07-13 20:07:36 +00:00
|
|
|
symbol = NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-13 20:00:50 +00:00
|
|
|
if (!symbol) {
|
|
|
|
sprintf(output, "$%04x", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (symbol->addr == value) {
|
|
|
|
if (prefer_name) {
|
|
|
|
sprintf(output, "%s ($%04x)", symbol->name, value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(output, "$%04x (%s)", value, symbol->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
if (prefer_name) {
|
|
|
|
sprintf(output, "%s+$%03x ($%04x)", symbol->name, value - symbol->addr, value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(output, "$%04x (%s+$%03x)", value, symbol->name, value - symbol->addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:46:00 +00:00
|
|
|
static const char *debugger_value_to_string(GB_gameboy_t *gb, value_t value, bool prefer_name)
|
|
|
|
{
|
|
|
|
if (!value.has_bank) return value_to_string(gb, value.value, prefer_name);
|
|
|
|
|
|
|
|
static __thread char output[256];
|
|
|
|
const GB_bank_symbol_t *symbol = GB_map_find_symbol(gb->bank_symbols[value.bank], value.value);
|
|
|
|
|
|
|
|
if (symbol && (value.value - symbol->addr > 0x1000 || symbol->addr == 0) ) {
|
|
|
|
symbol = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Avoid overflow */
|
|
|
|
if (symbol && strlen(symbol->name) > 240) {
|
|
|
|
symbol = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!symbol) {
|
|
|
|
sprintf(output, "$%02x:$%04x", value.bank, value.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (symbol->addr == value.value) {
|
|
|
|
if (prefer_name) {
|
|
|
|
sprintf(output, "%s ($%02x:$%04x)", symbol->name, value.bank, value.value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(output, "$%02x:$%04x (%s)", value.bank, value.value, symbol->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
if (prefer_name) {
|
|
|
|
sprintf(output, "%s+$%03x ($%02x:$%04x)", symbol->name, value.value - symbol->addr, value.bank, value.value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(output, "$%02x:$%04x (%s+$%03x)", value.bank, value.value, symbol->name, value.value - symbol->addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static value_t read_lvalue(GB_gameboy_t *gb, lvalue_t lvalue)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
|
|
|
/* Not used until we add support for operators like += */
|
|
|
|
switch (lvalue.kind) {
|
|
|
|
case LVALUE_MEMORY:
|
2016-07-15 14:06:46 +00:00
|
|
|
if (lvalue.memory_address.has_bank) {
|
|
|
|
banking_state_t state;
|
|
|
|
save_banking_state(gb, &state);
|
|
|
|
switch_banking_state(gb, lvalue.memory_address.bank);
|
|
|
|
value_t r = VALUE_16(GB_read_memory(gb, lvalue.memory_address.value));
|
|
|
|
restore_banking_state(gb, &state);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return VALUE_16(GB_read_memory(gb, lvalue.memory_address.value));
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2018-02-28 17:39:22 +00:00
|
|
|
case LVALUE_MEMORY16:
|
|
|
|
if (lvalue.memory_address.has_bank) {
|
|
|
|
banking_state_t state;
|
|
|
|
save_banking_state(gb, &state);
|
|
|
|
switch_banking_state(gb, lvalue.memory_address.bank);
|
2020-04-08 16:17:45 +00:00
|
|
|
value_t r = VALUE_16(GB_read_memory(gb, lvalue.memory_address.value) |
|
|
|
|
(GB_read_memory(gb, lvalue.memory_address.value + 1) * 0x100));
|
2018-02-28 17:39:22 +00:00
|
|
|
restore_banking_state(gb, &state);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return VALUE_16(GB_read_memory(gb, lvalue.memory_address.value) |
|
|
|
|
(GB_read_memory(gb, lvalue.memory_address.value + 1) * 0x100));
|
|
|
|
|
2018-10-12 13:41:25 +00:00
|
|
|
case LVALUE_REG16:
|
|
|
|
return VALUE_16(*lvalue.register_address);
|
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
case LVALUE_REG_L:
|
2016-07-14 17:46:00 +00:00
|
|
|
return VALUE_16(*lvalue.register_address & 0x00FF);
|
2016-03-30 20:07:55 +00:00
|
|
|
|
|
|
|
case LVALUE_REG_H:
|
2016-07-14 17:46:00 +00:00
|
|
|
return VALUE_16(*lvalue.register_address >> 8);
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
2018-10-12 13:41:25 +00:00
|
|
|
|
2018-10-12 14:09:30 +00:00
|
|
|
return VALUE_16(0);
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2016-06-18 17:29:11 +00:00
|
|
|
static void write_lvalue(GB_gameboy_t *gb, lvalue_t lvalue, uint16_t value)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
|
|
|
switch (lvalue.kind) {
|
|
|
|
case LVALUE_MEMORY:
|
2016-07-15 14:06:46 +00:00
|
|
|
if (lvalue.memory_address.has_bank) {
|
|
|
|
banking_state_t state;
|
|
|
|
save_banking_state(gb, &state);
|
|
|
|
switch_banking_state(gb, lvalue.memory_address.bank);
|
|
|
|
GB_write_memory(gb, lvalue.memory_address.value, value);
|
|
|
|
restore_banking_state(gb, &state);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GB_write_memory(gb, lvalue.memory_address.value, value);
|
2016-03-30 20:07:55 +00:00
|
|
|
return;
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2018-02-28 17:39:22 +00:00
|
|
|
case LVALUE_MEMORY16:
|
|
|
|
if (lvalue.memory_address.has_bank) {
|
|
|
|
banking_state_t state;
|
|
|
|
save_banking_state(gb, &state);
|
|
|
|
switch_banking_state(gb, lvalue.memory_address.bank);
|
|
|
|
GB_write_memory(gb, lvalue.memory_address.value, value);
|
2020-04-08 16:17:45 +00:00
|
|
|
GB_write_memory(gb, lvalue.memory_address.value + 1, value >> 8);
|
2018-02-28 17:39:22 +00:00
|
|
|
restore_banking_state(gb, &state);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GB_write_memory(gb, lvalue.memory_address.value, value);
|
|
|
|
GB_write_memory(gb, lvalue.memory_address.value + 1, value >> 8);
|
|
|
|
return;
|
2016-03-30 20:07:55 +00:00
|
|
|
|
|
|
|
case LVALUE_REG16:
|
|
|
|
*lvalue.register_address = value;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case LVALUE_REG_L:
|
|
|
|
*lvalue.register_address &= 0xFF00;
|
|
|
|
*lvalue.register_address |= value & 0xFF;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case LVALUE_REG_H:
|
|
|
|
*lvalue.register_address &= 0x00FF;
|
|
|
|
*lvalue.register_address |= value << 8;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:46:00 +00:00
|
|
|
/* 16 bit value <op> 16 bit value = 16 bit value
|
|
|
|
25 bit address <op> 16 bit value = 25 bit address
|
|
|
|
16 bit value <op> 25 bit address = 25 bit address
|
|
|
|
25 bit address <op> 25 bit address = 16 bit value (since adding pointers, for examples, makes no sense)
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2016-07-14 17:46:00 +00:00
|
|
|
Boolean operators always return a 16-bit value
|
|
|
|
*/
|
|
|
|
#define FIX_BANK(x) ((value_t){a.has_bank ^ b.has_bank, a.has_bank? a.bank : b.bank, (x)})
|
|
|
|
|
|
|
|
static value_t add(value_t a, value_t b) {return FIX_BANK(a.value + b.value);}
|
|
|
|
static value_t sub(value_t a, value_t b) {return FIX_BANK(a.value - b.value);}
|
|
|
|
static value_t mul(value_t a, value_t b) {return FIX_BANK(a.value * b.value);}
|
2020-04-24 17:37:57 +00:00
|
|
|
static value_t _div(value_t a, value_t b)
|
|
|
|
{
|
2016-07-14 17:46:00 +00:00
|
|
|
if (b.value == 0) {
|
|
|
|
return FIX_BANK(0);
|
|
|
|
}
|
|
|
|
return FIX_BANK(a.value / b.value);
|
2016-03-30 20:07:55 +00:00
|
|
|
};
|
2020-04-24 17:37:57 +00:00
|
|
|
static value_t mod(value_t a, value_t b)
|
|
|
|
{
|
2016-07-14 17:46:00 +00:00
|
|
|
if (b.value == 0) {
|
|
|
|
return FIX_BANK(0);
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
2016-07-14 17:46:00 +00:00
|
|
|
return FIX_BANK(a.value % b.value);
|
2016-03-30 20:07:55 +00:00
|
|
|
};
|
2016-07-14 17:46:00 +00:00
|
|
|
static value_t and(value_t a, value_t b) {return FIX_BANK(a.value & b.value);}
|
|
|
|
static value_t or(value_t a, value_t b) {return FIX_BANK(a.value | b.value);}
|
|
|
|
static value_t xor(value_t a, value_t b) {return FIX_BANK(a.value ^ b.value);}
|
|
|
|
static value_t shleft(value_t a, value_t b) {return FIX_BANK(a.value << b.value);}
|
|
|
|
static value_t shright(value_t a, value_t b) {return FIX_BANK(a.value >> b.value);}
|
|
|
|
static value_t assign(GB_gameboy_t *gb, lvalue_t a, uint16_t b)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
|
|
|
write_lvalue(gb, a, b);
|
|
|
|
return read_lvalue(gb, a);
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:46:00 +00:00
|
|
|
static value_t bool_and(value_t a, value_t b) {return VALUE_16(a.value && b.value);}
|
|
|
|
static value_t bool_or(value_t a, value_t b) {return VALUE_16(a.value || b.value);}
|
|
|
|
static value_t equals(value_t a, value_t b) {return VALUE_16(a.value == b.value);}
|
|
|
|
static value_t different(value_t a, value_t b) {return VALUE_16(a.value != b.value);}
|
|
|
|
static value_t lower(value_t a, value_t b) {return VALUE_16(a.value < b.value);}
|
|
|
|
static value_t greater(value_t a, value_t b) {return VALUE_16(a.value > b.value);}
|
|
|
|
static value_t lower_equals(value_t a, value_t b) {return VALUE_16(a.value <= b.value);}
|
|
|
|
static value_t greater_equals(value_t a, value_t b) {return VALUE_16(a.value >= b.value);}
|
|
|
|
static value_t bank(value_t a, value_t b) {return (value_t) {true, a.value, b.value};}
|
|
|
|
|
2016-07-01 10:12:04 +00:00
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
static struct {
|
|
|
|
const char *string;
|
2020-04-09 11:21:07 +00:00
|
|
|
int8_t priority;
|
2016-07-14 17:46:00 +00:00
|
|
|
value_t (*operator)(value_t, value_t);
|
|
|
|
value_t (*lvalue_operator)(GB_gameboy_t *, lvalue_t, uint16_t);
|
2016-03-30 20:07:55 +00:00
|
|
|
} operators[] =
|
|
|
|
{
|
|
|
|
// Yes. This is not C-like. But it makes much more sense.
|
|
|
|
// Deal with it.
|
|
|
|
{"+", 0, add},
|
|
|
|
{"-", 0, sub},
|
2016-07-01 10:12:04 +00:00
|
|
|
{"||", 0, bool_or},
|
2016-03-30 20:07:55 +00:00
|
|
|
{"|", 0, or},
|
|
|
|
{"*", 1, mul},
|
|
|
|
{"/", 1, _div},
|
|
|
|
{"%", 1, mod},
|
2016-07-01 10:12:04 +00:00
|
|
|
{"&&", 1, bool_and},
|
2016-03-30 20:07:55 +00:00
|
|
|
{"&", 1, and},
|
|
|
|
{"^", 1, xor},
|
|
|
|
{"<<", 2, shleft},
|
2020-04-09 11:21:07 +00:00
|
|
|
{"<=", 3, lower_equals},
|
|
|
|
{"<", 3, lower},
|
2016-03-30 20:07:55 +00:00
|
|
|
{">>", 2, shright},
|
2020-04-09 11:21:07 +00:00
|
|
|
{">=", 3, greater_equals},
|
|
|
|
{">", 3, greater},
|
|
|
|
{"==", 3, equals},
|
|
|
|
{"=", -1, NULL, assign},
|
|
|
|
{"!=", 3, different},
|
|
|
|
{":", 4, bank},
|
2016-03-30 20:07:55 +00:00
|
|
|
};
|
|
|
|
|
2016-07-14 17:46:00 +00:00
|
|
|
value_t debugger_evaluate(GB_gameboy_t *gb, const char *string,
|
2016-08-19 11:54:54 +00:00
|
|
|
size_t length, bool *error,
|
2016-07-06 21:29:25 +00:00
|
|
|
uint16_t *watchpoint_address, uint8_t *watchpoint_new_value);
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-07-06 21:29:25 +00:00
|
|
|
static lvalue_t debugger_evaluate_lvalue(GB_gameboy_t *gb, const char *string,
|
2016-08-19 11:54:54 +00:00
|
|
|
size_t length, bool *error,
|
2016-07-06 21:29:25 +00:00
|
|
|
uint16_t *watchpoint_address, uint8_t *watchpoint_new_value)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
|
|
|
*error = false;
|
|
|
|
// Strip whitespace
|
|
|
|
while (length && (string[0] == ' ' || string[0] == '\n' || string[0] == '\r' || string[0] == '\t')) {
|
|
|
|
string++;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
while (length && (string[length-1] == ' ' || string[length-1] == '\n' || string[length-1] == '\r' || string[length-1] == '\t')) {
|
|
|
|
length--;
|
|
|
|
}
|
2020-04-24 17:37:57 +00:00
|
|
|
if (length == 0) {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "Expected expression.\n");
|
2016-03-30 20:07:55 +00:00
|
|
|
*error = true;
|
|
|
|
return (lvalue_t){0,};
|
|
|
|
}
|
|
|
|
if (string[0] == '(' && string[length - 1] == ')') {
|
|
|
|
// Attempt to strip parentheses
|
2020-04-09 11:21:07 +00:00
|
|
|
signed depth = 0;
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
2016-03-30 20:07:55 +00:00
|
|
|
if (string[i] == '(') depth++;
|
|
|
|
if (depth == 0) {
|
|
|
|
// First and last are not matching
|
|
|
|
depth = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (string[i] == ')') depth--;
|
|
|
|
}
|
2016-07-06 21:29:25 +00:00
|
|
|
if (depth == 0) return debugger_evaluate_lvalue(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value);
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
else if (string[0] == '[' && string[length - 1] == ']') {
|
|
|
|
// Attempt to strip square parentheses (memory dereference)
|
2020-04-09 11:21:07 +00:00
|
|
|
signed depth = 0;
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
2016-03-30 20:07:55 +00:00
|
|
|
if (string[i] == '[') depth++;
|
|
|
|
if (depth == 0) {
|
|
|
|
// First and last are not matching
|
|
|
|
depth = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (string[i] == ']') depth--;
|
|
|
|
}
|
|
|
|
if (depth == 0) {
|
2016-07-15 14:06:46 +00:00
|
|
|
return (lvalue_t){LVALUE_MEMORY, .memory_address = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value)};
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-28 17:39:22 +00:00
|
|
|
else if (string[0] == '{' && string[length - 1] == '}') {
|
|
|
|
// Attempt to strip curly parentheses (memory dereference)
|
2020-04-09 11:21:07 +00:00
|
|
|
signed depth = 0;
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
2018-02-28 17:39:22 +00:00
|
|
|
if (string[i] == '{') depth++;
|
|
|
|
if (depth == 0) {
|
|
|
|
// First and last are not matching
|
|
|
|
depth = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (string[i] == '}') depth--;
|
|
|
|
}
|
|
|
|
if (depth == 0) {
|
|
|
|
return (lvalue_t){LVALUE_MEMORY16, .memory_address = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value)};
|
|
|
|
}
|
|
|
|
}
|
2016-03-30 20:07:55 +00:00
|
|
|
|
|
|
|
// Registers
|
2016-07-12 20:30:18 +00:00
|
|
|
if (string[0] != '$' && (string[0] < '0' || string[0] > '9')) {
|
|
|
|
if (length == 1) {
|
|
|
|
switch (string[0]) {
|
2016-03-30 20:07:55 +00:00
|
|
|
case 'a': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->registers[GB_REGISTER_AF]};
|
|
|
|
case 'f': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->registers[GB_REGISTER_AF]};
|
|
|
|
case 'b': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->registers[GB_REGISTER_BC]};
|
|
|
|
case 'c': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->registers[GB_REGISTER_BC]};
|
|
|
|
case 'd': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->registers[GB_REGISTER_DE]};
|
|
|
|
case 'e': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->registers[GB_REGISTER_DE]};
|
|
|
|
case 'h': return (lvalue_t){LVALUE_REG_H, .register_address = &gb->registers[GB_REGISTER_HL]};
|
|
|
|
case 'l': return (lvalue_t){LVALUE_REG_L, .register_address = &gb->registers[GB_REGISTER_HL]};
|
|
|
|
}
|
|
|
|
}
|
2016-07-12 20:30:18 +00:00
|
|
|
else if (length == 2) {
|
|
|
|
switch (string[0]) {
|
|
|
|
case 'a': if (string[1] == 'f') return (lvalue_t){LVALUE_REG16, .register_address = &gb->registers[GB_REGISTER_AF]};
|
|
|
|
case 'b': if (string[1] == 'c') return (lvalue_t){LVALUE_REG16, .register_address = &gb->registers[GB_REGISTER_BC]};
|
|
|
|
case 'd': if (string[1] == 'e') return (lvalue_t){LVALUE_REG16, .register_address = &gb->registers[GB_REGISTER_DE]};
|
|
|
|
case 'h': if (string[1] == 'l') return (lvalue_t){LVALUE_REG16, .register_address = &gb->registers[GB_REGISTER_HL]};
|
|
|
|
case 's': if (string[1] == 'p') return (lvalue_t){LVALUE_REG16, .register_address = &gb->registers[GB_REGISTER_SP]};
|
|
|
|
case 'p': if (string[1] == 'c') return (lvalue_t){LVALUE_REG16, .register_address = &gb->pc};
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 20:22:27 +00:00
|
|
|
GB_log(gb, "Unknown register: %.*s\n", (unsigned) length, string);
|
2016-03-30 20:07:55 +00:00
|
|
|
*error = true;
|
|
|
|
return (lvalue_t){0,};
|
|
|
|
}
|
|
|
|
|
2019-06-15 20:22:27 +00:00
|
|
|
GB_log(gb, "Expression is not an lvalue: %.*s\n", (unsigned) length, string);
|
2016-03-30 20:07:55 +00:00
|
|
|
*error = true;
|
|
|
|
return (lvalue_t){0,};
|
|
|
|
}
|
|
|
|
|
2016-07-14 17:46:00 +00:00
|
|
|
#define ERROR ((value_t){0,})
|
|
|
|
value_t debugger_evaluate(GB_gameboy_t *gb, const char *string,
|
2016-08-19 11:54:54 +00:00
|
|
|
size_t length, bool *error,
|
2016-07-14 17:46:00 +00:00
|
|
|
uint16_t *watchpoint_address, uint8_t *watchpoint_new_value)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
2017-01-12 21:11:26 +00:00
|
|
|
/* Disable watchpoints while evaulating expressions */
|
|
|
|
uint16_t n_watchpoints = gb->n_watchpoints;
|
|
|
|
gb->n_watchpoints = 0;
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2017-01-12 21:11:26 +00:00
|
|
|
value_t ret = ERROR;
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
*error = false;
|
|
|
|
// Strip whitespace
|
|
|
|
while (length && (string[0] == ' ' || string[0] == '\n' || string[0] == '\r' || string[0] == '\t')) {
|
|
|
|
string++;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
while (length && (string[length-1] == ' ' || string[length-1] == '\n' || string[length-1] == '\r' || string[length-1] == '\t')) {
|
|
|
|
length--;
|
|
|
|
}
|
2020-04-24 17:37:57 +00:00
|
|
|
if (length == 0) {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "Expected expression.\n");
|
2016-03-30 20:07:55 +00:00
|
|
|
*error = true;
|
2017-01-12 21:11:26 +00:00
|
|
|
goto exit;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
if (string[0] == '(' && string[length - 1] == ')') {
|
|
|
|
// Attempt to strip parentheses
|
2020-04-09 11:21:07 +00:00
|
|
|
signed depth = 0;
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
2016-03-30 20:07:55 +00:00
|
|
|
if (string[i] == '(') depth++;
|
|
|
|
if (depth == 0) {
|
|
|
|
// First and last are not matching
|
|
|
|
depth = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (string[i] == ')') depth--;
|
|
|
|
}
|
2017-01-12 21:11:26 +00:00
|
|
|
if (depth == 0) {
|
|
|
|
ret = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value);
|
|
|
|
goto exit;
|
|
|
|
}
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
else if (string[0] == '[' && string[length - 1] == ']') {
|
|
|
|
// Attempt to strip square parentheses (memory dereference)
|
2020-04-09 11:21:07 +00:00
|
|
|
signed depth = 0;
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
2016-03-30 20:07:55 +00:00
|
|
|
if (string[i] == '[') depth++;
|
|
|
|
if (depth == 0) {
|
|
|
|
// First and last are not matching
|
|
|
|
depth = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (string[i] == ']') depth--;
|
|
|
|
}
|
2016-07-15 14:06:46 +00:00
|
|
|
|
|
|
|
if (depth == 0) {
|
|
|
|
value_t addr = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value);
|
|
|
|
banking_state_t state;
|
2016-10-13 22:19:39 +00:00
|
|
|
if (addr.bank) {
|
2017-01-12 21:11:26 +00:00
|
|
|
save_banking_state(gb, &state);
|
|
|
|
switch_banking_state(gb, addr.bank);
|
2016-10-13 22:19:39 +00:00
|
|
|
}
|
2017-01-12 21:11:26 +00:00
|
|
|
ret = VALUE_16(GB_read_memory(gb, addr.value));
|
2016-10-13 22:19:39 +00:00
|
|
|
if (addr.bank) {
|
2017-01-12 21:11:26 +00:00
|
|
|
restore_banking_state(gb, &state);
|
2016-10-13 22:19:39 +00:00
|
|
|
}
|
2017-01-12 21:11:26 +00:00
|
|
|
goto exit;
|
2016-07-15 14:06:46 +00:00
|
|
|
}
|
2018-02-28 17:39:22 +00:00
|
|
|
}
|
|
|
|
else if (string[0] == '{' && string[length - 1] == '}') {
|
|
|
|
// Attempt to strip curly parentheses (memory dereference)
|
2020-04-09 11:21:07 +00:00
|
|
|
signed depth = 0;
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
2018-02-28 17:39:22 +00:00
|
|
|
if (string[i] == '{') depth++;
|
|
|
|
if (depth == 0) {
|
|
|
|
// First and last are not matching
|
|
|
|
depth = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (string[i] == '}') depth--;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2018-02-28 17:39:22 +00:00
|
|
|
if (depth == 0) {
|
|
|
|
value_t addr = debugger_evaluate(gb, string + 1, length - 2, error, watchpoint_address, watchpoint_new_value);
|
|
|
|
banking_state_t state;
|
|
|
|
if (addr.bank) {
|
|
|
|
save_banking_state(gb, &state);
|
|
|
|
switch_banking_state(gb, addr.bank);
|
|
|
|
}
|
|
|
|
ret = VALUE_16(GB_read_memory(gb, addr.value) | (GB_read_memory(gb, addr.value + 1) * 0x100));
|
|
|
|
if (addr.bank) {
|
|
|
|
restore_banking_state(gb, &state);
|
|
|
|
}
|
|
|
|
goto exit;
|
|
|
|
}
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
// Search for lowest priority operator
|
2020-04-09 11:21:07 +00:00
|
|
|
signed depth = 0;
|
2019-06-15 20:22:27 +00:00
|
|
|
unsigned operator_index = -1;
|
|
|
|
unsigned operator_pos = 0;
|
2020-04-09 11:32:52 +00:00
|
|
|
for (unsigned i = 0; i < length; i++) {
|
2016-03-30 20:07:55 +00:00
|
|
|
if (string[i] == '(') depth++;
|
|
|
|
else if (string[i] == ')') depth--;
|
|
|
|
else if (string[i] == '[') depth++;
|
|
|
|
else if (string[i] == ']') depth--;
|
|
|
|
else if (depth == 0) {
|
2020-04-09 11:21:07 +00:00
|
|
|
for (unsigned j = 0; j < sizeof(operators) / sizeof(operators[0]); j++) {
|
|
|
|
unsigned operator_length = strlen(operators[j].string);
|
|
|
|
if (operator_length > length - i) continue; // Operator too long
|
|
|
|
|
2016-07-01 10:12:04 +00:00
|
|
|
if (memcmp(string + i, operators[j].string, operator_length) == 0) {
|
2019-06-08 13:08:07 +00:00
|
|
|
if (operator_index != -1 && operators[operator_index].priority < operators[j].priority) {
|
|
|
|
/* for supporting = vs ==, etc*/
|
|
|
|
i += operator_length - 1;
|
2020-04-09 11:21:07 +00:00
|
|
|
break;
|
2019-06-08 13:08:07 +00:00
|
|
|
}
|
2016-03-30 20:07:55 +00:00
|
|
|
// Found an operator!
|
|
|
|
operator_pos = i;
|
|
|
|
operator_index = j;
|
2016-07-01 10:12:04 +00:00
|
|
|
/* for supporting = vs ==, etc*/
|
|
|
|
i += operator_length - 1;
|
|
|
|
break;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operator_index != -1) {
|
2019-06-15 20:22:27 +00:00
|
|
|
unsigned right_start = (unsigned)(operator_pos + strlen(operators[operator_index].string));
|
2016-07-14 17:46:00 +00:00
|
|
|
value_t right = debugger_evaluate(gb, string + right_start, length - right_start, error, watchpoint_address, watchpoint_new_value);
|
2017-01-12 21:11:26 +00:00
|
|
|
if (*error) goto exit;
|
2016-03-30 20:07:55 +00:00
|
|
|
if (operators[operator_index].lvalue_operator) {
|
2016-07-06 21:29:25 +00:00
|
|
|
lvalue_t left = debugger_evaluate_lvalue(gb, string, operator_pos, error, watchpoint_address, watchpoint_new_value);
|
2017-01-12 21:11:26 +00:00
|
|
|
if (*error) goto exit;
|
|
|
|
ret = operators[operator_index].lvalue_operator(gb, left, right.value);
|
|
|
|
goto exit;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
2016-07-14 17:46:00 +00:00
|
|
|
value_t left = debugger_evaluate(gb, string, operator_pos, error, watchpoint_address, watchpoint_new_value);
|
2017-01-12 21:11:26 +00:00
|
|
|
if (*error) goto exit;
|
|
|
|
ret = operators[operator_index].operator(left, right);
|
|
|
|
goto exit;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not an expression - must be a register or a literal
|
|
|
|
|
|
|
|
// Registers
|
2016-07-12 20:30:18 +00:00
|
|
|
if (string[0] != '$' && (string[0] < '0' || string[0] > '9')) {
|
|
|
|
if (length == 1) {
|
|
|
|
switch (string[0]) {
|
2017-01-12 21:11:26 +00:00
|
|
|
case 'a': ret = VALUE_16(gb->registers[GB_REGISTER_AF] >> 8); goto exit;
|
|
|
|
case 'f': ret = VALUE_16(gb->registers[GB_REGISTER_AF] & 0xFF); goto exit;
|
|
|
|
case 'b': ret = VALUE_16(gb->registers[GB_REGISTER_BC] >> 8); goto exit;
|
|
|
|
case 'c': ret = VALUE_16(gb->registers[GB_REGISTER_BC] & 0xFF); goto exit;
|
|
|
|
case 'd': ret = VALUE_16(gb->registers[GB_REGISTER_DE] >> 8); goto exit;
|
|
|
|
case 'e': ret = VALUE_16(gb->registers[GB_REGISTER_DE] & 0xFF); goto exit;
|
|
|
|
case 'h': ret = VALUE_16(gb->registers[GB_REGISTER_HL] >> 8); goto exit;
|
|
|
|
case 'l': ret = VALUE_16(gb->registers[GB_REGISTER_HL] & 0xFF); goto exit;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 20:30:18 +00:00
|
|
|
else if (length == 2) {
|
|
|
|
switch (string[0]) {
|
2017-01-12 21:11:26 +00:00
|
|
|
case 'a': if (string[1] == 'f') {ret = VALUE_16(gb->registers[GB_REGISTER_AF]); goto exit;}
|
|
|
|
case 'b': if (string[1] == 'c') {ret = VALUE_16(gb->registers[GB_REGISTER_BC]); goto exit;}
|
|
|
|
case 'd': if (string[1] == 'e') {ret = VALUE_16(gb->registers[GB_REGISTER_DE]); goto exit;}
|
|
|
|
case 'h': if (string[1] == 'l') {ret = VALUE_16(gb->registers[GB_REGISTER_HL]); goto exit;}
|
|
|
|
case 's': if (string[1] == 'p') {ret = VALUE_16(gb->registers[GB_REGISTER_SP]); goto exit;}
|
|
|
|
case 'p': if (string[1] == 'c') {ret = (value_t){true, bank_for_addr(gb, gb->pc), gb->pc}; goto exit;}
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-12 20:30:18 +00:00
|
|
|
else if (length == 3) {
|
|
|
|
if (watchpoint_address && memcmp(string, "old", 3) == 0) {
|
2017-01-12 21:11:26 +00:00
|
|
|
ret = VALUE_16(GB_read_memory(gb, *watchpoint_address));
|
|
|
|
goto exit;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 20:30:18 +00:00
|
|
|
if (watchpoint_new_value && memcmp(string, "new", 3) == 0) {
|
2017-01-12 21:11:26 +00:00
|
|
|
ret = VALUE_16(*watchpoint_new_value);
|
|
|
|
goto exit;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* $new is identical to $old in read conditions */
|
2016-07-12 20:30:18 +00:00
|
|
|
if (watchpoint_address && memcmp(string, "new", 3) == 0) {
|
2017-01-12 21:11:26 +00:00
|
|
|
ret = VALUE_16(GB_read_memory(gb, *watchpoint_address));
|
|
|
|
goto exit;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-15 11:31:27 +00:00
|
|
|
|
|
|
|
char symbol_name[length + 1];
|
|
|
|
memcpy(symbol_name, string, length);
|
|
|
|
symbol_name[length] = 0;
|
|
|
|
const GB_symbol_t *symbol = GB_reversed_map_find_symbol(&gb->reversed_symbol_map, symbol_name);
|
|
|
|
if (symbol) {
|
2017-01-12 21:11:26 +00:00
|
|
|
ret = (value_t){true, symbol->bank, symbol->addr};
|
|
|
|
goto exit;
|
2016-07-15 11:31:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 20:22:27 +00:00
|
|
|
GB_log(gb, "Unknown register or symbol: %.*s\n", (unsigned) length, string);
|
2016-03-30 20:07:55 +00:00
|
|
|
*error = true;
|
2017-01-12 21:11:26 +00:00
|
|
|
goto exit;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *end;
|
2020-04-09 11:21:07 +00:00
|
|
|
unsigned base = 10;
|
2016-07-12 20:30:18 +00:00
|
|
|
if (string[0] == '$') {
|
|
|
|
string++;
|
|
|
|
base = 16;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
uint16_t literal = (uint16_t) (strtol(string, &end, base));
|
2016-03-30 20:07:55 +00:00
|
|
|
if (end != string + length) {
|
2019-06-15 20:22:27 +00:00
|
|
|
GB_log(gb, "Failed to parse: %.*s\n", (unsigned) length, string);
|
2016-03-30 20:07:55 +00:00
|
|
|
*error = true;
|
2017-01-12 21:11:26 +00:00
|
|
|
goto exit;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
2017-01-12 21:11:26 +00:00
|
|
|
ret = VALUE_16(literal);
|
|
|
|
exit:
|
|
|
|
gb->n_watchpoints = n_watchpoints;
|
|
|
|
return ret;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 19:52:41 +00:00
|
|
|
struct debugger_command_s;
|
2016-10-19 20:48:46 +00:00
|
|
|
typedef bool debugger_command_imp_t(GB_gameboy_t *gb, char *arguments, char *modifiers, const struct debugger_command_s *command);
|
2016-04-05 22:43:35 +00:00
|
|
|
|
2016-08-13 19:52:41 +00:00
|
|
|
typedef struct debugger_command_s {
|
2016-04-05 22:43:35 +00:00
|
|
|
const char *command;
|
2016-06-18 17:29:11 +00:00
|
|
|
uint8_t min_length;
|
2016-04-05 22:43:35 +00:00
|
|
|
debugger_command_imp_t *implementation;
|
|
|
|
const char *help_string; // Null if should not appear in help
|
2016-08-13 19:52:41 +00:00
|
|
|
const char *arguments_format; // For usage message
|
2016-10-19 20:48:46 +00:00
|
|
|
const char *modifiers_format; // For usage message
|
2016-04-05 22:43:35 +00:00
|
|
|
} debugger_command_t;
|
|
|
|
|
|
|
|
static const char *lstrip(const char *str)
|
|
|
|
{
|
|
|
|
while (*str == ' ' || *str == '\t') {
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-07-17 21:39:43 +00:00
|
|
|
#define STOPPED_ONLY \
|
|
|
|
if (!gb->debug_stopped) { \
|
|
|
|
GB_log(gb, "Program is running. \n"); \
|
|
|
|
return false; \
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
#define NO_MODIFIERS \
|
|
|
|
if (modifiers) { \
|
|
|
|
print_usage(gb, command); \
|
|
|
|
return true; \
|
|
|
|
}
|
|
|
|
|
2016-08-13 19:52:41 +00:00
|
|
|
static void print_usage(GB_gameboy_t *gb, const debugger_command_t *command)
|
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
GB_log(gb, "Usage: %s", command->command);
|
|
|
|
|
2017-02-04 17:00:31 +00:00
|
|
|
if (command->modifiers_format) {
|
2016-10-19 20:48:46 +00:00
|
|
|
GB_log(gb, "[/%s]", command->modifiers_format);
|
2016-08-13 19:52:41 +00:00
|
|
|
}
|
2016-10-19 20:48:46 +00:00
|
|
|
|
|
|
|
if (command->arguments_format) {
|
|
|
|
GB_log(gb, " %s", command->arguments_format);
|
2016-08-13 19:52:41 +00:00
|
|
|
}
|
2016-10-19 20:48:46 +00:00
|
|
|
|
|
|
|
GB_log(gb, "\n");
|
2016-08-13 19:52:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool cont(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-05 22:43:35 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-07-17 21:39:43 +00:00
|
|
|
STOPPED_ONLY
|
|
|
|
|
2016-04-05 22:43:35 +00:00
|
|
|
if (strlen(lstrip(arguments))) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-05 22:43:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-07-17 21:39:43 +00:00
|
|
|
|
2016-04-05 22:43:35 +00:00
|
|
|
gb->debug_stopped = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool next(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-05 22:43:35 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-07-17 21:39:43 +00:00
|
|
|
STOPPED_ONLY
|
|
|
|
|
2016-04-05 22:43:35 +00:00
|
|
|
if (strlen(lstrip(arguments))) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-05 22:43:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2016-04-05 22:43:35 +00:00
|
|
|
gb->debug_stopped = false;
|
|
|
|
gb->debug_next_command = true;
|
|
|
|
gb->debug_call_depth = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool step(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-05 22:43:35 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-07-17 21:39:43 +00:00
|
|
|
STOPPED_ONLY
|
|
|
|
|
2016-04-05 22:43:35 +00:00
|
|
|
if (strlen(lstrip(arguments))) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-05 22:43:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool finish(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-05 22:43:35 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-07-17 21:39:43 +00:00
|
|
|
STOPPED_ONLY
|
|
|
|
|
2016-04-05 22:43:35 +00:00
|
|
|
if (strlen(lstrip(arguments))) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-05 22:43:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
gb->debug_stopped = false;
|
|
|
|
gb->debug_fin_command = true;
|
|
|
|
gb->debug_call_depth = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool stack_leak_detection(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-10 20:22:54 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-07-17 21:39:43 +00:00
|
|
|
STOPPED_ONLY
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2016-04-10 20:22:54 +00:00
|
|
|
if (strlen(lstrip(arguments))) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-10 20:22:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
gb->debug_stopped = false;
|
|
|
|
gb->stack_leak_detection = true;
|
|
|
|
gb->debug_call_depth = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool registers(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-05 22:43:35 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-04-05 22:43:35 +00:00
|
|
|
if (strlen(lstrip(arguments))) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-05 22:43:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-01 13:42:17 +00:00
|
|
|
|
2020-03-27 15:56:47 +00:00
|
|
|
GB_log(gb, "AF = $%04x (%c%c%c%c)\n", gb->registers[GB_REGISTER_AF], /* AF can't really be an address */
|
2019-06-01 13:42:17 +00:00
|
|
|
(gb->f & GB_CARRY_FLAG)? 'C' : '-',
|
|
|
|
(gb->f & GB_HALF_CARRY_FLAG)? 'H' : '-',
|
2020-01-03 19:11:45 +00:00
|
|
|
(gb->f & GB_SUBTRACT_FLAG)? 'N' : '-',
|
2019-06-01 13:42:17 +00:00
|
|
|
(gb->f & GB_ZERO_FLAG)? 'Z' : '-');
|
2020-03-27 15:56:47 +00:00
|
|
|
GB_log(gb, "BC = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_BC], false));
|
|
|
|
GB_log(gb, "DE = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_DE], false));
|
|
|
|
GB_log(gb, "HL = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_HL], false));
|
|
|
|
GB_log(gb, "SP = %s\n", value_to_string(gb, gb->registers[GB_REGISTER_SP], false));
|
|
|
|
GB_log(gb, "PC = %s\n", value_to_string(gb, gb->pc, false));
|
|
|
|
GB_log(gb, "IME = %s\n", gb->ime? "Enabled" : "Disabled");
|
2016-04-05 22:43:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:25:41 +00:00
|
|
|
/* Find the index of the closest breakpoint equal or greater to addr */
|
2016-07-14 20:25:16 +00:00
|
|
|
static uint16_t find_breakpoint(GB_gameboy_t *gb, value_t addr)
|
2016-04-06 21:25:41 +00:00
|
|
|
{
|
|
|
|
if (!gb->breakpoints) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-14 20:25:16 +00:00
|
|
|
|
|
|
|
uint32_t key = BP_KEY(addr);
|
|
|
|
|
2020-04-09 11:32:52 +00:00
|
|
|
unsigned min = 0;
|
|
|
|
unsigned max = gb->n_breakpoints;
|
2016-04-06 21:25:41 +00:00
|
|
|
while (min < max) {
|
2016-06-18 17:29:11 +00:00
|
|
|
uint16_t pivot = (min + max) / 2;
|
2016-07-14 20:25:16 +00:00
|
|
|
if (gb->breakpoints[pivot].key == key) return pivot;
|
|
|
|
if (gb->breakpoints[pivot].key > key) {
|
|
|
|
max = pivot;
|
2016-04-06 21:25:41 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
min = pivot + 1;
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 17:29:11 +00:00
|
|
|
return (uint16_t) min;
|
2016-04-06 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool breakpoint(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-05 22:43:35 +00:00
|
|
|
{
|
2019-04-12 17:29:43 +00:00
|
|
|
bool is_jump_to = true;
|
|
|
|
if (!modifiers) {
|
|
|
|
is_jump_to = false;
|
|
|
|
}
|
|
|
|
else if (strcmp(modifiers, "j") != 0) {
|
|
|
|
print_usage(gb, command);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2016-04-05 22:43:35 +00:00
|
|
|
if (strlen(lstrip(arguments)) == 0) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-05 22:43:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-14 20:25:16 +00:00
|
|
|
if (gb->n_breakpoints == (typeof(gb->n_breakpoints)) -1) {
|
|
|
|
GB_log(gb, "Too many breakpoints set\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:24:21 +00:00
|
|
|
char *condition = NULL;
|
|
|
|
if ((condition = strstr(arguments, " if "))) {
|
|
|
|
*condition = 0;
|
|
|
|
condition += strlen(" if ");
|
|
|
|
/* Verify condition is sane (Todo: This might have side effects!) */
|
|
|
|
bool error;
|
2019-06-15 20:22:27 +00:00
|
|
|
debugger_evaluate(gb, condition, (unsigned)strlen(condition), &error, NULL, NULL);
|
2016-07-01 15:24:21 +00:00
|
|
|
if (error) return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:43:35 +00:00
|
|
|
bool error;
|
2019-06-15 20:22:27 +00:00
|
|
|
value_t result = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL);
|
2016-07-14 20:25:16 +00:00
|
|
|
uint32_t key = BP_KEY(result);
|
2016-04-06 21:25:41 +00:00
|
|
|
|
|
|
|
if (error) return true;
|
|
|
|
|
2016-06-18 17:29:11 +00:00
|
|
|
uint16_t index = find_breakpoint(gb, result);
|
2016-07-14 20:25:16 +00:00
|
|
|
if (index < gb->n_breakpoints && gb->breakpoints[index].key == key) {
|
|
|
|
GB_log(gb, "Breakpoint already set at %s\n", debugger_value_to_string(gb, result, true));
|
2016-07-01 15:24:21 +00:00
|
|
|
if (!gb->breakpoints[index].condition && condition) {
|
|
|
|
GB_log(gb, "Added condition to breakpoint\n");
|
|
|
|
gb->breakpoints[index].condition = strdup(condition);
|
|
|
|
}
|
|
|
|
else if (gb->breakpoints[index].condition && condition) {
|
|
|
|
GB_log(gb, "Replaced breakpoint condition\n");
|
|
|
|
free(gb->breakpoints[index].condition);
|
|
|
|
gb->breakpoints[index].condition = strdup(condition);
|
|
|
|
}
|
|
|
|
else if (gb->breakpoints[index].condition && !condition) {
|
|
|
|
GB_log(gb, "Removed breakpoint condition\n");
|
|
|
|
free(gb->breakpoints[index].condition);
|
|
|
|
gb->breakpoints[index].condition = NULL;
|
|
|
|
}
|
2016-04-06 21:25:41 +00:00
|
|
|
return true;
|
2016-04-05 22:43:35 +00:00
|
|
|
}
|
2016-04-06 21:25:41 +00:00
|
|
|
|
2016-06-10 15:03:12 +00:00
|
|
|
gb->breakpoints = realloc(gb->breakpoints, (gb->n_breakpoints + 1) * sizeof(gb->breakpoints[0]));
|
2016-04-06 21:25:41 +00:00
|
|
|
memmove(&gb->breakpoints[index + 1], &gb->breakpoints[index], (gb->n_breakpoints - index) * sizeof(gb->breakpoints[0]));
|
2016-07-14 20:25:16 +00:00
|
|
|
gb->breakpoints[index].key = key;
|
|
|
|
|
2016-07-01 15:24:21 +00:00
|
|
|
if (condition) {
|
|
|
|
gb->breakpoints[index].condition = strdup(condition);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gb->breakpoints[index].condition = NULL;
|
|
|
|
}
|
2016-04-06 21:25:41 +00:00
|
|
|
gb->n_breakpoints++;
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
gb->breakpoints[index].is_jump_to = is_jump_to;
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
if (is_jump_to) {
|
|
|
|
gb->has_jump_to_breakpoints = true;
|
|
|
|
}
|
2016-04-06 21:25:41 +00:00
|
|
|
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, "Breakpoint set at %s\n", debugger_value_to_string(gb, result, true));
|
2016-04-05 22:43:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool delete(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-06 21:25:41 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-04-06 21:25:41 +00:00
|
|
|
if (strlen(lstrip(arguments)) == 0) {
|
2016-08-13 19:52:41 +00:00
|
|
|
for (unsigned i = gb->n_breakpoints; i--;) {
|
|
|
|
if (gb->breakpoints[i].condition) {
|
|
|
|
free(gb->breakpoints[i].condition);
|
2016-07-01 15:24:21 +00:00
|
|
|
}
|
2016-04-06 21:25:41 +00:00
|
|
|
}
|
2016-08-13 19:52:41 +00:00
|
|
|
free(gb->breakpoints);
|
|
|
|
gb->breakpoints = NULL;
|
|
|
|
gb->n_breakpoints = 0;
|
2016-04-06 21:25:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool error;
|
2019-06-15 20:22:27 +00:00
|
|
|
value_t result = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL);
|
2016-07-14 20:25:16 +00:00
|
|
|
uint32_t key = BP_KEY(result);
|
2016-04-06 21:25:41 +00:00
|
|
|
|
|
|
|
if (error) return true;
|
|
|
|
|
2018-05-13 20:17:23 +00:00
|
|
|
uint16_t index = 0;
|
|
|
|
for (unsigned i = 0; i < gb->n_breakpoints; i++) {
|
|
|
|
if (gb->breakpoints[i].key == key) {
|
|
|
|
/* Full match */
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (gb->breakpoints[i].addr == result.value && result.has_bank != (gb->breakpoints[i].bank != (uint16_t) -1)) {
|
|
|
|
/* Partial match */
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2018-05-13 20:17:23 +00:00
|
|
|
if (index >= gb->n_breakpoints) {
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, "No breakpoint set at %s\n", debugger_value_to_string(gb, result, true));
|
2016-04-06 21:25:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-13 20:17:23 +00:00
|
|
|
result.bank = gb->breakpoints[index].bank;
|
|
|
|
result.has_bank = gb->breakpoints[index].bank != (uint16_t) -1;
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2016-07-01 15:24:21 +00:00
|
|
|
if (gb->breakpoints[index].condition) {
|
|
|
|
free(gb->breakpoints[index].condition);
|
|
|
|
}
|
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
if (gb->breakpoints[index].is_jump_to) {
|
|
|
|
gb->has_jump_to_breakpoints = false;
|
|
|
|
for (unsigned i = 0; i < gb->n_breakpoints; i++) {
|
|
|
|
if (i == index) continue;
|
|
|
|
if (gb->breakpoints[i].is_jump_to) {
|
|
|
|
gb->has_jump_to_breakpoints = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2016-07-01 15:24:21 +00:00
|
|
|
memmove(&gb->breakpoints[index], &gb->breakpoints[index + 1], (gb->n_breakpoints - index - 1) * sizeof(gb->breakpoints[0]));
|
2016-04-06 21:25:41 +00:00
|
|
|
gb->n_breakpoints--;
|
2016-06-10 15:03:12 +00:00
|
|
|
gb->breakpoints = realloc(gb->breakpoints, gb->n_breakpoints * sizeof(gb->breakpoints[0]));
|
2016-04-06 21:25:41 +00:00
|
|
|
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, "Breakpoint removed from %s\n", debugger_value_to_string(gb, result, true));
|
2016-04-06 21:25:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-06 21:29:25 +00:00
|
|
|
/* Find the index of the closest watchpoint equal or greater to addr */
|
2016-07-14 20:25:16 +00:00
|
|
|
static uint16_t find_watchpoint(GB_gameboy_t *gb, value_t addr)
|
2016-07-06 21:29:25 +00:00
|
|
|
{
|
|
|
|
if (!gb->watchpoints) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-14 20:25:16 +00:00
|
|
|
uint32_t key = WP_KEY(addr);
|
2020-04-09 11:32:52 +00:00
|
|
|
unsigned min = 0;
|
|
|
|
unsigned max = gb->n_watchpoints;
|
2016-07-06 21:29:25 +00:00
|
|
|
while (min < max) {
|
|
|
|
uint16_t pivot = (min + max) / 2;
|
2016-07-14 20:25:16 +00:00
|
|
|
if (gb->watchpoints[pivot].key == key) return pivot;
|
|
|
|
if (gb->watchpoints[pivot].key > key) {
|
|
|
|
max = pivot;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
min = pivot + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (uint16_t) min;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool watch(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-07-06 21:29:25 +00:00
|
|
|
{
|
|
|
|
if (strlen(lstrip(arguments)) == 0) {
|
|
|
|
print_usage:
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-07-06 21:29:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-14 20:25:16 +00:00
|
|
|
if (gb->n_watchpoints == (typeof(gb->n_watchpoints)) -1) {
|
|
|
|
GB_log(gb, "Too many watchpoints set\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
if (!modifiers) {
|
|
|
|
modifiers = "w";
|
|
|
|
}
|
|
|
|
|
2016-07-06 21:29:25 +00:00
|
|
|
uint8_t flags = 0;
|
2016-10-19 20:48:46 +00:00
|
|
|
while (*modifiers) {
|
|
|
|
switch (*modifiers) {
|
2016-07-06 21:29:25 +00:00
|
|
|
case 'r':
|
|
|
|
flags |= GB_WATCHPOINT_R;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
flags |= GB_WATCHPOINT_W;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto print_usage;
|
|
|
|
}
|
2016-10-19 20:48:46 +00:00
|
|
|
modifiers++;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!flags) {
|
|
|
|
goto print_usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *condition = NULL;
|
|
|
|
if ((condition = strstr(arguments, " if "))) {
|
|
|
|
*condition = 0;
|
|
|
|
condition += strlen(" if ");
|
|
|
|
/* Verify condition is sane (Todo: This might have side effects!) */
|
|
|
|
bool error;
|
|
|
|
/* To make $new and $old legal */
|
|
|
|
uint16_t dummy = 0;
|
|
|
|
uint8_t dummy2 = 0;
|
2019-06-15 20:22:27 +00:00
|
|
|
debugger_evaluate(gb, condition, (unsigned)strlen(condition), &error, &dummy, &dummy2);
|
2016-07-06 21:29:25 +00:00
|
|
|
if (error) return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool error;
|
2019-06-15 20:22:27 +00:00
|
|
|
value_t result = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL);
|
2016-07-14 20:25:16 +00:00
|
|
|
uint32_t key = WP_KEY(result);
|
2016-07-06 21:29:25 +00:00
|
|
|
|
|
|
|
if (error) return true;
|
|
|
|
|
|
|
|
uint16_t index = find_watchpoint(gb, result);
|
2016-07-14 20:25:16 +00:00
|
|
|
if (index < gb->n_watchpoints && gb->watchpoints[index].key == key) {
|
|
|
|
GB_log(gb, "Watchpoint already set at %s\n", debugger_value_to_string(gb, result, true));
|
2016-07-21 12:20:25 +00:00
|
|
|
if (gb->watchpoints[index].flags != flags) {
|
2016-07-06 21:29:25 +00:00
|
|
|
GB_log(gb, "Modified watchpoint type\n");
|
|
|
|
gb->watchpoints[index].flags = flags;
|
|
|
|
}
|
|
|
|
if (!gb->watchpoints[index].condition && condition) {
|
|
|
|
GB_log(gb, "Added condition to watchpoint\n");
|
|
|
|
gb->watchpoints[index].condition = strdup(condition);
|
|
|
|
}
|
|
|
|
else if (gb->watchpoints[index].condition && condition) {
|
|
|
|
GB_log(gb, "Replaced watchpoint condition\n");
|
|
|
|
free(gb->watchpoints[index].condition);
|
|
|
|
gb->watchpoints[index].condition = strdup(condition);
|
|
|
|
}
|
|
|
|
else if (gb->watchpoints[index].condition && !condition) {
|
|
|
|
GB_log(gb, "Removed watchpoint condition\n");
|
|
|
|
free(gb->watchpoints[index].condition);
|
|
|
|
gb->watchpoints[index].condition = NULL;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
gb->watchpoints = realloc(gb->watchpoints, (gb->n_watchpoints + 1) * sizeof(gb->watchpoints[0]));
|
|
|
|
memmove(&gb->watchpoints[index + 1], &gb->watchpoints[index], (gb->n_watchpoints - index) * sizeof(gb->watchpoints[0]));
|
2016-07-14 20:25:16 +00:00
|
|
|
gb->watchpoints[index].key = key;
|
2016-07-06 21:29:25 +00:00
|
|
|
gb->watchpoints[index].flags = flags;
|
|
|
|
if (condition) {
|
|
|
|
gb->watchpoints[index].condition = strdup(condition);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gb->watchpoints[index].condition = NULL;
|
|
|
|
}
|
|
|
|
gb->n_watchpoints++;
|
|
|
|
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, "Watchpoint set at %s\n", debugger_value_to_string(gb, result, true));
|
2016-07-06 21:29:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool unwatch(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-07-06 21:29:25 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-07-06 21:29:25 +00:00
|
|
|
if (strlen(lstrip(arguments)) == 0) {
|
2016-08-13 19:52:41 +00:00
|
|
|
for (unsigned i = gb->n_watchpoints; i--;) {
|
|
|
|
if (gb->watchpoints[i].condition) {
|
|
|
|
free(gb->watchpoints[i].condition);
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-13 19:52:41 +00:00
|
|
|
free(gb->watchpoints);
|
|
|
|
gb->watchpoints = NULL;
|
|
|
|
gb->n_watchpoints = 0;
|
2016-07-06 21:29:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool error;
|
2019-06-15 20:22:27 +00:00
|
|
|
value_t result = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL);
|
2016-07-14 20:25:16 +00:00
|
|
|
uint32_t key = WP_KEY(result);
|
2016-07-06 21:29:25 +00:00
|
|
|
|
|
|
|
if (error) return true;
|
|
|
|
|
2018-05-13 20:17:23 +00:00
|
|
|
uint16_t index = 0;
|
|
|
|
for (unsigned i = 0; i < gb->n_watchpoints; i++) {
|
|
|
|
if (gb->watchpoints[i].key == key) {
|
|
|
|
/* Full match */
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (gb->watchpoints[i].addr == result.value && result.has_bank != (gb->watchpoints[i].bank != (uint16_t) -1)) {
|
|
|
|
/* Partial match */
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2018-05-13 20:17:23 +00:00
|
|
|
if (index >= gb->n_watchpoints) {
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, "No watchpoint set at %s\n", debugger_value_to_string(gb, result, true));
|
2016-07-06 21:29:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2018-05-13 20:17:23 +00:00
|
|
|
result.bank = gb->watchpoints[index].bank;
|
|
|
|
result.has_bank = gb->watchpoints[index].bank != (uint16_t) -1;
|
2016-07-06 21:29:25 +00:00
|
|
|
|
|
|
|
if (gb->watchpoints[index].condition) {
|
|
|
|
free(gb->watchpoints[index].condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove(&gb->watchpoints[index], &gb->watchpoints[index + 1], (gb->n_watchpoints - index - 1) * sizeof(gb->watchpoints[0]));
|
|
|
|
gb->n_watchpoints--;
|
2020-04-24 17:37:57 +00:00
|
|
|
gb->watchpoints = realloc(gb->watchpoints, gb->n_watchpoints *sizeof(gb->watchpoints[0]));
|
2016-07-06 21:29:25 +00:00
|
|
|
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, "Watchpoint removed from %s\n", debugger_value_to_string(gb, result, true));
|
2016-07-06 21:29:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool list(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-06 21:25:41 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-04-06 21:25:41 +00:00
|
|
|
if (strlen(lstrip(arguments))) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-06 21:25:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gb->n_breakpoints == 0) {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "No breakpoints set.\n");
|
2016-04-06 21:25:41 +00:00
|
|
|
}
|
2016-07-06 21:29:25 +00:00
|
|
|
else {
|
|
|
|
GB_log(gb, "%d breakpoint(s) set:\n", gb->n_breakpoints);
|
|
|
|
for (uint16_t i = 0; i < gb->n_breakpoints; i++) {
|
2016-07-14 20:25:16 +00:00
|
|
|
value_t addr = (value_t){gb->breakpoints[i].bank != (uint16_t)-1, gb->breakpoints[i].bank, gb->breakpoints[i].addr};
|
2016-07-06 21:29:25 +00:00
|
|
|
if (gb->breakpoints[i].condition) {
|
2019-04-12 17:29:43 +00:00
|
|
|
GB_log(gb, " %d. %s (%sCondition: %s)\n", i + 1,
|
2016-07-14 20:25:16 +00:00
|
|
|
debugger_value_to_string(gb, addr, addr.has_bank),
|
2019-04-12 17:29:43 +00:00
|
|
|
gb->breakpoints[i].is_jump_to? "Jump to, ": "",
|
2016-07-13 20:00:50 +00:00
|
|
|
gb->breakpoints[i].condition);
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-04-12 17:29:43 +00:00
|
|
|
GB_log(gb, " %d. %s%s\n", i + 1,
|
|
|
|
debugger_value_to_string(gb, addr, addr.has_bank),
|
|
|
|
gb->breakpoints[i].is_jump_to? " (Jump to)" : "");
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
2016-07-01 15:24:21 +00:00
|
|
|
}
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gb->n_watchpoints == 0) {
|
|
|
|
GB_log(gb, "No watchpoints set.\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GB_log(gb, "%d watchpoint(s) set:\n", gb->n_watchpoints);
|
|
|
|
for (uint16_t i = 0; i < gb->n_watchpoints; i++) {
|
2016-07-14 20:25:16 +00:00
|
|
|
value_t addr = (value_t){gb->watchpoints[i].bank != (uint16_t)-1, gb->watchpoints[i].bank, gb->watchpoints[i].addr};
|
2016-07-06 21:29:25 +00:00
|
|
|
if (gb->watchpoints[i].condition) {
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, " %d. %s (%c%c, Condition: %s)\n", i + 1, debugger_value_to_string(gb, addr, addr.has_bank),
|
2016-07-13 20:00:50 +00:00
|
|
|
(gb->watchpoints[i].flags & GB_WATCHPOINT_R)? 'r' : '-',
|
|
|
|
(gb->watchpoints[i].flags & GB_WATCHPOINT_W)? 'w' : '-',
|
|
|
|
gb->watchpoints[i].condition);
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-04-24 17:37:57 +00:00
|
|
|
GB_log(gb, " %d. %s (%c%c)\n", i + 1, debugger_value_to_string(gb, addr, addr.has_bank),
|
2016-07-13 20:00:50 +00:00
|
|
|
(gb->watchpoints[i].flags & GB_WATCHPOINT_R)? 'r' : '-',
|
|
|
|
(gb->watchpoints[i].flags & GB_WATCHPOINT_W)? 'w' : '-');
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
2016-07-01 15:24:21 +00:00
|
|
|
}
|
2016-04-06 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
static bool _should_break(GB_gameboy_t *gb, value_t addr, bool jump_to)
|
2016-04-06 21:25:41 +00:00
|
|
|
{
|
2016-06-18 17:29:11 +00:00
|
|
|
uint16_t index = find_breakpoint(gb, addr);
|
2016-07-14 20:25:16 +00:00
|
|
|
uint32_t key = BP_KEY(addr);
|
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
if (index < gb->n_breakpoints && gb->breakpoints[index].key == key && gb->breakpoints[index].is_jump_to == jump_to) {
|
2016-07-01 15:24:21 +00:00
|
|
|
if (!gb->breakpoints[index].condition) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool error;
|
|
|
|
bool condition = debugger_evaluate(gb, gb->breakpoints[index].condition,
|
2019-06-15 20:22:27 +00:00
|
|
|
(unsigned)strlen(gb->breakpoints[index].condition), &error, NULL, NULL).value;
|
2016-07-01 15:24:21 +00:00
|
|
|
if (error) {
|
|
|
|
/* Should never happen */
|
|
|
|
GB_log(gb, "An internal error has occured\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return condition;
|
2016-04-06 21:25:41 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
static bool should_break(GB_gameboy_t *gb, uint16_t addr, bool jump_to)
|
2016-07-14 20:25:16 +00:00
|
|
|
{
|
|
|
|
/* Try any-bank breakpoint */
|
|
|
|
value_t full_addr = (VALUE_16(addr));
|
2019-04-12 17:29:43 +00:00
|
|
|
if (_should_break(gb, full_addr, jump_to)) return true;
|
2016-07-14 20:25:16 +00:00
|
|
|
|
|
|
|
/* Try bank-specific breakpoint */
|
|
|
|
full_addr.has_bank = true;
|
|
|
|
full_addr.bank = bank_for_addr(gb, addr);
|
2019-04-12 17:29:43 +00:00
|
|
|
return _should_break(gb, full_addr, jump_to);
|
2016-07-14 20:25:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool print(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-05 22:43:35 +00:00
|
|
|
{
|
|
|
|
if (strlen(lstrip(arguments)) == 0) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-05 22:43:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
if (!modifiers || !modifiers[0]) {
|
|
|
|
modifiers = "a";
|
|
|
|
}
|
|
|
|
else if (modifiers[1]) {
|
|
|
|
print_usage(gb, command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:43:35 +00:00
|
|
|
bool error;
|
2019-06-15 20:22:27 +00:00
|
|
|
value_t result = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL);
|
2016-04-05 22:43:35 +00:00
|
|
|
if (!error) {
|
2016-10-19 20:48:46 +00:00
|
|
|
switch (modifiers[0]) {
|
|
|
|
case 'a':
|
|
|
|
GB_log(gb, "=%s\n", debugger_value_to_string(gb, result, false));
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
GB_log(gb, "=%d\n", result.value);
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
GB_log(gb, "=$%x\n", result.value);
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
GB_log(gb, "=0%o\n", result.value);
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
{
|
|
|
|
if (!result.value) {
|
|
|
|
GB_log(gb, "=%%0\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char binary[17];
|
|
|
|
binary[16] = 0;
|
|
|
|
char *ptr = &binary[16];
|
|
|
|
while (result.value) {
|
|
|
|
*(--ptr) = (result.value & 1)? '1' : '0';
|
|
|
|
result.value >>= 1;
|
|
|
|
}
|
|
|
|
GB_log(gb, "=%%%s\n", ptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-04-05 22:43:35 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool examine(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-05 22:43:35 +00:00
|
|
|
{
|
|
|
|
if (strlen(lstrip(arguments)) == 0) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-05 22:43:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool error;
|
2019-06-15 20:22:27 +00:00
|
|
|
value_t addr = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL);
|
2016-10-19 20:48:46 +00:00
|
|
|
uint16_t count = 32;
|
|
|
|
|
|
|
|
if (modifiers) {
|
|
|
|
char *end;
|
|
|
|
count = (uint16_t) (strtol(modifiers, &end, 10));
|
|
|
|
if (*end) {
|
|
|
|
print_usage(gb, command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:43:35 +00:00
|
|
|
if (!error) {
|
2016-07-14 18:15:24 +00:00
|
|
|
if (addr.has_bank) {
|
2016-07-15 14:06:46 +00:00
|
|
|
banking_state_t old_state;
|
|
|
|
save_banking_state(gb, &old_state);
|
|
|
|
switch_banking_state(gb, addr.bank);
|
2016-07-14 18:15:24 +00:00
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
while (count) {
|
|
|
|
GB_log(gb, "%02x:%04x: ", addr.bank, addr.value);
|
2020-04-09 11:32:52 +00:00
|
|
|
for (unsigned i = 0; i < 16 && count; i++) {
|
2016-10-19 20:48:46 +00:00
|
|
|
GB_log(gb, "%02x ", GB_read_memory(gb, addr.value + i));
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
addr.value += 16;
|
|
|
|
GB_log(gb, "\n");
|
2016-07-14 18:15:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 14:06:46 +00:00
|
|
|
restore_banking_state(gb, &old_state);
|
2016-07-14 18:15:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-10-19 20:48:46 +00:00
|
|
|
while (count) {
|
|
|
|
GB_log(gb, "%04x: ", addr.value);
|
2020-04-09 11:32:52 +00:00
|
|
|
for (unsigned i = 0; i < 16 && count; i++) {
|
2016-10-19 20:48:46 +00:00
|
|
|
GB_log(gb, "%02x ", GB_read_memory(gb, addr.value + i));
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
addr.value += 16;
|
|
|
|
GB_log(gb, "\n");
|
2016-07-14 18:15:24 +00:00
|
|
|
}
|
2016-04-05 22:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:55:23 +00:00
|
|
|
static bool disassemble(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
|
|
|
{
|
|
|
|
if (strlen(lstrip(arguments)) == 0) {
|
|
|
|
arguments = "pc";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool error;
|
2019-06-15 20:22:27 +00:00
|
|
|
value_t addr = debugger_evaluate(gb, arguments, (unsigned)strlen(arguments), &error, NULL, NULL);
|
2016-10-19 20:55:23 +00:00
|
|
|
uint16_t count = 5;
|
|
|
|
|
|
|
|
if (modifiers) {
|
|
|
|
char *end;
|
|
|
|
count = (uint16_t) (strtol(modifiers, &end, 10));
|
|
|
|
if (*end) {
|
|
|
|
print_usage(gb, command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
if (addr.has_bank) {
|
|
|
|
banking_state_t old_state;
|
|
|
|
save_banking_state(gb, &old_state);
|
|
|
|
switch_banking_state(gb, addr.bank);
|
|
|
|
|
|
|
|
GB_cpu_disassemble(gb, addr.value, count);
|
|
|
|
|
|
|
|
restore_banking_state(gb, &old_state);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GB_cpu_disassemble(gb, addr.value, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool mbc(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-04-08 23:00:10 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
|
|
|
|
2016-04-08 23:00:10 +00:00
|
|
|
if (strlen(lstrip(arguments))) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-04-08 23:00:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GB_cartridge_t *cartridge = gb->cartridge_type;
|
|
|
|
|
|
|
|
if (cartridge->has_ram) {
|
2016-08-13 19:52:41 +00:00
|
|
|
GB_log(gb, "Cartrdige includes%s RAM: $%x bytes\n", cartridge->has_battery? " battery-backed": "", gb->mbc_ram_size);
|
2016-04-08 23:00:10 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "No cartridge RAM\n");
|
2016-04-08 23:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cartridge->mbc_type) {
|
2020-04-08 16:07:29 +00:00
|
|
|
if (gb->is_mbc30) {
|
|
|
|
GB_log(gb, "MBC30\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
static const char *const mapper_names[] = {
|
|
|
|
[GB_MBC1] = "MBC1",
|
|
|
|
[GB_MBC2] = "MBC2",
|
|
|
|
[GB_MBC3] = "MBC3",
|
|
|
|
[GB_MBC5] = "MBC5",
|
|
|
|
[GB_HUC1] = "HUC1",
|
|
|
|
[GB_HUC3] = "HUC3",
|
|
|
|
};
|
|
|
|
GB_log(gb, "%s\n", mapper_names[cartridge->mbc_type]);
|
|
|
|
}
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "Current mapped ROM bank: %x\n", gb->mbc_rom_bank);
|
2016-04-08 23:00:10 +00:00
|
|
|
if (cartridge->has_ram) {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "Current mapped RAM bank: %x\n", gb->mbc_ram_bank);
|
|
|
|
GB_log(gb, "RAM is curently %s\n", gb->mbc_ram_enable? "enabled" : "disabled");
|
2016-04-08 23:00:10 +00:00
|
|
|
}
|
2016-07-09 16:25:13 +00:00
|
|
|
if (cartridge->mbc_type == GB_MBC1 && gb->mbc1_wiring == GB_STANDARD_MBC1_WIRING) {
|
2016-07-09 14:34:55 +00:00
|
|
|
GB_log(gb, "MBC1 banking mode is %s\n", gb->mbc1.mode == 1 ? "RAM" : "ROM");
|
2016-04-08 23:00:10 +00:00
|
|
|
}
|
2016-07-09 16:25:13 +00:00
|
|
|
if (cartridge->mbc_type == GB_MBC1 && gb->mbc1_wiring == GB_MBC1M_WIRING) {
|
|
|
|
GB_log(gb, "MBC1 uses MBC1M wiring. \n");
|
|
|
|
GB_log(gb, "Current mapped ROM0 bank: %x\n", gb->mbc_rom0_bank);
|
|
|
|
GB_log(gb, "MBC1 multicart banking mode is %s\n", gb->mbc1.mode == 1 ? "enabled" : "disabled");
|
|
|
|
}
|
2016-04-08 23:00:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "No MBC\n");
|
2016-04-08 23:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cartridge->has_rumble) {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "Cart contains a rumble pak\n");
|
2016-04-08 23:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cartridge->has_rtc) {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "Cart contains a real time clock\n");
|
2016-04-08 23:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool backtrace(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-08-09 19:48:53 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
|
|
|
|
2016-08-09 19:48:53 +00:00
|
|
|
if (strlen(lstrip(arguments))) {
|
2016-08-13 19:52:41 +00:00
|
|
|
print_usage(gb, command);
|
2016-08-09 19:48:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2017-06-03 13:19:39 +00:00
|
|
|
GB_log(gb, " 1. %s\n", debugger_value_to_string(gb, (value_t){true, bank_for_addr(gb, gb->pc), gb->pc}, true));
|
2019-06-15 20:22:27 +00:00
|
|
|
for (unsigned i = gb->backtrace_size; i--;) {
|
2016-08-09 19:48:53 +00:00
|
|
|
GB_log(gb, "%3d. %s\n", gb->backtrace_size - i + 1, debugger_value_to_string(gb, (value_t){true, gb->backtrace_returns[i].bank, gb->backtrace_returns[i].addr}, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool ticks(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-09-16 08:58:31 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-09-16 08:58:31 +00:00
|
|
|
STOPPED_ONLY
|
|
|
|
|
|
|
|
if (strlen(lstrip(arguments))) {
|
|
|
|
print_usage(gb, command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-09 11:32:52 +00:00
|
|
|
GB_log(gb, "Ticks: %llu. (Resetting)\n", (unsigned long long)gb->debugger_ticks);
|
2016-09-16 08:58:31 +00:00
|
|
|
gb->debugger_ticks = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-01 19:08:34 +00:00
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool palettes(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
2016-10-01 19:08:34 +00:00
|
|
|
{
|
2016-10-19 20:48:46 +00:00
|
|
|
NO_MODIFIERS
|
2016-10-01 19:08:34 +00:00
|
|
|
if (strlen(lstrip(arguments))) {
|
|
|
|
print_usage(gb, command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-16 10:59:33 +00:00
|
|
|
if (!GB_is_cgb(gb)) {
|
2016-10-01 19:08:34 +00:00
|
|
|
GB_log(gb, "Not available on a DMG.\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_log(gb, "Background palettes: \n");
|
|
|
|
for (unsigned i = 0; i < 32; i++) {
|
2017-04-19 20:26:39 +00:00
|
|
|
GB_log(gb, "%04x ", ((uint16_t *)&gb->background_palettes_data)[i]);
|
2016-10-01 19:08:34 +00:00
|
|
|
if (i % 4 == 3) {
|
|
|
|
GB_log(gb, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_log(gb, "Sprites palettes: \n");
|
|
|
|
for (unsigned i = 0; i < 32; i++) {
|
2017-04-19 20:26:39 +00:00
|
|
|
GB_log(gb, "%04x ", ((uint16_t *)&gb->sprite_palettes_data)[i]);
|
2016-10-01 19:08:34 +00:00
|
|
|
if (i % 4 == 3) {
|
|
|
|
GB_log(gb, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-22 11:55:17 +00:00
|
|
|
|
|
|
|
static bool lcd(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
|
|
|
{
|
|
|
|
NO_MODIFIERS
|
|
|
|
if (strlen(lstrip(arguments))) {
|
|
|
|
print_usage(gb, command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
GB_log(gb, "LCDC:\n");
|
|
|
|
GB_log(gb, " LCD enabled: %s\n",(gb->io_registers[GB_IO_LCDC] & 128)? "Enabled" : "Disabled");
|
2019-06-08 11:35:52 +00:00
|
|
|
GB_log(gb, " %s: %s\n", (gb->cgb_mode? "Sprite priority flags" : "Background and Window"),
|
2017-04-22 11:55:17 +00:00
|
|
|
(gb->io_registers[GB_IO_LCDC] & 1)? "Enabled" : "Disabled");
|
|
|
|
GB_log(gb, " Objects: %s\n", (gb->io_registers[GB_IO_LCDC] & 2)? "Enabled" : "Disabled");
|
|
|
|
GB_log(gb, " Object size: %s\n", (gb->io_registers[GB_IO_LCDC] & 4)? "8x16" : "8x8");
|
|
|
|
GB_log(gb, " Background tilemap: %s\n", (gb->io_registers[GB_IO_LCDC] & 8)? "$9C00" : "$9800");
|
|
|
|
GB_log(gb, " Background and Window Tileset: %s\n", (gb->io_registers[GB_IO_LCDC] & 16)? "$8000" : "$8800");
|
|
|
|
GB_log(gb, " Window: %s\n", (gb->io_registers[GB_IO_LCDC] & 32)? "Enabled" : "Disabled");
|
|
|
|
GB_log(gb, " Window tilemap: %s\n", (gb->io_registers[GB_IO_LCDC] & 64)? "$9C00" : "$9800");
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2017-04-22 11:55:17 +00:00
|
|
|
GB_log(gb, "\nSTAT:\n");
|
|
|
|
static const char *modes[] = {"Mode 0, H-Blank", "Mode 1, V-Blank", "Mode 2, OAM", "Mode 3, Rendering"};
|
|
|
|
GB_log(gb, " Current mode: %s\n", modes[gb->io_registers[GB_IO_STAT] & 3]);
|
|
|
|
GB_log(gb, " LYC flag: %s\n", (gb->io_registers[GB_IO_STAT] & 4)? "On" : "Off");
|
|
|
|
GB_log(gb, " H-Blank interrupt: %s\n", (gb->io_registers[GB_IO_STAT] & 8)? "Enabled" : "Disabled");
|
|
|
|
GB_log(gb, " V-Blank interrupt: %s\n", (gb->io_registers[GB_IO_STAT] & 16)? "Enabled" : "Disabled");
|
|
|
|
GB_log(gb, " OAM interrupt: %s\n", (gb->io_registers[GB_IO_STAT] & 32)? "Enabled" : "Disabled");
|
|
|
|
GB_log(gb, " LYC interrupt: %s\n", (gb->io_registers[GB_IO_STAT] & 64)? "Enabled" : "Disabled");
|
2019-05-15 10:39:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-06-22 15:38:54 +00:00
|
|
|
GB_log(gb, "\nCurrent line: %d\n", gb->current_line);
|
|
|
|
GB_log(gb, "Current state: ");
|
|
|
|
if (!(gb->io_registers[GB_IO_LCDC] & 0x80)) {
|
|
|
|
GB_log(gb, "Off\n");
|
|
|
|
}
|
|
|
|
else if (gb->display_state == 7 || gb->display_state == 8) {
|
|
|
|
GB_log(gb, "Reading OAM data (%d/40)\n", gb->display_state == 8? gb->oam_search_index : 0);
|
|
|
|
}
|
2019-01-19 17:32:26 +00:00
|
|
|
else if (gb->display_state <= 3 || gb->display_state == 24 || gb->display_state == 31) {
|
2019-02-16 02:19:16 +00:00
|
|
|
GB_log(gb, "Glitched line 0 OAM mode (%d cycles to next event)\n", -gb->display_cycles / 2);
|
2018-06-22 15:38:54 +00:00
|
|
|
}
|
|
|
|
else if (gb->mode_for_interrupt == 3) {
|
|
|
|
signed pixel = gb->position_in_line > 160? (int8_t) gb->position_in_line : gb->position_in_line;
|
|
|
|
GB_log(gb, "Rendering pixel (%d/160)\n", pixel);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GB_log(gb, "Sleeping (%d cycles to next event)\n", -gb->display_cycles / 2);
|
|
|
|
}
|
2017-04-22 11:55:17 +00:00
|
|
|
GB_log(gb, "LY: %d\n", gb->io_registers[GB_IO_LY]);
|
|
|
|
GB_log(gb, "LYC: %d\n", gb->io_registers[GB_IO_LYC]);
|
2020-04-24 17:37:57 +00:00
|
|
|
GB_log(gb, "Window position: %d, %d\n", (signed) gb->io_registers[GB_IO_WX] - 7, gb->io_registers[GB_IO_WY]);
|
2017-04-22 11:55:17 +00:00
|
|
|
GB_log(gb, "Interrupt line: %s\n", gb->stat_interrupt_line? "On" : "Off");
|
2019-05-15 10:39:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool apu(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
|
|
|
{
|
|
|
|
NO_MODIFIERS
|
|
|
|
if (strlen(lstrip(arguments))) {
|
|
|
|
print_usage(gb, command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GB_log(gb, "Current state: ");
|
2019-05-17 17:48:49 +00:00
|
|
|
if (!gb->apu.global_enable) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, "Disabled\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GB_log(gb, "Enabled\n");
|
2019-05-17 17:48:49 +00:00
|
|
|
for (uint8_t channel = 0; channel < GB_N_CHANNELS; channel++) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, "CH%u is %s, DAC %s; current sample = 0x%x\n", channel + 1,
|
|
|
|
gb->apu.is_active[channel] ? "active " : "inactive",
|
|
|
|
GB_apu_is_DAC_enabled(gb, channel) ? "active " : "inactive",
|
|
|
|
gb->apu.samples[channel]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_log(gb, "SO1 (left output): volume %u,", gb->io_registers[GB_IO_NR50] & 0x07);
|
2019-05-17 17:48:49 +00:00
|
|
|
if (gb->io_registers[GB_IO_NR51] & 0x0f) {
|
|
|
|
for (uint8_t channel = 0, mask = 0x01; channel < GB_N_CHANNELS; channel++, mask <<= 1) {
|
|
|
|
if (gb->io_registers[GB_IO_NR51] & mask) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, " CH%u", channel + 1);
|
|
|
|
}
|
|
|
|
}
|
2019-05-17 17:48:49 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, " no channels");
|
|
|
|
}
|
|
|
|
GB_log(gb, "%s\n", gb->io_registers[GB_IO_NR50] & 0x80 ? " VIN": "");
|
|
|
|
|
|
|
|
GB_log(gb, "SO2 (right output): volume %u,", gb->io_registers[GB_IO_NR50] & 0x70 >> 4);
|
2019-05-17 17:48:49 +00:00
|
|
|
if (gb->io_registers[GB_IO_NR51] & 0xf0) {
|
|
|
|
for (uint8_t channel = 0, mask = 0x10; channel < GB_N_CHANNELS; channel++, mask <<= 1) {
|
|
|
|
if (gb->io_registers[GB_IO_NR51] & mask) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, " CH%u", channel + 1);
|
|
|
|
}
|
|
|
|
}
|
2019-05-17 17:48:49 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, " no channels");
|
|
|
|
}
|
|
|
|
GB_log(gb, "%s\n", gb->io_registers[GB_IO_NR50] & 0x80 ? " VIN": "");
|
|
|
|
|
|
|
|
|
2019-05-17 17:48:49 +00:00
|
|
|
for (uint8_t channel = GB_SQUARE_1; channel <= GB_SQUARE_2; channel++) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, "\nCH%u:\n", channel + 1);
|
|
|
|
GB_log(gb, " Current volume: %u, current sample length: %u APU ticks (next in %u ticks)\n",
|
2019-05-18 00:46:24 +00:00
|
|
|
gb->apu.square_channels[channel].current_volume,
|
|
|
|
(gb->apu.square_channels[channel].sample_length ^ 0x7FF) * 2 + 1,
|
|
|
|
gb->apu.square_channels[channel].sample_countdown);
|
2019-05-15 10:39:08 +00:00
|
|
|
|
|
|
|
uint8_t nrx2 = gb->io_registers[channel == GB_SQUARE_1? GB_IO_NR12 : GB_IO_NR22];
|
|
|
|
GB_log(gb, " %u 256 Hz ticks till next volume %screase (out of %u)\n",
|
|
|
|
gb->apu.square_channels[channel].volume_countdown,
|
|
|
|
nrx2 & 8 ? "in" : "de",
|
|
|
|
nrx2 & 7);
|
|
|
|
|
|
|
|
uint8_t duty = gb->io_registers[channel == GB_SQUARE_1? GB_IO_NR11 :GB_IO_NR21] >> 6;
|
|
|
|
GB_log(gb, " Duty cycle %s%% (%s), current index %u/8%s\n",
|
2019-05-17 19:03:10 +00:00
|
|
|
duty > 3? "" : (const char *[]){"12.5", " 25", " 50", " 75"}[duty],
|
|
|
|
duty > 3? "" : (const char *[]){"_______-", "-______-", "-____---", "_------_"}[duty],
|
|
|
|
gb->apu.square_channels[channel].current_sample_index & 0x7f,
|
|
|
|
gb->apu.square_channels[channel].current_sample_index >> 7 ? " (suppressed)" : "");
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-05-17 17:48:49 +00:00
|
|
|
if (channel == GB_SQUARE_1) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, " Frequency sweep %s and %s (next in %u APU ticks)\n",
|
2019-05-17 19:03:10 +00:00
|
|
|
gb->apu.sweep_enabled? "active" : "inactive",
|
|
|
|
gb->apu.sweep_decreasing? "decreasing" : "increasing",
|
|
|
|
gb->apu.square_sweep_calculate_countdown);
|
2019-05-15 10:39:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 17:48:49 +00:00
|
|
|
if (gb->apu.square_channels[channel].length_enabled) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, " Channel will end in %u 256 Hz ticks\n",
|
|
|
|
gb->apu.square_channels[channel].pulse_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GB_log(gb, "\nCH3:\n");
|
|
|
|
GB_log(gb, " Wave:");
|
2019-05-17 17:48:49 +00:00
|
|
|
for (uint8_t i = 0; i < 32; i++) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, "%s%X", i%4?"":" ", gb->apu.wave_channel.wave_form[i]);
|
|
|
|
}
|
2019-05-17 17:48:49 +00:00
|
|
|
GB_log(gb, "\n");
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, " Current position: %u\n", gb->apu.wave_channel.current_sample_index);
|
|
|
|
|
2019-05-17 17:48:49 +00:00
|
|
|
GB_log(gb, " Volume %s (right-shifted %u times)\n",
|
2019-05-17 19:03:10 +00:00
|
|
|
gb->apu.wave_channel.shift > 4? "" : (const char *[]){"100%", "50%", "25%", "", "muted"}[gb->apu.wave_channel.shift],
|
|
|
|
gb->apu.wave_channel.shift);
|
2019-05-15 10:39:08 +00:00
|
|
|
|
|
|
|
GB_log(gb, " Current sample length: %u APU ticks (next in %u ticks)\n",
|
2019-05-18 00:46:24 +00:00
|
|
|
gb->apu.wave_channel.sample_length ^ 0x7ff,
|
2019-05-15 10:39:08 +00:00
|
|
|
gb->apu.wave_channel.sample_countdown);
|
|
|
|
|
2019-05-17 17:48:49 +00:00
|
|
|
if (gb->apu.wave_channel.length_enabled) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, " Channel will end in %u 256 Hz ticks\n",
|
|
|
|
gb->apu.wave_channel.pulse_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GB_log(gb, "\nCH4:\n");
|
|
|
|
GB_log(gb, " Current volume: %u, current sample length: %u APU ticks (next in %u ticks)\n",
|
|
|
|
gb->apu.noise_channel.current_volume,
|
2019-05-18 00:46:24 +00:00
|
|
|
gb->apu.noise_channel.sample_length * 4 + 3,
|
2019-05-15 10:39:08 +00:00
|
|
|
gb->apu.noise_channel.sample_countdown);
|
|
|
|
|
|
|
|
GB_log(gb, " %u 256 Hz ticks till next volume %screase (out of %u)\n",
|
|
|
|
gb->apu.noise_channel.volume_countdown,
|
|
|
|
gb->io_registers[GB_IO_NR42] & 8 ? "in" : "de",
|
|
|
|
gb->io_registers[GB_IO_NR42] & 7);
|
|
|
|
|
2019-05-17 17:48:49 +00:00
|
|
|
GB_log(gb, " LFSR in %u-step mode, current value ",
|
2019-05-15 10:39:08 +00:00
|
|
|
gb->apu.noise_channel.narrow? 7 : 15);
|
2019-05-17 17:48:49 +00:00
|
|
|
for (uint16_t lfsr = gb->apu.noise_channel.lfsr, i = 15; i--; lfsr <<= 1) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, "%u%s", (lfsr >> 14) & 1, i%4 ? "" : " ");
|
|
|
|
}
|
|
|
|
|
2019-05-17 17:48:49 +00:00
|
|
|
if (gb->apu.noise_channel.length_enabled) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, " Channel will end in %u 256 Hz ticks\n",
|
|
|
|
gb->apu.noise_channel.pulse_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GB_log(gb, "\n\nReminder: APU ticks are @ 2 MiHz\n");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool wave(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command)
|
|
|
|
{
|
|
|
|
if (strlen(lstrip(arguments)) || (modifiers && !strchr("fcl", modifiers[0]))) {
|
|
|
|
print_usage(gb, command);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t shift_amount = 1, mask;
|
2019-05-17 17:48:49 +00:00
|
|
|
if (modifiers) {
|
2020-04-24 17:37:57 +00:00
|
|
|
switch (modifiers[0]) {
|
2019-05-15 10:39:08 +00:00
|
|
|
case 'c':
|
|
|
|
shift_amount = 2;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
shift_amount = 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mask = (0xf << (shift_amount - 1)) & 0xf;
|
|
|
|
|
2019-05-17 17:48:49 +00:00
|
|
|
for (int8_t cur_val = 0xf & mask; cur_val >= 0; cur_val -= shift_amount) {
|
|
|
|
for (uint8_t i = 0; i < 32; i++) {
|
|
|
|
if ((gb->apu.wave_channel.wave_form[i] & mask) == cur_val) {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, "%X", gb->apu.wave_channel.wave_form[i]);
|
2019-05-17 17:48:49 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-05-15 10:39:08 +00:00
|
|
|
GB_log(gb, "%c", i%4 == 2 ? '-' : ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GB_log(gb, "\n");
|
|
|
|
}
|
|
|
|
|
2017-04-22 11:55:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool help(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *command);
|
2016-08-13 19:52:41 +00:00
|
|
|
|
2017-02-04 17:00:31 +00:00
|
|
|
#define HELP_NEWLINE "\n "
|
2016-08-13 19:52:41 +00:00
|
|
|
|
|
|
|
/* Commands without implementations are aliases of the previous non-alias commands */
|
2016-04-05 22:43:35 +00:00
|
|
|
static const debugger_command_t commands[] = {
|
|
|
|
{"continue", 1, cont, "Continue running until next stop"},
|
|
|
|
{"next", 1, next, "Run the next instruction, skipping over function calls"},
|
|
|
|
{"step", 1, step, "Run the next instruction, stepping into function calls"},
|
|
|
|
{"finish", 1, finish, "Run until the current function returns"},
|
2020-04-11 15:27:31 +00:00
|
|
|
{"backtrace", 2, backtrace, "Displays the current call stack"},
|
2016-08-13 19:52:41 +00:00
|
|
|
{"bt", 2, }, /* Alias */
|
2016-10-19 20:48:46 +00:00
|
|
|
{"sld", 3, stack_leak_detection, "Like finish, but stops if a stack leak is detected (Experimental)"},
|
2020-04-11 15:27:31 +00:00
|
|
|
{"ticks", 2, ticks, "Displays the number of CPU ticks since the last time 'ticks' was" HELP_NEWLINE
|
|
|
|
"used"},
|
2016-04-05 22:43:35 +00:00
|
|
|
{"registers", 1, registers, "Print values of processor registers and other important registers"},
|
2016-04-08 23:00:10 +00:00
|
|
|
{"cartridge", 2, mbc, "Displays information about the MBC and cartridge"},
|
2016-08-13 19:52:41 +00:00
|
|
|
{"mbc", 3, }, /* Alias */
|
2019-05-15 10:39:08 +00:00
|
|
|
{"apu", 3, apu, "Displays information about the current state of the audio chip"},
|
2019-06-20 22:03:52 +00:00
|
|
|
{"wave", 3, wave, "Prints a visual representation of the wave RAM." HELP_NEWLINE
|
2019-05-15 10:39:08 +00:00
|
|
|
"Modifiers can be used for a (f)ull print (the default)," HELP_NEWLINE
|
2019-06-20 22:03:52 +00:00
|
|
|
"a more (c)ompact one, or a one-(l)iner", "", "(f|c|l)"},
|
2017-04-22 11:55:17 +00:00
|
|
|
{"lcd", 3, lcd, "Displays information about the current state of the LCD controller"},
|
2016-10-01 19:08:34 +00:00
|
|
|
{"palettes", 3, palettes, "Displays the current CGB palettes"},
|
2016-10-19 20:48:46 +00:00
|
|
|
{"breakpoint", 1, breakpoint, "Add a new breakpoint at the specified address/expression" HELP_NEWLINE
|
2019-04-12 17:29:43 +00:00
|
|
|
"Can also modify the condition of existing breakpoints." HELP_NEWLINE
|
2019-04-12 17:30:02 +00:00
|
|
|
"If the j modifier is used, the breakpoint will occur just before" HELP_NEWLINE
|
|
|
|
"jumping to the target.",
|
2019-06-21 11:18:48 +00:00
|
|
|
"<expression>[ if <condition expression>]", "j"},
|
2016-08-13 19:52:41 +00:00
|
|
|
{"delete", 2, delete, "Delete a breakpoint by its address, or all breakpoints", "[<expression>]"},
|
|
|
|
{"watch", 1, watch, "Add a new watchpoint at the specified address/expression." HELP_NEWLINE
|
2016-10-19 20:48:46 +00:00
|
|
|
"Can also modify the condition and type of existing watchpoints." HELP_NEWLINE
|
|
|
|
"Default watchpoint type is write-only.",
|
|
|
|
"<expression>[ if <condition expression>]", "(r|w|rw)"},
|
2016-08-13 19:52:41 +00:00
|
|
|
{"unwatch", 3, unwatch, "Delete a watchpoint by its address, or all watchpoints", "[<expression>]"},
|
2016-07-06 21:29:25 +00:00
|
|
|
{"list", 1, list, "List all set breakpoints and watchpoints"},
|
2016-10-19 20:48:46 +00:00
|
|
|
{"print", 1, print, "Evaluate and print an expression" HELP_NEWLINE
|
|
|
|
"Use modifier to format as an address (a, default) or as a number in" HELP_NEWLINE
|
|
|
|
"decimal (d), hexadecimal (x), octal (o) or binary (b).",
|
|
|
|
"<expression>", "format"},
|
2016-08-13 19:52:41 +00:00
|
|
|
{"eval", 2, }, /* Alias */
|
2016-10-19 20:48:46 +00:00
|
|
|
{"examine", 2, examine, "Examine values at address", "<expression>", "count"},
|
2016-08-13 19:52:41 +00:00
|
|
|
{"x", 1, }, /* Alias */
|
2017-02-04 15:49:22 +00:00
|
|
|
{"disassemble", 1, disassemble, "Disassemble instructions at address", "<expression>", "count"},
|
2016-10-19 20:55:23 +00:00
|
|
|
|
2016-08-09 19:48:53 +00:00
|
|
|
|
2016-08-13 19:52:41 +00:00
|
|
|
{"help", 1, help, "List available commands or show help for the specified command", "[<command>]"},
|
|
|
|
{NULL,}, /* Null terminator */
|
2016-04-05 22:43:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const debugger_command_t *find_command(const char *string)
|
|
|
|
{
|
|
|
|
size_t length = strlen(string);
|
2016-08-13 19:52:41 +00:00
|
|
|
for (const debugger_command_t *command = commands; command->command; command++) {
|
2016-04-05 22:43:35 +00:00
|
|
|
if (command->min_length > length) continue;
|
|
|
|
if (memcmp(command->command, string, length) == 0) { /* Is a substring? */
|
2016-08-13 19:52:41 +00:00
|
|
|
/* Aliases */
|
|
|
|
while (!command->implementation) {
|
|
|
|
command--;
|
|
|
|
}
|
2016-04-05 22:43:35 +00:00
|
|
|
return command;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-08-13 19:52:41 +00:00
|
|
|
static void print_command_shortcut(GB_gameboy_t *gb, const debugger_command_t *command)
|
|
|
|
{
|
|
|
|
GB_attributed_log(gb, GB_LOG_BOLD | GB_LOG_UNDERLINE, "%.*s", command->min_length, command->command);
|
2020-04-24 17:37:57 +00:00
|
|
|
GB_attributed_log(gb, GB_LOG_BOLD, "%s", command->command + command->min_length);
|
2016-08-13 19:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_command_description(GB_gameboy_t *gb, const debugger_command_t *command)
|
|
|
|
{
|
|
|
|
print_command_shortcut(gb, command);
|
|
|
|
GB_log(gb, ": ");
|
2017-02-04 15:49:22 +00:00
|
|
|
GB_log(gb, (const char *)&" %s\n" + strlen(command->command), command->help_string);
|
2016-08-13 19:52:41 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
static bool help(GB_gameboy_t *gb, char *arguments, char *modifiers, const debugger_command_t *ignored)
|
2016-08-13 19:52:41 +00:00
|
|
|
{
|
|
|
|
const debugger_command_t *command = find_command(arguments);
|
|
|
|
if (command) {
|
|
|
|
print_command_description(gb, command);
|
|
|
|
GB_log(gb, "\n");
|
|
|
|
print_usage(gb, command);
|
|
|
|
|
|
|
|
command++;
|
|
|
|
if (command->command && !command->implementation) { /* Command has aliases*/
|
|
|
|
GB_log(gb, "\nAliases: ");
|
|
|
|
do {
|
|
|
|
print_command_shortcut(gb, command);
|
|
|
|
GB_log(gb, " ");
|
|
|
|
command++;
|
|
|
|
} while (command->command && !command->implementation);
|
|
|
|
GB_log(gb, "\n");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-12 21:11:26 +00:00
|
|
|
for (command = commands; command->command; command++) {
|
2016-08-13 19:52:41 +00:00
|
|
|
if (command->help_string) {
|
|
|
|
print_command_description(gb, command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-09 19:48:53 +00:00
|
|
|
void GB_debugger_call_hook(GB_gameboy_t *gb, uint16_t call_addr)
|
2016-04-10 19:24:53 +00:00
|
|
|
{
|
|
|
|
/* Called just after the CPU calls a function/enters an interrupt/etc... */
|
2016-04-10 20:22:54 +00:00
|
|
|
|
|
|
|
if (gb->stack_leak_detection) {
|
|
|
|
if (gb->debug_call_depth >= sizeof(gb->sp_for_call_depth) / sizeof(gb->sp_for_call_depth[0])) {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "Potential stack overflow detected (Functions nest too much). \n");
|
2016-04-10 20:22:54 +00:00
|
|
|
gb->debug_stopped = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gb->sp_for_call_depth[gb->debug_call_depth] = gb->registers[GB_REGISTER_SP];
|
|
|
|
gb->addr_for_call_depth[gb->debug_call_depth] = gb->pc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 19:48:53 +00:00
|
|
|
if (gb->backtrace_size < sizeof(gb->backtrace_sps) / sizeof(gb->backtrace_sps[0])) {
|
|
|
|
|
|
|
|
while (gb->backtrace_size) {
|
|
|
|
if (gb->backtrace_sps[gb->backtrace_size - 1] < gb->registers[GB_REGISTER_SP]) {
|
|
|
|
gb->backtrace_size--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gb->backtrace_sps[gb->backtrace_size] = gb->registers[GB_REGISTER_SP];
|
|
|
|
gb->backtrace_returns[gb->backtrace_size].bank = bank_for_addr(gb, call_addr);
|
|
|
|
gb->backtrace_returns[gb->backtrace_size].addr = call_addr;
|
|
|
|
gb->backtrace_size++;
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:24:53 +00:00
|
|
|
gb->debug_call_depth++;
|
|
|
|
}
|
|
|
|
|
2016-06-18 17:29:11 +00:00
|
|
|
void GB_debugger_ret_hook(GB_gameboy_t *gb)
|
2016-04-10 19:24:53 +00:00
|
|
|
{
|
|
|
|
/* Called just before the CPU runs ret/reti */
|
2016-04-10 20:22:54 +00:00
|
|
|
|
2016-04-10 19:24:53 +00:00
|
|
|
gb->debug_call_depth--;
|
2016-04-10 20:22:54 +00:00
|
|
|
|
|
|
|
if (gb->stack_leak_detection) {
|
|
|
|
if (gb->debug_call_depth < 0) {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_log(gb, "Function finished without a stack leak.\n");
|
2016-04-10 20:22:54 +00:00
|
|
|
gb->debug_stopped = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (gb->registers[GB_REGISTER_SP] != gb->sp_for_call_depth[gb->debug_call_depth]) {
|
2016-07-13 20:00:50 +00:00
|
|
|
GB_log(gb, "Stack leak detected for function %s!\n", value_to_string(gb, gb->addr_for_call_depth[gb->debug_call_depth], true));
|
2016-07-12 20:30:18 +00:00
|
|
|
GB_log(gb, "SP is $%04x, should be $%04x.\n", gb->registers[GB_REGISTER_SP],
|
2016-04-10 20:22:54 +00:00
|
|
|
gb->sp_for_call_depth[gb->debug_call_depth]);
|
|
|
|
gb->debug_stopped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-09 19:48:53 +00:00
|
|
|
|
|
|
|
while (gb->backtrace_size) {
|
|
|
|
if (gb->backtrace_sps[gb->backtrace_size - 1] <= gb->registers[GB_REGISTER_SP]) {
|
|
|
|
gb->backtrace_size--;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-10 19:24:53 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 20:25:16 +00:00
|
|
|
static bool _GB_debugger_test_write_watchpoint(GB_gameboy_t *gb, value_t addr, uint8_t value)
|
2016-07-06 21:29:25 +00:00
|
|
|
{
|
2016-07-09 11:37:11 +00:00
|
|
|
uint16_t index = find_watchpoint(gb, addr);
|
2016-07-14 20:25:16 +00:00
|
|
|
uint32_t key = WP_KEY(addr);
|
|
|
|
|
|
|
|
if (index < gb->n_watchpoints && gb->watchpoints[index].key == key) {
|
2016-07-06 21:29:25 +00:00
|
|
|
if (!(gb->watchpoints[index].flags & GB_WATCHPOINT_W)) {
|
2016-07-14 20:25:16 +00:00
|
|
|
return false;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
if (!gb->watchpoints[index].condition) {
|
|
|
|
gb->debug_stopped = true;
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, "Watchpoint: [%s] = $%02x\n", debugger_value_to_string(gb, addr, true), value);
|
|
|
|
return true;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
bool error;
|
|
|
|
bool condition = debugger_evaluate(gb, gb->watchpoints[index].condition,
|
2019-06-15 20:22:27 +00:00
|
|
|
(unsigned)strlen(gb->watchpoints[index].condition), &error, &addr.value, &value).value;
|
2016-07-06 21:29:25 +00:00
|
|
|
if (error) {
|
|
|
|
/* Should never happen */
|
|
|
|
GB_log(gb, "An internal error has occured\n");
|
2016-07-14 20:25:16 +00:00
|
|
|
return false;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
if (condition) {
|
|
|
|
gb->debug_stopped = true;
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, "Watchpoint: [%s] = $%02x\n", debugger_value_to_string(gb, addr, true), value);
|
|
|
|
return true;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-14 20:25:16 +00:00
|
|
|
return false;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 20:25:16 +00:00
|
|
|
void GB_debugger_test_write_watchpoint(GB_gameboy_t *gb, uint16_t addr, uint8_t value)
|
|
|
|
{
|
|
|
|
if (gb->debug_stopped) return;
|
|
|
|
|
|
|
|
/* Try any-bank breakpoint */
|
|
|
|
value_t full_addr = (VALUE_16(addr));
|
|
|
|
if (_GB_debugger_test_write_watchpoint(gb, full_addr, value)) return;
|
|
|
|
|
|
|
|
/* Try bank-specific breakpoint */
|
|
|
|
full_addr.has_bank = true;
|
|
|
|
full_addr.bank = bank_for_addr(gb, addr);
|
|
|
|
_GB_debugger_test_write_watchpoint(gb, full_addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool _GB_debugger_test_read_watchpoint(GB_gameboy_t *gb, value_t addr)
|
2016-07-06 21:29:25 +00:00
|
|
|
{
|
2016-09-29 22:09:17 +00:00
|
|
|
uint16_t index = find_watchpoint(gb, addr);
|
2016-07-14 20:25:16 +00:00
|
|
|
uint32_t key = WP_KEY(addr);
|
|
|
|
|
|
|
|
if (index < gb->n_watchpoints && gb->watchpoints[index].key == key) {
|
2016-07-06 21:29:25 +00:00
|
|
|
if (!(gb->watchpoints[index].flags & GB_WATCHPOINT_R)) {
|
2016-07-14 20:25:16 +00:00
|
|
|
return false;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
if (!gb->watchpoints[index].condition) {
|
|
|
|
gb->debug_stopped = true;
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, "Watchpoint: [%s]\n", debugger_value_to_string(gb, addr, true));
|
|
|
|
return true;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
bool error;
|
|
|
|
bool condition = debugger_evaluate(gb, gb->watchpoints[index].condition,
|
2019-06-15 20:22:27 +00:00
|
|
|
(unsigned)strlen(gb->watchpoints[index].condition), &error, &addr.value, NULL).value;
|
2016-07-06 21:29:25 +00:00
|
|
|
if (error) {
|
|
|
|
/* Should never happen */
|
|
|
|
GB_log(gb, "An internal error has occured\n");
|
2016-07-14 20:25:16 +00:00
|
|
|
return false;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
if (condition) {
|
|
|
|
gb->debug_stopped = true;
|
2016-07-14 20:25:16 +00:00
|
|
|
GB_log(gb, "Watchpoint: [%s]\n", debugger_value_to_string(gb, addr, true));
|
|
|
|
return true;
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-14 20:25:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GB_debugger_test_read_watchpoint(GB_gameboy_t *gb, uint16_t addr)
|
|
|
|
{
|
|
|
|
if (gb->debug_stopped) return;
|
|
|
|
|
|
|
|
/* Try any-bank breakpoint */
|
|
|
|
value_t full_addr = (VALUE_16(addr));
|
|
|
|
if (_GB_debugger_test_read_watchpoint(gb, full_addr)) return;
|
|
|
|
|
|
|
|
/* Try bank-specific breakpoint */
|
|
|
|
full_addr.has_bank = true;
|
|
|
|
full_addr.bank = bank_for_addr(gb, addr);
|
|
|
|
_GB_debugger_test_read_watchpoint(gb, full_addr);
|
2016-07-06 21:29:25 +00:00
|
|
|
}
|
|
|
|
|
2016-07-17 21:39:43 +00:00
|
|
|
/* Returns true if debugger waits for more commands */
|
2017-05-26 17:16:19 +00:00
|
|
|
bool GB_debugger_execute_command(GB_gameboy_t *gb, char *input)
|
2016-07-17 21:39:43 +00:00
|
|
|
{
|
|
|
|
if (!input[0]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *command_string = input;
|
|
|
|
char *arguments = strchr(input, ' ');
|
|
|
|
if (arguments) {
|
|
|
|
/* Actually "split" the string. */
|
|
|
|
arguments[0] = 0;
|
|
|
|
arguments++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
arguments = "";
|
|
|
|
}
|
|
|
|
|
2016-10-19 20:48:46 +00:00
|
|
|
char *modifiers = strchr(command_string, '/');
|
|
|
|
if (modifiers) {
|
|
|
|
/* Actually "split" the string. */
|
|
|
|
modifiers[0] = 0;
|
|
|
|
modifiers++;
|
|
|
|
}
|
|
|
|
|
2016-07-17 21:39:43 +00:00
|
|
|
const debugger_command_t *command = find_command(command_string);
|
|
|
|
if (command) {
|
2016-10-19 20:48:46 +00:00
|
|
|
return command->implementation(gb, arguments, modifiers, command);
|
2016-07-17 21:39:43 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
GB_log(gb, "%s: no such command.\n", command_string);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
typedef enum {
|
|
|
|
JUMP_TO_NONE,
|
|
|
|
JUMP_TO_BREAK,
|
|
|
|
JUMP_TO_NONTRIVIAL,
|
|
|
|
} jump_to_return_t;
|
|
|
|
|
|
|
|
static jump_to_return_t test_jump_to_breakpoints(GB_gameboy_t *gb, uint16_t *address);
|
|
|
|
|
2016-06-18 17:29:11 +00:00
|
|
|
void GB_debugger_run(GB_gameboy_t *gb)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
2016-09-06 21:37:11 +00:00
|
|
|
if (gb->debug_disable) return;
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
char *input = NULL;
|
2020-03-27 14:49:14 +00:00
|
|
|
if (gb->debug_next_command && gb->debug_call_depth <= 0 && !gb->halted) {
|
2016-03-30 20:07:55 +00:00
|
|
|
gb->debug_stopped = true;
|
|
|
|
}
|
|
|
|
if (gb->debug_fin_command && gb->debug_call_depth == -1) {
|
|
|
|
gb->debug_stopped = true;
|
|
|
|
}
|
|
|
|
if (gb->debug_stopped) {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_cpu_disassemble(gb, gb->pc, 5);
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
next_command:
|
|
|
|
if (input) {
|
|
|
|
free(input);
|
|
|
|
}
|
2019-04-12 17:29:43 +00:00
|
|
|
if (gb->breakpoints && !gb->debug_stopped && should_break(gb, gb->pc, false)) {
|
2016-03-30 20:07:55 +00:00
|
|
|
gb->debug_stopped = true;
|
2016-07-13 20:00:50 +00:00
|
|
|
GB_log(gb, "Breakpoint: PC = %s\n", value_to_string(gb, gb->pc, true));
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_cpu_disassemble(gb, gb->pc, 5);
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
if (gb->breakpoints && !gb->debug_stopped) {
|
|
|
|
uint16_t address = 0;
|
|
|
|
jump_to_return_t jump_to_result = test_jump_to_breakpoints(gb, &address);
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
bool should_delete_state = true;
|
|
|
|
if (gb->nontrivial_jump_state && should_break(gb, gb->pc, true)) {
|
|
|
|
if (gb->non_trivial_jump_breakpoint_occured) {
|
|
|
|
gb->non_trivial_jump_breakpoint_occured = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gb->non_trivial_jump_breakpoint_occured = true;
|
|
|
|
GB_log(gb, "Jumping to breakpoint: PC = %s\n", value_to_string(gb, gb->pc, true));
|
|
|
|
GB_cpu_disassemble(gb, gb->pc, 5);
|
|
|
|
GB_load_state_from_buffer(gb, gb->nontrivial_jump_state, -1);
|
|
|
|
gb->debug_stopped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (jump_to_result == JUMP_TO_BREAK) {
|
|
|
|
gb->debug_stopped = true;
|
|
|
|
GB_log(gb, "Jumping to breakpoint: PC = %s\n", value_to_string(gb, address, true));
|
|
|
|
GB_cpu_disassemble(gb, gb->pc, 5);
|
|
|
|
gb->non_trivial_jump_breakpoint_occured = false;
|
|
|
|
}
|
|
|
|
else if (jump_to_result == JUMP_TO_NONTRIVIAL) {
|
|
|
|
if (!gb->nontrivial_jump_state) {
|
|
|
|
gb->nontrivial_jump_state = malloc(GB_get_save_state_size(gb));
|
|
|
|
}
|
|
|
|
GB_save_state_to_buffer(gb, gb->nontrivial_jump_state);
|
|
|
|
gb->non_trivial_jump_breakpoint_occured = false;
|
|
|
|
should_delete_state = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gb->non_trivial_jump_breakpoint_occured = false;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
if (should_delete_state) {
|
|
|
|
if (gb->nontrivial_jump_state) {
|
|
|
|
free(gb->nontrivial_jump_state);
|
|
|
|
gb->nontrivial_jump_state = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 21:37:11 +00:00
|
|
|
if (gb->debug_stopped && !gb->debug_disable) {
|
2016-03-30 20:07:55 +00:00
|
|
|
gb->debug_next_command = false;
|
|
|
|
gb->debug_fin_command = false;
|
2016-04-10 20:22:54 +00:00
|
|
|
gb->stack_leak_detection = false;
|
2016-03-30 20:07:55 +00:00
|
|
|
input = gb->input_callback(gb);
|
|
|
|
|
2017-04-19 20:26:39 +00:00
|
|
|
if (input == NULL) {
|
|
|
|
/* Debugging is no currently available, continue running */
|
|
|
|
gb->debug_stopped = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-26 17:16:19 +00:00
|
|
|
if (GB_debugger_execute_command(gb, input)) {
|
2016-04-05 22:43:35 +00:00
|
|
|
goto next_command;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
2016-04-05 22:43:35 +00:00
|
|
|
|
2016-07-17 21:39:43 +00:00
|
|
|
free(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GB_debugger_handle_async_commands(GB_gameboy_t *gb)
|
|
|
|
{
|
|
|
|
char *input = NULL;
|
2016-04-05 22:43:35 +00:00
|
|
|
|
2016-07-18 11:30:21 +00:00
|
|
|
while (gb->async_input_callback && (input = gb->async_input_callback(gb))) {
|
2017-05-26 17:16:19 +00:00
|
|
|
GB_debugger_execute_command(gb, input);
|
2016-03-30 20:07:55 +00:00
|
|
|
free(input);
|
|
|
|
}
|
2016-07-13 20:00:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 19:48:48 +00:00
|
|
|
void GB_debugger_add_symbol(GB_gameboy_t *gb, uint16_t bank, uint16_t address, const char *symbol)
|
|
|
|
{
|
|
|
|
bank &= 0x1FF;
|
|
|
|
|
|
|
|
if (!gb->bank_symbols[bank]) {
|
|
|
|
gb->bank_symbols[bank] = GB_map_alloc();
|
|
|
|
}
|
|
|
|
GB_bank_symbol_t *allocated_symbol = GB_map_add_symbol(gb->bank_symbols[bank], address, symbol);
|
|
|
|
if (allocated_symbol) {
|
|
|
|
GB_reversed_map_add_symbol(&gb->reversed_symbol_map, bank, allocated_symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 20:00:50 +00:00
|
|
|
void GB_debugger_load_symbol_file(GB_gameboy_t *gb, const char *path)
|
|
|
|
{
|
|
|
|
FILE *f = fopen(path, "r");
|
|
|
|
if (!f) return;
|
|
|
|
|
|
|
|
char *line = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
size_t length = 0;
|
|
|
|
while ((length = getline(&line, &size, f)) != -1) {
|
|
|
|
for (unsigned i = 0; i < length; i++) {
|
|
|
|
if (line[i] == ';' || line[i] == '\n' || line[i] == '\r') {
|
|
|
|
line[i] = 0;
|
|
|
|
length = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (length == 0) continue;
|
|
|
|
|
2019-06-15 20:22:27 +00:00
|
|
|
unsigned bank, address;
|
2016-07-13 20:00:50 +00:00
|
|
|
char symbol[length];
|
|
|
|
|
2019-07-13 14:17:55 +00:00
|
|
|
if (sscanf(line, "%x:%x %s", &bank, &address, symbol) == 3) {
|
2020-04-25 19:48:48 +00:00
|
|
|
GB_debugger_add_symbol(gb, bank, address, symbol);
|
2016-07-13 20:00:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2018-04-02 16:57:39 +00:00
|
|
|
void GB_debugger_clear_symbols(GB_gameboy_t *gb)
|
|
|
|
{
|
2020-04-09 11:32:52 +00:00
|
|
|
for (unsigned i = sizeof(gb->bank_symbols) / sizeof(gb->bank_symbols[0]); i--;) {
|
2018-04-02 16:57:39 +00:00
|
|
|
if (gb->bank_symbols[i]) {
|
|
|
|
GB_map_free(gb->bank_symbols[i]);
|
|
|
|
gb->bank_symbols[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 11:32:52 +00:00
|
|
|
for (unsigned i = sizeof(gb->reversed_symbol_map.buckets) / sizeof(gb->reversed_symbol_map.buckets[0]); i--;) {
|
2018-04-02 16:57:39 +00:00
|
|
|
while (gb->reversed_symbol_map.buckets[i]) {
|
|
|
|
GB_symbol_t *next = gb->reversed_symbol_map.buckets[i]->next;
|
|
|
|
free(gb->reversed_symbol_map.buckets[i]);
|
|
|
|
gb->reversed_symbol_map.buckets[i] = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 20:00:50 +00:00
|
|
|
const GB_bank_symbol_t *GB_debugger_find_symbol(GB_gameboy_t *gb, uint16_t addr)
|
|
|
|
{
|
2016-07-14 20:25:16 +00:00
|
|
|
uint16_t bank = bank_for_addr(gb, addr);
|
2016-07-14 17:46:00 +00:00
|
|
|
|
2016-07-13 20:00:50 +00:00
|
|
|
const GB_bank_symbol_t *symbol = GB_map_find_symbol(gb->bank_symbols[bank], addr);
|
|
|
|
if (symbol) return symbol;
|
2016-07-14 17:46:00 +00:00
|
|
|
if (bank != 0) return GB_map_find_symbol(gb->bank_symbols[0], addr); /* Maybe the symbol incorrectly uses bank 0? */
|
|
|
|
|
2016-07-13 20:00:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *GB_debugger_name_for_address(GB_gameboy_t *gb, uint16_t addr)
|
|
|
|
{
|
|
|
|
const GB_bank_symbol_t *symbol = GB_debugger_find_symbol(gb, addr);
|
|
|
|
if (symbol && symbol->addr == addr) return symbol->name;
|
|
|
|
return NULL;
|
2016-08-19 11:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The public version of debugger_evaluate */
|
|
|
|
bool GB_debugger_evaluate(GB_gameboy_t *gb, const char *string, uint16_t *result, uint16_t *result_bank)
|
|
|
|
{
|
|
|
|
bool error = false;
|
|
|
|
value_t value = debugger_evaluate(gb, string, strlen(string), &error, NULL, NULL);
|
|
|
|
if (result) {
|
|
|
|
*result = value.value;
|
|
|
|
}
|
|
|
|
if (result_bank) {
|
|
|
|
*result_bank = value.has_bank? value.value : -1;
|
|
|
|
}
|
|
|
|
return error;
|
2017-02-04 17:00:31 +00:00
|
|
|
}
|
2017-04-17 17:16:17 +00:00
|
|
|
|
|
|
|
void GB_debugger_break(GB_gameboy_t *gb)
|
|
|
|
{
|
|
|
|
gb->debug_stopped = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GB_debugger_is_stopped(GB_gameboy_t *gb)
|
|
|
|
{
|
|
|
|
return gb->debug_stopped;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GB_debugger_set_disabled(GB_gameboy_t *gb, bool disabled)
|
|
|
|
{
|
|
|
|
gb->debug_disable = disabled;
|
|
|
|
}
|
2019-04-12 17:29:43 +00:00
|
|
|
|
|
|
|
/* Jump-to breakpoints */
|
|
|
|
|
|
|
|
static bool is_in_trivial_memory(uint16_t addr)
|
|
|
|
{
|
|
|
|
/* ROM */
|
|
|
|
if (addr < 0x8000) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
/* HRAM */
|
|
|
|
if (addr >= 0xFF80 && addr < 0xFFFF) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
/* RAM */
|
|
|
|
if (addr >= 0xC000 && addr < 0xE000) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef uint16_t GB_opcode_address_getter_t(GB_gameboy_t *gb, uint8_t opcode);
|
|
|
|
|
|
|
|
uint16_t trivial_1(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
return gb->pc + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t trivial_2(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
return gb->pc + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t trivial_3(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
return gb->pc + 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t jr_r8(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
return gb->pc + 2 + (int8_t)GB_read_memory(gb, gb->pc + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool condition_code(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
switch ((opcode >> 3) & 0x3) {
|
|
|
|
case 0:
|
|
|
|
return !(gb->registers[GB_REGISTER_AF] & GB_ZERO_FLAG);
|
|
|
|
case 1:
|
|
|
|
return (gb->registers[GB_REGISTER_AF] & GB_ZERO_FLAG);
|
|
|
|
case 2:
|
|
|
|
return !(gb->registers[GB_REGISTER_AF] & GB_CARRY_FLAG);
|
|
|
|
case 3:
|
|
|
|
return (gb->registers[GB_REGISTER_AF] & GB_CARRY_FLAG);
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t jr_cc_r8(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
if (!condition_code(gb, opcode)) {
|
|
|
|
return gb->pc + 2;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
return gb->pc + 2 + (int8_t)GB_read_memory(gb, gb->pc + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t ret(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
return GB_read_memory(gb, gb->registers[GB_REGISTER_SP]) |
|
|
|
|
(GB_read_memory(gb, gb->registers[GB_REGISTER_SP] + 1) << 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint16_t ret_cc(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
if (condition_code(gb, opcode)) {
|
|
|
|
return ret(gb, opcode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return gb->pc + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t jp_a16(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
return GB_read_memory(gb, gb->pc + 1) |
|
|
|
|
(GB_read_memory(gb, gb->pc + 2) << 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t jp_cc_a16(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
if (condition_code(gb, opcode)) {
|
|
|
|
return jp_a16(gb, opcode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return gb->pc + 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t rst(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
return opcode ^ 0xC7;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t jp_hl(GB_gameboy_t *gb, uint8_t opcode)
|
|
|
|
{
|
|
|
|
return gb->hl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GB_opcode_address_getter_t *opcodes[256] = {
|
|
|
|
/* X0 X1 X2 X3 X4 X5 X6 X7 */
|
|
|
|
/* X8 X9 Xa Xb Xc Xd Xe Xf */
|
|
|
|
trivial_1, trivial_3, trivial_1, trivial_1, trivial_1, trivial_1, trivial_2, trivial_1, /* 0X */
|
|
|
|
trivial_3, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_2, trivial_1,
|
|
|
|
trivial_2, trivial_3, trivial_1, trivial_1, trivial_1, trivial_1, trivial_2, trivial_1, /* 1X */
|
|
|
|
jr_r8, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_2, trivial_1,
|
|
|
|
jr_cc_r8, trivial_3, trivial_1, trivial_1, trivial_1, trivial_1, trivial_2, trivial_1, /* 2X */
|
|
|
|
jr_cc_r8, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_2, trivial_1,
|
|
|
|
jr_cc_r8, trivial_3, trivial_1, trivial_1, trivial_1, trivial_1, trivial_2, trivial_1, /* 3X */
|
|
|
|
jr_cc_r8, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_2, trivial_1,
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, /* 4X */
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1,
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, /* 5X */
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1,
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, /* 6X */
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1,
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, NULL, trivial_1, /* 7X */
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1,
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, /* 8X */
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1,
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, /* 9X */
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1,
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, /* aX */
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1,
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, /* bX */
|
|
|
|
trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1, trivial_1,
|
|
|
|
ret_cc, trivial_1, jp_cc_a16, jp_a16, jp_cc_a16, trivial_1, trivial_2, rst, /* cX */
|
|
|
|
ret_cc, ret, jp_cc_a16, trivial_2, jp_cc_a16, jp_a16, trivial_2, rst,
|
|
|
|
ret_cc, trivial_1, jp_cc_a16, NULL, jp_cc_a16, trivial_1, trivial_2, rst, /* dX */
|
|
|
|
ret_cc, ret, jp_cc_a16, NULL, jp_cc_a16, NULL, trivial_2, rst,
|
|
|
|
trivial_2, trivial_1, trivial_1, NULL, NULL, trivial_1, trivial_2, rst, /* eX */
|
|
|
|
trivial_2, jp_hl, trivial_3, NULL, NULL, NULL, trivial_2, rst,
|
|
|
|
trivial_2, trivial_1, trivial_1, trivial_1, NULL, trivial_1, trivial_2, rst, /* fX */
|
|
|
|
trivial_2, trivial_1, trivial_3, trivial_1, NULL, NULL, trivial_2, rst,
|
|
|
|
};
|
|
|
|
|
|
|
|
static jump_to_return_t test_jump_to_breakpoints(GB_gameboy_t *gb, uint16_t *address)
|
|
|
|
{
|
|
|
|
if (!gb->has_jump_to_breakpoints) return JUMP_TO_NONE;
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
if (!is_in_trivial_memory(gb->pc) || !is_in_trivial_memory(gb->pc + 2) ||
|
|
|
|
!is_in_trivial_memory(gb->registers[GB_REGISTER_SP]) || !is_in_trivial_memory(gb->registers[GB_REGISTER_SP] + 1)) {
|
|
|
|
return JUMP_TO_NONTRIVIAL;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
/* Interrupts */
|
|
|
|
if (gb->ime) {
|
|
|
|
for (unsigned i = 0; i < 5; i++) {
|
|
|
|
if ((gb->interrupt_enable & (1 << i)) && (gb->io_registers[GB_IO_IF] & (1 << i))) {
|
|
|
|
if (should_break(gb, 0x40 + i * 8, true)) {
|
|
|
|
if (address) {
|
|
|
|
*address = 0x40 + i * 8;
|
|
|
|
}
|
|
|
|
return JUMP_TO_BREAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
uint16_t n_watchpoints = gb->n_watchpoints;
|
|
|
|
gb->n_watchpoints = 0;
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
uint8_t opcode = GB_read_memory(gb, gb->pc);
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
if (opcode == 0x76) {
|
|
|
|
gb->n_watchpoints = n_watchpoints;
|
|
|
|
if (gb->ime) { /* Already handled in above */
|
|
|
|
return JUMP_TO_NONE;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
if (gb->interrupt_enable & gb->io_registers[GB_IO_IF] & 0x1F) {
|
|
|
|
return JUMP_TO_NONTRIVIAL; /* HALT bug could occur */
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
return JUMP_TO_NONE;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
GB_opcode_address_getter_t *getter = opcodes[opcode];
|
|
|
|
if (!getter) {
|
|
|
|
gb->n_watchpoints = n_watchpoints;
|
|
|
|
return JUMP_TO_NONE;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
uint16_t new_pc = getter(gb, opcode);
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
gb->n_watchpoints = n_watchpoints;
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
if (address) {
|
|
|
|
*address = new_pc;
|
|
|
|
}
|
2019-05-15 10:39:08 +00:00
|
|
|
|
2019-04-12 17:29:43 +00:00
|
|
|
return should_break(gb, new_pc, true) ? JUMP_TO_BREAK : JUMP_TO_NONE;
|
|
|
|
}
|