avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Align Data and RAM Gaps


From: Erik Christiansen
Subject: Re: [avr-gcc-list] Align Data and RAM Gaps
Date: Sat, 9 Aug 2014 18:18:01 +1000
User-agent: Mutt/1.5.21+145 (2a1c5d3dd72e) (2012-12-30)

On 08.08.14 12:43, Thomas D. Dean wrote:
> I have a 256 byte buffer that is accessed with two pointers, head and tail.
> 
> In the AS code, I want to only save the low order byte of the pointer. This
> is do-able, if the buffer is aligned at 0x0100.

At the cost of 2-byte arithmetic in the queue and dequeue functions,
arbitrary alignment works fine, using only single byte read/write
index values. E.g.:

   ldiw  Z,buf
   add   ZL,r24         ; Your modulo 2^8 index.
   addc  ZH,__zero_reg__

The index still auto-wraps at 256, since you increment it prior to using
it as an offset to the 16-bit buffer base address. (There's code of mine
in thousands of PABXs running with such a queue, and the calls get
through fine.)

> In the C file,
> char buf[256] __attribute__ ((aligned (256)));
> In the AS file,
> .global buf
> 
> Looking at the map file, I see
> 
>   0x0000000000800100                tx_buf_head_ptr
>   0x0000000000800200                buf
>   0x0000000000800300                rx_buf_tail_ptr
>   0x0000000000800302                tx_buf_tail_ptr
> 
> There is a gap of 254 bytes.
> 
> How do I avoid this gap?

OK, let's assume it were necessary: The least intrusive way is then to
ensure that tx_buf_head_ptr is allocated after buf, and that should
happen if you move its declaration after that of the buffer, in the C
file. Since you're starting out acceptably aligned, that ought to work
for as it stands. Clearly, buf is declared in the first object file
linked, because later link order would also defeat this winkle, as other
allocations disturb alignment.

Note that .data is normally allocated before .bss, so it also stops
working as soon as you add any initialised variables. (Unless they end
suitably aligned.) In that case, I'd try swapping the order of .data and
.bss output sections, in a custom linker script made from the default
one.

> What determines the linker allocation order?

First the linker script, then object file order on the avr-gcc or avr-ld
command line. Since you are not supplying a custom script, the default
script is used. That may be separated from excess verbosity with:

avr-ld --verbose | gawk -- "/===/ { show = 1 - show } ; { if (show) print }"

As uninitialised data space, your buffer ought to be allocated in .bss,
which is allocated after .data in the avr-ld default linker script I've
just examined.

I'll be off-line for a week or so, from a few hours hence, so please
repost pronto if elaboration is sought.

Erik

-- 
Arguing that Java is better than C++ is like arguing that grasshoppers taste
better than tree bark.                                   - Thant Tessman




reply via email to

[Prev in Thread] Current Thread [Next in Thread]