- •Chapter 1. Introduction
- •Support for all 8051 Variants
- •Books About the C Language
- •Chapter 2. Compiling with the Cx51 Compiler
- •Environment Variables
- •Running Cx51 from the Command Prompt
- •ERRORLEVEL
- •Cx51 Output Files
- •Control Directives
- •Directive Categories
- •Reference
- •Chapter 3. Language Extensions
- •Keywords
- •Memory Areas
- •Program Memory
- •Internal Data Memory
- •External Data Memory
- •Far Memory
- •Special Function Register Memory
- •Memory Models
- •Small Model
- •Compact Model
- •Large Model
- •Memory Types
- •Explicitly Declared Memory Types
- •Implicit Memory Types
- •Data Types
- •Bit Types
- •Special Function Registers
- •sbit
- •Absolute Variable Location
- •Pointers
- •Generic Pointers
- •Pointer Conversions
- •Abstract Pointers
- •Function Declarations
- •Function Parameters and the Stack
- •Passing Parameters in Registers
- •Function Return Values
- •Specifying the Memory Model for a Function
- •Specifying the Register Bank for a Function
- •Register Bank Access
- •Interrupt Functions
- •Reentrant Functions
- •Chapter 4. Preprocessor
- •Directives
- •Stringize Operator
- •Predefined Macro Constants
- •Chapter 5. 8051 Derivatives
- •Analog Devices MicroConverter B2 Series
- •Atmel 89x8252 and Variants
- •Dallas 80C320, 420, 520, and 530
- •Arithmetic Accelerator
- •Data Pointers
- •Library Routines
- •Philips 8xC750, 8xC751, and 8xC752
- •Philips 80C51MX Architecture
- •Philips and Atmel WM Dual DPTR
- •Customization Files
- •STARTUP.A51
- •INIT.A51
- •XBANKING.A51
- •Basic I/O Functions
- •Memory Allocation Functions
- •Optimizer
- •General Optimizations
- •Options for Code Generation
- •Segment Naming Conventions
- •Data Objects
- •Program Objects
- •Interfacing C Programs to Assembler
- •Function Parameters
- •Parameter Passing in Registers
- •Parameter Passing in Fixed Memory Locations
- •Function Return Values
- •Using the SRC Directive
- •Register Usage
- •Overlaying Segments
- •Example Routines
- •Small Model Example
- •Compact Model Example
- •Large Model Example
- •Data Storage Formats
- •Bit Variables
- •Signed and Unsigned Long Integers
- •Generic and Far Pointers
- •Floating-point Numbers
- •Accessing Absolute Memory Locations
- •Absolute Memory Access Macros
- •Linker Location Controls
- •The _at_ Keyword
- •Debugging
- •Chapter 7. Error Messages
- •Fatal Errors
- •Actions
- •Errors
- •Syntax and Semantic Errors
- •Warnings
- •Chapter 8. Library Reference
- •Intrinsic Routines
- •Library Files
- •Standard Types
- •va_list
- •Absolute Memory Access Macros
- •CBYTE
- •CWORD
- •DBYTE
- •DWORD
- •FARRAY, FCARRAY
- •FVAR, FCVAR,
- •PBYTE
- •PWORD
- •XBYTE
- •XWORD
- •Routines by Category
- •Buffer Manipulation
- •Character Conversion and Classification
- •Data Conversion
- •Math Routines
- •Memory Allocation Routines
- •Stream Input and Output Routines
- •String Manipulation Routines
- •Miscellaneous Routines
- •Include Files
- •8051 Special Function Register Include Files
- •ABSACC.H
- •ASSERT.H
- •CTYPE.H
- •INTRINS.H
- •MATH.H
- •SETJMP.H
- •STDARG.H
- •STDDEF.H
- •STDIO.H
- •STDLIB.H
- •STRING.H
- •Reference
- •Compiler-related Differences
- •Library-related Differences
- •Appendix B. Version Differences
- •Version 6.0 Differences
- •Version 5 Differences
- •Version 4 Differences
- •Version 3.4 Differences
- •Version 3.2 Differences
- •Version 3.0 Differences
- •Version 2 Differences
- •Appendix C. Writing Optimum Code
- •Memory Model
- •Variable Location
- •Variable Size
- •Unsigned Types
- •Local Variables
- •Other Sources
- •Appendix D. Compiler Limits
- •Appendix E. Byte Ordering
- •Recursive Code Reference Error
- •Problems Using the printf Routines
- •Uncalled Functions
- •Using Monitor-51
- •Trouble with the bdata Memory Type
- •Function Pointers
- •Glossary
- •Index
154 |
Chapter 6. Advanced Programming Techniques |
|
|
XBANKING.A51
This file provides routines for far (HDATA) and const far (HCONST) memory type support. The extended LX51 linker/locater manages the extended address spaces HDATA and HCONST that are addressed with far and const far. The Cx51 Compiler uses a 3-byte generic pointer to access these memory areas. Variables defined with the far memory type are placed in the memory class HDATA. Variables defined with const far get the memory class HCONST. The LX51 linker/locater allows you to locate these memory classes in the physical 16MB code or 16MB xdata spaces. To use far memory with the C51 Compiler for classic 8051 devices you must use the “VARBANKING” directive described on page 84.
The memory types far and const far provide support for the large code/xdata spaces of new 8051 devices. If the CPU you are using provides an extended 24-bit DPTR register, you may adapt the default version of the file XBANKING.A51 and define the symbols listed in the following table.
Constant Name Description
?C?XPAGE1SFR SFR address of DPTR page register that contains DPTR bit 16-23.
?C?XPAGE1RST Reset value of the ?C?XPAGE1SFR to address the X:0 region. This setting used by the C51 compiler when you are using the VARBANKING(1) directive. With VARBANKING(1) the C51 compiler saves the ?C?XPAGE1SFR at the beginning of interrupt functions and sets this register to the ?C?XPAGE1RST value
The far memory type allows you to address special memory areas like EEPROM 6 space or strings in code banking ROM. Your application accesses these memory
areas as if they are a part of the standard 8051 memory space. Example programs in the folder C51\EXAMPLES\FARMEMORY show how to use the C51 far memory type on classic 8051 devices. If an example that fulfills your requirements is not provided, you may adapt the access routines listed in the table below.
Access Routine |
Description |
|
|
?C?CLDXPTR, ?C?CSTXPTR load/store a BYTE (char) in extended memory.
?C?ILDXPTR, ?C?ISTXPTR load/store a WORD (int) in extended memory.
?C?PLDXPTR, ?C?PSTXPTR load/store a 3-BYTE pointer in extended memory.
?C?LLDXPTR, ?C?LSTXPTR load/store a DWORD (long) in extended memory.
Keil Software — Cx51 Compiler User’s Guide |
155 |
|
|
Each access routine gets as a parameter the memory address in a 3-byte pointer representation in the CPU registers R1/R2/R3. The register R3 holds the memory type value. For classic 8051 devices, the Cx51 compiler uses the following memory type values:
R3 Value |
Memory Type |
Memory Class |
Address Range |
0x00 |
data / idata |
DATA / IDATA |
I:0x00-I:0xFF |
0x01 |
xdata |
XDATA |
X:0x0000-X:0xFFFF |
0x02-0x7F |
far |
HDATA |
X:0x010000-X:0x7E0000 |
0x80-0xFD |
far const |
HCONST |
C:0x800000-C:0xFD0000 (far const is |
|
|
|
mapped into the banked memory areas) |
0xFE |
pdata |
XDATA |
one 256-byte page in XDATA memory |
0xFF |
code |
CODE / CONST |
C:0x0000-C:0xFFFF |
The R3 values 0x00, 0x01, 0xFE and 0xFF are already handled within the runtime library. Only the values 0x02 - 0xFE are passed to the XPTR access routines described above. The AX51 macro assembler provides the MBYTE operator that calculates the R3 value that needs to be passed to the XPTR access function. Below is an AX51 Assembler example for using XPTR access functions:
MOV |
R1,#LOW |
(variable) |
; gives LSB address byte of variable |
MOV |
R1,#HIGH |
(variable) |
; gives MSB address byte of variable |
MOV |
R1,#MBYTE (variable) |
; gives memory type byte of variable |
|
CALL |
?C?CLDXPTR |
|
; load BYTE variable into A |
6
156 |
Chapter 6. Advanced Programming Techniques |
|
|
Basic I/O Functions
The following files contain the source code for the low-level stream I/O routines. When you use the µVision2 IDE, you can simply add the modified versions to the project.
C Source File |
Description |
PUTCHAR.C |
Used by all stream routines that output characters. You may adapt this |
|
routine to your individual hardware (for example, LCD or LED displays). |
|
The default version outputs characters via the serial interface. An |
|
XON/XOFF protocol is used for flow control. Linefeed characters (‘\n’) are |
|
converted into carriage return/linefeed sequences (‘\r\n’). |
GETKEY.C |
Used by all stream routines that input characters. You may adapt this routine |
|
to your individual hardware (for example, for matrix keyboards). |
|
The default version reads a character via the serial interface. No data |
|
conversions are performed. |
Memory Allocation Functions
The following files contain the source code for the memory allocation routines.
|
|
C Source File |
|
Description |
|
|
|
CALLOC.C |
|
Allocates memory for an array from the memory pool. |
|
|
|
FREE.C |
|
Returns a previously allocated memory block to the memory pool. |
|
|
|
INIT_MEM.C |
|
Specifies the location and size of a memory pool from which memory may be |
|
6 |
|
|
|
allocated using the malloc, calloc, and realloc functions. |
|
|
MALLOC.C |
|
Allocates memory from the memory pool. |
|
|
|
|
|
|
||
|
|
REALLOC.C |
|
Resizes a previously allocated memory block. |
|
|
|
|
|
|
|