CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to reduce ROM space ??

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
arun
Guest







How to reduce ROM space ??
PostPosted: Wed Sep 08, 2004 7:36 am     Reply with quote

Hi,

I have developed a large program (it works very well ) for use on PIC 16F877 , the ROM size occupied is around 75% and the RAM is 59%.

Is this too much or should I re-structure my program so that it occupies less ROM space. ???

Some tips would be very helpful.

thanks
arun
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 08, 2004 8:01 am     Reply with quote

It fits doesn't it???
Ttelmah
Guest







Re: How to reduce ROM space ??
PostPosted: Wed Sep 08, 2004 8:01 am     Reply with quote

arun wrote:
Hi,

I have developed a large program (it works very well ) for use on PIC 16F877 , the ROM size occupied is around 75% and the RAM is 59%.

Is this too much or should I re-structure my program so that it occupies less ROM space. ???

Some tips would be very helpful.

thanks
arun

If you don't need to fit anything else, and the code works, why worry?. The whole ROM is there to be used (unless you are using ICD, in which case a small amount at the top should not be used). I have a couple of projects here, that use over 98% of the ROM.

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Sep 08, 2004 9:43 am     Reply with quote

I had a project, once, that ran just fine (I needed to tweek it a bit to get the whole code to fit) but if I added two int8 variables I ran out of space. If it runs, don't worry about it. Smile

Ronald
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 08, 2004 10:21 am     Reply with quote

I have some that is so packed that just declaring one variable before the other causes out of ROM. It is due to then bank switching.
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Wed Sep 08, 2004 10:33 am     Reply with quote

I have a simple set of rules I follow when developing code and products:

1) Define your own data types, this will save you hours when you have to port your code.

2) Try not to use intrisic functions like, CCS's bit_test. If you do then define them as macros like #define BIT_TEST(x) bit_test(x). Also always used the #case in CCS. This way if you port code to anothe chip or compiler you can easily change the bit_test implementation.

3) Try to organize your code into modules, like a file for the flash chip, etc. This way if you reuse the chip or need module for another project it is easy to port.

4) Never optimize code when writting first time. Write all code in the simpliest easiest way for people to read and understand. If you need to optimize then comment out unoptimized code and write a new optimized version. Do not optimize until absoultly needed! Like when you run out of ROM and not before.

5) When starting code figure out your error reporting system first! For example will errors be logged to flash or out serial port, etc.

6) Put code in to check for vaild inputs and outputs for all functions. What I like to do is but a #define to enable this error checking. For example:
Code:

#define CHECK_INPUT 1

UINT foo(UINT input)
{
#if CHECK_INPUT
    if (input>100)
    {
        printf("Error in foo input out of range\n");
        return ERROR;
     }
#endif
   //more code

  //simular error checking for return
}

Using this if you run out of ROM you can disable error checking or turn off for production releases.

7) If you have to write assembly, think about it some more... That is my philosphy is when I drop to assembly I need to make sure I am working on a high volume product where production costs are going to be more important than development and maintenance costs. I am not saying assmebly is bad but rather it is expensive for development and such. For example I have code where I use assembly for timing some fast data. If I ever change chip of clocks then code breaks.

8) When designing hardware I always ask questions like, does this input need to trigger an interrupt? Can I put this feature under software control? I also keep a list of mistakes I've made and try to refer to them when reviewing hardware designs. Like "Oh I should have made an option for the part run at 3.3V not 5V, so next design I consider the option.

9) Ever problem has a unique soultion that is just wonderful, pretty, and simple. Well 90% of the time you will not see it so get over it! That is most of the time brute force works quicker. That is you can spend hours setting up you data structures and such to make the code and application simple and fast. Or you can get a faster processor and do it brute force in less time. Make sure that if you are searching for the unique soultion it is because of the right reasons, like you need the lowest BOM cost possible, even if development takes weeks longer.

10) Write the manual for your product first! This will save a lot of time figuring out how something should behave as you write code.

11) Opinion are like a** holes, everyone has one and they are usually full of crap. Well these are some my opinions and I know I am full of it! Heck I am so full of it, that I don't even do all these things even though I know I should.

Trampas
bdavis



Joined: 31 May 2004
Posts: 86
Location: Colorado Springs, CO

View user's profile Send private message

PostPosted: Fri Sep 10, 2004 11:13 pm     Reply with quote

I agree with what Trampas posted. The reason for using 'C' is to reduce development time, decrease bugs, to decrease debug time, and increase portability. I know what you mean when you want to look for optimizations at 75% full for ROM. My comfort range is 50% usage for future maintenance and new features, but of course that depends on the project and if you plan on doing field upgrades for a year or more.

The biggest things I could see were:

- Use bits for everything you can - (single cycle, single instruction, and efficient use of ram).

- I use asm to toggle a bit - CCS added a function for it, but I haven't looked at a listing to see if is a single instruction. If you use "byte & (~mask)), then it is ten times the code and ten times slower on the pic. The 18Fxxx has a BTG instruction, which is nice.

- Use pointers instead of arrays for data in ram. I declare the data as an array, then use pointers in the code to access the data. This saved more code space than any other simple optimization, and is portable. Look at your listing file and you can see about a 50% savings in ROM usage for these lines of code. I did hear that earlier compiler versions may have problems using too many pointers - ie: "*c = *b;"

- From all listings and experimentation, CCS doesn't fully optimize pointers and such using the 18Fxxx registers yet (in version 3.202). I wouldn't recommend it unless you are actually running out of code space, but you can see huge savings in code space optimizing the indirect registers in assembler - this was about 16x smaller than using arrays and about 8-10x smaller than using pointers. The 18Fxxx has the ability to clear or copy and increment in a single instruction for indirect (pointer) registers. The compiler seems to only use this for each line of 'c' code, but not consecutive lines. The 16x in improvement was seen in initializing an array at runtime.

-Any assembler is implemented as a macro, and should have a very good reason for using it (speed, code space, precicise behavior, etc).

- Highly recommend looking at the listing file- I have found compiler bugs before even running the code doing this. I will give you a better understanding of what the compiler is doing. I'm still learning everything about CCS, but so far, I'm happy, and hope the above helps...
Trampas



Joined: 04 Sep 2004
Posts: 89
Location: NC

View user's profile Send private message MSN Messenger

PostPosted: Sat Sep 11, 2004 8:09 am     Reply with quote

I know for real compilers, like Microsoft's compiler they recommend you do not use pointers for indexing into arrays. Which seems funny at the first, but the reason is optimizations.

That is the high end compilers are often smarter about the hardware the code runs on than the programmer. For example:

Code:

BYTE data[20]
UINT i;

for(i=0; i<20; i++)
{
   data[i]=data[i]+1;
}


Some of the high end compilers running on newer chips will replace the code above with routines that used MMX instructions. That is they can pack the byte array into 32 bit registers. Thus read 4 bytes at a time, add 1 to all 4 bytes in one instruction then write out all four bytes. However if the programmer used pointer the compiler has a harder timer understanding what the user is trying to do and may do it slower.

So the thing you have to do is know your compiler! That is CCS may compile better with pointer. While M$ compiler may be faster with array indexing.

A few years ago I was writting some code for a PC and got a book on optimizations. The book talked about how to eliminate floating point operations. However the example code they had ran slower than the floating point code on the newer PCs. The book was written for earlier X86 before the floating point was more optimized.

Again know your chip and your compiler!

Trampas
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group