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 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 is 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 to which the address refers. If this bit is set, it indicates that the address is of something in program memory; a clear bit 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, the MSb (i.e. bit number
23) indicates the memory space to which the address refers. If this bit is set, it
indicates that the address is of an object residing in data memory; if it is clear, this
indicates an object in the program memory. The remainder of this address represents the
full address in the indicated memory space. The macro _PTR3_ADDR_MASK
can
be used to obtain the address bits encoded within 24-bit mixed-space pointers when building
for PIC18 devices. The macro is defined as a mask that when ANDed with an address will
strip away any bits used to identify the memory space of the pointer target, leaving just
the address in that memory space. Note that for efficiency reasons, the meaning of the
memory space bit is the opposite of 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.