![]() |
|
If you can't view the Datasheet, Please click here to try to view without PDF Reader . |
|
Datasheet File OCR Text: |
dsp56KCCUM/d document rev. 2.0, 07/1999 motorola dsp56000 family optimizing c compiler users manual, release 6.3 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . .
suite56, once, and mfax are trademarks of motorola, inc. motorola reserves the right to make changes without further notice to any products herein. motorola makes no warranty, representation or guarantee regarding the suitability of its products for any particular purpose, nor does motorola assume any liability arising out of the application or use of any product or circuit, and specifically disclaims any and all liability, including without limitation consequential or incidental damages. typical parameters which may be provided in motorola data sheets and/or specifications can and do vary in different applications and actual performance may vary over time. all operating parameters, including typicals must be validated for each customer application by customers technical experts. motorola does not convey any license under its patent rights nor the rights of others. motorola products are not designed, intended, or authorized for use as components in systems intended for surgical implant into the body, or other applications intended to support life, or for any other application in which the failure of the motorola product could create a situation where personal injury or death may occur. should buyer purchase or use motorola products for any such unintended or unauthorized application, buyer shall indemnify and hold motorola and its officers, employees, subsidiaries, affiliates, and distributors harmless against all claims, costs, damages, and expenses, and reasonable attorney fees arising out of, directly or indirectly, any claim of personal injury or death associated with such unintended or unauthorized use, even if such claim alleges that motorola was negligent regarding the design or manufacture of the part. motorola and are registered trademarks of motorola, inc. motorola, inc. is an equal opportunity/affirmative action employer. all other tradenames, trademarks, and registered trademarks are the property of their respective owners. ? copyright motorola, inc., 1999. all rights reserved. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola v chapter 1 introduction 1.1 overview ....................................................... 1-1 1.2 error codes ..................................................... 1-4 1.3 notation ........................................................ 1-4 1.4 manual organization .............................................. 1-5 chapter 2 installation guide 2.1 introduction ..................................................... 2-1 2.2 installation on an ms-dos machine (80386 or 80486) .................. 2-1 2.3 standard installation on a sun ..................................... 2-3 2.4 alternate installation on a sun ..................................... 2-4 2.5 test program .................................................... 2-4 chapter 3 control program options 3.1 overview ....................................................... 3-1 3.2 g56k command line options ....................................... 3-5 3.2.1 preprocessor phase options ...................................... 3-7 3.2.2 compile phase options ........................................ 3-20 3.3 assemble phase options .......................................... 3-30 3.4 link phase options .............................................. 3-31 chapter 4 about g56k 4.1 introduction ..................................................... 4-1 4.2 identifiers ....................................................... 4-1 4.3 predefined preprocessor macro names ................................ 4-1 4.4 data types and sizes .............................................. 4-1 4.4.1 integral data types ............................................ 4-2 4.4.2 floating-point types ........................................... 4-3 4.4.2.1 floating-point format description . . ............................ 4-3 4.4.2.2 comparison with ieee std 754-1985 standard ................... 4-5 table of contents f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . vi motorola dsp56000 family optimizing c compiler users manual motorola 4.4.3 pointer types . . ............................................... 4-7 4.5 register usage ................................................... 4-8 4.6 memory usage ................................................... 4-9 4.6.1 activation record ............................................ 4-10 4.6.2 global/static data ............................................ 4-13 4.7 compiler naming conventions ..................................... 4-13 4.8 subroutine call sequence ......................................... 4-14 4.8.1 caller sequence .............................................. 4-14 4.8.2 callee sequence .............................................. 4-15 4.8.3 return sequence .............................................. 4-15 4.9 software support for aritmetic routines . . ........................... 4-15 4.10 run-time safety . . . .............................................. 4-16 4.10.1 memory allocation checks ..................................... 4-16 4.10.2 run-time stack checks ........................................ 4-17 4.11 optimization techniques implemented ............................... 4-17 4.11.1 register promotion and lifetime analysis ......................... 4-17 4.11.2 common sub-expression elimination . . ........................... 4-17 4.11.3 constant propagation/folding ................................... 4-18 4.11.4 dead code elimination ........................................ 4-18 4.11.5 tail merging. . . .............................................. 4-18 4.11.6 strength reduction ............................................ 4-19 4.11.7 loop invariant code motion .................................... 4-19 4.11.8 hardware do loop instruction .................................. 4-19 4.11.9 loop rotation. . .............................................. 4-20 4.11.10 jump optimizations ........................................... 4-20 4.11.11 instruction combination ........................................ 4-20 4.11.12 leaf routine detection ........................................ 4-21 4.11.13 function in-lining ............................................. 4-21 4.11.14 instruction scheduling / microcode compaction .................... 4-21 chapter 5 mixing c and assembly language 5.1 overview ....................................................... 5-1 5.2 in-line assembly code ............................................ 5-1 5.2.1 instruction template ........................................... 5-3 5.2.2 output/input operands .......................................... 5-5 5.2.3 explicit register saving ......................................... 5-7 5.2.4 in-line assembly code examples ................................. 5-8 5.2.5 controlling labels generated by the compiler ...................... 5-17 5.2.5.1 calling assembly subroutines ................................ 5-17 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola vii 5.2.5.2 calling c subroutines from assembly code ..................... 5-18 5.2.5.3 referencing assembly global variables from c .................. 5-20 5.2.5.4 referencing global c variables from assembly language ......... 5-21 5.2.6 specifying registers for variables ................................ 5-22 5.2.7 optimizer effects on code ...................................... 5-22 5.3 #pragma directive . .............................................. 5-22 5.4 out-of-line assembly code ........................................ 5-25 5.4.1 general template ............................................. 5-26 5.4.1.1 prologue . . . .............................................. 5-27 5.4.1.2 save all registers ........................................... 5-27 5.4.1.3 main program ............................................. 5-28 5.4.1.4 restore all registers ........................................ 5-28 5.4.1.5 epilogue . . . .............................................. 5-29 5.4.1.6 out-of-line assembly code example ........................... 5-29 5.4.2 global c and static variables in c ............................... 5-33 5.4.3 using run-time stack for local data . . ........................... 5-34 5.4.4 calling c routines ............................................ 5-35 5.4.5 optimization techniques ....................................... 5-36 chapter 6 software-hardware integration 6.1 overview ....................................................... 6-1 6.2 run-time environment specification files ............................ 6-1 6.3 crt0 file ........................................................ 6-2 6.4 bootstrapping the c program ........................................ 6-2 6.5 memory configuration and management . . ............................ 6-3 6.6 interrupt vectors . . ............................................... 6-6 6.7 miscellaneous code ............................................... 6-8 6.8 signal file ...................................................... 6-9 6.9 signal() ....................................................... 6-10 6.9.1 raise() ..................................................... 6-11 6.10 setjmp file ..................................................... 6-12 6.11 host-supported i/o (printf (), et al) .................................. 6-12 6.11.1 dsp functions __send () and __receive () .......................... 6-13 6.11.2 the host-side i/o driver ....................................... 6-13 6.11.3 communication between the host and dsp ........................ 6-13 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . viii motorola dsp56000 family optimizing c compiler users manual motorola appendix a programming support a.1 standard ansi header files ........................................a-1 a.2 ansi c library sub-routines .......................................a-2 a.2.1 hosted vs. non-hosted library routines ...........................a-2 a.3 forcing library routines out-of-line .................................a-2 a.4 function descriptions .............................................a-4 a.4.1 abortforce abnormal program termination ........................a-9 a.4.2 absabsolute value of integer .................................. a-10 a.4.3 acosarc cosine ............................................. a-11 a.4.4 asinarc sine . .............................................. a-12 a.4.5 atanarc tangent ............................................ a-13 a.4.6 atan2arc tangent of angle defined by point y/x .................... a-14 a.4.7 atexitregister a function for execution at normal program termination . a-15 a.4.8 atofstring to floating point .................................... a-16 a.4.9 atoistring to integer ......................................... a-17 a.4.10 atolstring to long integer ..................................... a-18 a.4.11 bsearchperform binary search ................................. a-19 a.4.12 callocdynamically allocate zero-initialized storage for objects ....... a-20 a.4.13 ceilceiling function ......................................... a-22 a.4.14 clearerrclear any error indicators associated with a specified stream . . . a-22 a.4.15 coscosine . . . .............................................. a-23 a.4.16 coshhyperbolic cosine ....................................... a-23 a.4.17 divinteger division with remainder . . ........................... a-24 a.4.18 exitterminate program normally ............................... a-25 a.4.19 expexponential, ex .......................................... a-26 a.4.20 fabsabsolute value of a double ................................ a-26 a.4.21 fcloseclose a stream ......................................... a-27 a.4.22 feoftest the end-of-file indicator of a specified stream .............. a-28 a.4.23 ferrortest the error indicator of a stream ......................... a-29 a.4.24 fflushflush all pending output associated with a stream ............. a-29 a.4.25 fgetcread a character from the specified stream ................... a-30 a.4.26 fgetposget value of file position indicator associated with a stream .... a-31 a.4.27 fgetsread a string from the specified stream ...................... a-32 a.4.28 floorfloor function .......................................... a-33 a.4.29 fmodfloating point remainder ................................. a-33 a.4.30 fopenopen a named file on the hosts disk ....................... a-34 a.4.31 fprintfwrite formatted output to a stream ........................ a-36 a.4.32 fputcwrite a single character to a stream ......................... a-36 a.4.33 fputswrite a string to a stream ................................. a-37 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola ix a.4.34 freadread data directly from a stream ........................... a-38 a.4.35 freefree storage allocated by calloc, malloc, and realloc . . . ......... a-39 a.4.36 freopenopen named file on disk, to be accessed via specified stream . . a-40 a.4.37 frexpbreak a floating point number into mantissa and exponent ...... a-41 a.4.38 fscanfread formatted input from a stream ........................ a-42 a.4.39 fseekset the file position indicator associated with a stream . ......... a-44 a.4.40 fsetposset the file position indicator associated with a stream ........ a-45 a.4.41 ftellget the file position indicator associated with a stream . ......... a-46 a.4.42 fwritewrite data directly to a stream . ........................... a-47 a.4.43 getcread a character from the specified stream .................... a-48 a.4.44 getcharread a character from standard input ...................... a-48 a.4.45 getsread a string from standard input ........................... a-49 a.4.46 isalnumtest for alphanumeric character ......................... a-50 a.4.47 isalphatest for alphabetic character. . ........................... a-51 a.4.48 iscntrltest for control character ................................ a-52 a.4.49 isdigittest for numeric character ............................... a-52 a.4.50 isgraphtest for printing character, excluding space and tab . ......... a-54 a.4.51 islowertest for lower-case alphabetic characters ................... a-54 a.4.52 isprinttest for printing character, excluding \t ................... a-55 a.4.53 ispuncttest for punctuation character ........................... a-56 a.4.54 isspacetest for white-space character ........................... a-56 a.4.55 isuppertest for upper-case alphabetic character ................... a-57 a.4.56 isxdigittest for hexadecimal numeric character ................... a-58 a.4.57 labsabsolute value of a long integer . ........................... a-58 a.4.58 ldexpmultiply floating point number by 2 ........................ a-59 a.4.59 ldivlong integer division with remainder ........................ a-60 a.4.60 lognatural logarithm, base e .................................. a-61 a.4.61 log10base ten logarithm ..................................... a-62 a.4.62 longjmpexecute a non-local jump . . . ........................... a-63 a.4.63 mallocdynamically allocate uninitialized storage .................. a-64 a.4.64 mblenlength of a multibyte character ........................... a-64 a.4.65 mbstowcsconvert multibyte string to wide character string . ......... a-66 a.4.66 mbtowcconvert a multibyte character to a wide character . . ......... a-68 a.4.67 memchrfind a character in a memory area ....................... a-70 a.4.68 memcmpcompare portion of two memory areas ................... a-71 a.4.69 memcpycopy from one area to another .......................... a-72 a.4.70 memmovecopy storage ...................................... a-73 a.4.71 memsetinitialize memory area ................................. a-74 a.4.72 modfbreak a double into its integral and fractional parts. . . ......... a-75 a.4.73 perrorprint error message ..................................... a-76 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . x motorola dsp56000 family optimizing c compiler users manual motorola a.4.74 powraise a double to a power ................................. a-77 a.4.75 printfprint to standard output .................................. a-78 a.4.76 putcwrite a single character to a stream ......................... a-83 a.4.77 putcharwrite a character to standard output ...................... a-84 a.4.78 putswrite a string to standard output . ........................... a-85 a.4.79 qsortquick sort ............................................. a-86 a.4.80 raiseraise a signal .......................................... a-87 a.4.81 randpseudo- random number generator .......................... a-88 a.4.82 reallochange size of dynamically allocated storage area ............. a-88 a.4.83 removeremove a file from the disk . . ........................... a-90 a.4.84 rename rename a file on the disk . . . ........................... a-90 a.4.85 rewindreset the file position indicator to the beginning of the file ..... a-90 a.4.86 scanfread formatted input from standard input .................... a-91 a.4.87 setjmpsave reference of current calling environment for later use by longjmp ..................................... a-92 a.4.88 setbufread formatted input from standard input ................... a-93 a.4.89 setvbufalter stream buffering ................................. a-94 a.4.90 signalset up signal handler .................................... a-94 a.4.91 sinsine ................................................... a-95 a.4.92 sinhhyperbolic sine ......................................... a-96 a.4.93 sprintfprint to a string ........................................ a-96 a.4.94 sqrtsquare root ............................................. a-97 a.4.95 srandseed the pseudo-random number generator .................. a-98 a.4.96 sscanfread formatted input from a string ........................ a-98 a.4.97 strcatconcatenate two strings .................................. a-99 a.4.98 strchrfind first occurrence of a character in a string ............... a-100 a.4.99 strcmpcompare two strings .................................. a-100 a.4.100 strcollcompare two strings based on current locale ............... a-102 a.4.101 strcpycopy one string into another . . .......................... a-102 a.4.102 strcspncompute length of prefix of one string consisting entirely of characters not from another ................ a-104 a.4.103 strerrormap error code into an error message string ............... a-105 a.4.104 strlendetermine length of a string . . . .......................... a-106 a.4.105 strncatconcatenate a portion of one string to another .............. a-106 a.4.106 strncmpcompare a portions of two strings ...................... a-108 a.4.107 strncpycopy a portion of one string into another .................. a-109 a.4.108 strpbrkfind first occurrence of a character from one string in another . a-110 a.4.109 strrchrfind last occurrence of a character from one string in another . . a-111 a.4.110 strspncompute length of prefix of one string consisting entirely of characters from another ................... a-112 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola xi a.4.111 strstrfind the first occurrence of one string in another ............. a-112 a.4.112 strtodstring to double ....................................... a-114 a.4.113 strtokbreak string into tokens ................................ a-115 a.4.114 strtolstring to long integer ................................... a-116 a.4.115 strtoulstring to unsigned long integer .......................... a-118 a.4.116 strxfrmtransform a string into locale-independent form ............ a-120 a.4.117 tantangent . . ............................................. a-120 a.4.118 tanhhyperbolic tangent ..................................... a-121 a.4.119 tmpfilecreate a temporary binary file .......................... a-121 a.4.120 tmpnamcreate a temporary file name .......................... a-122 a.4.121 tolowerconvert uppercase character to lowercase ................. a-122 a.4.122 toupperconvert lowercase character to uppercase ................. a-123 a.4.123 ungetcpush a character back onto an input stream ................. a-124 a.4.124 vfprintfwrite formatted output to a stream using a va_list . . ........ a-124 a.4.125 vprintfwrite formatted output to standard output using a va_list ..... a-125 a.4.126 vsprintfwrite formatted output to a string using a va_list . . . ........ a-126 a.4.127 wcstombsconvert wchar_t array to multibyte string ............... a-126 a.4.128 wctombconvert wchar_t character to multibyte character . . ........ a-128 appendix b dsp56000/dsp56001 instruction set and assembler directive summary b.1 overview .......................................................b-1 b.2 instruction set ...................................................b-1 b.2.1 arithmetic instructions ..........................................b-1 b.2.2 logical instructions ............................................b-2 b.2.3 bit manipulation instructions ....................................b-3 b.2.4 loop instructions ..............................................b-3 b.2.5 move instructions ..............................................b-4 b.2.6 program control instructions .....................................b-4 b.3 directive summary ...............................................b-5 b.3.1 assembly control ..............................................b-5 b.3.2 symbol definition .............................................b-6 b.3.3 data definition/storage allocation ................................b-6 b.3.4 listing control and options ......................................b-6 b.3.5 object file control .............................................b-7 b.3.6 macros and conditional assembly ................................b-7 b.3.7 structured programming ........................................b-8 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . xii motorola dsp56000 family optimizing c compiler users manual motorola appendix c utilities c.1 utility programs. . . ...............................................c-1 c.1.1 asm56000motorola dsp56000/dsp56001 coff assembler ..........c-1 c.1.1.1 asm5600 options ...........................................c-2 c.1.2 cldinfomemory size information from motorola dsp coff object file. . c-4 c.1.3 cldlodmotorola coff to lod format converter ...................c-5 c.1.4 cofdmpmotorola dsp coff file dump utility ....................c-5 c.1.4.1 cofdmp options ............................................c-5 c.1.5 dsplibmotorola dsp coff librarian ............................c-6 c.1.5.1 dsplib options ..............................................c-6 c.1.6 dsplnkmotorola dsp coff linker . . ............................c-7 c.1.6.1 dsplnk options .............................................c-8 c.1.7 gdb56gnu source-level debugger for dsp56000/dsp56001 ........ c-11 c.1.7.1 gdb56 options ............................................. c-11 c.1.8 run56motorola dsp56000/dsp56001 simulator based execution device. .......................................... c-12 c.1.8.1 run56 options ............................................. c-12 c.1.9 srecmotorola dsp s-record conversion utility ................... c-13 c.1.9.1 srec options .............................................. c-13 appendix d gnu debugger (gdb) d.1 introduction .....................................................d-1 d.1.1 commands not implemented ....................................d-1 d.1.2 dsp56000 family differences / specific requirements ................d-1 d.1.3 summary of gdb .............................................d-3 d.1.4 gnu general public license .....................................d-3 d.1.5 preamble .....................................................d-3 d.1.6 terms and conditions ..........................................d-4 d.1.7 no warranty. . . ...............................................d-6 d.1.8 end of terms and conditions ....................................d-7 d.2 input and output conventions .......................................d-8 d.3 specifying gdbs files ........................................... d-10 d.3.1 specifying files with arguments ................................. d-10 d.3.2 specifying files with commands ................................ d-10 d.4 compiling your program for debugging . . ........................... d-12 d.5 running your program under gdb ................................. d-12 d.5.1 your programs arguments ..................................... d-13 d.5.2 your programs environment ................................... d-13 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola xiii d.5.3 your programs working directory . . . ........................... d-14 d.5.4 your programs input and output ................................ d-14 d.5.5 debugging an already-running process ........................... d-15 d.5.6 killing the child process ....................................... d-16 d.6 stopping and continuing .......................................... d-16 d.6.1 signals ..................................................... d-16 d.6.2 breakpoints . . . .............................................. d-18 d.6.2.1 setting breakpoints ......................................... d-18 d.6.2.2 deleting breakpoints ....................................... d-19 d.6.2.3 disabling breakpoints ...................................... d-20 d.6.2.4 break conditions .......................................... d-21 d.6.2.5 commands executed on breaking . . ........................... d-22 d.6.2.6 cannot insert breakpoints error . . ........................... d-24 d.6.3 continuing .................................................. d-25 d.6.4 stepping .................................................... d-25 d.7 examining the stack ............................................. d-27 d.7.1 stack frames . . .............................................. d-27 d.7.2 backtraces .................................................. d-28 d.7.3 selecting a frame ............................................. d-29 d.7.4 information on a frame ........................................ d-30 d.8 examining source files ........................................... d-30 d.8.1 printing source lines .......................................... d-30 d.8.2 searching source files ......................................... d-32 d.8.3 specifying source directories ................................... d-32 d.9 examining data . . . .............................................. d-33 d.9.1 expressions . . . .............................................. d-33 d.9.2 program variables ............................................ d-34 d.9.3 artificial arrays .............................................. d-35 d.9.4 format options . .............................................. d-35 d.9.5 output formats . .............................................. d-37 d.9.5.1 examining memory ........................................ d-38 d.9.6 automatic display ............................................ d-40 d.9.7 value history . . .............................................. d-41 d.9.8 convenience variables ......................................... d-42 d.9.9 registers .................................................... d-43 d.9.9.1 examples . . .............................................. d-44 d.10 examining the symbol table ...................................... d-44 d.11 altering execution. .............................................. d-45 d.11.1 assignment to variables ....................................... d-46 d.11.2 continuing at a different address ................................ d-46 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . xiv motorola dsp56000 family optimizing c compiler users manual motorola d.11.3 giving the program a signal .................................... d-47 d.11.4 returning from a function ...................................... d-48 d.12 canned sequences of commands ................................... d-48 d.13 user-defined commands ......................................... d-48 d.13.1 command files .............................................. d-49 d.13.2 commands for controlled output ................................ d-49 d.14 options and arguments for gdb ................................... d-50 d.14.1 mode options. . .............................................. d-50 d.14.2 file-specifying options ........................................ d-51 d.14.3 other arguments ............................................. d-51 d.15 using gdb under gnu emacs ..................................... d-52 appendix e additional support e.1 motorola dsp product support ...................................... e-2 e.2 dsp56000clasx assembler/simulator. . . ............................ e-2 e.2.1 macro cross assembler features: ................................. e-3 e.2.2 simulator features: ............................................ e-3 e.3 dsp320to56001 translator ......................................... e-4 e.3.1 dsp320to56001 translator features: . . ............................ e-4 e.4 dsp56000adsx application development systems (ads) ............... e-4 e.4.1 dsp56000ads ads hardware features: ........................... e-4 e.4.2 dsp56000adsx ads software features: .......................... e-5 e.4.2.1 support integrated circuits: ................................... e-5 e.5 dr. bub electronic bulletin board ................................... e-6 e.5.1 motorola dsp news .......................................... e-14 e.5.2 motorola field application engineers. . ........................... e-14 e.5.3 design hotline C 1-800-521-6274 ................................ e-14 e.5.4 dsp applications helplineC (512) 891-3230 ....................... e-14 e.5.5 dsp marketing information C (512) 891-2030 ...................... e-15 e.5.6 dsp third-party support information C (512) 891-3098 .............. e-15 e.5.7 dsp university support C (512) 891-3098 ......................... e-15 e.5.8 dsp training courses C (602) 994-6900 ........................... e-15 e.5.9 reference books and manuals ................................... e-16 e.5.10 general dsp: . . .............................................. e-16 e.5.11 digital audio and filters: ....................................... e-17 e.5.12 c programming language: ..................................... e-18 e.5.13 controls: .................................................... e-19 e.5.14 graphics: ................................................... e-19 e.5.15 image processing: ............................................ e-21 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola xv e.5.16 motorola dsp manuals: ........................................ e-21 e.5.17 numerical methods: ........................................... e-21 e.5.18 pattern recognition: ........................................... e-22 e.5.19 speech: ..................................................... e-23 e.5.20 telecommunications: .......................................... e-23 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . xvi motorola dsp56000 family optimizing c compiler users manual motorola f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola xvii 3-1 dsp56kcc options ......................................... 3-4 4-1 identifier length limits ...................................... 4-1 4-2 predefined macro list and explanation .......................... 4-2 4-3 integral data type sizes and ranges ............................ 4-2 4-4 floating-point data type sizes and ranges ....................... 4-3 4-5 floating-point format description . . ............................ 4-5 4-6 comparison of dsp56kcc and ieee 754-1985 ................... 4-6 4-7 pointer size and range ....................................... 4-8 4-8 dsp56000 family processor registers .......................... 4-8 4-9 dsp56kcc registers and usage . . . ............................ 4-9 4-10 floating-point arithmetic .................................... 4-16 4-11 48-bit long integer arithmetic ................................ 4-16 5-1 operand constraints/modifiers associations ...................... 5-7 5-2 reg_save names ............................................ 5-8 5-3 output and input operands for section 5-3 ....................... 5-9 a-1 ansi standard header files ...................................a-1 a-2 in-line library routines .....................................a-3 a-3 function descriptions ........................................a-4 a-4 argument range/output range ...............................a-14 a-5 mode arguments ..........................................a-34 a-6 conversion specifyers ......................................a-42 a-7 anssbi flags .............................................a-79 a-8 conversion characters ......................................a-80 b-1 arithmetic operations ........................................b-1 b-2 logical operations ..........................................b-2 b-3 bit manipulation instructions ..................................b-3 b-4 loop operation instructions ...................................b-3 list of tables f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . xviii motorola dsp56000 family optimizing c compiler users manual motorola b-5 move operation instructions ..................................b-4 b-6 program control instructions ..................................b-4 b-7 assembly control directives ..................................b-5 b-8 control symbol directives ....................................b-6 b-9 storage allocation directives ..................................b-6 b-10 output listing directives .....................................b-6 b-11 object file directives ........................................b-7 b-12 conditional assembly directives . . . ............................b-7 b-13 programming directives ......................................b-8 c-1 asm56000 command line options . ............................c-3 c-2 cofdmp command line options . . . ............................c-5 c-3 cldinfo command line options ................................c-7 c-4 dsplnk command line options ................................c-9 c-5 gdb56 command line options ................................ c-11 c-6 run56 command line options ................................ c-12 c-7 srec command line options ................................. c-13 d-1 gdb file commands ....................................... d-11 d-2 environment variables ...................................... d-14 d-3 working directory commands ............................... d-14 d-4 keywords . . .............................................. d-17 d-5 breakpoints . .............................................. d-18 d-6 deleting breakpoints commands . . . ........................... d-20 d-7 disabling breakpoint commands. . . ........................... d-20 d-8 stepping commands ........................................ d-25 d-9 backtraces. . .............................................. d-28 d-10 frame commands .......................................... d-29 d-11 frame command .......................................... d-30 d-12 list commands ............................................ d-31 d-13 single source line commands ............................... d-31 d-14 directory commands ....................................... d-33 d-15 gdb operators ............................................ d-34 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola xix d-16 print output format letters .................................. d-37 d-17 memory output format letters ............................... d-38 d-18 print output format letters .................................. d-38 d-19 display commands ......................................... d-41 d-20 info commands ........................................... d-42 d-21 convenience variables ...................................... d-43 d-22 symbol-file commands ..................................... d-45 d-23 define commands ......................................... d-48 d-24 controlled output commands ................................ d-50 d-25 mode options ............................................. d-51 d-26 file specifying options ..................................... d-51 d-27 emac commands .......................................... d-52 e-1 user support ............................................... e-1 e-2 available software .......................................... e-6 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . xx motorola dsp56000 family optimizing c compiler users manual motorola f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola xxi 1-1 motorola software development system ......................... 1-2 4-1 mantissa and exponent data range of c floating point . . . .......... 4-4 4-2 default program memory configuration ........................ 4-10 4-3 activation record .......................................... 4-11 4-4 default data memory configuration ........................... 4-12 4-5 run-time stack growth ..................................... 4-13 6-1 environment memory configuration ............................ 6-4 list of figures f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . xxii motorola dsp56000 family optimizing c compiler users manual motorola f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola xxiii 2-1 test program ............................................... 2-4 3-1 lib56cy.clb . ............................................... 3-5 3-2 g56_exec_prefix ........................................ 3-5 3-3 test new version of dsp56000/1 c ............................ 3-5 3-4 test new version of ansi c library ........................... 3-6 3-5 test new version of dsp56kcc. . . ............................ 3-6 3-6 generate a preprocessed file .................................. 3-6 3-7 compile file.c / generate executable ouput file ................... 3-6 3-8 preprocess program foo.c .................................... 3-7 3-9 compile and run program dsp.c . . . ............................ 3-8 3-10 program dsp.c .............................................. 3-9 3-11 preprocess c source program foo.c. ............................ 3-9 3-12 cpp output . .............................................. 3-10 3-13 -i option used to include the file myinclude.h ................. 3-11 3-14 test program file.c ....................................... 3-12 3-15 program greeting.c prints message . ........................... 3-14 3-16 program big.c ............................................. 3-15 3-17 program big.c ............................................. 3-15 3-18 test program test.c ........................................ 3-16 3-19 -v option . . . .............................................. 3-18 3-20 debug flag .............................................. 3-19 3-21 -wcomment option ........................................ 3-19 3-22 r4 ...................................................... 3-22 3-23 program foo.c ............................................. 3-23 3-24 -mx-memory option ........................................ 3-24 3-25 generate an optimized assembly language file (test.asm) of the c source program (test.c). .................... 3-26 3-26 generate an optimized assembly language file test.asm ........... 3-26 3-27 -w ...................................................... 3-27 list of examples f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . xxiv motorola dsp56000 family optimizing c compiler users manual motorola 3-28 -wimplicit. . .............................................. 3-28 3-29 -wreturn-type ............................................. 3-28 3-30 -wunused . . .............................................. 3-29 3-31 pass a single option to the assembler. ........................... 3-31 3-32 pass multiple options to the assembler. ......................... 3-31 3-33 -crt file .................................................. 3-32 3-34 pass a single option to the linker. . . . ........................... 3-32 3-35 pass multiple options to the linker.. . ........................... 3-32 3-36 -llibrary .............................................. 3-32 3-37 -r ctlfile. .............................................. 3-33 5-1 reg_save . . . ............................................... 5-8 5-2 instruction_template ......................................... 5-9 5-3 operand_id . ............................................... 5-9 5-4 oes modifier j ............................................ 5-10 5-5 oes modifier e ............................................ 5-10 5-6 oes modifier h ........................................... 5-10 5-7 oes modifier k ........................................... 5-11 5-8 oes modifier g ........................................... 5-11 5-9 oes modifier g ........................................... 5-12 5-10 oes modifier i ............................................ 5-12 5-11 oes modifier f ............................................ 5-13 5-12 oes modifier p and q ....................................... 5-13 5-13 input expression / output expression .......................... 5-14 5-14 read-only operand ........................................ 5-14 5-15 write-only operand ........................................ 5-14 5-16 read-write operand ........................................ 5-14 5-17 read-write operand ........................................ 5-15 5-18 multiple instruction single-line . ........................... 5-15 5-19 multiple instruction multiple-line .......................... 5-15 5-20 mulitple use of _ _asm() .................................... 5-15 5-21 line separation ............................................ 5-16 5-22 instruction template label ................................... 5-16 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola xxv 5-23 program dsp56001 sci timer ............................... 5-17 5-24 calling assembly from c .................................... 5-18 5-25 calling c from assembly .................................... 5-19 5-26 calling c from assembly .................................... 5-19 5-27 generate data with assembly language ........................ 5-20 5-28 access data with c ......................................... 5-20 5-29 generate data with c ....................................... 5-21 5-30 4-tap fir filter ............................................ 5-21 5-31 general template for out-of-line assembly code ................ 5-30 5-32 global label in assembly language ........................... 5-33 5-33 global variable declaration .................................. 5-33 5-34 changing a global label .................................... 5-34 5-35 run-time stack allocation ................................... 5-34 5-36 run-time stack deallocation ................................. 5-35 5-37 calling c routines ......................................... 5-35 5-38 readx / ready routines .................................... 5-37 6-1 dsp56000/dsp56001 operation mode change ................... 6-3 6-2 c bootstrap code location change . ............................ 6-3 6-3 fast stack . . ............................................... 6-5 6-4 fast heap . . ............................................... 6-6 6-5 user-defined interrupt vector table ............................ 6-7 6-6 interrupt service routine ..................................... 6-8 6-7 sample host-side glue code ................................. 6-15 a-1 abort .....................................................a-9 a-2 abs...................................................... a-10 a-3 acos ..................................................... a-11 a-4 asin ..................................................... a-12 a-5 attan .................................................... a-13 a-6 attan2 ................................................... a-14 a-7 atexit .................................................... a-15 a-8 atof ..................................................... a-16 a-9 atoi ..................................................... a-17 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . xxvi motorola dsp56000 family optimizing c compiler users manual motorola a-10 atol ..................................................... a-18 a-11 bsearch .................................................. a-19 a-12 calloc .................................................... a-21 a-13 ceil ..................................................... a-22 a-14 clearerr .................................................. a-22 a-15 cos ...................................................... a-23 a-16 cosh ..................................................... a-23 a-17 div ...................................................... a-24 a-18 exit ..................................................... a-25 a-19 exp ..................................................... a-26 a-20 fabs ..................................................... a-27 a-21 fclose .................................................... a-27 a-22 feof ..................................................... a-28 a-23 fflush .................................................... a-29 a-24 fgetc .................................................... a-30 a-25 fgetpos .................................................. a-31 a-26 fgets .................................................... a-32 a-27 floor .................................................... a-33 a-28 fmod .................................................... a-33 a-29 fopen .................................................... a-35 a-30 fprintf ................................................... a-36 a-31 fputc .................................................... a-36 a-32 fputs .................................................... a-37 a-33 fread .................................................... a-38 a-34 free ..................................................... a-39 a-35 freopen .................................................. a-40 a-36 frexp .................................................... a-41 a-37 fscanf ................................................... a-43 a-38 fseek .................................................... a-44 a-39 fsetpos ................................................... a-45 a-40 ftell ..................................................... a-46 a-41 fwrite .................................................... a-47 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola xxvii a-42 getc ..................................................... a-48 a-43 getchar .................................................. a-49 a-44 gets ..................................................... a-49 a-45 islnum ................................................... a-50 a-46 isalpha ................................................... a-51 a-47 iscntrl ................................................... a-52 a-48 isdigit ................................................... a-53 a-49 isgraph .................................................. a-54 a-50 islower .................................................. a-55 a-51 isprint ................................................... a-55 a-52 ispunct ................................................... a-56 a-53 isspace ................................................... a-57 a-54 isupper .................................................. a-57 a-55 isxdigit .................................................. a-58 a-56 labs ..................................................... a-59 a-57 ldexp .................................................... a-59 a-58 ldiv ..................................................... a-60 a-59 log ...................................................... a-61 a-60 log10 .................................................... a-62 a-61 longjmp .................................................. a-63 a-62 malloc ................................................... a-64 a-63 mblem ................................................... a-65 a-64 mbstowcs . . .............................................. a-67 a-65 mbtowc .................................................. a-69 a-66 memchr .................................................. a-70 a-67 memcmp . . . .............................................. a-71 a-68 memcpy . . . .............................................. a-72 a-69 memmove . . .............................................. a-73 a-70 memset .................................................. a-74 a-71 modf .................................................... a-75 a-72 perror ................................................... a-76 a-73 pow ..................................................... a-77 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . xxviii motorola dsp56000 family optimizing c compiler users manual motorola a-74 printf .................................................... a-82 a-75 putc ..................................................... a-83 a-76 putchar .................................................. a-84 a-77 puts ..................................................... a-85 a-78 qsort .................................................... a-86 a-79 raise ..................................................... a-87 a-80 rand ..................................................... a-88 a-81 realloc ................................................... a-89 a-82 remove .................................................. a-90 a-83 rename .................................................. a-90 a-84 rewind ................................................... a-91 a-85 scanf .................................................... a-91 a-86 setjmp ................................................... a-92 a-87 signal .................................................... a-95 a-88 sin ...................................................... a-95 a-89 sinh ..................................................... a-96 a-90 sprintf ................................................... a-97 a-91 sqrt ..................................................... a-97 a-92 srand .................................................... a-98 a-93 strcat .................................................... a-99 a-94 strchr ................................................... a-100 a-95 strcmp .................................................. a-101 a-96 strcpy .................................................. a-103 a-97 strcspn .................................................. a-104 a-98 strerror ................................................. a-105 a-99 strlen ................................................... a-106 a-100 strncat .................................................. a-107 a-101 strncmp ................................................. a-108 a-102 strncpy ................................................. a-109 a-103 strbrk ................................................... a-110 a-104 strrchr .................................................. a-111 a-105 strspn ................................................... a-112 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola xxix a-106 strstr ................................................... a-113 a-107 strtod ................................................... a-114 a-108 strtok ................................................... a-115 a-109 strtol ................................................... a-117 a-110 strtoul .................................................. a-119 a-111 tan ..................................................... a-120 a-112 tanh .................................................... a-121 a-113 tmpnam ................................................. a-122 a-114 tolower ................................................. a-123 a-115 toupper ................................................. a-123 a-116 ungetc .................................................. a-124 a-117 vfprintf ................................................. a-125 a-118 vprintf .................................................. a-125 a-119 vsprintf ................................................. a-126 a-120 wcstombs . . ............................................. a-127 a-121 wctomb ................................................. a-129 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . xxx motorola dsp56000 family optimizing c compiler users manual motorola f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . overview motorola introduction 1-1 chapter 1 introduction 1.1 overview the dsp56kcc gnu based c cross-compiler is the latest high-level language development system for the motorola dsp56000 family of digital signal processors (dsp). it includes: ? an integrated control program g56k ? an ansi compliant c language preprocessor mcpp ? an ansi optimizing c compiler g56-cc1 ? a dsp56000 common object file format (coff) assembler asm56000 ? a coff linker dsplnk ? a coff librarian dsplib ? a source level debugger for c gdb56 ? a simulator based execution program run56 ? various object file manipulation tools srec, cldlod, cofdmp this integrated software system runs on a variety of machines and operating systems, including the ibm pc (80386 family and above 386sx, 486, etc.), and sun sparc workstations. the compiler implements the full c language as defined in american national standard for information systems - programming language - c, ansi x3.159-1989 . it accepts one or more c language source files as input and generates a corresponding number of assembly language source files which are suitable as inputs to the dsp56000 coff assembler. the compiler automatically implements numerous optimizations which simplifies implementing fast and efficient dsp algorithms. the c language preprocessor is an implementation of the ansi standard which includes support for arbitrary text file inclusion, macro definition and expansion, and conditional compilation. the preprocessor exists as a separate program and may be used as a general-purpose macro preprocessor. the compiler control program, g56k , is the standard compiler interface used to control the sequence of operations required to compile a program. this control program allows the user to select a wide variety of control options which affect the four compilation phases f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 1-2 motorola dsp56000 family optimizing c compiler users manual motorola overview preprocessing, compiling, assembling, and linking. the control program parses the command line options, and invokes the required sub-programs on the specified files. figure 1-1. motorola software development system c source files coff assembler coff linker coff librarian ansi c compiler run56 sim56000 gdb56 user defined libraries ansi c library ansi c library ads56000 execution devices users input files asm files target system ansi c preprocessor srec optional user provided prom programmer utility conversion f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . overview motorola introduction 1-3 object files are stored using the coff format. coff stands for common object file format. utilities such as cldinfo and cldlod may be used to gain visibility into object files. 1. given a list of c source files from the user (see figure 1-1) and options to apply to those files, the control program runs each file through the c preprocessor and the c compiler. the compiler creates individual assembly language source files for each c source file provided on the command line. 2. the control program then sends the compiler output from these files to the assembler, in addition to any assembly language files specified by the user on the g56k command line. 3. the assembler output is routed to the linker for final processing. the linker tries to resolve all unresolved link-time symbols with the standard (and any explicitly requested) c libraries. the coff linker object file output may then be directed to any of several execution devices. notice that the assembler can also be used to create library files which can be included in a user defined library. 4. the execution devices shown in figure 1-1 are: a. run56 which allows the dsp56000/1 code (in coff format) to be executed on the host computers cpu b. gdb56 which is a debugging tool for trouble-shooting the compiled application c. sim56000 which is a complete dsp56000/1 simulator that can be used to execute the compiled application (in either coff format or .lod file format) and allow examination of registers and memory d. ads56000 is the development system hardware that can then be used to load and execute the compiled application (in either coff format or .lod file format) on the ads development system e. the target system shown is the users custom dsp system. note: the three execution devices in the shaded boxes are not part of the c compiler software. the coff linker output can be used by these execution devices directly. the conversion utility srec (see figure 1-1) can be used to convert the executable file from the coff linker to a suitable format for prom burning. these proms can then be used on the ads development system or the users target system. the prom programmer, ads development system and users target system are not part of the dsp56kcc compiler. the dsp56000 family represents a departure from more conventional architectures on which other implementations of the c language are based. also, the nature of dsp applications dictates that a greater measure of control be provided to the programmer in specifying the constraints of the run-time environment. for these reasons, the components f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 1-4 motorola dsp56000 family optimizing c compiler users manual motorola error codes of the development system include options for handling separate memory spaces, stack initialization, chip operating modes and other issues. the purpose of this manual is to: 1. provide detailed installation procedures for both unix based systems and ms-dos based systems. this manual explains how to install and operate the dsp56kcc ansi c compiler development system in a hosted environment. 2. provide an overview of the compiler operation. it also includes information on combining c modules with assembly language programs and advanced topics pertaining to compiler run-time characteristics. 3. provide reference information on compiler options, ansi library routines, and utilities. this manual assumes a familiarity with the c programming language, with the host machine and operating system environment. it also assumes that the programmer understands how to create and edit c language source files on this host system. for more information on the c language and other dsp56000/1 development tools, see the references listed in appendix d. 1.2 error codes the error messages generated by the compiler are intended to be complete without additional explanation. since the compiler produces a detailed description of the problem rather than an error code, these error messages have not been reproduced in this manual. 1.3 notation the following notation will be used in this text. 1. a prompt is indicated in this manual by: c:\> 2. an example of an ms-dos directory name is: \usr\directory 3. the contents of an ms-dos directory are examined by entering: c:\> dir 4. the contents of an ms-dos file are displayed by entering: c:\> type file 5. the program hello.exe would be executed by the command line: c:\> hello f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . manual organization motorola introduction 1-5 1.4 manual organization installation details are provided in chapter 2, the compiler operation is described in chapters 3-6 and reference information is in chapter 3 and appendices a-e. the contents of each chapter and each appendix are described below. chapter 1, "introduction," describes the overall organization of the dsp56kcc compiler system. it also details the structure of this document, and conventions followed herein. chapter 2, "installation guide," describes the installation and organization of dsp56kcc. it details how to set up an operating environment on the host system by defining global environment variables and includes a step-by-step installation procedure. chapter 3, "control program options," discusses the four passes of the compilation process with particular attention to the functions of the compiler control program g56k. this chapter includes a list of the compiler invocation options along with example command lines for different memory and program configurations. chapter 4, "about g56k," provides information on the compiler run-time environment, including explanations of compiler register and memory usage, stack frame architecture, stack overflow checking, and defining/referencing of absolute memory locations. additionally, this chapter covers implementation issues such as data type sizes. chapter 5, "mixing c and assembly language," discusses the methods for using assembly language in conjunction with c language programs. it covers the inclusion of assembly language within a c source file and also describes linking assembly language modules with c modules and linking c modules with assembly language modules. chapter 6, "software-hardware integration," describes how to modify a programs run-time environment, how to write software to handle interrupts, and the setjmp/longjmp ansi library routines. appendix a, "programming support," provides a complete description and brief example for every ansi library subroutine distributed with the c compiler. appendix b, "dsp56000/dsp56001 instruction set and assembler directive summary," provides a brief overview of the assembly language instructions and assembler directives. appendix c, "utilities," provides dsp56000/1 assembler manual pages for each of the supporting programming utilities provided with the compiler. appendix d, "gnu debugger (gdb)," provides a description of software, hardware, support telephone numbers, and suggested reading for the c language and various areas of dsp. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 1-6 motorola dsp56000 family optimizing c compiler users manual motorola manual organization appendix e, "additional support," , provides information on how you can obtain further support from motorola and third party companies. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . installation on an ms-dos machine (80386 or 80486) motorola installation guide 2-1 chapter 2 installation guide 2.1 introduction this chapter describes installation on ms-dos and sun computers. two installation procedures are detailed for the sun. the first procedure uses the default location for the files. the second procedure allows the user to select the directory where the compilers files will be located. only one procedure is needed for ms-dos machines. the various parts of the compiler reside in a directory tree named dsp. the default location for the dsp directory tree is /usr/local on unix systems. if this default location is acceptable, then perform the standard installation; if it is not acceptable, then perform the alternate installation. the alternate installation procedure allows the user to install the dsp directory tree anywhere. 2.2 installation on an ms-dos machine (80386 or 80486) 1. insert the supplied floppy labeled disk 1 into floppy drive a:. 2. change to floppy drive a, with the command a:. 3. run the install program, install.exe. this installation program will ask questions about the computer being used and about where the compilers directory tree, dsp, is to reside. 4. add all new lines of code specified by install into the autoexec.bat file. the only difference between the standard and alternate installation procedure on the pc is whether or not the default output drive or default location is selected. if the defaults are not selected, an environment variable named dsploc must be set in the autoexec.bat file. the install program will provide directions. the lines to be added to the autoexec.bat file follow: f. termcap must be set to the current location of 2-2 motorola dsp56000 family optimizing c compiler users manual motorola installation on an ms-dos machine (80386 or 80486) h. dsploc need only be set if the default output drive or the default location is not chosen. if dsploc is set, it must be set to the location of the dsp directory tree. for example, if the user installed the compilers directory tree dsp in the directory d:\usr\mydir, then dsploc would need to be set as d:\usr\mydir: set dsploc= standard installation on a sun motorola installation guide 2-3 as an example, the following line in the autoexec.bat file will enable an 8mb swap file with automatic deletion of the swap file: set dos4gvm=deleteswap virtualsize#8192 the following line will use f drive for the swap space instead of the current drive. set dos4gvm=deleteswap swapname#f:\big.swp 8. there is a read.me file included in the package and it should be read for any recent changes in the installation on compiler itself. 2.3 standard installation on a sun 1. insert the supplied first floppy diskette into the tape drive. 2. login as root . 3. enter the command: cd /usr/local. 4. enter the command: bar xzvf /dev/rfd0. if the floppy drive must be accessed via a different device file than rfd0 , then use the appropriate device for your system. 5. logout. 6. make sure that all users add /usr/local/dsp/bin to their path. this enables the shell to find the control program g56k and other programs in the dsp56kcc distribution package. swapname the swap file name for the memory manager uses for the swap space. default is dos4gvm.swp on the current drive. deleteswap specifies that the swap file should be deleted after memory management. virtualsize the size of the virtual memory space. default is 16mb. the value should be entered as numeric value of kb. minmem the minimum amount of ram managed by the memory manager. default value is 512kb. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 2-4 motorola dsp56000 family optimizing c compiler users manual motorola alternateinstallationonasun 2.4 alternate installation on a sun 1. insert the supplied first floppy diskette into the floppy drive. 2. login as root , or as yourself, if access permissions allow. 3. inside the shell, use the command cd to go to the directory where the compilers dsp directory tree is to reside. for this example, assume that the compiler is to be installed in /usr/mydir (referred to by overview motorola control program options 3-1 chapter 3 control program options 3.1 overview program g56k is the control program for motorolas optimizing c compiler for the dsp56000/dsp56001 family of digital signal processors. the program g56k automates control of the four c compiler phases C preprocessing, compiling, assembling, and linking. the program g56k utilizes a command syntax similar to those adopted by unix utilities. the g56k syntax is: g56k [options] files where: 1. [options] is one or more of the options found in this chapter. one difference between g56k and unix-style utilities is that the combination of multiple single character options is not allowed. for example, -o -g instructs the compiler to generate an optimized executable with source level debug information, whereas -og, which is acceptable to unix-style compilers is not acceptable to g56k. 2. file ? is the file(s) to be processed. program g56k accepts input filenames suffixed with .c (ansi c source files), .i (preprocessed c source files), .asm (dsp56000/dsp56001 assembly code), and .cln (coff link files). the control program processes each file according to this suffix. the g56k output is controlled by the specific set of command line options provided. for instance, if no command line arguments are provided, the compiler will generate a coff load file a.cld. if the -c option is invoked, the compiler will generate a coff link file suffixed with .cln. a complete description of the command line options, with examples, is provided in section 3.2. note: it is strongly recommended that g56k always be used to invoke the c compiler utilities rather than individually executing them. a standard directory search list is consulted by g56k for: f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-2 motorola dsp56000 family optimizing c compiler users manual motorola overview 1. each of the four executables, i. mcppC the c compiler preprocessor, j. g56-cc1C the c compiler/optimizer, k. alo56 Cthe assembly language optimizer, l. asm56000 C the dsp56000/dsp56001 assembler, m. dsplnk C the dsp56000/dsp56001 linker. 2. start-up file, crt056*.cln . where * is a single character (x, y or l; i.e., x data memory, y data memory or long data memory) to denote the memory model. 3. ansi c library, lib56c*.clb . where * is a single character (x, y or l) to denote the memory model. this standard directory search list for unix systems is: 1. /usr/local/dsp/bin/ 2. /usr/local/dsp/lib/ 3. /lib/ 4. /usr/lib/ 5. . / the standard ms-dos directory search list for the path set up in section 2.2 is: 1. c:\dsp\bin 2. c:\dsp\lib 3. c: 4. c:\dos 5. other directories in the path name note that if the environment variable dsploc is set, the value of dsploc will be substituted for 1 and 2 above. table 3-1 lists all the user selectable options used by g56k . they are grouped to show what program uses each option. all of these options are described in detail later in this chapter; however, these lists provide an overview of what options are available. notice that there is a -v option listed under both g56k command options and preprocessor phase options. this is actually the same option but it is used by these two programs in different ways (see section 3.2 and section 3.2.1). under compile phase options, there is a group of -f options; these are the machine independent optimization options whereas the -m options below are the optimization f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . overview motorola control program options 3-3 options specific to the dsp56000/1. although these various methods of optimization are all effective, they may have side effects which are undesirable in specific cases, e.g. an optimization option may increase code speed at the cost of increased memory usage. it is often preferable to trade memory space for speed, but in cases where the extra memory space is not available, this particular optimization could not be used. the various compiler phases will report errors; however, the user has the option to turn off all warnings using -w and can enable additional warnings individually or as a group using -wall . the warnings which are not enabled by -wall are those listed below -wall in table 3-1, dsp56kcc options, on page 3-4. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-4 motorola dsp56000 family optimizing c compiler users manual motorola overview table 3-1. dsp56kcc options g56k command line options preprocesso rphase options assemble phase options link phase options compile phase options -bdirectory -bprefix -o file -v -c -dmacro -dmacro=d efn -e -idir -i- -i file -m -mm -nostdinc -pedantic -p -v -umacro -wcomment -wtrigraphs -asm string -c -crt file -j string -llibrary -r mapfile -alo -fno-opt -fno-peephole -fno-strength-reduce -fno-defer-pop -fforce-addr -finline-functions -fcaller-saves -fkeep-inline-functio ns -fwritable-strings -fcond-mismatch -fvolatile -ffixed-reg -g -o -m56002 -mno-dsp -mno-do-loop-gener ation -mno-linv-plus-biv-pr omotion -mp-mem-switchtabl e -mstack-check -mx-memory -my-memory -ml-memory -pedantic -q -s -w -w -wimplicit -wreturn-type -wunused -wswitch -wall -wshadow -wid-clash-len -wpointer-arith -wcast-qual -wwrite-strings f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . g56k command line options motorola control program options 3-5 3.2 g56k command line options the default options are: 1. use strict ansi syntax. 2. perform all machine dependent and independent optimizations. 3. use trigraphs. 4. locate data in the y data memory space. -bdirectory add directory to the standard search list and have it searched first. this can also be accomplished by defining the environment variable g56_exec_prefix . note that only one additional directory can be specified and that the -b option will override the environment variable. example 3-1. lib56cy.clb to test a new version of the ansi c library, lib56cy.clb, which is installed as \dsp\new\lib56cy.clb use: c:\> g56k -b\dsp\new\ file.c -o file.cld example 3-2. g56_exec_prefix using the g56_exec_prefix environment variable to have the same effect as example 3-1, include in the .cshrc or .login files: set g56_exec_prefix=c:\dsp\new\ and then execute: c:\> g56k file.c -o file.cld example 3-3. test new version of dsp56000/1 c to test a new version of the dsp56000/1 c preprocessor before permanent installation, install a new mcpp program as c:\tmp\new\mcpp and then execute: c:\> g56k -bc:\tmp\new\ testfile.c -bprefix direct g56k to search for compilation phases, start-up files and libraries whose names are prefixed with the word prefix. note that only one additional prefix can be specified. this is very similar to the -b option. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-6 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options example 3-4. test new version of ansi c library test a new version of the ansi c library, lib56cy.clb, installed as c:\dsp\lib\new-lib56cy.clb. c:\> g56k -bnew- file.c -o file.cld example 3-5. test new version of dsp56kcc test a new version of the dsp56kcc preprocessor before permanent installation. install the new mcpp program as c:\dsp\bin\new-mcpp and c:\> g56k -bnew- testfile.c -o file select file as the output file. this applies to all phases of the compiler. when the -o flag is not in use, the following file names are used by the compiler as the default output file names depending upon the compile options as follows: where stdout is standard output and prints to the console. example 3-6. generate a preprocessed file only generate a preprocessed file (do not invoke the compiler, assembler or linker) and put the results in file.i . c:\> g56k -e file.c -o file.i example 3-7. compile file.c / generate executable ouput file compile file.c and generate the executable output file, fft.cld . if an output name is not given, the default file name is a.cld . c:\> g56k file.c -o fft.cld -e (preprocess only) stdout -s (compile only) foo.asm -c (no linkage) foo.cln complete process a.cld f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . g56k command line options motorola control program options 3-7 -v verbose mode. the compiler control program announces to stderr all commands that it attempts to execute for each phase of the compilation process. this command is also used by the preprocessor to print the software version information. if the -e option is selected, -v will only enable the verbose mode, otherwise it will enable the verbose mode and print the version information. 3.2.1 preprocessor phase options the options listed below control the c preprocessor, which is run on each c source file before actual compilation. some options described only make sense when used in combination with the -e option (preprocess only), as the requested preprocessor output may be unsuitable for actual compilation. the default option is to use ansi c syntax. for example, if the -idir option is not specified then ansi specifies that the current working directory will be searched first for user defined include files. -c tell the preprocessor not to discard comments. this option is only valid when used in conjunction with the -e option. example 3-8. preprocess program foo.c this example preprocesses a simple program, foo.c, without discarding comments. c:\> type foo.c /* * this comment wont be deleted. */ main() { printf("hello, dsp56000\n"); } c:\> g56k -e -c foo.c # 1 "foo.c" /* * this comment wont be deleted. */ main() { printf("hello, dsp56000\n"); } f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-8 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options -dmacro define the preprocessor macro macro with a constant value of 1. this is equivalent to making macro a constant set to one. example 3-9. compile and run program dsp.c compile and run a simple program, dsp.c, and enable or disable a printed message depending on the macro definition given at the command line. c:\> type dsp.c #include g56k command line options motorola control program options 3-9 example 3-10. program dsp.c the program dsp.c uses the macro from_command_line which prints a message to the standard output using a message code given on the command line. c:\> type dsp.c #include 3-10 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options example 3-12. cpp output the cpp output can be saved into file " foo.i " by using the -o option. c:\> type foo.c #define delay 1000 main() { int cnt = delay; while(cnt--); } c:\> g56k -e foo.c -o foo.i c:\> dir foo.c foo.i c:\> type foo.i #1"foo.c" main() { intcnt=1000; while(cnt--); } -idir the control line of the c source program of the form #include g56k command line options motorola control program options 3-11 a delay program foo.c uses delay constant delay which is defined in the include file, myinclude.h . the program uses the control line #include myinclude.h to include the definition of the constant delay . without any option the include file should be located in the currently working directory since it is not in the standard include directory. assuming that the include file myinclude.h is desired to be in the directory ./inc, the following sequence of the commands explains how the -i option is used to include the file myinclude.h in the ./inc directory with the control line #include myinclude.h in the foo.c program. example 3-13. -i option used to include the file myinclude.h c:\> dir foo.c inc/ c:\> dir inc myinclude.h c:\> type foo.c #include "myinc.h" /* this is the control line to include it */ main() { int cnt; cnt = delay; while(cnt--); } c:\> type inc\myinc.h #define delay 100 c:\> g56k -i.\inc foo.c c:\> dir a.cld foo.c inc/ -i- this option is always used in conjunction with the -i dir option and limits the file search to look for file inclusions #include filename , whereas -i dir alone directs c compiler to search the directory dir for both file inclusion 3-12 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options as an example, the sequence of the options -idira -i- -idirb directs c compiler to use both the directories dira and dirb for the file inclusion filename and dirb only for file inclusion < filename >. note: the -i- option inhibits the use of the current directory as the first search directory for #include filename . there is no way to override this effect of -i- . however, the directory which is current when the compiler is invoked can be searched by using -i . this is different from the preprocessors default search list, but it is often satisfactory. -i- does not inhibit the use of the standard system directories for header files. thus, -i- and -nostdinc are independent. a test program file.c is used to test a file operation fopen() which is, in this example, desired to be developed for a dsp56000/1 system. the file include g56k command line options motorola control program options 3-13 c:\> type mysys\stdio.h typedef struct file { /* file data structure to develop */ char name[10]; char buffer[1024]; } file; file *fopen(char *, char *); /* new function to develop */ c:\> g56k -i.\inc -i- -i.\mysys -e file.c # 1 file.c # 1 .\mysys\stdio.h1 typedef struct file { char name[10]; char buffer[1024]; } file; file*fopen(char*,char*); # 1 file.c2 # 1 .\inc\cnt.h1 # 2 file.c2 main() { int delay = 25; file *fp; fp = fopen (myfile, w); while (--delay); } notice that the file inclusion cnt.h is from the directory ./inc as shown in the line # 1 .\inc\cnt.h 1 and the file inclusion 3-14 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options example 3-15. program greeting.c prints message the program greeting.c prints a simple message using the macro message. the file macros.c contains the macro definition, i.e. the actual message. the only role of the file macros.c is to provide the macro definitions and will not affect any other code or data segments. c:\> dir macros.c greeting.c c:\> type macros.c #define message "hello, world." c:\> type greeting.c #include g56k command line options motorola control program options 3-15 example 3-16. program big.c the program big.c, which prints the larger of two integers, uses the macro greater (x,y) which is defined in the file greater.h . a command line output using the -m option can be used for makefile utilities. for more information on how to use this dependency check the make utility information in any unix utility manual. c:\> dir big.c greater.h c:\> type big.c #include 3-16 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options c:\> type greater.h #define greater(x,y) ((x)>(y)?(x):(y)) c:\> g56k -mm big.c big.o : big.c greater.h c:\> dir big.c greater.h makefile text c:\> type makefile a.cld : big.o g56k big.o big.o : big.c greater.h g56k -c -o big.o big.c c:\> make g56k -c -o big.o big.c g56k big.o c:\> run56 a.cld big:20 -nostdinc do not search the standard system directories for file inclusions. only the directories specified with -i options (and the current directory, if appropriate) are searched. using both -nostdinc and -i- options, all directories from the search path except those specified can be eliminated. example 3-18. test program test.c a test program, test.c, is used to test a new version of the function printf() which is declared in a new header file inc/stdio.h . the directive #include g56k command line options motorola control program options 3-17 #include 3-18 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options example 3-19. -v option the -v option is selected using the control program g56k. the version numbers for g56k, mcpp and g56-cc1 are printed. this information is showing the commands that the control program invokes along with the selected options. in this case it is showing the default options plus the -v option. however, the user should not invoke these programs independently but should always use the control program to invoke them. c:\> dir foo.c c:\> g56k -v foo.c g56k version motorola version: g1.11 -- gnu 1.37.1 c:\dsp\bin\mcpp -v -undef -d__y_memory -trigraphs -$ -d__strict_ansi__ -d__dsp56k__ -d__optimize__ foo.c cca00527.cpp gnu cpp version 1.37.1 c:\dsp\bin\g56-cc1 cca00527.cpp -ansi -fstrength-reduce -quiet -dumpbase foo.c -o -version -o cca00527.asm gnu c version 1.37.1 motorola dsp56000/1 motorola version: g1.11 compiled by gnu c version 1.37.1. default target switches: -mdsp -mlinv-plus-biv-promotion -mdo-loop-generation -my-memory -mcall-overhead-reduction -mrep -mreload-cleanup -mnormalization-reduction c:\dsp\bin\asm56000 -c -b foo.cln -- cca00527.asm c:\dsp\bin\dsplnk -c -b a.cld --c:\dsp\lib\crt056y.cln foo.cln -l c:\dsp\lib\lib56cy.clb c:\> dir a.cld foo.c -umacro undefine macro macro. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . g56k command line options motorola control program options 3-19 example 3-20. debug flag an application program, test.c, is being tested and some portions of the code need to be debugged. the flag debug may be turned on or off through the command line with the -d and -u options respectively. this flag can then be used inside the program to enable/disable debugging features within the program. c:\> dir debug.c c:\> type debug.c #include 3-20 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options foo.c:4: warning: /* within comment foo.c:4: warning: /* within comment c:\> g56k foo.c -wtrigraphs warn if any trigraphs are encountered (trigraphs are sequences of three characters which are replaced by a single character. these trigraph sequences enable the input of characters that are not defined in the invariant code set as described in iso 646:1983, which is a subset of the seven-bit ascii code set.). 3.2.2 compile phase options the default options are: 1. perform all machine dependent and independent optimizations. 2. do not run the assembly language optimizer (alo56). 3. do not generate debugging information. 4. locate data only in the y data memory space. -alo run the assembly language optimizer on the assembly language output of g 56-cc1. this improves the utilization of parallel moves. -fno-opt disable all optimizations. -fno-peephole disable the peephole portion of optimization. -fno-strength-reduce disable the optimizations of loop strength reduction and elimination of iteration variables as well as dsp56000/1 specific looping optimizations ( do instruction usage, etc.). f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . g56k command line options motorola control program options 3-21 -fno-defer-pop by default, the compiler will try to defer (delay) restoring the stack pointer upon the return of a function call. the purpose of deferring restoration of the stack pointer is to reduce code size and decrease execution time; however, the stack penetration may increase (see the dsp56000 family manual for information on stack overflow). examples of function calls that will not incur deferred pops whether or not the -fno-defer-pop option is specified are: ? calls as function arguments ? calls in conditional expressions ? calls inside a statement expression -fforce-addr force memory address constants to be copied into registers before doing arithmetic on them. the code generated with this option may be better or it may be worse depending on the source code . this option forces memory addresses into registers which, in turn, may be handled as common sub-expressions. -finline-functions attempt to insert all simple functions in-line into their callers. the compiler heuristically decides which functions are simple enough to merit this form of integration. if all calls to a given function are inserted, and the function is declared static, then the function is no longer needed as a separate function and normally is not output as a separate function in assembly code. -fcaller-saves enable values to be allocated in registers that will be overwritten by function calls by emitting extra instructions to save and restore the registers around such calls. such allocation is done only when it seems to result in better code than would otherwise be produced. -fkeep-inline-functions output a separate run-time callable version of the function even if all calls to a given function are integrated and the function is declared static. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-22 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options -fwritable-strings store string constants in the writable data segment without making them unique. this is for compatibility with old programs which assume they can write into string constants. writing into string constants is poor technique; constants should be constant. -fcond-mismatch allow conditional expressions with mismatched types in the second and third arguments. the value of such an expression is void. -fvolatile consider all memory references through pointers to be volatile. -ffixed-reg treat the register named reg as a fixed register; generated code should never refer to it (except perhaps as a stack or frame pointer). legal values for reg are: r1, r2, r3, r4, r5, r7 this flag should be used sparingly as it can have devastating results on the code generated. example 3-22. r4 reserve r4 for later special purpose. c:\> g56k -o -ffixed-r4 file.c -o file.cld warning c code that utilizes library code can produces non-deterministic results, as the libraries have been written to utilize the complete set of registers. -g produce coff debugging information for gdb56, the gdb cross debugger for the dsp56000/dsp56001. a key feature afforded by the use of the gnu c compiler (g56k) teamed with the gnu source level debugger (gdb56) is that the programmer is allowed to generate optimized code with debug information (select options -g -o ) making it possible for the programmer to debug optimized code directly. due to the optimizations performed, it is possible that variables will not be defined (unused variable f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . g56k command line options motorola control program options 3-23 elimination), statements may not be executed (dead code elimination), and code may be executed early (code motion). this is a partial list of the oddities that may be encountered when debugging optimized code. however, the improved code performance due to optimization normally out weighs the problems encountered. example 3-23. program foo.c the program foo.c has a bug. in this application the line i *= 2 should be i += 2. in order to use the symbolic debugger, gdb56, to locate this problem, the -g option is used when foo.c is compiled. this causes the compiler to generate additional information used by the debugger. c:\> ls foo.c c:\> type foo.c main() { inti=100; i*=2; } c:\> g56k -g foo.c c:\> dir a.cld foo.c c:\> gdb56 a.cld -o perform machine dependent and independent optimizations. this is the default mode of the compiler. invoking the compiler with the optimizer may cause compile times to increase and require more system memory. invoking the compiler without the optimizer should be done only when the programmer requires additional flexibility while debugging code. an example of such flexibility includes the ability to assign new values to declared c variables. additionally, non-optimized code takes register usage clues from the storage class specifier register, something not done with the optimizer invoked. disabling the optimizer is done via -f options listed above. -m56002 enables the generation of dsp56002 instructions. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-24 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options -mno-dsp-optimization disables all motorola optimizer enhancements. -mno-do-loop-generation disable do instruction usage by optimizer. -mno-biv-plus-linv-promotion disable the promotion of address expressions to address registers within loops. this optimization transforms array base address plus induction variable expressions into auto-increment/decrement style memory references. -mp-mem-switchtable forces the compiler to locate all switch tables in p memory. -mstack-check generate extra run-time code to check for run-time stack collision with the heap. this option causes run-time execution times to increase dramatically. -mx-memory direct the compiler to locate data in the x data memory space. memory modes cannot be mixed, i.e. only one of -m x-memory , -m y-memory or -m l-memory may be selected. example 3-24. -mx-memory option an application is programmed to utilize only the dsp56001 x data memory space and therefore must be compiled using the -mx-memory option. c:\> ls x.c c:\> type x.c void function(int a, int b); int x; main() { int arg1,arg2; function(arg1, arg2); } void function(int a, int b) { x=a+b; f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . g56k command line options motorola control program options 3-25 } c:\> g56k -s -mx-memory x.c c:\> dir x.asm x.c -my-memory direct the compiler to locate data in the y data memory space. this is the default memory mode. memory modes cannot be mixed, i.e. only one of -m x-memory , -m y-memory or -m l-memory may be selected. -ml-memory direct the compiler to locate data in the l data memory space. this has 2 side effects. 1. a performance increase for 48-bit data code (long, float, and double). 2. this requires that the x and y memory spaces be evenly populated. memory modes cannot be mixed, i.e. only one of -m x-memory , -m y-memory or -m l-memory may be selected. -pedantic issue all the warnings demanded by strict ansi standard c; reject all programs that use forbidden extensions. without this option, certain gnu extensions and traditional c features are supported. with this option, they are rejected. valid ansi standard c programs will compile properly with or without this option. -pedantic does not cause warning messages for use of the alternate keywords whose names begin and end with __ . -q direct the compiler to execute in verbose mode. -s compile to dsp56000/1 assembly code with the original c source lines as comments but do not assemble. the assembly language output is placed into a file suffixed .asm. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-26 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options example 3-25. generate an optimized assembly language file (test.asm) of the c source program (test.c). c:\> dir test.c c:\> type test.c #include g56k command line options motorola control program options 3-27 the compiler sees only the calls to setjmp . it cannot know where longjmp will be called; in fact, a signal handler could call it at any point in the code. as a result, a warning may be issued even when there is no problem because longjmp cannot be called at the place which would cause a problem. a function can return either with or without a value. (falling off the end of the function body is considered returning without a value.) for example, this function would evoke such a warning: foo (a) { if(a>0) return a; } spurious warnings can occur because gnu cc does not realize that certain functions (including abort and longjmp) will never return. ? an expression-statement contains no side effects. example 3-27. -w extra warning messages are wanted to help find potential problems in a test function, foo(), which is programmed to return a value only if a > 0. c:\> dir foo.c c:\> type foo.c int foo(int); main() { int i; foo(i); } int foo(a) { if(a > 0) return a; } c:\> g56k -w foo.c foo.c: in function main: foo.c:4: warning: i may be used uninitialized in this function foo.c: in function foo: foo.c:11: warning: this function may return with or without a value f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-28 motorola dsp56000 family optimizing c compiler users manual motorola g56k command line options -wimplicit warn whenever a function is implicitly declared. example 3-28. -wimplicit the function foo() is declared implicitly in the program foo.c, the -wimplicit option will generate a warning message for this situation. c:\> dir foo.c c:\> type foo.c main() { foo(); } int foo(){} c:\> g56k -wimplicit foo.c foo.c: in function main: foo.c:3: warning: implicit declaration of function foo c:\> dir a.cld foo.c -wreturn-type warn whenever a function is defined with a return-type that defaults to int. also warn about any return statement with no return-value in a function whose return-type is not void. example 3-29. -wreturn-type the function foo() is declared as a function that should return an integer but in this case does not return an integer. the -wreturn-type option generates a warning message in this situation. c:\> dir foo.c c:\> type foo.c int foo(), main(); int main() { return foo(); } f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . g56k command line options motorola control program options 3-29 int foo(){} c:\> g56k -wreturn-type foo.c foo.c: in function foo: foo.c:6: warning: control reaches end of non-void function c:\> dir a.cld foo.c -wunused warn whenever a local variable is unused aside from its declaration, whenever a function is declared static but never defined and whenever a statement computes a result that is explicitly not used. example 3-30. -wunused the file foo.c contains an undefined static function, unused local variable, and a dead statement. the -wunused option will issue warnings to indicate these situations. c:\> dir foo.c c:\> type foo.c static int foo(); main() { int x; 2+3; } c:\> g56k -wunused foo.c foo.c: in function main: foo.c:5: warning: statement with no effect foo.c:4: warning: unused variable x foo.c: at top level: foo.c:1: warning: foo declared but never defined c:\> dir a.cld foo.c f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-30 motorola dsp56000 family optimizing c compiler users manual motorola assemble phase options -wswitch warn whenever a switch statement has an enumeration type of index and lacks a case for one or more of the named codes of that enumeration. (the presence of a default label prevents this warning.) case labels outside the enumeration range also provoke warnings when this option is used. -wall all of the above -w options combined. the remaining -w options described below are not implied by -wall because certain kinds of useful macros are almost impossible to write without causing those warnings. -wshadow warn whenever a local variable shadows another local variable. -wid-clash-len warn whenever two distinct identifiers match in the first len characters. this may help prepare a program that will compile with certain obsolete compilers. -wpointer-arith warn about anything that depends on the sizeof a function type or of void. gnu c assigns these types a size of 1, for convenience in calculations with void * pointers and pointers to functions. -wcast-qual warn whenever a pointer is cast so as to remove a type qualifier from the target type. for example, warn if a const char * is cast to an ordinary char *. -wwrite-strings give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will generate a warning. these warnings help at compile time to find code that can try to write into a string constant, but only if const in declarations and prototypes have been used carefully. 3.3 assemble phase options this group of assemble phase options is the sub-set of the available assembler options that are compiler oriented (see the motorola dsp56000 macro assembler reference manual for a complete option list). the default option is to add to the standard search list the directory that the c compiler writes its output into and then search that directory first. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . link phase options motorola control program options 3-31 -asm string pass the argument string directly to asm56000, the dsp56000/1 assembler. example 3-31. pass a single option to the assembler. c:\> g56k -asm -v file.c example 3-32. pass multiple options to the assembler. c:\> g56k -asm -v -os,cre file.c -c compile and/or assemble the source files, suppressing the link phase. this option generates corresponding output files suffixed .cln. affected input files are suffixed with .c and .asm. 3.4 link phase options the options listed below control the link phase of the compilation process. this group of link phase options is the sub-set of the available linker options that are compiler oriented (see the motorola dsp56000 linker/librarian reference manual for a complete option list). the -crt and -l options locate the file provided as an argument by searching a standard list of directories, see section 3.1, overview. for this directory list. the default option is to add the c compiler output directory into the standard search list and search that directory first. -crt file replace the default start-up file ( crt056.cln ) with file . g56k searches the standard list of directories to find the start-up file. in addition, any directory defined using the -b option or the g56_exec_prefix environment variable will be searched. for additional information, see chapter 6. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-32 motorola dsp56000 family optimizing c compiler users manual motorola link phase options example 3-33. -crt file compile the c program foo.c with the crt0 file crt.asm. notice that the crt0 file crt.asm should be assembled before use since the option -crt takes .cln file not .asm file. c:\> dir crt.asm foo.c c:\> g56k -c crt.asm c:\> dir crt.cln crt.asm foo.c c:\> g56k -crt crt.cln foo.c -j string pass the argument string directly to dsplnk, the dsp56000/1 linker. example 3-34. pass a single option to the linker. c:\> g56k -j -v file.c example 3-35. pass multiple options to the linker. c:\> g56k -j -v -i file.c -llibrary search the standard list of directories for a library file named liblibrary.clb . the linker automatically expands library from the option command into liblibrary.clb and uses this file as if it had been specified precisely by name. example 3-36. -llibrary compile the source code using the special dsp application library. searching the standard list of directories for a library named libdspaps.clb . c:\> g56k -o file.c -ldspaps -r ctlfile search the standard list of directories for the memory control file ctlfile to be passed as an argument to the dsp56000/1 relocatable linker. this control file will be used as a table to locate object files sections to be linked. for more detailed information, see the -r options and the section on memory control file in the motorola linker/librarian reference manual. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . link phase options motorola control program options 3-33 compile the source code main.c and data.c with the memory configuration described in the control file map.ctl . notice that the section main_c of the program main.c is located at the memory address p:$3000 and the section of data_c of the data data.c is located at the memory address y:$5000 . see chapter 5 for detailed information on the in-line assembly code ( _ _ asm( ... ) ). example 3-37. -r ctlfile c:\> type map.ctl sectionmain_c p:$3000 section data_c y:$5000 c:\> type data.c intdata=0x1;/*testvalue*/ c:\> type main.c extern int data; main() { int i; for(i=0;i<10;i++) __asm(rol%0:=d(data):0 (data)); } c:\> g56k -j -mmap.map -r map.ctl main.c data.c c:\> type map.map ... y memory (default) start end length section 5000 5000 1 data_c ... p memory (default) start end length section 3000 3008 9 main_c ... f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 3-34 motorola dsp56000 family optimizing c compiler users manual motorola link phase options f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . data types and sizes motorola about g56k 4-1 chapter 4 about g56k 4.1 introduction the dsp56000 digital signal processors are designed to execute dsp oriented calculations as fast as possible. as a by-product, they have an architecture that is somewhat unconventional for c programming. because of this architecture, there are characteristics of the compiler, and the code generated by the compiler, that the programmer must understand in order to take full advantage of the dsp56kcc programming environment. all programmers, whether they are familiar with dsp or not, should understand the dsp56000 family architecture before attempting to program it in c. the following sections provide important information on data types, storage classes, memory and register usage, and other topics which will be useful to the dsp56000 application developer programming in c. 4.2 identifiers an identifier is defined as a sequence of letters, digits and underscore characters (_). the first character must be a letter or underscore. dsp56kcc identifier length limits are listed in table 4-1. 4.3 predefined preprocessor macro names dsp56kcc supports and expands all ansi defined macros and three additional non-ansi predefined macro names. table 4-2 lists the macros and their explanation. 4.4 data types and sizes due to the word orientation of the dsp56000 family (24-bit words), all data types are aligned on word boundaries. this has several side effects, one of which is that s izeof (char) is equal to sizeof(int). table 4-1. identifier length limits identifier storage class length global/static (external linkage) 255 auto unlimited f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 4-2 motorola dsp56000 family optimizing c compiler users manual motorola data types and sizes 4.4.1 integral data types the type char, unsigned char, signed char, short int, int, long int and the enumerated types comprise the integral data types. the type sizes and ranges are defined in table 4-3. table 4-2. predefined macro list and explanation macro ansi explanation _ _line_ _ yes the line number of the current source line (a decimal constant). _ _file_ _ yes the name of the source file (a character string). _ _date_ _ yes the compilation date (a character string of the form mmm dd yyyy e.g., jul 22 1991). _ _time_ _ yes the compilation time (a character string of the form hh:mm:ss). _ _stdc_ _ yes decimal constant 1, indicates ansi conformation. _ _dsp56k_ _ no decimal constant 1, indicates that code is being generated for the dsp56000/dsp56001 digital signal processor. _ _version_ _ no the version number of the compiler (a character string of the form d.dd.d). _ _include_level_ _ no decimal constant, indicates the current depth of file inclusion. table 4-3. integral data type sizes and ranges data type size (words) min value max value char 1 -8388608 8388607 unsigned char 1 0 0xffffff short 1 -8388608 8388607 unsigned short 1 0 0xffffff int 1 -8388608 8388607 unsigned int 1 0 0xffffff long 2 -140737488355328 140737488355327 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . data types and sizes motorola about g56k 4-3 4.4.2 floating-point types in dsp56kcc, the c data types float and double are both implemented as single precision (see table 4-5). dsp56kcc does not implement the ieee std 754-1985 standard format for binary floating-point arithmetic. a description of the format and a comparison with the ieee standard follow. 4.4.2.1 floating-point format description figure 4-1 illustrates the floating -point format used in dsp56kcc. figure 4-1a shows that the exponent and mantissa occupy consecutive memory locations. figure 4-1b is in number line format and shows the fractional nature of the mantissa and the fact that, due to the nature of a fractional arithmetic mantissa, the numbers between -0.5 and +0.5 (except for zero) are not needed and are therefore reserved. figure 4-1c shows the range used by the exponent in this implementation. notice how this compares with the ieee implementation shown in table 4-6. figure 4-1d is a combined number line showing the range of numbers which can be represented in dsp56kcc. the mantissa $c00000 ( -0.5) is not included as the smallest negative floating-point number because the normalization routine automatically detects the two leading ones and decrements the exponent which, if at $003fff, will result in an underflow. therefore, the smallest negative mantissa has been set to $bfffff (-0.916). table 4-5 lists the specific floating-point format information for dsp56kcc and is a tabular version of the information in figure 4-1. table 4-4. floating-point data type sizes and ranges data type size (words) min value max value float 2 1.175494351e-38 3.402823466e+38 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 4-4 motorola dsp56000 family optimizing c compiler users manual motorola data types and sizes figure 4-1. mantissa and exponent data range of c floating point $000000 $ffffff positive mantissa negative mantissa $7fffff $800000 $400000 $3fffff $bfffff $c00000 -1.0000000 -0.5000003 0.0000000 0.5000000 0.9999997 0.4999997 -0.5000000 -0.0000003 $000001 0.0000003 reserved reserved mantissa value (hex) (decimal) exponent $7fffff $000000 $003fff $004000 2 -8192 2 8192 reserved exponent value (hex) (decimal) (a) floating-point data arrangement in memory (b) mantissa data range exponent mantissa floating-point data addr addr+1 (c) exponent data range e = $003fff m = $800000 e = $000000 m = $bfffff e = $000000 m = $400000 e = $003fff m = $7fffff largest negative number -1.00 x 2 +8192 -0.109 x 10 +2817 smallest negative number -0.5 x 2 -8192 -0.916 x 10 -2816 largest positive number +0.999 x 2 +8192 +0.109 x 10 +2817 smallest positive number +0.5 x 2 -8192 +0.916 x 10 -2816 ==== 0 note: e = exponent and m = mantissa (d) mantissa and exponent data range f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . data types and sizes motorola about g56k 4-5 4.4.2.2 comparison with ieee std 754-1985 standard since the ieee floating-point arithmetic standard is well publicized, it is useful to compare these two floating-point formats. the dsp56kcc floating-point format differs from the ieee standard primarily in its handling of floating-point exceptions. other differences are noted in table 4-6. conversion between the ieee standard format and dsp56kcc format is straightforward. as shown in table 4-6, the dsp56kcc mantissa precision is one bit less than the ieee single precision format. this is the result of using twos complement arithmetic in the dsp56000/dsp56001. the dsp56kcc exponent width is three bits more than the ieee double precision format. this provides an extremely large (approximately 100,000 db) dynamic range which eliminates exponent overflow for most applications. if exponent overflow occurs, the result is limited to the maximum representable floating-point number of the correct sign. if exponent underflow occurs, the result is limited to the minimum representable floating-point number, which is zero. although the dsp56kcc format does not provide the arithmetic safety offered by the ieee standard, it avoids extensive error checking and exceptions in favor of real-time execution speed and efficient implementation on the dsp56000/dsp56001. all exception conditions are handled in-line (i.e., an exception routine is not invoked) according to predefined rules. the reason for this is the fact that real-time systems must continue to process data non-stop. it is not possible to stop execution until the application program determines a solution to the problem and corrects it and so there is no choice but to provide an output with some amount of error should an exception occur. table 4-5. floating-point format description ieee format characteristic dsp56kcc value decimal value m*2 (e - ebias) mantissa 24-bit twos complement, normalized fractional mantissa. this gives a precision of approximately 7 decimal digits. a hidden leading 1 is not implementedinthisformat(seefigure4-1). largest positive mantissa $7fffff smallest positive mantissa $400000 floating-point zero mantissa $000000 smallest negative mantissa $bfffff largest negative mantissa $800000 reserved mantissas $000001 through $3fffff and $c00000 through $ffffff f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 4-6 motorola dsp56000 family optimizing c compiler users manual motorola data types and sizes note: 1. no distinct exponents are reserved for plus infinity, minus infinity, not-a-number (ieee nan), minus zero or denormalized numbers as is done in ieee format. 2. all reserved mantissas are illegal since they represent denormalized mantissas. 3. if the 15 th bit is set, exponent overflow has occurred. 4. if the 16 th bit is set, exponent underflow has occurred. exponent 14-bit exponent (unsigned integer, biased by ebias = $1fff). stored as a 24-bit unsigned integer with 10 leading zeros. the 14-bit exponent used by dsp56kcc provides a larger dynamic range than ieee double precision format. largest exponent (biased) $003fff = 2 +8192 smallest exponent (biased) $000000 = 2 -8192 reserved exponents $004000 through $ffffff table 4-6. comparison of dsp56kcc and ieee 754-1985 characteristic dsp56kcc format ieee format mantissa precision 23 bits 24 bits hidden leading one no yes mantissa format 24-bit twos complement fraction 23-bit unsigned magnitude fraction exponent width 16 bits (14 bits used) 8 bits 11 bits maximum exponent +8192 +127 (8 bit case) +1023 (11 bit case) minimum exponent -8191 -127 (8 bit case) -1022 (11 bit case) exponent bias +8191 +127 (8 bit case) +1023 (11 bit case) formatwidth 48bits 32bits(8bitcase) 64 bits (11 bit case) table 4-5. floating-point format description ieee format characteristic dsp56kcc value f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . data types and sizes motorola about g56k 4-7 one major difference is the use of affine arithmetic in the ieee standard versus the use of saturation arithmetic in the dsp56kcc format. affine arithmetic gives separate identity to plus infinity, minus infinity, plus zero and minus zero. in operations involving these values, finite quantities remain finite and infinite quantities remain infinite. in contrast, dsp56kcc format gives special identity only to unsigned zero. this format performs saturation arithmetic such that any result out of the representable floating-point range is replaced with the closest floating-point representation. since the dynamic range of this format is quite large, it is adequate for most applications. the ieee floating-point standard provides extensive error handling required by affine arithmetic, denormalized numbers, signaling not-a-numbers (nans) and quiet nans. it postpones the introduction of computational errors by using internal signaling and user traps to process each exception condition. computational errors will be introduced by the application program if the calculation is completed instead of aborting the program. the dsp56kcc format introduces computation errors when an exception occurs in order to maintain real-time execution. an error flag (l bit in ccr) is set to inform the application program that an exception has occurred. this bit will remain set until reset by the application program. the user can then eliminate the exception by algorithm modifications. 4.4.3 pointer types with dsp56kcc, all pointers are 16-bits (see table 4-7). when computing addresses with integer arithmetic, only the least significant 16-bits are relevant. rounding round to nearest round to nearest round to +/- infinity round to zero infinity arithmetic saturation limiting affine operations denormalized numbers no (forced to zero) yes (with min exp) exceptions divide by zero overflow negative square root invalid operation divide by zero overflow underflow inexact arithmetic table 4-6. comparison of dsp56kcc and ieee 754-1985 characteristic dsp56kcc format ieee format f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 4-8 motorola dsp56000 family optimizing c compiler users manual motorola register usage 4.5 register usage the dsp56000 family digital signal processor register set is shown in table 4-8. dsp56kcc uses all of the registers listed in table 4-8 with the exception of the m n address modifier registers. table 4-7. pointer size and range data type size (words) min value max value pointers 1 0 0xffff table 4-8. dsp56000 family processor registers data alu x n input registers x1, x0 (24-bits) y n input registers y1, y0 (24-bits) a n accumulator registers a2 (8-bits), a1, a0 (24-bits) b n accumulator registers b2 (8-bits), b1, b0 (24-bits) x input register x (x1 : x0, 48-bits) y input register y (y1 : y0, 48-bits) a10 input register a10 (a1 : a0, 48-bits) b10 input register b10 (b1 : b0, 48-bits) a accumulator a (a2 : a1 : a0, 56-bits) b accumulator b (b2 : b1 : b0, 56-bits) address alu r n address registers r0-r7 (16-bits) n n address offset registers n0-n7 (16-bits) m n address modifier registers m0-m7 (16-bits) f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . memory usage motorola about g56k 4-9 warning the mn address modifier registers are not used directly by dsp56kcc. some of these registers are implied whenever any address registers (r0 - r7) are referenced either in c library or in c. while assembly code can access and use these registers, the programmer must restore them to their previous state ($ffff) before returning control to dsp56kcc. failing to do so will cause unpredictable errors when dsp56kcc accesses these registers. the programmer is required to preserve any registers that are directly used in in-line and in out-of-line assembly language code (see chapter 5, mixing c and assembly language ). table 4-9 outlines the compilers usage of each register. 4.6 memory usage memory can be partitioned in several ways to provide high-speed operation and additional off-chip memory expansion. program and data memory are separate and the data memory is divided into two separate 24-bit wide memory spaces, x and y which can be combined to form a third 48-bit wide memory space, l. dsp56kcc generates code that can utilize x or y or l data memory. by default, the y memory model is selected. each separate memory model is supported by separate run-time libraries that have been written and compiled/assembled to handle each memory space. see chapter 3, control program options , for examples of the -mx-memory , table 4-9. dsp56kcc registers and usage register usage r0 frame pointer r6 stack pointer r1 - r5, r7 register promotion by the optimizer n0 - n7 code generator temporary m0 - m7 used by compiler; keep this as $ffff a 48-bit function return value. float, double, or long a1 24-bit and 16-bit return value. integer or pointer b, x, y 48-bit register promotion by optimizer x1, x0, y1, y0 24-bit register promotion by optimizer f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 4-10 motorola dsp56000 family optimizing c compiler users manual motorola memory usage -my-memory and -ml-memory command-line options for changing target memory spaces. by default, the compiler expects that each memory space is fully populated and several global c variables are defined (see chapter 6 software-hardware integration for information about customizing the memory configuration). figure 4-2 and figure 4-4 illustrate the default program and data memory configuration. the l memory model is recommended for best performance when long, float or double data types are extensively used. figure 4-2. default program memory configuration 4.6.1 activation record an activation record is the run-time representation of a c subroutine. a typical dsp56kcc activation record consists of the following elements and is illustrated in figure 4-3: 1. parameter data space. information passed to c subroutines is stored in a parameter data space which is similar to the local data space (see figure 4-3). however, the data is in reverse order and each parameter is referenced via a negative offset from the frame pointer. actual parameters are pushed onto the activation record in reverse order by the calling subroutine. 2. old frame pointer. once the called subroutine has completed execution, the frame pointer will be updated with this value. program area jsr fabort jmpf__start 31 3 2 1 0 jsr fabort jsr fabort jsr fabort ? ? ? interrupt table top_of_memory $ffff max f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . memory usage motorola about g56k 4-11 3. return address which is pushed on the dsps system stack high (ssh) register. this is the return address to the calling subroutine. the return address is not saved for subroutines that have been determined to be a leaf. a leaf subroutine is one that makes no subroutine calls. 4. local data space. the location of c variables that have a lifetime that extends only as long as the subroutine is active and that could not be explicitly promoted to register storage class by the optimizer. note: the frame pointer (r0) points to the first element in the local data space . 5. register spill and compiler temporary space. this area is utilized by the compiler to store intermediate results and preserve registers. note: the stack pointer (r6) points to the next available data memory location. figure 4-3. activation record each subroutine called puts a new copy of the subroutine activation record in the run-time stack and returning from the subroutine removes the activation record. the run-time stack is described in figure 4-4, default data memory configuration, on page 4-12. the variables shown in the bottom of the x or y memory option selected memory are controlled by the crt0 file. for example, the f_ _fp_shift variable is typically 23 words but can be changed by the user or may vary with later releases of this compiler. when the l memory option is selected, the heap, run-time stack, global/static data and data that is more than 24 bits in length will occupy one word in l memory i.e., 48-bit memory. 16-bit ? ? ? param n param 1 param 2 old frame pointer return address ( ssh ) local data register spill/temp area stack pointer r6 frame pointer r0 higher memory lower memory (1 word) (1 word) f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 4-12 motorola dsp56000 family optimizing c compiler users manual motorola memory usage and 24-bit data memory will occupy only one word in y memory. dsize is set by the linker and points to the top address of the global and static data. dsize is used by the crt0 file as the default initial stack pointer. the dynamic run-time stack growth is illustrated in figure 4-5. in this example, there is one activation record as execution of the sample c code begins. this activation record is pushed onto the stack and a new activation record is built with a dynamic link to the old frame pointer. when the function returns, the functions activation record is popped and the original activation record is restored to its original place on the stack. figure 4-4. default data memory configuration global/static data f_ _fp_shift (23 words) f_ _time (1 word) ferrno(1word) f_ _stack_safety (1 word) f_ _mem_limit (1 word) top_of_memory $ffff max f_ _break (1 word) f_ _y_size (1 word) heap run-time stack x or y memory option selected l (x:y) memory option selected predefined in crt0 16- and 24-bit data memory 48-bit memory heap run-time stack global/static data dsize f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . compiler naming conventions motorola about g56k 4-13 figure 4-5. run-time stack growth 4.6.2 global/static data by default, global and static data elements are located below the run-time stack and each element is referenced by a unique label that is known at compile-time (see chapter 6, software-hardware integration for additional information). 4.7 compiler naming conventions the compiler uses five different internal label formats and a special section naming format. these six separate formats simplify the procedures to combine hand written assembly language and c language statements. use of these formats also makes compiler generated assembly language listings easier to read. it is strongly recommended that the programmer avoid using labels with these formats. l# local labels. generally the targets of conditional and unconditional jumps. where # is a decimal digit. lc# string constant labels. the data memory location for c string constants, such as string constant. func_1 activation record main activation record main activation record main activation record dynamic link execution begins func_1 called func_1 complete main() { func_1(); } sample c code old frame pointer new frame pointer f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 4-14 motorola dsp56000 family optimizing c compiler users manual motorola subroutine call sequence 4.8 subroutine call sequence each time a c language subroutine is called, a strict calling convention is followed. the subroutine calling sequence is broken down into three sub-sequences that are strictly defined. the three sub-sequences are caller, callee and return sequence. note: this calling convention must be followed when writing in-line or out-of-line assembly language subroutines that call subroutines written in c. 4.8.1 caller sequence the caller portion of the subroutine calling sequence is responsible for: 1. pushing arguments onto the activation record (in reverse order). 2. actual subroutine call (jsr). 3. stack pointer adjustment. additional caller sequence when the subroutine called will return a structure: 4. allocate space in the callers activation record to store the return structure. f software support for aritmetic routines motorola about g56k 4-15 5. pass the return value address in accumulator a. 4.8.2 callee sequence during the initial portion of the subroutine calling sequence, the callee is responsible for: 1. saving return address (ssh) and the old frame pointer (r0). 2. updating frame and stack pointers. 3. saving the following registers, as required, b1, b0, x1, x0, y1, y0, r1 - r5 and r7. 4.8.3 return sequence during the final portion of the subroutine calling sequence, the callee is responsible for: 1. placing the return value in accumulator a. 2. testing the return value. this optimizes the case where function calls are arguments to conditional operators. additional callee sequence when the subroutine called will return a structure: 3. the return value is not passed in accumulator a. a copy of the return structure is placed into the space allocated in the callers activation record and pointed to by accumulator a. 4.9 software support for aritmetic routines the dsp56000 family architecture provides full hardware support for all 24-bit arithmetic operations, and partial support for 48-bit integer operations. support for all float/double and a portion of the 48-bit long is provided via special software library routines. these special library routines do not pass arguments to the routines according to the normal subroutine calling convention, instead, each routine has its arguments passed in the a and b accumulators and the result returned in the a accumulator, limiting the overhead involved in the call/return sequence. see table 4-10 and table 4-11 for a list of the routines supported in software. all but two of these routines restore all registers used except for the a accumulator which contains the result. the two routines that do not restore the registers are f_ _uldiv and f_ _ulmod which use the registers according the c standard shown in table 4-9. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 4-16 motorola dsp56000 family optimizing c compiler users manual motorola run-time safety 4.10 run-time safety dsp56kcc provides two methods for providing run-time memory utilization checks. the first method, memory allocation check, is automatic. the second method, run-time stack probes, is provided by selecting the command-line argument -mstack-check . 4.10.1 memory allocation checks memory allocation checks are provided during every call to the run-time library routines malloc, calloc and realloc. this automatic run-time check determines when the heap is about to collide with the run-time stack. when this occurs, the library routine returns a null pointer and sets the global variable errno to enomem . table 4-10. floating-point arithmetic routine source module fadd_ba fadd56.asm fsub_ba fsub56.asm fmpy_ba fmpy56.asm fdiv_ba fdiv56.asm fneg_a fneg56.asm fcmp_a fcmp56.asm table 4-11. 48-bit long integer arithmetic routine source module div_ba idiv56.asm mod_ba imod56.asm udiv_ba uidiv56.asm umod_ba uimod56.asm lmpy_ba l_mpy56.asm ldiv_ba l_div56.asm lmod_ba l_mod56.asm f_ _uldiv ulongdivmod.c f_ _ulmod ulongdivmod.c f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . optimization techniques implemented motorola about g56k 4-17 4.10.2 run-time stack checks if the programmer does not use malloc, calloc or realloc there is no automatic run-time collision check. a tool is provided to allow the programmer to check the code during debugging to see if a stack collision is likely to occur when the code is executing normally. by selecting the -mstack-check option on the command-line, the compiler is instructed to generate extra code to watch the stack and heap and detect when the run-time stack is about to collide with the heap. this may be important when writing code for embedded applications. when/if a stack collision occurs, the execution device, such as run56, will report that the collision occurred. note: all run-time libraries provided have been compiled/assembled without the stack checking option. thus it is possible to have a run-time stack/heap collision during execution of library routines. 4.11 optimization techniques implemented this section provides a brief overview of the optimization techniques currently implemented by dsp56kcc. dsp56kcc implements most machine-independent c language optimization techniques as well as machine-specific optimizations. by default, the control program g56k enables all levels of optimization (see chapter 3, control program options. ) 4.11.1 register promotion and lifetime analysis the compiler automatically identifies all variables that are candidates for promotion to the register storage class. using standard data flow techniques, lifetime analysis is performed to determine the best variables for promotion. when variable lifetimes do not overlap, more than one variable may be promoted to a single register. 4.11.2 common sub-expression elimination a common sub-expression, or cse, is created when two or more statements compute the same value. when cses are detected during data flow analysis, the compiler eliminates all but one of the computations and propagates the result. a classic example of a cse is the array element assignment from another array, array_1[index + 1] = array_2[index + 1]; where the expression index + 1 is the cse. this optimization is especially effective as cses become candidates for register promotion. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 4-18 motorola dsp56000 family optimizing c compiler users manual motorola optimization techniques implemented 4.11.3 constant propagation/folding propagation of constants is detected during data flow analysis and is simply the replacement of variable references with constant references when possible. for example, a=3; /* block of c code with no references to a */ func_call ( a + 709 ); becomes: /* block of c code */ func_call ( 3 + 709 ); constant folding is the replacement of run-time computations with compile-time computations. this optimization works well with constant propagation, as it exposes many new opportunities for folding. the example above would be further transformed to: /* block of c code */ func_call( 712 ); 4.11.4 dead code elimination during data flow analysis, the compiler detects when the results of c expressions are never used. when this is detected, the offending c statements are eliminated from code generation. this includes (1) the initialization of variables that are not referenced in the subroutine and (2) the situation where the variables next reference is not an assignment. to guarantee code generation, a volatile type specifier can be used to declare the left side of the expression. main() { int volatil ei=0,j=1; } the example above generates code to initialize variables i and j even though they are not used anywhere else. without the key word volatile, the optimizing c compiler will eliminate the two local variables because they are not referenced anywhere in the main function. 4.11.5 tail merging when two or more conditional blocks of code have similar ending sequences, the sections of code are rewritten to generate similar code only once. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . optimization techniques implemented motorola about g56k 4-19 this is a space saving optimization technique. for example: 4.11.6 strength reduction strength reduction replaces expensive operators with less expensive operators. this optimization method is very machine specific. for instance, a popular strength reduction for many compilers is to replace a multiplication by a constant with a series of shifts, additions and subtractions. the exact opposite is the case on the dsp56000/dsp56001, however since a series of left shifts is replaced with a single multiply by a constant power of 2. 4.11.7 loop invariant code motion loop invariant code motion is a method in which all c expressions that yield the same result each time through the loop are moved outside of the loop and are executed once prior to entering the loop. 4.11.8 hardware do loop instruction the dsp56000 family architecture provides a method in hardware to perform zero overhead looping via the do instruction. dsp56kcc may exchange the standard increment/compare/conditional jump sequence with a single do instruction (this is called do loop promotion) when the following conditions are met: 1. the body of the loop contains no subroutine calls, 2. the loop is entered from the top, i.e., no goto label entries. 3. no conditional exits from the loop are allowed. 4. the loops induction variable is only altered in the body of the loop once per iteration. please note that this includes any modifications to the induction variable within the actual for or while statement. if (a>b) { b=a; } func (a); if (a>b) { b=a; func (a); } else func (a); becomes f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 4-20 motorola dsp56000 family optimizing c compiler users manual motorola optimization techniques implemented 4.11.9 loop rotation loop rotation is the elimination of the code generated to check the loops entry conditions. when a loop fails to qualify for do loop promotion i.e., it does not meet the four conditions listed above, it will qualify for loop rotation if the length of the loop is known at compile-time, for example: for(i=0;i<10;i++) the loop created with this for statement will always be executed at least one time. therefore, the is i < 10? test does not have to be run the first time through the loop and as a result, can be eliminated during the first pass through the loop only. if the result of the first test cannot be predetermined then it cannot be eliminated. in the example below, the number of loops is a variable (and therefore cannot be predetermined) that may equal zero. for(i=0;i 4-22 motorola dsp56000 family optimizing c compiler users manual motorola optimization techniques implemented f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . in-line assembly code motorola mixing c and assembly language 5-1 chapter 5 mixing c and assembly language 5.1 overview in cases where the dsp56000/dsp56001 programmer requires direct access to the hardware or greater performance at the inner-loop of an algorithm, c programs can be mixed with assembly programs. this chapter describes two methods for combining c and assembly language source code. the first is the in-line method which is a convenient way to put assembly code into the compiler output via the non-ansi c directive _ _asm(). the second is the out-of-line method, a generic method of combining c and assembly language source files. warning before mixing c and assembly, read and understand chapter 4, about g56k, and the dsp56000 family manual. attempting to write programs for this dsp without knowledge of the chip and how the compiler utilizes registers, memory, defines labels, etc. may generate unsatisfactory results. however, with an understanding of the dsp architecture and how this implementation of c uses it, programming should be straightforward. labels which begin with a double underline (e.g., _ _asm () ) in this manual have a space between the double underlines to visually separate them. do not separate the leading double underlines with a space when coding them (i.e., code _ _asm () as __asm () ). 5.2 in-line assembly code in-line assembly code is assembly code that is inserted inside a c statement in a c source file. since assembly code is generated from this c statement directly, the c statement looks like assembly code in the c source and is referred to as i n-line assembly code. all of the assembly code to be generated is visible at the c source-level and it is often convenient to intermix assembly code with a c source program in this fashion. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-2 motorola dsp56000 family optimizing c compiler users manual motorola in-line assembly code typically, in-line assembly code is used when: 1. inserting a small amount of assembly code directly into the compiler output i.e., inner loop kernels. 2. writing fast assembly language routines to be called by c subroutines. this eliminates the need to manage data referencing, register saving and allocation, and function call/return overhead. the key word _ _asm is introduced as an extension to the ansi c language standard and this key word is used like a function call to specify the in-line assembly code generation rule discussed below. the in-line assembly statement syntax is: _ _asm (instruction_template: output_operands: input _operands: reg_save); where: 1. instruction_template is a string used to describe a sequence of assembly code instructions that are to be inserted into the compiler output stream. it may specify arguments, which are listed in output_operands and input_operands. it does this via a substring called an operand expansion string (oes). an oes starts with a %. oes and instruction_template interpretation is described in section 5.2.1. 2. output_operands are optional operands used to provide specific output information to the compiler. each output_operand string is separated by a comma and should begin with the character =. as an example, the output_operand =a (cptr) means the c variable cptr will get its value from this output operand, which will be in an address register. see section 5.2.2 for more details. 3. input_operands are optional operands to provide specific input information to the compiler. each input_operand is separated by a comma and may include a c variable. as an example, the input_operand a (cptr) means the value of this input operand will be taken from the c variable cptr, and placed in an address register. again, full descriptions of the input and output operands can be found in section 5.2.2. 4. reg_save specifies registers that are to be explicitly reserved for the __ asm () statement. the registers to be saved must be named as strings such as a and b. each register is separated by a comma (see section 5.2.3 for additional information) the compiler assumes that all data residing in the reg_save registers will be invalidated by the _ _ asm () statement. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . in-line assembly code motorola mixing c and assembly language 5-3 5.2.1 instruction template the first argument of the _ _asm() extension is the instruction template or assembler instruction template. this instruction template is mandatory, and describes the line or lines of assembly language to be placed into the compilers assembly language output (see section in section 5.2.4). this template is not parsed for assembly language syntax violations and is simply written to the compiler output. as a result, the compiler will not detect assembly-time errors. these errors will be caught by the assembler. more than one assembly instruction can be put into a single instruction template by using the line separator \n. the line separator, or newline, can be utilized as in a normal c statement. the line continuation sequence, a \ followed by an immediate newline, is particularly useful when an instruction template contains an assembly instruction that is too long to fit in one source line (see section 5-18 and section in section 5.2.4). other c language character constants such as \t, \f, etc. can also be used in the instruction template. in many situations, it is desirable to use the values of c variables and/or expressions directly in the instruction template. since all memory and register accesses are accomplished through variables, manipulating memory and registers directly using assembly code requires knowledge of their locations. without optimizations, the current value of a variable will be maintained in memory at a specific address. however, an optimizing c compiler such as dsp56kcc may retain a variable in a register and perform operations on that variable without updating the memory location corresponding to the variable between operations. this enhances the performance of the compiled code but makes accessing variables in memory risky. in order to guarantee that the correct value of a variable is returned when it is referenced, a mechanism called operand expansion string (oes) is provided. the oes allows a variable to be securely accessed even though its current location is unknown. the operand expansion string is a substring of the instruction template and begins with the character %. this string is usually two or three characters long and provides the compiler with special information about an operand, and how its reference should be printed. an oes must reference only one c variable or expression, which in turn must be listed in either one or both operand lists (see section 5.2.2). the oes is parsed by the compiler and gives sufficient information to allow the variable to be correctly referenced by the assembly language instruction in the instruction template. most examples in section 5.2.4 include an oes. the oes syntax is: % [modifier] operand_id_number f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-4 motorola dsp56000 family optimizing c compiler users manual motorola in-line assembly code where: 1. modifier is a single optional character which specifies a particular class of operand. the available modifiers are j , e , h , k , g , i , f , p , and q . j an offset register ( nx ) associated with the corresponding address registers ( rx ). since the offset register is paired with the address register, the allocated offset register has the same index as the address register (see section in section 5.2.4). e a1 or b1 , upper word of the destination registers a or b (see section in section 5.2.4). h a0 or b0 , lower word of the destination registers a or b (see section in section 5.2.4). k a2 or b2 , extension register of the destination register, a or b (see section in section 5.2.4). g select the 24-bit portion of the 48-bit alu register ( x or y ) that is not occupied by data pointed to by the operand id e.g., if the operand id points to x0 then x1 is selected and similarly x1 ? x0 , y0 ? y1 , y1 ? y0 (see section and section 5-9 in section 5.2.4). i strip the 0 or 1 from the allocated register name i.e., a1 ? a , a0 ? a , b1 ? b , b0 ? b (see section in section 5.2.4). f insert the memory space identifier ( x or y ) for the memory location (see section in section 5.2.4). p generate an immediate 16-bit constant without # sign (see example in section 5.2.4). q generate an immediate 24-bit constant without # sign (see section in section 5.2.4). 2. operand_id_number specifies the operand location in the operand descriptor list (see section in section 5.2.4). the operand descriptor list is a concatenation of the output operands and input operands (see section 5.2.2). the first operand is labeled zero and there can be up to 31 more operands in the list. more than one instruction template can be used if more than 32 operands are needed. in-line assembly code can also be used to insert a label directly into the compiler output. a label without any white spaces in the in-line assembly code instruction template will guarantee that the same template label will be in the compiler output (see section ). care should be taken not to use labels that the c compiler generates. using the same labels as the c compiler will cause a duplicate label error (see section 4.7, "compiler naming conventions," on page 4-13.) f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . in-line assembly code motorola mixing c and assembly language 5-5 5.2.2 output/input operands the operand list is a concatenation of output and input operands which the oes can access via the operand_id_number (see section 5.2.1). output or input operands consist of operands separated by a comma (,). each operand should be associated with a c expression and its operand constraint described below. a colon, :, is used to separate the assembler instruction template from the first output operand. a second colon separates the final output operand from the first input operand. a third colon can be used to separate the input operands from the optional field reg_save . two consecutive colons are required when only input operands are needed in the operand list, leaving us with the empty list of output operands. the operand syntax is: [=]operand_constraint (c_expression) where: 1. = differentiates input and output operands. all output operands must use this character first. 2. operand constraint is a single character that describes the type of resource (memory or register) that an operand is to be loaded into or read from. each operand constraint has an optional set of modifiers that may be applied in the instruction template. 3. c_expression is any valid c expression defined by the ansi c standard. the c expression can be either l-value or r-value. any output operand should use the l-value to specify the memory location to store the data. the available operand constraints are a , d , r , s , i and m . all of these constraints originate from the dsp56000 family architecture; therefore, a full understanding of these constraints requires that the programmer understand this architecture. the constraints are: a one of the address registers ( rx , where x = 0 through 7; see section 5 of the dsp56000 family users manual ) will be allocated to the c expression (see oes modifier j: the following in-line assembly code is used to generate executable assembly code. notice that the actual register selection is totally dependent on the c compiler but the register selected (r3 in this example) is guaranteed to be related to the c expression used (in this case cptr, see section 5.2). on page 10). the c expression will be promoted to this register. typically the c expression should be a pointer to be assigned to an address register. the oes modifier, j , can only be associated with operand constraint a (see section 5.2.1). f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-6 motorola dsp56000 family optimizing c compiler users manual motorola in-line assembly code d one of the 56-bit accumulators ( a or b which are referred to as destination registers; see section 4 of the dsp56000/dsp56001 users manual ) will be allocated to the c expression (see oes modifier e: the modifier e can be used to generate the assembly code below because a1 is the upper part of register a. on page 10 through oes modifier k: the k modifier can be used to generate the following assembly code because a2 is the extension portion of register a. on page 11). the c expression will be promoted to this register. care must be exercised to make sure the size of the c expression is consistent with the accumulator length. the oes modifiers e , h and k can be associated with operand constraint d (see section 5.2.1). r one of the input registers ( x0 or y0 which are also called multiplier registers; see section 4 of the dsp56000/dsp56001 users manual ) will be allocated to the c expression (see oes modifier i: the modifier can be used to generate the following assembly code because x is a register without a 0 or 1 portion. on page 12). the c expression will be promoted to this register. the oes modifiers g and i can only be associated with operand constraint r (see section 5.2.1). s one of the input registers ( x0 , x1 , y0 or y1 which are also called source registers; see section 4 of the dsp56000/dsp56001 users manual ) will be allocated to the c expression (see example 5-13 on page 5-14 through example 5-17 on page 5-15). the c expression will be promoted to this register. the oes modifiers g and i can only be associated with operand constraint s (see section 5.2.1). i an immediate constant; a constant is generated in the form of #constant if no modifier is specified. with p or q modifier, the value is generated without the # sign. the value is the 16-bit constant with p modifier, and 24-bit constant with q modifier (see example 5-12). m a memory location will be allocated to the c expression (see example 5-11). the dsp56000/dsp56001 has four memory spaces: x, y, l and p but the c compiler may only use the x, y or l memory spaces for this expression. the oes modifier f can only be associated with operand constraint m (see section 5.2.1). number inherit all memory or register characteristics of the operand indicated by the operand id number (see example 5-3). this constraint is usually used for read/write operands which are declared by using the same c variable as both the input and output operand. the operand is sometimes referred to as a read-only operand if it is used only as input (see example 5-14) and it is called a write-only operand if it is used only as an output (see f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . in-line assembly code motorola mixing c and assembly language 5-7 example 5-15). in most of cases, the operand is used as both an input and an output operand (see example 5-16 and example 5-17). in these cases the operand should be described as both. since output operands should be listed first, the operand id number is determined when the input operand is declared. the id number will be used as the operand constraint of the input operand. section lists the operand constraints and their related modifiers. 5.2.3 explicit register saving it is possible to manually perform register allocation, in fact this may simplify the process of converting an existing body of dsp56000/dsp56001 assembly language subroutines to in-line assembly code. the programmer need only identify each register explicitly referenced in the assembly code and list them in the reg_save argument region (see section 5.2). this guarantees that the content of each register is preserved across _ _asm() calls. use of registers r0 and r6 is prohibited in the assembly code because they are reserved for the c compiler during run-time where r0 is the frame pointer and r6 is the stack pointer. registers mn are used by the compiler as temporary registers and nn are not used by the c compiler at all. as a result, these registers do not need to be saved unless the programmer uses them in assembly code. if they are used in assembly code, they should only be used as local variables. explicit register saving is done through specifying the registers to be saved. a string is used to specify each register. the valid register names are listed in table 5-2. table 5-1. operand constraints/modifiers associations operand constraint modifier a -address register ( rx )% j -rns paired offset register d -destination register ( a , b )% e % h % k -upper word ( a1 , b1 ) -lower word ( a0 , b0 ) -extension ( a2 , b2 ) r -multiplier register ( x0 , y0 )% g % i -select other register portion -strip 0/1 from register name s -source register ( x0 , x1 , y0 , y1 )% g % i -select other register portion -strip 0/1 from register name i -constant (#number) % p % q -16 bit immediate value - 24 bit immediate value m -memory location % f -memory space identifier number -any one of above %(corresponding modifier f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-8 motorola dsp56000 family optimizing c compiler users manual motorola in-line assembly code an example for reg_save is a program performing an iir filter operation which is based on the dr. bub iir filter program iir1.asm. the program data structure is the same as the dr. bub program and data is passed through the c variable data. pointers sptr and cptr are used to point to static variables. example 5-1. reg_save reg_save : this in-line assembly code is converted from the dr. bub iir filter program using reg_save . this program is based on y memory model, and there are other in-line assembly features used here for the demonstration of the reg save. section 5.2.4 may need to be referenced for the other feature of the in-line assembly. iir1( int data, int* sptr, int* cptr ) { __ asm volatile ( move %0,a\n\ move %1,r1\n\ move %2,r4\n\ move y:(r1)+,x0\n\ move y:(r4)+,y0\n\ mac x0,y0,a y:(r4)-,y0\n\ move y:(r1),x1\n\ macr x1,y0,a x0,y:(r1)-\n\ move a,y:(r1) : /* no output */ : d (data), a (sptr), a (cptr) : a, x1, x0, y0, r1, r4 ); } 5.2.4 in-line assembly code examples the examples in this section illustrate the practical application of the _ _asm() extension. the main purpose of this section is to show how to write in-line assembly code. since these examples are intended to illustrate the information presented earlier in this chapter, references to the appropriate subjects have been included. example 5-2 illustrates the use of the in-line assembly code instruction_template. since this in-line assembly code directly clears register a, the programmer should check to be sure that the contents of a are not needed. table 5-2. reg_save names register type valid names accumulator a,b input x0, x1, y0, y1 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . in-line assembly code motorola mixing c and assembly language 5-9 example 5-2. instruction_template instruction_template: the following are a few examples of how to utilize the instruction template in in-line assembly code. this feature allows the generation of any valid assembly instruction and it is probably the most frequently used feature in in-line assembly coding. __ asm(clr a); /* clears the register a */ _ _asm(move #$10, a2); /* load the register a2 with the hex value 10 */ _ _asm(hcr equ $ffe8); /* equate the host control register to $ffe8 */ a pseudo operand will be used to illustrate use of the oes operand id number. the pseudo operand functions as an input or output operand. example 5-3 uses five pseudo operands: v, w, x, y and z each of which is referenced by operand ids 0 , 1 , 2 , 3 and 4 , respectively. the pseudo operands are used as in the oes %0 , %1 , %2 , %3 and %4 . table 5-3 shows which operands in example 5-3 are input or output operands. example 5-3. operand_id instruction template with operand_id : in order to illustrate how to use output or input operands, pseudo operands v, w, x, y, and z are used. the operand_id listed in this example can be used as part of an instruction_template . __ asm(instruction_template : v, w, x : y,z); example 5-3 through example 5-12 illustrate the use of oes modifier (see section 5.2.1). table 5-3. output and input operands for section 5-3 operand id pseudo operand operand type oes 0voutput%0 1woutput%1 2xoutput%2 3 y input %3 4 z input %4 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-10 motorola dsp56000 family optimizing c compiler users manual motorola in-line assembly code example 5-4. oes modifier j oes modifier j : the following in-line assembly code is used to generate executable assembly code. notice that the actual register selection is totally dependent on the c compiler but the register selected (r3 in this example) is guaranteed to be related to the c expression used (in this case cptr, see section 5.2). in-line assembly code: char *cptr; __ asm(move (%0)+%j0::a(cptr)); assembly code generated: move (r3)+n3 example 5-5. oes modifier e oes modifier e : the modifier e can be used to generate the assembly code below because a1 is the upper part of register a. in-line assembly code: int foo; __ asm(move #$ffffff,%e0:=d(foo)); assembly code generated: move #$ffffff,a1 example 5-6. oes modifier h oes modifier h : the h modifier can be used to generate the following assembly code because a0 is the lower part of register a. in-line assembly code: int foo; __ asm(move #$ffffff,%h0:=d(foo)) assembly code generated: move #$ffffff,a0 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . in-line assembly code motorola mixing c and assembly language 5-11 example 5-7. oes modifier k oes modifier k : the k modifier can be used to generate the following assembly code because a2 is the extension portion of register a. in-line assembly code: int foo; _ _asm(move #$ff,%k0:=d (foo)); assembly code generated: move #$ff,a2 example 5-8. oes modifier g oes modifier g : swap the most significant 24-bit portion and the least significant 24-bit portion of 48-bit registers xand y to allow the or instruction to operate on an entire 48-bit register. /* * the following assembly code could be generated (note that the * optimizer may vary the code actually generated). move x1,a1 move x0,x1 move a1,x0 * * the variable foo can be allocated to either x0 , x1 , y0 ,or y1 * by using the operand constraint s . the swap operation can * be applied to the register allocated to the variable foo by * using the following in-line assembly code. * */ main() { int foo; _ _asm volatile ("move %g0,a1" : : "s" (foo)); _ _asm volatile ("move %0,%g0" : "=s" (foo) : "0" (foo)); _ _asm volatile ("move a1,%0" : "=s" (foo)); } f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-12 motorola dsp56000 family optimizing c compiler users manual motorola in-line assembly code example 5-9. oes modifier g oes modifier g : program bit checker looks to see if any bit in the 48-bit registers x or y is a one. in this case, bit checker looks to see whether the variable foo which is placed in either the xor yregister is all zeros or contains a one. the result is stored in register a1. if register a1 is not 0x000000, then foo has one or more bits set to one. /* * the variable foo can be allocated to either the x or y register by using * the operand constraint s . the or instruction only operates on 24-bit * registers so that to or the x register with another register, x1 must * be ored separately from x0 . the same applies for the y register. */ main() { long volatile foo; _ _asm volatile (clr a); _ _asm volatile (or %0,a :: s (foo)); _ _asm volatile (or %g0,a :: s (foo)); } example 5-10. oes modifier i oes modifier i : the modifier can be used to generate the following assembly code because x is a register without a 0 or 1 portion. in-line assembly code: int foo; _ _asm(move l:<$0,%i0 : =r(foo)); assembly code generated: move l:<$0, x f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . in-line assembly code motorola mixing c and assembly language 5-13 example 5-11. oes modifier f oes modifier f : the f modifier can be used to generate the following assembly code. assuming that y memory space is used and the memory location of the variable foo is 233, then the desired memory space indicator y will be automatically generated by the f modifier. in-line assembly code: int foo; __ asm(move #$ffffff,%f0: =m (foo)); assembly code generated: move #$ffffff,y:233 example 5-12. oes modifier p and q oes modifier p , and q : this in-line assembly code programs the sci of dsp56001 by setting up the sci registers located at x:$fff0 and x:$fff2. you may use modifier p for any 16-bit value. #define scr 0xfff0 #define sccr 0xfff2 #define v_scr 0x2000 #define v_sccr 0x013f main() { __asm(movep%0,x:%q1::i(v_scr),i(scr)); _ _asm(movep %0,x:%q1 : : i ( v_sccr), i (sccr)); } assembly code generated: movep #>$002000,x:65520 movep #>$00013f,x:65522 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-14 motorola dsp56000 family optimizing c compiler users manual motorola in-line assembly code example 5-13. input expression / output expression input expression / output expression: this in-line assembly code uses the pseudo assembly mnemonic asm_instruction and refers to two c expressions: output_expression and input_expression. this example illustrates how to interpret the operand constraint (see section 5.2.2) and operand id (see section 5.2.1 and example 5-3). the example implies that the c expression output_expression is expanded with constraint d and is an output of the assembly code instruction asm_instruction. similarly, the c expression input_expression is expanded with constraint s and used as an input to the assembly code instruction asm_instruction. __ asm(asm_instruction %1,%0 : =d (output_expression) : s (input_expression)); example 5-14. read-only operand read-only operand: this in-line assembly code uses the pseudo assembly mnemonic asm_instruction and uses input_expression as a read-only operand. __ asm(asm_instruction %0 :: s (input_expression)); example 5-15. write-only operand write-only operand: this in-line assembly code uses the pseudo assembly mnemonic asm_instruction and uses output_expression as a write-only operand. __ asm(asm_instruction %0 :=d(output_expression)); example 5-16. read-write operand read-write operand: an addition is programmed using in-line assembly code and the c expression result is used as a read-write operand. the variable, foo, is used as a read only operand. notice that operand constraint 0 was used to reference the add instructions second source operand which is also the destination operand (see the dsp56000/dsp56001 users manual appendix a for the syntax of the add instruction). int foo, result; __ asm(add %1,%0 : =d (result) : s (foo), 0 (result)); f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . in-line assembly code motorola mixing c and assembly language 5-15 example 5-17. read-write operand read-write operand: the same result will be obtained as in example 5-16. notice how the operand id is changed according to the placement of the c variables. int foo, result; __ asm(add %2,%0 : =d (result) : 0 (result), s (foo)); example 5-18. multiple instruction single-line multiple instruction single-line: an in-line assembly program which places a value (e.g. $709) in register a and negates the result is written in one line. this one line will generate two lines of assembly code in the c compiler output. __ asm(move #$709,a\n neg a); example 5-19. multiple instruction multiple-line multiple instruction multiple-line: the two lines of in-line assembly code in this example have the same effect as the one line in section 5-18. notice that using two lines increases the in-line assembly code readability. the line continuation character \ used at the end of the in-line assembly codes first line makes this possible. __ asm(move #$709,a\n\ neg a); example 5-20. mulitple use of _ _asm() multiple use of _ _asm() . this example and example 5-21 are done in-line with the compiler performing all register allocation and all operands are referenced via c expressions.this in-line program is a single memory space version of the dr. bub iir filter program iir1.asm. the method used to write this in-line assembly program is to use an _ _asm() statement for each assembly language instruction. int iir1( int data, int* sptr, int* cptr ) { int state1, state2, coef; __ asm ( move y:(%1)+,%0 : =r (state1) : a (sptr) ); __ asm ( move y:(%1)+,%0 : =r (coef) : a (cptr) ); __ asm ( mac %2,%3,%0\ty:(%4)-,%1 : =d (data), =r (coef) : r (state1), 1 (coef), a (cptr), 0 (data) ); __ asm ( move y:(%1),%0 : =s (state2) : a (sptr) ); __ asm ( macr %1,%2,%0\t%3,y:(%4)- f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-16 motorola dsp56000 family optimizing c compiler users manual motorola in-line assembly code :=d(data) : s (state2), r (coef), r (state1), a (sptr) ); __ asm ( move %0,y:(%1) : : d (data), a (sptr) ); return data; } example 5-21. line separation line separation. this in-line program is functionally identical to section except that line separation is used to insert the entire assembly language program (dr. bub iir filter program, iir1.asm) into a single _ _asm() statement. notice how much easier it is to read the program. int iir1( int data, int* sptr, int* cptr ) { int state1, state2, coef; __asm("\n\ move y:(%4)+,%2\n\ move y:(%5)+,%1\n\ mac %8,%7,%0\ty:(%5)-,%1\n\ move y:(%4),%3\n\ macr %9,%7,%0\t%8,y:(%4)-\n\ move %6,y:(%4)": =d (data), =r (coef), =r (state1), =s (state2): a (sptr), a (cptr), 0 (data), 1 (coef),2 (state1), 3 (state2) ); return data; } example 5-22. instruction template label instruction template label: the following in-line assembly code which generates the label foo uses a return character \n to insure that there is no white space in front of the label. __ asm(\nfoo); f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . in-line assembly code motorola mixing c and assembly language 5-17 example 5-23. program dsp56001 sci timer program dsp56001 sci timer: the sci timer can be programmed so that a sci interrupt service routine is accessed periodically. the following in-line assembly program is based on the sci timer described in section 11.2 of the dsp56000/dsp56001 user manual. _ _asm(\nscr equ $fff0); _ _asm(\nsccr equ $fff2); _ _asm(\nipr equ $ffff); main() { _ _asm( move #0,r7\n\ movep #$2000,x:scr\n\ movep #$013f,x:sccr\n\ movep #$c000,x:ipr\n\ andi #$fc,mr\n\ \nend jmp end); _ _asm (org p:$001c\n\ move (r7)+\n\ nop); } 5.2.5 controlling labels generated by the compiler using the _ _asm() keyword, it is possible for the programmer to override the compilers label generation conventions for subroutines and global variables. this may be useful for: 1. calling assembly language subroutines. 2. calling c subroutines from assembly code. 3. referencing assembly language global variables from c. 4. referencing global c variables from assembly code. 5.2.5.1 calling assembly subroutines calling a subroutine or function requires using a label that points to the subroutine or function. the c compiler uses a predetermined labeling convention (see section 4.7, "compiler naming conventions," on page 4-13), and does not generate arbitrary labels like most assembly programs. in order to call assembly subroutines labeled in an arbitrary fashion, _ asm() can be used to overwrite the c convention label with an arbitrary label. to illustrate how to use the _ _asm directive for this purpose, section 4-7 reads the data at x memory location $100 and y memory location y+2. for test purposes, the y memory space is filled with the integer sequence 0 through 9 and the x memory space is left uninitialized. the printf() statement prints the data returned from the functions valofx(100) and valofy(y+2). these two functions are constructed in assembly code and reside in another file. the two assembly subroutines, readx and ready, are written f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-18 motorola dsp56000 family optimizing c compiler users manual motorola in-line assembly code using the out-of-line assembly technique described in section 5.4. the listings for readx and ready are shown in example 5-38 in section 5.4.5. by using the statement extern int valofx() _ _asm(readx), valofy() _ _asm(ready); all c compiler generated function labels for valofx() and valofy() are replaced by the labels readx and ready, respectively. example 5-24. calling assembly from c calling assembly from c. this c program (called test.c) can be used to examine the data in x or y memory by calling assembly routines readx or ready. notice that the assembly code for readx and ready is listed in section 5-38 of section 5.4.5. c:\> type test.c #include in-line assembly code motorola mixing c and assembly language 5-19 example 5-25. calling c from assembly calling c from assembly. this c subroutine (called c_print.c) uses the standard c library routine, printf() , to print the input argument as a string. c:\> type c_print.c #include 5-20 motorola dsp56000 family optimizing c compiler users manual motorola in-line assembly code 5.2.5.3 referencing assembly global variables from c the data in assembly language programs must be accessible to c programs to take full advantage of the dsp56000 family architecture since the c language cannot access all of the dsp56000 features directly. one way to access this data is through global data which can be defined in assembly language and accessed in the c program environment. this feature is particularly useful to allocate modulo buffers for c variables. detailed information on modulo buffers can be found in the dsp56000/dsp56001 users manual , section 5 address generation unit and address modes. example 5-27. generate data with assembly language generate data with assembly language. the data file, sqtbl.asm, is generated in assembly language and consists of the squares of the numbers 0 - 8. notice that directives bsc, dc, ds, dsm and dsr (see section 6.3 of the motorola dsp56000 macro assembler reference manual ) can be used depending on the application. c:\> type sqtbl.asm section data global table org y: table dc 0,1,4,9,16,25,36,49,64 endsec example 5-28. access data with c access data with c. this test program (called test.c) prints the value of 5 2 on the screen. c:\> type test.c #include in-line assembly code motorola mixing c and assembly language 5-21 5.2.5.4 referencing global c variables from assembly language one dsp56kcc feature is that global data in a c program is available to assembly language programs. this feature is particularly useful when the data to be processed by an assembly language program is generated by the c program. example 5-29 provides coefficients that are used in example 5-30. example 5-29. generate data with c generate data with c. data file, data.c , is generated by a c language program and contains the coefficients of an average filter which takes the average of the last four input data. % cat data.c /* * data.c */ int cwaddr[] _ _asm(waddr); int ccaddr[] _ _asm(caddr); int ntap _ _asm(n_1); int cwaddr[4]; int ccaddr[] = { 0x200000, 0x200000, 0x200000, 0x200000 }; intntap=4; example 5-30. 4-tap fir filter 4-tap fir filter access data with assembly language: this fir filter reads an input sample from memory location y:input and writes the filtered output to memory location y:output. the input data array is stored in x memory starting at waddr and the fir coefficients are stored in y memory starting at caddr. notice that the memory space for waddr and caddr is allocated in the c routine described in example 5-29. % cat fir.asm org p: move #waddr,r0 move #caddr,r4 move y:n_1,m0 move m0,m4 movep y:input,x:(r0) clr a x:(r0)+,x0y:(r4)+,y0 rep #n-1 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-22 motorola dsp56000 family optimizing c compiler users manual motorola #pragma directive mac x0,y0,a x:(r0)+,x0y:(r4)+,y0 macr x0,x0,a (r0)- movep a,y:output end c:\> g56k -s data.c c:\> g56k -c fir.asm c:\> g56k -c data.asm c:\> g56k fir.cln data.cln 5.2.6 specifying registers for variables dsp56kcc allows the programmer to identify a specific register for local and global variables, but due to the limited number of registers available, this may not have a positive effect on run-time performance. with this in mind, this feature should be used sparingly. both global and local variables are candidates for promotion to specific registers and syntactically they look the same: register int *ptr __ asm(r5); by specifying a specific register for a local or global variable, the programmer is reserving the register for the variables entire scope (global for the entire program, local for the function in which they are declared). this means that the compiler will not use the register for any other purpose and the register will not be saved and restored by the c function call. 5.2.7 optimizer effects on code all in-line assembly code is visible to the optimizer and as such it is possible that the optimizer will convert it into a new form or eliminate it entirely if it is determined to be unreachable or dead. in order to guarantee that code is not removed by the optimizer, the ansi keyword volatile must be used. __ asm volatile (); 5.3 #pragma directive the purpose of this section is to explain the proper techniques for manipulating the assemblers run-time and load-time counters while programming in the c language. currently the motorola dsp assemblers allow the programmer to specify both a run-time location and a load-time location for any object; however, there is no corresponding concept within c. the generic #pragma facility is used to add this capability rather than f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . #pragma directive motorola mixing c and assembly language 5-23 extending the c language. users now have complete freedom in specifying both the run-time and load-time counters for any static or global object. these directives may be used with either code or data. this flexibility is achieved by allowing the user to modify any of eight counter strings maintained by the compiler two for each memory space: x, y, l and p. when an object is or defined, the current values of those counter strings are bound to that object. syntax for the pragma directive is #pragma counter_string argument c function or data storage definition #pragma counter_string where 1. the two #pragma statements must encase the entire definition. 2. counter_string in the first #pragma specifies which phase (run or load time) and memory space is to be affected. it can be x_run , y_run , l_run , p_run , x_load , y_load , l_load ,or p_load . the memory model used in the c compiler should match the memory model specified by counter_string . note that the p_run and p_load counter_strings will always have an effect. 3. the argument in the first #pragma is the string that will be passed as either the runtime or load-time argument to the org assembler directive. this address, which is optional, is of the form x:address_value where x is the counter associated with the x , y , l ,or p memory, and address_value is the value to be initially assigned to that counter. as an example, p:$300 might be used for the counter string p_load . 4. the c function or data storage definition is a declaration that reserves storage. 5. the second counter_string should be the same as the first counter_string and will return the memory specification to the default setting. if and only if the memory space of the counter string in the #pragma directive matches the memory model of the c compiler , then the compiler will insert an assembly org statement of the form: (1) org a:runtime_address,b:loadtime_address or (2) org a:runtime_address f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-24 motorola dsp56000 family optimizing c compiler users manual motorola #pragma directive wherea is the run time counter and runtime_address is the optional initial value for that counter, as specified in the argument to #pragma . b is the load time counter and loadtime_address is the optional initial value for that counter, as specified in the argument to #pragma . the following two examples illustrate that the load time counter is optional. see the section on the org statement in the motorola dsp56000/dsp56001 assembler manual for a complete description and list of options. notice that the pragma directive run-time counter string will only affect the run-time address and the pragma directive load-time counter string will only affect the load-time address. as a simple example, assuming that y-memory model (or default memory model) is used (see chapter 3 to change the memory model to be used), the following c segment #pragma y_load x:$100 int coeff[5] = {0x19999a, 0x200000, 0x266666, 0x2ccccd, 0x333333}; #pragma y_load produces the following assembly language code: globalfcoeff orgy:,x:$100 fcoeff dc 1677722 dc 2097152 dc 2516582 dc 2936013 dc 3355443 notice that the second #pragma directive will remove the affect of the first memory specification, i.e., #pragma y_load x:$100, and the rest of the code generated by the c compiler will be in the default memory area (see chapter 3 in order to change this default memory model). the above example code will be loaded in the x memory location $100, and it should be copied to the y memory space upon system start-up. when burning a prom, only one memory space is desired to be used, as an example, p memory space, so that only one prom is enough for both data and program. in such case, both the data and the program will be burned in the prom and the data should be moved to the data memory space (either, x, y or l memory space depending on the program) upon system start-up. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . out-of-line assembly code motorola mixing c and assembly language 5-25 lets assume that the coefficients of the above example is desired to be in the program space when burning the prom. then the following c segment #pragma y_load p:$100 int coeff[5] = {0x19999a, 0x200000, 0x266666, 0x2ccccd, 0x333333}; #pragma y_load produces the following assembly language code: global fcoeff org y:,p:$100 fcoeff dc 1677722 dc 2097152 dc 2516582 dc 2936013 dc 3355443 the above assembly code will be loaded into the p memory space at p:$100 for the prom burning, and it should be copied to the l memory space before the actual program is executed. manipulating the assemblers run-time and load-time counters requires a thorough understanding of the underlying assumptions about memory layout, which are made by the compiler (see chapter 6). note: incorrect use of this feature may cause compile-time, link-time and even run-time errors. 5.4 out-of-line assembly code out-of-line assembly code is assembly code written in a separate source file that is called from a c program. separating the assembly code and c code in this way provides a powerful and flexible interface to the dsp56000 family architecture. this out-of-line method may be used to convert existing assembly subroutines, write new subroutines completely in assembly language or access both data spaces (x and y). the key advantage of out-of-line assembly code is that it provides a complete assembly programming environment for the dsp56000 family whereas the in-line assembly code must follow the c programming environment rules. writing out-of-line assembly code requires a complete understanding of the c cross compiler and the dsp56000 family architecture. for out-of-line assembly code to be callable from a c program, the following five basic elements should be included in the assembly source file in sequence. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-26 motorola dsp56000 family optimizing c compiler users manual motorola out-of-line assembly code 1. c subroutine entry code (prologue code). 2. save all registers to be used. 3. main program. 4. restore all registers used. 5. c subroutine exit code (epilogue code). each of these steps can be programmed as a macro; however, using generic macros can unnecessarily increase the size of the resulting assembly code program. the dsps ability to move data in parallel with arithmetic operations provides opportunities to significantly optimize the assembly code by combining steps within a macro and by combining the code of two or more macros. in order to illustrate the steps listed above, the out-of-line assembly code template is described first and each element of the template is then explained in detail. after reviewing the five elements, some optimization techniques are discussed. 5.4.1 general template the following template is a generic form used to make the c function foo. there are five distinct elements in the template which are numbered as above. the actual code for the prologue and epilogue is shown but the save all registers to be used, main program, and restore all registers used are listed as comments because their actual code depends on the main program. global ffoo ; prologue: ffoo ; sets up entry point (c function address), move r0,y:(r6)+ ; updates frame pointer (r0) and saves lua (r6)+,r0 ; return address move ssh,y:(r6)+ ; ; save all registers to be used ; main program ; restore all registers used move y:-(r6),ssh ; epilogue:restores return address, updates move y:-(r6),r0 ; restores return address, updates tst a ; frame pointer (r0), and updates the sr with ; the contents of register a rts f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . out-of-line assembly code motorola mixing c and assembly language 5-27 5.4.1.1 prologue the first two lines of the prologue make the assembly program visible to the c program so that the subroutine or function is callable from the c program. in this case, any one of the following c statements can be used to access out-of-line assembly code. foo(); x = foo(); x = foo(arg1, arg2, arg3); the first function call assumes that the c function does not use any arguments and does not return any values. the second only returns a value which is the same data type as the variable x. the last call assumes that the c function uses the three arguments: arg1, arg2 and arg3 and then returns the value x. the rest of the prologue saves the old frame pointer, updates the current frame pointer and saves the return address. the return address is saved when the jsr instruction pushes the hardware stack which saves the program counter in the high 16 bits of the system stack i.e., ssh. this section of the prologue provides bookkeeping for the c compiler activation record. detailed information on the structure of the activation record can be found in section 4.6.1, "activation record," on page 4-10. after this prologue, the r0 register is the frame pointer and r6 is the stack pointer. 5.4.1.2 save all registers all registers used in the main program should be saved before the main program changes them. this step is the second element of the template save all registers to be used. in order to save the registers, r6 is used as a stack pointer. the stack grows upward and the current stack pointer (r6) points to the next element above the top of stack. the following statement saves one register to the top of stack and sets the stack pointer to the next available stack location. move x0,y:(r6)+ two registers, y1 and x1, can be saved with the following statements: move y1,y:(r6)+ move x1,y:(r6)+ since saving and restoring the registers are the subroutines responsibility, the order of saving the registers should be in accordance with their restoration. the restore process should be exactly the reverse order of the register saving sequence. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-28 motorola dsp56000 family optimizing c compiler users manual motorola out-of-line assembly code 5.4.1.3 main program the main c function program accesses the parameters passed, executes the operations on the parameters and returns a value. passing parameters is done through the activation record which has been pushed onto the stack. the activation record frame pointer is pointed to by r0 and the first parameter is placed 3 locations below the frame pointer (see section 4.6.1, "activation record," on page 4-10). the parameters are arranged in reversed order. for example, the following statement should be used to move the first single word parameter to register r3 . move #-3,n0 ; the first parameter location move y:(r0+n0),r3 ; load it to r3. assuming the first three parameters are one word long, the following statements move the second and third parameters to registers r4 and r5 , respectively. move #-4,n0 move y:(r0+n0),r4 move #-5,n0 move y:(r0+n0),r5 5.4.1.4 restore all registers the stack pointer, r2 , is needed to restore the registers. the following code will restore one register. at this point in the functions execution, the stack pointer points to the location above the last saved register, hence the pre-decrement. move y:-(r2),x0 ; restore the restoring procedure can be simplified if more than one register is to be restored. restoring registers x0, x1, y0 and y1 can be done by the statements below. move (r2)- move y:(r2)-,x0 move y:(r2)-,x1 move y:(r2)-,y0 move y:(r2),y1 after the function has finished, a return value can be passed to the caller. any 32-bit or 16-bit value must be returned through register a. if the return value is larger than 32 bits, then the compiler allocates the proper amount of buffer space and register a1 becomes the pointer to this buffer space upon callee execution. it is the callees responsibility to copy any return values from the buffer whose address resides in register a1. this is the method used for returning structs. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . out-of-line assembly code motorola mixing c and assembly language 5-29 5.4.1.5 epilogue the out-of-line template epilogue is the reverse of the prologue. the epilogue restores the return address and updates the frame pointer, r0. notice that the stack pointer, r6, should be decremented before each move operation. in addition, register a is tested to update the sr register. this testing is a part of the c compiler code generation feature and should be included (see section 5.4.5 for optimization). 5.4.1.6 out-of-line assembly code example the following example illustrates using the general template as well as the fact that the dsp56000/dsp56001 performs fractional arithmetic. assume that section mod1 contains two c callable subroutines, mac01() and mac02() and section mod2 contains a single c callable subroutine, mactwo() . the two functions mac01() and mac02() take one argument each and mactwo() takes two arguments. these functions perform multiplication according to the following formulas and return their results: note that multiplication on the dsp56000/dsp56001 treats data as fractions; however, the c programming environment does not support a fractional data type. therefore data will be passed as integers from the c programming environment and will be treated as fractional data in the dsp56000/dsp56001. this can be an advantage if fractional arithmetic is desired since this is normally difficult to accomplish in c. note that the calling routine must ensure arg is in range to prevent overflow. the following function declarations (ansi standard) can be used to declare functions which are known to perform fractional arithmetic. internally, the size of integers is the same as the size of fractions. int mac01(int); int mac02(int); int mactwo(int, int); mac01(arg) calculates arg * 0.01 mac02(arg) calculates arg * 0.02 mactwo(arg1, arg2) calculates arg1 * arg2 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-30 motorola dsp56000 family optimizing c compiler users manual motorola out-of-line assembly code these three functions can be called as follows: int fractvalue; fractvalue = mac01(0x123456); /* the value 0x123456 is 0.142222166 in fractional */ fractvalue = mactwo(0x123456, 0x0147ae); /* 0x0147ae is 0.001 */ the conversion between fractional data and integer data (in hex form) can be performed using the evaluate command in the dsp56000 simulator. example 5-31. general template for out-of-line assembly code general template for out-of-line assembly code: the two sections of this out-of-line assembly code are mod1 and mod2. the first section implements: 1. mac01(arg) which takes a fractional argument and returns 0.01 times the argument and 2. mac02(arg) which takes a fractional argument and returns 0.02 times the argument. the second section implements mactwo(arg) which takes two fractional arguments and returns arg1*arg2 . ; section mod1: ; int mac01(int arg); ; takes a fractional argument and returns 0.01 * argument. ; int mac02(int arg); ; takes a fractional argument and returns 0.02 * argument. section mod1 global fmac01 fmac01 move r0,y:(r6)+ ; prologue lua (r6)+,r0 move ssh,y:(r6)+ move x1,y:(r6)+ ; save register x1 move y0,y:(r6)+ ; save register y0 move n0,y:(r6)+ ; save register n0 move #-3,n0 move y:(r0+n0),x1 ; argument move #0.01,y0 ; operand 0.01 clr a macr x1,y0,a ; main program: calculates multiplication of ;x1andy0 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . out-of-line assembly code motorola mixing c and assembly language 5-31 move y:-(r6),n0 ; restore register n0 move y:-(r6),y0 ; restore register y0 move y:-(r6),x1 ; restore register x1 move y:-(r6),ssh ; epilogue move y:-(r6),r0 tst a rts global fmac02 fmac02 move r0,y:(r6)+ ; prologue lua (r6)+,r0 move ssh,y:(r6)+ move x1,y:(r6)+ ; save register x1 move y0,y:(r6)+ ; save register y0 move n0,y:(r6)+ ; save register n0 move #-3,n0 move y:(r0+n0),x1 ; argument move #0.02,y0 ; operand 0.02 clr a macr x1,y0,a ; main program: calculates multiplication of ;x1andy0 move y:-(r6),n0 ; restore register n0 move y:-(r6),y0 ; restore register y0 move y:-(r6),x1 ; restore register x1 move y:-(r6),ssh ; epilogue move y:-(r6),r0 tst a rts endsec ; section mod2 ; int mactwo( int arg1, int arg2); ; takes two fractional arguments and returns arg1 * arg2. section mod2 global fmactwo fmactwo move r0,y:(r6)+ ; prologue lua (r6)+,r0 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-32 motorola dsp56000 family optimizing c compiler users manual motorola out-of-line assembly code move ssh,y:(r6)+ move x1,y:(r6)+ ; save register x1 move y0,y:(r6)+ ; save register y0 move n0,y:(r6)+ ; save register n0 move #-3,n0 move y:(r0+n0),x1 ; the first argument move #-4,n0 move y:(r0+n0),y0 ; the second argument clr a macr x1,y0,a ; main program: ; calculates multiplication of x1 and y0 move y:-(r6),n0 ; restore register n0 move y-:(r6),y0 ; restore register y0 move y:-(r6),x1 ; restore register x1 move y:-(r6),ssh ; epilogue move y:-(r6),r0 tst a rts endsec the dsp56kcc control program, g56k, should be used to assemble the out-of-line assembly code. the two sections of this code, mod1 and mod2, can be put in the same file or in separate files.the following command lines and source files provide a test for the case where each program is in a separate file. c:\> type main.c #include out-of-line assembly code motorola mixing c and assembly language 5-33 5.4.2 global c and static variables in c the global c variables are accessed using labels generated by the c compiler. any variables that are static to an assembly language subroutine will be accessed the same way. these variables are placed into memory at compile-time and are referenced symbolically according to the labels automatically generated by the compiler. however, it is possible to override the default labels by using the _ _asm() keyword as explained in section 5.2.5, "controlling labels generated by the compiler," . for example, using the default labeling convention, the global integer, ginteger which can be declared within the c statement extern int ginteger; is loaded into the input register x0 in assembly code as follows: move y:fginteger,x0 when declaring c global variables in an assembly language file, the programmer must be careful to follow the label generating convention or use the __ asm() keyword to report to the compiler that the labeling convention has been changed. in both cases, the assembler directive global is used to export the labels to the c files. do not use the xdef/xref pair of directives. note that it is the programmers responsibility to allocate space for the global variables declared in this manner. in the example below, this is done with the assembler directive dc. also, ansi c requires that all global variables be initialized to zero if they are not explicitly initialized. example 5-32. global label in assembly language global label in assembly language. this example shows assembly code that defines a global integer (named fginteger) which is normally accessed as ginteger in the c environment and fginteger in the assembly programming environment. fginteger dc $0 globalfginteger example 5-33. global variable declaration global variable declaration. this is the c code equivalent to section which defines the global integer ginteger. extern int ginteger; f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-34 motorola dsp56000 family optimizing c compiler users manual motorola out-of-line assembly code example 5-34. changing a global label changing a global label. this example shows c code that generates a global integer (ginteger) which is accessed as ginteger in both the c environment and the assembly programming environment. extern int ginteger _ _asm(ginteger); which will appear in assembly language code as: ginteger dc $0 global ginteger 5.4.3 using run-time stack for local data the run-time stack may be used when the programmer requires a temporary data space for automatic style variables i.e., local variables in subroutines. using the run-time stack requires additional steps in the prologue and epilogue sections. it is the subroutines responsibility to automatically allocate and deallocate the stack at run-time. in the prologue, an extra step is required to save the run-time stack space. keeping in mind that the stack pointer must always point to the next available stack location, the stack space is allocated by advancing the stack pointer by the amount of space required. one way to allocate this space is shown in the section . example 5-35. run-time stack allocation run-time stack allocation: this code segment can be inserted in the general template prologue for out-of-line assembly code. notice that size in the move statement below should be replaced with the appropriate constant. move #size ,n6; the stack size needed nop ; wait until n6 is available to operate on r6 move (r6)+n6 ; allocate the run-time stack for locals referencing the data space can then be accomplished using negative offsets from the stack pointer or via initialized address registers. there are many alternatives to these methods but they are all similar. in the epilogue, an extra step is required to restore the stack pointer i.e., deallocate the run-time local stack. this is simply the reverse of the allocation process in the prologue. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . out-of-line assembly code motorola mixing c and assembly language 5-35 example 5-36. run-time stack deallocation run-time stack deallocation: this code segment can be inserted in the general template epilogue for out-of-line assembly code. notice that size in the move statement below should be replaced with the appropriate constant. move #size ,n6; the stack size used before nop ; wait until n6 is available to operate on r6 move (r6)-n6 ; deallocate the run-time stack there are many ways to do this, one simple optimization would be to advance the n6 load instruction in the program to eliminate the nop. 5.4.4 calling c routines c routines are routines that are callable by a c program and may be written in either c or assembly language. when writing assembly language subroutines, it may be necessary to call library routines that have been provided or that have been written by the programmer e.g., a call to sin() or printf() . in order to do this, the programmer must follow 3 steps: 1. push arguments onto the run-time stack in reverse order. 2. make the subroutine call. 3. restore the stack pointer. the following example assumes that the four parameters to be passed to the c function foo() are located in registers a1, b1, x0 and x1; respectively. the first four lines of assembly code push the arguments onto the run-time stack in reverse order. the jsr statement makes the subroutine call and the last two statements restore the stack pointer. example 5-37. calling c routines calling c routines. the c function, foo() , is called from the following assembly code. function foo() is declared as int foo(int, int, int, int) ; move x1,y:(r6)+ ; pushing arguments onto move x0,y:(r6)+; run-time stack in reverse move b1,y:(r6)+; order move a1,y:(r6)+ jsr ffoo ; subroutine call move #4,n6 ; the stack size restored. nop move (r6)-n6 ; restore it. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-36 motorola dsp56000 family optimizing c compiler users manual motorola out-of-line assembly code 5.4.5 optimization techniques the general template for out-of-line assembly code provides a clean template to build c callable functions. however, the dsp56000 family microprocessor chips have powerful features such as multiple instruction execution (multiply and accumulate) and parallel data move operations that may allow additional optimization. after constructing the out-of-line assembly code from the general template, some hand-optimization can be performed by combining several assembly statements. information about these optimization techniques can be obtained from the dsp56000/dsp56001 users manual . some optimization techniques which are related to the c compiler are discussed in this section but additional optimization can be achieved using the architectural features described in the users manual. the r0 register (which is updated by the out-of-line assembly code template prologue) is required only when parameters are passed to the c function. if there are no parameters to be passed, then the following two assembly code lines are not required and can be omitted: move r0,y:(r6)+ lua (r6)+,r0 the return address (ssh) was saved in the out-of-line assembly code prologue but it is only required when a c function calls another c function. a c function is called a leaf function if it does not call any other c function. in leaf functions, the return address is not needed and does not have to be saved. similarly the epilogue can be optimized in the same way as the prologue. for any leaf function or non parameter c function, the epilogue size can be reduced by eliminating: move y:-(r6),ssh; for a leaf c function or move y:-(r6),r0; for a non-parameter c function the test statement tst a in the epilogue can be eliminated if the function does not return any values. the test statement may be required due to the c compilers optimization features since it provides condition flags for an if statement in a function call. for example, if the out-of-line assembly function foo() is used in the statement if(foo()) { }, then the c compiler will not generate code to test the return value when a jcc statement is issued. this is primarily because the c compiler uses the condition flags which were executed at the end of the epilogue of foo() . a variety of optimizations can be achieved by combining the move instructions and main program code to utilize parallel moves (see section 5.4.5 and section 5-38 which point out possible optimizations in the comments). these and other dsp56000 specific f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . out-of-line assembly code motorola mixing c and assembly language 5-37 optimizations can dramatically improve the quality of the application specific library routines. a careful review of the dsp56000/dsp56001 users manual will be worthwhile for efficient library development. example 5-38. readx / ready routines readx / ready routines. this out-of-line assembly program (called memread.asm) reads the contents of x or y memory and returns the data to the caller. the readx subroutine returns the x memory space contents pointed to by the input argument and the ready subroutine returns the y memory contents. section memread org p: global readx ; prologue readx mover 0,y:(r6)+; lua (r6)+,r0 ; move r1,y:(r6)+; save register r1 move #-3,n0 ; the first parameter move x:(r0+n0),r1 ; move x:(r1),a; main program ; restore register & epilogue move y:-(r6),r1 ; note: r6 decrement can tst a y:-(r6),r0; be a parallel move in the rts ; main program. global ready ; prologue ready move r0,y:(r6)+ ; lua (r6)+,r0 ; move r1,y:(r6)+ ; save register r1 move #-3,n0 ; the first parameter move y:(r0+n0),r1 ; move y:(r1),a ; main program ; restore register & epilogue move y:-(r6),r1 ; note: r6 decrement can tst a y:-(r6),r0 ; be a parallel move in the rts ; main program. endsec f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 5-38 motorola dsp56000 family optimizing c compiler users manual motorola out-of-line assembly code f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . run-time environment specification files motorola software-hardware integration 6-1 chapter 6 software-hardware integration 6.1 overview this chapter explains how the run-time environment may be changed and provides examples of some changes and their effects. the run-time environment provided with the compiler assumes, as a default, that the simulator is the target execution device. several aspects of the default run-time environment must be altered in order to adapt the compiler to work with a custom hardware configuration. the files which are alterable are discussed and classified according to effect. aspects of the run-time environment such as: bootstrapping, interrupts and memory management are addressed individually. 6.2 run-time environment specification files the run-time environment is specified by three assembly language files: crt056[x,y,l].asm, signal56[x,y,l].asm and setjmp56[x,y,l].asm where x, y, or l denote the memory model (see chapters 2 and 4). these files may need to be modified if the run-time environment is to be customized. the crt0 file contains parameters which specify the c bootstrap code, memory configuration, memory management, interrupt vectors and other miscellaneous code. this file must be modified to match the software and hardware configuration because the memory configuration and interrupt vectors are determined by the hardware. the information in this manual on the crt0 file applies to dsp56kcc version g1.11. the crt0 file may be different for other versions of this compiler. the signal file, which is equivalent to a hardware interrupt, is implemented in the c environment. the signal file contains the code and data structures used to implement the signal() and raise() library functions. changing this file is not recommended unless necessary since any change to this file requires detailed knowledge of the dsp56000 family interrupt mechanism in addition to the signal and raise functions. this file is closely tied to the signal.h file. the setjmp file contains code which implements the functions setjmp() and longjmp() . this file will probably never need to be modified unless the signal file is changed; however, if either the setjmp file or setjmp.h are modified, the code in both files must be made consistent. the source code for setjmp() and longjmp() is provided with dsp56kcc to allow modification should the signal mechanism need to be changed. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 6-2 motorola dsp56000 family optimizing c compiler users manual motorola crt0 file the operation of setjmp() and longjmp() is described in section 6.10 and detailed implementation information can be obtained from the files provided with dsp56kcc. 6.3 crt0 file the following subsections describe the various functions of the crt0 file. 6.4 bootstrapping the c program the processor enters a c program through the c bootstrap code in the crt0 file. the c bootstrap code in crt0 provides the c environment required to execute a c program. this environment includes a global data area, static data area, stack area, heap area, function calling mechanism, etc. this environment must be established before c programs will execute correctly. the following steps are normally taken to initialize the processor to execute c code: 1. jump from the chip reset vector to the c bootstrap code labeled at f_ _start in ctr0 . remember that the mode select pins on the chip control the chip operating mode when leaving reset which, in turn, controls the reset vector address (see the motorola dsp56000/dsp56001 users manual for more details). 2. configure all hardware registers needed (i.e,. omr, host port, etc.). this is also a proper place to initialize any non-c related data structures or peripheral hardware such as the bcr, port b and port c. 3. load the stack pointer, r6, with a pointer to the base of the stack. remember that the stack grows up (the value in the stack pointer gets greater as data is pushed). the value of dsize is generated by the linker and is the first address above the statically allocated data (c global and static variables). by default, this value is used as the initial stack pointer. 4. optional: initialize the frame pointer, r0. this may be of use to some debuggers when stack back traces are performed. program execution will not be affected if this initialization is omitted. 5. call main() with instruction jsr fmain . notice that the label is fmain and there are no parameters passed to the main function. the normal c compiler start-up passes two arguments but g56k does not pass any arguments because dsp56kcc does not support a particular hosted environment. the bootstrap code is followed with the label f_ _crt0_end . this label is used by gdb56 and run56 to detect program termination. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . memory configuration and management motorola software-hardware integration 6-3 note: labels which begin with a double underline (e.g., _ _crt0_end) in this manual have a space between the double underlines to visually separate them. do not separate the leading double underlines with a space when coding them (i.e., code _ _crt0_end as __crt0_end). example 6-1. dsp56000/dsp56001 operation mode change mode 2 has a reset vector of $e000 which must contain a jmp to the c program bootstrap code. adding the following code segment to the crt0 file will change the bootstrap mode to mode2. section mode2_reset org p:$e000 jmp f_ _start; jump to the c start-up program. endsec example 6-2. c bootstrap code location change hardware was designed to have a 256 byte rom monitor located in the program memory space starting at $0000 and ending at $ff. program ram starts at location p:$100. the following changes to the crt0 file will change the beginning location of the c bootstrap code to the first available ram location (p:$100). the ds statement allocates program space starting at p:$0000 and lets the rom be located at address p:$0000. the org statement places the c bootstrap code at memory location p:$100. change this portion of the crt0 file: org p: f_ _start to: org p:$0000 ds $100 org p:$100 f_ _start 6.5 memory configuration and management the dsp56000 family supports three memory spaces: program memory (p memory), x and y data memories. the dsp56000 family design philosophy is to support concurrent access to these memory spaces to accelerate dsp operations. the c programming language does not support separate memory spaces but instead treats memory as a single memory space. as a result, there is a difference between hardware memory organization and c memory utilization. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 6-4 motorola dsp56000 family optimizing c compiler users manual motorola memory configuration and management dsp56kcc provides three different memory models to minimize these differences. they are the x memory model, y memory model and l memory model. selection of the appropriate memory model is made using the -m option of the control program, g56k (see section 3.2.2, "compile phase options," on page 3-20). the memory configuration for each memory model is discussed later in this section. the crt0 file can be modified to configure memory for any hardware design. there are four data segments in the c programming environment. these are the program segment, global-static data segment, stack data segment and heap data segment. the program segment is located in program memory. the three data segments can be located in x memory, y memory or l memory space (which is a concatenation of x and y memory) depending on the memory model used. figure 6-1. environment memory configuration as indicated in section 5.1, "overview," on page 5-1, global and static data reside at the bottom of the available data memory and the top address of the global and static data area, which is called dsize, is set by the linker. the constant top_of_memory is defined to indicate the top of the entire available memory. the two data segments, heap and stack, are located as shown section 5.1, "overview," on page 5-1. the stack is located so that it can grow up and the heap is located so that it can grow down. there are two locations used to indicate the initial values for the heap pointer and stack pointer. these locations are _ _y_size and _ _break and are initialized in the crt0 file as dsize and top_of_memory, respectively. xmemorymodel global / static stack heap _ _y_size _ _break x: top_of_memory dsize ymemorymodel global / static stack heap y: lmemorymodel global / static stack heap l: stack pointer f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . memory configuration and management motorola software-hardware integration 6-5 in summary, two variables _ _y_size and _ _break and the constant top_of_memory are used to configure the data segment. the program segment is configured using the org statement in the crt0 file. the variable, _ _y_size , should be initialized with the initial stack pointer and the variable, _ _break , should be initialized with the initial heap pointer. warning the stack and heap regions must not contain on-chip peripheral memory or the static or global data regions. also, no region may be reconfigured after the c main function is called. variables _ _y_size and _ _break should not be altered by an arbitrary function since they are utilized by system level libraries such as malloc and free. example 6-3. fast stack in this example, the stack is required to reside in an 8k sram starting at l:$4000. the following program reserves the stack space using org and ds statements and sets the initial stack pointer to the sram stack area. add this section to the crt0 file: section fast_ram org l:$4000 ds $2000 endsec change the following line of c bootstrap code in the crt0 file: move y:f_ _y_size,r6 to: move #$4000,r6 f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 6-6 motorola dsp56000 family optimizing c compiler users manual motorola interrupt vectors example 6-4. fast heap the heap is required to reside in an 8k sram starting at l:$4000. the following program reserves the heap space using org and ds statements and sets the initial heap pointer to the sram heap area. add this section to the crt0 file: section fast_ram org l:$4000 ds $2000 endsec change the following line of c bootstrap code in the crt0 file: top_of_memory equ $ffbe to: top_of_memory equ $5fff sometimes hardware configurations map more than one memory space into a single physical memory. other implementations partially populate various address spaces leaving holes. some may have different regions with fast memory and slow memory. all of these special cases can usually be handled by modifying the crt0 file. when multiple memory spaces are mapped into a single physical memory, the memory must be partitioned among the memory spaces. a way to restrict the linker from overlapping these memory spaces is needed. for example, suppose that both the y and p spaces are mapped into the same 64k physical ram and need to be partitioned with the low 48k for program memory and the high 16k for data memory. the linker can be restricted from allocating across holes in physical memory by using the org and ds directives to confiscate those addresses. note that the linker may not automatically take advantage of memory which is present between holes. it may be required to manually place assembly language data structures to utilize this memory. 6.6 interrupt vectors the interrupt vector locations for the dsp56000 family (a.k.a. interrupt source in section 8 of the dsp56000/dsp56001 users manual ) contain one or two instructions each to be executed when the interrupt assigned to that location occurs. there are 32 interrupt vectors available, all of which should be initialized with some value to avoid undefined behavior resulting from an unexpected interrupt. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . interrupt vectors motorola software-hardware integration 6-7 the crt0 file contains code to initialize these interrupt vectors. by default, all vectors are initialized with the instruction jsr fabort . the first element of the vector table, which is the hardware reset interrupt, is initialized with the instruction jmp f_ _start. the purpose of the c function abort() , which is labeled as fabort in the assembly environment, is to stop program execution and leave the chip in the stop mode. the f_ _start label is the program address of the c program bootstrap code which calls the c main() function after execution. interrupt vectors that are to be used must be reprogrammed to point to the interrupt service routines instead of the abort() function. initialization of the interrupt vectors in the crt0 file reduces the size of the resulting application program and may increase its speed. the following crt0 code segment (called section reset) is the default interrupt vector table initialization. section reset org p:$0 jmp f_start org p:$2 dup 31 jsr fabort endm endsec the interrupt vector table can be changed to point to user-provided interrupt service routines instead of the abort() routine in this portion of crt0 . example 6-5 illustrates how to initialize pointers to these user-provided interrupt service routines. example 6-5. user-defined interrupt vector table assume the hardware supports all interrupts and each interrupt service routine is located at the address labeled interruptxx (where xx is the value of the interrupt vector). the following code initializes the interrupt vector table. each service routine starting at interruptxx can be programmed in assembly language as shown in section . section reset org p:$0 jmp f_start jsr interrupt02 jsr interrupt04 jsr interrupt06 jsr interrupt08 jsr interrupt3e endsec f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 6-8 motorola dsp56000 family optimizing c compiler users manual motorola miscellaneous code example 6-6. interrupt service routine interrupt service routine: this service routine updates the global variable f_ _time at each sci timer interrupt. the sci timer period can be set by programming the sccr (see section 11.2.2.3 of the dsp56000/dsp56001 users manual ). this program is based on the y memory model and the sci interface control registers (x:fff0 and x:fff2) should be initialized for proper sci timer operation (see section 11.2 in the dsp56000/dsp56001 users manual for detailed information on the sci operation). section interrupt org p: global interrupt1c interrupt1c move (r6)+ ; secure the stack pointer ; (refer to section 5.4.1.4) move r2,y:(r6) ; save the r2 register move y:f_ _time,r2 ; retrieve the variable _ _time move (r2)+ ; increment the variable move r2,y:f_ _time ; save the result move y:(r6)-,r2 ; restore the r2 register rti ; return from interrupt service endsec notice that fast interrupts can also be programmed by modifying the crt0 file in the same way as for the long interrupts (see chapter 8 of the dsp56000/dsp56001 users manual for more information on fast and long interrupts).) 6.7 miscellaneous code there are other data structures and code related to the run-time environment in the crt0 file. they are: 1. error code variable, errno , is a global integer used to record failure codes in c library calls. this error code variable will exist if c library calls are used. this variable can be utilized as a debugging aid in order to check which error code is returned from the c function calls. 2. heap-stack checking window variable, _ _stack_safety , is a global variable declared in the crt0 file that is used by the heap allocation routines, malloc() , calloc() and realloc() to avoid heap-stack growth collisions. if the distance between the bottom of the heap and the top of the stack is less than the value contained in _ _stack_safety , then the heap allocation routine will return an error code indicating that no more memory is available. the value may be set as required f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . signal file motorola software-hardware integration 6-9 by the application since the window _ _stack_safety is declared as a global variable. 3. memory limit variable, _ _mem_limit, is a global variable declared in the crt0 file and used by the library routine brk() to disallow any meaningless memory requests. this variable should have the same value as _ _break upon entry into main(). for information on how to use the function brk() , refer to appendix b in this manual. 4. dynamic time variable, _ _time, is a global variable declared in the crt0 file and used as a volatile timer counter by the simulator. this variable is updated by the dsp56000 simulator (run56) every clock cycle. examining this variable allows the programmer to determine program execution time. this variable is only used by the simulator and can be omitted when the program is to be executed by hardware. 5. host i/o stub functions, _ _send() and _ _ receive(), are defined in crt0 and are called by the standard i/o library functions. the provided crt0 file only has stub functions, as both gdb56 and run56 watch these addresses and perform all i/o directly. 6. floating-point shift table, _ _fp_shift, is a pointer used by floating-point library functions. this pointer may be removed if the application does not use floating-point. all of these variables, constants, functions and pointers are related to the run-time environments that are used by c library functions and must be properly set. 6.8 signal file the hardware level interrupt mechanism (see section 8.3 of the dsp56000/dsp56001 users manual ) is more efficient than the signal() function. however, in many cases interrupts can be handled in the c environment and it is often more preferable to do so. there are two functions, signal() and raise() , used to support programming interrupt service routines in c. these functions are not associated with the crt0 file. although they are more complicated than the simple hardware interrupt vector table discussed in section 6.6 (see section 8.2 of the dsp56000/dsp56001 users manual ) they provide very handy tools for the c programmer. a thorough knowledge of the signal() function and the c environment is needed in order to modify the signal() function. this section describes how the signal() function is currently implemented. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 6-10 motorola dsp56000 family optimizing c compiler users manual motorola signal() 6.9 signal() the signal() function is passed two arguments: 1. a signal number on the dsp56000 family processor, the signal number corresponds directly to the interrupt vector address. notice that the signal number is not an integer sequence but is an even number. 2. a function pointer the function pointer passed is assumed to belong to a c function either generated by this compiler or by assembly code. signal() performs the following three steps when binding the specified signal number and function: 1. the instruction jsr f_ _c_sig_goto_dispatch+ signal() motorola software-hardware integration 6-11 modification of the signal file is only recommended when optimization of the service time is critical to the application. upon return from the c signal handler, all the registers are restored. finally, the rti instruction is executed to return to the code that was executing when the interrupt occurred. notice two factors in this scheme, 1. all registers are saved and restored before and after the c signal handler and 2. the rti instruction is executed by the _ _c_sig_dispatch() function. warning the signal handler must not contain the rti instruction at the end of the program regardless which language is used to program the interrupt. the signal handler does not need to save or restore any context or registers. the function _ _c_sig_dispatch() will not act like a normal c function because it never returns to its caller. instead, it will return to the code that was executing when the interrupt happened by executing the rti instruction. assembly language interrupt handlers can coexist with c signal handlers. the code in the signal file will not alter any interrupt vector except the one specified by the signal number passed to the signal() function (see the first of the three steps above). the c signal interface could be used with an assembly routine but would be unnecessarily slow. to use an assembly language interrupt handler, alter the vector (e.g., interrupt 08) with a jsr to it (e.g., jsr interrupt 08)or use a fast interrupt routine. 6.9.1 raise() the raise() function is used to simulate an interrupt. the code in raise() simply calls the entry in _ _c_sig_goto_dispatch that is matched to the interrupt vector specified by the signal number passed. the ansi standard signal handlers sig_dfl , sig_err and sig_ign are implemented by the hand-coded functions _ _sig_dfl() , _ _sig_err() and _ _ sig_ign() , respectively. 1. sig_dfl notes that the interrupt happened by incrementing _ _sig_drop_count and then returns. 2. sig_err calls abort() and never returns. 3. sig_ign returns without any effect (i.e., ignore). f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 6-12 motorola dsp56000 family optimizing c compiler users manual motorola setjmp file the mechanisms used to implement the c signal interface may be altered to fit a particular hardware application. any series of alterations applied to the signal file must leave an implementation conforming to the ansi standard x3.159 for c. alteration of the signal file is done at ones own risk and is not generally advised. again, the contents of the signal file must remain consistent with the include file signal.h . 6.10 setjmp file the functions setjmp() and longjmp() are implemented in the setjmp file. the setjmp() function stores the current process status (i.e., the current execution context) in a buffer that is passed. the longjmp() function is used to restore the process status to the execution context which was saved by setjmp() . saving the current execution context is done by saving the stack pointer, the return program counter value, the frame pointer and all of the callee-save registers into the buffer. the buffer that is passed to setjmp () should have enough space for the saving process. the structure jmp_buf defined in setjmp.h allocates the buffer space needed for the operation. the function setjmp() always returns a zero. the function longjmp () takes two arguments, an environment buffer and a return value. it restores all registers from the buffer passed, including the frame pointer and stack pointer. it then places the return value passed into accumulator a and sets the ccr to reflect the return value just stored in accumulator a. the function longjmp() discards the return program counter on the hardware stack and jumps to the address pointed to by the program counter stored in the buffer. this file must conform to the include files setjmp.h and longjmp.h . since these two algorithms are very straightforward, modification of the file may be not needed. if modification is absolutely necessary, then the ansi standard of the functions setjmp() and longjmp() should be followed. 6.11 host-supported i/o (printf (), et al) the library provided with dsp56kcc includes a full implementation of the ansi c standard input and output routines. these routines already work with gdb56 and run56, and can easily be embedded in custom applications. anywhere that formatted i/o is desired, these library routines can be included to simplify development. the entire suite of routines is based upon a simple communication protocol between the dsp and a host resident i/o driver, so porting the entire system to custom hardware is trivial. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . host-supported i/o (printf (), et al) motorola software-hardware integration 6-13 6.11.1 dsp functions __send () and __receive () all standard i/o functions, no matter how complicated, are built upon two simple communication functions, __send () , and __receive () . __send () sends a message to the i/o driver code residing on the host. __receive () retrieves a message from that same driver. implementing these two functions is all that need be done on the dsp in order to support standard i/o on custom hardware. it is assumed that some sort of hardware communication channel exists between the host and the dsp. __send () and __receive () implement a simple message passing mechanism on top of such a channel. __send () accepts two arguments: the address of the buffer to send, and the number of words to draw from that buffer. __receive () accepts the address of a buffer in which to place the received message as its single argument. all of the interactions between the host and dsp are driven by the library code running on the dsp; because the dsp is in control, it knows the size of return messages from the host, rendering a count argument to the function __receive () superfluous. __send () and __receive () are as simple as they seem; the complexity of the standard i/o package is embedded in the host-side driver and the lib56c[xyl].clb library routines. 6.11.2 the host-side i/o driver the application running on the host must have the provided i/o driver embedded in it. the driver is written in c, and uses typically available library routines such as open (), close () , read () , and write () to perform the actions requested by the dsp. the i/o code is written as an event-driven state machine, so that the host side application can perform concurrently with the dsp when the dsp is not requesting i/o activity. in fact, the i/o driver on the host may be interrupt driven. the host-side driver consists of the code in dsp/etc/hostio.h and dsp/etc/hostio.c. the meat of the package consists of two functions, init_host_io_structures () and process_pending_host_io () , and two buffers hio_send and hio_receive . the function init_host_io_structures () is called to initialize host-side driver data structures before dsp execution is commenced. the buffer hio_send is used to send messages to the dsp, and hio_receive is used to receive messages from the dsp. the function process_pending_host_io () considers the current state of the buffers and its own internal state, and then performs any required buffer modification or host i/o. 6.11.3 communication between the host and dsp the messages passed between the dsp and the hosts i/o driver are defined in the file dsp/include/ioprim.h. all sequences of communication are initiated by the dsp as a direct result of a call to a standard i/o function. each standard i/o call may initiate a series of f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 6-14 motorola dsp56000 family optimizing c compiler users manual motorola host-supported i/o (printf (), et al) messages between the dsp and the host, with the host eventually returning a message containing the completion status of the original request. the file dsp/include/ioprim.h is included by both the host-side i/o driver, and the standard i/o library code; it defines the constant definitions used in the aforementioned messages. a typical series of events and messages that comprise a standard i/o call might look like this: 1. the application running on the dsp makes a call to fopen () . 2. the library code in lib56c[xyl].clb calls __send () , with a buffer that contains the code dsp_open, the flags, the mode, and the string length of the path. 3. the host receives the message into the buffer hio_receive , sets its valid flag, and calls process_pending_host_io () . the state machine inside process_pending_host_io () notes that it is now in the middle of an open file request, records the values from the first message, and then returns. at this point, code written by the application developer must check the valid flag of the buffer hio_send; in this case, the buffer hio_send has not been marked valid. 4. the library code in lib56c[xyl].clb calls __send () again, this time sending the path. 5. again, the host receives the message into the buffer hio_receive , and calls process_pending_host_io () after setting the buffers valid flag. 6. process_pending_host_io () uses the information from the two messages to perform the file open. it then builds an operation status message, places it in the buffer hio_send , and sets that buffers valid flag. process_pending_host_io () resets its internal state and returns. 7. the host checks the buffer valid flag on hio_send , sees that it is true, and transmits the message to the dsp. 8. the library code running on the dsp finishes the fopen () call and returns. on the host side of the interface, the application writer must write the code that exchanges data with the dsp, the code that calls process_pending_host_io () , and the code that checks buffer valid flags. on the dsp side of the interface, the application writer must write the routines __send () and __receive () . the communication between the dsp and the host is always initiated by the dsp and always follows a predetermined pattern, depending on the initial message. because this communication is so simple, the code that calls process_pending_host_io () can also be quite simple. example 6-7 is a hypothetical non-reentrant interrupt handler written in c. it uses two functions, peek () and poke () , to access some sort of hardware communication device connected to the dsp. the functions peek () and poke () arent provided; theyre simply an abstraction for host-side hardware access. this code assumes that the dsp sends the size of a message directly before sending a message. check_buffer_size is a macro f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . host-supported i/o (printf (), et al) motorola software-hardware integration 6-15 defined in dsp/etc/hostio.h. it should always be used to ensure that the buffer hio_receive is large enough to handle the incoming message. finally, this example assumes that the function signal () is available to register interrupt handlers. this code doesnt have to be implemented in an interrupt driven manner; periodic polling could be used as well. the critical issues are that the communication must be reliable, and that the system must not deadlock; the latter is easy to ensure, given the simple nature of the communication protocol. example6-7. samplehost-sidegluecode void interrupt_driven_io ( ) { int i; /* get the size of the message from the dsp. */ int size = peek ( in_port ); /* make sure that hio_receive is large enough. */ check_buffer_size ( & hio_receive, size ); /* read the message via hardware, into the buffer. */ for ( i = 0; i < size; ++ i ) { hio_receive.buffer[i] = peek ( in_port ); } /* mark the buffer as valid, perform any requested i/o. */ hio_receive.valid_p = true; process_pending_host_io ( ); /* if the driver wants to send to the dsp, then do so now. */ if ( hio_send.valid_p ) { for ( i = 0; i < hio_send.length; ++ i ) { poke ( out_port, hio_send.buffer[i] ); } hio_send.valid_p = false; } /* re-register this handler for future dsp message interrupts. */ signal ( sig_in_port, interrupt_driven_io ); } f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . 6-16 motorola dsp56000 family optimizing c compiler users manual motorola host-supported i/o (printf (), et al) f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola programming support a-1 appendix a programming support a.1 standard ansi header files each function provided in the ansi c library lib56c[xyl].clb is declared with the appropriate prototype in a header , whose contents are made available by the #include preprocessing directive. in addition to function prototypes, the header also defines all type and macro definitions required for ansi conformance. table a-3 lists the ansi standard header files: in general, header files may be included in any order and as often as desired with no negative side effects. the one exception occurs when including < assert.h > . the definition of the assert macro depends on the definition of the macro ndebug . the contents of the standard header files provided are exactly as described by the ansi document x3.159-1989 dated december 14, 1989. table a-1. ansi standard header files header files a-2 motorola dsp56000 family optimizing c compiler users manual motorola a.2 ansi c library sub-routines there are three library images provided with each compiler, ? lib56c x .clb x memory model ? lib56c y .clb y memory model ? lib56c l .clb l memory model each library contains all of the ansi defined sub-routines compiled and/or assembled for the specific memory model. the c and assembly language source for each library routine is distributed free of charge with the compiler. a.2.1 hosted vs. non-hosted library routines some of the standard ansi defined routines perform i/o. programs that use these functions will not encounter problems when run using gdb56 or run56 . extra work may be needed to port hosted i/o routines to custom hardware configurations. non-hosted routines will not encounter any problems on custom hardware. for a description of run56 , see appendix c, utilities. . a.3 forcing library routines out-of-line for performance reasons, several run-time library routines have been created to be used in-line by the compiler. the compiler in-lines a sub-routine by replacing the occurrence of the sub-routine call with the body of the sub-routine itself. this provides execution time benefits: 1. eliminates sub-routine call overhead. this is a substantial portion of the run-time for small library routines. 2. exposes more optimization opportunities to the optimizer. the library routines in table a-2 are automatically in-lined by the compiler when their header file is included: f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola programming support a-3 when it is necessary to disable this feature, possibly for debugging or decreasing program size, simply do one of the following. 1. add the following line to each c module (or once to a common header file) #undef routine_name where routine_name is the library routine that must be forced out-of-line. for example, to force the library routine ceil out-of-line: #undef ceil 2. use the command-line option -u , see chapter 3, control program options . this will force the library routine to be called for this compilation . if the code is re-compiled, the -u option must be used again. c:\> g56k -uceil file.c 3. do not include the header file. table a-2. in-line library routines header: ctype.h header: math.h header: stdlib.h header: string.h isalnum isalpha iscntrl isdigit ceil abs strcmp isgraph islower isprint ispunct fabs labs strcpy isspace isupper isxdigit tolower floor toupper fmod f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . a-4 motorola dsp56000 family optimizing c compiler users manual motorola a.4 function descriptions table a-3 describes each function in complete detail. the synopsis provides the syntax of the function and the options section discusses each option in detail. many function descriptions also include references to related functions and an example of how to use the function. the list shown below provides an abbreviated description of each function. table a-3. function descriptions function description abort force abnormal program termination. abs absolute value of integer. acos arc cosine. asin arc sine. atan arc tangent. atan2 arc tangent of angle defined by point y/x. atexit register a function for execution upon normal program termination. atof string to floating point. atoi string to integer. atol string to long integer. bsearch perform binary search. calloc dynamically allocate zero-initialized storage for objects. ceil ceiling function. clearerr clear error indicators associated with a stream. cos cosine. cosh hyperbolic cosine. div integer division with remainder. exit terminate program normally. exp exponential, e x . fabs absolute value of a double. fclose close a stream. feof test the end-of-file indicator of a stream. ferror test the error indicator of a stream. f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola programming support a-5 fflush flush all output pending on a stream. fgetc read a character from a stream. fgetpos retrieve the current value of the file position indicator of a stream. fgets readastringfromastream. floor floor function. fmod floating point remainder. fopen open a named file on the disk, to be accessed via a stream. fprintf write formatted output to a stream. fputc write a character to a stream. fputs writeastringtoastream. fread read unformatted input from a stream. free free storage allocated by calloc, malloc, and realloc. freopen open a named file on the disk, to be accessed via a stream. frexp break a floating point number into mantissa and exponent. fscanf read formatted input from a stream. fseek set a streams file position indicator. fsetpos set a streams file position indicator. ftell retrieve the current value of a streams file position indicator. fwrite write unformatted output to a stream. getc read a character from a stream (this may be a macro). getchar read a character from the stream stdin (this may be a macro). gets read a string from the stream stdin. isalnum test for alphanumeric character. isalpha test for alphabetic character. iscntrl test for control character. isdigit test for numeric character. isgraph test for printing character, excluding space and tab. islower test for lower-case alphabetic characters. table a-3. function descriptions (continued) function description f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . a-6 motorola dsp56000 family optimizing c compiler users manual motorola isprint test for printing character, excluding \t. ispunct test for punctuation character. isspace test for white-space character. isupper test for upper-case alphabetic character. isxdigit test for hexadecimal numeric character. labs absolute value of a long integer. ldexp multiply floating point number by a power of two. ldiv long integer division with remainder. log natural logarithm, base e. log10 base ten logarithm. longjmp execute a non-local jump. malloc dynamically allocate uninitialized storage. mblen length of a multibyte character. mbstowcs convert multibyte string to wide character string. mbtowc convert a multibyte character to a wide character. memchr find a character in a memory area. memcmp compare portion of two memory areas. memcpy copy from one area to another. memmove copy from one area to another (source and destination may overlap). memset initialize memory area. modf break a double into its integral and fractional parts. perror print error message indicated by errno. pow raise a double to a power. printf write formatted output to the stream stdout. putc write a character to a stream (this may be a macro). putchar write a character to the stream stdout (this may be a macro). puts write a string to the stream stdout. qsort quick sort. table a-3. function descriptions (continued) function description f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola programming support a-7 raise raise a signal. rand pseudo- random number generator. realloc change size of dynamically allocated storage area. remove remove a file from the disk. rename rename a file on the disk. rewind reset the file position indicator of a stream to the beginning of the file on the disk. scanf read formatted input from the stream stdin. setjmp save a reference of the current calling environment for later use by longjmp. setbuf associate a buffer with a stream. setvbuf associate a buffer with a stream, while also specifying the buffering mode and buffer size. signal set up signal handler. sin sine. sinh hyperbolic sine. sprintf write formatted output to a string. sqrt square root. srand seed the pseudo-random number generator. sscanf read formatted input from a string. strcat concatenate two strings. strchr find first occurrence of a character in a string. strcmp compare two strings. strcoll compare two strings based on current locale. strcpy copy one string into another. strcspn compute the length of the prefix of a string not contained in a second string. strerror map error code into an error message string. strlen determine length of a string. strncat concatenate a portion of one string to another. strncmp compare a portions of two strings. strncpy copy a portion of one string into another. table a-3. function descriptions (continued) function description f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . a-8 motorola dsp56000 family optimizing c compiler users manual motorola strpbrk find the first occurrence of a character from one string in another. strrchr find the last occurrence of a character in a string. strspn compute the length of the prefix of a string contained in a second string. strstr find the first occurrence of one string in another. strtod string to double. strtok break string into tokens. strtol string to long integer. strtoul string to unsigned long integer. strxfrm transform a string into locale-independent form. tan tangent. tanh hyperbolic tangent. tmpfile create a temporary binary file on the disk to be referenced via a stream. tmpnam generate a unique, valid file name. tolower convert uppercase character to lowercase. toupper convert lowercase character to uppercase. ungetc push a character back onto a specified input stream. vfprintf write formatted output to a stream, using a va_list. vprintf write formatted output to the stream stdout, using a va_list. vsprintf write formatted output to a string, using a va_list. wcstombs convert wchar_t array to multibyte string. wctomb convert wchar_t character to multibyte character. table a-3. function descriptions (continued) function description f r e e s c a l e s e m i c o n d u c t o r , i freescale semiconductor, inc. f o r m o r e i n f o r m a t i o n o n t h i s p r o d u c t , g o t o : w w w . f r e e s c a l e . c o m n c . . . motorola programming support a-9 a.4.1 abortforce abnormal program termination #include a-10 motorola dsp56000 family optimizing c compiler users manual motorola a.4.2 absabsolute value of integer #include motorola programming support a-11 a.4.3 acosarc cosine #include a-12 motorola dsp56000 family optimizing c compiler users manual motorola a.4.4 asinarc sine #include motorola programming support a-13 a.4.5 atanarc tangent #include a-14 motorola dsp56000 family optimizing c compiler users manual motorola a.4.6 atan2arc tangent of angle defined by point y/x #include motorola programming support a-15 a.4.7 atexitregister a function for execution at normal program termination #include a-16 motorola dsp56000 family optimizing c compiler users manual motorola a.4.8 atofstring to floating point #include motorola programming support a-17 a.4.9 atoistring to integer #include a-18 motorola dsp56000 family optimizing c compiler users manual motorola a.4.10 atolstring to long integer #include motorola programming support a-19 a.4.11 bsearchperform binary search #include a-20 motorola dsp56000 family optimizing c compiler users manual motorola a.4.12 callocdynamically allocate zero-initialized storage for objects #include motorola programming support a-21 example a-12. calloc #include a-22 motorola dsp56000 family optimizing c compiler users manual motorola a.4.13 ceilceiling function #include motorola programming support a-23 a.4.15 coscosine #include a-24 motorola dsp56000 family optimizing c compiler users manual motorola a.4.17 divinteger division with remainder #include motorola programming support a-25 a.4.18 exitterminate program normally #include a-26 motorola dsp56000 family optimizing c compiler users manual motorola a.4.19 expexponential, e x #include motorola programming support a-27 example a-20. fabs #include a-28 motorola dsp56000 family optimizing c compiler users manual motorola a.4.22 feoftest the end-of-file indicator of a specified stream #include motorola programming support a-29 a.4.23 ferrortest the error indicator of a stream #include a-30 motorola dsp56000 family optimizing c compiler users manual motorola a.4.25 fgetcread a character from the specified stream #include motorola programming support a-31 a.4.26 fgetposget value of file position indicator associated with a stream #include a-32 motorola dsp56000 family optimizing c compiler users manual motorola a.4.27 fgetsread a string from the specified stream #include motorola programming support a-33 a.4.28 floorfloor function #include a-34 motorola dsp56000 family optimizing c compiler users manual motorola a.4.30 fopenopen a named file on the hosts disk #include motorola programming support a-35 refer to the following sections for more information: ? section a.4.33, fputswrite a string to a stream, on page a-37 ? section a.4.27, fgetsread a string from the specified stream, on page a-32 ? section a.4.31, fprintfwrite formatted output to a stream, on page a-36 example a-29. fopen #include a-36 motorola dsp56000 family optimizing c compiler users manual motorola a.4.31 fprintfwrite formatted output to a stream #include motorola programming support a-37 a.4.33 fputswrite a string to a stream #include a-38 motorola dsp56000 family optimizing c compiler users manual motorola a.4.34 freadread data directly from a stream #include motorola programming support a-39 a.4.35 freefree storage allocated by calloc, malloc, and realloc #include a-40 motorola dsp56000 family optimizing c compiler users manual motorola a.4.36 freopenopen named file on disk, to be accessed via specified stream #include motorola programming support a-41 a.4.37 frexpbreak a floating point number into mantissa and exponent #include a-42 motorola dsp56000 family optimizing c compiler users manual motorola a.4.38 fscanfread formatted input from a stream #include motorola programming support a-43 fscanf returns eof if an input failure is detected before any conversions take place. otherwise it returns the number of assignments made. note that an optional assignment suppression character * may follow the initial %. this character will cause fscanf to discard the converted value without advancing along the list of object pointers. refer to the following sections for more information: ? section a.4.86, scanfread formatted input from standard input, on page a-91 ? section a.4.96, sscanfread formatted input from a string, on page a-98 example a-37. fscanf (a) the following program will, assuming that the input pending on standard input is my 98, store the three characters m, y, \0 will be stored in word[], and 98 will be stored in number. #include a-44 motorola dsp56000 family optimizing c compiler users manual motorola a.4.39 fseekset the file position indicator associated with a stream #include motorola programming support a-45 a.4.40 fsetposset the file position indicator associated with a stream #include a-46 motorola dsp56000 family optimizing c compiler users manual motorola a.4.41 ftellget the file position indicator associated with a stream #include motorola programming support a-47 a.4.42 fwritewrite data directly to a stream #include a-48 motorola dsp56000 family optimizing c compiler users manual motorola a.4.43 getcread a character from the specified stream #include motorola programming support a-49 example a-43. getchar #include a-50 motorola dsp56000 family optimizing c compiler users manual motorola a.4.46 isalnumtest for alphanumeric character #include motorola programming support a-51 a.4.47 isalphatest for alphabetic character #include a-52 motorola dsp56000 family optimizing c compiler users manual motorola a.4.48 iscntrltest for control character #include motorola programming support a-53 example a-48. isdigit #include a-54 motorola dsp56000 family optimizing c compiler users manual motorola a.4.50 isgraphtest for printing character, excluding space and tab #include motorola programming support a-55 example a-50. islower #include a-56 motorola dsp56000 family optimizing c compiler users manual motorola a.4.53 ispuncttest for punctuation character #include motorola programming support a-57 example a-53. isspace #include a-58 motorola dsp56000 family optimizing c compiler users manual motorola a.4.56 isxdigittest for hexadecimal numeric character #include motorola programming support a-59 example a-56. labs #include a-60 motorola dsp56000 family optimizing c compiler users manual motorola a.4.59 ldivlong integer division with remainder #include motorola programming support a-61 a.4.60 lognatural logarithm, base e #include a-62 motorola dsp56000 family optimizing c compiler users manual motorola a.4.61 log10base ten logarithm #include motorola programming support a-63 a.4.62 longjmpexecute a non-local jump #include a-64 motorola dsp56000 family optimizing c compiler users manual motorola a.4.63 mallocdynamically allocate uninitialized storage #include motorola programming support a-65 1. if s is a null pointer, mblen returns a zero. if s is not null , mblen returns 2. zero if s points to a null character, 3. the number of characters that comprise the multibyte character, or 4. -1 if an invalid multi-byte character is formed. in no case will the return value exceed n or the mb_cur_max macro. refer to the following sections for more information: ? section a.4.66, mbtowcconvert a multibyte character to a wide character, on page a-68 ? section a.4.128, wctombconvert wchar_t character to multibyte character, on page a-128 note: the dsp56000/dsp56001 does not provide byte addressing, thus characters always require an entire word of memory each. one way to better utilize data memory (with a run-time cost) is to combine the ansi data type wchar_t and the special ansi multibyte and wide character library routines. example a-63. mblem #include a-66 motorola dsp56000 family optimizing c compiler users manual motorola a.4.65 mbstowcsconvert multibyte string to wide character string #include motorola programming support a-67 example a-64. mbstowcs #include a-68 motorola dsp56000 family optimizing c compiler users manual motorola a.4.66 mbtowcconvert a multibyte character to a wide character #include motorola programming support a-69 example a-65. mbtowc #include a-70 motorola dsp56000 family optimizing c compiler users manual motorola a.4.67 memchrfind a character in a memory area #include motorola programming support a-71 a.4.68 memcmpcompare portion of two memory areas #include a-72 motorola dsp56000 family optimizing c compiler users manual motorola a.4.69 memcpycopy from one area to another #include motorola programming support a-73 a.4.70 memmovecopy storage #include a-74 motorola dsp56000 family optimizing c compiler users manual motorola a.4.71 memsetinitialize memory area #include motorola programming support a-75 a.4.72 modfbreak a double into its integral and fractional parts #include a-76 motorola dsp56000 family optimizing c compiler users manual motorola a.4.73 perrorprint error message #include motorola programming support a-77 a.4.74 powraise a double to a power #include |