December 5, 2009

Preprocessor -Coding myth 4

There is this classic case which came as a surprise to me when a fairly senior member of my team started arguing with me about a particular implementation that had made....below is the case
#define BusClock ((Crystal_Frequency)/(Prescalar))

Now the argument was ...why was i not calculating manually and typing in the values.
Our Dude Says >> See Macros will be just replaced by the compiler so when you load it into the register like this

BCLK = BusClock;

then there will be more run time due to the division of crystal freq by prescalar.
Well this is totally wrong. There are parts of the statement which are correct and which are wrong.
Correct parts >> Macro's will be dumbly replaced ( not by the compiler but by the preprocessor).
Which means that when my code goes to the compiler it looks like this
BCLK = 600000/12;
where cystal is 6Mhz and Prescalar is 12.
Does that mean it will go to the assembler in the same way....No!! and that is the wrong part.
As a part of basic optimization the compiler will be clever enough to do this division of 600000/12 and feed the appropriate value into BCLK. which means actually it looks like
BCLK = 50000;
before anything meaningful really begins.
In most compilers this cannot be turned off and is a basic optimisation. Infact, I have known a compiler that had a problem when we gave too big numbers. It would just give some gibberish error.

So the basic rule is that ...
If the compiler has all the information that it needs to create a single number ( after as many operations ...like 1+(10000)*12/(53) etc...) it will do it on its own to give a nearest number. Note that these divisions and multiplications are Integer unless you explicitly tell the compiler that they are double. So based on how you choose the values you might end up with varying results.

If anyone knows of a compiler that doesn't do that, please enlighten me...
Please leave your comment. You can subscribe to this blog by using the links under "Subscribe" section.


Powered by ScribeFire.

November 9, 2009

Eeprom and Flash Emulated Eeprom

This post talks about some basics of EEPROM and FEE.
To start with the discussion it is necessary to know what EEPROM stands for. EEPROM stands for Electrically Erasable PROM. Where PROM means Programmable Read only memory. The very fact that we are highlighting the Electrically Erasable means that there are other methods to erase the PROM. I leave it to the reader to find out what these are and leave as a comment.
Why do we need it?
In any system there are some set of parameters that need to be remembered through power cycles and can/may change between power cycles. An example....Well Your monitor settings ( i.e. the brightness contrast etc that you do on your monitor) is remembered through power cycles even if you connect the monitor to another PC or whatever. One of the places to store this could be the EEPROM.
What does this mean?
To write to the EEPROM i should be able to erase it and then rewrite it. Also it should be capable of being written several times ( maybe a few lakh times). Also, it should have data retention capacity. i.e if i write it today and check after say some 10 years the data should be same in the absence of any power.
What is FEE then?
As you can see some of the properties of EEPROM are held by the flash memory also. Like i can write into the Flash memory electrically, it can retain information across power cycles, it can be written multiple times. However, the biggest different is that the flash technology ensures that it can be written only in huge chunks like 64 or 128 bytes. Also, the life of the Flash memory is much lesser than the EEPROM. On the other hand the best part is that it costs much less than the EEPROM.
So FEE which stands for Flash Emulated EEPROM is basically a method of emulating the behaviour of the EEPROM using the Flash memory. This means if a Flash memory has 10000 write cyles, i should make it some how work as if it is 100,000 write cycles ( like in case of EEPROM). Also, i should provide somehow the capability of writing a single word/ single byte as available in the EEPROM.
In short if i have FEE then the application should feel as if it is having EEPROM....
How to do this ? Well that is for another post....


Please leave your comment. You can subscribe to this blog by using the links under "Subscribe" section.


Powered by ScribeFire.