11.5.2 Data Initialization Template

As noted in Standard Data Section Names, the MPLAB XC32 Language Tools support BSS-type sections (memory that is initialized to zero) 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          */
  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 CodeDescription
0Fill the output section with zeros
1Copy bytes from word in the data array

Format 0 is used to zero len bytes of data starting at the destination address; there is no further data required.

Format 1 is used to copy the data from flash immediately following the record. len bytes are copied. The end of the record is padded to the next aligned word.