Getting Started with Development Tools
Development Tools Home
Getting Started Development Tools Home
Step 1 An Overview of Embedded Systems
Step 2 Start Now with Microchip Development Tools
Step 3 Implementing an Embedded System Design with MPLAB® IDE
Step 4 The Development Cycle
Step 5 MPLAB® Project Manager
Step 6 Language Tools
Step 7 Target Debugging
Step 8 Programming
Step 9 MPLAB® IDE: For More Information
Getting Started with MPLAB Editor
Getting Started with MPASM/MPLINK
Getting Started with MPLAB SIM
Getting Started with MPLAB ICD
Getting Started with MPLAB C18 1
Getting Started with MPLAB C18 2
Getting Started with MPLAB C18 3
Introduction to Microchip’s Development Tools
Introduction to MPLAB® IDE
Tips and Tricks Using MPLAB® IDE v6.61
Introduction to MPLAB® SIM Software Simulator
Basic dsPIC Development Tools
Introduction to the Signal Analysis PICtail daughterboard
Choosing a Debug Tool
MPLAB® IDE User's Guide
MPLAB® IDE Quick Chart
MPLAB® Download Page
Online Discussion Groups
Development Tools Selector Guide
Available Books
Third Party Tools
  Featured Products
 

Pick your MCU
 
 
     

START NOW – THE SIMULATOR

Getting Started with MPLAB® SIM

MPLAB SIM is one of the debug engines that can be used with MPLAB. The other debug engines are hardware devices, while MPLAB SIM is a software program that runs on your PC. MPLAB SIM provides many of the same features as in-circuit emulators and in-circuit debuggers. The difference is that both in-circuit emulators and in-circuit debuggers allow the code to be run on actual silicon, and also allow a target application hardware to be functional while being debugged.

MPLAB SIM has features to simulate hardware interaction with other signals and devices, and since it is running as software on the PC, it has complete information about the internal state of the simulated chip at each instruction. This is a little different from the hardware debuggers because, while they are running code at full speed, they typically cannot monitor all registers and all memory in real time.

Both MPLAB SIM and the hardware debuggers can do the traditional functions of debuggers, but due to their differences, they can have unique features of their own. This presentation will identify the functions and features of MPLAB SIM.

The debugger is a part of MPLAB IDE, and whether you are using MPLAB SIM, MPLAB ICE or MPLAB ICD 2, most operations are exactly the same. This allows you to develop code using the simulator, then when your hardware is ready, you can use a hardware debugger to further test your code in practice without having to learn how to use a new tool.

These are the basic debug functions:

    Reset the target, in order to restart the application

    Execute the code so the program can be tested to verify it functions as designed

    Halt the code at breakpoints

    While halted at breakpoints, examine and modify memory and variables to analyze and debug the application code

    Single Step through code to closely inspect how it executes. This allows the engineer to go through code one instruction (or one C statement) at a time while monitoring affected variables, registers and flags. Single stepping essentially “zooms in” on code to ensure that it operates correctly in complex and critical sections with ranges of variable values and under various test conditions.

Most debuggers also have additional features to help analyze and debug the application. Some of these are listed here:

    Watch points group and monitor selected variables and memory locations into a convenient, custom readout.

    Trace buffers capture the streams of instructions executed and reveal the contents of changing register values.

    A Stopwatch can time a section of code. Routines can be optimized, and critical code timing can be accurately measured and adjusted.

    Complex breakpoints offer a method for establishing breakpoints or for gathering data in the trace buffer based upon multiple conditions. Simple breakpoints allow setting breakpoints in the source code or anywhere in program memory. Complex breakpoints allow getting a breakpoint on a condition such as

       – after the main routine called “RefreshDisplay” executes then
       –
    wait for subroutine “ReadTemp” to execute. Then
       –
    break if the variable named “Temperature” is greater than 20.

Complex events can be constructed to count events, so that a subroutine would have to be executed, for example, 15 times before it starts looking for a value on a pin or in a register. This kind of breakpoint allows you to describe the condition where your code misbehaves, to halt at a breakpoint or to trace code when that condition occurs. This is usually a faster way of finding bugs than simply setting simple breakpoints and stepping through your code.

MPLAB SIM is a simulator, and as a result it has certain characteristics that make it a unique debug engine. In modern Windows based PCs, many things are happening in the background --other programs may be running, hardware may be communicating to the PC, and so on. So the speed of the simulation is determined by

    how fast your PC executes,

    the complexity of the current simulation, and

    the number of other tasks executing on your PC.

Currently the maximum speed of MPLAB SIM is on the order of 10 MIPS, or 10 million instructions per second. This will be affected by how many other things are being done by your PC, by the code the simulator is running, and by the other tasks that the simulator is performing. The simulator simulates the operation of

    the core CPU and it’s internal registers,

    memory, and

    many of it peripherals.

