Search This Blog

Tuesday, January 3, 2012

Checksum Byte in ROM

To ensure integrity of contents of ROM, every system must perform checksum calculation. The process of checksum is carried out to detect any corruption in contents of ROM. ROM can get corrupted in many ways major one is the current surge during on and off procedure. The checksum process uses a byte known as checksum byte. The checksum byte is an extra byte that is tagged at the end of a series of bytes of data.
The process of checksum is as follows:
1)      Add the bytes together and drop the carries.
2)      Take the 2’s complement of the sum. This 2’s compliment is our checksum byte, which becomes last byte of the series.

Let us take an example.
Consider size of ROM being 6 bytes.  Having hex values as follows.
32h – 45h – 67h – 12h – 11h – 32h

Now we wish to find first the checksum byte.
For that purpose lets add them up.
32h + 45h + 67h + 12h + 11h + 32h = 133h
So dropping the carries we get 33h
Now 2’s compliment of 33h = CDh
So CDh is our checksum byte.

Now we wish to ensure data integrity.
For this purpose we are going to add 0Ch to previous all bytes.
32h + 45h + 67h + 12h + 11h + 32h + CDh = 200h
Dropping the carries we get 00h ensuring that the data is not corrupted.
If we get any other value except 00h it means that the data in the ROM is corrupted.

This is how we perform checksum calculation.

A program based on this method.
void main()
                unsigned char mydata[] = {0x32,0x11,0x43,0x65,0x67,0x22};
                unsigned char checksum;
                unsigned char sum=0;
                unsigned char i;
                for(i = 0; i<mydata.length(); i++)
                                sum = sum + mydata[x];
                }//end of for loop
                checksum = ~sum + 1; //2's complement
                 * do whatever you want with the checksum byte
}// end of main
// end of program