Using gcc to build rommable code

June 8, 2013 Here are some links to other people who are working out these issues:

Once you have a proper cross compiler setup going, the next trick is getting the addesses right for code that goes into ROM (or flash maybe). Getting the text address right is only part of the game, but even that takes some effort. My cross compiler setup generates "elf" format object files. However, there is a very handy option to the linker:

ld68k --oformat binary -s -o rom_image $(OBJS)
The "-oformat binary" tells the linker to do exactly what I want, namely just emit the bytes that will go into ROM without any headers or extra sections of any kind. It is also possible to ask for motorola S-records or even intel hex (via "srec" and "ihex"). The command "objdump68k -i" gives a complete list.

All this is great, but in general any absolute addresses in the code will be set up to run at address zero. This is fine if the ROM will sit at address zero (but in most systems I work with there is RAM at address zero, and the ROM needs to live someplace like 0xFC0000. The only way I have found to get that straightened out is by using a linker control file, otherwise known as a linker script.

ld68k -T rom.lds --oformat binary -s -o rom_image $(OBJS)
As they tell you in the Gnu ld manual, every link is controlled by a linker script, whether you know it or not. Supplying one of your own is just an opportunity for you to control what is going on. Of course to write them, you have to learn the linker control language. A very simple one that I have used is this one:
SECTIONS
{
  . = 0xFC0000;
  .text :
  {
    *(.text)
    _etext = .;
    __etext = .;
  }
}
This does the job getting the text in the right place, and in fact allowed me to run a few simple roms that set up their own stack pointer in RAM. However as soon as I introduced some non-stack variables (that would go into a .bss or .data segment), they end up getting placed into ROM addresses, which clearly will not work (this is fine for "const" data items though and is exactly what "const" would be expected to do.

I need to dedide where .bss and .data go and extend my loader control file. Also, I need to add some initialization code to do two things:

Stay tuned.
Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org