Emulator Debugging Output (printf-style)

The Uzebox now have a fully functional emulator! Download and discuss it here.
Post Reply
braddock
Posts: 26
Joined: Fri Jul 31, 2009 12:09 pm

Emulator Debugging Output (printf-style)

Post by braddock »

I've created a patch for the Uzem emulator that allows the program running within the simulation to print strings and data to stdout. It is a cheap trick, but over the past few days I've found it indispensable!

I am also very interested to hear from you all of other debugging techniques people are using inside or outside of various emulators.

For example, the author of FatFS has a nice little packages called "suart" for using the hardware ISP channel as a serial port for debugging: http://elm-chan.org

Here is my emulator patch to apply to avr8.cpp within avr8::exec():

Code: Select all

*** avr8.cpp.orig	2009-08-06 17:23:03.000000000 -0700
--- avr8.cpp	2009-08-06 17:31:03.000000000 -0700
***************
*** 1317,1322 ****
--- 1317,1335 ----
  		break;
  	}
  
+   /* Check for "magic" debug output */
+   {
+     const int magicAddr = 0xffe;
+     static int gotMagic=0;
+     unsigned char v = sram[magicAddr - SRAMBASE];
+     if (gotMagic && v != (unsigned char) '\xba') {
+       printf("%c", v);
+       gotMagic = 0;
+     } else if (v == (unsigned char) '\xba') {
+       gotMagic = 1;
+     }
+   }
+ 
  #if DISASM
  	// Don't spew disassembly during interrupts
  	if (singleStep && !interruptLevel)
Very simply, the code checks if the user writes the "magic" byte 0xba to the "magic" SRAM address 0xffe. If so, then the next byte the user writes to 0xffe will be printed to stdout. Obviously if your program is making use of 0xffe, it needs to save and then restore the value.

This hack is harmless if run on real hardware, and the emulator is uneffected if running a program which actually uses 0xffe (except for the slight chance that a spurious byte might be written to the stdout occassionally).

I then created a little set of inline wrapper print routines for printing strings, ints, hex, etc which are easily added to the project.

Code: Select all

#ifndef EMUDEBUG_H_
#define EMUDEBUG_H_ 1

#ifndef USE_EMU_DBG
#  define USE_EMU_DBG 1
#endif
#ifndef DBG_ADDR
#  define DBG_ADDR 0xffe 
#endif
#define DBG_MAGIC '\xba'
#define DBG_PTR ((char volatile *) DBG_ADDR)

#if !USE_EMU_DBG
inline void DBGc(char value) {};
inline void DBGs(const char *s) {};
inline void DBGx(unsigned char byte) {};
inline void DBGi(int val) {};
#else

inline void DBGc(char value) {
  *DBG_PTR = DBG_MAGIC;
  *DBG_PTR = value;
}

inline void DBGs(const char *s) {
  for (; *s != 0x0; s++)
    DBGc(*s);
}

inline void DBGx(unsigned char byte) {
	unsigned char nibble;
	nibble=(byte>>4);
  DBGc(nibble + (nibble < 10 ? '0' : '7'));
	nibble=(byte&0xf);
  DBGc(nibble + (nibble < 10 ? '0' : '7'));
}

inline void DBGi(int val) {
  unsigned char c;
  int v2,p,vlen;
  if (val < 0) {
    DBGc('-');
    val = -val;
  } else if (val == 0) {
    DBGc('0');
    return;
  }
  /* Determine number of digits */
  v2 = val;
  p = 1;
  for (vlen=0; v2 != 0; v2 = v2 / 10)
    p *= 10;

  do {
    p = p/10;
    c = (val / p) % 10;
    DBGc(c + '0');
    val = val - c*p;
  } while (p != 1);
}
#endif // USE_EMU_DBG

#endif //EMUDEBUG_H_
User avatar
Jhysaun
Posts: 214
Joined: Tue Nov 04, 2008 12:32 am

Re: Emulator Debugging Output (printf-style)

Post by Jhysaun »

When I debug, I use Print on my screen Section with text. I then run it in the emulator.

Pretty Basic, but it gets the job done. :D

>J
Lerc wrote:I intend to use my powerful skills of procrastination to ensure that when I get to making things, the chips will be available.
DavidEtherton
Posts: 252
Joined: Tue Dec 02, 2008 12:38 am
Location: Carlsbad, California (USA)

Re: Emulator Debugging Output (printf-style)

Post by DavidEtherton »

Nice one! I've done temporary hacks to print out access to particular ports for numeric debug spew, never made anything as nice as this.

-Dave
User avatar
paul
Posts: 457
Joined: Sat May 02, 2009 8:41 am
Location: Brisbane, Australia

Re: Emulator Debugging Output (printf-style)

Post by paul »

Thanks, braddock - now I can stop including fonts_8x8.pic.inc just to do simple development debugging.
Post Reply