5.3.6.3.1 Pointers to Both Memory Spaces

When a data pointer is assigned the address of one or more objects that have been allocated memory in the data space and also assigned the address of one or more objects that have been allocated memory in the program memory space, the pointer is said to have targets with mixed memory spaces. Such pointers fall into one of the mixed target space pointer classifications (listed in 5.3.6.3 Data Pointers) and the address will be encoded so that the target memory space can be determined at runtime. The encoding of these pointer types are as follows.

For the Baseline/Mid-range 16-bit mixed target space pointer, the MSb of the address (i.e., bit number 15) indicates the memory space that the address references. If this bit is set, it indicates that the address is of something in program memory; clear indicates an object in the data memory. The remainder of this address represents the full address in the indicated memory space.

For the PIC18 16-bit mixed target space pointer, any address above the highest data space address is that of an object in the program space memory; otherwise, the address is of a data space memory object.

For the PIC18 24-bit mixed target space pointer, bit number 21 indicates the memory space that the address references. If this bit is set, it indicates that the address is of an object residing in data memory; if it is clear, it indicates an object in the program memory. The remainder of this address represents the full address in the indicated memory space. Such pointers cannot be used to access special memory areas at high addresses (which have bit number 21 set), such as the Device information Area (DIA). Note that for efficiency reasons, the meaning of the memory space bit is the opposite to that for Baseline and Mid-range devices.

If assembly code references a C pointer, the compiler will force that pointer to become a 16-bit mixed target space pointer, in the case of Baseline or Mid-range programs, or a 24-bit mixed target space pointer, for PIC18 programs. These pointer types have unrestricted access to all memory areas and will operate correctly, even if assignments (of a correctly formatted address) are made to the pointer in the assembly code.