The HI-TECH PICC18 compiler places objects declared as const
into program space by default. No special qualifiers like C18's RAM
/ROM
are needed:
3.5.3 Objects in Program Space
const
objects are usually placed in program space. On the PIC18 devices, the program space is
byte-wide, the compiler stores one character per byte location and values are read using the table
read instructions. All const
-qualified data objects and string literals are placed in the const
psect.
The const
psect is placed at an address above the upper limit of RAM since RAM and const
pointers use this address to determine if an access to ROM or RAM is required.
Note that placing frequently updated data in the microcontroller's flash memory may not be such a good idea, as flash has a limited number of program/erase cycles.
far
pointers can be used to dereference program memory:
3.4.12.2 Const and Far Pointers
const
and far
pointers can either be 16 or 24 bits wide. Their size can be toggled with the --CP=24
or --CP=16
command line option. The code used to dereference them also changes with their size.
The same pointer size must be used for all modules in a project.
A pointer to far
is identical to a pointer to const
, except that pointers to far
may be used to
write to the address they hold. A pointer to const
objects cannot be used to write as the const
qualifier imposes that the object is read-only.
const
and far
pointers which are 16 bits wide can access all RAM areas and most of the program
space. At runtime when dereferenced, the contents of the pointer are examined. For addresses above
the upper limit of RAM the program space is accessed using table read or table write instructions.
Addresses below the upper limit of RAM access the data space. Even if the address held by a pointer
to const
is in RAM, the RAM location may not be changed.
The default linker options always place const
data at addresses above the upper limit of the data
space so that the correct memory space is accessed when dereferencing with pointers.
If the target device selected has more than 64k bytes of program space memory, then only the
lower 64k bytes may be accessed with 16-bit wide pointers. Provided that all program space objects
that need to be dereferenced are in the lower 64k bytes, 16-bit pointers to const
and far
objects
may still be used. The smaller pointer size results in less RAM required and less code produced and
so should be used whenever possible.
const
and far
pointers which are 24 bits wide can access all RAM areas and all of the program
space. At runtime when dereferenced, the contents of the pointer are examined. If bit number 21
in the address is set, the address is assumed to be a RAM address. Bit number 21 of the address is
then ignored. If Bit number 21 is clear, then the address is assumed to be of an object in the program
space and the access is performed using table read or table write instructions. Again, no writes to
objects are permitted using a pointer to const
.
Note that when dereferencing a 24-bit pointer, the most significant implemented bit (bit number
21) of the TBLPTRU
register may be overwritten. This bit may be used to enable access to the
configuration area of the PIC18 device. If loading the table pointer registers from hand-written
assembler code, make no assumptions about the state of bit number 21 prior to executing table read
or write instructions.
The quotes are from HI-TECH PICC18 v9.51 manual.