hex file refers to a file with hex as suffix and Intel hex coding rules. It can be opened directly with text editing tools. It is usually used to program microcontroller or ROM. in essence, it is to program memory, which contains the data corresponding to each address.
Xilinx mcs file used for program solidification is named after. mcs suffix, but its essence is hex file. After changing the suffix to. Hex, it can be opened directly using File tools, which conforms to Intel hex file format.
Define a file format yourself
For example, we have 10 data that need to be stored in 10 addresses. We can define a file format ourselves:
address a1:data d1 address a2:data d2 address a3:data d3
The contents of the file are like this:
1001:11 //Address 0x1001 store data 0x11 1002:22 1003:33 1004:44 1005:55 1006:66 1007:77 1008:88 1009:99 100A:AA
If the amount of data is small, this representation is still clear. If the amount of data is large, this data storage method will make the number of file lines very large.
If you can put multiple data on the same line, let's change the file format:
Line data length n Start address a:data d1 data d2....data dn
Then the above 10 rows of data become the following:
041001:11223344 //Data stored at 4 addresses starting from 1001: 11 22 33 44 041005:55667788 //Data stored at 4 addresses starting from 1005: 55 66 77 88 021009:99AA //Data stored at 2 addresses from 1009: 99 AA
In this way, 10 rows of data are compressed to 3 rows. Of course, 10 data can be represented in one row:
0A1001:112233445566778899AA //10 address data from 1001
In order to ensure the reliability of file transmission, we also need to add verification data attached to the end of each line to verify the data of this line.
The verification algorithm adopts cumulative sum, which only sums the data part, and takes the low 8-bit data of the cumulative sum.
Therefore, the above 10 groups of data become the following:
041001:11223344 AA //0xAA = 0x11 + 0x22 + 0x33 + 0x44 041005:55667788 BA //0x1BA = 0x55 + 0x66 + 0x77 + 0x88 021009:99AA 43 //0x143 = 0x99 + 0xAA
In this way, the integrity and accuracy of data in the transmission process are greatly improved. The starting HEX file represents the address and data of memory in a similar way.
Detailed explanation of HEX file format
We use Notepad + + to open a Hex file:
You can see that some data are distinguished by different colors, and the meaning of data with the same color is the same.
The data in black font is the real data part. One byte at the end of each line is the check byte of the current line data. Check sum = 0x100 - cumulative sum.
Notepad + + has the automatic check function of HEX file. If the cumulative sum is incorrect, the last byte will be in red font.
The first byte data after the colon, such as 02 / 10, respectively indicates that there are 2 / 16 bytes of data in the current line.
Each row of data in HEX file consists of four parts:
Start code: + number of data (2 bytes) + start address (4 bytes) + record type (1 byte) + data (N bytes) + checksum (1 byte)
The record type corresponds to:
00: Data, example:0B0010006164647265737320676170A7 01: End of file, example:00000001FF 02: Extension segment address, example:020000021200EA 03: Start segment address, example:0400000300003800C1 04: Extended linear address, example:02000004FFFFFC 05: Linear start address, example:04000005000000CD2A
For detailed description, you can view Wikipedia: https://en.wikipedia.org/wiki/Intel_HEX
Data format analysis of the first row:
:02 0000 04 0800 F2 02: This line has 2 bytes of data 0000: Starting address of this line data 04: The record type of this line is extended linear address 0800: 2 Byte data F2: Checksum=0x100-(02+04+08) = 0xF2
Data format analysis of intermediate line:
:10 0010 00 CD020008CF020008D102000800000000 55 10: This line has 16 bytes of data 0010: Starting address of this line: 0 x0010 00: The record type of this line is data CD020008CF020008D102000800000000: 16 Byte data 55: Checksum
Data in the last few lines:
:1029F00001020304060708090000000000000000AF//data :102A00000000000001020304010203040607080994//data :04 2A10 00 02040608 AE //data :04 0000 05 080000F9 F6 //Starting linear address :00 0000 01 FF //End of file
Other file formats, such as the BIN file format, are simple and straightforward. There is only the data part, and there is no address information or verification information. Therefore, the starting address needs to be specified when using the BIN file format.
You can view the differences between various file formats: What are the differences between BIN, HEX, AXF and ELF file formats