The GNU Debugger contains both a source level debugger and a 68HC11/68HC12 simulator. The debugger allows source level debugging (C, C++, Java, ADA, and many others) as well as assembly-level debugging. The simulator handles all 68HC11 instructions and supports some 68HC11 devices (timer, serial device, eeprom, spi).

1. Debug With GDB

This is a quick example of some GDB commands. This example is based on a small calculator program.

Start gdb as follows:

  m6811-elf-gdb calc.elf
Select the simulator as the GDB target:
 (gdb) target sim
 Connected to the simulator.
Load the program in simulator memory:
 (gdb) load calc.elf
Loading section .page0, size 0x20 lma 0x0
Loading section .text, size 0xeba lma 0x8000
Loading section .eh_frame, size 0x1fc lma 0x8eba
Loading section .rodata, size 0x295 lma 0x90b6
Loading section .vectors, size 0x40 lma 0xffc0
Start address 0x8e8f
Transfer rate: 40280 bits in <1 sec.
Install some breakpoints:
  (gdb) b _start
Breakpoint 1 at 0x8e8f
  (gdb) b *0x8e95
Breakpoint 2 at 0x8e95
  (gdb) b main
Breakpoint 3 at 0x89dc: file calc.c, line 525.
  (gdb) b calc.c:318
Breakpoint 4 at 0x8552: file calc.c, line 318.
Run the program. The simulator loads the PC from the vector's table, as for a 68HC11 reset.
  (gdb) run
Breakpoint 1, 0x8e8f in _start ()
Disassemble with the x command:
  (gdb) x/4i $pc
(gdb) x/4i $pc
0x8e8f <_start>:        lds     #0x7fff
0x8e92 <_start+3>:      jsr     0x8a67 <__premain>
0x8e95 <__map_data_sections>:   ldx     #0x934b
0x8e98 <__map_data_sections+3>: ldy     #0x1100 <values>
Resume execution to stop at the next breakpoint:
  (gdb) c
Continuing.

Breakpoint 2, 0x8e95 in __map_data_sections ()
  (gdb) c
Continuing.

Breakpoint 3, main () at calc.c:525
525       _serial_init ();
Resume execution again. The calculator will print the help description and will fall in another breakpoint.
  (gdb) c
Continuing.
Simple Calculator Test Program
add     Pop two values and push their sum
sub     Pop two values and push their subtraction
mul     Pop two values and push their mul
div     Divide the two values
and     Logical and between the two values
or      Logical or between the two values
quit    Quit the calculator
list    List the stack
help    This help
dec     Switch in decimal mode
hex     Switch in hexadecimal mode

Breakpoint 4, print_value (stack=0x20, mode=PRINT_DEC, which=-1) at calc.c:316
316       if (which < 0)
Look where we are in the program. The list command displays several lines before and after the current source line (line 316).
  (gdb) list
311         " %o\r\n",
312         " %b\r\n"
313       };
314
315       /* Print the top of the stack if no index is specified. */
316       if (which < 0)
317         {
318           which = stack->top - 1;
319           if (which < 0)
320             {
Print the content of the stack parameter. This is a pointer to a structure, you can print it as follows:
  (gdb) print *stack
$3 = {values = 0x7fd3, top = 0, max = 10, mode = PRINT_DEC}
You can setup the pretty-print mode to get some nice output:
  (gdb) set print pretty on
  (gdb) print *stack
$5 = {
  values = 0x7fd3, 
  top = 0, 
  max = 10, 
  mode = PRINT_DEC
}
If you resume execution, you can push a number on the stack by typing it on the keyboard. The program will then stop again in print_value and you can have a look at the stack parameter again. Looking at values of the stack is straightforward:
\  (gdb) print stack->values[0]
$6 = 23
Print the info about the simulator and the 68HC11 registers:
  (gdb) sim info-cpu
M68HC11:
  HPRIO     @ 0x103c 0x00 
  CONFIG    @ 0x103f 0xff NOSEC NOCOP ROMON EEON  
  OPTION    @ 0x1039 0x00 
  INIT      @ 0x103d 0x01 Ram = 0x0000 IO = 0x1000
CPU info:
  Absolute cycle: 800638
  Syscall emulation: yes, via 0xcd <n>
  Memory errors detection: no
  Stop on interrupt: no
Interrupts Info:
  Interrupts raised: 0
  Min interrupts masked sequence: 13 cycles
  Max interrupts masked sequence: 13 cycles
Print the info about other devices:
  (gdb) sim info-timer
  ...
  (gdb) sim info-sio
  ...
  (gdb) sim info-eeprom
  ...

2. Simulator

The simulator is part of gdb. It can also be run separately with the m6811-elf-run program.

   m6811-elf-run calc.elf

You can specify the value of the 68HC11 CONFIG register with the -cpu-config=value option. For example, to disable the EEPROM, pass -cpu-config=0xe.

You can pass the -v option to the simulator. After completion of your program (by calling exit or hitting ^C), the simulator will print execution statistics.

3. Buffalo Monitor

Gdb supports the Buffalo monitor as a target. The communication is made using the serial line. Gdb sends buffalo monitor commands and analyses the result of those commands. To use the monitor type the following gdb commands:

   (gdb) set remotebaud 9600
   (gdb) target buffalo device
where device is the serial device your target is connected to. In general, this is /dev/ttya, /dev/ttyb for Solaris and /dev/ttyS0, /dev/ttyS1 for Linux.

Once connected, you can load a program using the gdb load command:

   (gdb) load file

And execute that program on the target:

   (gdb) run