Sometimes it is useful to have the variable directly allocated in a named segment instead of using a #pragma. The following listing is an example of how to do this.
#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";
So with some pragmas in a common header file and with another definition for the macro, it is possible to allocate variables depending on a macro.
Declaration = <TypeSpec> <Declarator> [@"<Section>"][=<Initializer>];
Variables declared and defined with the @"section" syntax behave exactly like variables declared after their respective pragmas.
The section name used has to be known at the declaration time by a previous section pragma.
#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 good practice #pragma CONST_SEC DEFAULT // not necessary, // but 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