02.08.2013 Views

Introduction To CodeWarrior™ – Simulating The - Freescale ...

Introduction To CodeWarrior™ – Simulating The - Freescale ...

Introduction To CodeWarrior™ – Simulating The - Freescale ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Laboratory Short Course<br />

<strong>Introduction</strong> to CodeWarrior <strong>–</strong><br />

<strong>Simulating</strong> the Microcontroller in<br />

Assembly Language<br />

www.freescale.com/universityprograms<br />

<strong>Freescale</strong> and the <strong>Freescale</strong> logo are trademarks of <strong>Freescale</strong> Semiconductor, Inc.<br />

All other product or service names are the property of their respective owners<br />

© <strong>Freescale</strong> Semiconductor, Inc. 2006.<br />

Document Number: LABS12CINTRO03S /REV 1


Reading this Document<br />

Answers provided to the Instructor assume that the reader is using <strong>Freescale</strong> HCS12C Family Student Learning Kit,<br />

and CodeWarrior development software.<br />

This short course has been created using an adapted version of the Process Oriented Guided Inquiry Learning (POGIL)<br />

method. For more information visit www.pogil.org<br />

<strong>Freescale</strong> Semiconductor LABS12CINTRO03S, Rev 1 1


Overview<br />

A laboratory experience is vital for us to be able to start programming the microcontroller and to be able to really understand how it<br />

works and to apply it in embedded system designs. This module shows the student how to enter an assembly language program in<br />

the CodeWarrior® integrated development environment and then to simulate its execution using the True-Time simulator/debugger.<br />

Learning Objectives<br />

In this module we introduce you to the CodeWarrior® programming environment. We will see how to start the system and enter a<br />

program to be run with the debugger. In the next module we will give you a program and you will start to learn to use the HCS12<br />

instruction set by modifying if to change how it operates.<br />

Success Criteria<br />

At the end of this module you will be able to assemble a program and to run and debug it with the CodeWarrior simulator/debugger.<br />

Prerequisites<br />

You must know about the HCS12 instruction set and the memory addressing modes available. You do not have to know the<br />

instruction set in detail. <strong>The</strong> following lab modules will help you learn that.<br />

More Resources and Further Information<br />

Cady, Fredrick M., Software and Hardware Engineering: Assembly and C Programming for the <strong>Freescale</strong> HCS12<br />

Microcontroller, 2 nd edition. (New York: Oxford University Press, Inc., 2008). Chapter 5 An Assembler Program, Chapter 6 <strong>The</strong><br />

Linker, Chapter 7 <strong>The</strong> HCS12 Instruction Set.<br />

Smart Linker, Metrowerks Corporation, Austin, TX, 2003.<br />

Debug (DBG) Module V1 Block User Guide, S12DBGV1/D, <strong>Freescale</strong> Semiconductor, 2003.<br />

MC9S12C128 Data Sheet: Covers MC9S12C Family and MC9S12GC Family, <strong>Freescale</strong> Semiconductor, Austin, Texas, 2005.<br />

Montañez, E. and Ruggles, S., MCUSLK_CSM12C32 - Getting Started with the Microcontroller Student Learning Kit<br />

(MCUSLK), <strong>Freescale</strong> Semiconductor, Austin, Texas, 2005.<br />

Sibigtroth, J. M., CPU12RM/AD rev. 3 <strong>–</strong> CPU12 Reference Manual, <strong>Freescale</strong> Semiconductor, Austin, Texas, 2002.<br />

Williams, J., AN2548SW1 <strong>–</strong> CodeWarrior Project Software files for AN2548/D, <strong>Freescale</strong> Semiconductor, Austin, Texas, 2003.<br />

Debugger HCS12 Onchip DBG Module User Interface, Metrowerks, 2003.<br />

Using the CodeWarrior® Simulator<br />

1. Launch the CodeWarrior IDE.<br />

a. From the Windows desktop, click Start > Programs > <strong>Freescale</strong> CodeWarrior > CW for HC12 Vx.x > CodeWarrior IDE.<br />

