7.2.2.1 HEX File Formats

Although there is only one Intel HEX file specification, Intel HEX files assume different formats based on the types of records that they contain. These formats were originally developed to accommodate Intel devices with more than 64KB of memory, but they have been adapted to suit other devices. The formats applicable to Microchip tools are described in this section.

If the data to be programmed into a device does not need to be placed at HEX file addresses greater than 0xFFFF (remember that these might map to lower devices addresses if the device's memory is word addressable), the HEX file to hold such a program image only need contain Data (type 0) and End-of-file (type 1) record types. Any HEX file that contains only these record types are often said to conform to the Microchip INHX8M format.

If data has to be placed at higher locations, Microchip tools use the type 4, Extended Linear Address Record, to allow higher addresses to be used in the HEX file. This record defines the base address to which the Address Offset specified by subsequent Data Records is added. The base address is calculated as the type 4 record's Argument payload shifted left 16 bits. HEX files that use no record types other than type 4 (and the accompanying type 5) records in addition to any type 0 and 1 records are considered to conform to the Microchip INHX32 format.

The following shows how a type 4 record is used in an INHX32-conforming HEX file.
:0A00D00010EEECF2110057EF00F003
:020000040030CA
:02000200FFFFFE
:00000001FF
The first line shows a Data Record holding data at HEX-file address offset 0x00D0 (bolded). If this was the first record in the file, then Hexmate will assume that the base address has a default value of 0. The first byte (index 0) in this record (0x10) has HEX file address 00. The next byte of data (index 1) has the value 0xEE and this is located at HEX file address 1, etc.

The following line in the file contains an Extended Linear Address Record, as indicated by the 04 present in the Record Type field. The Data/Argument field for this record holds the value 0x0030 (underlined), which specifies a base address of 0x300000 when shifted left 16 bits. This new base address must be added to the Address Offsets of all subsequent Data Records, unless another type 4 record is used to change that base address. The following Data Record in the file indicates an Address Offset of 0x0002. The data address for the first byte in this record (the byte 0xFF) is calculated as the base address, 0x300000, plus the Address Offset specified by the Data Record, all modulo 232. So the first data byte has HEX file address 0x300002. The subsequent byte of data in this record (another 0xFF byte) has the address 0x300003.

The type 5 Start Linear Address Record that can accompany type 4 Extended Linear Address Records is typically not used by any Microchip debugging tools; however GCC-based MPLAB XC compilers might produce these records. Hexmate will process these records as described in 7.2.3.6 Sla Hexmate Option, which also describes how this option can be used to insert a type 5 record into the HEX file output.

Hexmate can also read HEX files that are said to conform to the Microchip INHX16 format, although this format is less commonly used. In addition to the type 0 and 1 records, these file formats can also contain type 2 Extended Segment Address Records. Like type 4 records, these specify a base address to which the Address Offset specified by subsequent Data Records is added. The base address is calculated as the Argument payload of the type 2 record shifted left 4 bits.

The following HEX file shows how a type 2 record is used.
:0240F0001200BC
:020000021000EC
:04FFFC005BEF20F0A7
:00000001FF

The first line shows a Data Record holding data at HEX-file address offset 0x40F0 (bolded). If this was the first record in the file, then Hexmate will assume that the base address has a default value of 0. The first byte (index 0) in this record (0x12) has HEX file address 00. The next byte of data (index 1) has the value 0x00 and the HEX file address 1, etc. The following line in the file contains an Extended Segment Address Record, as indicated by the 02 present in the Record Type field. The Argument field for this record holds the value 0x1000 (underlined), which specifies a base address of 0x10000 when shifted left 4 bits, and which must be added to the address of all subsequent Data Records. The following Data Record specifies an address offset of 0xFFFC. The HEX file address for the first byte in this record is calculated as the base address, 0x10000, plus the modulo 216 result of the offset specified by the Data record plus the byte's index in that record. So the first byte of data is located at address 0x1FFFC.

The type 3 Start Segment Address Record that can accompany type 2 Extended Segment Address Records is typically not used by any Microchip debugging tools; however GCC-based MPLAB XC compilers might produce these records. Hexmate will process these records as described in 7.2.3.7 Ssa Hexmate option, which also describes how this option can be used to insert a type 3 record into the HEX file output.

A summary of the record types allowable in each format is tabulated below.
Table 7-3. HEX file Formats
FormatValid record typesHEX file address range
INHX8M0, 116 bit
INHX160, 1, 2, 320 bit
INHX320, 1, 4, 532 bit

If a HEX file contains type 2 or 3 and also contains type 4 or 5 records, then the file does not conform to any named format and these files are often considered invalid. Hexmate will process such files, but will always write output files with a recognized format. If a mixed-format file is passed to Hexmate, any type 3 or 5 records not permitted in the output format specified by the -format option will be removed; any type 2 record will be converted to a type 4 record when producing an INHX32 format, and any type 4 record will be converted to a type 2 record if possible when producing a INHX16 format; otherwise, an error will be produced.

Note that converting from INHX8M to INHX16, or from either INHX8M or INHX16 to INHX32 can be easily performed. Converting the other way might not be possible. An INHX32 format can only be converted to a INHX16 or INHX8M format if the base addresses specified in the type 4 records do not place data outside the HEX file address range, indicated in the above table, that the format can represent.