PostHeaderIcon PIC18F65K80: Extended Addressing and the Three Ways a Hex File Goes Wrong

Intel HEX was designed when addresses were small. Each data record carries a two-byte offset, which was ample for the memories of the time and is not ample for a part with 64K of program memory plus configuration and identification areas. The format’s answer is the extended address record: a special record that sets the upper bits for the records that follow.

That mechanism works, and because it works silently, it produces files that pass every validity check and describe the wrong memory. Three failure modes account for most of them, and all three are visible in a file that is otherwise well-formed.

The record types that matter

Type Purpose Consequence if mishandled
00 Data The payload itself
01 End of file Anything after it is ignored
02 Extended segment address Upper bits, segment-scaled
04 Extended linear address Upper bits, linear

Types 02 and 04 both set upper bits, and they scale differently. A tool that mixes them, or a reader that assumes one while the file uses the other, places data at addresses offset by a multiple of the segment size — which on this part usually lands writes outside the intended region, or wraps within it.

Failure one: the address that resets

An extended address record applies to the records after it, until another extended record appears. A file that changes the upper bits for the configuration area and never restores them will place subsequent data at the wrong address.

The visible symptom is a file whose data records are all valid and whose total length is correct, but whose contents, when loaded, put code where constants belong. The audit is straightforward: track the current upper bits while reading, and print the effective address of each record. Any jump in effective address that does not correspond to a region boundary is a bug.

Failure two: word addresses recorded as byte addresses

The core addresses words, and the programmer’s notion of an address may be in words while the file’s offsets are in bytes. A file produced with the wrong convention has every record at twice or half its correct address.

This one is more insidious than it sounds, because for the first few kilobytes the two conventions produce addresses that are both plausible. The divergence shows at the first region boundary — the point at which the two mappings disagree by more than the region’s size.

The test: load the file into a reader that shows the effective address map, and compare the boundaries against the part’s documented regions. A boundary that sits at an odd multiple of a documented value is a convention error.

Failure three: the regions that are not program memory

A complete file for this device contains more than program memory: configuration registers at their own address range, and an identification area. Some tools export them, some do not, and some export them at addresses that differ between tool versions.

A recovered file that omits configuration is a file that restores a device which boots into whatever configuration the destination part happened to carry. That is the single most common cause of a restored unit behaving differently from the original.

Region Contents Usually in an export
Program memory Code and tables Yes
Configuration Protection, oscillator, watchdog Sometimes
Data EEPROM Settings, calibration Rarely
Identification Part number, revision, unique ID No

The last row cannot be written at all, and it is the reason a recovered file can never fully describe a device.

Validating a recovered file

Four checks, in order, none of which take long:

  1. Record audit. Walk every record, track the effective address, and print the map. Region boundaries should match the documentation.
  2. Reset vector check. The word at the reset address should be a branch, and its target should be inside the program region.
  3. Fill check. Erased regions read as a uniform value. A region that is neither code nor uniform fill is either data or an error.
  4. Round trip. Program a blank device, read it back, and compare record for record. Discrepancies indicate address handling, not content.

Step four catches everything the first three miss, and it is the only one that tests the whole chain.

What “heximal” leaves out

A hex file is a memory image. It carries no information about:

  • which regions were readable and which were gated;
  • whether the source device was locked;
  • what the data array contained;
  • which silicon revision produced it.

Those four facts decide whether the file is a faithful record or a partial one, and none of them can be recovered from the file itself. They belong in the documentation that accompanies it, and their absence is why recovered files are so often misread years later.

Practical note on this part

Sixty-four pins and 64K of program memory mean a complete image is a substantial file, and a partial one is easy to mistake for a complete one. The regions beyond program memory are small, and their absence changes a file’s size by a few hundred bytes — an amount no one notices.

That is the argument for the record audit: it reports boundaries, and boundaries are where the truth is.

Related reading

FAQ

What is an extended linear address record? A record that sets the upper bits of the address for subsequent data records.

Can a hex file be valid and wrong? Yes. Checksums verify the record, not the address it lands at.

Why does a restored device boot differently? Usually because the file omitted configuration, and the destination’s own configuration applied.

Is the identification area in the file? No. It is factory-written and cannot be programmed.

How do I detect an address convention error? Compare the file’s region boundaries against the documented ones; an odd multiple indicates a word/byte mismatch.

Conclusion

Extended addressing is a small mechanism with three specific failure modes, all of which produce files that validate and mislead. A record audit, a reset-vector check, a fill check and a round trip resolve them in minutes — and the four facts a hex file cannot carry are the ones worth writing down before the file is filed away.

Comments are closed.