2. Create New Project.<br />

a. From the IDE main menu bar, select File > New. New window appears.<br />

b. Highlight HC(S)12 New Project Wizard.<br />

c. Enter a Project Name that makes sense to you (like lab_x).<br />

d. Enter a Location for the project. This should be on a drive or in a directory where you want to keep your HCS12 projects.<br />

e. Click OK.<br />

3. In the New Project Wizard<br />

Page 2: Select MC9S12C32 (or your derivative) as the derivative you want to use and click Next.<br />

<strong>Freescale</strong> Semiconductor LABS12CINTRO03S, Rev 1 2


Page 3: Check the language support you want (Assembly) and click Next.<br />

Page 4: Check Relocatable Assembly.<br />

Page 5: Check Full Chip Simulation and P&E Multilink/Cyclone Pro and click Finish.<br />

In the Project Manager Window<br />

4. Switch to the Full Chip Simulation for debugging by clicking the pull down arrow and selecting Full Chip Simulation. (This<br />

does not use the hardware for debugging. If you want to actually use the HCS12 hardware, choose P&E Multilink Cyclone<br />

Pro.)<br />

5. Create listing files by opening the Simulator Settings panel by clicking the icon shown above.<br />

a. Click on + next to Target in the Target Settings.<br />

b. Highlight Assembler for HC12 and in that panel click on Options.<br />

(i) Check Generate a listing file.<br />

(ii) Check Object File Format. Choose ELF/DWARF 2.0 Object File Format.<br />

(iii) Click OK.<br />

c. Click OK.<br />

6. Open the Sources folder (click on the + if it is not open).<br />

7. Double click on main.asm and enter the following code after the comment<br />

; Your program code goes here.<br />

; Your program code goes here<br />

; Initialize I/O<br />

main_loop:<br />

; DO<br />

nop ; A "no operation" operation<br />

ldx #ConstData ; Initialize X with an address<br />

ldab #$02 ; Initialize a counter with data<br />

; Load and store data in a loop<br />

loop:<br />

ldaa 0,x ; Get the data<br />

staa VarData ; Store it in RAM<br />

ldaa 2,x ; Get another byte<br />

staa VarData+1 ; Store it<br />

inx ; Increment the pointer<br />

decb ; Decrement the counter<br />

bne loop ; Loop until B = 0<br />

nop<br />

; Forever<br />

bra main_loop<br />

<strong>Freescale</strong> Semiconductor LABS12CINTRO03S, Rev 1 3


;**************************************************************<br />

MyConst:SECTION<br />

; Place constant data here<br />

ConstData: DC.B $0a,$0b,$11,10<br />

;**************************************************************<br />

MyData: SECTION<br />

; Place variable data here<br />

VarData: DS.B 2 ; Two bytes of storage<br />

8. Save it to whatever name you want: File > Save As. (Make sure you give it an .asm filename, or you can just use the main.asm<br />

file if you want.)<br />

9. Check to make sure debugging information is going to be generated.<br />

a. In the Project Manager Files window, check to see that there is a • in the rightmost column under the bug icon.<br />

10. Assemble the file to check for errors:<br />

a. Click on Project > Compile.<br />

b. Correct any errors.<br />

11. Have a look at your list file.<br />

a. File > Open and position to the bin folder in your project and open the .lst file.<br />

Explore 1.<br />

1. How many bytes are in the program and constant data?<br />

2. You cannot tell from this listing in what memory locations the VarData variable bytes will be located. Why not?<br />

3. What op code (in hexadecimal) is generated for a nop instruction?<br />

4. Do any of the labels in the program generate any code bytes?<br />

Stimulate 1.<br />

1. Why does the instruction ldx ConstData assemble to CExx xx?<br />

2. What are the four bytes (in hexadecimal) assembled as constant data bytes by the assembler directive DC.B?<br />

3. In the ConstData why does DC.B produce different data bytes for $10 and 10?<br />

In future lab exercises you may wish to add other source files to your project. Here is how you do that.<br />

