Memory Model Selection When Using the MPLAB® XC16 Compiler

Last modified by Microchip on 2023/11/09 09:12

Why is it Important?

Many compilers employ memory models that act as a set of rules on how code and data are placed in memory areas on the target device and how they are accessed. The MPLAB® XC16 C compiler is one such compiler that uses memory models to allow you to take advantage of a relatively complex memory architecture with little effort.

The selection of the memory model settings is important because it can greatly affect the size of the code produced by the compiler and how fast this code will execute. The memory model will control the memory region in which data is linked and how it is accessed. Independent settings are available for basic scalar objects (such as char and int), aggregate objects (arrays and structures), and also for const-qualified objects. The location of objects will dictate the code sequence required to access them. The memory model settings also control the code sequences used to call functions which again affects your code’s performance.

The MPLAB XC16 C compiler will choose default memory model settings but these may not be the best for your project, so you should always review and select the best settings for each of your projects. By default, MPLAB XC16 will choose small-scalarlarge-data for devices that have more than 6 or 7 kbytes of data memory (depending upon the selected target family); otherwise, the compiler will choose a small-datasmall-scalar memory model.

If possible, use the small-data and small-scalar settings. These will fully utilize the near data memory of the device but they can only be used for small projects that have low (less than 6 or 7 kbytes) data-memory requirements. Selecting large-data along with small-scalar will allow the larger aggregate objects to use all the available data memory while allowing your small scalar objects to be efficiently accessed in near memory.

Selecting const-in-data is the most efficient of the const-object settings but this will increase data memory usage. Select the const-in-code setting, if data memory is limited.

The small-code model uses calls whose range is relative to their position. As your code size increases beyond the 32 kWord limit of these calls, it becomes more likely that relative calls will fail to reach their destination and your code will require the absolute calls used by the large-code model. Since the linker will generate an error if relative calls are out of range, start your projects using the small-code model and only swap to the large-code model if errors prevent your code from linking.

Remember that you can use a different memory model selection for each module in your project though you might have to qualify external references with the near and far specifiers to properly access them in mismatching modules. You can also qualify variables and functions individually using the near and far qualifiers if you wish to override the currently used memory model selection.

Consult your compiler’s User’s Guide for full information on the memory model options and their effects.