The following listing shows a method of directly allocating variables in a named segment, rather than using a #pragma.
#pragma DATA_SEG __SHORT_SEG tiny #pragma DATA_SEG not_tiny #pragma DATA_SEG __SHORT_SEG tiny_b #pragma DATA_SEG DEFAULT int i@"tiny"; int j@"not_tiny"; int k@"tiny_b";
With some pragmas in a common header file and with another macro definition, you can allocate variables depending on the macro.
Declaration = <TypeSpec> <Declarator>[@"<Section>"][=<Initializer>];
Variables declared and defined with the @"section" syntax behave exactly like variables declared after their respective pragmas.
Specify the section name using a section pragma before the declaration occurs as shown in the following listings.
#pragma DATA_SEC __SHORT_SEG MY_SHORT_DATA_SEC #pragma DATA_SEC MY_DATA_SEC #pragma CONST_SEC MY_CONST_SEC #pragma DATA_SEC DEFAULT // not necessary, but is good practice #pragma CONST_SEC DEFAULT // not necessary, but is good practice int short_var @"MY_SHORT_DATA_SEC"; // OK, accesses are short int ext_var @"MY_DATA_SEC" = 10; // OK, goes into // MY_DATA_SECT int def_var; / OK, goes into DEFAULT_RAM const int cst_var @"MY_CONST_SEC" = 10; // OK, goes into MY_CONST_SECT
SECTIONS MY_ZRAM = READ_WRITE 0x00F0 TO 0x00FF; MY_RAM = READ_WRITE 0x0100 TO 0x01FF; MY_ROM = READ_ONLY 0x2000 TO 0xFEFF; MY_STACK = READ_WRITE 0x0200 TO 0x03FF; END PLACEMENT MY_CONST_SEC,DEFAULT_ROM INTO MY_ROM; MY_SHORT_DATA_SEC INTO MY_ZRAM; MY_DATA_SEC, DEFAULT_RAM INTO MY_RAM; SSTACK INTO MY_STACK; END