In order to test the application on the simulator, stimulus signals can be applied to pins and registers.

To evaluate performance, the simulator can log changing registers to files for further analysis.

Starting Up MPLAB SIM

This is the MPLAB IDE desktop when it is first started up. There is a standard menu across the top, a tool bar below this, a blank “Workspace window” and a status bar on the bottom.

MPLAB SIM is selected as the debug engine from the Debugger menu. Note the other functions on the debug menu, such as Run, Step, and Breakpoints.

Once a debug engine is selected, the toolbar is appended with some new icons for running, halting, single stepping, and resetting the target.

The Status bar now shows some additional information.

    MPLAB SIM shows as the current debug engine.

    The simulated processor is listed, in this case the PIC18F452,

    then the program counter,

    the W register,

    the current state of the internal CPU flags and

    the current selected file register bank.

The toolbar icons, the menus, or the hot keys listed on the menus can be used to execute the debug functions. Note that some are a little more complex, such as Reset, which actually has four types of reset actions.

Once MPLAB SIM is established as the debug engine, whenever a project is built, it is automatically loaded into the simulator’s program memory to be run and tested.

One debug window is the source code window. This is actually the editor window, and breakpoints can be set by clicking on a line with the right mouse button. Single stepping with the source code window in focus will single step through the C source lines. Since you are in the editor, changes can be done quickly, and the project can be rebuilt.

The Program Memory window shows the machine code that will be executed by the simulator. Single stepping with this window in focus will allow you to step through each machine instruction

Another window, called the Disassembly Listing window shows high level source code interspersed with machine code generated by each C statement.

You can also open up a watch window and drag the variables from your program to them to see the contents as you break and single-step through your code. While debugging, other windows are available to view

    register memory,

    stack memory, and

    non-volatile data memory areas.

Simulator Stopwatch

The Stopwatch dialog can time a section of code as it run in the simulator. The Stopwatch calculations are based upon the instructions executed and the setting entered for the Processor Frequency. The processor frequency is set to 20 Mhz in this example. From the number of instruction cycles executed, the total time is calculated. This is the time it would take to run on a real chip.

The stopwatch has two pairs of readouts, one displays the total simulated clock cycles and the corresponding execution time, and the other can be zeroed out, to make a measurement from one breakpoint to the next. The stopwatch is one way to measure time in the simulator, but there is another:

Simulator Trace Analyzer

The trace buffer records instructions when they execute and puts a time stamp on each instruction. After events are captured in the trace buffer, the time stamp can be used to measure time between instructions. The trace buffer has the advantage that it can capture large amounts of data selectively and each instruction has a time stamp. You can capture just the interrupt routine, for instance, and then easily calculate the time between interrupts and the total time each interrupt took to execute.

Simulator I/O

MPLAB SIM simulates the CPU core with the various microcontroller program, file and data memory areas.

Simulation includes pin inputs and outputs as well as many of the other peripherals.

The peripherals communicate to the application through special function registers.

A complex Stimulus Generator simulates signals that can be applied to the device under simulation.

The stimulus generator can send signals to pins or to registers in the simulator.

The activity of the simulator can be sent to a Log file for later analysis. This is done by using either the USART as a communication device for inputs and outputs, or by using the register log feature. Both stimulus and logging activity can be driven either by execution at a specified program counter address or by sequencing “on demand.”

“On demand” means that whenever that register is read by an instruction, a value is read or written to a list, then the next position in the list is queued up to be used for the next read or write operation.

Simulator Stimulus

There are three types of Stimulus sources for MPLAB SIM:

    Manual triggers are changes in digital signal levels caused by clicking on a button with a mouse. These allow you to simulate the action of closing a switch, or pulsing a pin.

    A Cyclic stimulus generates a repeating waveform, either a for a pre-determined length of time, or continuously.

    Sequential data is data that can be applied to pins, registers, or bits in registers from a list.

A list for sequential data can be entered in a dialog or it can come from a file.

Stimulus Controller

A list for sequential data can be entered in a dialog or it can come from a file. Here is the Stimulus Controller for MPLAB SIM. In this dialog, you can select actions to apply to a pin and then, when your program is running, you can press the associated “Fire” button to the left of the pin name to activate that signal. For instance, a pulse can be set up to occur on PORTA pin RA3. Here, when the Fire button is pressed, the simulated signal on RA3 will pulse high for 8 microseconds.

More actions can be added. Here two more are shown,

    one to force the INT0 pin low when we press the fire button, and

    one to toggle the T0CKI pin for the Timer/Counter.

