RC-51 ANSI-C compiler for the 8051 family Reference Manual Document version 23 February 2010 RC-51 Contents 1. INTRODUCTION ..............................................................................................................6 1.1 Purpose of this manual....................................................................................................................6 1.2 Scope of this manual.......................................................................................................................6 1.3 Additional help or information..........................................................................................................6 2. OVERVIEW........................................................................................................................7 2.1 Organization of the manual..............................................................................................................7 2.2 Differences between version 3 and previous versions.....................................................................7 2.2.1 Storage rules............................................................................................................................7 2.2.2 Extended control management.................................................................................................7 2.2.3 Miscellaneous...........................................................................................................................8 2.2.4 Optimization..............................................................................................................................8 2.2.5 Environment.............................................................................................................................8 2.2.6 Additional attributes for functions..............................................................................................8 2.3 First programs.................................................................................................................................9 2.3.1 "Hello !".....................................................................................................................................9 2.3.2 Counter.....................................................................................................................................9 2.3.3 Sine........................................................................................................................................10 3. HOW TO USE RC-51......................................................................................................13 3.1 Introduction....................................................................................................................................13 3.1.1 Organization of the directories................................................................................................13 3.2 Compilation....................................................................................................................................13 3.2.1 Files generated.......................................................................................................................14 3.2.2 QUIET control.........................................................................................................................14 3.2.3 Linker......................................................................................................................................14 3.3 Compilation from Ride7.................................................................................................................15 3.4 Provided source.............................................................................................................................15 4. LANGUAGE EXTENSIONS.............................................................................................16 4.1 Extension to the ANSI standard.....................................................................................................16 4.1.1 Few restrictions.......................................................................................................................16 4.1.2 Superset.................................................................................................................................16 - 2 - RC-51 4.1.3 New keywords........................................................................................................................16 4.1.4 Global directive.......................................................................................................................16 4.2 Reserved keywords.......................................................................................................................16 4.3 New type qualifiers.........................................................................................................................17 4.3.1 Space qualifiers......................................................................................................................17 4.3.2 Code.......................................................................................................................................18 4.3.3 xdata, data, idata, pdata ........................................................................................................18 4.3.4 bdata.......................................................................................................................................19 4.3.5 Bit space qualifier...................................................................................................................19 4.3.6 sfr...........................................................................................................................................20 4.3.7 sbit..........................................................................................................................................20 4.3.8 Generic...................................................................................................................................21 4.4 Absolute allocation by ‘at’...............................................................................................................21 4.5 New function attributes..................................................................................................................22 4.5.1 Interrupt..................................................................................................................................22 4.5.2 using (setting).........................................................................................................................22 4.5.3 Reentrant ...............................................................................................................................23 4.5.4 alien........................................................................................................................................23 4.5.5 Model qualification..................................................................................................................24 4.6 asm instruction..............................................................................................................................24 4.7 Intrinsic functions...........................................................................................................................25 5. IMPLEMENTATION.........................................................................................................26 5.1 Base data types.............................................................................................................................26 5.1.1 List of base types....................................................................................................................26 5.1.2 Big endian storage rules.........................................................................................................26 5.1.3 Bit type....................................................................................................................................26 5.1.4 Standard word (int) width........................................................................................................27 5.1.5 Floating point precision...........................................................................................................27 5.2 Pointers.........................................................................................................................................28 5.2.1 General...................................................................................................................................28 5.2.2 Why use memory specific pointers?.......................................................................................28 5.2.3 Pointer location.......................................................................................................................29 5.2.4 Semantic rules for qualified pointers.......................................................................................29 5.2.5 Handling of pointers in function parameters............................................................................31 5.3 Recursion......................................................................................................................................31 5.4 Interfacing C programs with MA51.................................................................................................32 5.5 Parameter passing conventions.....................................................................................................32 5.5.1 Rules......................................................................................................................................32 - 3 - RC-51 5.5.2 Examples................................................................................................................................35 5.6 Segment naming convention.........................................................................................................35 5.7 Parameter convention....................................................................................................................35 5.8 Values returned by functions..........................................................................................................36 5.9 Predefined names..........................................................................................................................36 5.10 External stack (HUGE model or EXTSTK control).......................................................................37 5.11 Internal xdata (XRAM).................................................................................................................37 5.12 Startup code................................................................................................................................37 6. PRAGMAS.......................................................................................................................38 6.1 Using pragmas..............................................................................................................................38 6.1.1 What is a pragma?.................................................................................................................38 6.1.2 Indicating a pragma................................................................................................................38 6.1.3 List of pragmas.......................................................................................................................39 6.2 Environment pragmas....................................................................................................................41 6.2.1 Reference directory................................................................................................................41 6.2.2 Errors and warnings................................................................................................................41 6.2.3 Listing file................................................................................................................................42 6.2.4 Object or assembler source file..............................................................................................45 6.3 Implementation controls.................................................................................................................48 6.3.1 Memory...................................................................................................................................48 6.3.2 Optimization............................................................................................................................55 6.3.3 Compliance pragma...............................................................................................................60 6.3.4 Debugging pragma.................................................................................................................61 7. LIBRARIES......................................................................................................................62 7.1 string.h...........................................................................................................................................62 7.2 stdio.h............................................................................................................................................63 7.2.1 putchar....................................................................................................................................63 7.2.2 getchar ...................................................................................................................................63 7.2.3 ungetchar................................................................................................................................63 7.2.4 _getkey...................................................................................................................................64 7.2.5 sprintf and printf......................................................................................................................64 7.2.6 sscanf.....................................................................................................................................64 7.3 stdlib.h...........................................................................................................................................66 7.4 type.h.............................................................................................................................................67 7.5 math.h............................................................................................................................................68 7.5.1 Double functions ....................................................................................................................68 - 4 - RC-51 7.5.2 Float functions .......................................................................................................................69 7.5.3 Long double functions.............................................................................................................70 7.6 reg5x.h...........................................................................................................................................70 8. APPENDIX.......................................................................................................................71 8.1 Math performances........................................................................................................................71 8.1.1 Integers...................................................................................................................................71 8.1.2 Float numbers.........................................................................................................................71 8.2 Error messages.............................................................................................................................72 8.2.1 Error types..............................................................................................................................72 8.2.2 Causes...................................................................................................................................72 8.2.3 System errors.........................................................................................................................72 8.2.4 Preprocessor errors................................................................................................................73 8.2.5 Compilation errors..................................................................................................................74 9. BIBLIOGRAPHY..............................................................................................................91 9.1 ‘C’ language...................................................................................................................................91 9.2 8051 microcontrollers....................................................................................................................91 10. FLIP8051 SPECIFIC DIRECTIVES ADDENDUM.........................................................92 11. INDEX............................................................................................................................93 12. HISTORY.......................................................................................................................96 - 5 - 1. Introduction RC-51 1. Introduction The ‘C’ language is the standard high level language for microcontroller based applications. Just as the PC is the standard for personal computing, the 8051 is the standard for 8-bit microcontrollers and the concept of a ‘C’ compiler for an 8051 executing on a PC is now familiar. The Raisonance RC-51 compiler version 3 is one of the most efficient compilers available. 1.1 Purpose of this manual This manual gives an installation procedure and overview of the RC-51. If you are using the compiler for the first time, please read this manual before compiling your first programs. The authors of this compiler have made every effort to comply with the ANSI standard for the ‘C’ language, but have added a number of extensions to cater for the unique 8051 architecture: ● New keywords. ● Controls specific to RC-51. ● New grammatical rules. 1.2 Scope of this manual This manual is applicable to Raisonance RC-51 compiler version 3 (or later). It describes the extensions to, and differences from, ANSI-C. It is not intended to be a ‘C’ language tutorial nor an introduction to the grammar. For information on the language "The ‘C’ language" by B.W. Kerninghan and D.M. Ritchie is highly recommended and is the standard reference in this field. This manual does not provide details of 8051 architecture. Previous experience of 8051 architecture is desirable but not mandatory, knowledge of the 8051 instruction set is not necessary. Other relevant documents are described in Chapter 9 Bibliography. 1.3 Additional help or information Please visit the Raisonance website: http://www.raisonance.com/ and the forum http://www.raisonance.com/Forum/punbb/ or contact Raisonance. Address: Raisonance S.A.S. 17, Avenue Jean Kuntzmann, 38330 Montbonnot Saint Martin France Telephone: +33 4 76 61 02 30 Fax: +33 4 76 41 81 68 Email: [email protected] If you find any errors or omissions, or if you have suggestions for improving this manual, please let us know by email. - 6 - RC-51 2. Overview 2. Overview The 8051 architecture uses 64 Kbytes of address space (16-bit addressing) for both code and data. 2.1 Organization of the manual ● The contents are given at the beginning of the manual. ● The overview contains some examples as a guide to the implementation. ● Section 3 describes how to install and run RC-51 (DOS version). ● Section 4 describes the extensions and differences between the RC-51 and ANSI-C specifications. ● Section 5 explains the implementation needed to optimise the generated code and link programs written in assembler language with those written for RC-51. ● Section 6 describes the compiler controls (or controls entered as command lines). ● Section 7 gives a list of the functions available in RC51x.LIB and RC51Fx.LIB libraries. ● Finally, appendices discuss performances and lists error messages. 2.2 Differences between version 3 and previous versions This section describes the differences between the Raisonance RC-51 compiler version 3 (or later) and earlier versions. It may be skipped if you did not use previous versions. Numerous features have been added compared to earlier versions. In particular it is compatible with the 80C251 microcontroller and is one of the most efficient compilers on the market. 2.2.1 Storage rules When storing 16-bit words, compilers for 8-bit microcontrollers use either little-endian storage (lower order byte at lower address) or big-endian (higher order byte at lower address). As the little-endian convention was the most used and is a little more efficient (for variables in external RAMs), it was chosen for previous Raisonance RC-51 compilers. However, to be compatible with the 80C251, the Raisonance RC-51 compiler architecture has been rewritten with big-endian storage conventions, which has no effect on ‘C’ source files, but programmers mixing ‘C’ and assembler will have to check memory allocations particularly when 16-bit numbers are passed into functions as parameters. 2.2.2 Extended control management 2.2.2.1 Implementation controls Implementation controls affect the nature of the generated code through the compilation model and memory size, variables, registers and functions parameters management, and interrupt handling. The following list summarizes the main features of this new RC-51 compiler concerning implementation controls. ● The compilation model and program memory size may be precisely controlled allowing you to choose between long or absolute calls and jumps, to specify the stack location, and to specify the functions return addresses location. Five compilation models are available (TINY, SMALL, COMPACT, LARGE, HUGE) for three program memory sizes (SMALL, COMPACT, LARGE). (For memory models see Section 5.3.1.1) ● The definition of PUBLIC symbol DEFINEJOKER now allows untyped external references. ● As in the previous versions, you may use different register banks via REGISTERBANK (register bank reservation) and using (register bank selection) directives, which have replaced the setting statement. Register usage of external functions may even be optimized with reference to a user file (REGFILE). - 7 - 2. Overview RC-51 ● The interrupt vector table location may be defined by two directives that specify the table offset (INTVECTOR) and the interval between adjacent interrupt vectors (INTERVAL). Interrupts may be disabled with the DISABLE control, which acts as a macro. 2.2.2.2 Environment controls Unlike implementation controls, environment controls only specify the compilation context. The following list summarizes the main features of the new RC-51 compiler concerning environment controls. ● The listing file may be enabled ([NO]PRINT), its format controlled (PAGEWIDTH), and its content controlled: macro expansion may be performed (PREPRINT), the assembler source file list may be included (LISTINCLUDE) as well as conditional blocks affected (or not) by compilation ([NO]COND) into the listing file. Lastly, a symbol table containing all the symbols used by the compiled source file may be displayed in the listing file (SYMBOLS). ● The compiler may generate an assembler source file or an object file. When generated, the object file name may be specified (OBJECT directive), additional information used by Raisonance development tools may be included (OBJECTEXTEND) as well as debugging information (DEBUG). 2.2.3 Miscellaneous ● The previous Raisonance RC-51 compiler had three BCD float types. Two new IEEE-754 types have been added. IEEE types are less accurate, but faster. ● Bit keyword was, in the previous version, a space qualification. In the new version, it has become a Type and may be applied, for example, to the Type returned by a function. ● The four external stacks have been unified into one, to optimise interrupt service code. The XDATA space that contained this stack is now named PDATA. ● The intrinsic keyword qualifies library functions that have to be inserted ‘in-line’. In RC-51 some ‘intrinsic’ functions have been included which result in ‘in-line’ code rather than an ACALL or LCALL instruction. The prototypes of these functions are given in the intrins.h header file, for example swapnibble. ● Spatial qualifiers can be applied to local variables. 2.2.4 Optimization Due to numerous improvements, the Raisonance RC-51 compiler version 3 is one of the most efficient compilers, based on usual benchmarks results. These improvements are because: ● Code factorization (common sub-expressions) has been made more efficient. ● Registers usage has been highly optimized. ● All libraries have been re-examined and optimized. 2.2.5 Environment The linker has been modified in order to: ● Automatically include necessary libraries. ● Optimize some library functions. In particular, the printf function has been re-written to have more capability. 2.2.6 Additional attributes for functions Compared to the previous versions, version 3 allows: ● A function to return a bit value (bit has become a type that may be used as any other type of the ‘C’ language). ● A function may be re-entrant, which means that it may be called by several processes at the same time. - 8 - RC-51 2. Overview ● A function may be declared with the alien attribute and consequently may call, or be called from, PL/M-51 routines. ● A function may be model qualified which means that it may have a personal model, different from the general model. ● A function’s parameters may be space qualified. 2.3 First programs Here are some simple examples with comments, as an introduction to Raisonance RC 51 compiler: 2.3.1 "Hello !" #include <stdio.h> #include <reg51.h> void main(void) { printf ("\nHello world !"); while (1); } /* Program invoked by: rc51 sample1.c l51 sample1.obj */ ● The RC51S.LIB library contains the standard functions, including the ANSI standard printf function which, by default, transmits data on the 8051 UART. It is not necessary to specify the libraries in the command line as they are automatically searched for by the linker. ● The initialization function ensures automatic initialization and execution of the main() function. But, because there is no operating system, the function must include an infinite « while (1) » loop. ● The string \nHello world! is automatically placed in CODE memory space. ● The include file stdio.h concerns ANSI libraries function declarations, adapted for the 8051. ● The include file reg51.h declares the 8051’s Special Functions Registers (SFRs). This declaration was unnecessary in this example, as the main function does not refer to SFRs. 2.3.2 Counter #include <stdio.h> #include <reg51.h> xdata long unsigned counter = 0; /* External interrupt 0 */ void it_ext0 (void) interrupt 1 using 1 { /* Increment counter */ counter++; - 9 - 2. Overview RC-51 } void main(void) { long unsigned n = counter; EX0 = 1; IT0 = 1; //Edge level interrupt EA = 1; while (1) if ( n!= counter ) printf ("\nCount: %ld", n = counter ); } /* Program invoked by: rc51 sample2.c SMALL l51 sample2.obj */ ● The command line includes the keyword SMALL, which specifies the use of the small memory model. It was not necessary, as the SMALL model is the default one. ● New keywords such as interrupt and xdata have been added to the language, which is consequently a superset of ANSI-C. ● The it_ext0 () function is an interrupt routine. The compiler automatically generates the interrupt vector. To change the register bank, use the using statement followed by the register bank reference. 2.3.3 Sine The following function is an implementation of sine(x). Functions declared in the math.h file do not need to be rewritten by the programmer and should be used where ever possible as they were crafted in assembly language to be as efficient as possible. #include <stdio.h> #pragma auto #define ORDER 7 /* Max power. */ #define NB_OP (ORDER/2) #define M_PI 3.141592 /* Global variables */ float f1; long unsigned fact (unsigned n) /* n! */ { return ( n == 1 ) ? 1: n * fact ( n - 1 ); } double sinus (double f1) { - 10 -
Description: