Copying Initialized global variables from ROM to RAM

naseeam
naseeam used Ask the Experts™
on
In Renesas/Mitsubishi M32R Family based system, linker puts initialized global variables in section ROM_D.  At run-time these globals variables need to be copied to section D of RAM.  This copying can be described in 'C' language because linker automatically generates the labels which indicate the start and end address of each section.  For example, label __TOP_ROM_D is generated to specify the start address of initialized global variables in ROM.

PLEASE CORRECT ME IF I'M WRONG.  First I'll extern these labels as follows:

extern unsigned char  __TOP_D;   /* Starting address of initialized global variables in RAM */
extern unsigned char  __TOP_ROM_D;   /* starting address of initialized global variables in ROM */
extern unsigned char  __END_D;    /* Ending address of intialized global variable in RAM */

Then, I'll copy from ROM to RAM as follows:

memcpy(__TOP_D, __TOP_ROM_D, __END_D - __TOP_D);

Please use attach link to refer to linker manual for more information.  In particular, see sections 5.1 and 5.2.3 and Figures 5.1 and 5.2.

http://documentation.renesas.com/eng/products/tool/rej10j0515_m3tcc32r_u.pdf
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I think extern should be arrays as follows:

extern unsigned char __TOP_D[];
extern unsigned char __TOP_ROM_D[];
extern unsigned char __END_D[];
Commented:
If it's anything like any of the embedded systems which I've worked with (including Renesas, for the H8), this copy is done for you in a startup assembly file which eventually calls your main.  You can access the region with the symbols provided (and naseeam is probably right, these are labels with no size), but the actual copy is done for you before your main starts.

Easy way to check if it is:  Run a tiny sample progam under your debugger, like this:

unsigned char i = 7;
unsigned char j;

int main(void)
{
  if (i == j)
  j = 1;
 else
  j = 2;
}

break at your main, and look at the values of i and j.  If i is 7 and j is 0, then both the copy of the initialized variables has been done, as well as the zeroing of the uninitialized ones.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial