Introduction
 
This demo will focus on using a simple debugging tool called the D-Bug12 Monitor. D-Bug12 is a software program that is burned into the Flash memory of the HCS12 chip and by default runs when the chip is first powered on. Using a serial communication program such as Hyperterminal, you can interact with DBug12 to use it for basic debugging. For instance, you can view the contents of registers and memory locations, you can generate simple assembly programs, and you can single-step through a program. During this demo, we will also start getting a feel for the registers and memory model of the HCS12. Note that the instructions below (and for the rest of the course) are for Windows 2000 or Windows XP.
 
 
Dragon12 Board
 
During the course you will be using a "Dragon-12" development board from Wytec. Development boards are hardware development platforms commonly used in the preliminary stages of design. The Dragon-12 development board consists of an HCS12 chip connected to a variety of peripheral components such as RS232 serial port, header pins, LCD, LED, etc. These hardware components facilitate communication with the board, testing of real-world input and output, etc. Dragon-12 information, including a photo and schematics, can be found on the Dragon-12 Resources page.
 
 
Starting D-Bug12 on the Dragon12
 
Connections: To use the Dragon12 board, first connect the RS232 port on the Dragon12 (the port nearest the LCD display) to a COMM1 or COMM2 serial port on the PC at your station using a 9-pin RS232 cable. Then, connect the power plug (9VDC/500 mA) to the power jack on the lower left side of the Dragon12. On powering up, the eight inline LEDs on the left-center of the board should chase from right to left, and the "EVB" LED in the lower right part of the board should be lit.
 
Starting Hyperterminal: Once the Dragon12 has powered on, we need to communicate with it using a serial communications program, such as Hyperterminal for Windows. To start a "Hyperterminal" session in windows, go to the Start menu and click into the "Programs -> Accessories -> Communication" submenu. If you have never used Hyperterminal, you will be presented with an initial setup screen where you must enter the area code (915). You will be prompted for a name (make it anything you want) and an icon (choose any you want).
 
Hyperterminal Setup: Once into Hyperterminal, use the following settings: 9600 bps, 8 data bits, parity=none, stop bits=1, xon/xoff flow control. Set it up to use either the COMM1 or COMM2 serial port, depending on which is connected to the Dragon12 board (usually we use COMM1 which is the topmost port). Note that if you are in a Hyperterminal session which is active or connected to something, you must first disconnect by clicking on the "disconnect" telephone icon. Then edit the configuration under File > Properties > Configure. While still in Hyperterminal, you restart the session by clicking on the "connect" telephone icon and then hitting the "reset" button on the board as described above. You can set the length of the scrollable history by going to File > Properties > Settings > Backscroll buffer lines = 500. This can be handy if you need to scroll back to find exactly what commands you entered, etc.
 
After you have powered on the board and successfully started Hyperterminal, hit the "reset" button on the lower-center part of the Dragon12 card. The D-Bug12 prompt should show up as below:
 
 
Screenshot of a D-Bug12 Hyperterminal Session
 
 
If your screen is blank or gibberish, try hitting the reset button on the HCS12 board. If you still can't get Hyperterminal to set up right, make sure the right COMM port is hooked up and that the Hyperterminal settings are as noted above.
 
If you have problems with a Rev.E board spontaneously resetting when you run a program, double check that you are using the proper 9 VDC/500 mA power plug. The 7.5 VDC/300 mA plugs supplied with older Dragon12 boards are underpowered and can cause a brown-out of the Rev. E board under a slight load (voltage drops below minimum supply voltage), which results in a spontaneous reset.
 
Main D-Bug12 Commands
 
The main D-Bug12 commands we will be using are listed below. They are described in full detail in the DBug12 manual. In Motorola syntax all hex numbers begin with a '$' dollar sign. In the listings below, we use the '$' sign to help remind us that we are working in hex. However, since DBug12 always assumes the numbers you enter are hex, you don't have to use the leading dollar sign if you don't want to.
 
  1.  help - lists a brief description of the DBug12 commands
  2.  rd - "register display" will display the contents of registers
  3.  md $start $end - "memory display" displays the contents of memory from the $start address to the $end address
  4.  mm $address - "memory modify" will modify the contents of a memory location at $address
  5.  asm $address  - "assembler" command is used as an interactive memory editor starting at the specified $address. This can be used to write simple assembly programs. It can also be used to list the assembly commands of a file.
  6.   g $address - "go" command will execute a program beginning at specified $address. The program will run until it terminates or you hit the abort program button on the board
  7.  t - "trace" command is used to execute one step at a time; you can use "t 3" to execute three instructions, etc.
  8.  gt $stop_address - "go till" command is used to execute the program from the current PC address to the specified $stop_address. This can be handy if you want to run to a specified point and then stop. You need to set the start address of the program using the PC counter (see below).
  9.  pc $start address - "program counter" is used to set the starting address of the next instruction executed (we use it in conjunction with the trace command to start a program at the first instruction of that program).
  10.   device - "device" is used to display essential information about the HCS12 hardware.
 
 
Procedure
 
