From: Toni Wilen Date: Sat, 18 Apr 2020 09:44:44 +0000 (+0300) Subject: Easy to use debug logger. X-Git-Tag: 4400~75 X-Git-Url: https://git.unchartedbackwaters.co.uk/w/?a=commitdiff_plain;h=5a3bed90f8edcef97920bfad01375fe08cb7286c;p=francis%2Fwinuae.git Easy to use debug logger. --- diff --git a/cia.cpp b/cia.cpp index 2d8262f8..704a0ea3 100644 --- a/cia.cpp +++ b/cia.cpp @@ -2242,10 +2242,24 @@ static uae_u32 REGPARAM2 cia_lgeti (uaecptr addr) return cia_lget (addr); } + +static bool cia_debug(uaecptr addr, uae_u32 value, int size) +{ + if (addr == DEBUG_SPRINTF_ADDRESS || addr == DEBUG_SPRINTF_ADDRESS + 4 || + (addr == DEBUG_SPRINTF_ADDRESS + 2 && currprefs.cpu_model < 68020) || + (addr == DEBUG_SPRINTF_ADDRESS + 6 && currprefs.cpu_model < 68020)) { + return debug_sprintf(addr, value, size); + } + return false; +} + static void REGPARAM2 cia_bput (uaecptr addr, uae_u32 value) { int r = (addr & 0xf00) >> 8; + if (cia_debug(addr, value, sz_byte)) + return; + if (isgarynocia(addr)) { dummy_put(addr, 1, false); return; @@ -2284,6 +2298,9 @@ static void REGPARAM2 cia_wput (uaecptr addr, uae_u32 v) { int r = (addr & 0xf00) >> 8; + if (cia_debug(addr, v, sz_word)) + return; + if (isgarynocia(addr)) { dummy_put(addr, 2, false); return; @@ -2323,6 +2340,8 @@ static void REGPARAM2 cia_wput (uaecptr addr, uae_u32 v) static void REGPARAM2 cia_lput (uaecptr addr, uae_u32 value) { + if (cia_debug(addr, value, sz_long)) + return; cia_wput (addr, value >> 16); cia_wput (addr + 2, value & 0xffff); } diff --git a/debug.cpp b/debug.cpp index eb144431..7246695a 100644 --- a/debug.cpp +++ b/debug.cpp @@ -7217,3 +7217,146 @@ bool debug_trainer_event(int evt, int state) } return false; } + +#define DEBUGSPRINTF_SIZE 32 +static int debugsprintf_cnt; +struct dsprintfstack +{ + uae_u32 val; + int size; +}; +static dsprintfstack debugsprintf_stack[DEBUGSPRINTF_SIZE]; +static uae_u16 debugsprintf_latch, debugsprintf_latched; + +static void read_bstring(char *out, int max, uae_u32 addr) +{ + out[0] = 0; + if (!valid_address(addr, 1)) + return; + uae_u8 l = get_byte(addr); + if (l > max) + l = max; + addr++; + for (int i = 0; i < l && i < max; i++) { + uae_u8 c = 0; + if (valid_address(addr, 1)) { + c = get_byte(addr); + } + if (c == 0) { + c = '.'; + } + addr++; + out[i] = c; + out[i + 1] = 0; + } +} + +static void read_string(char *out, int max, uae_u32 addr) +{ + out[0] = 0; + for (int i = 0; i < max; i++) { + uae_u8 c = 0; + if (valid_address(addr, 1)) { + c = get_byte(addr); + } + addr++; + out[i] = c; + out[i + 1] = 0; + if (!c) + break; + } +} + +static void debug_sprintf_do(uae_u32 s) +{ + int cnt = 0; + char format[MAX_DPATH]; + char out[MAX_DPATH]; + read_string(format, MAX_DPATH - 1, s); + char *p = format; + char *d = out; + struct dsprintfstack *stack = debugsprintf_stack; + *d = 0; + for (;;) { + char c = *p++; + char cn = *p; + if (c == 0) + break; + if (c == '%' && cn == '%') { + *d++ = '%'; + p++; + } else if (c == '%') { + if (stack >= &debugsprintf_stack[DEBUGSPRINTF_SIZE]) { + *d++ = '['; + *d++ = '?'; + *d++ = ']'; + } else { + if (cn == 'b') { + char tmp[MAX_DPATH]; + read_bstring(tmp, MAX_DPATH - 1, stack->val); + strcpy(d, tmp); + } else if (cn == 's') { + char tmp[MAX_DPATH]; + read_string(tmp, MAX_DPATH - 1, stack->val); + strcpy(d, tmp); + } else if (cn == 'p') { + sprintf(d, "%08x", stack->val); + } else if (cn == 'x') { + sprintf(d, stack->size == sz_long ? "%08x" : (stack->size == sz_word ? "%04x" : "%02x"), stack->val); + } else if (cn == 'd') { + sprintf(d, "%d", stack->val); + } else if (cn == 'u') { + sprintf(d, "%u", stack->val); + } else { + d[0] = '?'; + d[1] = 0; + } + p++; + d += strlen(d); + stack++; + } + } else { + *d++ = c; + } + *d = 0; + } + write_log("%s", out); +} + +bool debug_sprintf(uaecptr addr, uae_u32 val, int size) +{ + if (currprefs.uaeboard < 2) + return false; + + uae_u32 v = val; + if (size == sz_word && currprefs.cpu_model < 68020) { + v &= 0xffff; + if (!(addr & 2)) { + debugsprintf_latch = v; + debugsprintf_latched = 1; + } else if (debugsprintf_latched) { + v |= debugsprintf_latch << 16; + size = sz_long; + if (!(addr & 4) && debugsprintf_cnt > 0) { + debugsprintf_cnt--; + } + } + } + if (size != sz_word) { + debugsprintf_latched = 0; + } + if (addr & 4) { + if (size != sz_long) + return true; + debug_sprintf_do(v); + debugsprintf_cnt = 0; + debugsprintf_latched = 0; + } else { + if (debugsprintf_cnt < DEBUGSPRINTF_SIZE) { + debugsprintf_stack[debugsprintf_cnt].val = v; + debugsprintf_stack[debugsprintf_cnt].size = size; + debugsprintf_cnt++; + } + } + return true; +} diff --git a/include/debug.h b/include/debug.h index 7b0cb0bf..36c80089 100644 --- a/include/debug.h +++ b/include/debug.h @@ -61,6 +61,8 @@ extern int debug_safe_addr(uaecptr addr, int size); extern void debug_invalid_reg(int reg, int size, uae_u16 val); extern void debug_check_reg(uae_u32 addr, int write, uae_u16 v); extern int memwatch_access_validator; +#define DEBUG_SPRINTF_ADDRESS 0xbfff00 +extern bool debug_sprintf(uaecptr, uae_u32, int); extern void debug_init_trainer(const TCHAR*); extern void debug_trainer_match(void);