From: ColdFire [ColdFire@WildRice.com] on behalf of David Fiddes
[D.J.Fiddes@hw.ac.uk]
Sent: 08 May 1998 15:10
To: Coldfire CPU Discussion List
Subject: RE: FLASH

Hi,

Here's my first attempt at doing what's necessary to get gcc programs to run
out of the SBC5206 FLASH. It requires a new crt0.o, an updated linker script
and a slightly complex object code shuffling sequence.

First of all grab the necessary files (crt0.o and sbc5206-rom.ld) from the
zip file (http://www.calm.hw.ac.uk/davidf/coldfire/sbc5206-flash.zip) and
copy them to you gcc-m68k/m68k-coff/lib/m5200 directory.

When you've built your app using teh new linker file and crt0.o and run
objdump on the COFF EXE you should see something similar to this:

c:\coldfire\gcc-m68k\examples>m68k-coff-objdump --section-headers hello.x

hello.x:     file format coff-m68k

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         00006550  ffe20000  ffe20000  000000a8  2**2
                  CONTENTS, ALLOC, LOAD, CODE
  1 .data         00000340  00010000  00010000  000065f8  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          0000001c  00010340  00010340  00000000  2**2
                  ALLOC

Notice the "VMA" value for each section. The .text(code) section starts at
0xFFE20000 (user flash) and the .data and .bss are located in user RAM as we
would expect. What we have to do now is "shuffle" the sections so they
appear line-a-stern in the finished FLASH/ROM image. We do this by using
objcopy as follows:

m68k-coff-objdump --adjust-section-vma .text=0x10000
m68k-coff-objdump --adjust-section-vma .data= (calculate 0x10000 + .text
size)

(we can skip .bss as it's really just a marker for space not actual data)

You need to do a bit of maths here to work out where the data should
start...in this case 0x00016550. If we use objdump again:

c:\coldfire\gcc-m68k\examples>m68k-coff-objdump --section-headers hello.x

hello.x:     file format coff-m68k

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .text         00006550  00010000  00010000  00002000  2**2
                  CONTENTS, ALLOC, LOAD, CODE
  1 .data         00000340  00016550  00016550  00008550  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          0000001c  ffe26890  ffe26890  00000000  2**2
                  ALLOC

Now we need to convert to an SREC for download:

m68k-coff-objcopy -O srec hello.x hello.srec

If you then download the SREC into your board and type "upuser" followed by
"UPDATE" this will put the program in RAM. To test it...switch off your
board and then turn it on and type "go FFE20000"...It should just work!

I've have had to make a small mod to the stock crt0.S from Newlib-1.8.0 as
it doesn't copy the .data section from flash to ram on startup as it should.
Here is my simple patch:

*** /source/newlib-1.8.0/libgloss/m68k/crt0.S	Fri Mar 14 22:38:21 1997
--- crt0.S	Thu May 07 19:00:51 1998
***************
*** 73,78 ****
--- 73,96 ----
  	link	a6, IMM(-8)

  /*
+  * Copy data in ROM to RAM if necessary
+  * Assume that DATA and BSS sizes are multiples of 4.
+  * (put an . = ALIGN(0x04) in your linker script to ensure this)
+  */
+  	lea	copy_start,a0		| Get start of DATA in RAM
+ 	lea	SYM(etext),a2		| Get start of DATA in ROM
+ 	cmpl	a0,a2			| Are they the same?
+ 	beq.s	NOCOPY			| Yes, no copy necessary
+ 	lea	copy_end,a1		| Get end of DATA in RAM
+ 	bra.s	COPYLOOPTEST		| Branch into copy loop
+ COPYLOOP:
+ 	movel	a2@+,a0@+		| Copy word from ROM to RAM
+ COPYLOOPTEST:
+ 	cmpl	a1,a0			| Done?
+ 	bcs.s	COPYLOOP		| No, skip
+ NOCOPY:
+
+ /*
   * zero out the bss section.
   */
  	movel	IMM(__bss_start), d1

This needs a few new variables defined in the linker script to tell the
program where it has to copy things from (see sbc5206-rom.ld for details).
It should be possible to use this for "normal" ram based programs as it only
copys things when necessary...it'll need a few mods to the linker scripts
though. If this actually works I'll think about rolling it into an update
for my compiler I'm working on.

Dave


________________________________________________________________________
ColdFire Discussion List
See: <http://www.WildRice.com/ColdFire/>
