View previous topic :: View next topic |
Author |
Message |
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
How to copy arrays quickly ?? |
Posted: Wed Jan 17, 2007 11:00 am |
|
|
Hi,
How do I quickly copy arrays in PCM 3.228 ??
I use a for loop to run through the arrays, but are there any faster methods than this ???
thanks
arunb |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jan 17, 2007 11:23 am |
|
|
Use memcpy(). It's in the CCS manual. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jan 17, 2007 5:49 pm |
|
|
Memcpy() is indeed a better solution than a for-loop. Something not mentioned in the manual is that the CCS compiler is using an internal optimized memcpy() routine, not the memcpy routine you will find in one of the standard include files. The internal routine is so fast because it uses the index register which has an auto increment feature.
For PIC18 only:
One limitation of the CCS generated code is that it uses only 1 of the 3 available index registers. There are some situations where you have to copy data from one array to another but memcpy is not flexible enough. In these situations you can achieve higher speeds by writing your own (assembler) code using two index registers. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Jan 18, 2007 10:01 am |
|
|
Any chance either of you fellows could post that type of assembly code.
I know this isn't a forum for assembly code, but now you have my interest peeked. I would love to see how this is done.
You could use a #asm #endasm and claim it to be ccs related. : )
Are both of you also on the Microchip forum??
I could ask for it there. |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Thu Jan 18, 2007 12:53 pm |
|
|
Code: | #byte FSR0H = 0xFEA
#byte FSR0L = 0xFE9
#byte FSR1H = 0xFE2
#byte FSR1L = 0xFE1
#byte POSTINC0 = 0xFEE
#byte POSTINC1 = 0xFE6
void memcpy_asm(int8 *src, int8 *dest, int8 n) { // n number of bytes to copy
#asm
movff &dest+1, FSR1H
movff dest, FSR1L
movff &src+1, FSR0H
movff src, FSR0L
loop:
movff POSTINC0, POSTINC1
decfsz n
goto loop
#endasm
} |
...not tested.
PS. Though I don't know how safe it is to use the FSR0-2 registers without the compiler being aware of their corrupted contents. (you might want to save/restore them, or as mentioned above knowing the compiler uses FSR0, using FSR1-2 would be safer..) |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Jan 18, 2007 2:03 pm |
|
|
thanks, I'll test it out. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Jan 18, 2007 8:20 pm |
|
|
Hi Ttreitmey,
No, I'm not on the Microchip forum. This forum is taking all my spare time.
Does someone know if the LFSR assembly instruction is supported by CCS? I only get compiler errors. |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Jan 18, 2007 9:00 pm |
|
|
Funny, I thought they all were.
How can you find out which are/are not? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 18, 2007 9:15 pm |
|
|
This compiles OK with vs. 3.249:
Code: |
#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#define FSR0 0xFE9
//================================
void main()
{
#asm
LFSR FSR0, 0x0100
#endasm
while(1);
} |
|
|
|
|