C-SPY macros to save and restore memory and registers

Technical Note 24166



6/20/2012 1:55 PM


If you need to save memory and/or registers in your target to make debugging more efficient this tech note is for you. The example provided is for Freescale 6812, but with appropriate adaptations it should do the trick for other chips as well.

This example macro file shows how to save registers and a range of memory cells to a simple hex file. Here the file is named "c:\test.hex," but feel free to use whatever you like - Just remember to use the same file name saving and restoring. Also, for convenience, you should specify this file in "Project -> Options -> C-SPY -> Setup file" such that you get this loaded each time you start C-SPY. Then you can call the macros from the "Control -> Quick Watch" dialog - just give the name of the macro, for example memory_save() and push return or click "Recalculate." You probably do not want to "Add Watch," since that would mean executing the macro each time the watch is updated.

To give you some more detail, the file first defines some variables (you have access to these in the quick watch dialog too). The most interesting variables would be the mem_begin and mem_end - these are used to specify the range of memory that is to be saved and restored. Extending this to multiple ranges should be straighthforward.

Then we have the memory save function:

  • Open a file ("C:\test.hex") for writing
  • Save register values to the file, showing the values in the report window
  • Save mem_begin and mem_end to the file.
  • Save memory contents from mem_begin to mem_end.

There are some lines commented out, starting with

// Optional register:

This is to allow you to save/restore the PP register only when needed (e.g., banking used). The memory read function is basically the inverse, it is really very straightforward.

The execUserSetup() finally, is where the values for mem_begin and mem_end are defined. If there is a macro by this name, it is automatically called once after the target program is downloaded. Please see C-SPY documentation for more info in other similar macros.

Additional macros

There is an additional exmple with macros where you can specify the name of the file, i.e. no need to update the macro source.

Example of a command to enter in the QuickWatch command line to dump the contents of a variable in a binary format:

DumpMem("dumpmem.bin", &trace_buffer, sizeof(trace_buffer))

Example of a command to enter in the QuickWatch command line to dump the contents of ARM registers in a readable format:


NOTE DumpReg

If size of int is too small for address(es) you are interested in, try casting to (long) instead of to (int).


All product names are trademarks or registered trademarks of their respective owners.

Related Tech Notes

© IAR Systems 1995-2016 - All rights reserved.