3.6 User-defined Psects for PIC18 Devices

Rather than use the PIC Assembler's predefined psects, you will need to define your own unique psects to have code or objects linked to special locations. These psects can then be linked to the required address without affecting the placement of the remainder of your code or data.

In this chapter's example, the program's entry point consists of a short segment of code that is to be executed immediately after a Reset. This code, therefore, must be linked at the Reset vector, address 0.

To place code or objects into a psect, use a PSECT directive and the name of the psect you wish to use. The first time a psect is used in a module, you must define the psect flags that are relevant for the psect. These flags control which memory space the psect will reside in and further refine how the psect is linked. The flags can be omitted on subsequent uses of the PSECT directive for the same psect. The flags on psects with the same name but in other modules must agree.

In the example, the psect that was used to hold the Reset code was defined as follows, but note that the flags used with this psect are only relevant for PIC18 devices.
PSECT resetVec,class=CODE,reloc=2
resetVec:
    goto main

The psect name being defined and selected by this directive is resetVec. The label and goto instruction that follow will be part of this psect. Note that psect names are case sensitive.

A psect is typically associated with a linker class through the class psect flag. In the above example, the resetVec psect has been associated with a linker class called CODE, which is a class predefined by the assembler. A class defines one or more ranges of memory in which the psect can be linked, if desired. As the resetVec psect in this example must be explicitly linked to an absolute address (the Reset vector), the class association is not actually necessary, but it is common to always specify a psect's class, if only to make the psect's listing in the map file easier to find. Psects associated with a class can still be placed at arbitrary locations using a linker option, in which case, the class's memory ranges are ignored. The predefined classes are listed in the MPLAB® XC8 PIC® Assembler User's Guide.

The reloc flag in the resetVec psect ensures that the start of the psect is aligned to an address that is a multiple of the flag's value. It is critical that this flag be set to 2 for any PIC18 psects that hold executable code, as instructions must be word aligned on these devices. Set it to 1 (the default value if no reloc flag is specified) for psects holding instructions for other devices. With this flag in place, the linker will issue an error if the linker option you use to place the psect, which is shown in section Building the Example, requests an incompatible address. For other situations where a psect has allocated memory anywhere in a linker class range, the linker will choose an address that is aligned to the specified value.

Any of the predefined psects used to hold instructions, such as code, specify the relevant reloc value for the device you are using.