PostHeaderIcon Break IC PIC12F609 Heximal: The File Format Is Part of the Result

Extraction produces bytes in memory. Delivery produces a file, and the file is not a neutral container — it is a specification that says where each byte belongs, how it is grouped, and whether it can be trusted. On a small device the file is short enough to inspect by eye, and doing so catches most of the errors that matter.

This part holds around a thousand instruction words, which is a few kilobytes of file. That size makes the format question unusually tractable: the whole thing fits on a screen, and a wrong convention is visible rather than buried.

The record structure

The format in common use is a text format of one line per record. Each line begins with a colon and carries a fixed sequence of fields.

Field Width Meaning
byte count one byte how many data bytes follow
address two bytes where those bytes go, big-endian
record type one byte what kind of record this is
data as counted the payload, often empty
checksum one byte two’s complement of the sum of all preceding bytes

Four record types appear in practice: a data record, an end-of-file record, an extended address record that supplies the upper bits of a large address, and a start-address record that a microcontroller loader usually ignores. On a device this small only the first two are needed, and a file that contains others is either produced by a tool with assumptions about a larger part or has been through a converter.

The checksum is simple to verify and worth verifying: sum every byte from the count through the last data byte, take the low eight bits, and the checksum should be the value that makes the total zero. A file with a bad checksum on any line is a file that was edited by hand and not recomputed — which is common, and which is exactly the file you want to know about before programming it.

Word mapping is where images get corrupted

The instruction word on this core is fourteen bits. A file carries bytes. Two conventions bridge the gap, and they are not interchangeable.

The first stores each word as two bytes with the low byte first and the top two bits of the high byte unused. The second uses a packed representation where the fourteen-bit words are concatenated. The first is what most tools expect for this family; the second appears when an image has been round-tripped through a converter designed for a different architecture.

The failure mode is quiet. A file with the wrong convention has the right length, passes its own checksums, and loads. What it programs is a set of words in which every pair has been swapped, which disassembles into something that superficially resembles code and runs for a few instructions.

The test is the first word. On a working image the reset vector is a jump, and its encoding is not symmetric between the two conventions. Checking that word 0 decodes to a jump before anything else takes one line of inspection and settles the question.

Address convention: bytes or words

The next trap is the address field. Some tools record the address of a byte in the file; others record the address of a word in the device. The two differ by a factor of two, and mixing them puts the whole image at twice or half its intended location.

The tell is the end-of-file address relative to the configuration word’s known position. On this family the configuration word lives at the top of the map, and an image whose last data record lands at a value consistent with that position is using the correct convention. One that lands at twice the value is not.

What the file should carry beyond the program

Three additions make a delivered file self-describing, and none of them is standard.

A header comment stating the device, the word-width convention and the address convention costs three lines and removes all ambiguity. A separate small file carrying the configuration word, decoded, prevents the most common programming error — omitting it because the tool’s default length excluded it. And a plain-text listing of the addresses that were not read, or that read as blank, prevents a later reader from assuming the array ended where the file ended.

Verifying before delivery

The verification is fast enough that there is no reason to skip it.

  1. Every record’s checksum recomputes correctly.
  2. Word 0 decodes to a jump into the implemented range.
  3. The last data record’s address is consistent with the configuration word’s position.
  4. Reloading the file into a programmer’s buffer and comparing against the original read gives an identical byte sequence.
  5. The decoded configuration word is a combination the silicon accepts.

Step four is the one that catches convention errors that survive the first three, because it exercises the same path a customer will use.

Why the format matters more on small devices

On a large image, a format error affects a fraction of the content and is likely to be caught by structural checks. Here, a swapped word order affects every word, and a wrong address convention moves the entire program. There is no redundancy to absorb the mistake.

The compensating advantage is that the whole file can be inspected. A person can read every line of a file this size, and the checks above take less time than setting up the programmer.

The delivery

  • the image file, with convention stated in a header
  • the configuration word as a separate decoded item
  • a note on any address range that was blank, absent or unread
  • the identifier and revision words
  • a host-side checksum, labelled as a transfer check rather than a correctness proof

For the same eight-pin family seen from the design-constraint side, where the stack depth rather than the file format is the subject, PIC12F615 software recovery covers the code. Where the device also carries an encryption peripheral that must be handled before the image is complete, PIC12F635 program recovery adds that step. And for a mid-range part of the same generation where the image is four times larger and the record types become necessary rather than optional, PIC16F684 binary recovery provides the comparison.

A small discipline with a large return

State the convention before stating the content. It is one sentence in a delivery note, it takes twenty seconds to write, and it prevents an entire category of failure in which a correct extraction is rejected as faulty because the file that carried it spoke a slightly different dialect than the tool that received it.

Comments are closed.