Is there a way to "dump the lowest value of SP" used by your program?

By Bengalack

Master (192)

Bengalack의 아바타

13-09-2020, 08:31

Does a script/functionality exist in openMSX that can monitor the SP-register at a given begin-to-end sequence? The output should be the lowest value recorded.

The use case is this: I'd like to find out how much stack-space my start-up code uses (it's written in C, BTW).

Login or 등록 to post comments

By Manuel

Ascended (16977)

Manuel의 아바타

13-09-2020, 09:51

That would require some script coding.

By Bengalack

Master (192)

Bengalack의 아바타

13-09-2020, 11:18

Ok, thanks. Well I'm not in desperate need. One can always manage without. ...Until my tcl-skills are vastly improved. And that will take a while, ha ha Big smile

--But it sure would come in handy when memory-requirements gets tight and/or when you want to rule out potential root-causes when bug hunting Smile

By NYYRIKKI

Enlighted (5595)

NYYRIKKI의 아바타

14-09-2020, 02:53

Hmm... you should setup some break points and try something like:

set losp [reg sp]
debug set_condition {[reg sp] < $::losp} {set ::losp [reg sp]}

By wouter_

Champion (432)

wouter_의 아바타

14-09-2020, 08:53

My first idea was the same as Nyyrikki's. It's a pretty simple script, after each executed Z80 instruction it checks if there's a new lower SP value. But because it executes some Tcl code after every instruction it will slowdown emulation speed considerably. For a debugging tool that's probably acceptable.

A slightly more complex solution could be to not trigger on every instruction but only on writes to the stack region. You'll have to make an estimate of this region (let's say 0xe000-0xf300). The 2nd line in Nyyrikki's script then becomes

debug set_watchpoint write_mem {0xe000 0xf2ff} {[reg sp] < $::losp} {set ::losp [reg sp]}

Another alternative (which doesn't use any emulator specific features) is:

  • At the start of your code initialize the stack region with a known value.
  • At the end of your code check up to what point those values have been overwritten.

By pgimeno

Master (238)

pgimeno의 아바타

14-09-2020, 10:58

Note that at interrupt may or may not come at maximum stack usage. It's possible that this does not happen during testing, but happens later. For this reason you should account for the stack usage caused by the interrupt handler, on top of the minimum SP you find.

And that's assuming that you don't have recursive calls that depend on external factors (user input, file contents etc.); for those, the total stack usage can't be determined in advance. Another factor that comes to mind: calling ROM routines may produce different stack usage on different machines.

So, don't set the stack size too tight!

By Bengalack

Master (192)

Bengalack의 아바타

21-09-2020, 08:20

Thanks! Definitely some good ideas here that can do the trick!

And this one:

wouter_ wrote:

Another alternative (which doesn't use any emulator specific features) is:

  • At the start of your code initialize the stack region with a known value.
  • At the end of your code check up to what point those values have been overwritten.

seems like a super easy thing to do. Strange I didn't think of it myself Smile