From 66dacd1816b2fe1086a4e614df37d587b2b7ae2d Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Mon, 2 Dec 2019 17:44:54 -0500 Subject: [PATCH] tools/emul: add "Min SP" debug value This gives the maximum size of the stack at any given moment during the execution of the program. It's useful to figure out if the stack gets dangerously close to the heap. --- tools/emul/bshell/shell.c | 1 + tools/emul/emul.c | 10 ++++++++++ tools/emul/emul.h | 11 +++++++---- tools/emul/shell/shell.c | 1 + 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/tools/emul/bshell/shell.c b/tools/emul/bshell/shell.c index 31df626..2560414 100644 --- a/tools/emul/bshell/shell.c +++ b/tools/emul/bshell/shell.c @@ -186,5 +186,6 @@ int main() termInfo.c_lflag |= ECHO; termInfo.c_lflag |= ICANON; tcsetattr(0, TCSAFLUSH, &termInfo); + emul_printdebug(); return 0; } diff --git a/tools/emul/emul.c b/tools/emul/emul.c index d060123..e39dd07 100644 --- a/tools/emul/emul.c +++ b/tools/emul/emul.c @@ -48,6 +48,7 @@ Machine* emul_init() { memset(m.mem, 0, 0x10000); m.ramstart = 0; + m.minsp = 0xffff; for (int i=0; i<0x100; i++) { m.iord[i] = NULL; m.iowr[i] = NULL; @@ -65,6 +66,10 @@ bool emul_step() { if (!m.cpu.halted) { Z80Execute(&m.cpu); + ushort newsp = m.cpu.R1.wr.SP; + if (newsp != 0 && newsp < m.minsp) { + m.minsp = newsp; + } return true; } else { return false; @@ -75,3 +80,8 @@ void emul_loop() { while (emul_step()); } + +void emul_printdebug() +{ + fprintf(stderr, "Min SP: %04x\n", m.minsp); +} diff --git a/tools/emul/emul.h b/tools/emul/emul.h index 61d2b8f..8b47400 100644 --- a/tools/emul/emul.h +++ b/tools/emul/emul.h @@ -2,15 +2,17 @@ #include #include "libz80/z80.h" -typedef uint8_t (*IORD) (); -typedef void (*IOWR) (uint8_t data); +typedef byte (*IORD) (); +typedef void (*IOWR) (byte data); typedef struct { Z80Context cpu; - uint8_t mem[0x10000]; + byte mem[0x10000]; // Set to non-zero to specify where ROM ends. Any memory write attempt // below ramstart will trigger a warning. - uint16_t ramstart; + ushort ramstart; + // The minimum value reached by SP at any point during execution. + ushort minsp; // Array of 0x100 function pointers to IO read and write routines. Leave to // NULL when IO port is unhandled. IORD iord[0x100]; @@ -20,3 +22,4 @@ typedef struct { Machine* emul_init(); bool emul_step(); void emul_loop(); +void emul_printdebug(); diff --git a/tools/emul/shell/shell.c b/tools/emul/shell/shell.c index cb74f2a..215dace 100644 --- a/tools/emul/shell/shell.c +++ b/tools/emul/shell/shell.c @@ -186,5 +186,6 @@ int main() termInfo.c_lflag |= ECHO; termInfo.c_lflag |= ICANON; tcsetattr(0, TCSAFLUSH, &termInfo); + emul_printdebug(); return 0; }