PostHeaderIcon Break Microcontroller PIC18F4220 Binary: Proving an Image Is Complete

A file arrives. Its length is plausible, it disassembles to something that looks like a program, and the customer is satisfied. Then the replacement board behaves oddly in one mode, and the question everyone asks is whether the extraction was complete. By then the answer is not recoverable from the file, because the file does not record what it omitted.

Completeness is a property that has to be established at extraction time and written down. On this device that means being explicit about the address map, about what a blank region means, and about which addresses the part does not implement at all.

The map, region by region

The address space of a PIC18F device is not uniform. It has implemented regions, unimplemented holes, and special areas that are addressed like memory but behave nothing like it. A file that treats the space as a single flat span will include the holes and will flatten the special areas into ordinary words.

Address range Content How a file usually represents it
low program space reset vector and code words
high program space code and tables words
unimplemented gap nothing, reads as a constant often filled with the erased pattern
identity area manufacturer identifier, revision sometimes included, usually not
configuration area configuration bytes included on some tools, dropped on others

The unimplemented gap is the first source of confusion. It reads back as a fixed pattern, which is indistinguishable from a region of the array that happens to be erased. A file that fills it silently looks identical to one that read real blank memory, and the length of the file no longer proves anything about the range that was actually covered.

The fix is a header, not a smarter tool. Every delivered image should state the range that was read, the range that was not read, and the range that does not exist.

Word width and the two-byte convention

An eighteen-series core uses sixteen-bit instruction words, so the natural file representation is two bytes per word, little-endian. That is the common convention and it is not the only one in use.

A mixed-convention image has a characteristic appearance: every other word looks plausible and the ones between them look like noise, or the disassembly is uniformly shifted. Both are instantly recognisable once seen and very confusing the first time.

Two habits prevent it. State the convention in the delivery. And verify alignment against a known structural feature — the reset vector’s jump target — before interpreting anything.

What blank actually means

Blank, in a flash device, means the cells are in their erased state. Reading a blank region returns the erased pattern, and that pattern is data: it marks the end of the code, and its position is a measurement of how much of the array the production line programmed.

Three distinct situations produce the same visible pattern, and they need to be told apart.

The first is genuinely blank space after the end of the program. This is normal and expected on a part that is only partly used.

The second is a region that was erased by a failed programming attempt. The pattern is identical to the first case, but the position is wrong — it sits in the middle of the address space, between two regions of valid code, which no linker would produce.

The third is a region that the extraction never covered. Here the tool filled the gap with the erased pattern to keep the file rectangular, and the pattern is not a measurement at all. Only the delivery header distinguishes this case from the other two.

Structural criteria that prove an image is a program

Four checks together give high confidence, and they work without any reference to the original source.

  1. The reset vector contains a jump whose target lies inside the implemented range and points at plausible initialisation code.
  2. The interrupt vectors contain jumps to addresses that are also reachable as call targets.
  3. The instruction stream decodes without a long run of illegal opcodes, other than in regions already identified as tables.
  4. The configuration bytes decode to a combination the silicon accepts.

A fifth check applies where the image contains tables: a computed jump followed by a run of return-with-literal instructions is a table, and the table’s extent should be consistent with the jump’s index range.

Failing any of these localises the defect to an address. That is much more useful than a checksum, which only says that the file it was computed from has not changed.

Checksums and what they can and cannot tell you

A checksum computed over the image is worth including, for one purpose: it lets a customer confirm a year later that the file they have is the file that was delivered. It says nothing about whether the extraction was correct.

A checksum computed by the device over its own array, and compared against one computed on the host over the received image, is a different thing and is genuinely useful. Agreement proves the transfer was faithful. It does not prove the array itself was fully read, because a device-side checksum over a truncated range is still self-consistent.

Both belong in the delivery, labelled distinctly, because conflating them is how a transfer check gets mistaken for a correctness proof.

Where the image stops being the whole story

An image is a snapshot of one device. A product is that device plus its board, and some of what determines behaviour lives outside the array.

Configuration bytes are inside the array’s address space but not inside the program. The identity area is read-only and cannot be copied, though recording it matters. External serial memories, if present, are not in the image at all and are sometimes where the per-unit data lives.

A delivery that names these explicitly saves the customer from concluding that the image is incomplete when in fact the job’s scope was narrower than the product.

The delivery, concretely

  • the image with its byte order and word width stated
  • the address range read, and any range excluded and why
  • the regions that do not exist on this part, marked as holes rather than filled
  • the configuration bytes decoded
  • the identity and revision words as read
  • a host-side checksum, and separately any device-side verification performed
  • a list of address ranges where the byte values are inferred rather than measured

That last line is unusual and it is the one that makes the document trustworthy. Every extraction has at least one such range, and naming it costs a sentence.

For the scope question of which regions belong in a copy at all, PIC18F4220 binary copying covers the three-region split. On the smaller mid-range device where the address space has fewer holes and the same criteria apply more simply, PIC16F616 heximal recovery is the closer comparison. And where the array is stored as charge rather than as flash, the completeness question acquires a second dimension, covered in PIC16C74B binary reading.

One habit worth adopting

State the range before stating the result. A one-line header written at the start of the job — this device, this address range, this protection state — is the single artefact that prevents most of the arguments that follow a delivery. It takes twenty seconds and it answers the question that otherwise cannot be answered once the device has left the bench.

Comments are closed.