11.6.2 Data Initialization Template

As noted in 11.6.1 Standard Data Section Names, the MPLAB XC-DSC Language Tools support BSS-type sections (memory that is not initialized) as well as data-type sections (memory that receives initial values). The data-type sections receive initial values at start-up and the BSS-type sections are filled with zeros.

A generic data initialization template is used that supports any number of arbitrary BSS-type sections or data-type sections. The data initialization template is created by the linker and is loaded into an output section named .dinit in program memory. Start-up code in the run-time library interprets the template and initializes data memory accordingly.

The data initialization template contains one record for each output section in data memory. The template is terminated by a null instruction word. The format of a data initialization record is:

/* data init record */
struct data_record {
  char         *dst;      /* destination address  */
  unsigned int len;       /* length in bytes      */
  unsigned int format:7;  /* format code          */
  unsigned int page:9;    /* destination page     */
  char         dat[0];    /* variable length data */
};

The first element of the record is a pointer to the section in data memory. The second and third elements are the section length and format code, respectively. The fourth element is the page value of the section. On EDS devices, the page value will be in the range 0x001 to 0x1FF. On all other devices, the page value will be zero. The last element is an optional array of data bytes. For BSS-type sections, no data bytes are required.

The format code has three possible values.

Table 11-2. Format Code Values
Format Code Description
0 Fill the output section with zeros
1 Copy 2 bytes of data from each instruction word in the data array
2 Copy 3 bytes of data from each instruction word in the data array

By default, data records are created using format 2. Format 2 conserves program memory by using the entire 24-bit instruction word to store initial values. Note that this format causes the encoded instruction words to appear as random and possibly invalid instructions if viewed in the disassembler.

Format 1 data records may be created by specifying the --no-pack-data option. Format 1 uses only the lower 16 bits of each 24-bit instruction word to store initial values. The upper byte of each instruction word is filled with 0x0 by default and causes the template to appear as NOP instructions if viewed in the disassembler (and will be executed as such by the DSC device). A different value may be specified for the upper byte of the data template with the --fill-data option.