A “Toggle” action will alternate between setting the pin high and low each time the Fire button is pressed.

SCL Workbook

For more complex stimulus control, actions are entered in the SCL Workbook.

SCL stands for Stimulus Control Language, but with the graphical user interface of MPLAB, all stimulus events can be set up with these easy graphical dialogs. Once the events are described in the SCL Workbook, they are compiled into an SCL file that can then be loaded into the Stimulus Controller.

SCL Workbook: Pin/Register Actions Tab

There are five tabs in this workbook.

The first tab in the SCL Workbook uses a list of times and signals to be applied to pins and registers.

For instance, a series of events can be set up on pin RA1.

    15 microseconds after the program is reset and started,

    the pin RA1 will go high.

    Then at 35 microseconds after the start of the program pin RA1 will go low.

Here sequences of events are applied to

    pin RA1,

    PORTB,

    the Timer interrupt flag, and

    to the A/D buffer.

The list of values under the column labeled PORTB are 8-bit values that will be applied to all eight pins of PORTB at the times entered down the left column. The next column is a list for a signal connected to the Timer0 interrupt flag, an internal register. The two columns on the right allow the A/D buffer to be set to a sequence of 10-bit values.

SCL Workbook: Advanced Pin/Register Tab

The second tab in the SCL Workbook, the Advanced Pin/Register tab, provides conditional control over events. Here a condition must occur before the stimulus is activated. The condition is described in the bottom section. The condition is gating the event to happen 2 microseconds after pin CCP1 goes low. When that occurs, a value of 55 will be applied to PORTB.

Other events can be described on subsequent lines. The event can be a “1x” event, which means that it will occur only once, or it can be a Continuous event, meaning that will happen every time the associated condition occurs. Here a second event happens each time TMR2 reaches a value of 84. When this happens, PORTB will change to a value of 10, and the INT1 pin will go high. There is a 10 microsecond re-arm delay. This prevents this event from happening sooner that 10 microseconds after its last firing.

SCL Workbook: Clock Stimulus Tab

The Clock Stimulus tab of the SCL Workbook generates repeating digital waveforms.

Four waveforms are entered here.

In the first line has the definition for a signal called “jStrobe.” This line defines a clock stimulus signal that holds the RA5 pin low for 10 instruction cycles, then goes high for 15 instruction cycles. It starts immediately, and never stops. A Comments column and the Label column are user-defined labels for this dialog, and are free-form, allowing any text entry. The label is arbitrary, simply for tagging the lines in this dialog with a descriptive name.

Line two describes a waveform that will be applied to RA6.

    RA6 will start out low,

    stay low for 100 cycles,

    then go high for 150 cycles.

    It will not start until the program counter reaches the routine called “test.”

    It will begin cycling and

    will stop when the program counter reaches a routine labeled “done.”

Line three describes a signal applied to RC0.

    RC0 will start high,

    go low for 2 cycles,

    then go high for 50 cycles

    while the INT1 pin is Low.

    It will continue cycling, going low for 2 cycles,

    then high for 50 cycles

    until the INT1 pin goes high.

Line four generates a signal on

    RC3 that starts low,

    stays low for 3 cycles,

    then goes high for 30 cycles.

    It starts this operation only after 1500 cycles have elapsed since the program was reset, and

    will stop 1800 cycles after the program started, being active for only those 300 cycles.

SCL Workbook: Register Injection Tab

The Register injection tab allows values from a file to be injected into a register. In this case values from the file named waves.txt are the source for data going into ADRESL whenever it is read.

SCL Workbook: Register Trace Tab

The last tab in the SCL workbook allows values to be output to a file, just the opposite of the Register Injection tab. In this example, every time PORTB changes values, it will be written into the file portwave.txt

Using these last two tabs allows fairly complex simulation. For instance the simulation of two waveforms being injected on pins, then being added and sent to an output port could be set up. In this case, the Register Injection would receive waveform values from the two inputs, and the Register Trace function would log the output to a file.

Compiled SCL Workbook Attached to Stimulus Controller

When the SCL workbook is complete, pressing the “Generate SCL From Workbook” button will generate a compiled SCL file that can then be attached to the Stimulus Controller, and tested with the simulator.

Summary

MPLAB SIM is a powerful tool for testing code on your PC without any other tools. Algorithms can be tested using the debug features to validate that maximum/minimum variable values are handled correctly. The Stopwatch and Trace Analyzer allow measurement of code sections for optimization and analysis. And the stimulus features can be used to simulate real-world signals. All these tools are free with MPLAB IDE and provide a powerful framework to building embedded applications using Microchip PIC and dsPIC microcontrollers.

Start Now With MPASM and MPLINK

Start Now With MPLAB ICD 2