The "Intel-Standard" HEX file is one of the most popular and commonly used formats in the 8052 world. The standard is used to burn the 8052 program into an EPROM, PROM, etc. For example, an 8052 assembler will usually generate an Intel Standard HEX file which can then be loaded into an EPROM programmer and burned into the chip.
An Intel Standard HEX file is an ASCII file with one "record" per line. Each line has the following format:
|1||Record Marker: The first character of the line is always a colon (ASCII 0x3A) to identify the line as an Intel HEX file|
|2 - 3||Record Length: This field contains the number of data bytes in the register represented as a 2-digit hexidecimal number. This is the total number of data bytes, not including the checksum byte nor the first 9 characters of the line.|
|4 - 7||Address: This field contains the address where the data should be loaded into the chip. This is a value from 0 to 65,535 represented as a 4-digit hexidecimal value.|
|8 - 9||Record Type: This field indicates the type of record for this line. The possible values are: 00=Register contains normal data. 01=End of File. 02=Extended address.|
|10 - ?||Data Bytes: The following bytes are the actual data that will be burned into the EPROM. The data is represented as 2-digit hexidecimal values.|
|Last 2 characters||Checksum: The last two characters of the line are a checksum for the line. The checksum value is calculated by taking the two's complement of the sum of all the preceeding data bytes, excluding the checksum byte itself and the colon at the beginning of the line.|
echo $STARTSECTION; ?>Calculating the Checksum echo $ENDSECTION; ?>
As mentioned in the format table above, the last two characters represent a checksum of the data in the line. Since the checksum is a two-digit hexidecimal value, it may represent a value of 0 to 255, inclusive.
The checksum is calculated by summing the value of the data on the line, excluding the leading colon and checksum byte itself, and taking its two's complement. For example, the line:
Record Length: 03 (3 bytes of data)
Address: 0030 (the 3 bytes will be stored at 0030, 0031, and 0032)
Record Type: 00 (normal data)
Data: 02, 33, 7A
- 03 + 00 + 30 + 00 + 02 + 33 + 7A = E2
For those unfamiliar with calculating a two's complement, it's quite simple: The two's complement of a number if the value which must be added to the number to reach the value 256 (decimal). That is to say, E2 + 1E = 100.
You may also calculate the two's complement by subtracting the value from 100h. In other words, 100h - E2h = 1Eh -- which is the checksum.
If the value in question is greater than FFh, simply take the part which is less than 100h. For example, if you want the two's complement of the value 494h, simply drop the leading "4" which leaves you with 94h. The two's complement of 94h is 6Ch.