Need to word align tables in code space

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Need to word align tables in code space

2,075 次查看
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.
标签 (1)
0 项奖励
回复
2 回复数

938 次查看
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 项奖励
回复

938 次查看
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 项奖励
回复