Best way to locate some C code in work RAM?

Ask anything your want about Megadrive/Genesis programming.

Moderator: BigEvilCorporation

Post Reply
Very interested
Posts: 125
Joined: Tue Oct 09, 2012 8:15 am

Best way to locate some C code in work RAM?

Post by doragasu » Tue Aug 09, 2016 2:14 pm

I want to do some tests to see if I'm able to write to the flash chips of my WiFi cartridge from the Megadrive itself. As you cannot read data (e.g. program instructions) from the flash chips while data is being erased/written to them, I want to locate the code handling flash routines in RAM. So far I have successfully tested using function attributes, e.g.:

Code: Select all

void foo(void) __attribute__((section(".data")));

void foo(void) {
   // Some useful code here...
I can see on the map file that function foo is located in RAM. The code executes perfect, and I have verified the function is in RAM by printing on the screen the value of foo pointer (it was located at 0xFF0002 address BTW, exactly the same address indicated on the map file).

So this approach works if I want to place a function on RAM, but on the flash handling module I have a lot of functions I'd like to place on RAM. I could use the attribute definition above on each function, but I suppose there must be other way of putting all the code generated by the module on RAM. Also I'm not sure about how the section attribute will behave with inline functions.

So, is there any magic to place all the code of a module in RAM without having to "attribute" each function?

Also, on the test I did, I used ".data" section. I suppose there must be a better approach, like creating a new section and modifying the linker script to locate the section in RAM. But I'm not 100% this would work without modifying SGDK startup code to make sure the code is copied from the ROM to the appropiate RAM location before main() is executed.

And one last question... Is there any restriction I should be aware of while doing this trick? E.g. should I manually add an attribute to guarantee the code is aligned to a specific boundary?

Very interested
Posts: 287
Joined: Mon Nov 30, 2015 1:55 pm

Re: Best way to locate some C code in work RAM?

Post by cero » Tue Aug 09, 2016 5:33 pm

From info gcc:
`section ("SECTION-NAME")'
Normally, the compiler places the code it generates in the `text'
section. Sometimes, however, you need additional sections, or you
need certain particular functions to appear in special sections.
The `section' attribute specifies that a function lives in a
particular section. For example, the declaration:

extern void foobar (void) __attribute__ ((section ("bar")));

puts the function `foobar' in the `bar' section.

Some file formats do not support arbitrary sections so the
`section' attribute is not available on all platforms. If you
need to map the entire contents of a module to a particular
section, consider using the facilities of the linker instead.
Now, don't ask me how to tell the linker to rename a file's .text to .data, but I'm sure it's possible. I know objcopy can do so.

Post Reply