Hello all,
may you give me an explanation of following piece of code (target is ST10F276):
U8 ndata * Dest; U8 xhuge * Source; register U16 wTemp; while([condition]) { ..... wTemp = *((U16 xhuge *) Source); *((U8 ndata *) (Dest++)) = *((U8 ndata *) & wTemp); *((U8 ndata *) (Dest++)) = *((U8 ndata *) & wTemp + 1); Source += 2; ..... }
I understand that read operation (instructions) of data from xhuge pointer is very slow - therefore complete word is read out and stored into any register given by compiler.
But what is an advantage of write per Bytes? AFAIK the processor ST10F276 has 16bit wide bus, has not?
Thanks for your hints in advance,
Best regards Iggy
Mike, thanks for Your comment!
Sorry for confusing, ndata is defined as following "empty" symbolic constant:
#define ndata
Perhaps You did remember correctly - memory-mapped registers on page 47 of the user manual (www.st.com/.../CD00153027.pdf). According to following code I would say that it does not seem that Source is 16bit-aligned memory. This code precedes above mentioned code - due to copying from odd address. Above mentioned code only starts coping from the even adress.
if ((U16) Source & 1) { *((U8 ndata *) (Dest++)) = *((U8 xhuge *) (Source++));
Yes, you are right - modern compilers can evaluate a variable location (CPU register / RAM) in better way than a man.
However, I am still wondering why the content of 16bit register is copying per Byte. Any idea what I am missing..?
Imagine that Source is aligned and Dest isn't. I think you'll find that per-byte copying will be the most efficient in this case.
Perhaps a reason of this approach is:
premise: Word (16bit) storing into Dest always begins at even address (= default feature of such processor).
If Source starts at odd address, then only one Byte (not whole Word - 16bit) is copied into 1st Byte of destination Word. Afterwards per-Byte coyping must be performed, otherwise one Byte gap (2nd Byte of 1st destination Word) occurs in the Dest buffer.