Back to ESAcademy Home Page


www.philipsmcu.com8051 Memory Configuartions with C Compilers

by Andy Ayre, ESAcademy
based on the C51Primer, by Mike Beach, Hitex UK

The Keil and Raisonance C Compilers provide a variety of memory models. When do we choose which model?

[ Introduction | Available memory models | Choosing a model | Using a model ]

 

Home

News

Training Classes

Products

Consulting

Technical Library

Contact Us

Recommended Books

Choosing The Best Memory Configuration/Model

With the three memory models, a decision has to be made as to which one to use. Single chip 8051 users may only use the SMALL model, unless they have an external RAM fitted which can be page addressed from Port 0 and optionally, Port 2, using MOVX A,@R0 addressing.

This permits the COMPACT model. While it is possible to change the global memory model half way through a project, it is not recommended!

SMALL: Total RAM 128 bytes

Using this memory model, the number of global variables must be kept to a minimum to allow the linker's OVERLAY function to work to best effect. With 8052/32 versions, the manual use of the 128 byte IDATA area above 80H can allow applications additional variable storage space, however the amount of space required for the stack must be kept in mind.

The SMALL model can support very large programs by manually forcing large and/or slow data objects in to an external RAM, if fitted. Also variables that need to be viewed in real time are best located here, as dual-ported emulators (like the ones from Hitex and Raisonance) can read their values on the fly. This approach is generally best for large, time-critical applications, as the SMALL global model guarantees that local variables and function parameters will have the fastest access, while large arrays can be located off-chip.

COMPACT: Total RAM 256 bytes off-chip, 128 or 256 bytes on-chip.

Suitable for programs where, for example, the on-chip memory is applied to an operating system. The compact model is rarely used for an entire program, but more usual in combination with the SMALL switch reserved for interrupt routines.

COMPACT is especially useful for programs with a large number of medium speed 8 bit variables, for which the MOVX A,@R0 is very suitable.

It can be useful in applications where stack usage is very high, meaning that data needs to be off-chip. Note that register variables are still used, so the loss of speed will not be significant in situations where only a small number of local variables and/or passed parameters are used.

LARGE: Total RAM up to 64KB, 128 or 256 bytes on-chip.

Permits slow access to a very large memory space and is perhaps the easiest model to use. Again, not often used for an entire program, but in combination with SMALL. As with COMPACT, register variables are still used and so efficiency remains reasonable.
In summary, there are five memory spaces available for data storage, each of which has particular pros and cons.

Here are some recommendations for the best use of each:

DATA: 128 bytes; SMALL model default location

Best For:
Frequently accessed data requiring the fastest access. Interrupt routines whose run time is critical should use DATA, usually by declaring the function as "SMALL". Another recommended usage is for background code that is frequently run and has many parameters to pass. If you are using re-entrant functions, the re-entrant stacks should be located here as a priority.

Worst For:
Variable arrays and structures that contain more than a few bytes.

IDATA; 128 bytes or 256 bytes; Not model-dependant

Best For:
Fast access to data arrays and structures of limited size (up to around 32 bytes each) but not totalling more than 64 or so bytes. As these data types require indirect addressing, they are ideally placed in the indirectly addressable area. The stack is also located in IDATA as it is indirectly addressed.

Worst For:
Large data arrays and/or fast access words.

CODE: 64K bytes

Best For:
Constants and large lookup tables, plus opcodes, of course!

Worst For:
Variables! It's ROM - Read Only Memory that can not be written to.

PDATA: 256 bytes; COMPACT model default area

Best For:
Medium speed interrupt and fast background char (8 bit) variables and moderate-sized arrays and structures. Also good for variables which need to be viewed in real-time using an in-circuit emulator with dual ported memory.

Worst For:
Very large data arrays and structure above 256 bytes.
Very frequently used data (in interrupts etc..).
Integer and long data.

XDATA; up to 64K bytes; LARGE model default area

Best For:
Large variable arrays and structures (over 256 bytes)
Slow or infrequently-used background variables. Also good for variables which need to be viewed in real-time using an in-circuit emulator with dual ported memory.

Worst For:
Frequently-accessed or fast interrupt variables.

[ Introduction | Available memory models | Choosing a model | Using a model ]