Start a valid Hyperterminal/D-Bug12 session as noted above.
 
   1. Try the "register display" command by typing "rd" at the D-Bug12 prompt. Note the contents of the PC, SP, X, Y, and D registers. These are all 16 bit registers and their contents should be displayed/recorded as four consecutive hex characters (each hex character represents four bits).
 
   2. Type "md $1000 $1020" at the prompt and note the hex representation and the ASCII representation on the right hand side. There are 12K Bytes of RAM starting at address $1000 in the HCS12, and what you are seeing is the content of memory cells at the beginning of RAM. The contents of RAM change as the program runs, and are usually random on first power-up. In industrial embedded applications, RAM is usually used for variables and the stack, and program code is usually put into Flash memory as noted below. However, in this course we use RAM to hold program code itself since our programs are small and need to be loaded in quickly and easily.
 
   3. Type "md $e320 $e340" at the prompt. This displays part of Flash memory. Look at the ASCII characters on the right side of the display. The ASCII representation of the numbers 0-9 is $30 - $39, and the ASCII representation for the lower case letters 'a' - 'z' is $61 - $7A. So this part of Flash memory is a symbol table where we predefine ASCII characters for the HCS12. A complete representation of ASCII hex is shown at the internet site asciitable.com.
 
      There are 256 KBytes of Flash memory on the hcs12 chip we use, and the flash memory is accessed in 16 KByte pages via a technique called "paging". The three pages (48 KBytes) of Flash which are easily visible on the Dragon12 are located in the address range between $4000 and $FFF0; we looked in part of this region using the above "md $e320 $e340" command. The memory in flash is non-volatile and, whether the power is on or off, remains until we erase it. In industrial applications, flash memory usually contains the program code, and may also contain well predefined symbols which are used by running programs. Flash is block erasable (ie, you can't just change a single byte in flash memory), whereas EEPROM (see below) is byte-erasable.
 
   4. Now we are going to write some hex values to RAM using the memory modify (mm) command. At the prompt type the six lines shown below:
 
      >mm $1000 68
      >mm $1001 65
      >mm $1002 6c
      >mm $1003 6c
      >mm $1004 6f
      >mm $1005 20
 
      where each single line is entered at the prompt (hit return after each line). Even though we are writing addresses in hex ($1000, $1001, etc.) we are entering hex values (68, 65 etc) WITHOUT the leading '$' dollar sign. DBug12 assumes everything is in hex, so we can write our numbers whichever way we want. If you make a mistake, simply retype the line with the correct parameters.
 
   5. Type ">md $1000 $1020" again, and on the right part of the display look at the ASCII representation of what you just wrote (it should say 'hello'). Note that $20 is ASCII for a space. You could determine the ASCII code of the other alphabetic characters from the discussion above.
 
   6. Now we are going to write some hex values to EEPROM using the "mm" command. EEPROM is non-volatile memory like Flash, but unlike Flash it is byte-erasable (in Flash memory, you must erase entire blocks before they can be re-programmed with new data). EEPROM is used for special, rarely changed values such as configuration information, serial numbers, version numbers, lookup tables, etc. EEPROM for our current board configuration is located in the address range from $0400 to $0fff. See the Memory Map for the HCS12 page for more information on the memory model of the 68hcs12 chip. To write a message to EEPROM, type the six lines shown below:
 
      >mm $0400 68
      >mm $0401 65
      >mm $0402 6c
      >mm $0403 6c
      >mm $0404 6f
      >mm $0405 20
 
      As before, we are entering hex values (68, 65 etc) WITHOUT the leading '$' dollar sign, whereas for clarity we use the leading dollar sign for addresses.
 
   7. Type in "md $0400" and see that you get the same message as we did in the RAM exercise.
 
  8. With Hyperterminal still running, unplug the power to the Dragon12 board and wait for about 30 seconds. Note that Hyperterminal is "dead" when the board power is removed. Now, plug the power jack back into the Dragon12 board and hit the "reset" button. You should get a fresh D-Bug12 session start prompt. Note that the hex contents of registers PC, SP, X, Y, and D should be same as the first time you checked at the start of this demo. The reason: on reset, the CPU registers are set to their default state, so each time you start the microcontroller it will be in a consistent, known state.
 
  9. Look at the contents of memory to see what changed when the electrical power was removed. To check RAM, enter "md $1000" at the prompt. Note the hex values at locations $1000 - $1005. Note that the RAM values changed since RAM is volatile memory and the contents are lost when the power is turned off. To check EEPROM, enter "md $0400" at the prompt and note the hex values at locations $0400 - $0405. Note that the EEPROM values are the same since it is non-volatile memory (meaning that the contents of memory remain even after the power has been removed).
 
  10. Using D-Bug12 "asm" command, we will now enter a simple assembly program which starts at address $2000:
 
                 >asm $2000
      ...........>ldaa #$02
      ...........>ldab #$01
      ...........>aba
      ...........>swi
      ...........>.
 
      In the entries above, the series of dots "....." denote what D-Bug12 initially responds with after you enter each line. That is, after you hit the "enter" key D-Bug12 responds with the current value at an address and then gives you a prompt for you to enter the new value. You hit "enter" at the end of each of your input lines. You terminate your input with the period "." , and once you terminate D-Bug12 will respond with the normal prompt.
 
      In the program above, we are first loading the hex value $02 into accumulator A using the "LDAA" command. Note that the hash sign "#" means immediate addressing, which is a form of addressing required when we use a constant value (addressing will be discussed more fully in the lectures). The next command loads the hex value $01 into accumulator B using the "LDAB" command. The following command "ABA" adds the contents of accumulator A ($02) to the contents of accumulator B ($01) and stores the result ($03) back in accumulator A. The final command "SWI" stands for "software interrupt" and is used to terminate a program.
  11. Now before we run the program lets look at what's in the registers using the register display "rd" command:
 
      >rd
 
      you should see the contents of the various registers. Notice in particular the value of the program counter PC - this is the address of the current instruction that will be executed. For now the number in PC is random, but shortly it will use our program addresses. Also note the contents of accumulators A and B. These are displayed as "D= A:B" since register D is a 16 bit register formed by concatenating the 8 bit registers A and B. We can treat A and B as separate entities for now. Note also that all register values are displayed in hex.
  12. The program we created above using the "asm" command is a complete program sitting in RAM at memory addresses $2000 - $2006. Lets run the program using the go "g" command and the starting address of $2000:
 
      >g $2000
 
      D-Bug12 should respond with "User Bkpt Encountered" and then show the contents of the registers. Note in particular that PC=2007 since the program stopped at the SWI instruction at address $2006 (the PC points to the address of the next instruction which will run). Also note that accumulator A contains the value $03,the result of adding $02 to $01 and overwriting the initial value of $02 that was in A.
  13. We can get a little more information by single-stepping through the program using the trace "t" command. Each time we enter "t", the current instruction is executed and the program halted until another trace is entered. The current instruction is determined by the contents of the program counter PC register, which contains the address of the instruction to run. We say that the PC "points" to the current instruction. When this instruction is executed, the PC is then automatically updated to point to the next instruction. So, when we run the next "t" command, it will execute this new instruction, the PC is auto-incremented again, etc.
 
      With this background, we are ready to begin. First, hit the "reset" button on the Dragon12 board. This clears the registers to their reset values but does not affect RAM. Then, set PC to point to the first line of our program:
 
      >pc $2000
 
      D-Bug12 should respond with a display of the register contents, with PC=2000. The trace command will execute the instruction located at PC = 2000, which is the first instruction of our program.
 
  14. Now we can actually enter a "t" command at the prompt to run our first instruction:
 
      >t
 
      We should get a display which again shows the contents of all the registers. Note that the program counter was automatically updated when we ran the trace command and now has the value PC=2002. This is the address of the current instruction which will be run the next time we enter a "t" command. Also note that register A = 02, which resulted from the "LDAA #$02" instruction, and that register B contains the initialized value of 0 because we haven't yet reached the instruction that puts a new value in B. As a reminder, the register contents are hex values and don't show with a $ prefix in D-Bug12.
 
      Now run another "t" command, and note that PC = 2004, A=02 and B=01. We have now stored the value $01 in B. The program counter PC=2004 is the current instruction and will be run when we next enter a "t" command. Run another "t" command, and we will see that PC=2006, A=03 and B=01. The next "t" command executes the SWI instruction, and we can stop here.
 
      Below are screen shots of what you would see if you executed the above steps correctly in sequence:
 
 
 
 
 
 
Just for Fun
 
If you want to try some very simple hardware exercises with DBug12 on the Dragon12, try the following commands to light the LEDs on Port B:
 
    * >mm 026A C3 (this will set port J DDR to output, needed for Rev. E board)
    * >mm 0268 00 (this will set Port J bits low to enable Port B LEDs on Rev. E boards)
    * >mm 03 FF (this will set the Port B Data Direction Register at address $0003 to be all outputs)
    * >mm 01 FF (this should light all LEDs on Port B, which is at address $0001)
    * >mm 01 01 (this should light only LED0 of Port B)
    * >mm 01 55 (this should light every other LED of Port B)
    * >mm 01 00 (this should turn off all LEDs on Port B)
    * try some other LED patterns; writing a "1" to a Port B bit lights an LED, and writing a 0 to a Port B bit turns the LED off.
 
Further Information
 
Further information about loading and running programs using DBug12 can be found in the DBug12 Program Demo page. You don't have to do this just yet, but have a look if you want. The DBug12 manual manual also contains helpful information.
 
Though we don't use Linux in this class, if you are curious you can have a look at the "Using DBug12 With Linux" link for the basics of how to use Kermit and Linux for HCS12 development. Kermit is a powerful serial communications program (more capable and reliable than Hyperterminal), and Linux is a free, open-source operating system which will run on nearly any PC. By using Kermit and Linux combined with open-source GNU development tools, you can create a powerful HCS12 development system which doesn't cost as much as a windows/code warrior based system.
 
 
 
D-bug12 Tutorial