5.4.2.1.1 Objects in Common Data Memory

Most 8-bit PIC devices have a dedicated area of data memory that is directly accessible at all times, regardless of which memory bank is currently selected. On Baseline and Mid-range devices, this is known as common memory. On PIC18 devices, it is referred to as Access bank memory, and although it is implement in a different way, the benefits are similar. This guide uses the term 'common memory' to refer to this memory, regardless of which device is being discussed. As objects in common memory can be accessed without additional instructions to select a data bank, their use can lead to smaller code that is faster to execute.

The size of the common memory area varies considerably; from a few bytes on some Mid-range devices, to many tens of bytes of Access bank memory on PIC18 devices. Some Baseline devices do not implement any common memory at all. Check your device data sheet to see how much common memory is available. The compiler defines the preprocessor macro _COMMON_SIZE_ to indicate the size of common memory for the selected device, allowing code to be conditionally customized. Common memory shares addresses with regular banked data memory.

The compiler often reserves an amount of common memory for storage of data used by the program. There is no means to prevent this from taking place; however any unused common memory can be used by your program. It is best used for objects that are frequently accessed, or which are accessed by code that must execute quickly.

To place objects in the common memory and have them accessed without bank selection instructions, use the near type qualifier (see 5.3.9.4 Near Type Qualifier) in conjunction with the -maddrqual option (see 4.6.1.1 Addrqual Option). The argument given to the -maddrqual option determines the compiler's response should there not be common memory available.

Do not try to force objects into common memory if the near qualifier does not have the desired effect. Attempts to place an object into common memory by making it absolute (__at() construct) and with an address somewhere in the common area might correctly locate the object, but that object might still be accessed as if it was banked. Nor should you attempt to allocate objects to one of the sections that will be linked into common memory (using the __section specifier).

If the near qualifier is not honored, check the map file to see the amount of free memory in the linker class representing the common area. Typically this class is called COMMON.