12. Add other source files to the relocatable files in the project:<br />

<strong>Freescale</strong> Semiconductor LABS12CINTRO03S, Rev 1 4


a. Click on the Add New Text File icon:<br />

b. Edit the file.<br />

c. File > Save As (use .asm as the extension for assembler files).<br />

d. Add it to the project: Project > Add *.asm to the project. In Add Files check both Full Chip Simulation and P&E Multilink<br />

Cyclone Pro.<br />

e. Assemble/Compile it: Project > Compile.<br />

13. View the linker parameter file.<br />

a. Click the + next to Prm and double click the Full_Chip_Simulation_linker.prm file to open it in the editor.<br />

Explore 2.<br />

<strong>The</strong> linker parameter file allows us to locate the various parts of the program in the proper sort of memory. For example, all code<br />

and constant data must be in ROM and variable data and the stack in RAM.<br />

1. Look at the SEGMENTS portion.<br />

a. What memory locations are used for RAM?<br />

b. Where memory locations are used for ROM?<br />

2. Look at the PLACEMENT portion. A variety of default placement options are included. We are interested in where the<br />

DEFAULT_ROM (code and constant data SECTIONS in our assembly program) and the DEFAULT_RAM (variable data<br />

SECTION) will be located.<br />

a. In what memory locations will the code and constants be located?<br />

b. In what memory locations will the variable data be located?<br />

Stimulate 2.<br />

1. What change would you make to the linker parameter file if the target hardware had RAM in memory locations $1000 - $1FFF<br />

(0x1000 <strong>–</strong> 0x1FFF)?<br />

2. What change would you make to the linker parameter file if you wanted to locate the code and constants in memory starting at<br />

$4000 (0x4000)?<br />

14. Make the project.<br />

a. Project > Make or click on the icon<br />

b. Correct any linking errors.<br />

15. Inspect the Linker Map file.<br />

a. Click the + on Linker Map and open Full_Chip_Simulation.map.<br />

Explore 3.<br />

STARTUP SECTION<br />

<strong>Freescale</strong> Semiconductor LABS12CINTRO03S, Rev 1 5


1. What is the starting address of the program?<br />

SECTION-ALLOCATION SECTION<br />

2. What number base is used to tell you the size of the various sections?<br />

3. How many bytes of ROM are used by this program?<br />

4. How many bytes of RAM are used by this program?<br />

OBJECT-ALLOCATION SECTION<br />

5. What is the memory address of the label main_loop?<br />

6. What is the memory address of the label loop?<br />

7. In what memory locations would you find MyData?<br />

16. Run the program without the PBMCUSLK using the CodeWarrior True-Time Simulator.<br />

a. Make sure Full_Chip_Simulation is shown in the Project Manager pull down window as shown in step 4 above.<br />

b. Launch the True-Time Simulator by Project > Debug, F5, or clicking on the icon<br />

If a loader warning appears click OK.<br />

c. If Simulator is not the debug target, click on Component > Set Target and chose Simulator Target Interface and click<br />

OK.<br />

d. If your program has not been loaded, or you want to reload it, click Simulator > Load and load bin\simulator.abs.<br />

e. You can use all the features of the simulator on the code.<br />

Explore 4.<br />

After the simulator has been launched, have a look at the display:<br />

1. <strong>The</strong> Source window shows your source program.<br />

2. <strong>The</strong> Assembly window shows the program as it looks in memory. Right Click in the Assembly window and check Display<br />

Code to make your display show the code in the memory locations $C000 - $C01A. Right Click again and check Display<br />

Symbolic to show symbolic instead of decimal addresses.<br />

3. <strong>The</strong> Registers window shows the current status/contents of all registers.<br />

4. <strong>The</strong> Data window will show the current value of constant and variable data in your program. Click on the +ConstData to show<br />

the four constant data bytes. Right Click on ConstData and select FORMAT ALL Hex. Click on the +VarData to show the two<br />

variable bytes.<br />

5. <strong>The</strong> Procedure window shows what procedure you are currently in and the Command window allows you to enter<br />

