Need to word align tables in code space

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Need to word align tables in code space

2,089 Views
Nickx
Contributor I
system: Codewarrior 4.6
processor: 9s12c32
 
This will actually be a two part question.  First, what is the correct way to define a table in code.  I have it defined as:
          const unsigned int Table1[8] = {0,1,2,3,4,5,6,7};
The compiler will place it in flash or ROM depending upon how I use the table in the program.
 
For example:
    void SomeFunction()
    {
       unsigned int i;
       i=Table1[0]
    }
Places the table at address 0 in RAM but, ....
 
   void SomeFunction()
   {
       const unsigned int * i;
       i=Table1;
   }
Places the table at address 0xC043.  Is there a better way to define the table such that it will always wind up in code space?  I tried using #pragma CODE_SEG but the compiler just ignored it.  Any help is appreciated.
 
Second question:  What compiler directive or technique can be used to word align the table?
The align directive (#pragma align 2) doesn't work.   Doing a test compile then poking a byte in if needed is not an option.   Fixing the table at a defined address is not an option either, although I would like to know how to do that.  Basically the table should be relocatable, always the same size and always begin on an even word.  Thanks, hope you can help.
Labels (1)
0 Kudos
Reply
2 Replies

952 Views
CompilerGuru
NXP Employee
NXP Employee

>    void SomeFunction()
>    {
>       unsigned int i;
>       i=Table1[0]
>    }
>Places the table at address 0 in RAM but, ....

No, the compiler directly assigns the value of Table1[0] to i,
or if i is not used afterwards, he wont even do that.
So Table1 is not at 0, its not linked. And address 0 is usually not RAM too, bye the way.

>   void SomeFunction()
>   {
>       const unsigned int * i;
>       i=Table1;
>   }
>Places the table at address 0xC043. 
>Is there a better way to define the table such that it will always >wind up in code space? 

It does end up in the flash, if it ends up in the application. If you do not want that the compiler does constant propagation, make it volatile, but usually there is no need to do that.

>I tried using #pragma CODE_SEG but the compiler just >ignored it.  Any help is appreciated.

For  constants, the #pragma CONST_SEG
applies, check the compiler manual.

>Second question:  What compiler directive or technique can
>be used to word align the table?
>The align directive (#pragma align 2) doesn't work.   Doing a >test compile then poking a byte in if needed is not an option.   >Fixing the table at a defined address is not an option either, >although I would like to know how to do that.  Basically the >table should be relocatable, always the same size and always >begin on an even word.  Thanks, hope you can help.



The alignment can be done in the *.prm file.
Use a #pragma CONST_SEG XXX and then place XXX into a placement defined with ALIGN 2, check the linker manual for details (or create a dummy project for the XGATE with the wizard for a S12X, the XGATE needs all its data 2 byte aligned too.)

Daniel

0 Kudos
Reply

952 Views
JimDon
Senior Contributor III
Here is another hint - you don't have to write code to figure out where things were placed, just examine the map file. It's your friend.
0 Kudos
Reply