Vault 7: Projects
This publication series is about specific projects related to the Vault 7 main publication.
Memory Allocation and Access
2012-2013 Microchip Technology Inc. DS50002071C-page 163
10.8 PACKING DATA STORED IN FLASH
The 16-bit core families use a 24-bit Flash word size. The architecture supports the
mapping of areas of Flash into the data space, as discussed in
Section 10.4 Variables in Program Space. Unfortunately this mapping is only 16
bits wide to fit in with data space dimensions.
The compiler supports using the upper byte of Flash via packed storage. Use of this
upper byte can offer a code-size savings for large structures, but this is more expensive
to access. The type-qualifier __pack_upper_byte added to a declaration indicates
that the variable should be placed into Flash memory and use the upper byte. Unlike
other qualifiers in use with MPLAB XC16 C Compiler, such as __psv__, this qualifier
combines placement and access control.
10.8.1 Packed Example
__pack_upper_byte char message[] = "Hello World!\n";
will allocate the message string into Flash memory in such a way that the upper byte
of memory contains valid data.
There are no restrictions to the types of __pack_upper_byte data. The compiler will
'pack' structures as if __attribute__((packed)) had also been specified. This fur-
ther eliminates wasted space due to padding.
Like other extended type qualifiers, the __pack_upper_byte type qualifier enforces
a unique addressing space on the compiler; therefore, it is important to maintain this
qualifier when passing values as parameters. Do not be tempted to cast away the
__pack_upper_byte qualifier it won't work.
10.8.2 Usage Considerations
When using this qualifier, consider the following:
1. __pack_upper_byte data is best used for large data sets that do not need to
be accessed frequently or that do not have important access timing.
2. Sequential accesses to __pack_upper_byte data objects will improve access
performance.
3. A version of mempcy is defined in libpic30.h, and its prototype is:
void _memcpy_packed(void *dst, __pack_upper_byte void *src,
unsigned int len);
4. The following style of declaration is invalid for packed memory:
__pack_upper_byte char *message = "Hello World!\n";
Here, message is a pointer to __pack_upper_byte space, but the string "Hello
World!\n", is in normal const data space, which is not compatible with
__pack_upper_byte. There is no standard C way to specify a different source
address space for the literal string. Instead declare message as an object (such
as an array declaration in Section 10.8.1 Packed Example).
5. The TBLPAG SFR may be corrupted during access of a packed variable.
Protego_Release_01_05-Related-OEM-Documentation-MPLAB-XC16-C-Compiler.pdf