simulator/debugger commands.<br />

6. <strong>The</strong> Memory window allows the display of memory contents. Right Click in the Memory window and select Address . . . and<br />

in the Display Address window enter C000.<br />

a. What is displayed here?<br />

7. Try resizing any of the windows.<br />

Run the Program in the Simulator<br />

Explore 5.<br />

1. In the Source window position the cursor pointer over the nop instruction and Right Click and select Set Breakpoint.<br />

<strong>Freescale</strong> Semiconductor LABS12CINTRO03S, Rev 1 6


2. Click the Green Start/Continue Arrow or click Run > Start/Continue. <strong>The</strong> program should run to your breakpoint and stop.<br />

(Note: A breakpoint stops the program before it executes that statement.)<br />

3. Now, step through your program one assembly language statement at a time by clicking on the single-step button or pressing<br />

F11 until you reach the nop instruction<br />

at the beginning of the program again.<br />

(We will see what the LDS instruction is<br />

all about later.)<br />

4. At each step inspect the Register window to see what is happening to the registers. Try to predict what will happen at each<br />

step BEFORE<br />

you click on the step button.<br />

Stimulate 3.<br />

1. Under what circumstances does it make sense to run and debug your programs in the simulator?<br />

2. When would you want to run and debug your programs using the hardware debugger?<br />

Skill Exercise 1<br />

1. Double click on each of the register boxes in the Register window and enter a value of zero for each register. You won't be<br />

able to change the CCR.<br />

2. Press the Reset Target button (or type control-R) to return the program to the initial reset state.<br />

3. Single-step through the program and complete a table showing<br />

the result of each instruction. <strong>The</strong> CCR (Condition Code<br />

Register) bits NZVC a re shown bold w hen th e bit is set.<br />

Instruction D A B IX IY PC SP NZVC<br />

Reset state 00 00 00 00 00 00 00 00 C000 FFFE 00 00<br />

lds<br />

#__SEG_END_SSTACK<br />

nop<br />

ldx #ConstData<br />

ldab #$02<br />

ldaa 0,x<br />

staa VarData<br />

ldaa 2,x<br />

staa VarData+1<br />

inx<br />

decb<br />

bne loop<br />

ldaa 0,x<br />

staa VarData<br />

ldaa 2,x<br />

staa VarData+1<br />

inx<br />

decb<br />

bne loop<br />

<strong>Freescale</strong> Semiconductor LABS12CINTRO03S, Rev 1 7


nop<br />

bra main_loop<br />

Communication<br />

1. As you step through your program explain to your lab instructor what you see on the screen. In particular, explain what is<br />

happening to each register and to the condition code register as you step through your program. What questions do you have?<br />

Reflection on Learning<br />

1. For the program entered and stepped through, list any instructions that you are not sure how they work. Compare your list with<br />

your lab partner's and if you understand something that he or she does not, explain it so that they understand. If there are still<br />

instructions you are not sure of, ask your laboratory instructor to explain them to you.<br />

<strong>Freescale</strong> Semiconductor LABS12CINTRO03S, Rev 1 8


Revision History<br />

Revision Comments Author<br />

0 Initial Release Fred Cady<br />

1 Conversion to CodeWarrior 4.6 Fred Cady<br />

<strong>Freescale</strong> Semiconductor LABS12CINTRO03S, Rev 1 9


How to Reach Us:<br />

Home Page:<br />

www.freescale.com<br />

Web Support:<br />

http://www.freescale.com/support<br />

USA/Europe or Locations Not Listed:<br />

<strong>Freescale</strong> Semiconductor, Inc.<br />

Technical Information Center, EL516<br />

2100 East Elliot Road<br />

Tempe, Arizona 85284<br />

+1-800-521-6274 or +1-480-768-2130<br />

www.freescale.com/support<br />

Europe, Middle East, and Africa:<br />

<strong>Freescale</strong> Halbleiter Deutschland GmbH<br />

Technical Information Center<br />

Schatzbogen 7<br />

81829 Muenchen, Germany<br />

+44 1296 380 456 (English)<br />

+46 8 52200080 (English)<br />

+49 89 92103 559 (German)<br />

+33 1 69 35 48 48 (French)<br />

www.freescale.com/support<br />

Japan:<br />

<strong>Freescale</strong> Semiconductor Japan Ltd.<br />

Headquarters<br />

ARCO <strong>To</strong>wer 15F<br />

1-8-1, Shimo-Meguro, Meguro-ku,<br />

<strong>To</strong>kyo 153-0064<br />

Japan<br />

0120 191014 or +81 3 5437 9125<br />

support.japan@freescale.com<br />

Asia/Pacific:<br />

<strong>Freescale</strong> Semiconductor Hong Kong Ltd.<br />

Technical Information Center<br />

2 Dai King Street<br />

Tai Po Industrial Estate<br />

Tai Po, N.T., Hong Kong<br />

+800 2666 8080<br />

support.asia@freescale.com<br />

For Literature Requests Only:<br />

<strong>Freescale</strong> Semiconductor Literature Distribution<br />

Center<br />

P.O. Box 5405<br />

Denver, Colorado 80217<br />

1-800-441-2447 or 303-675-2140<br />

Fax: 303-675-2150<br />

LDCFor<strong>Freescale</strong>Semiconductor@hibbertgroup.com<br />

<strong>Freescale</strong> and the <strong>Freescale</strong> logo are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other<br />

product or service names are the property of their respective owners. ARM is the registered<br />

trademark of ARM Limited. ARM9, ARM11, and ARML210 are the trademarks of ARM Limited.<br />

Java and all other Java-based marks are trademarks or registered trademarks of Sun<br />

Microsystems, Inc. in the U.S. and other countries. <strong>The</strong> “PowerPC” name is a trademark of IBM<br />

Corp. and used under license.<br />

© <strong>Freescale</strong> Semiconductor, Inc. 2006.<br />

Document Number: LABS12CINTRO03S /REV 1<br />

Information in this document is provided solely to enable system and software<br />

implementers to use <strong>Freescale</strong> Semiconductor products. <strong>The</strong>re are no express or<br />

implied copyright license granted hereunder to design or fabricate any integrated<br />

circuits or integrated circuits based on the information in this document.<br />

<strong>Freescale</strong> Semiconductor reserves the right to make changes without further notice to<br />

any products herein. <strong>Freescale</strong> Semiconductor makes no warranty, representation or<br />

guarantee regarding the suitability of its products for any particular purpose, nor does<br />

<strong>Freescale</strong> Semiconductor assume any liability arising out of the application or use of<br />

any product or circuit, and specifically disclaims any and all liability, including without<br />

limitation consequential or incidental damages. “Typical” parameters which may be<br />

provided in <strong>Freescale</strong> Semiconductor data sheets and/or specifications can and do<br />

vary in different applications and actual performance may vary over time. All operating<br />

parameters, including “Typicals” must be validated for each customer application by<br />

customer’s technical experts. <strong>Freescale</strong> Semiconductor does not convey any license<br />

under its patent rights nor the rights of others. <strong>Freescale</strong> Semiconductor products are<br />

not designed, intended, or authorized for use as components in systems intended for<br />

surgical implant into the body, or other applications intended to support or sustain life,<br />

or for any other application in which the failure of the <strong>Freescale</strong> Semiconductor<br />

product could create a situation where personal injury or death may occur. Should<br />

Buyer purchase or use <strong>Freescale</strong> Semiconductor products for any such unintended or<br />

unauthorized application, Buyer shall indemnify and hold <strong>Freescale</strong> Semiconductor<br />

and its officers, employees, subsidiaries, affiliates, and distributors harmless against<br />

all claims, costs, damages, and expenses, and reasonable attorney fees arising out<br />

of, directly or indirectly, any claim of personal injury or death associated with such<br />

unintended or unauthorized use, even if such claim alleges that <strong>Freescale</strong><br />

Semiconductor was negligent regarding the design or manufacture of the part.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!